2021-04-22 22:54:08 +02:00
|
|
|
import { IUnleashConfig } from '../types/option';
|
|
|
|
import { IUnleashStores } from '../types/stores';
|
|
|
|
import { Logger } from '../logger';
|
2021-08-12 15:04:37 +02:00
|
|
|
import { ISettingStore } from '../types/stores/settings-store';
|
2022-07-21 15:40:31 +02:00
|
|
|
import { IEventStore } from '../types/stores/event-store';
|
|
|
|
import {
|
|
|
|
SettingCreatedEvent,
|
|
|
|
SettingDeletedEvent,
|
|
|
|
SettingUpdatedEvent,
|
|
|
|
} from '../types/events';
|
2022-08-26 09:09:48 +02:00
|
|
|
import { validateOrigins } from '../util/validateOrigin';
|
|
|
|
import {
|
|
|
|
FrontendSettings,
|
|
|
|
frontendSettingsKey,
|
|
|
|
} from '../types/settings/frontend-settings';
|
|
|
|
import BadDataError from '../error/bad-data-error';
|
2021-04-22 22:54:08 +02:00
|
|
|
|
|
|
|
export default class SettingService {
|
2022-08-26 09:09:48 +02:00
|
|
|
private config: IUnleashConfig;
|
|
|
|
|
2021-04-22 22:54:08 +02:00
|
|
|
private logger: Logger;
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
private settingStore: ISettingStore;
|
2021-04-22 22:54:08 +02:00
|
|
|
|
2022-07-21 15:40:31 +02:00
|
|
|
private eventStore: IEventStore;
|
|
|
|
|
2022-08-26 09:09:48 +02:00
|
|
|
// SettingService.getFrontendSettings is called on every request to the
|
|
|
|
// frontend API. Keep fetched settings in a cache for fewer DB queries.
|
|
|
|
private cache = new Map<string, unknown>();
|
|
|
|
|
2021-04-22 22:54:08 +02:00
|
|
|
constructor(
|
2022-07-21 15:40:31 +02:00
|
|
|
{
|
|
|
|
settingStore,
|
|
|
|
eventStore,
|
|
|
|
}: Pick<IUnleashStores, 'settingStore' | 'eventStore'>,
|
2022-08-26 09:09:48 +02:00
|
|
|
config: IUnleashConfig,
|
2021-04-22 22:54:08 +02:00
|
|
|
) {
|
2022-08-26 09:09:48 +02:00
|
|
|
this.config = config;
|
|
|
|
this.logger = config.getLogger('services/setting-service.ts');
|
2021-04-22 22:54:08 +02:00
|
|
|
this.settingStore = settingStore;
|
2022-07-21 15:40:31 +02:00
|
|
|
this.eventStore = eventStore;
|
2021-04-22 22:54:08 +02:00
|
|
|
}
|
|
|
|
|
2022-08-26 09:09:48 +02:00
|
|
|
async get<T>(id: string, defaultValue?: T): Promise<T> {
|
|
|
|
if (!this.cache.has(id)) {
|
|
|
|
this.cache.set(id, await this.settingStore.get(id));
|
|
|
|
}
|
|
|
|
return (this.cache.get(id) as T) || defaultValue;
|
2021-04-22 22:54:08 +02:00
|
|
|
}
|
|
|
|
|
2022-07-21 15:40:31 +02:00
|
|
|
async insert(id: string, value: object, createdBy: string): Promise<void> {
|
2022-08-26 09:09:48 +02:00
|
|
|
this.cache.delete(id);
|
2022-07-21 15:40:31 +02:00
|
|
|
const exists = await this.settingStore.exists(id);
|
|
|
|
if (exists) {
|
|
|
|
await this.settingStore.updateRow(id, value);
|
|
|
|
await this.eventStore.store(
|
|
|
|
new SettingUpdatedEvent({
|
|
|
|
createdBy,
|
|
|
|
data: { id },
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
await this.settingStore.insert(id, value);
|
|
|
|
await this.eventStore.store(
|
|
|
|
new SettingCreatedEvent({
|
|
|
|
createdBy,
|
|
|
|
data: { id },
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
2021-04-22 22:54:08 +02:00
|
|
|
}
|
2021-08-12 15:04:37 +02:00
|
|
|
|
2022-07-21 15:40:31 +02:00
|
|
|
async delete(id: string, createdBy: string): Promise<void> {
|
2022-08-26 09:09:48 +02:00
|
|
|
this.cache.delete(id);
|
2022-07-21 15:40:31 +02:00
|
|
|
await this.settingStore.delete(id);
|
|
|
|
await this.eventStore.store(
|
|
|
|
new SettingDeletedEvent({
|
|
|
|
createdBy,
|
|
|
|
data: {
|
|
|
|
id,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
2021-08-12 15:04:37 +02:00
|
|
|
}
|
2022-08-26 09:09:48 +02:00
|
|
|
|
|
|
|
async deleteAll(): Promise<void> {
|
|
|
|
this.cache.clear();
|
|
|
|
await this.settingStore.deleteAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
async setFrontendSettings(
|
|
|
|
value: FrontendSettings,
|
|
|
|
createdBy: string,
|
|
|
|
): Promise<void> {
|
|
|
|
const error = validateOrigins(value.frontendApiOrigins);
|
|
|
|
if (error) {
|
|
|
|
throw new BadDataError(error);
|
|
|
|
}
|
|
|
|
await this.insert(frontendSettingsKey, value, createdBy);
|
|
|
|
}
|
|
|
|
|
|
|
|
async getFrontendSettings(): Promise<FrontendSettings> {
|
|
|
|
return this.get(frontendSettingsKey, {
|
|
|
|
frontendApiOrigins: this.config.frontendApiOrigins,
|
|
|
|
});
|
|
|
|
}
|
2021-04-22 22:54:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = SettingService;
|