2021-04-12 10:45:41 +02:00
|
|
|
import apiClient from './clients/api-client';
|
2020-11-23 17:36:33 +01:00
|
|
|
import { AxiosResponse } from 'axios';
|
2021-07-01 12:04:48 +02:00
|
|
|
import { Setting, SettingBulkResult, SettingError, SettingName, SettingValue } from '../models/setting';
|
2020-11-23 17:36:33 +01:00
|
|
|
|
|
|
|
export default class SettingAPI {
|
2021-06-17 17:08:22 +02:00
|
|
|
static async get (name: SettingName): Promise<Setting> {
|
2021-06-01 11:24:43 +02:00
|
|
|
const res: AxiosResponse<{setting: Setting}> = await apiClient.get(`/api/settings/${name}`);
|
2020-11-23 17:36:33 +01:00
|
|
|
return res?.data?.setting;
|
|
|
|
}
|
|
|
|
|
2021-07-01 12:04:48 +02:00
|
|
|
static async query (names: Array<SettingName>): Promise<Map<SettingName, string>> {
|
2021-03-30 15:56:36 +02:00
|
|
|
const params = new URLSearchParams();
|
|
|
|
params.append('names', `['${names.join("','")}']`);
|
|
|
|
|
|
|
|
const res: AxiosResponse = await apiClient.get(`/api/settings?${params.toString()}`);
|
2021-05-18 16:31:19 +02:00
|
|
|
return SettingAPI.toSettingsMap(names, res?.data);
|
2020-11-24 16:26:18 +01:00
|
|
|
}
|
|
|
|
|
2021-07-01 12:04:48 +02:00
|
|
|
static async update (name: SettingName, value: SettingValue): Promise<Setting> {
|
2021-04-07 16:21:12 +02:00
|
|
|
const res: AxiosResponse = await apiClient.patch(`/api/settings/${name}`, { setting: { value } });
|
2021-07-01 12:04:48 +02:00
|
|
|
if (res.status === 304) { return { name, value: `${value}` }; }
|
|
|
|
return res?.data?.setting;
|
2021-04-07 16:21:12 +02:00
|
|
|
}
|
|
|
|
|
2021-07-01 12:04:48 +02:00
|
|
|
static async bulkUpdate (settings: Map<SettingName, SettingValue>, transactional = false): Promise<Map<SettingName, SettingBulkResult>> {
|
2021-06-10 10:39:42 +02:00
|
|
|
const res: AxiosResponse = await apiClient.patch(`/api/settings/bulk_update?transactional=${transactional}`, { settings: SettingAPI.toObjectArray(settings) });
|
2021-03-30 15:56:36 +02:00
|
|
|
return SettingAPI.toBulkMap(res?.data?.settings);
|
|
|
|
}
|
|
|
|
|
2021-06-17 17:08:22 +02:00
|
|
|
static async isPresent (name: SettingName): Promise<boolean> {
|
2021-04-06 17:47:47 +02:00
|
|
|
const res: AxiosResponse = await apiClient.get(`/api/settings/is_present/${name}`);
|
|
|
|
return res?.data?.isPresent;
|
|
|
|
}
|
|
|
|
|
2021-07-01 12:04:48 +02:00
|
|
|
private static toSettingsMap (names: Array<SettingName>, data: Record<string, string|null>): Map<SettingName, string> {
|
2021-03-24 17:31:50 +01:00
|
|
|
const map = new Map();
|
2021-05-18 16:31:19 +02:00
|
|
|
names.forEach(name => {
|
|
|
|
map.set(name, data[name] || '');
|
2021-03-24 17:31:50 +01:00
|
|
|
});
|
|
|
|
return map;
|
|
|
|
}
|
2021-03-30 15:56:36 +02:00
|
|
|
|
2021-07-01 12:04:48 +02:00
|
|
|
private static toBulkMap (data: Array<Setting|SettingError>): Map<SettingName, SettingBulkResult> {
|
2021-03-30 15:56:36 +02:00
|
|
|
const map = new Map();
|
|
|
|
data.forEach(item => {
|
|
|
|
const itemData: SettingBulkResult = { status: true };
|
|
|
|
if ('error' in item) {
|
|
|
|
itemData.error = item.error;
|
|
|
|
itemData.status = false;
|
|
|
|
}
|
|
|
|
if ('value' in item) {
|
|
|
|
itemData.value = item.value;
|
|
|
|
}
|
2021-06-10 10:39:42 +02:00
|
|
|
if ('localized' in item) {
|
|
|
|
itemData.localized = item.localized;
|
|
|
|
}
|
2021-03-30 15:56:36 +02:00
|
|
|
|
2022-08-23 13:12:13 +02:00
|
|
|
map.set(item.name, itemData);
|
2021-03-30 15:56:36 +02:00
|
|
|
});
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
2021-07-01 12:04:48 +02:00
|
|
|
private static toObjectArray (data: Map<SettingName, SettingValue>): Array<Record<string, SettingValue>> {
|
2021-03-30 15:56:36 +02:00
|
|
|
const array = [];
|
|
|
|
data.forEach((value, key) => {
|
|
|
|
array.push({
|
|
|
|
name: key,
|
|
|
|
value
|
2021-07-01 12:04:48 +02:00
|
|
|
});
|
2021-03-30 15:56:36 +02:00
|
|
|
});
|
|
|
|
return array;
|
|
|
|
}
|
2020-11-23 17:36:33 +01:00
|
|
|
}
|