@tomei/sso 0.33.8 → 0.34.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (150) hide show
  1. package/.commitlintrc.json +22 -22
  2. package/.eslintrc +16 -16
  3. package/.eslintrc.js +35 -35
  4. package/.gitlab-ci.yml +16 -16
  5. package/.husky/commit-msg +15 -15
  6. package/.husky/pre-commit +7 -7
  7. package/.prettierrc +4 -4
  8. package/Jenkinsfile +57 -57
  9. package/README.md +23 -23
  10. package/__tests__/unit/components/group/group.spec.ts +79 -79
  11. package/__tests__/unit/components/group-object-privilege/group-object-privilege.spec.ts +88 -88
  12. package/__tests__/unit/components/group-privilege/group-privilege.spec.ts +68 -68
  13. package/__tests__/unit/components/group-reporting-user/group-reporting-user.spec.ts +66 -66
  14. package/__tests__/unit/components/group-system-access/group-system-access.spec.ts +83 -83
  15. package/__tests__/unit/components/login-user/l.spec.ts +746 -746
  16. package/__tests__/unit/components/login-user/login.spec.ts +1064 -1064
  17. package/__tests__/unit/components/password-hash/password-hash.service.spec.ts +31 -31
  18. package/__tests__/unit/components/system/system.spec.ts +254 -254
  19. package/__tests__/unit/components/system-privilege/system-privilege.spec.ts +83 -83
  20. package/__tests__/unit/components/user-group/user-group.spec.ts +86 -86
  21. package/__tests__/unit/components/user-object-privilege/user-object-privilege.spec.ts +78 -78
  22. package/__tests__/unit/components/user-privilege/user-privilege.spec.ts +72 -72
  23. package/__tests__/unit/components/user-system-access/user-system-access.spec.ts +89 -89
  24. package/__tests__/unit/redis-client/redis.service.spec.ts +23 -23
  25. package/__tests__/unit/session/session.service.spec.ts +47 -47
  26. package/__tests__/unit/system-privilege/system-privilage.spec.ts +91 -91
  27. package/coverage/clover.xml +1452 -1452
  28. package/coverage/coverage-final.json +47 -47
  29. package/coverage/lcov-report/base.css +224 -224
  30. package/coverage/lcov-report/block-navigation.js +87 -87
  31. package/coverage/lcov-report/components/group/group.repository.ts.html +117 -117
  32. package/coverage/lcov-report/components/group/group.ts.html +327 -327
  33. package/coverage/lcov-report/components/group/index.html +130 -130
  34. package/coverage/lcov-report/components/group-object-privilege/group-object-privilege.repository.ts.html +117 -117
  35. package/coverage/lcov-report/components/group-object-privilege/group-object-privilege.ts.html +321 -321
  36. package/coverage/lcov-report/components/group-object-privilege/index.html +130 -130
  37. package/coverage/lcov-report/components/group-privilege/group-privilege.repository.ts.html +117 -117
  38. package/coverage/lcov-report/components/group-privilege/group-privilege.ts.html +303 -303
  39. package/coverage/lcov-report/components/group-privilege/index.html +130 -130
  40. package/coverage/lcov-report/components/group-reporting-user/group-reporting-user.repository.ts.html +117 -117
  41. package/coverage/lcov-report/components/group-reporting-user/group-reporting-user.ts.html +327 -327
  42. package/coverage/lcov-report/components/group-reporting-user/index.html +130 -130
  43. package/coverage/lcov-report/components/group-system-access/group-system-access.repository.ts.html +117 -117
  44. package/coverage/lcov-report/components/group-system-access/group-system-access.ts.html +309 -309
  45. package/coverage/lcov-report/components/group-system-access/index.html +130 -130
  46. package/coverage/lcov-report/components/login-history/index.html +115 -115
  47. package/coverage/lcov-report/components/login-history/login-history.repository.ts.html +117 -117
  48. package/coverage/lcov-report/components/login-user/index.html +130 -130
  49. package/coverage/lcov-report/components/login-user/login-user.ts.html +5007 -5007
  50. package/coverage/lcov-report/components/login-user/user.repository.ts.html +117 -117
  51. package/coverage/lcov-report/components/password-hash/index.html +115 -115
  52. package/coverage/lcov-report/components/password-hash/password-hash.service.ts.html +126 -126
  53. package/coverage/lcov-report/components/system/index.html +130 -130
  54. package/coverage/lcov-report/components/system/system.repository.ts.html +117 -117
  55. package/coverage/lcov-report/components/system/system.ts.html +909 -909
  56. package/coverage/lcov-report/components/system-privilege/index.html +130 -130
  57. package/coverage/lcov-report/components/system-privilege/system-privilege.repository.ts.html +120 -120
  58. package/coverage/lcov-report/components/system-privilege/system-privilege.ts.html +390 -390
  59. package/coverage/lcov-report/components/user-group/index.html +130 -130
  60. package/coverage/lcov-report/components/user-group/user-group.repository.ts.html +117 -117
  61. package/coverage/lcov-report/components/user-group/user-group.ts.html +354 -354
  62. package/coverage/lcov-report/components/user-object-privilege/index.html +130 -130
  63. package/coverage/lcov-report/components/user-object-privilege/user-object-privilege.repository.ts.html +117 -117
  64. package/coverage/lcov-report/components/user-object-privilege/user-object-privilege.ts.html +312 -312
  65. package/coverage/lcov-report/components/user-privilege/index.html +130 -130
  66. package/coverage/lcov-report/components/user-privilege/user-privilege.repository.ts.html +117 -117
  67. package/coverage/lcov-report/components/user-privilege/user-privilege.ts.html +306 -306
  68. package/coverage/lcov-report/components/user-system-access/index.html +130 -130
  69. package/coverage/lcov-report/components/user-system-access/user-system-access.repository.ts.html +117 -117
  70. package/coverage/lcov-report/components/user-system-access/user-system-access.ts.html +312 -312
  71. package/coverage/lcov-report/enum/group-type.enum.ts.html +108 -108
  72. package/coverage/lcov-report/enum/index.html +160 -160
  73. package/coverage/lcov-report/enum/index.ts.html +93 -93
  74. package/coverage/lcov-report/enum/user-status.enum.ts.html +105 -105
  75. package/coverage/lcov-report/enum/yn.enum.ts.html +96 -96
  76. package/coverage/lcov-report/index.html +370 -370
  77. package/coverage/lcov-report/models/group-object-privilege.entity.ts.html +333 -333
  78. package/coverage/lcov-report/models/group-privilege.entity.ts.html +315 -315
  79. package/coverage/lcov-report/models/group-reporting-user.entity.ts.html +339 -339
  80. package/coverage/lcov-report/models/group-system-access.entity.ts.html +324 -324
  81. package/coverage/lcov-report/models/group.entity.ts.html +435 -435
  82. package/coverage/lcov-report/models/index.html +310 -310
  83. package/coverage/lcov-report/models/login-history.entity.ts.html +252 -252
  84. package/coverage/lcov-report/models/staff.entity.ts.html +411 -411
  85. package/coverage/lcov-report/models/system-privilege.entity.ts.html +354 -354
  86. package/coverage/lcov-report/models/system.entity.ts.html +423 -423
  87. package/coverage/lcov-report/models/user-group.entity.ts.html +354 -354
  88. package/coverage/lcov-report/models/user-object-privilege.entity.ts.html +330 -330
  89. package/coverage/lcov-report/models/user-privilege.entity.ts.html +315 -315
  90. package/coverage/lcov-report/models/user-system-access.entity.ts.html +315 -315
  91. package/coverage/lcov-report/models/user.entity.ts.html +522 -522
  92. package/coverage/lcov-report/prettify.css +1 -1
  93. package/coverage/lcov-report/prettify.js +2 -2
  94. package/coverage/lcov-report/redis-client/index.html +115 -115
  95. package/coverage/lcov-report/redis-client/redis.service.ts.html +240 -240
  96. package/coverage/lcov-report/session/index.html +115 -115
  97. package/coverage/lcov-report/session/session.service.ts.html +246 -246
  98. package/coverage/lcov-report/sorter.js +196 -196
  99. package/coverage/lcov.info +2490 -2490
  100. package/coverage/test-report.xml +128 -128
  101. package/create-sso-user.sql +39 -39
  102. package/dist/src/components/group/group.d.ts +1 -0
  103. package/dist/src/components/group/group.js +45 -18
  104. package/dist/src/components/group/group.js.map +1 -1
  105. package/dist/src/components/group-object-privilege/group-object-privilege.js +4 -2
  106. package/dist/src/components/group-object-privilege/group-object-privilege.js.map +1 -1
  107. package/dist/src/components/group-object-privilege/group-object-privilege.repository.js.map +1 -1
  108. package/dist/src/components/group-privilege/group-privilege.repository.js.map +1 -1
  109. package/dist/src/components/login-user/login-user.d.ts +1 -1
  110. package/dist/src/components/system-privilege/system-privilege.js +1 -1
  111. package/dist/src/components/system-privilege/system-privilege.js.map +1 -1
  112. package/dist/src/components/user-object-privilege/user-object-privilege.js.map +1 -1
  113. package/dist/src/interfaces/group-search-attr.interface.d.ts +1 -0
  114. package/dist/tsconfig.tsbuildinfo +1 -1
  115. package/jest.config.js +14 -14
  116. package/migrations/20240314080602-create-user-table.js +108 -108
  117. package/migrations/20240314080603-create-user-group-table.js +85 -85
  118. package/migrations/20240314080604-create-user-user-group-table.js +55 -55
  119. package/migrations/20240314080605-create-login-history-table.js +53 -53
  120. package/migrations/20240527064925-create-system-table.js +78 -78
  121. package/migrations/20240527064926-create-system-privilege-table.js +67 -67
  122. package/migrations/20240527065342-create-group-table.js +89 -89
  123. package/migrations/20240527065633-create-group-reporting-user-table.js +76 -76
  124. package/migrations/20240528011551-create-group-system-access-table.js +72 -72
  125. package/migrations/20240528023018-user-system-access-table.js +75 -75
  126. package/migrations/20240528032229-user-privilege-table.js +75 -75
  127. package/migrations/20240528063003-create-group-privilege-table.js +75 -75
  128. package/migrations/20240528063051-create-group-object-privilege-table.js +84 -84
  129. package/migrations/20240528063107-create-user-object-privilege-table.js +83 -83
  130. package/package.json +89 -89
  131. package/sampledotenv +7 -7
  132. package/sonar-project.properties +22 -22
  133. package/src/components/group/group.ts +1514 -1456
  134. package/src/components/group-object-privilege/group-object-privilege.repository.ts +3 -2
  135. package/src/components/group-object-privilege/group-object-privilege.ts +44 -31
  136. package/src/components/group-privilege/group-privilege.repository.ts +2 -1
  137. package/src/components/system-privilege/system-privilege.ts +7 -4
  138. package/src/components/user-object-privilege/user-object-privilege.ts +0 -1
  139. package/src/interfaces/group-object-privilege.interface.ts +14 -14
  140. package/src/interfaces/group-search-attr.interface.ts +1 -0
  141. package/src/interfaces/system-privilege-search.interface.ts +5 -5
  142. package/src/models/group-system-access.entity.ts +81 -81
  143. package/tsconfig.build.json +5 -5
  144. package/tsconfig.json +22 -22
  145. package/dist/__tests__/unit/components/group-privilege/group-privilege.test.d.ts +0 -1
  146. package/dist/__tests__/unit/components/group-privilege/group-privilege.test.js +0 -71
  147. package/dist/__tests__/unit/components/group-privilege/group-privilege.test.js.map +0 -1
  148. package/dist/__tests__/unit/components/login-user/login-user.spec.d.ts +0 -0
  149. package/dist/__tests__/unit/components/login-user/login-user.spec.js +0 -6
  150. package/dist/__tests__/unit/components/login-user/login-user.spec.js.map +0 -1
