-
Notifications
You must be signed in to change notification settings - Fork 18
/
setting.service.ts
120 lines (103 loc) · 3.31 KB
/
setting.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { Appearance } from '../../types/appearance';
import { Errors } from 'typescript-rest';
import { CoreDatabase as Db } from './core.database';
import { pick } from 'lodash';
import { Setting } from './interfaces/setting.interface';
import {
SettingResponse, CreateSettingDto, EditSettingDto, PaginateSetting,
SettingsGroup,
SettingResponseFields as fields
} from './dto/setting.dto';
import { Document } from 'mongoose';
import { KeyValue } from '../../types/data.types';
import { Repository } from '../../database/repository';
import { appearance } from './appearance/setting.appearance';
import { ContainerService } from '../../container';
export class SettingService {
async getAppearance(): Promise<Appearance> {
const config = ContainerService.getAppearance('settings') || appearance;
return config;
}
async getSettingsByName(name?: string): Promise<SettingsGroup> {
const result = new SettingsGroup();
if (name) {
const docs = await Db.Setting.find({
name: name
}).exec();
if (docs) {
docs.forEach((doc) => {
result[doc.key] = doc.value;
});
}
}
return result;
}
async getSettingsByKey(name: string): Promise<SettingResponse> {
const setting = await Db.Setting.findOne({
key: name
}).exec();
return this.pure(setting);
}
async search(keyword?: string, value?: string, limit = 15): Promise<Array<KeyValue>> {
return Repository.search(Db.Setting, keyword, value, '', limit);
}
async create(entry: CreateSettingDto): Promise<SettingResponse> {
const doc = new Db.Setting(entry);
const result = await doc.save();
return this.pure(result);
}
async updateSettingsByName(name: string, entry: SettingsGroup): Promise<SettingsGroup> {
const keys = Object.keys(entry);
for (let key of keys) {
const instance = {
key: key,
value: entry[key]
};
await Db.Setting.findOneAndUpdate(
{ key: key, name: name },
{ $set: instance },
{ upsert: true, 'new': true }).exec();
}
return this.getSettingsByName(name);
}
async update(entry: EditSettingDto): Promise<SettingResponse> {
if (entry.id) {
const result = await Db.Setting.findOneAndUpdate(
{ _id: entry.id },
{ $set: entry },
{ upsert: true, 'new': true }).exec();
return this.pure(result);
} else {
throw new Errors.BadRequestError('settings not found');
}
}
async query(
keyword?: string,
page?: number,
size?: number,
sort?: string
): Promise<PaginateSetting> {
const condition = keyword ? { name: new RegExp(keyword, 'i') } : {};
const query = Db.Setting.find(condition).sort(sort);
const collection = Db.Setting.find(condition);
const result = Repository.query<Setting & Document, SettingResponse>(query, collection, page, size, fields);
return result;
}
async get(id: string): Promise<SettingResponse> {
const result: any = await Repository.get(Db.Setting, id);
const picked = this.pure(result);
return picked;
}
async remove(id: string): Promise<boolean> {
return Repository.remove(Db.Setting, id);
}
private pure(entry: Setting & Document): SettingResponse {
return pick(entry, [
'id',
'name',
'key',
'value',
'description',
])
}
}