-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface.service.ts
More file actions
283 lines (222 loc) · 9.54 KB
/
interface.service.ts
File metadata and controls
283 lines (222 loc) · 9.54 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import _ from 'lodash';
import { getFilter } from '../../util/api';
import type {
RouterContext,
RouterInput,
RouterOutput,
InterfaceDocument,
InterfaceGroupDocument,
InterfaceComponentDocument,
} from './interface.types';
export class Service {
constructor() {}
// Interface Methods
async getInterface(input: RouterInput['getInterface'], ctx: RouterContext): Promise<RouterOutput['getInterface']> {
if (!input) throw new Error('Input should not be void');
console.log('Interface.Service.getInterface', input);
const filter = getFilter(input);
const interfac = await ctx.app.model.Interface.findOne(filter).select('-meta').exec();
if (!interfac) throw new Error('Interface not found');
if (!interfac.version) interfac.version = 1;
if (!interfac.meta) interfac.meta = {};
return interfac as InterfaceDocument;
}
async getInterfaces(input: RouterInput['getInterfaces'], ctx: RouterContext): Promise<RouterOutput['getInterfaces']> {
if (!input) throw new Error('Input should not be void');
const filter = getFilter(input);
console.log('Interface.Service.getInterfaces', input, filter);
const interfaces = await ctx.app.model.Interface.find(filter).select('-meta').exec();
const rolesOnUsers = ['Super User']; // await this.fetchRolesFromContext(ctx);
for (const interfac of interfaces) {
if (!interfac.version) interfac.version = 1;
if (!interfac.meta) interfac.meta = {};
}
return interfaces.filter((interfac) => {
const roles = [];
return this.hasPermission(roles, rolesOnUsers);
}) as InterfaceDocument[];
}
async createInterface(
input: RouterInput['createInterface'],
ctx: RouterContext
): Promise<RouterOutput['createInterface']> {
if (!input) throw new Error('Input should not be void');
const existingInterface = await ctx.app.model.Interface.findOne({ key: input.data.key }).exec();
const version = existingInterface ? existingInterface.version + 1 : 1;
const newInterface = await ctx.app.model.Interface.create({
...input.data,
status: input.data.status || 'Draft',
version,
createdDate: new Date(),
});
await ctx.app.service.Job.updateMetrics({}, ctx);
return newInterface as InterfaceDocument;
}
async createInterfaceDraft(
input: RouterInput['createInterfaceDraft'],
ctx: RouterContext
): Promise<RouterOutput['createInterfaceDraft']> {
if (!input) throw new Error('Input should not be void');
console.log('Interface.Service.createInterfaceDraft', input);
const existingInterface = await ctx.app.model.Interface.findOne({ key: input.data.key }).exec();
const version = existingInterface ? existingInterface.version + 1 : 1;
const newInterface = await ctx.app.model.Interface.create({
...input.data,
status: input.data.status || 'Draft',
version,
createdDate: new Date(),
});
await ctx.app.service.Job.updateMetrics({}, ctx);
return newInterface as InterfaceDocument;
}
async updateInterface(
input: RouterInput['updateInterface'],
ctx: RouterContext
): Promise<RouterOutput['updateInterface']> {
if (!input) throw new Error('Input should not be void');
console.log('Interface.Service.updateInterface', input);
const filter = getFilter(input);
let interfac: any = await ctx.app.model.Interface.findOne(filter).exec();
if (!interfac) throw new Error('Interface does not exist');
if (interfac.status === 'Published' || input.data.groupId) {
await ctx.app.model.Interface.updateMany({ key: interfac.key, status: 'Draft' }, { status: 'Archived' }).exec();
const version =
(await ctx.app.model.Interface.find({ key: interfac.key }).sort({ version: -1 }).exec())[0].version + 1;
interfac = (await ctx.app.model.Interface.create({
...interfac,
...input.data,
status: 'Draft',
version,
createdDate: new Date(),
})) as InterfaceDocument;
} else {
await ctx.app.model.Interface.updateOne(filter, input.data).exec();
interfac = await ctx.app.model.Interface.findOne(filter).exec();
}
await ctx.app.service.Job.updateMetrics({}, ctx);
return interfac as InterfaceDocument;
}
async deleteInterface(
input: RouterInput['deleteInterface'],
ctx: RouterContext
): Promise<RouterOutput['deleteInterface']> {
if (!input) throw new Error('Input should not be void');
console.log('Interface.Service.deleteInterface', input);
const filter = getFilter(input);
const interfac = await ctx.app.model.Interface.findOne(filter).exec();
if (!interfac) throw new Error('Interface does not exist');
interfac.status = 'Archived';
interfac.updatedDate = new Date();
await ctx.app.model.Interface.updateOne(filter, interfac).exec();
await ctx.app.service.Job.updateMetrics({}, ctx);
return interfac as InterfaceDocument;
}
async getInterfaceGroup(
input: RouterInput['getInterfaceGroup'],
ctx: RouterContext
): Promise<RouterOutput['getInterfaceGroup']> {
if (!input) throw new Error('Input should not be void');
console.log('Interface.Service.getInterfaceGroup', input);
const filter = getFilter(input);
const group = await ctx.app.model.InterfaceGroup.findOne(filter).exec();
if (!group) throw new Error('InterfaceGroup not found');
if (!group.roles) group.roles = [];
// for (const role of group.roles) {
// role.role = await ctx.app.service.Profile.findRole({
// where: { id: { equals: role.role.id } },
// });
// }
return group as InterfaceGroupDocument;
}
async getInterfaceGroups(
input: RouterInput['getInterfaceGroups'],
ctx: RouterContext
): Promise<RouterOutput['getInterfaceGroups']> {
console.log('Interface.Service.getInterfaceGroups', input);
const filter = getFilter(input);
const groups = await ctx.app.model.InterfaceGroup.find(filter).exec();
return groups as InterfaceGroupDocument[];
}
async createInterfaceGroup(
input: RouterInput['createInterfaceGroup'],
ctx: RouterContext
): Promise<RouterOutput['createInterfaceGroup']> {
if (!input) throw new Error('Input should not be void');
console.log('Interface.Service.createInterfaceGroup', input);
const newGroup = await ctx.app.model.InterfaceGroup.create(input.data);
return newGroup as InterfaceGroupDocument;
}
async updateInterfaceGroup(
input: RouterInput['updateInterfaceGroup'],
ctx: RouterContext
): Promise<RouterOutput['updateInterfaceGroup']> {
if (!input) throw new Error('Input should not be void');
console.log('Interface.Service.updateInterfaceGroup', input);
const filter = getFilter(input);
await ctx.app.model.InterfaceGroup.updateOne(filter, input.data, {
runValidators: true,
}).exec();
const updatedGroup = await ctx.app.model.InterfaceGroup.findOne(filter).exec();
if (!updatedGroup) throw new Error('InterfaceGroup update failed');
return updatedGroup as InterfaceGroupDocument;
}
async getInterfaceComponent(
input: RouterInput['getInterfaceComponent'],
ctx: RouterContext
): Promise<RouterOutput['getInterfaceComponent']> {
if (!input) throw new Error('Input should not be void');
console.log('Interface.Service.getInterfaceComponent', input);
const filter = getFilter(input);
const component = await ctx.app.model.InterfaceComponent.findOne(filter).exec();
if (!component) throw new Error('InterfaceComponent not found');
return component as InterfaceComponentDocument;
}
async createInterfaceComponent(
input: RouterInput['createInterfaceComponent'],
ctx: RouterContext
): Promise<RouterOutput['createInterfaceComponent']> {
if (!input) throw new Error('Input should not be void');
console.log('Interface.Service.createInterfaceComponent', input);
if (
await ctx.app.model.InterfaceComponent.findOne({
$or: [{ name: input.data.name }, { key: input.data.key }],
}).exec()
) {
throw new Error('Component template already exists');
}
const component = await ctx.app.model.InterfaceComponent.create({
name: input.data.name,
key: input.data.key,
status: input.data.status,
meta: input.data.meta,
});
await ctx.app.service.Job.updateMetrics({}, ctx);
return component as InterfaceComponentDocument;
}
async updateInterfaceComponent(
input: RouterInput['updateInterfaceComponent'],
ctx: RouterContext
): Promise<RouterOutput['updateInterfaceComponent']> {
if (!input) throw new Error('Input should not be void');
console.log('Interface.Service.updateInterfaceComponent', input);
const filter = getFilter(input);
const data = Object.entries(input.data).reduce((acc, [key, value]: any) => {
if (value?.set) acc[key] = value.set;
return acc;
}, {} as any);
await ctx.app.model.InterfaceComponent.updateOne(filter, data, {
runValidators: true,
}).exec();
const updatedComponent = await ctx.app.model.InterfaceComponent.findOne(filter).exec();
if (!updatedComponent) throw new Error('InterfaceComponent update failed');
await ctx.app.service.Job.updateMetrics({}, ctx);
return updatedComponent as InterfaceComponentDocument;
}
// Helper Methods
private async fetchRolesFromContext(ctx: RouterContext): Promise<string[]> {
return ctx.profile?.roles.filter((r) => r.status === 'Active').map((r) => r.role.name) || [];
}
private hasPermission(roles: string[], rolesOnUsers: string[]): boolean {
return rolesOnUsers.includes('Super User') || _.intersection(roles, rolesOnUsers).length > 0;
}
}