105 lines
No EOL
2.9 KiB
TypeScript
105 lines
No EOL
2.9 KiB
TypeScript
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 = {
|
|
notifications: ChannelId,
|
|
playdateReminders: ChannelId
|
|
}
|
|
|
|
export type PermissionRuntimeGroupConfiguration = {
|
|
allowMemberManagingPlaydates: boolean
|
|
}
|
|
|
|
export type CalendarRuntimeGroupConfiguration = {
|
|
title: null | string,
|
|
description: null | string,
|
|
location: null | string
|
|
}
|
|
|
|
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);
|
|
return
|
|
}
|
|
|
|
this.repository.create({
|
|
...value,
|
|
group: this.group
|
|
});
|
|
}
|
|
getTransformer(): ConfigurationTransformer {
|
|
return new ConfigurationTransformer(
|
|
[
|
|
{
|
|
path: ['channels', 'notifications'],
|
|
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
|
|
}
|
|
]
|
|
)
|
|
}
|
|
|
|
} |