refactor(configuration): moved Groups to configuration folder
This commit is contained in:
parent
9155f630d9
commit
863ae3fab2
8 changed files with 19 additions and 19 deletions
|
|
@ -0,0 +1,77 @@
|
|||
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 <ChannelId>configValue.value;
|
||||
case TransformerType.PermissionBoolean:
|
||||
return configValue.value === '1';
|
||||
case TransformerType.String:
|
||||
return configValue.value;
|
||||
}
|
||||
}
|
||||
|
||||
public getTransformerType(configKey: string): Nullable<TransformerType> {
|
||||
const path = configKey.split('.');
|
||||
return GroupConfigurationTransformers.TRANSFORMERS.find(
|
||||
transformer => {
|
||||
return ArrayUtils.arraysEqual(transformer.path, path);
|
||||
}
|
||||
)?.type;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue