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
|
|
@ -1,80 +0,0 @@
|
|||
import {RuntimeGroupConfiguration} from "./RuntimeGroupConfiguration";
|
||||
import {GroupConfigurationRepository} from "../Database/Repositories/GroupConfigurationRepository";
|
||||
import {GroupModel} from "../Database/Models/GroupModel";
|
||||
import {GroupConfigurationResult, GroupConfigurationTransformers} from "./GroupConfigurationTransformers";
|
||||
// @ts-expect-error set-path is provided
|
||||
import setPath from 'object-path-set';
|
||||
import deepmerge from "deepmerge";
|
||||
import {Nullable} from "../types/Nullable";
|
||||
// @ts-expect-error Any is fine
|
||||
import {isPlainObject} from "is-plain-object";
|
||||
|
||||
export class GroupConfigurationHandler {
|
||||
static DEFAULT_CONFIGURATION: RuntimeGroupConfiguration = {
|
||||
channels: null,
|
||||
permissions: {
|
||||
allowMemberManagingPlaydates: false
|
||||
},
|
||||
calendar: {
|
||||
title: null,
|
||||
description: null,
|
||||
location: null
|
||||
}
|
||||
}
|
||||
|
||||
private readonly transformers: GroupConfigurationTransformers = new GroupConfigurationTransformers();
|
||||
|
||||
constructor(
|
||||
private readonly repository: GroupConfigurationRepository,
|
||||
private readonly group: GroupModel
|
||||
) {
|
||||
}
|
||||
|
||||
|
||||
public saveConfiguration(path: string, value: string): void {
|
||||
const configuration = this.repository.findConfigurationByPath(this.group, path);
|
||||
|
||||
if (configuration) {
|
||||
this.repository.update(
|
||||
{
|
||||
...configuration,
|
||||
value: value
|
||||
}
|
||||
)
|
||||
return;
|
||||
}
|
||||
|
||||
this.repository.create({
|
||||
group: this.group,
|
||||
key: path,
|
||||
value: value,
|
||||
});
|
||||
}
|
||||
|
||||
public getConfiguration(): RuntimeGroupConfiguration {
|
||||
return deepmerge(GroupConfigurationHandler.DEFAULT_CONFIGURATION, this.getDatabaseConfiguration(), {
|
||||
isMergeableObject: isPlainObject
|
||||
});
|
||||
}
|
||||
|
||||
public getConfigurationByPath(path: string): Nullable<GroupConfigurationResult> {
|
||||
const configuration = this.repository.findConfigurationByPath(this.group, path);
|
||||
if (!configuration) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return this.transformers.getValue(configuration);
|
||||
}
|
||||
|
||||
private getDatabaseConfiguration(): Partial<RuntimeGroupConfiguration> {
|
||||
const values = this.repository.findGroupConfigurations(this.group);
|
||||
const configuration: Partial<RuntimeGroupConfiguration> = {};
|
||||
|
||||
values.forEach((configValue) => {
|
||||
const value = this.transformers.getValue(configValue);
|
||||
setPath(configuration, configValue.key, value);
|
||||
})
|
||||
|
||||
return configuration;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue