76 lines
No EOL
2.2 KiB
TypeScript
76 lines
No EOL
2.2 KiB
TypeScript
import {ConfigurationProvider} from "../ConfigurationProvider";
|
|
import {ServerConfigurationModel} from "../../Database/Models/ServerConfigurationModel";
|
|
import {ServerConfigurationRepository} from "../../Database/Repositories/ServerConfigurationRepository";
|
|
import {Snowflake} from "discord.js";
|
|
import { ConfigurationModel } from "../../Database/Models/ConfigurationModel";
|
|
import { Model } from "../../Database/Models/Model";
|
|
import { Nullable } from "../../types/Nullable";
|
|
import {ConfigurationTransformer, TransformerType} from "../ConfigurationTransformer";
|
|
|
|
export type RuntimeServerConfiguration = {
|
|
permissions: PermissionRuntimeServerConfiguration,
|
|
timezone: string
|
|
}
|
|
|
|
export type PermissionRuntimeServerConfiguration = {
|
|
groupCreation: GroupCreatePermissionRuntimeServerConfiguration
|
|
}
|
|
|
|
export type GroupCreatePermissionRuntimeServerConfiguration = {
|
|
allowEveryone: boolean
|
|
}
|
|
|
|
export class ServerConfigurationProvider implements ConfigurationProvider<
|
|
ServerConfigurationModel,
|
|
RuntimeServerConfiguration
|
|
> {
|
|
constructor(
|
|
private readonly repository: ServerConfigurationRepository,
|
|
private readonly serverid: Snowflake
|
|
) {
|
|
}
|
|
|
|
get defaults(): RuntimeServerConfiguration {
|
|
return {
|
|
permissions: {
|
|
groupCreation: {
|
|
allowEveryone: false
|
|
}
|
|
},
|
|
timezone: '',
|
|
}
|
|
}
|
|
get(path: string): Nullable<ServerConfigurationModel> {
|
|
return this.repository.findConfigurationByPath(this.serverid, path);
|
|
}
|
|
getAll(): ServerConfigurationModel[] {
|
|
return this.repository.findServerConfigurations(this.serverid);
|
|
}
|
|
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,
|
|
serverid: this.serverid
|
|
});
|
|
}
|
|
getTransformer(): ConfigurationTransformer {
|
|
return new ConfigurationTransformer(
|
|
[
|
|
{
|
|
path: ['permissions', 'groupCreation', 'allowEveryone'],
|
|
type: TransformerType.PermissionBoolean
|
|
},
|
|
{
|
|
path: ['timezone'],
|
|
type: TransformerType.Timezone
|
|
}
|
|
]
|
|
)
|
|
}
|
|
|
|
} |