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 {
|
|
|
|
SettingCreatedEvent,
|
|
|
|
SettingDeletedEvent,
|
|
|
|
SettingUpdatedEvent,
|
|
|
|
} from '../types/events';
|
2023-09-27 15:23:05 +02:00
|
|
|
import EventService from './event-service';
|
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
|
|
|
|
2023-09-27 15:23:05 +02:00
|
|
|
private eventService: EventService;
|
2022-07-21 15:40:31 +02:00
|
|
|
|
2021-04-22 22:54:08 +02:00
|
|
|
constructor(
|
2023-09-27 15:23:05 +02:00
|
|
|
{ settingStore }: Pick<IUnleashStores, 'settingStore'>,
|
2022-08-26 09:09:48 +02:00
|
|
|
config: IUnleashConfig,
|
2023-09-27 15:23:05 +02:00
|
|
|
eventService: EventService,
|
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;
|
2023-09-27 15:23:05 +02:00
|
|
|
this.eventService = eventService;
|
2021-04-22 22:54:08 +02:00
|
|
|
}
|
|
|
|
|
2023-10-31 12:38:21 +01:00
|
|
|
/**
|
|
|
|
* @deprecated use getWithDefault instead
|
|
|
|
*/
|
|
|
|
async get<T>(id: string, defaultValue?: T): Promise<T | undefined> {
|
|
|
|
const value = await this.settingStore.get<T>(id);
|
|
|
|
return value || defaultValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
async getWithDefault<T>(id: string, defaultValue: T): Promise<T> {
|
|
|
|
const value = await this.settingStore.get<T>(id);
|
2022-12-14 17:35:22 +01:00
|
|
|
return value || 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> {
|
|
|
|
const exists = await this.settingStore.exists(id);
|
|
|
|
if (exists) {
|
|
|
|
await this.settingStore.updateRow(id, value);
|
2023-09-27 15:23:05 +02:00
|
|
|
await this.eventService.storeEvent(
|
2022-07-21 15:40:31 +02:00
|
|
|
new SettingUpdatedEvent({
|
|
|
|
createdBy,
|
|
|
|
data: { id },
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
await this.settingStore.insert(id, value);
|
2023-09-27 15:23:05 +02:00
|
|
|
await this.eventService.storeEvent(
|
2022-07-21 15:40:31 +02:00
|
|
|
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> {
|
|
|
|
await this.settingStore.delete(id);
|
2023-09-27 15:23:05 +02:00
|
|
|
await this.eventService.storeEvent(
|
2022-07-21 15:40:31 +02:00
|
|
|
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> {
|
|
|
|
await this.settingStore.deleteAll();
|
|
|
|
}
|
2021-04-22 22:54:08 +02:00
|
|
|
}
|