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';
|
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;
|
|
|
|
|
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> {
|
2022-12-14 17:35:22 +01:00
|
|
|
const value = await this.settingStore.get(id);
|
|
|
|
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);
|
|
|
|
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> {
|
|
|
|
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> {
|
|
|
|
await this.settingStore.deleteAll();
|
|
|
|
}
|
2021-04-22 22:54:08 +02:00
|
|
|
}
|