2023-06-16 21:54:17 +02:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2023-10-09 16:25:03 +02:00
|
|
|
import { JobName } from '../job';
|
|
|
|
import { CommunicationEvent, ICommunicationRepository, IJobRepository, ISystemConfigRepository } from '../repositories';
|
2023-10-26 00:13:05 +02:00
|
|
|
import { SystemConfigThemeDto } from './dto/system-config-theme.dto';
|
2023-09-04 21:45:59 +02:00
|
|
|
import { SystemConfigDto, mapConfig } from './dto/system-config.dto';
|
2023-06-16 21:54:17 +02:00
|
|
|
import { SystemConfigTemplateStorageOptionDto } from './response-dto/system-config-template-storage-option.dto';
|
2023-01-21 17:11:55 +01:00
|
|
|
import {
|
|
|
|
supportedDayTokens,
|
|
|
|
supportedHourTokens,
|
|
|
|
supportedMinuteTokens,
|
|
|
|
supportedMonthTokens,
|
|
|
|
supportedPresetTokens,
|
|
|
|
supportedSecondTokens,
|
2023-09-28 19:47:31 +02:00
|
|
|
supportedWeekTokens,
|
2023-01-21 17:11:55 +01:00
|
|
|
supportedYearTokens,
|
2023-01-24 05:13:42 +01:00
|
|
|
} from './system-config.constants';
|
2023-01-21 17:11:55 +01:00
|
|
|
import { SystemConfigCore, SystemConfigValidator } from './system-config.core';
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class SystemConfigService {
|
|
|
|
private core: SystemConfigCore;
|
|
|
|
constructor(
|
|
|
|
@Inject(ISystemConfigRepository) repository: ISystemConfigRepository,
|
2023-10-06 09:01:14 +02:00
|
|
|
@Inject(ICommunicationRepository) private communicationRepository: ICommunicationRepository,
|
2023-02-25 15:12:03 +01:00
|
|
|
@Inject(IJobRepository) private jobRepository: IJobRepository,
|
2023-01-21 17:11:55 +01:00
|
|
|
) {
|
2023-10-09 02:51:03 +02:00
|
|
|
this.core = SystemConfigCore.create(repository);
|
2023-01-21 17:11:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
get config$() {
|
|
|
|
return this.core.config$;
|
|
|
|
}
|
|
|
|
|
2023-10-26 00:13:05 +02:00
|
|
|
async getTheme(): Promise<SystemConfigThemeDto> {
|
|
|
|
const { theme } = await this.core.getConfig();
|
|
|
|
return theme;
|
|
|
|
}
|
|
|
|
|
2023-01-21 17:11:55 +01:00
|
|
|
async getConfig(): Promise<SystemConfigDto> {
|
|
|
|
const config = await this.core.getConfig();
|
|
|
|
return mapConfig(config);
|
|
|
|
}
|
|
|
|
|
|
|
|
getDefaults(): SystemConfigDto {
|
|
|
|
const config = this.core.getDefaults();
|
|
|
|
return mapConfig(config);
|
|
|
|
}
|
|
|
|
|
|
|
|
async updateConfig(dto: SystemConfigDto): Promise<SystemConfigDto> {
|
|
|
|
const config = await this.core.updateConfig(dto);
|
2023-02-25 15:12:03 +01:00
|
|
|
await this.jobRepository.queue({ name: JobName.SYSTEM_CONFIG_CHANGE });
|
2023-10-06 09:01:14 +02:00
|
|
|
this.communicationRepository.broadcast(CommunicationEvent.CONFIG_UPDATE, {});
|
2023-01-21 17:11:55 +01:00
|
|
|
return mapConfig(config);
|
|
|
|
}
|
|
|
|
|
|
|
|
async refreshConfig() {
|
|
|
|
await this.core.refreshConfig();
|
2023-05-26 21:43:24 +02:00
|
|
|
return true;
|
2023-01-21 17:11:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
addValidator(validator: SystemConfigValidator) {
|
|
|
|
this.core.addValidator(validator);
|
|
|
|
}
|
|
|
|
|
|
|
|
getStorageTemplateOptions(): SystemConfigTemplateStorageOptionDto {
|
|
|
|
const options = new SystemConfigTemplateStorageOptionDto();
|
|
|
|
|
|
|
|
options.dayOptions = supportedDayTokens;
|
2023-09-28 19:47:31 +02:00
|
|
|
options.weekOptions = supportedWeekTokens;
|
2023-01-21 17:11:55 +01:00
|
|
|
options.monthOptions = supportedMonthTokens;
|
|
|
|
options.yearOptions = supportedYearTokens;
|
|
|
|
options.hourOptions = supportedHourTokens;
|
|
|
|
options.secondOptions = supportedSecondTokens;
|
|
|
|
options.minuteOptions = supportedMinuteTokens;
|
|
|
|
options.presetOptions = supportedPresetTokens;
|
|
|
|
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
}
|