@@ -1,1456 +1,1514 @@
1
- import { ClassError, ObjectBase } from '@tomei/general';
2
- import { GroupRepository } from './group.repository';
3
- import { IGroupAttr } from '../../interfaces/group.interface';
4
- import { GroupTypeEnum } from 'enum';
5
- import { LoginUser } from '../login-user/login-user';
6
- import { IGroupSearchAttr } from '../../interfaces/group-search-attr.interface';
7
- import { ApplicationConfig } from '@tomei/config';
8
- import { Op } from 'sequelize';
9
- import { ActionEnum, Activity } from '@tomei/activity-history';
10
- import { GroupSystemAccessRepository } from '../group-system-access/group-system-access.repository';
11
- import SystemModel from '../../models/system.entity';
12
- import { GroupSystemAccess } from '../group-system-access';
13
- import { RedisService } from '../../redis-client/redis.service';
14
- import SystemPrivilegeModel from '../../models/system-privilege.entity';
15
- import { GroupPrivilegeRepository } from '../group-privilege/group-privilege.repository';
16
- import { SystemPrivilege } from '../system-privilege/system-privilege';
17
- import GroupPrivilegeModel from '../../models/group-privilege.entity';
18
- import { GroupObjectPrivilegeRepository } from '../group-object-privilege/group-object-privilege.repository';
19
- import { GroupObjectPrivilege } from '../group-object-privilege/group-object-privilege';
20
- import { GroupPrivilege } from '../group-privilege/group-privilege';
21
-
22
- export class Group extends ObjectBase {
23
- ObjectId: string;
24
- ObjectName: string;
25
- TableName: 'sso_Group';
26
- ObjectType = 'Group';
27
-
28
- Name: string;
29
- Description: string;
30
- Type: GroupTypeEnum;
31
- ParentGroupCode: string;
32
- InheritParentPrivilegeYN: string;
33
- InheritParentSystemAccessYN: string;
34
- Status: string;
35
- ParentGroup?: any;
36
- private _CreatedById: number;
37
- private _CreatedAt: Date;
38
- private _UpdatedById: number;
39
- private _UpdatedAt: Date;
40
- private static _Repo = new GroupRepository();
41
- private static _GroupSystemAccessRepo = new GroupSystemAccessRepository();
42
- private static _GroupPrivilegeRepo = new GroupPrivilegeRepository();
43
- private static _GroupObjectPrivilegeRepo = new GroupObjectPrivilegeRepository();
44
- private static _RedisService: RedisService;
45
- get GroupCode(): string {
46
- return this.ObjectId;
47
- }
48
-
49
- set GroupCode(value: string) {
50
- this.ObjectId = value;
51
- }
52
-
53
- get CreatedById(): number {
54
- return this._CreatedById;
55
- }
56
-
57
- get CreatedAt(): Date {
58
- return this._CreatedAt;
59
- }
60
-
61
- get UpdatedById(): number {
62
- return this._UpdatedById;
63
- }
64
-
65
- get UpdatedAt(): Date {
66
- return this._UpdatedAt;
67
- }
68
-
69
- private constructor(groupAttr?: IGroupAttr) {
70
- super();
71
- if (groupAttr) {
72
- this.GroupCode = groupAttr.GroupCode;
73
- this.Name = groupAttr.Name;
74
- this.Description = groupAttr?.Description;
75
- this.Type = groupAttr?.Type;
76
- this.ParentGroupCode = groupAttr?.ParentGroupCode;
77
- this.InheritParentPrivilegeYN = groupAttr?.InheritParentPrivilegeYN;
78
- this.InheritParentSystemAccessYN = groupAttr?.InheritParentSystemAccessYN;
79
- this.Status = groupAttr?.Status;
80
- this._CreatedById = groupAttr.CreatedById;
81
- this._CreatedAt = groupAttr.CreatedAt;
82
- this._UpdatedById = groupAttr.UpdatedById;
83
- this._UpdatedAt = groupAttr.UpdatedAt;
84
- }
85
- }
86
-
87
- public static async init(dbTransaction: any, GroupCode?: string) {
88
- try {
89
- Group._RedisService = await RedisService.init();
90
- if (GroupCode) {
91
- const group = await Group._Repo.findByPk(GroupCode, {
92
- transaction: dbTransaction,
93
- });
94
- if (group) {
95
- return new Group(group);
96
- } else {
97
- throw Error('Group not found');
98
- }
99
- }
100
- return new Group();
101
- } catch (error) {
102
- throw new ClassError(
103
- 'Group',
104
- 'GroupErrMsg01',
105
- 'Failed To Initialize Group',
106
- );
107
- }
108
- }
109
-
110
- public static async findAll(
111
- page: number,
112
- row: number,
113
- dbTransaction: any,
114
- loginUser: LoginUser,
115
- search?: IGroupSearchAttr,
116
- ) {
117
- //This method will list all group based on the query params.
118
- //Part 1: Privilege Checking
119
- const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
120
- const isPrivileged = await loginUser.checkPrivileges(
121
- systemCode,
122
- 'GROUP_LIST',
123
- );
124
-
125
- if (!isPrivileged) {
126
- throw new ClassError(
127
- 'Group',
128
- 'GroupErrMsg04',
129
- 'User is not privileged to list group',
130
- );
131
- }
132
-
133
- //Part 2: Retrieve listing
134
- const queryObj: any = {};
135
-
136
- let options: any = {
137
- transaction: dbTransaction,
138
- };
139
-
140
- if (page && row) {
141
- options = {
142
- ...options,
143
- limit: row,
144
- offset: row * (page - 1),
145
- order: [['CreatedAt', 'DESC']],
146
- };
147
- }
148
-
149
- if (search) {
150
- Object.entries(search).forEach(([key, value]) => {
151
- queryObj[key] = {
152
- [Op.substring]: value,
153
- };
154
- });
155
-
156
- options = {
157
- ...options,
158
- where: queryObj,
159
- };
160
-
161
- const result = await Group._Repo.findAllWithPagination(options);
162
-
163
- //Map the result to Group instance
164
- return {
165
- Count: result.count,
166
- Groups: result.rows.map(
167
- (group) => new Group(group.get({ plain: true })),
168
- ),
169
- };
170
- }
171
- }
172
-
173
- public static async create(
174
- loginUser: LoginUser,
175
- dbTransaction: any,
176
- group: Group,
177
- ) {
178
- try {
179
- //Part 1: Privilege Checking
180
- const systemCode =
181
- ApplicationConfig.getComponentConfigValue('system-code');
182
- const isPrivileged = await loginUser.checkPrivileges(
183
- systemCode,
184
- 'GROUP_CREATE',
185
- );
186
- if (!isPrivileged) {
187
- throw new Error('You do not have permission to create group');
188
- }
189
-
190
- //Part 2: Validation
191
- if (!group.GroupCode) {
192
- throw new ClassError(
193
- 'Group',
194
- 'GroupErrMsg02',
195
- 'Group Code is required',
196
- );
197
- }
198
-
199
- if (!group.Name) {
200
- throw new ClassError(
201
- 'Group',
202
- 'GroupErrMsg02',
203
- 'Group Name is required',
204
- );
205
- }
206
-
207
- if (!group.Type) {
208
- throw new ClassError(
209
- 'Group',
210
- 'GroupErrMsg02',
211
- 'Group Type is required',
212
- );
213
- }
214
-
215
- //Check if group code is unique
216
- const existingGroupCode = await Group._Repo.findByPk(group.GroupCode, {
217
- transaction: dbTransaction,
218
- });
219
-
220
- if (existingGroupCode) {
221
- throw new ClassError(
222
- 'Group',
223
- 'GroupErrMsg03',
224
- 'Duplicate GroupCode found.',
225
- );
226
- }
227
-
228
- //Validate parent group code if passed. Call Group._Repo.findByPk
229
- if (group.ParentGroupCode) {
230
- const parentGroup = await Group._Repo.findByPk(group.ParentGroupCode, {
231
- transaction: dbTransaction,
232
- });
233
-
234
- if (!parentGroup) {
235
- throw new ClassError(
236
- 'Group',
237
- 'GroupErrMsg04',
238
- 'ParentGroupCode is not found.',
239
- );
240
- }
241
-
242
- //If Params.group.GroupCode = Params.group?.ParentGroupCode, throw new ClassError
243
- if (group.GroupCode === group.ParentGroupCode) {
244
- throw new ClassError(
245
- 'Group',
246
- 'GroupErrMsg05',
247
- 'GroupCode and ParentGroupCode cannot be the same.',
248
- );
249
- }
250
- }
251
-
252
- //Part 3: Create Group
253
- //Initialise new Group instance and populate
254
- const newGroup = new Group(group);
255
- newGroup.ObjectId = group.GroupCode;
256
- newGroup.Name = group.Name;
257
- newGroup.Type = group.Type;
258
- newGroup.Description = group.Description;
259
- newGroup.ParentGroupCode = group.ParentGroupCode;
260
- newGroup.InheritParentPrivilegeYN = group.InheritParentPrivilegeYN;
261
- newGroup.InheritParentSystemAccessYN = group.InheritParentSystemAccessYN;
262
- newGroup.Status = 'Active';
263
- newGroup._CreatedById = loginUser.UserId;
264
- newGroup._UpdatedById = loginUser.UserId;
265
-
266
- //Call Group._Repo create method
267
- const entityGroupAfter = {
268
- GroupCode: newGroup.ObjectId,
269
- Name: newGroup.Name,
270
- Type: newGroup.Type,
271
- Description: newGroup.Description,
272
- ParentGroupCode: newGroup.ParentGroupCode,
273
- InheritParentPrivilegeYN: newGroup.InheritParentPrivilegeYN,
274
- InheritParentSystemAccessYN: newGroup.InheritParentSystemAccessYN,
275
- Status: newGroup.Status,
276
- CreatedById: newGroup._CreatedById,
277
- UpdatedById: newGroup._UpdatedById,
278
- CreatedAt: newGroup._CreatedAt,
279
- UpdatedAt: newGroup._UpdatedAt,
280
- };
281
-
282
- await Group._Repo.create(entityGroupAfter, {
283
- transaction: dbTransaction,
284
- });
285
-
286
- //Part 4: Record Create Group Activity and return newGroup
287
-
288
- const entityValueBefore = {};
289
-
290
- //Instantiate new activity
291
- const activity = new Activity();
292
- activity.ActivityId = activity.createId();
293
- activity.Action = ActionEnum.ADD;
294
- activity.Description = 'Create Group';
295
- activity.EntityType = 'Group';
296
- activity.EntityId = newGroup.ObjectId;
297
- activity.EntityValueBefore = JSON.stringify(entityValueBefore);
298
- activity.EntityValueAfter = JSON.stringify(entityGroupAfter);
299
-
300
- //Call Activity.create method
301
- await activity.create(loginUser.ObjectId, dbTransaction);
302
-
303
- return newGroup;
304
- } catch (error) {
305
- throw error;
306
- }
307
- }
308
-
309
- protected static async checkDuplicateGroupCode(
310
- dbTransaction: any,
311
- GroupCode,
312
- ) {
313
- const isGroupCodeExist = await Group._Repo.findOne({
314
- where: { GroupCode },
315
- transaction: dbTransaction,
316
- });
317
-
318
- if (isGroupCodeExist) {
319
- throw new ClassError(
320
- 'Group',
321
- 'GroupErrMsg07',
322
- 'GroupCode already exists.',
323
- );
324
- }
325
- }
326
-
327
- public async update(
328
- loginUser: LoginUser,
329
- dbTransaction: any,
330
- group: {
331
- GroupCode: string;
332
- NewGroupCode?: string;
333
- Name?: string;
334
- Description?: string;
335
- Type?: GroupTypeEnum;
336
- ParentGroupCode?: string;
337
- InheritParentPrivilegeYN?: string;
338
- InheritParentSystemAccessYN?: string;
339
- Status?: string;
340
- },
341
- ) {
342
- //Part 1: Privilege Checking
343
- const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
344
- const isPrivileged = await loginUser.checkPrivileges(
345
- systemCode,
346
- 'GROUP_UPDATE',
347
- );
348
-
349
- if (!isPrivileged) {
350
- throw new ClassError(
351
- 'Group',
352
- 'GroupErrMsg06',
353
- 'You do not have the privilege to update Group',
354
- );
355
- }
356
- try {
357
- const currentGroup = await Group.init(dbTransaction, group.GroupCode);
358
- if (group.NewGroupCode) {
359
- await Group.checkDuplicateGroupCode(dbTransaction, group.NewGroupCode);
360
- }
361
-
362
- if (
363
- group.ParentGroupCode &&
364
- currentGroup.ParentGroupCode !== group.ParentGroupCode
365
- ) {
366
- const parentGroup = await Group.init(
367
- dbTransaction,
368
- group.ParentGroupCode,
369
- );
370
- if (!parentGroup) {
371
- throw new ClassError(
372
- 'Group',
373
- 'GroupErrMsg08',
374
- 'Parent Group Code not found',
375
- );
376
- }
377
- }
378
-
379
- const entityValueBefore = {
380
- GroupCode: currentGroup.GroupCode,
381
- Name: currentGroup.Name,
382
- Type: currentGroup.Type,
383
- Description: currentGroup.Description,
384
- ParentGroupCode: currentGroup.ParentGroupCode,
385
- InheritParentPrivilegeYN: currentGroup.InheritParentPrivilegeYN,
386
- InheritParentSystemAccessYN: currentGroup.InheritParentSystemAccessYN,
387
- Status: currentGroup.Status,
388
- CreatedById: currentGroup._CreatedById,
389
- UpdatedById: currentGroup._UpdatedById,
390
- CreatedAt: currentGroup._CreatedAt,
391
- UpdatedAt: currentGroup._UpdatedAt,
392
- };
393
-
394
- currentGroup.GroupCode = group?.NewGroupCode || currentGroup.GroupCode;
395
- currentGroup.Name = group?.Name || currentGroup.Name;
396
- currentGroup.Type = group?.Type || currentGroup.Type;
397
- currentGroup.Description = group?.Description || currentGroup.Description;
398
- currentGroup.ParentGroupCode =
399
- group?.ParentGroupCode || currentGroup.ParentGroupCode;
400
- currentGroup.InheritParentPrivilegeYN =
401
- group?.InheritParentPrivilegeYN ||
402
- currentGroup.InheritParentPrivilegeYN;
403
- currentGroup.InheritParentSystemAccessYN =
404
- group?.InheritParentSystemAccessYN ||
405
- currentGroup.InheritParentSystemAccessYN;
406
- currentGroup.Status = group?.Status || currentGroup.Status;
407
- currentGroup._UpdatedById = loginUser.UserId;
408
- currentGroup._UpdatedAt = new Date();
409
-
410
- await Group._Repo.update(
411
- {
412
- GroupCode: currentGroup.GroupCode,
413
- Name: currentGroup.Name,
414
- Type: currentGroup.Type,
415
- Description: currentGroup.Description,
416
- ParentGroupCode: currentGroup.ParentGroupCode,
417
- InheritParentPrivilegeYN: currentGroup.InheritParentPrivilegeYN,
418
- InheritParentSystemAccessYN: currentGroup.InheritParentSystemAccessYN,
419
- Status: currentGroup.Status,
420
- UpdatedById: currentGroup._UpdatedById,
421
- UpdatedAt: currentGroup._UpdatedAt,
422
- },
423
- {
424
- where: {
425
- GroupCode: group.GroupCode,
426
- },
427
- transaction: dbTransaction,
428
- },
429
- );
430
-
431
- const entityValueAfter = {
432
- GroupCode: currentGroup.GroupCode,
433
- Name: currentGroup.Name,
434
- Type: currentGroup.Type,
435
- Description: currentGroup.Description,
436
- ParentGroupCode: currentGroup.ParentGroupCode,
437
- InheritParentPrivilegeYN: currentGroup.InheritParentPrivilegeYN,
438
- InheritParentSystemAccessYN: currentGroup.InheritParentSystemAccessYN,
439
- Status: currentGroup.Status,
440
- CreatedById: currentGroup._CreatedById,
441
- UpdatedById: currentGroup._UpdatedById,
442
- CreatedAt: currentGroup._CreatedAt,
443
- UpdatedAt: currentGroup._UpdatedAt,
444
- };
445
-
446
- const activity = new Activity();
447
- activity.ActivityId = activity.createId();
448
- activity.Action = ActionEnum.UPDATE;
449
- activity.Description = `Update Group ${group.Type}`;
450
- activity.EntityType = 'Group';
451
- activity.EntityId = group.GroupCode;
452
- activity.EntityValueBefore = JSON.stringify(entityValueBefore);
453
- activity.EntityValueAfter = JSON.stringify(entityValueAfter);
454
- await activity.create(loginUser.ObjectId, dbTransaction);
455
-
456
- return currentGroup;
457
- } catch (error) {
458
- throw error;
459
- }
460
- }
461
-
462
- public static async getSystemAccesses(
463
- loginUser: LoginUser,
464
- dbTransaction: any,
465
- GroupCode: string,
466
- Page: number,
467
- Rows: number,
468
- Search: {
469
- SystemCode?: string;
470
- Status?: string;
471
- },
472
- ) {
473
- // Part 1: Privilege Checking
474
- const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
475
- const isPrivileged = await loginUser.checkPrivileges(
476
- systemCode,
477
- 'SYSTEM_ACCESS_VIEW',
478
- );
479
-
480
- if (!isPrivileged) {
481
- throw new ClassError(
482
- 'Group',
483
- 'GroupErrMsg06',
484
- 'You do not have the privilege to view system access',
485
- );
486
- }
487
-
488
- try {
489
- // Part 2: Validation
490
- await Group.init(dbTransaction, GroupCode);
491
-
492
- // Part 3: Retrieve System Access and returns
493
- const queryObj: any = { GroupCode: GroupCode };
494
-
495
- if (Search) {
496
- Object.entries(Search).forEach(([key, value]) => {
497
- queryObj[key] = value;
498
- });
499
- }
500
-
501
- let options: any = {
502
- where: queryObj,
503
- distinct: true,
504
- transaction: dbTransaction,
505
- };
506
-
507
- if (Page && Rows) {
508
- options = {
509
- ...options,
510
- limit: Rows,
511
- offset: Rows * (Page - 1),
512
- order: [['CreatedAt', 'DESC']],
513
- };
514
- }
515
-
516
- const systemAccess = await Group._GroupSystemAccessRepo.findAndCountAll(
517
- options,
518
- );
519
- return systemAccess;
520
- } catch (error) {
521
- return error;
522
- }
523
- }
524
-
525
- private static async getInheritedSystemAccess(
526
- dbTransaction: any,
527
- group: Group,
528
- ): Promise<any[]> {
529
- const options: any = {
530
- where: {
531
- GroupCode: group.GroupCode,
532
- Status: 'Active',
533
- },
534
- include: [
535
- {
536
- model: SystemModel,
537
- },
538
- ],
539
- transaction: dbTransaction,
540
- };
541
- let systemAccess = await Group._GroupSystemAccessRepo.findAll(options);
542
-
543
- if (group.InheritParentSystemAccessYN === 'Y' && group.ParentGroupCode) {
544
- const parentGroup = await Group.init(
545
- dbTransaction,
546
- group.ParentGroupCode,
547
- );
548
- const parentSystemAccesses = await this.getInheritedSystemAccess(
549
- dbTransaction,
550
- parentGroup,
551
- );
552
- systemAccess = systemAccess.concat(parentSystemAccesses);
553
- }
554
- return systemAccess;
555
- }
556
-
557
- public static async getParentSystemAccesses(
558
- loginUser: LoginUser,
559
- dbTransaction: any,
560
- GroupCode: string,
561
- ) {
562
- // Part 1: Privilege Checking
563
- const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
564
- const isPrivileged = await loginUser.checkPrivileges(
565
- systemCode,
566
- 'SYSTEM_ACCESS_VIEW',
567
- );
568
-
569
- if (!isPrivileged) {
570
- throw new ClassError(
571
- 'Group',
572
- 'GroupErrMsg06',
573
- 'You do not have the privilege to view system access',
574
- );
575
- }
576
-
577
- try {
578
- const group = await Group.init(dbTransaction, GroupCode);
579
- if (group.InheritParentSystemAccessYN !== 'Y' && !group.ParentGroupCode) {
580
- return [];
581
- } else {
582
- const parentGroup = await Group.init(
583
- dbTransaction,
584
- group.ParentGroupCode,
585
- );
586
- const inheritSystemAccess = await Group.getInheritedSystemAccess(
587
- dbTransaction,
588
- parentGroup,
589
- );
590
- return inheritSystemAccess;
591
- }
592
- } catch (error) {
593
- throw error;
594
- }
595
- }
596
-
597
- public static async addSystemAccesses(
598
- loginUser: LoginUser,
599
- dbTransaction: any,
600
- GroupCode: string,
601
- SystemCodes: string[],
602
- ) {
603
- // Part 1: Privilege Checking
604
- const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
605
- const isPrivileged = await loginUser.checkPrivileges(
606
- systemCode,
607
- 'SYSTEM_ACCESS_CREATE',
608
- );
609
-
610
- if (!isPrivileged) {
611
- throw new ClassError(
612
- 'Group',
613
- 'GroupErrMsg07',
614
- 'You do not have the privilege to create system access',
615
- );
616
- }
617
-
618
- try {
619
- if (SystemCodes.length > 0) {
620
- for (const element of SystemCodes) {
621
- const CurrentGroupSystemAccess = await Group.getSystemAccesses(
622
- loginUser,
623
- dbTransaction,
624
- GroupCode,
625
- 1,
626
- Number.MAX_SAFE_INTEGER,
627
- { SystemCode: element },
628
- );
629
-
630
- if (CurrentGroupSystemAccess?.count > 0) {
631
- throw new ClassError(
632
- 'Group',
633
- 'GroupErrMsg08',
634
- 'System access already exists',
635
- );
636
- }
637
-
638
- const groupSystemAccess = await GroupSystemAccess.init(dbTransaction);
639
- groupSystemAccess.createId();
640
- groupSystemAccess.GroupCode = GroupCode;
641
- groupSystemAccess.SystemCode = element;
642
- groupSystemAccess.Status = 'Active';
643
- groupSystemAccess.CreatedById = +loginUser.ObjectId;
644
- groupSystemAccess.CreatedAt = new Date();
645
- groupSystemAccess.UpdatedById = +loginUser.ObjectId;
646
- groupSystemAccess.UpdatedAt = new Date();
647
-
648
- const EntityValueAfter = {
649
- GroupCode: groupSystemAccess.GroupCode,
650
- SystemCode: groupSystemAccess.SystemCode,
651
- Status: groupSystemAccess.Status,
652
- CreatedById: groupSystemAccess.CreatedById,
653
- CreatedAt: groupSystemAccess.CreatedAt,
654
- UpdatedById: groupSystemAccess.UpdatedById,
655
- UpdatedAt: groupSystemAccess.UpdatedAt,
656
- };
657
-
658
- const systemAccess = await Group._GroupSystemAccessRepo.create(
659
- EntityValueAfter,
660
- {
661
- transaction: dbTransaction,
662
- },
663
- );
664
-
665
- const activity = new Activity();
666
- activity.ActivityId = activity.createId();
667
- activity.Action = ActionEnum.ADD;
668
- activity.Description = 'Create Group System Access';
669
- activity.EntityType = 'GroupSystemAccess';
670
- activity.EntityId = systemAccess.GroupSystemAccessId?.toString();
671
- activity.EntityValueBefore = JSON.stringify({});
672
- activity.EntityValueAfter = JSON.stringify(EntityValueAfter);
673
-
674
- await activity.create(loginUser.ObjectId, dbTransaction);
675
- }
676
-
677
- return { Message: 'Successfully added.' };
678
- }
679
- } catch (error) {
680
- throw error;
681
- }
682
- }
683
-
684
- public static async deleteSystemAccess(
685
- loginUser: LoginUser,
686
- dbTransaction: any,
687
- GroupCode: string,
688
- SystemCode: string,
689
- ) {
690
- // Part 1: Privilege Checking
691
- const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
692
- const isPrivileged = await loginUser.checkPrivileges(
693
- systemCode,
694
- 'SYSTEM_ACCESS_DELETE',
695
- );
696
-
697
- if (!isPrivileged) {
698
- throw new ClassError(
699
- 'Group',
700
- 'GroupErrMsg08',
701
- 'You do not have the privilege to delete system access',
702
- );
703
- }
704
-
705
- try {
706
- const currentGroupSystemAccess = await Group.getSystemAccesses(
707
- loginUser,
708
- dbTransaction,
709
- GroupCode,
710
- 1,
711
- Number.MAX_SAFE_INTEGER,
712
- { SystemCode: SystemCode },
713
- );
714
-
715
- if (currentGroupSystemAccess.count < 1) {
716
- throw new ClassError(
717
- 'Group',
718
- 'GroupErrMsg10',
719
- 'No associated system access found.',
720
- );
721
- }
722
-
723
- await Group._GroupSystemAccessRepo.delete(
724
- GroupCode,
725
- SystemCode,
726
- dbTransaction,
727
- );
728
-
729
- const EntityValueBefore = {
730
- GroupCode: currentGroupSystemAccess?.rows[0]?.GroupCode,
731
- SystemCode: currentGroupSystemAccess?.rows[0]?.SystemCode,
732
- Status: currentGroupSystemAccess?.rows[0]?.Status,
733
- CreatedById: currentGroupSystemAccess?.rows[0]?.CreatedById,
734
- CreatedAt: currentGroupSystemAccess?.rows[0]?.CreatedAt,
735
- UpdatedById: currentGroupSystemAccess?.rows[0]?.UpdatedById,
736
- UpdatedAt: currentGroupSystemAccess?.rows[0]?.UpdatedAt,
737
- };
738
-
739
- const activity = new Activity();
740
- activity.ActivityId = activity.createId();
741
- activity.Action = ActionEnum.DELETE;
742
- activity.Description = 'Delete Group System Access';
743
- activity.EntityType = 'GroupSystemAccess';
744
- activity.EntityId =
745
- currentGroupSystemAccess?.rows[0]?.GroupSystemAccessId?.toString();
746
- activity.EntityValueBefore = JSON.stringify(EntityValueBefore);
747
- activity.EntityValueAfter = JSON.stringify({});
748
-
749
- await activity.create(loginUser.ObjectId, dbTransaction);
750
-
751
- return { Message: 'System access removed.', SystemCode: SystemCode };
752
- } catch (error) {
753
- throw error;
754
- }
755
- }
756
-
757
- public static async getSystemPrivileges(
758
- loginUser: LoginUser,
759
- dbTransaction: any,
760
- GroupCode: string,
761
- search?: {
762
- SystemCode?: string;
763
- Status?: string;
764
- },
765
- ) {
766
- try {
767
- //Part 1: Privilege Checking
768
- const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
769
- const isPrivileged = await loginUser.checkPrivileges(
770
- systemCode,
771
- 'GROUP_PRIVILEGE_VIEW',
772
- );
773
-
774
- if (!isPrivileged) {
775
- throw new ClassError(
776
- 'Group',
777
- 'GroupErrMsg11',
778
- 'You do not have the privilege to view group privileges',
779
- );
780
- }
781
-
782
- //Set group to instantiation of existing Group
783
- await Group.init(dbTransaction, GroupCode);
784
-
785
- //Part 3: Retrieve Group Own Privilege
786
- //Retrieve group data and it's privileged by calling Group._Repo.findAll
787
- let where: any = {
788
- GroupCode,
789
- };
790
-
791
- let systemWhere: any = {};
792
-
793
- if (search) {
794
- if (search.Status) {
795
- where = {
796
- ...where,
797
- Status: search.Status,
798
- };
799
- }
800
-
801
- if (search.SystemCode) {
802
- systemWhere = {
803
- SystemCode: {
804
- [Op.substring]: search.SystemCode,
805
- },
806
- };
807
- }
808
- }
809
-
810
- const groupOwnPrivileges = await Group._GroupPrivilegeRepo.findAll({
811
- where,
812
- include: [
813
- {
814
- model: SystemPrivilegeModel,
815
- where: systemWhere,
816
- },
817
- ],
818
- transaction: dbTransaction,
819
- });
820
-
821
- //Create variable called privileges and Map the SystemPrivilege data retrieved from 3.1 into SystemPrivilege object and push it to the privileges variables
822
- let privileges: SystemPrivilege[] = [];
823
-
824
- for (const groupPrivilege of groupOwnPrivileges) {
825
- const systemPrivilege = await SystemPrivilege.init(
826
- dbTransaction,
827
- );
828
- systemPrivilege.setAttributes(groupPrivilege.Privilege.get({ plain: true }));
829
- privileges.push(systemPrivilege);
830
- }
831
-
832
- return privileges;
833
- } catch (error) {
834
- throw error;
835
- }
836
- }
837
-
838
- public static async getInheritedSystemPrivileges(
839
- dbTransaction: any,
840
- GroupCode: string,
841
- search?: {
842
- SystemCode?: string;
843
- Status?: string;
844
- PrivilegeCode?: string;
845
- },
846
- ): Promise<SystemPrivilege[]> {
847
- try {
848
- //Retrieve group data and it's privileges by calling Group._Repo.findAll
849
- let where: any = {
850
- GroupCode,
851
- };
852
-
853
- let groupPrivilegeWhere: any = {};
854
- let systemPrivilegeWhere: any = {};
855
-
856
- if (search) {
857
- if (search.Status) {
858
- groupPrivilegeWhere = {
859
- Status: search.Status,
860
- };
861
- }
862
-
863
- if (search.SystemCode) {
864
- systemPrivilegeWhere = {
865
- SystemCode: {
866
- [Op.substring]: search.SystemCode,
867
- },
868
- };
869
- }
870
- }
871
- const group = await Group._Repo.findOne({
872
- where: where,
873
- include: [
874
- {
875
- model: GroupPrivilegeModel,
876
- where: groupPrivilegeWhere,
877
- include: [
878
- {
879
- model: SystemPrivilegeModel,
880
- where: systemPrivilegeWhere,
881
- },
882
- ],
883
- },
884
- ],
885
- transaction: dbTransaction,
886
- });
887
-
888
- //Retrieve group object privileges by calling LoginUser._GroupObjectPrivilegeRepo.findAll
889
- let objectWhere: any = {
890
- GroupCode,
891
- };
892
- let systemWhere: any = {};
893
- if (search) {
894
- Object.entries(search).forEach(([key, value]) => {
895
- if (key === 'SystemCode') {
896
- systemWhere[key] = {
897
- [Op.substring]: value,
898
- };
899
- } else {
900
- objectWhere[key] = {
901
- [Op.substring]: value,
902
- };
903
- }
904
- });
905
- }
906
- const groupObjectPrivileges = await Group._GroupObjectPrivilegeRepo.findAll({
907
- where: objectWhere,
908
- include: [
909
- {
910
- model: SystemPrivilegeModel,
911
- where: systemWhere,
912
- },
913
- ],
914
- transaction: dbTransaction,
915
- });
916
-
917
- //Map to SystemPrivilege object
918
- let privileges: SystemPrivilege[] = [];
919
- for (const groupPrivilege of group.GroupPrivileges) {
920
- const systemPrivilege = await SystemPrivilege.init(dbTransaction);
921
- systemPrivilege.setAttributes(groupPrivilege.Privilege.get({ plain: true }));
922
- privileges.push(systemPrivilege);
923
- }
924
-
925
- for (const groupObjectPrivilege of groupObjectPrivileges) {
926
- const systemPrivilege = await SystemPrivilege.init(dbTransaction);
927
- systemPrivilege.setAttributes(groupObjectPrivilege.Privilege.get({ plain: true }));
928
- privileges.push(systemPrivilege);
929
- }
930
-
931
- //Part 2: Retrieve Privileges Inherited from Parent Group
932
- //if group data retrieved from 1.1 have InheritParentPrivilegeYN == "Y" and ParentGroupCode value is not empty. Call this method again
933
- if (group.InheritParentPrivilegeYN === 'Y' && group.ParentGroupCode) {
934
- const inheritedPrivileges = await Group.getInheritedSystemPrivileges(
935
- dbTransaction,
936
- group.ParentGroupCode,
937
- search,
938
- );
939
- privileges = privileges.concat(inheritedPrivileges);
940
- }
941
-
942
- //format to make sure no duplicate
943
- const uniquePrivileges = Array.from(new Set(privileges.map(a => a.PrivilegeCode)))
944
- .map(PrivilegeCode => {
945
- return privileges.find(a => a.PrivilegeCode === PrivilegeCode);
946
- });
947
-
948
- return uniquePrivileges;
949
- } catch (error) {
950
- throw error;
951
- }
952
- }
953
-
954
- public static async getParentSystemPrivileges(
955
- loginUser: LoginUser,
956
- dbTransaction: any,
957
- GroupCode: string,
958
- search?: {
959
- SystemCode?: string;
960
- Status?: string;
961
- PrivilegeCode?: string;
962
- },
963
- ): Promise<SystemPrivilege[]> {
964
- try {
965
- //Part 1: Privilege Checking
966
- const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
967
- const isPrivileged = await loginUser.checkPrivileges(
968
- systemCode,
969
- 'GROUP_PRIVILEGE_VIEW',
970
- );
971
-
972
- if (!isPrivileged) {
973
- throw new ClassError(
974
- 'Group',
975
- 'GroupErrMsg11',
976
- 'You do not have the privilege to view group privileges',
977
- );
978
- }
979
-
980
- //Part 2: Validation
981
- //Set group to instantiation of existing Group
982
- const group = await Group.init(dbTransaction, GroupCode);
983
- //Check if group.InheritParentPrivilegeYN == "Y" and ParentGroupCode value is not empty. if no then return an empty array
984
- if (group.InheritParentPrivilegeYN !== 'Y' && !group.ParentGroupCode) {
985
- return [];
986
- }
987
-
988
- //Part 3: Retrieve Group Own Privilege
989
- //Retrieve group data and it's privileged by calling Group.getIheritedSystemPrivileges
990
- const privileges = await Group.getInheritedSystemPrivileges(
991
- dbTransaction,
992
- group.ParentGroupCode,
993
- search,
994
- );
995
-
996
- return privileges;
997
- } catch (error) {
998
- throw error;
999
- }
1000
- }
1001
-
1002
- public static async assignGroupObjectPrivilege(
1003
- loginUser: LoginUser,
1004
- dbTransaction: any,
1005
- GroupCode: string,
1006
- GroupObjectPrivileges: GroupObjectPrivilege[]
1007
- ): Promise<string> {
1008
- try {
1009
- //Part 1: Privilege Checking
1010
- const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
1011
- const isPrivileged = await loginUser.checkPrivileges(
1012
- systemCode,
1013
- 'GROUP_OBJECT_PRIVILEGE_ASSIGN',
1014
- );
1015
-
1016
- if (!isPrivileged) {
1017
- throw new ClassError(
1018
- 'Group',
1019
- 'GroupErrMsg12',
1020
- 'You do not have the privilege to assign group object privilege',
1021
- );
1022
- }
1023
-
1024
- //Part 2: Validation
1025
- //Initialise group with group init
1026
- const group = await Group.init(dbTransaction, GroupCode);
1027
- //Retrieve all group system access by calling Group.getSystemAccesses
1028
- const groupSystemAccesses = await Group.getSystemAccesses(
1029
- loginUser,
1030
- dbTransaction,
1031
- GroupCode,
1032
- 1,
1033
- Number.MAX_SAFE_INTEGER,
1034
- {},
1035
- );
1036
-
1037
- //If Group.InheritParentSystemAccess == "Y" and Group.ParentGroupCode exist, initialise parent group
1038
- let parentGroupSystemAccesses: any = {};
1039
- if (group.InheritParentSystemAccessYN === 'Y' && group.ParentGroupCode) {
1040
- //Retrieve all parent group system access by calling Group.getSystemAccesses
1041
- parentGroupSystemAccesses = await Group.getSystemAccesses(
1042
- loginUser,
1043
- dbTransaction,
1044
- group.ParentGroupCode,
1045
- 1,
1046
- Number.MAX_SAFE_INTEGER,
1047
- {},
1048
- );
1049
- }
1050
-
1051
- // For each Params.GroupObjectPrivileges.
1052
- for (const groupObjectPrivilege of GroupObjectPrivileges) {
1053
- //Initialise existing System privilege
1054
- const systemPrivilege = await SystemPrivilege.init(dbTransaction, groupObjectPrivilege.PrivilegeCode);
1055
- //Check whether the system codes used by that privilege is exist inside the group system access
1056
- const combinedSystemAccesses = {
1057
- ...groupSystemAccesses.rows,
1058
- ...parentGroupSystemAccesses.rows,
1059
- };
1060
- const systemAccess = combinedSystemAccesses.find(
1061
- (systemAccess) => systemAccess.SystemCode === systemPrivilege.SystemCode,
1062
- );
1063
- if (!systemAccess) {
1064
- throw new ClassError(
1065
- 'Group',
1066
- 'GroupErrMsg13',
1067
- 'Failed to assign privilege ' + groupObjectPrivilege.PrivilegeCode + ' due to non-existent system access.',
1068
- );
1069
- }
1070
-
1071
- //Check whether the group object privilege already exist by using Group._GroupObjectPrivilegesRepo.findOne
1072
- const groupObjectPrivilegeData = await Group._GroupObjectPrivilegeRepo.findOne({
1073
- where: {
1074
- GroupCode,
1075
- PrivilegeCode: groupObjectPrivilege.PrivilegeCode,
1076
- ObjectId: groupObjectPrivilege.ObjectId,
1077
- ObjectType: groupObjectPrivilege.ObjectType,
1078
- },
1079
- transaction: dbTransaction,
1080
- });
1081
- //If GroupObjectPrivilege record exist. Skip this loop and proceed to the next privilege code
1082
- if (groupObjectPrivilegeData) {
1083
- continue;
1084
- } else {
1085
- //Call GroupObjectPrivilege.create
1086
- await GroupObjectPrivilege.create(
1087
- loginUser,
1088
- dbTransaction,
1089
- groupObjectPrivilege,
1090
- );
1091
- }
1092
- }
1093
-
1094
- return 'Successfully added.';
1095
- } catch (error) {
1096
- throw error;
1097
- }
1098
- }
1099
-
1100
- public static async getGroubObjectPrivileges(
1101
- loginUser: LoginUser,
1102
- dbTransaction: any,
1103
- GroupCode: string,
1104
- search?: {
1105
- PrivilegeCode?: string;
1106
- ObjectType?: string;
1107
- ObjectId?: string;
1108
- SystemCode?: string;
1109
- }
1110
- ): Promise<SystemPrivilege[]> {
1111
- try {
1112
- // Part 1: Privilege Checking
1113
- const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
1114
- const isPrivileged = await loginUser.checkPrivileges(
1115
- systemCode,
1116
- 'GROUP_PRIVILEGE_VIEW',
1117
- );
1118
-
1119
- if (!isPrivileged) {
1120
- throw new ClassError(
1121
- 'Group',
1122
- 'GroupErrMsg11',
1123
- 'You do not have the privilege to view group privileges',
1124
- );
1125
- }
1126
-
1127
- // Part 2: Validation
1128
- // Set group to instantiation of existing Group
1129
- await Group.init(dbTransaction, GroupCode);
1130
-
1131
- // Part 3: Retrieve Group Own Privilege
1132
- // Retrieve group object privileges by calling LoginUser._GroupObjectPrivilegeRepo.findAll
1133
- let where: any = {
1134
- GroupCode,
1135
- };
1136
-
1137
- let systemWhere: any = {};
1138
-
1139
- if (search) {
1140
- Object.entries(search).forEach(([key, value]) => {
1141
- if (key === 'SystemCode') {
1142
- systemWhere[key] = {
1143
- [Op.substring]: value,
1144
- };
1145
- } else {
1146
- where[key] = {
1147
- [Op.substring]: value,
1148
- };
1149
- }
1150
- });
1151
- }
1152
-
1153
- const groupObjectPrivileges = await Group._GroupObjectPrivilegeRepo.findAll({
1154
- where,
1155
- include: [
1156
- {
1157
- model: SystemPrivilegeModel,
1158
- where: systemWhere,
1159
- },
1160
- ],
1161
- transaction: dbTransaction,
1162
- });
1163
- // Create variable called privileges and Map the SystemPrivilege data retrieved from 3.1 into SystemPrivilege object and push it to the privileges variables
1164
- let privileges: SystemPrivilege[] = [];
1165
- for (const groupObjectPrivilege of groupObjectPrivileges) {
1166
- const systemPrivilege = await SystemPrivilege.init(
1167
- dbTransaction,
1168
- );
1169
- systemPrivilege.setAttributes(groupObjectPrivilege.Privilege.get({ plain: true }));
1170
- privileges.push(systemPrivilege);
1171
- }
1172
-
1173
- //Remove duplicate
1174
- const uniquePrivileges = Array.from(new Set(privileges.map(a => a.PrivilegeCode)))
1175
- .map(PrivilegeCode => {
1176
- return privileges.find(a => a.PrivilegeCode === PrivilegeCode);
1177
- });
1178
-
1179
- // Create the result based on the spec on return then returns it.
1180
- return uniquePrivileges;
1181
- } catch (error) {
1182
- throw error;
1183
- }
1184
- }
1185
-
1186
- public static async assignGroupPrivileges(
1187
- loginUser: LoginUser,
1188
- dbTransaction: any,
1189
- GroupCode: string,
1190
- PrivilegeCodes: string[],
1191
- ) {
1192
- try {
1193
- // Part 1: Privilege Checking
1194
- const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
1195
- const isPrivileged = await loginUser.checkPrivileges(
1196
- systemCode,
1197
- 'GROUP_PRIVILEGE_ASSIGN',
1198
- );
1199
-
1200
- if (!isPrivileged) {
1201
- throw new ClassError(
1202
- 'Group',
1203
- 'GroupErrMsg06',
1204
- 'You do not have the privilege to assign group privileges',
1205
- );
1206
- }
1207
-
1208
- // Part 2: Validation, Create and Record Activity
1209
- // Initialise group with group init
1210
-
1211
- const group = await Group.init(dbTransaction, GroupCode);
1212
-
1213
- // Retrieve all group system access by calling Group.getSystemAccess
1214
- const groupSystemAccesses = await Group.getSystemAccesses(
1215
- loginUser,
1216
- dbTransaction,
1217
- GroupCode,
1218
- 1,
1219
- Number.MAX_SAFE_INTEGER,
1220
- {},
1221
- );
1222
-
1223
- // If Group.InheritParentSystemAccess == "Y" and Group.ParentGroupCode exist
1224
- let parentGroupSystemAccesses: any = {};
1225
- if (group.InheritParentSystemAccessYN === 'Y' && group.ParentGroupCode) {
1226
- // Retrieve all parent group system access by calling Group.getSystemAccess
1227
- parentGroupSystemAccesses = await Group.getSystemAccesses(
1228
- loginUser,
1229
- dbTransaction,
1230
- group.ParentGroupCode,
1231
- 1,
1232
- Number.MAX_SAFE_INTEGER,
1233
- {},
1234
- );
1235
- }
1236
-
1237
- // For each Params.PrivilegesCodes.
1238
- for (const PrivilegeCode of PrivilegeCodes) {
1239
- // Initialise existing System privilege by calling SystemPrivilege.init
1240
- const systemPrivilege = await SystemPrivilege.init(dbTransaction, PrivilegeCode);
1241
- //Check whether the system codes used by that privilege is exist inside the group system access retrieved from step 2.2 & 2.4. If system code does not exist in group system access, throw a new ClassError by passing:
1242
- // Classname: "Group"
1243
- // MessageCode: "GroupErrMsg0X"
1244
- // Message: "Failed to assign privilege <PrivilegeCode> due to non-existent system access."
1245
- const combinedSystemAccesses = {
1246
- ...groupSystemAccesses.rows,
1247
- ...parentGroupSystemAccesses.rows,
1248
- };
1249
- const systemAccess = combinedSystemAccesses.find(
1250
- (systemAccess) => systemAccess.SystemCode === systemPrivilege.SystemCode,
1251
- );
1252
- if (!systemAccess) {
1253
- throw new ClassError(
1254
- 'Group',
1255
- 'GroupErrMsg13',
1256
- 'Failed to assign privilege ' + PrivilegeCode + ' due to non-existent system access.',
1257
- );
1258
- }
1259
-
1260
- //Check whether the group privilege exist by using Group._GroupPrivilegesRepo.findOne
1261
- const groupPrivilege = await Group._GroupPrivilegeRepo.findOne({
1262
- where: {
1263
- GroupCode,
1264
- PrivilegeCode,
1265
- },
1266
- transaction: dbTransaction,
1267
- });
1268
-
1269
- //If GroupPrivilege record exist and status is "Active". Skip this loop and proceed to the next privilege code
1270
- if (groupPrivilege && groupPrivilege.Status === 'Active') {
1271
- continue;
1272
- }
1273
-
1274
- let entityValueBefore = {};
1275
- let entityValueAfter = {};
1276
- let action = ActionEnum.ADD;
1277
- let description = 'Create Group Privilege';
1278
- let entityId = null;
1279
- //If GroupPrivilege record exist and status is not "Active" do the following:
1280
- if (groupPrivilege && groupPrivilege.Status !== 'Active') {
1281
- //Set this GroupPrivilege entity as EntityValueBefore
1282
- entityValueBefore = {
1283
- GroupCode: groupPrivilege.GroupCode,
1284
- PrivilegeCode: groupPrivilege.PrivilegeCode,
1285
- Status: groupPrivilege.Status,
1286
- CreatedById: groupPrivilege.CreatedById,
1287
- CreatedAt: groupPrivilege.CreatedAt,
1288
- UpdatedById: groupPrivilege.UpdatedById,
1289
- UpdatedAt: groupPrivilege.UpdatedAt,
1290
- };
1291
-
1292
- //Update the status to active using Group._GroupPrivilegesRepo.Update.
1293
- const updatedPayload = {
1294
- Status: 'Active',
1295
- UpdatedById: loginUser.UserId,
1296
- UpdatedAt: new Date(),
1297
- };
1298
- await Group._GroupPrivilegeRepo.update(
1299
- updatedPayload,
1300
- {
1301
- where: {
1302
- GroupCode,
1303
- PrivilegeCode,
1304
- },
1305
- transaction: dbTransaction,
1306
- },
1307
- );
1308
-
1309
- //Set updated GroupPrivilege as EntityValueAfter
1310
- entityValueAfter = {
1311
- GroupCode: groupPrivilege.GroupCode,
1312
- PrivilegeCode: groupPrivilege.PrivilegeCode,
1313
- Status: updatedPayload.Status,
1314
- CreatedById: groupPrivilege.CreatedById,
1315
- CreatedAt: groupPrivilege.CreatedAt,
1316
- UpdatedById: updatedPayload.UpdatedById,
1317
- UpdatedAt: updatedPayload.UpdatedAt,
1318
- };
1319
-
1320
- //Instantiate new activity from Activity class
1321
- action = ActionEnum.UPDATE;
1322
- description = 'Update Group Privilege';
1323
- entityId = groupPrivilege.GroupPrivilegeId;
1324
- } else {
1325
- //If GroupPrivilege record does not exist, do the following:
1326
- //Initialise empty GroupPrivilege.
1327
- const newGroupPrivilege = await GroupPrivilege.init(dbTransaction);
1328
- //Set the attributes
1329
- newGroupPrivilege.setAttributes({
1330
- GroupCode,
1331
- PrivilegeCode,
1332
- Status: 'Active',
1333
- CreatedById: loginUser.UserId,
1334
- CreatedAt: new Date(),
1335
- UpdatedById: loginUser.UserId,
1336
- UpdatedAt: new Date(),
1337
- });
1338
-
1339
- // Set EntityValueAfter to above instance.
1340
- entityValueAfter = {
1341
- GroupCode: newGroupPrivilege.GroupCode,
1342
- PrivilegeCode: newGroupPrivilege.PrivilegeCode,
1343
- Status: newGroupPrivilege.Status,
1344
- CreatedById: newGroupPrivilege.CreatedById,
1345
- CreatedAt: newGroupPrivilege.CreatedAt,
1346
- UpdatedById: newGroupPrivilege.UpdatedById,
1347
- UpdatedAt: newGroupPrivilege.UpdatedAt,
1348
- };
1349
-
1350
- //Call Group._GroupPrivilegesRepo.create
1351
- const groupPrivilege = await Group._GroupPrivilegeRepo.create(entityValueAfter, {
1352
- transaction: dbTransaction,
1353
- });
1354
- action = ActionEnum.ADD;
1355
- description = 'Create Group Privilege';
1356
- entityId = groupPrivilege.GroupPrivilegeId;
1357
- }
1358
-
1359
- //Instantiate new activity from Activity class, call createId() method, then set:
1360
- const activity = new Activity();
1361
- activity.ActivityId = activity.createId();
1362
- activity.Action = action;
1363
- activity.Description = description;
1364
- activity.EntityType = 'GroupPrivilege';
1365
- activity.EntityId = entityId;
1366
- activity.EntityValueBefore = JSON.stringify(entityValueBefore);
1367
- activity.EntityValueAfter = JSON.stringify(entityValueAfter);
1368
-
1369
- //Call new activity create method
1370
- await activity.create(loginUser.ObjectId, dbTransaction);
1371
- }
1372
-
1373
- return 'Successfully added.';
1374
- } catch (error) {
1375
- throw error;
1376
- }
1377
- }
1378
-
1379
- public static async deleteGroupPrivilege(
1380
- loginUser: LoginUser,
1381
- dbTransaction: any,
1382
- GroupCode: string,
1383
- PrivilegeCodes: string[],
1384
- ) {
1385
- try {
1386
- // Part 1: Privilege Checking
1387
- const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
1388
- const isPrivileged = await loginUser.checkPrivileges(
1389
- systemCode,
1390
- 'GROUP_PRIVILEGE_DELETE',
1391
- );
1392
-
1393
- if (!isPrivileged) {
1394
- throw new ClassError(
1395
- 'Group',
1396
- 'GroupErrMsg06',
1397
- 'You do not have the privilege to delete group privileges',
1398
- );
1399
- }
1400
-
1401
- // Part 2: Validation, Create and Record Activity
1402
- // For each Params.PrivilegesCodes.
1403
- for (const PrivilegeCode of PrivilegeCodes) {
1404
- //Check whether the record exist in database by calling Group._GroupPrivilegeRepo.findOne
1405
- const groupPrivilege = await Group._GroupPrivilegeRepo.findOne({
1406
- where: {
1407
- GroupCode,
1408
- PrivilegeCode,
1409
- },
1410
- transaction: dbTransaction,
1411
- });
1412
-
1413
- //If the record does not exist, throw a new ClassError
1414
- if (!groupPrivilege) {
1415
- throw new ClassError(
1416
- 'Group',
1417
- 'GroupErrMsg14',
1418
- 'GroupPrivilege not found.',
1419
- );
1420
- }
1421
-
1422
- //Set the EntityValueBefore to the GroupPrivilegesValue from step 1.c.
1423
- const entityValueBefore = {
1424
- GroupCode: groupPrivilege.GroupCode,
1425
- PrivilegeCode: groupPrivilege.PrivilegeCode,
1426
- Status: groupPrivilege.Status,
1427
- CreatedById: groupPrivilege.CreatedById,
1428
- CreatedAt: groupPrivilege.CreatedAt,
1429
- UpdatedById: groupPrivilege.UpdatedById,
1430
- UpdatedAt: groupPrivilege.UpdatedAt,
1431
- };
1432
-
1433
- //Call Group._GroupPrivilegeRepo.delete
1434
- await Group._GroupPrivilegeRepo.delete(
1435
- GroupCode,
1436
- PrivilegeCode,
1437
- dbTransaction,
1438
- );
1439
- // Instantiate new activity from Activity class, call createId() method, then set:
1440
- const activity = new Activity();
1441
- activity.ActivityId = activity.createId();
1442
- activity.Action = ActionEnum.DELETE;
1443
- activity.Description = 'DELETE Group Privilege';
1444
- activity.EntityType = 'GroupPrivilege';
1445
- activity.EntityId = groupPrivilege.GroupPrivilegeId.toString();
1446
- activity.EntityValueBefore = JSON.stringify(entityValueBefore);
1447
- activity.EntityValueAfter = JSON.stringify({});
1448
- //Call new activity create method
1449
- await activity.create(loginUser.ObjectId, dbTransaction);
1450
- }
1451
- return 'Successfully deleted.';
1452
- } catch (error) {
1453
- throw error;
1454
- }
1455
- }
1456
- }
1
+ import { ClassError, ObjectBase } from '@tomei/general';
2
+ import { GroupRepository } from './group.repository';
3
+ import { IGroupAttr } from '../../interfaces/group.interface';
4
+ import { GroupTypeEnum } from 'enum';
5
+ import { LoginUser } from '../login-user/login-user';
6
+ import { IGroupSearchAttr } from '../../interfaces/group-search-attr.interface';
7
+ import { ApplicationConfig } from '@tomei/config';
8
+ import { Op } from 'sequelize';
9
+ import { ActionEnum, Activity } from '@tomei/activity-history';
10
+ import { GroupSystemAccessRepository } from '../group-system-access/group-system-access.repository';
11
+ import SystemModel from '../../models/system.entity';
12
+ import { GroupSystemAccess } from '../group-system-access';
13
+ import { RedisService } from '../../redis-client/redis.service';
14
+ import SystemPrivilegeModel from '../../models/system-privilege.entity';
15
+ import { GroupPrivilegeRepository } from '../group-privilege/group-privilege.repository';
16
+ import { SystemPrivilege } from '../system-privilege/system-privilege';
17
+ import GroupPrivilegeModel from '../../models/group-privilege.entity';
18
+ import { GroupObjectPrivilegeRepository } from '../group-object-privilege/group-object-privilege.repository';
19
+ import { GroupObjectPrivilege } from '../group-object-privilege/group-object-privilege';
20
+ import { GroupPrivilege } from '../group-privilege/group-privilege';
21
+
22
+ export class Group extends ObjectBase {
23
+ ObjectId: string;
24
+ ObjectName: string;
25
+ TableName: 'sso_Group';
26
+ ObjectType = 'Group';
27
+
28
+ Name: string;
29
+ Description: string;
30
+ Type: GroupTypeEnum;
31
+ ParentGroupCode: string;
32
+ InheritParentPrivilegeYN: string;
33
+ InheritParentSystemAccessYN: string;
34
+ Status: string;
35
+ ParentGroup?: any;
36
+ private _CreatedById: number;
37
+ private _CreatedAt: Date;
38
+ private _UpdatedById: number;
39
+ private _UpdatedAt: Date;
40
+ private static _Repo = new GroupRepository();
41
+ private static _GroupSystemAccessRepo = new GroupSystemAccessRepository();
42
+ private static _GroupPrivilegeRepo = new GroupPrivilegeRepository();
43
+ private static _GroupObjectPrivilegeRepo =
44
+ new GroupObjectPrivilegeRepository();
45
+ private static _RedisService: RedisService;
46
+ get GroupCode(): string {
47
+ return this.ObjectId;
48
+ }
49
+
50
+ set GroupCode(value: string) {
51
+ this.ObjectId = value;
52
+ }
53
+
54
+ get CreatedById(): number {
55
+ return this._CreatedById;
56
+ }
57
+
58
+ get CreatedAt(): Date {
59
+ return this._CreatedAt;
60
+ }
61
+
62
+ get UpdatedById(): number {
63
+ return this._UpdatedById;
64
+ }
65
+
66
+ get UpdatedAt(): Date {
67
+ return this._UpdatedAt;
68
+ }
69
+
70
+ private constructor(groupAttr?: IGroupAttr) {
71
+ super();
72
+ if (groupAttr) {
73
+ this.GroupCode = groupAttr.GroupCode;
74
+ this.Name = groupAttr.Name;
75
+ this.Description = groupAttr?.Description;
76
+ this.Type = groupAttr?.Type;
77
+ this.ParentGroupCode = groupAttr?.ParentGroupCode;
78
+ this.InheritParentPrivilegeYN = groupAttr?.InheritParentPrivilegeYN;
79
+ this.InheritParentSystemAccessYN = groupAttr?.InheritParentSystemAccessYN;
80
+ this.Status = groupAttr?.Status;
81
+ this._CreatedById = groupAttr.CreatedById;
82
+ this._CreatedAt = groupAttr.CreatedAt;
83
+ this._UpdatedById = groupAttr.UpdatedById;
84
+ this._UpdatedAt = groupAttr.UpdatedAt;
85
+ }
86
+ }
87
+
88
+ public static async init(dbTransaction: any, GroupCode?: string) {
89
+ try {
90
+ Group._RedisService = await RedisService.init();
91
+ if (GroupCode) {
92
+ const group = await Group._Repo.findByPk(GroupCode, {
93
+ transaction: dbTransaction,
94
+ });
95
+ if (group) {
96
+ return new Group(group);
97
+ } else {
98
+ throw Error('Group not found');
99
+ }
100
+ }
101
+ return new Group();
102
+ } catch (error) {
103
+ throw new ClassError(
104
+ 'Group',
105
+ 'GroupErrMsg01',
106
+ 'Failed To Initialize Group',
107
+ );
108
+ }
109
+ }
110
+
111
+ public static async findAll(
112
+ page: number,
113
+ row: number,
114
+ dbTransaction: any,
115
+ loginUser: LoginUser,
116
+ search?: IGroupSearchAttr,
117
+ ) {
118
+ //This method will list all group based on the query params.
119
+ //Part 1: Privilege Checking
120
+ const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
121
+ const isPrivileged = await loginUser.checkPrivileges(
122
+ systemCode,
123
+ 'GROUP_LIST',
124
+ );
125
+
126
+ if (!isPrivileged) {
127
+ throw new ClassError(
128
+ 'Group',
129
+ 'GroupErrMsg04',
130
+ 'User is not privileged to list group',
131
+ );
132
+ }
133
+
134
+ //Part 2: Retrieve listing
135
+ const queryObj: any = {};
136
+
137
+ let options: any = {
138
+ transaction: dbTransaction,
139
+ };
140
+
141
+ if (page && row) {
142
+ options = {
143
+ ...options,
144
+ limit: row,
145
+ offset: row * (page - 1),
146
+ order: [['CreatedAt', 'DESC']],
147
+ };
148
+ }
149
+
150
+ if (search) {
151
+ Object.entries(search).forEach(([key, value]) => {
152
+ queryObj[key] = {
153
+ [Op.substring]: value,
154
+ };
155
+ });
156
+
157
+ options = {
158
+ ...options,
159
+ where: queryObj,
160
+ };
161
+
162
+ const result = await Group._Repo.findAllWithPagination(options);
163
+
164
+ //Map the result to Group instance
165
+ return {
166
+ Count: result.count,
167
+ Groups: result.rows.map(
168
+ (group) => new Group(group.get({ plain: true })),
169
+ ),
170
+ };
171
+ }
172
+ }
173
+
174
+ public static async create(
175
+ loginUser: LoginUser,
176
+ dbTransaction: any,
177
+ group: Group,
178
+ ) {
179
+ try {
180
+ //Part 1: Privilege Checking
181
+ const systemCode =
182
+ ApplicationConfig.getComponentConfigValue('system-code');
183
+ const isPrivileged = await loginUser.checkPrivileges(
184
+ systemCode,
185
+ 'GROUP_CREATE',
186
+ );
187
+ if (!isPrivileged) {
188
+ throw new Error('You do not have permission to create group');
189
+ }
190
+
191
+ //Part 2: Validation
192
+ if (!group.GroupCode) {
193
+ throw new ClassError(
194
+ 'Group',
195
+ 'GroupErrMsg02',
196
+ 'Group Code is required',
197
+ );
198
+ }
199
+
200
+ if (!group.Name) {
201
+ throw new ClassError(
202
+ 'Group',
203
+ 'GroupErrMsg02',
204
+ 'Group Name is required',
205
+ );
206
+ }
207
+
208
+ if (!group.Type) {
209
+ throw new ClassError(
210
+ 'Group',
211
+ 'GroupErrMsg02',
212
+ 'Group Type is required',
213
+ );
214
+ }
215
+
216
+ //Check if group code is unique
217
+ const existingGroupCode = await Group._Repo.findByPk(group.GroupCode, {
218
+ transaction: dbTransaction,
219
+ });
220
+
221
+ if (existingGroupCode) {
222
+ throw new ClassError(
223
+ 'Group',
224
+ 'GroupErrMsg03',
225
+ 'Duplicate GroupCode found.',
226
+ );
227
+ }
228
+
229
+ //Validate parent group code if passed. Call Group._Repo.findByPk
230
+ if (group.ParentGroupCode) {
231
+ const parentGroup = await Group._Repo.findByPk(group.ParentGroupCode, {
232
+ transaction: dbTransaction,
233
+ });
234
+
235
+ if (!parentGroup) {
236
+ throw new ClassError(
237
+ 'Group',
238
+ 'GroupErrMsg04',
239
+ 'ParentGroupCode is not found.',
240
+ );
241
+ }
242
+
243
+ //If Params.group.GroupCode = Params.group?.ParentGroupCode, throw new ClassError
244
+ if (group.GroupCode === group.ParentGroupCode) {
245
+ throw new ClassError(
246
+ 'Group',
247
+ 'GroupErrMsg05',
248
+ 'GroupCode and ParentGroupCode cannot be the same.',
249
+ );
250
+ }
251
+ }
252
+
253
+ //Part 3: Create Group
254
+ //Initialise new Group instance and populate
255
+ const newGroup = new Group(group);
256
+ newGroup.ObjectId = group.GroupCode;
257
+ newGroup.Name = group.Name;
258
+ newGroup.Type = group.Type;
259
+ newGroup.Description = group.Description;
260
+ newGroup.ParentGroupCode = group.ParentGroupCode;
261
+ newGroup.InheritParentPrivilegeYN = group.InheritParentPrivilegeYN;
262
+ newGroup.InheritParentSystemAccessYN = group.InheritParentSystemAccessYN;
263
+ newGroup.Status = 'Active';
264
+ newGroup._CreatedById = loginUser.UserId;
265
+ newGroup._UpdatedById = loginUser.UserId;
266
+
267
+ //Call Group._Repo create method
268
+ const entityGroupAfter = {
269
+ GroupCode: newGroup.ObjectId,
270
+ Name: newGroup.Name,
271
+ Type: newGroup.Type,
272
+ Description: newGroup.Description,
273
+ ParentGroupCode: newGroup.ParentGroupCode,
274
+ InheritParentPrivilegeYN: newGroup.InheritParentPrivilegeYN,
275
+ InheritParentSystemAccessYN: newGroup.InheritParentSystemAccessYN,
276
+ Status: newGroup.Status,
277
+ CreatedById: newGroup._CreatedById,
278
+ UpdatedById: newGroup._UpdatedById,
279
+ CreatedAt: newGroup._CreatedAt,
280
+ UpdatedAt: newGroup._UpdatedAt,
281
+ };
282
+
283
+ await Group._Repo.create(entityGroupAfter, {
284
+ transaction: dbTransaction,
285
+ });
286
+
287
+ //Part 4: Record Create Group Activity and return newGroup
288
+
289
+ const entityValueBefore = {};
290
+
291
+ //Instantiate new activity
292
+ const activity = new Activity();
293
+ activity.ActivityId = activity.createId();
294
+ activity.Action = ActionEnum.ADD;
295
+ activity.Description = 'Create Group';
296
+ activity.EntityType = 'Group';
297
+ activity.EntityId = newGroup.ObjectId;
298
+ activity.EntityValueBefore = JSON.stringify(entityValueBefore);
299
+ activity.EntityValueAfter = JSON.stringify(entityGroupAfter);
300
+
301
+ //Call Activity.create method
302
+ await activity.create(loginUser.ObjectId, dbTransaction);
303
+
304
+ return newGroup;
305
+ } catch (error) {
306
+ throw error;
307
+ }
308
+ }
309
+
310
+ protected static async checkDuplicateGroupCode(
311
+ dbTransaction: any,
312
+ GroupCode,
313
+ ) {
314
+ const isGroupCodeExist = await Group._Repo.findOne({
315
+ where: { GroupCode },
316
+ transaction: dbTransaction,
317
+ });
318
+
319
+ if (isGroupCodeExist) {
320
+ throw new ClassError(
321
+ 'Group',
322
+ 'GroupErrMsg07',
323
+ 'GroupCode already exists.',
324
+ );
325
+ }
326
+ }
327
+
328
+ public async update(
329
+ loginUser: LoginUser,
330
+ dbTransaction: any,
331
+ group: {
332
+ GroupCode: string;
333
+ NewGroupCode?: string;
334
+ Name?: string;
335
+ Description?: string;
336
+ Type?: GroupTypeEnum;
337
+ ParentGroupCode?: string;
338
+ InheritParentPrivilegeYN?: string;
339
+ InheritParentSystemAccessYN?: string;
340
+ Status?: string;
341
+ },
342
+ ) {
343
+ //Part 1: Privilege Checking
344
+ const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
345
+ const isPrivileged = await loginUser.checkPrivileges(
346
+ systemCode,
347
+ 'GROUP_UPDATE',
348
+ );
349
+
350
+ if (!isPrivileged) {
351
+ throw new ClassError(
352
+ 'Group',
353
+ 'GroupErrMsg06',
354
+ 'You do not have the privilege to update Group',
355
+ );
356
+ }
357
+ try {
358
+ const currentGroup = await Group.init(dbTransaction, group.GroupCode);
359
+ if (group.NewGroupCode) {
360
+ await Group.checkDuplicateGroupCode(dbTransaction, group.NewGroupCode);
361
+ }
362
+
363
+ if (
364
+ group.ParentGroupCode &&
365
+ currentGroup.ParentGroupCode !== group.ParentGroupCode
366
+ ) {
367
+ const parentGroup = await Group.init(
368
+ dbTransaction,
369
+ group.ParentGroupCode,
370
+ );
371
+ if (!parentGroup) {
372
+ throw new ClassError(
373
+ 'Group',
374
+ 'GroupErrMsg08',
375
+ 'Parent Group Code not found',
376
+ );
377
+ }
378
+ }
379
+
380
+ const entityValueBefore = {
381
+ GroupCode: currentGroup.GroupCode,
382
+ Name: currentGroup.Name,
383
+ Type: currentGroup.Type,
384
+ Description: currentGroup.Description,
385
+ ParentGroupCode: currentGroup.ParentGroupCode,
386
+ InheritParentPrivilegeYN: currentGroup.InheritParentPrivilegeYN,
387
+ InheritParentSystemAccessYN: currentGroup.InheritParentSystemAccessYN,
388
+ Status: currentGroup.Status,
389
+ CreatedById: currentGroup._CreatedById,
390
+ UpdatedById: currentGroup._UpdatedById,
391
+ CreatedAt: currentGroup._CreatedAt,
392
+ UpdatedAt: currentGroup._UpdatedAt,
393
+ };
394
+
395
+ currentGroup.GroupCode = group?.NewGroupCode || currentGroup.GroupCode;
396
+ currentGroup.Name = group?.Name || currentGroup.Name;
397
+ currentGroup.Type = group?.Type || currentGroup.Type;
398
+ currentGroup.Description = group?.Description || currentGroup.Description;
399
+ currentGroup.ParentGroupCode =
400
+ group?.ParentGroupCode || currentGroup.ParentGroupCode;
401
+ currentGroup.InheritParentPrivilegeYN =
402
+ group?.InheritParentPrivilegeYN ||
403
+ currentGroup.InheritParentPrivilegeYN;
404
+ currentGroup.InheritParentSystemAccessYN =
405
+ group?.InheritParentSystemAccessYN ||
406
+ currentGroup.InheritParentSystemAccessYN;
407
+ currentGroup.Status = group?.Status || currentGroup.Status;
408
+ currentGroup._UpdatedById = loginUser.UserId;
409
+ currentGroup._UpdatedAt = new Date();
410
+
411
+ await Group._Repo.update(
412
+ {
413
+ GroupCode: currentGroup.GroupCode,
414
+ Name: currentGroup.Name,
415
+ Type: currentGroup.Type,
416
+ Description: currentGroup.Description,
417
+ ParentGroupCode: currentGroup.ParentGroupCode,
418
+ InheritParentPrivilegeYN: currentGroup.InheritParentPrivilegeYN,
419
+ InheritParentSystemAccessYN: currentGroup.InheritParentSystemAccessYN,
420
+ Status: currentGroup.Status,
421
+ UpdatedById: currentGroup._UpdatedById,
422
+ UpdatedAt: currentGroup._UpdatedAt,
423
+ },
424
+ {
425
+ where: {
426
+ GroupCode: group.GroupCode,
427
+ },
428
+ transaction: dbTransaction,
429
+ },
430
+ );
431
+
432
+ const entityValueAfter = {
433
+ GroupCode: currentGroup.GroupCode,
434
+ Name: currentGroup.Name,
435
+ Type: currentGroup.Type,
436
+ Description: currentGroup.Description,
437
+ ParentGroupCode: currentGroup.ParentGroupCode,
438
+ InheritParentPrivilegeYN: currentGroup.InheritParentPrivilegeYN,
439
+ InheritParentSystemAccessYN: currentGroup.InheritParentSystemAccessYN,
440
+ Status: currentGroup.Status,
441
+ CreatedById: currentGroup._CreatedById,
442
+ UpdatedById: currentGroup._UpdatedById,
443
+ CreatedAt: currentGroup._CreatedAt,
444
+ UpdatedAt: currentGroup._UpdatedAt,
445
+ };
446
+
447
+ const activity = new Activity();
448
+ activity.ActivityId = activity.createId();
449
+ activity.Action = ActionEnum.UPDATE;
450
+ activity.Description = `Update Group ${group.Type}`;
451
+ activity.EntityType = 'Group';
452
+ activity.EntityId = group.GroupCode;
453
+ activity.EntityValueBefore = JSON.stringify(entityValueBefore);
454
+ activity.EntityValueAfter = JSON.stringify(entityValueAfter);
455
+ await activity.create(loginUser.ObjectId, dbTransaction);
456
+
457
+ return currentGroup;
458
+ } catch (error) {
459
+ throw error;
460
+ }
461
+ }
462
+
463
+ public static async getSystemAccesses(
464
+ loginUser: LoginUser,
465
+ dbTransaction: any,
466
+ GroupCode: string,
467
+ Page: number,
468
+ Rows: number,
469
+ Search: {
470
+ SystemCode?: string;
471
+ Status?: string;
472
+ },
473
+ ) {
474
+ // Part 1: Privilege Checking
475
+ const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
476
+ const isPrivileged = await loginUser.checkPrivileges(
477
+ systemCode,
478
+ 'SYSTEM_ACCESS_VIEW',
479
+ );
480
+
481
+ if (!isPrivileged) {
482
+ throw new ClassError(
483
+ 'Group',
484
+ 'GroupErrMsg06',
485
+ 'You do not have the privilege to view system access',
486
+ );
487
+ }
488
+
489
+ try {
490
+ // Part 2: Validation
491
+ await Group.init(dbTransaction, GroupCode);
492
+
493
+ // Part 3: Retrieve System Access and returns
494
+ const queryObj: any = { GroupCode: GroupCode };
495
+
496
+ if (Search) {
497
+ Object.entries(Search).forEach(([key, value]) => {
498
+ queryObj[key] = value;
499
+ });
500
+ }
501
+
502
+ let options: any = {
503
+ where: queryObj,
504
+ distinct: true,
505
+ transaction: dbTransaction,
506
+ };
507
+
508
+ if (Page && Rows) {
509
+ options = {
510
+ ...options,
511
+ limit: Rows,
512
+ offset: Rows * (Page - 1),
513
+ order: [['CreatedAt', 'DESC']],
514
+ };
515
+ }
516
+
517
+ const systemAccess = await Group._GroupSystemAccessRepo.findAndCountAll(
518
+ options,
519
+ );
520
+ return systemAccess;
521
+ } catch (error) {
522
+ return error;
523
+ }
524
+ }
525
+
526
+ private static async getInheritedSystemAccess(
527
+ dbTransaction: any,
528
+ group: Group,
529
+ ): Promise<any[]> {
530
+ const options: any = {
531
+ where: {
532
+ GroupCode: group.GroupCode,
533
+ Status: 'Active',
534
+ },
535
+ include: [
536
+ {
537
+ model: SystemModel,
538
+ },
539
+ ],
540
+ transaction: dbTransaction,
541
+ };
542
+ let systemAccess = await Group._GroupSystemAccessRepo.findAll(options);
543
+
544
+ if (group.InheritParentSystemAccessYN === 'Y' && group.ParentGroupCode) {
545
+ const parentGroup = await Group.init(
546
+ dbTransaction,
547
+ group.ParentGroupCode,
548
+ );
549
+ const parentSystemAccesses = await this.getInheritedSystemAccess(
550
+ dbTransaction,
551
+ parentGroup,
552
+ );
553
+ systemAccess = systemAccess.concat(parentSystemAccesses);
554
+ }
555
+ return systemAccess;
556
+ }
557
+
558
+ public static async checkParentHierarchy(
559
+ dbTransaction: any,
560
+ GroupCode: string,
561
+ ListGroupCode: string[] = [],
562
+ ): Promise<boolean> {
563
+ ListGroupCode.push(GroupCode);
564
+
565
+ const group = await Group._Repo.findOne({
566
+ where: { GroupCode },
567
+ transaction: dbTransaction,
568
+ });
569
+
570
+ if (group?.ParentGroupCode) {
571
+ const isGroupCodeExist = ListGroupCode.includes(group.ParentGroupCode);
572
+ if (!isGroupCodeExist) {
573
+ await this.checkParentHierarchy(
574
+ dbTransaction,
575
+ group.ParentGroupCode,
576
+ ListGroupCode,
577
+ );
578
+ }
579
+ {
580
+ return false;
581
+ }
582
+ } else {
583
+ return true;
584
+ }
585
+ }
586
+
587
+ public static async getParentSystemAccesses(
588
+ loginUser: LoginUser,
589
+ dbTransaction: any,
590
+ GroupCode: string,
591
+ ) {
592
+ // Part 1: Privilege Checking
593
+ const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
594
+ const isPrivileged = await loginUser.checkPrivileges(
595
+ systemCode,
596
+ 'SYSTEM_ACCESS_VIEW',
597
+ );
598
+
599
+ if (!isPrivileged) {
600
+ throw new ClassError(
601
+ 'Group',
602
+ 'GroupErrMsg06',
603
+ 'You do not have the privilege to view system access',
604
+ );
605
+ }
606
+
607
+ try {
608
+ const group = await Group.init(dbTransaction, GroupCode);
609
+ if (group.InheritParentSystemAccessYN !== 'Y' || !group.ParentGroupCode) {
610
+ return [];
611
+ } else {
612
+ const parentGroup = await Group.init(
613
+ dbTransaction,
614
+ group.ParentGroupCode,
615
+ );
616
+ const inheritSystemAccess = await Group.getInheritedSystemAccess(
617
+ dbTransaction,
618
+ parentGroup,
619
+ );
620
+ return inheritSystemAccess;
621
+ }
622
+ } catch (error) {
623
+ throw error;
624
+ }
625
+ }
626
+
627
+ public static async addSystemAccesses(
628
+ loginUser: LoginUser,
629
+ dbTransaction: any,
630
+ GroupCode: string,
631
+ SystemCodes: string[],
632
+ ) {
633
+ // Part 1: Privilege Checking
634
+ const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
635
+ const isPrivileged = await loginUser.checkPrivileges(
636
+ systemCode,
637
+ 'SYSTEM_ACCESS_CREATE',
638
+ );
639
+
640
+ if (!isPrivileged) {
641
+ throw new ClassError(
642
+ 'Group',
643
+ 'GroupErrMsg07',
644
+ 'You do not have the privilege to create system access',
645
+ );
646
+ }
647
+
648
+ try {
649
+ if (SystemCodes.length > 0) {
650
+ for (const element of SystemCodes) {
651
+ const CurrentGroupSystemAccess = await Group.getSystemAccesses(
652
+ loginUser,
653
+ dbTransaction,
654
+ GroupCode,
655
+ 1,
656
+ Number.MAX_SAFE_INTEGER,
657
+ { SystemCode: element },
658
+ );
659
+
660
+ if (CurrentGroupSystemAccess?.count > 0) {
661
+ throw new ClassError(
662
+ 'Group',
663
+ 'GroupErrMsg08',
664
+ 'System access already exists',
665
+ );
666
+ }
667
+
668
+ const groupSystemAccess = await GroupSystemAccess.init(dbTransaction);
669
+ groupSystemAccess.createId();
670
+ groupSystemAccess.GroupCode = GroupCode;
671
+ groupSystemAccess.SystemCode = element;
672
+ groupSystemAccess.Status = 'Active';
673
+ groupSystemAccess.CreatedById = +loginUser.ObjectId;
674
+ groupSystemAccess.CreatedAt = new Date();
675
+ groupSystemAccess.UpdatedById = +loginUser.ObjectId;
676
+ groupSystemAccess.UpdatedAt = new Date();
677
+
678
+ const EntityValueAfter = {
679
+ GroupCode: groupSystemAccess.GroupCode,
680
+ SystemCode: groupSystemAccess.SystemCode,
681
+ Status: groupSystemAccess.Status,
682
+ CreatedById: groupSystemAccess.CreatedById,
683
+ CreatedAt: groupSystemAccess.CreatedAt,
684
+ UpdatedById: groupSystemAccess.UpdatedById,
685
+ UpdatedAt: groupSystemAccess.UpdatedAt,
686
+ };
687
+
688
+ const systemAccess = await Group._GroupSystemAccessRepo.create(
689
+ EntityValueAfter,
690
+ {
691
+ transaction: dbTransaction,
692
+ },
693
+ );
694
+
695
+ const activity = new Activity();
696
+ activity.ActivityId = activity.createId();
697
+ activity.Action = ActionEnum.ADD;
698
+ activity.Description = 'Create Group System Access';
699
+ activity.EntityType = 'GroupSystemAccess';
700
+ activity.EntityId = systemAccess.GroupSystemAccessId?.toString();
701
+ activity.EntityValueBefore = JSON.stringify({});
702
+ activity.EntityValueAfter = JSON.stringify(EntityValueAfter);
703
+
704
+ await activity.create(loginUser.ObjectId, dbTransaction);
705
+ }
706
+
707
+ return { Message: 'Successfully added.' };
708
+ }
709
+ } catch (error) {
710
+ throw error;
711
+ }
712
+ }
713
+
714
+ public static async deleteSystemAccess(
715
+ loginUser: LoginUser,
716
+ dbTransaction: any,
717
+ GroupCode: string,
718
+ SystemCode: string,
719
+ ) {
720
+ // Part 1: Privilege Checking
721
+ const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
722
+ const isPrivileged = await loginUser.checkPrivileges(
723
+ systemCode,
724
+ 'SYSTEM_ACCESS_DELETE',
725
+ );
726
+
727
+ if (!isPrivileged) {
728
+ throw new ClassError(
729
+ 'Group',
730
+ 'GroupErrMsg08',
731
+ 'You do not have the privilege to delete system access',
732
+ );
733
+ }
734
+
735
+ try {
736
+ const currentGroupSystemAccess = await Group.getSystemAccesses(
737
+ loginUser,
738
+ dbTransaction,
739
+ GroupCode,
740
+ 1,
741
+ Number.MAX_SAFE_INTEGER,
742
+ { SystemCode: SystemCode },
743
+ );
744
+
745
+ if (currentGroupSystemAccess.count < 1) {
746
+ throw new ClassError(
747
+ 'Group',
748
+ 'GroupErrMsg10',
749
+ 'No associated system access found.',
750
+ );
751
+ }
752
+
753
+ await Group._GroupSystemAccessRepo.delete(
754
+ GroupCode,
755
+ SystemCode,
756
+ dbTransaction,
757
+ );
758
+
759
+ const EntityValueBefore = {
760
+ GroupCode: currentGroupSystemAccess?.rows[0]?.GroupCode,
761
+ SystemCode: currentGroupSystemAccess?.rows[0]?.SystemCode,
762
+ Status: currentGroupSystemAccess?.rows[0]?.Status,
763
+ CreatedById: currentGroupSystemAccess?.rows[0]?.CreatedById,
764
+ CreatedAt: currentGroupSystemAccess?.rows[0]?.CreatedAt,
765
+ UpdatedById: currentGroupSystemAccess?.rows[0]?.UpdatedById,
766
+ UpdatedAt: currentGroupSystemAccess?.rows[0]?.UpdatedAt,
767
+ };
768
+
769
+ const activity = new Activity();
770
+ activity.ActivityId = activity.createId();
771
+ activity.Action = ActionEnum.DELETE;
772
+ activity.Description = 'Delete Group System Access';
773
+ activity.EntityType = 'GroupSystemAccess';
774
+ activity.EntityId =
775
+ currentGroupSystemAccess?.rows[0]?.GroupSystemAccessId?.toString();
776
+ activity.EntityValueBefore = JSON.stringify(EntityValueBefore);
777
+ activity.EntityValueAfter = JSON.stringify({});
778
+
779
+ await activity.create(loginUser.ObjectId, dbTransaction);
780
+
781
+ return { Message: 'System access removed.', SystemCode: SystemCode };
782
+ } catch (error) {
783
+ throw error;
784
+ }
785
+ }
786
+
787
+ public static async getSystemPrivileges(
788
+ loginUser: LoginUser,
789
+ dbTransaction: any,
790
+ GroupCode: string,
791
+ search?: {
792
+ SystemCode?: string;
793
+ Status?: string;
794
+ },
795
+ ) {
796
+ try {
797
+ //Part 1: Privilege Checking
798
+ const systemCode =
799
+ ApplicationConfig.getComponentConfigValue('system-code');
800
+ const isPrivileged = await loginUser.checkPrivileges(
801
+ systemCode,
802
+ 'GROUP_PRIVILEGE_VIEW',
803
+ );
804
+
805
+ if (!isPrivileged) {
806
+ throw new ClassError(
807
+ 'Group',
808
+ 'GroupErrMsg11',
809
+ 'You do not have the privilege to view group privileges',
810
+ );
811
+ }
812
+
813
+ //Set group to instantiation of existing Group
814
+ await Group.init(dbTransaction, GroupCode);
815
+
816
+ //Part 3: Retrieve Group Own Privilege
817
+ //Retrieve group data and it's privileged by calling Group._Repo.findAll
818
+ let where: any = {
819
+ GroupCode,
820
+ };
821
+
822
+ let systemWhere: any = {};
823
+
824
+ if (search) {
825
+ if (search.Status) {
826
+ where = {
827
+ ...where,
828
+ Status: search.Status,
829
+ };
830
+ }
831
+
832
+ if (search.SystemCode) {
833
+ systemWhere = {
834
+ SystemCode: {
835
+ [Op.substring]: search.SystemCode,
836
+ },
837
+ };
838
+ }
839
+ }
840
+
841
+ const groupOwnPrivileges = await Group._GroupPrivilegeRepo.findAll({
842
+ where,
843
+ include: [
844
+ {
845
+ model: SystemPrivilegeModel,
846
+ where: systemWhere,
847
+ },
848
+ ],
849
+ transaction: dbTransaction,
850
+ });
851
+
852
+ //Create variable called privileges and Map the SystemPrivilege data retrieved from 3.1 into SystemPrivilege object and push it to the privileges variables
853
+ const privileges: SystemPrivilege[] = [];
854
+
855
+ for (const groupPrivilege of groupOwnPrivileges) {
856
+ const systemPrivilege = await SystemPrivilege.init(dbTransaction);
857
+ systemPrivilege.setAttributes(
858
+ groupPrivilege.Privilege.get({ plain: true }),
859
+ );
860
+ privileges.push(systemPrivilege);
861
+ }
862
+
863
+ return privileges;
864
+ } catch (error) {
865
+ throw error;
866
+ }
867
+ }
868
+
869
+ public static async getInheritedSystemPrivileges(
870
+ dbTransaction: any,
871
+ GroupCode: string,
872
+ search?: {
873
+ SystemCode?: string;
874
+ Status?: string;
875
+ PrivilegeCode?: string;
876
+ },
877
+ ): Promise<SystemPrivilege[]> {
878
+ try {
879
+ //Retrieve group data and it's privileges by calling Group._Repo.findAll
880
+ const where: any = {
881
+ GroupCode,
882
+ };
883
+
884
+ let groupPrivilegeWhere: any = {};
885
+ let systemPrivilegeWhere: any = {};
886
+
887
+ if (search) {
888
+ if (search.Status) {
889
+ groupPrivilegeWhere = {
890
+ Status: search.Status,
891
+ };
892
+ }
893
+
894
+ if (search.SystemCode) {
895
+ systemPrivilegeWhere = {
896
+ SystemCode: {
897
+ [Op.substring]: search.SystemCode,
898
+ },
899
+ };
900
+ }
901
+ }
902
+ const group = await Group._Repo.findOne({
903
+ where: where,
904
+ include: [
905
+ {
906
+ model: GroupPrivilegeModel,
907
+ where: groupPrivilegeWhere,
908
+ separate: true,
909
+ include: [
910
+ {
911
+ model: SystemPrivilegeModel,
912
+ where: systemPrivilegeWhere,
913
+ },
914
+ ],
915
+ },
916
+ ],
917
+ transaction: dbTransaction,
918
+ });
919
+
920
+ //Retrieve group object privileges by calling LoginUser._GroupObjectPrivilegeRepo.findAll
921
+ const objectWhere: any = {
922
+ GroupCode,
923
+ };
924
+ const systemWhere: any = {};
925
+ if (search) {
926
+ Object.entries(search).forEach(([key, value]) => {
927
+ if (key === 'SystemCode') {
928
+ systemWhere[key] = {
929
+ [Op.substring]: value,
930
+ };
931
+ } else {
932
+ objectWhere[key] = {
933
+ [Op.substring]: value,
934
+ };
935
+ }
936
+ });
937
+ }
938
+ const groupObjectPrivileges =
939
+ await Group._GroupObjectPrivilegeRepo.findAll({
940
+ where: objectWhere,
941
+ include: [
942
+ {
943
+ model: SystemPrivilegeModel,
944
+ where: systemWhere,
945
+ },
946
+ ],
947
+ transaction: dbTransaction,
948
+ });
949
+
950
+ //Map to SystemPrivilege object
951
+ let privileges: SystemPrivilege[] = [];
952
+ for (const groupPrivilege of group.GroupPrivileges) {
953
+ const systemPrivilege = await SystemPrivilege.init(dbTransaction);
954
+ systemPrivilege.setAttributes(
955
+ groupPrivilege.Privilege.get({ plain: true }),
956
+ );
957
+ privileges.push(systemPrivilege);
958
+ }
959
+
960
+ for (const groupObjectPrivilege of groupObjectPrivileges) {
961
+ const systemPrivilege = await SystemPrivilege.init(dbTransaction);
962
+ systemPrivilege.setAttributes(
963
+ groupObjectPrivilege.Privilege.get({ plain: true }),
964
+ );
965
+ privileges.push(systemPrivilege);
966
+ }
967
+
968
+ //Part 2: Retrieve Privileges Inherited from Parent Group
969
+ //if group data retrieved from 1.1 have InheritParentPrivilegeYN == "Y" and ParentGroupCode value is not empty. Call this method again
970
+ if (group.InheritParentPrivilegeYN === 'Y' && group.ParentGroupCode) {
971
+ const inheritedPrivileges = await Group.getInheritedSystemPrivileges(
972
+ dbTransaction,
973
+ group.ParentGroupCode,
974
+ search,
975
+ );
976
+ privileges = privileges.concat(inheritedPrivileges);
977
+ }
978
+
979
+ //format to make sure no duplicate
980
+ const uniquePrivileges = Array.from(
981
+ new Set(privileges.map((a) => a.PrivilegeCode)),
982
+ ).map((PrivilegeCode) => {
983
+ return privileges.find((a) => a.PrivilegeCode === PrivilegeCode);
984
+ });
985
+
986
+ return uniquePrivileges;
987
+ } catch (error) {
988
+ throw error;
989
+ }
990
+ }
991
+
992
+ public static async getParentSystemPrivileges(
993
+ loginUser: LoginUser,
994
+ dbTransaction: any,
995
+ GroupCode: string,
996
+ search?: {
997
+ SystemCode?: string;
998
+ Status?: string;
999
+ PrivilegeCode?: string;
1000
+ },
1001
+ ): Promise<SystemPrivilege[]> {
1002
+ try {
1003
+ //Part 1: Privilege Checking
1004
+ const systemCode =
1005
+ ApplicationConfig.getComponentConfigValue('system-code');
1006
+ const isPrivileged = await loginUser.checkPrivileges(
1007
+ systemCode,
1008
+ 'GROUP_PRIVILEGE_VIEW',
1009
+ );
1010
+
1011
+ if (!isPrivileged) {
1012
+ throw new ClassError(
1013
+ 'Group',
1014
+ 'GroupErrMsg11',
1015
+ 'You do not have the privilege to view group privileges',
1016
+ );
1017
+ }
1018
+
1019
+ //Part 2: Validation
1020
+ //Set group to instantiation of existing Group
1021
+ const group = await Group.init(dbTransaction, GroupCode);
1022
+ //Check if group.InheritParentPrivilegeYN == "Y" and ParentGroupCode value is not empty. if no then return an empty array
1023
+ if (group.InheritParentPrivilegeYN !== 'Y' || !group.ParentGroupCode) {
1024
+ return [];
1025
+ }
1026
+
1027
+ //Part 3: Retrieve Group Own Privilege
1028
+ //Retrieve group data and it's privileged by calling Group.getIheritedSystemPrivileges
1029
+ const privileges = await Group.getInheritedSystemPrivileges(
1030
+ dbTransaction,
1031
+ group.ParentGroupCode,
1032
+ search,
1033
+ );
1034
+
1035
+ return privileges;
1036
+ } catch (error) {
1037
+ throw error;
1038
+ }
1039
+ }
1040
+
1041
+ public static async assignGroupObjectPrivilege(
1042
+ loginUser: LoginUser,
1043
+ dbTransaction: any,
1044
+ GroupCode: string,
1045
+ GroupObjectPrivileges: GroupObjectPrivilege[],
1046
+ ): Promise<string> {
1047
+ try {
1048
+ //Part 1: Privilege Checking
1049
+ const systemCode =
1050
+ ApplicationConfig.getComponentConfigValue('system-code');
1051
+ const isPrivileged = await loginUser.checkPrivileges(
1052
+ systemCode,
1053
+ 'GROUP_OBJECT_PRIVILEGE_ASSIGN',
1054
+ );
1055
+
1056
+ if (!isPrivileged) {
1057
+ throw new ClassError(
1058
+ 'Group',
1059
+ 'GroupErrMsg12',
1060
+ 'You do not have the privilege to assign group object privilege',
1061
+ );
1062
+ }
1063
+
1064
+ //Part 2: Validation
1065
+ //Initialise group with group init
1066
+ const group = await Group.init(dbTransaction, GroupCode);
1067
+ //Retrieve all group system access by calling Group.getSystemAccesses
1068
+ const groupSystemAccesses = await Group.getSystemAccesses(
1069
+ loginUser,
1070
+ dbTransaction,
1071
+ GroupCode,
1072
+ 1,
1073
+ Number.MAX_SAFE_INTEGER,
1074
+ {},
1075
+ );
1076
+
1077
+ //If Group.InheritParentSystemAccess == "Y" and Group.ParentGroupCode exist, initialise parent group
1078
+ let parentGroupSystemAccesses: any = {};
1079
+ if (group.InheritParentSystemAccessYN === 'Y' && group.ParentGroupCode) {
1080
+ //Retrieve all parent group system access by calling Group.getSystemAccesses
1081
+ parentGroupSystemAccesses = await Group.getSystemAccesses(
1082
+ loginUser,
1083
+ dbTransaction,
1084
+ group.ParentGroupCode,
1085
+ 1,
1086
+ Number.MAX_SAFE_INTEGER,
1087
+ {},
1088
+ );
1089
+ }
1090
+
1091
+ // For each Params.GroupObjectPrivileges.
1092
+ for (const groupObjectPrivilege of GroupObjectPrivileges) {
1093
+ //Initialise existing System privilege
1094
+ const systemPrivilege = await SystemPrivilege.init(
1095
+ dbTransaction,
1096
+ groupObjectPrivilege.PrivilegeCode,
1097
+ );
1098
+ //Check whether the system codes used by that privilege is exist inside the group system access
1099
+ const combinedSystemAccesses = {
1100
+ ...groupSystemAccesses.rows,
1101
+ ...parentGroupSystemAccesses.rows,
1102
+ };
1103
+ const systemAccess = combinedSystemAccesses.find(
1104
+ (systemAccess) =>
1105
+ systemAccess.SystemCode === systemPrivilege.SystemCode,
1106
+ );
1107
+ if (!systemAccess) {
1108
+ throw new ClassError(
1109
+ 'Group',
1110
+ 'GroupErrMsg13',
1111
+ 'Failed to assign privilege ' +
1112
+ groupObjectPrivilege.PrivilegeCode +
1113
+ ' due to non-existent system access.',
1114
+ );
1115
+ }
1116
+
1117
+ //Check whether the group object privilege already exist by using Group._GroupObjectPrivilegesRepo.findOne
1118
+ const groupObjectPrivilegeData =
1119
+ await Group._GroupObjectPrivilegeRepo.findOne({
1120
+ where: {
1121
+ GroupCode,
1122
+ PrivilegeCode: groupObjectPrivilege.PrivilegeCode,
1123
+ ObjectId: groupObjectPrivilege.ObjectId,
1124
+ ObjectType: groupObjectPrivilege.ObjectType,
1125
+ },
1126
+ transaction: dbTransaction,
1127
+ });
1128
+ //If GroupObjectPrivilege record exist. Skip this loop and proceed to the next privilege code
1129
+ if (groupObjectPrivilegeData) {
1130
+ continue;
1131
+ } else {
1132
+ //Call GroupObjectPrivilege.create
1133
+ await GroupObjectPrivilege.create(
1134
+ loginUser,
1135
+ dbTransaction,
1136
+ groupObjectPrivilege,
1137
+ );
1138
+ }
1139
+ }
1140
+
1141
+ return 'Successfully added.';
1142
+ } catch (error) {
1143
+ throw error;
1144
+ }
1145
+ }
1146
+
1147
+ public static async getGroubObjectPrivileges(
1148
+ loginUser: LoginUser,
1149
+ dbTransaction: any,
1150
+ GroupCode: string,
1151
+ search?: {
1152
+ PrivilegeCode?: string;
1153
+ ObjectType?: string;
1154
+ ObjectId?: string;
1155
+ SystemCode?: string;
1156
+ },
1157
+ ): Promise<SystemPrivilege[]> {
1158
+ try {
1159
+ // Part 1: Privilege Checking
1160
+ const systemCode =
1161
+ ApplicationConfig.getComponentConfigValue('system-code');
1162
+ const isPrivileged = await loginUser.checkPrivileges(
1163
+ systemCode,
1164
+ 'GROUP_PRIVILEGE_VIEW',
1165
+ );
1166
+
1167
+ if (!isPrivileged) {
1168
+ throw new ClassError(
1169
+ 'Group',
1170
+ 'GroupErrMsg11',
1171
+ 'You do not have the privilege to view group privileges',
1172
+ );
1173
+ }
1174
+
1175
+ // Part 2: Validation
1176
+ // Set group to instantiation of existing Group
1177
+ await Group.init(dbTransaction, GroupCode);
1178
+
1179
+ // Part 3: Retrieve Group Own Privilege
1180
+ // Retrieve group object privileges by calling LoginUser._GroupObjectPrivilegeRepo.findAll
1181
+ const where: any = {
1182
+ GroupCode,
1183
+ };
1184
+
1185
+ const systemWhere: any = {};
1186
+
1187
+ if (search) {
1188
+ Object.entries(search).forEach(([key, value]) => {
1189
+ if (key === 'SystemCode') {
1190
+ systemWhere[key] = {
1191
+ [Op.substring]: value,
1192
+ };
1193
+ } else {
1194
+ where[key] = {
1195
+ [Op.substring]: value,
1196
+ };
1197
+ }
1198
+ });
1199
+ }
1200
+
1201
+ const groupObjectPrivileges =
1202
+ await Group._GroupObjectPrivilegeRepo.findAll({
1203
+ where,
1204
+ include: [
1205
+ {
1206
+ model: SystemPrivilegeModel,
1207
+ where: systemWhere,
1208
+ },
1209
+ ],
1210
+ transaction: dbTransaction,
1211
+ });
1212
+ // Create variable called privileges and Map the SystemPrivilege data retrieved from 3.1 into SystemPrivilege object and push it to the privileges variables
1213
+ const privileges: SystemPrivilege[] = [];
1214
+ for (const groupObjectPrivilege of groupObjectPrivileges) {
1215
+ const systemPrivilege = await SystemPrivilege.init(dbTransaction);
1216
+ systemPrivilege.setAttributes(
1217
+ groupObjectPrivilege.Privilege.get({ plain: true }),
1218
+ );
1219
+ privileges.push(systemPrivilege);
1220
+ }
1221
+
1222
+ //Remove duplicate
1223
+ const uniquePrivileges = Array.from(
1224
+ new Set(privileges.map((a) => a.PrivilegeCode)),
1225
+ ).map((PrivilegeCode) => {
1226
+ return privileges.find((a) => a.PrivilegeCode === PrivilegeCode);
1227
+ });
1228
+
1229
+ // Create the result based on the spec on return then returns it.
1230
+ return uniquePrivileges;
1231
+ } catch (error) {
1232
+ throw error;
1233
+ }
1234
+ }
1235
+
1236
+ public static async assignGroupPrivileges(
1237
+ loginUser: LoginUser,
1238
+ dbTransaction: any,
1239
+ GroupCode: string,
1240
+ PrivilegeCodes: string[],
1241
+ ) {
1242
+ try {
1243
+ // Part 1: Privilege Checking
1244
+ const systemCode =
1245
+ ApplicationConfig.getComponentConfigValue('system-code');
1246
+ const isPrivileged = await loginUser.checkPrivileges(
1247
+ systemCode,
1248
+ 'GROUP_PRIVILEGE_ASSIGN',
1249
+ );
1250
+
1251
+ if (!isPrivileged) {
1252
+ throw new ClassError(
1253
+ 'Group',
1254
+ 'GroupErrMsg06',
1255
+ 'You do not have the privilege to assign group privileges',
1256
+ );
1257
+ }
1258
+
1259
+ // Part 2: Validation, Create and Record Activity
1260
+ // Initialise group with group init
1261
+
1262
+ const group = await Group.init(dbTransaction, GroupCode);
1263
+
1264
+ // Retrieve all group system access by calling Group.getSystemAccess
1265
+ const groupSystemAccesses = await Group.getSystemAccesses(
1266
+ loginUser,
1267
+ dbTransaction,
1268
+ GroupCode,
1269
+ 1,
1270
+ Number.MAX_SAFE_INTEGER,
1271
+ {},
1272
+ );
1273
+
1274
+ // If Group.InheritParentSystemAccess == "Y" and Group.ParentGroupCode exist
1275
+ let parentGroupSystemAccesses: any = {};
1276
+ if (group.InheritParentSystemAccessYN === 'Y' && group.ParentGroupCode) {
1277
+ // Retrieve all parent group system access by calling Group.getSystemAccess
1278
+ parentGroupSystemAccesses = await Group.getSystemAccesses(
1279
+ loginUser,
1280
+ dbTransaction,
1281
+ group.ParentGroupCode,
1282
+ 1,
1283
+ Number.MAX_SAFE_INTEGER,
1284
+ {},
1285
+ );
1286
+ }
1287
+
1288
+ // For each Params.PrivilegesCodes.
1289
+ for (const PrivilegeCode of PrivilegeCodes) {
1290
+ // Initialise existing System privilege by calling SystemPrivilege.init
1291
+ const systemPrivilege = await SystemPrivilege.init(
1292
+ dbTransaction,
1293
+ PrivilegeCode,
1294
+ );
1295
+ //Check whether the system codes used by that privilege is exist inside the group system access retrieved from step 2.2 & 2.4. If system code does not exist in group system access, throw a new ClassError by passing:
1296
+ // Classname: "Group"
1297
+ // MessageCode: "GroupErrMsg0X"
1298
+ // Message: "Failed to assign privilege <PrivilegeCode> due to non-existent system access."
1299
+ const combinedSystemAccesses = [
1300
+ ...groupSystemAccesses.rows,
1301
+ ...parentGroupSystemAccesses.rows,
1302
+ ];
1303
+ const systemAccess = combinedSystemAccesses.find(
1304
+ (systemAccess) =>
1305
+ systemAccess.SystemCode === systemPrivilege.SystemCode,
1306
+ );
1307
+ if (!systemAccess) {
1308
+ throw new ClassError(
1309
+ 'Group',
1310
+ 'GroupErrMsg13',
1311
+ 'Failed to assign privilege ' +
1312
+ PrivilegeCode +
1313
+ ' due to non-existent system access.',
1314
+ );
1315
+ }
1316
+
1317
+ //Check whether the group privilege exist by using Group._GroupPrivilegesRepo.findOne
1318
+ const groupPrivilege = await Group._GroupPrivilegeRepo.findOne({
1319
+ where: {
1320
+ GroupCode,
1321
+ PrivilegeCode,
1322
+ },
1323
+ transaction: dbTransaction,
1324
+ });
1325
+
1326
+ //If GroupPrivilege record exist and status is "Active". Skip this loop and proceed to the next privilege code
1327
+ if (groupPrivilege && groupPrivilege.Status === 'Active') {
1328
+ continue;
1329
+ }
1330
+
1331
+ let entityValueBefore = {};
1332
+ let entityValueAfter = {};
1333
+ let action = ActionEnum.ADD;
1334
+ let description = 'Create Group Privilege';
1335
+ let entityId = null;
1336
+ //If GroupPrivilege record exist and status is not "Active" do the following:
1337
+ if (groupPrivilege && groupPrivilege.Status !== 'Active') {
1338
+ //Set this GroupPrivilege entity as EntityValueBefore
1339
+ entityValueBefore = {
1340
+ GroupCode: groupPrivilege.GroupCode,
1341
+ PrivilegeCode: groupPrivilege.PrivilegeCode,
1342
+ Status: groupPrivilege.Status,
1343
+ CreatedById: groupPrivilege.CreatedById,
1344
+ CreatedAt: groupPrivilege.CreatedAt,
1345
+ UpdatedById: groupPrivilege.UpdatedById,
1346
+ UpdatedAt: groupPrivilege.UpdatedAt,
1347
+ };
1348
+
1349
+ //Update the status to active using Group._GroupPrivilegesRepo.Update.
1350
+ const updatedPayload = {
1351
+ Status: 'Active',
1352
+ UpdatedById: loginUser.UserId,
1353
+ UpdatedAt: new Date(),
1354
+ };
1355
+ await Group._GroupPrivilegeRepo.update(updatedPayload, {
1356
+ where: {
1357
+ GroupCode,
1358
+ PrivilegeCode,
1359
+ },
1360
+ transaction: dbTransaction,
1361
+ });
1362
+
1363
+ //Set updated GroupPrivilege as EntityValueAfter
1364
+ entityValueAfter = {
1365
+ GroupCode: groupPrivilege.GroupCode,
1366
+ PrivilegeCode: groupPrivilege.PrivilegeCode,
1367
+ Status: updatedPayload.Status,
1368
+ CreatedById: groupPrivilege.CreatedById,
1369
+ CreatedAt: groupPrivilege.CreatedAt,
1370
+ UpdatedById: updatedPayload.UpdatedById,
1371
+ UpdatedAt: updatedPayload.UpdatedAt,
1372
+ };
1373
+
1374
+ //Instantiate new activity from Activity class
1375
+ action = ActionEnum.UPDATE;
1376
+ description = 'Update Group Privilege';
1377
+ entityId = groupPrivilege.GroupPrivilegeId;
1378
+ } else {
1379
+ //If GroupPrivilege record does not exist, do the following:
1380
+ //Initialise empty GroupPrivilege.
1381
+ const newGroupPrivilege = await GroupPrivilege.init(dbTransaction);
1382
+ //Set the attributes
1383
+ newGroupPrivilege.setAttributes({
1384
+ GroupCode,
1385
+ PrivilegeCode,
1386
+ Status: 'Active',
1387
+ CreatedById: loginUser.UserId,
1388
+ CreatedAt: new Date(),
1389
+ UpdatedById: loginUser.UserId,
1390
+ UpdatedAt: new Date(),
1391
+ });
1392
+
1393
+ // Set EntityValueAfter to above instance.
1394
+ entityValueAfter = {
1395
+ GroupCode: newGroupPrivilege.GroupCode,
1396
+ PrivilegeCode: newGroupPrivilege.PrivilegeCode,
1397
+ Status: newGroupPrivilege.Status,
1398
+ CreatedById: newGroupPrivilege.CreatedById,
1399
+ CreatedAt: newGroupPrivilege.CreatedAt,
1400
+ UpdatedById: newGroupPrivilege.UpdatedById,
1401
+ UpdatedAt: newGroupPrivilege.UpdatedAt,
1402
+ };
1403
+
1404
+ //Call Group._GroupPrivilegesRepo.create
1405
+ const groupPrivilege = await Group._GroupPrivilegeRepo.create(
1406
+ entityValueAfter,
1407
+ {
1408
+ transaction: dbTransaction,
1409
+ },
1410
+ );
1411
+ action = ActionEnum.ADD;
1412
+ description = 'Create Group Privilege';
1413
+ entityId = groupPrivilege.GroupPrivilegeId;
1414
+ }
1415
+
1416
+ //Instantiate new activity from Activity class, call createId() method, then set:
1417
+ const activity = new Activity();
1418
+ activity.ActivityId = activity.createId();
1419
+ activity.Action = action;
1420
+ activity.Description = description;
1421
+ activity.EntityType = 'GroupPrivilege';
1422
+ activity.EntityId = entityId;
1423
+ activity.EntityValueBefore = JSON.stringify(entityValueBefore);
1424
+ activity.EntityValueAfter = JSON.stringify(entityValueAfter);
1425
+
1426
+ //Call new activity create method
1427
+ await activity.create(loginUser.ObjectId, dbTransaction);
1428
+ }
1429
+
1430
+ return 'Successfully added.';
1431
+ } catch (error) {
1432
+ throw error;
1433
+ }
1434
+ }
1435
+
1436
+ public static async deleteGroupPrivilege(
1437
+ loginUser: LoginUser,
1438
+ dbTransaction: any,
1439
+ GroupCode: string,
1440
+ PrivilegeCodes: string[],
1441
+ ) {
1442
+ try {
1443
+ // Part 1: Privilege Checking
1444
+ const systemCode =
1445
+ ApplicationConfig.getComponentConfigValue('system-code');
1446
+ const isPrivileged = await loginUser.checkPrivileges(
1447
+ systemCode,
1448
+ 'GROUP_PRIVILEGE_DELETE',
1449
+ );
1450
+
1451
+ if (!isPrivileged) {
1452
+ throw new ClassError(
1453
+ 'Group',
1454
+ 'GroupErrMsg06',
1455
+ 'You do not have the privilege to delete group privileges',
1456
+ );
1457
+ }
1458
+
1459
+ // Part 2: Validation, Create and Record Activity
1460
+ // For each Params.PrivilegesCodes.
1461
+ for (const PrivilegeCode of PrivilegeCodes) {
1462
+ //Check whether the record exist in database by calling Group._GroupPrivilegeRepo.findOne
1463
+ const groupPrivilege = await Group._GroupPrivilegeRepo.findOne({
1464
+ where: {
1465
+ GroupCode,
1466
+ PrivilegeCode,
1467
+ },
1468
+ transaction: dbTransaction,
1469
+ });
1470
+
1471
+ //If the record does not exist, throw a new ClassError
1472
+ if (!groupPrivilege) {
1473
+ throw new ClassError(
1474
+ 'Group',
1475
+ 'GroupErrMsg14',
1476
+ 'GroupPrivilege not found.',
1477
+ );
1478
+ }
1479
+
1480
+ //Set the EntityValueBefore to the GroupPrivilegesValue from step 1.c.
1481
+ const entityValueBefore = {
1482
+ GroupCode: groupPrivilege.GroupCode,
1483
+ PrivilegeCode: groupPrivilege.PrivilegeCode,
1484
+ Status: groupPrivilege.Status,
1485
+ CreatedById: groupPrivilege.CreatedById,
1486
+ CreatedAt: groupPrivilege.CreatedAt,
1487
+ UpdatedById: groupPrivilege.UpdatedById,
1488
+ UpdatedAt: groupPrivilege.UpdatedAt,
1489
+ };
1490
+
1491
+ //Call Group._GroupPrivilegeRepo.delete
1492
+ await Group._GroupPrivilegeRepo.delete(
1493
+ GroupCode,
1494
+ PrivilegeCode,
1495
+ dbTransaction,
1496
+ );
1497
+ // Instantiate new activity from Activity class, call createId() method, then set:
1498
+ const activity = new Activity();
1499
+ activity.ActivityId = activity.createId();
1500
+ activity.Action = ActionEnum.DELETE;
1501
+ activity.Description = 'DELETE Group Privilege';
1502
+ activity.EntityType = 'GroupPrivilege';
1503
+ activity.EntityId = groupPrivilege.GroupPrivilegeId.toString();
1504
+ activity.EntityValueBefore = JSON.stringify(entityValueBefore);
1505
+ activity.EntityValueAfter = JSON.stringify({});
1506
+ //Call new activity create method
1507
+ await activity.create(loginUser.ObjectId, dbTransaction);
1508
+ }
1509
+ return 'Successfully deleted.';
1510
+ } catch (error) {
1511
+ throw error;
1512
+ }
1513
+ }
1514
+ }