import {ChannelId} from "../types/DiscordTypes"; import {GroupConfigurationModel} from "../Database/Models/GroupConfigurationModel"; import {Nullable} from "../types/Nullable"; import {ArrayUtils} from "../Utilities/ArrayUtils"; export enum TransformerType { Locale, Channel, PermissionBoolean, String } type GroupConfigurationTransformer = { path: string[]; type: TransformerType, } export type GroupConfigurationResult = ChannelId | Intl.Locale | boolean | string | null export class GroupConfigurationTransformers { static TRANSFORMERS: GroupConfigurationTransformer[] = [ { path: ['channels', 'newPlaydates'], type: TransformerType.Channel, }, { path: ['channels', 'playdateReminders'], type: TransformerType.Channel, }, { path: ['permissions', 'allowMemberManagingPlaydates'], type: TransformerType.PermissionBoolean }, { path: ['calendar', 'title'], type: TransformerType.String }, { path: ['calendar', 'description'], type: TransformerType.String, }, { path: ['calendar', 'location'], type: TransformerType.String } ]; public getValue(configValue: GroupConfigurationModel): GroupConfigurationResult { const transformerType = this.getTransformerType(configValue.key); if (transformerType === undefined || transformerType === null) { throw new Error(`Can't find transformer for ${configValue.key}`); } switch (transformerType) { case TransformerType.Locale: return new Intl.Locale(configValue.value) case TransformerType.Channel: return configValue.value; case TransformerType.PermissionBoolean: return configValue.value === '1'; case TransformerType.String: return configValue.value; } } public getTransformerType(configKey: string): Nullable { const path = configKey.split('.'); return GroupConfigurationTransformers.TRANSFORMERS.find( transformer => { return ArrayUtils.arraysEqual(transformer.path, path); } )?.type; } }