refactor(configuration): Setup configuration and menu to be reuseable
This commit is contained in:
parent
863ae3fab2
commit
d46bbd84c5
21 changed files with 551 additions and 452 deletions
104
source/Configuration/Groups/GroupConfigurationProvider.ts
Normal file
104
source/Configuration/Groups/GroupConfigurationProvider.ts
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
import {ConfigurationProvider} from "../ConfigurationProvider";
|
||||
import {GroupConfigurationModel} from "../../Database/Models/GroupConfigurationModel";
|
||||
import {Nullable} from "../../types/Nullable";
|
||||
import {ChannelId} from "../../types/DiscordTypes";
|
||||
import { ConfigurationModel } from "../../Database/Models/ConfigurationModel";
|
||||
import { Model } from "../../Database/Models/Model";
|
||||
import {GroupConfigurationRepository} from "../../Database/Repositories/GroupConfigurationRepository";
|
||||
import {GroupModel} from "../../Database/Models/GroupModel";
|
||||
import {ConfigurationTransformer, TransformerType} from "../ConfigurationTransformer";
|
||||
|
||||
export type RuntimeGroupConfiguration = {
|
||||
channels: Nullable<ChannelRuntimeGroupConfiguration>,
|
||||
permissions: PermissionRuntimeGroupConfiguration,
|
||||
calendar: CalendarRuntimeGroupConfiguration
|
||||
};
|
||||
|
||||
export type ChannelRuntimeGroupConfiguration = {
|
||||
newPlaydates: ChannelId,
|
||||
playdateReminders: ChannelId
|
||||
}
|
||||
|
||||
export type PermissionRuntimeGroupConfiguration = {
|
||||
allowMemberManagingPlaydates: boolean
|
||||
}
|
||||
|
||||
export type CalendarRuntimeGroupConfiguration = {
|
||||
title: null | string,
|
||||
description: null | string,
|
||||
location: null | string
|
||||
}
|
||||
|
||||
export type GroupConfigurationResult =
|
||||
ChannelId | Intl.Locale | boolean | string | null
|
||||
|
||||
export class GroupConfigurationProvider implements ConfigurationProvider<
|
||||
GroupConfigurationModel,
|
||||
RuntimeGroupConfiguration
|
||||
> {
|
||||
constructor(
|
||||
private readonly repository: GroupConfigurationRepository,
|
||||
private readonly group: GroupModel
|
||||
) {
|
||||
}
|
||||
|
||||
get defaults(): RuntimeGroupConfiguration {
|
||||
return {
|
||||
channels: null,
|
||||
permissions: {
|
||||
allowMemberManagingPlaydates: false
|
||||
},
|
||||
calendar: {
|
||||
title: null,
|
||||
description: null,
|
||||
location: null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
get(path: string): Nullable<GroupConfigurationModel> {
|
||||
return this.repository.findConfigurationByPath(this.group, path);
|
||||
}
|
||||
getAll(): GroupConfigurationModel[] {
|
||||
return this.repository.findGroupConfigurations(this.group);
|
||||
}
|
||||
save(value: Omit<ConfigurationModel, "id"> & Partial<Model>): void {
|
||||
if (value.id) {
|
||||
// @ts-expect-error id is set, due to the check on line above
|
||||
this.repository.update(value);
|
||||
}
|
||||
|
||||
this.repository.create(value);
|
||||
}
|
||||
getTransformer(): ConfigurationTransformer {
|
||||
return new ConfigurationTransformer(
|
||||
[
|
||||
{
|
||||
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.Paragraph,
|
||||
},
|
||||
{
|
||||
path: ['calendar', 'location'],
|
||||
type: TransformerType.String
|
||||
}
|
||||
]
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue