2024-10-03 11:27:31 +02:00
|
|
|
import type { AllAppConfig, AppConfigRawResponse } from '$lib/types/application-configuration';
|
2025-06-16 15:59:14 +02:00
|
|
|
import { cachedApplicationLogo, cachedBackgroundImage } from '$lib/utils/cached-image-util';
|
2024-08-12 11:00:25 +02:00
|
|
|
import APIService from './api-service';
|
|
|
|
|
|
2024-09-09 10:29:41 +02:00
|
|
|
export default class AppConfigService extends APIService {
|
2024-08-12 11:00:25 +02:00
|
|
|
async list(showAll = false) {
|
2024-08-13 20:51:10 +02:00
|
|
|
let url = '/application-configuration';
|
|
|
|
|
if (showAll) {
|
|
|
|
|
url += '/all';
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-09 10:29:41 +02:00
|
|
|
const { data } = await this.api.get<AppConfigRawResponse>(url);
|
2025-01-19 15:30:31 +01:00
|
|
|
return this.parseConfigList(data);
|
2024-08-12 11:00:25 +02:00
|
|
|
}
|
|
|
|
|
|
2024-09-09 10:29:41 +02:00
|
|
|
async update(appConfig: AllAppConfig) {
|
2025-08-22 06:25:02 -06:00
|
|
|
// Convert all values to string, stringifying JSON where needed
|
|
|
|
|
const appConfigConvertedToString: Record<string, string> = {};
|
2024-10-25 21:33:54 +02:00
|
|
|
for (const key in appConfig) {
|
2025-08-22 06:25:02 -06:00
|
|
|
const value = (appConfig as any)[key];
|
|
|
|
|
if (typeof value === 'object' && value !== null) {
|
|
|
|
|
appConfigConvertedToString[key] = JSON.stringify(value);
|
|
|
|
|
} else {
|
|
|
|
|
appConfigConvertedToString[key] = String(value);
|
|
|
|
|
}
|
2024-10-25 21:33:54 +02:00
|
|
|
}
|
|
|
|
|
const res = await this.api.put('/application-configuration', appConfigConvertedToString);
|
2025-01-19 15:30:31 +01:00
|
|
|
return this.parseConfigList(res.data);
|
2024-08-12 11:00:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async updateFavicon(favicon: File) {
|
|
|
|
|
const formData = new FormData();
|
|
|
|
|
formData.append('file', favicon!);
|
|
|
|
|
|
2025-09-20 21:42:52 +02:00
|
|
|
await this.api.put(`/application-images/favicon`, formData);
|
2024-08-12 11:00:25 +02:00
|
|
|
}
|
|
|
|
|
|
2024-10-03 11:27:31 +02:00
|
|
|
async updateLogo(logo: File, light = true) {
|
2024-08-12 11:00:25 +02:00
|
|
|
const formData = new FormData();
|
|
|
|
|
formData.append('file', logo!);
|
|
|
|
|
|
2025-09-20 21:42:52 +02:00
|
|
|
await this.api.put(`/application-images/logo`, formData, {
|
2024-10-03 11:27:31 +02:00
|
|
|
params: { light }
|
|
|
|
|
});
|
2025-06-16 15:59:14 +02:00
|
|
|
cachedApplicationLogo.bustCache(light);
|
2024-08-12 11:00:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async updateBackgroundImage(backgroundImage: File) {
|
|
|
|
|
const formData = new FormData();
|
|
|
|
|
formData.append('file', backgroundImage!);
|
|
|
|
|
|
2025-09-20 21:42:52 +02:00
|
|
|
await this.api.put(`/application-images/background`, formData);
|
2025-06-16 15:59:14 +02:00
|
|
|
cachedBackgroundImage.bustCache();
|
2024-08-12 11:00:25 +02:00
|
|
|
}
|
2024-10-18 20:48:59 +02:00
|
|
|
|
2024-11-21 18:24:01 +01:00
|
|
|
async sendTestEmail() {
|
|
|
|
|
await this.api.post('/application-configuration/test-email');
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-19 06:02:07 -06:00
|
|
|
async syncLdap() {
|
|
|
|
|
await this.api.post('/application-configuration/sync-ldap');
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-19 15:30:31 +01:00
|
|
|
private parseConfigList(data: AppConfigRawResponse) {
|
|
|
|
|
const appConfig: Partial<AllAppConfig> = {};
|
|
|
|
|
data.forEach(({ key, value }) => {
|
|
|
|
|
(appConfig as any)[key] = this.parseValue(value);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return appConfig as AllAppConfig;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-25 21:33:54 +02:00
|
|
|
private parseValue(value: string) {
|
2025-08-22 06:25:02 -06:00
|
|
|
// Try to parse JSON first
|
|
|
|
|
try {
|
|
|
|
|
const parsed = JSON.parse(value);
|
|
|
|
|
if (typeof parsed === 'object' && parsed !== null) {
|
|
|
|
|
return parsed;
|
|
|
|
|
}
|
|
|
|
|
value = String(parsed);
|
|
|
|
|
} catch {}
|
|
|
|
|
|
|
|
|
|
// Handle rest of the types
|
2024-10-25 21:33:54 +02:00
|
|
|
if (value === 'true') {
|
|
|
|
|
return true;
|
|
|
|
|
} else if (value === 'false') {
|
|
|
|
|
return false;
|
2025-02-18 21:36:08 +01:00
|
|
|
} else if (/^-?\d+(\.\d+)?$/.test(value)) {
|
2024-10-26 00:15:31 +02:00
|
|
|
return parseFloat(value);
|
2024-10-25 21:33:54 +02:00
|
|
|
} else {
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-12 11:00:25 +02:00
|
|
|
}
|