update set config logic to change default prefered admin if a value is set and is not included in the current admins list

This commit is contained in:
CyferShepard
2025-05-01 14:35:51 +02:00
parent a4f0d07aaf
commit 833e8852eb
5 changed files with 55 additions and 29 deletions

View File

@@ -104,8 +104,8 @@ class EmbyAPI {
//Functions
async getUsers() {
if (!this.configReady) {
async getUsers(refreshConfig = false) {
if (!this.configReady || refreshConfig) {
const success = await this.#fetchConfig();
if (!success) {
return [];
@@ -133,9 +133,9 @@ class EmbyAPI {
}
}
async getAdmins() {
async getAdmins(refreshConfig = false) {
try {
const users = await this.getUsers();
const users = await this.getUsers(refreshConfig);
return users?.filter((user) => user.Policy.IsAdministrator) || [];
} catch (error) {
this.#errorHandler(error);

View File

@@ -105,8 +105,8 @@ class JellyfinAPI {
//Functions
async getUsers() {
if (!this.configReady) {
async getUsers(refreshConfig = false) {
if (!this.configReady || refreshConfig) {
const success = await this.#fetchConfig();
if (!success) {
return [];
@@ -133,9 +133,9 @@ class JellyfinAPI {
}
}
async getAdmins() {
async getAdmins(refreshConfig = false) {
try {
const users = await this.getUsers();
const users = await this.getUsers(refreshConfig);
return users?.filter((user) => user.Policy.IsAdministrator) || [];
} catch (error) {
this.#errorHandler(error);

View File

@@ -463,7 +463,24 @@ router.post("/setconfig", async (req, res) => {
settings.ServerID = systemInfo?.Id || null;
let query = 'UPDATE app_config SET settings=$1 where "ID"=1';
const query = 'UPDATE app_config SET settings=$1 where "ID"=1';
await db.query(query, [settings]);
}
}
const admins = await API.getAdmins(true);
const preferredAdmin = await new configClass().getPreferedAdmin();
if (admins && admins.length > 0 && preferredAdmin && !admins.map((item) => item.Id).includes(preferredAdmin)) {
const newAdmin = admins[0];
const settingsjson = await db.query('SELECT settings FROM app_config where "ID"=1').then((res) => res.rows);
if (settingsjson.length > 0) {
const settings = settingsjson[0].settings || {};
settings.preferred_admin = { userid: newAdmin.Id, username: newAdmin.Name };
const query = 'UPDATE app_config SET settings=$1 where "ID"=1';
await db.query(query, [settings]);
}

View File

@@ -148,7 +148,7 @@ router.get("/getSessions", async (req, res) => {
router.get("/getAdminUsers", async (req, res) => {
try {
const adminUser = await API.getAdmins();
const adminUser = await API.getAdmins(true);
res.send(adminUser);
} catch (error) {
res.status(503);

View File

@@ -44,6 +44,20 @@ export default function SettingsConfig() {
set12hr(Boolean(storage_12hr));
}
const fetchAdmins = async () => {
try {
const adminData = await axios.get(`/proxy/getAdminUsers`, {
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "application/json",
},
});
setAdmins(adminData.data);
} catch (error) {
console.log(error);
}
};
useEffect(() => {
Config.getConfig()
.then((config) => {
@@ -59,20 +73,6 @@ export default function SettingsConfig() {
setsubmissionMessage("Error Retrieving Configuration. Unable to contact Backend Server");
});
const fetchAdmins = async () => {
try {
const adminData = await axios.get(`/proxy/getAdminUsers`, {
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "application/json",
},
});
setAdmins(adminData.data);
} catch (error) {
console.log(error);
}
};
fetchAdmins();
}, [token]);
@@ -91,6 +91,8 @@ export default function SettingsConfig() {
console.log("Config updated successfully:", response.data);
setisSubmitted("Success");
setsubmissionMessage("Successfully updated configuration");
Config.setConfig();
fetchAdmins();
})
.catch((error) => {
let errorMessage = error.response.data.errorMessage;
@@ -98,7 +100,6 @@ export default function SettingsConfig() {
setisSubmitted("Failed");
setsubmissionMessage(`Error Updating Configuration: ${errorMessage}`);
});
Config.setConfig();
}
async function handleFormSubmitExternal(event) {
@@ -233,9 +234,13 @@ export default function SettingsConfig() {
</Form.Group>
{isSubmitted !== "" ? (
isSubmitted === "Failed" ? (
<Alert bg="dark" data-bs-theme="dark" variant="danger">{submissionMessage}</Alert>
<Alert bg="dark" data-bs-theme="dark" variant="danger">
{submissionMessage}
</Alert>
) : (
<Alert bg="dark" data-bs-theme="dark" variant="success">{submissionMessage}</Alert>
<Alert bg="dark" data-bs-theme="dark" variant="success">
{submissionMessage}
</Alert>
)
) : (
<></>
@@ -265,9 +270,13 @@ export default function SettingsConfig() {
{isSubmittedExternal !== "" ? (
isSubmittedExternal === "Failed" ? (
<Alert bg="dark" data-bs-theme="dark" variant="danger">{submissionMessageExternal}</Alert>
<Alert bg="dark" data-bs-theme="dark" variant="danger">
{submissionMessageExternal}
</Alert>
) : (
<Alert bg="dark" data-bs-theme="dark" variant="success">{submissionMessageExternal}</Alert>
<Alert bg="dark" data-bs-theme="dark" variant="success">
{submissionMessageExternal}
</Alert>
)
) : (
<></>