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, 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 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 { return this.repository.findConfigurationByPath(this.group, path); } getAll(): GroupConfigurationModel[] { return this.repository.findGroupConfigurations(this.group); } save(value: Omit & Partial): 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', '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 } ] ) } }