Adds group configuration
This commit is contained in:
parent
0d9cf6a370
commit
154002f6f3
16 changed files with 633 additions and 20 deletions
68
source/Groups/GroupConfigurationHandler.ts
Normal file
68
source/Groups/GroupConfigurationHandler.ts
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
import {RuntimeGroupConfiguration} from "./RuntimeGroupConfiguration";
|
||||
import {GroupConfigurationRepository} from "../Repositories/GroupConfigurationRepository";
|
||||
import {GroupModel} from "../Models/GroupModel";
|
||||
import {GroupConfigurationResult, GroupConfigurationTransformers} from "./GroupConfigurationTransformers";
|
||||
// @ts-ignore
|
||||
import setPath from 'object-path-set';
|
||||
import deepmerge from "deepmerge";
|
||||
import {Nullable} from "../types/Nullable";
|
||||
|
||||
export class GroupConfigurationHandler {
|
||||
private static DEFAULT_CONFIGURATION: RuntimeGroupConfiguration = {
|
||||
channels: null,
|
||||
locale: new Intl.Locale('en-GB'),
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
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