@tomei/sso 0.37.4 → 0.38.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (95) hide show
  1. package/__tests__/unit/components/group-object-privilege/group-object-privilege.spec.ts +4 -4
  2. package/__tests__/unit/components/group-privilege/group-privilege.spec.ts +3 -3
  3. package/__tests__/unit/components/user-object-privilege/user-object-privilege.spec.ts +3 -3
  4. package/__tests__/unit/components/user-privilege/user-privilege.spec.ts +3 -3
  5. package/dist/__tests__/unit/components/group-object-privilege/group-object-privilege.spec.js +4 -4
  6. package/dist/__tests__/unit/components/group-object-privilege/group-object-privilege.spec.js.map +1 -1
  7. package/dist/__tests__/unit/components/group-privilege/group-privilege.spec.js +3 -3
  8. package/dist/__tests__/unit/components/group-privilege/group-privilege.spec.js.map +1 -1
  9. package/dist/__tests__/unit/components/user-object-privilege/user-object-privilege.spec.js +3 -3
  10. package/dist/__tests__/unit/components/user-object-privilege/user-object-privilege.spec.js.map +1 -1
  11. package/dist/__tests__/unit/components/user-privilege/user-privilege.spec.js +3 -3
  12. package/dist/__tests__/unit/components/user-privilege/user-privilege.spec.js.map +1 -1
  13. package/dist/src/components/group/group.d.ts +24 -12
  14. package/dist/src/components/group/group.js +194 -42
  15. package/dist/src/components/group/group.js.map +1 -1
  16. package/dist/src/components/group-object-privilege/group-object-privilege.d.ts +1 -1
  17. package/dist/src/components/group-object-privilege/group-object-privilege.js +7 -7
  18. package/dist/src/components/group-object-privilege/group-object-privilege.js.map +1 -1
  19. package/dist/src/components/group-privilege/group-privilege.d.ts +1 -1
  20. package/dist/src/components/group-privilege/group-privilege.js +2 -2
  21. package/dist/src/components/group-privilege/group-privilege.js.map +1 -1
  22. package/dist/src/components/group-privilege/group-privilege.repository.d.ts +1 -1
  23. package/dist/src/components/group-privilege/group-privilege.repository.js +11 -3
  24. package/dist/src/components/group-privilege/group-privilege.repository.js.map +1 -1
  25. package/dist/src/components/login-user/login-user.js.map +1 -1
  26. package/dist/src/components/login-user/user.js +2 -2
  27. package/dist/src/components/login-user/user.js.map +1 -1
  28. package/dist/src/components/system/system.js +1 -1
  29. package/dist/src/components/system/system.js.map +1 -1
  30. package/dist/src/components/system-privilege/system-privilege.d.ts +4 -3
  31. package/dist/src/components/system-privilege/system-privilege.js +18 -11
  32. package/dist/src/components/system-privilege/system-privilege.js.map +1 -1
  33. package/dist/src/components/user-group/user-group.js +1 -1
  34. package/dist/src/components/user-group/user-group.js.map +1 -1
  35. package/dist/src/components/user-object-privilege/user-object-privilege.d.ts +1 -1
  36. package/dist/src/components/user-object-privilege/user-object-privilege.js +1 -1
  37. package/dist/src/components/user-object-privilege/user-object-privilege.js.map +1 -1
  38. package/dist/src/components/user-privilege/user-privilege.d.ts +1 -1
  39. package/dist/src/components/user-privilege/user-privilege.js +1 -1
  40. package/dist/src/components/user-privilege/user-privilege.js.map +1 -1
  41. package/dist/src/interfaces/group-object-privilege.interface.d.ts +1 -1
  42. package/dist/src/interfaces/group-privilege.interface.d.ts +1 -1
  43. package/dist/src/interfaces/group.interface.d.ts +1 -0
  44. package/dist/src/interfaces/system-privilege.interface.d.ts +1 -0
  45. package/dist/src/interfaces/user-object-privilege.interface.d.ts +1 -1
  46. package/dist/src/interfaces/user-privilege.interface.d.ts +1 -1
  47. package/dist/src/models/group-object-privilege.entity.d.ts +1 -1
  48. package/dist/src/models/group-object-privilege.entity.js +1 -1
  49. package/dist/src/models/group-object-privilege.entity.js.map +1 -1
  50. package/dist/src/models/group-privilege.entity.d.ts +1 -1
  51. package/dist/src/models/group-privilege.entity.js +1 -1
  52. package/dist/src/models/group-privilege.entity.js.map +1 -1
  53. package/dist/src/models/group.entity.d.ts +1 -0
  54. package/dist/src/models/group.entity.js +7 -0
  55. package/dist/src/models/group.entity.js.map +1 -1
  56. package/dist/src/models/system-privilege.entity.d.ts +1 -0
  57. package/dist/src/models/system-privilege.entity.js +8 -0
  58. package/dist/src/models/system-privilege.entity.js.map +1 -1
  59. package/dist/src/models/user-object-privilege.entity.d.ts +1 -1
  60. package/dist/src/models/user-object-privilege.entity.js +1 -1
  61. package/dist/src/models/user-object-privilege.entity.js.map +1 -1
  62. package/dist/src/models/user-privilege.entity.d.ts +1 -1
  63. package/dist/src/models/user-privilege.entity.js +1 -1
  64. package/dist/src/models/user-privilege.entity.js.map +1 -1
  65. package/dist/tsconfig.tsbuildinfo +1 -1
  66. package/migrations/20240527064926-create-system-privilege-table.js +66 -62
  67. package/migrations/20240527065342-create-group-table.js +4 -0
  68. package/migrations/20240528032229-user-privilege-table.js +2 -2
  69. package/migrations/20240528063003-create-group-privilege-table.js +12 -2
  70. package/migrations/20240528063051-create-group-object-privilege-table.js +2 -2
  71. package/migrations/20240528063107-create-user-object-privilege-table.js +2 -2
  72. package/package.json +8 -8
  73. package/src/components/group/group.ts +231 -60
  74. package/src/components/group-object-privilege/group-object-privilege.ts +9 -9
  75. package/src/components/group-privilege/group-privilege.repository.ts +16 -3
  76. package/src/components/group-privilege/group-privilege.ts +3 -3
  77. package/src/components/login-user/login-user.ts +0 -58
  78. package/src/components/login-user/user.ts +2 -4
  79. package/src/components/system/system.ts +1 -1
  80. package/src/components/system-privilege/system-privilege.ts +20 -12
  81. package/src/components/user-group/user-group.ts +1 -1
  82. package/src/components/user-object-privilege/user-object-privilege.ts +2 -2
  83. package/src/components/user-privilege/user-privilege.ts +2 -2
  84. package/src/interfaces/group-object-privilege.interface.ts +1 -1
  85. package/src/interfaces/group-privilege.interface.ts +1 -1
  86. package/src/interfaces/group.interface.ts +1 -0
  87. package/src/interfaces/system-privilege.interface.ts +1 -0
  88. package/src/interfaces/user-object-privilege.interface.ts +1 -1
  89. package/src/interfaces/user-privilege.interface.ts +1 -1
  90. package/src/models/group-object-privilege.entity.ts +1 -1
  91. package/src/models/group-privilege.entity.ts +1 -1
  92. package/src/models/group.entity.ts +6 -0
  93. package/src/models/system-privilege.entity.ts +7 -0
  94. package/src/models/user-object-privilege.entity.ts +1 -1
  95. package/src/models/user-privilege.entity.ts +1 -1
@@ -1,4 +1,4 @@
1
- import { ClassError, ObjectBase } from '@tomei/general';
1
+ import { ClassError, ObjectBase, TreeNodeBase } from '@tomei/general';
2
2
  import { GroupRepository } from './group.repository';
3
3
  import { IGroupAttr } from '../../interfaces/group.interface';
4
4
  import { GroupTypeEnum } from 'enum';
@@ -19,7 +19,7 @@ import { GroupObjectPrivilegeRepository } from '../group-object-privilege/group-
19
19
  import { GroupObjectPrivilege } from '../group-object-privilege/group-object-privilege';
20
20
  import { GroupPrivilege } from '../group-privilege/group-privilege';
21
21
 
22
- export class Group extends ObjectBase {
22
+ export class Group extends TreeNodeBase<Group> {
23
23
  ObjectId: string;
24
24
  ObjectName: string;
25
25
  TableName: 'sso_Group';
@@ -33,6 +33,10 @@ export class Group extends ObjectBase {
33
33
  InheritParentSystemAccessYN: string;
34
34
  Status: string;
35
35
  ParentGroup?: any;
36
+ _Path: string = '';
37
+ isChildrenLoaded = false;
38
+ isParentLoaded = false;
39
+
36
40
  private _CreatedById: number;
37
41
  private _CreatedAt: Date;
38
42
  private _UpdatedById: number;
@@ -43,6 +47,7 @@ export class Group extends ObjectBase {
43
47
  private static _GroupObjectPrivilegeRepo =
44
48
  new GroupObjectPrivilegeRepository();
45
49
  private static _RedisService: RedisService;
50
+
46
51
  get GroupCode(): string {
47
52
  return this.ObjectId;
48
53
  }
@@ -67,6 +72,10 @@ export class Group extends ObjectBase {
67
72
  return this._UpdatedAt;
68
73
  }
69
74
 
75
+ set Path(value: string) {
76
+ this._Path = value;
77
+ }
78
+
70
79
  private constructor(groupAttr?: IGroupAttr) {
71
80
  super();
72
81
  if (groupAttr) {
@@ -78,6 +87,7 @@ export class Group extends ObjectBase {
78
87
  this.InheritParentPrivilegeYN = groupAttr?.InheritParentPrivilegeYN;
79
88
  this.InheritParentSystemAccessYN = groupAttr?.InheritParentSystemAccessYN;
80
89
  this.Status = groupAttr?.Status;
90
+ this._Path = groupAttr?.Path;
81
91
  this._CreatedById = groupAttr.CreatedById;
82
92
  this._CreatedAt = groupAttr.CreatedAt;
83
93
  this._UpdatedById = groupAttr.UpdatedById;
@@ -108,6 +118,82 @@ export class Group extends ObjectBase {
108
118
  }
109
119
  }
110
120
 
121
+ async loadChildren(dbTransaction?: any) {
122
+ if (!this.GroupCode) {
123
+ throw Error('GroupCode is missing.');
124
+ }
125
+
126
+ const children = await Group._Repo.findAll({
127
+ where: { ParentGroupCode: this.GroupCode },
128
+ order: [['CreatedAt', 'ASC']],
129
+ transaction: dbTransaction,
130
+ });
131
+
132
+ this.children = children.map((child) => {
133
+ return new Group(child.get({ plain: true }));
134
+ });
135
+
136
+ this.isChildrenLoaded = true;
137
+ }
138
+
139
+ async loadParent(dbTransaction?: any) {
140
+ if (!this.GroupCode) {
141
+ throw Error('GroupCode are missing.');
142
+ }
143
+
144
+ if (this.ParentGroupCode) {
145
+ if (this.ParentGroupCode !== this.GroupCode) {
146
+ const parent = await Group._Repo.findByPk(this.ParentGroupCode, {
147
+ transaction: dbTransaction,
148
+ });
149
+ this.parent = new Group(parent.get({ plain: true }));
150
+ }
151
+ }
152
+
153
+ this.isParentLoaded = true;
154
+ }
155
+
156
+ async isLeaf(dbTransaction?: any): Promise<boolean> {
157
+ if (!this.isChildrenLoaded) {
158
+ await this.loadChildren(dbTransaction);
159
+ }
160
+
161
+ return this.children.length === 0;
162
+ }
163
+
164
+ async getPath(dbTransaction?: any): Promise<string> {
165
+ if (!this.isParentLoaded) {
166
+ await this.loadParent(dbTransaction);
167
+ }
168
+
169
+ if (this.parent) {
170
+ this._Path = (await this.parent.getPath(dbTransaction)) + '/' + this.Name;
171
+ return this._Path;
172
+ }
173
+ this._Path = this.GroupCode;
174
+ return this._Path;
175
+ }
176
+
177
+ protected async updatePath(dbTransaction?: any): Promise<void> {
178
+ const path = await this.getPath(dbTransaction);
179
+ this._Path = path;
180
+ }
181
+
182
+ async setParent(parent: Group, dbTransaction?: any): Promise<void> {
183
+ this.parent = parent;
184
+ await this.getPath(dbTransaction);
185
+ }
186
+
187
+ async getPathDetail(dbTransaction?: any): Promise<Group[]> {
188
+ const path = this._Path.split('/');
189
+ const groups: Group[] = [];
190
+ for (let i = 0; i < path.length; i++) {
191
+ const group = await Group.init(dbTransaction, path[i]);
192
+ groups.push(group);
193
+ }
194
+ return groups;
195
+ }
196
+
111
197
  public static async findAll(
112
198
  page: number,
113
199
  row: number,
@@ -263,6 +349,7 @@ export class Group extends ObjectBase {
263
349
  newGroup.Status = 'Active';
264
350
  newGroup._CreatedById = loginUser.UserId;
265
351
  newGroup._UpdatedById = loginUser.UserId;
352
+ newGroup._Path = await newGroup.getPath(dbTransaction);
266
353
 
267
354
  //Call Group._Repo create method
268
355
  const entityGroupAfter = {
@@ -273,6 +360,7 @@ export class Group extends ObjectBase {
273
360
  ParentGroupCode: newGroup.ParentGroupCode,
274
361
  InheritParentPrivilegeYN: newGroup.InheritParentPrivilegeYN,
275
362
  InheritParentSystemAccessYN: newGroup.InheritParentSystemAccessYN,
363
+ Path: newGroup._Path,
276
364
  Status: newGroup.Status,
277
365
  CreatedById: newGroup._CreatedById,
278
366
  UpdatedById: newGroup._UpdatedById,
@@ -291,7 +379,7 @@ export class Group extends ObjectBase {
291
379
  //Instantiate new activity
292
380
  const activity = new Activity();
293
381
  activity.ActivityId = activity.createId();
294
- activity.Action = ActionEnum.ADD;
382
+ activity.Action = ActionEnum.CREATE;
295
383
  activity.Description = 'Create Group';
296
384
  activity.EntityType = 'Group';
297
385
  activity.EntityId = newGroup.ObjectId;
@@ -325,19 +413,49 @@ export class Group extends ObjectBase {
325
413
  }
326
414
  }
327
415
 
416
+ private async updateChildrenPath(oldGroupCode: string, dbTransaction: any) {
417
+ try {
418
+ const isLeaf = await this.isLeaf(dbTransaction);
419
+ if (isLeaf) {
420
+ return;
421
+ }
422
+
423
+ const childrens = await Group._Repo.findAll({
424
+ where: {
425
+ Path: {
426
+ [Op.like]: `${oldGroupCode}/%`,
427
+ },
428
+ },
429
+ transaction: dbTransaction,
430
+ });
431
+
432
+ childrens.forEach(async (children) => {
433
+ //Break the path into array with oldGroupCode/ as separator;
434
+ const path = children.Path.split(`${oldGroupCode}/`);
435
+ //Retrive the last element of the array
436
+ const childPath = path[path.length - 1];
437
+ //Combine the childPath with this.Path
438
+ children.Path = `${this.Path}/${childPath}`;
439
+ await children.update({ Path: path }, { transaction: dbTransaction });
440
+ });
441
+ } catch (error) {
442
+ throw error;
443
+ }
444
+ }
445
+
328
446
  public async update(
329
447
  loginUser: LoginUser,
330
448
  dbTransaction: any,
331
449
  group: {
332
450
  GroupCode: string;
333
451
  NewGroupCode?: string;
334
- Name?: string;
335
- Description?: string;
336
- Type?: GroupTypeEnum;
337
- ParentGroupCode?: string;
338
- InheritParentPrivilegeYN?: string;
339
- InheritParentSystemAccessYN?: string;
340
- Status?: string;
452
+ Name: string;
453
+ Description: string;
454
+ Type: GroupTypeEnum;
455
+ ParentGroupCode: string;
456
+ InheritParentPrivilegeYN: string;
457
+ InheritParentSystemAccessYN: string;
458
+ Status: string;
341
459
  },
342
460
  ) {
343
461
  //Part 1: Privilege Checking
@@ -360,9 +478,34 @@ export class Group extends ObjectBase {
360
478
  await Group.checkDuplicateGroupCode(dbTransaction, group.NewGroupCode);
361
479
  }
362
480
 
481
+ const entityValueBefore = {
482
+ GroupCode: currentGroup.GroupCode,
483
+ Name: currentGroup.Name,
484
+ Type: currentGroup.Type,
485
+ Description: currentGroup.Description,
486
+ ParentGroupCode: currentGroup.ParentGroupCode,
487
+ InheritParentPrivilegeYN: currentGroup.InheritParentPrivilegeYN,
488
+ InheritParentSystemAccessYN: currentGroup.InheritParentSystemAccessYN,
489
+ Path: this.Path,
490
+ Status: currentGroup.Status,
491
+ CreatedById: currentGroup._CreatedById,
492
+ UpdatedById: currentGroup._UpdatedById,
493
+ CreatedAt: currentGroup._CreatedAt,
494
+ UpdatedAt: currentGroup._UpdatedAt,
495
+ };
496
+
497
+ let isPathChanged = false;
498
+ const oldGroupCode = currentGroup.GroupCode;
499
+ if (group.NewGroupCode) {
500
+ currentGroup.GroupCode = group.NewGroupCode;
501
+ isPathChanged = true;
502
+ }
503
+
504
+ //Check if ParentGroupCode is changed or added
363
505
  if (
364
- group.ParentGroupCode &&
365
- currentGroup.ParentGroupCode !== group.ParentGroupCode
506
+ (group.ParentGroupCode &&
507
+ currentGroup.ParentGroupCode !== group.ParentGroupCode) ||
508
+ (group.ParentGroupCode && !currentGroup.ParentGroupCode)
366
509
  ) {
367
510
  const parentGroup = await Group.init(
368
511
  dbTransaction,
@@ -375,24 +518,18 @@ export class Group extends ObjectBase {
375
518
  'Parent Group Code not found',
376
519
  );
377
520
  }
521
+ await currentGroup.setParent(parentGroup);
522
+ //Check if ParentGroupCode is removed
523
+ isPathChanged = true;
524
+ } else if (!group.ParentGroupCode && currentGroup.ParentGroupCode) {
525
+ await currentGroup.setParent(null);
526
+ isPathChanged = true;
378
527
  }
379
528
 
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
- };
529
+ if (isPathChanged) {
530
+ await this.updateChildrenPath(oldGroupCode, dbTransaction);
531
+ }
394
532
 
395
- currentGroup.GroupCode = group?.NewGroupCode || currentGroup.GroupCode;
396
533
  currentGroup.Name = group?.Name || currentGroup.Name;
397
534
  currentGroup.Type = group?.Type || currentGroup.Type;
398
535
  currentGroup.Description = group?.Description || currentGroup.Description;
@@ -780,7 +917,7 @@ export class Group extends ObjectBase {
780
917
 
781
918
  const activity = new Activity();
782
919
  activity.ActivityId = activity.createId();
783
- activity.Action = ActionEnum.ADD;
920
+ activity.Action = ActionEnum.CREATE;
784
921
  activity.Description = 'Create Group System Access';
785
922
  activity.EntityType = 'GroupSystemAccess';
786
923
  activity.EntityId = systemAccess.GroupSystemAccessId?.toString();
@@ -984,6 +1121,15 @@ export class Group extends ObjectBase {
984
1121
  },
985
1122
  };
986
1123
  }
1124
+
1125
+ if (search.PrivilegeCode) {
1126
+ systemPrivilegeWhere = {
1127
+ ...systemPrivilegeWhere,
1128
+ PrivilegeCode: {
1129
+ [Op.substring]: search.PrivilegeCode,
1130
+ },
1131
+ };
1132
+ }
987
1133
  }
988
1134
  const group = await Group._Repo.findOne({
989
1135
  where: where,
@@ -1010,12 +1156,12 @@ export class Group extends ObjectBase {
1010
1156
  const systemWhere: any = {};
1011
1157
  if (search) {
1012
1158
  Object.entries(search).forEach(([key, value]) => {
1013
- if (key === 'SystemCode') {
1014
- systemWhere[key] = {
1159
+ if (key === 'Status') {
1160
+ objectWhere[key] = {
1015
1161
  [Op.substring]: value,
1016
1162
  };
1017
1163
  } else {
1018
- objectWhere[key] = {
1164
+ systemWhere[key] = {
1019
1165
  [Op.substring]: value,
1020
1166
  };
1021
1167
  }
@@ -1064,9 +1210,11 @@ export class Group extends ObjectBase {
1064
1210
 
1065
1211
  //format to make sure no duplicate
1066
1212
  const uniquePrivileges = Array.from(
1067
- new Set(privileges.map((a) => a.PrivilegeCode)),
1068
- ).map((PrivilegeCode) => {
1069
- return privileges.find((a) => a.PrivilegeCode === PrivilegeCode);
1213
+ new Set(privileges.map((a) => a.SystemPrivilegeId)),
1214
+ ).map((SystemPrivilegeId) => {
1215
+ return privileges.find(
1216
+ (a) => a.SystemPrivilegeId === SystemPrivilegeId,
1217
+ );
1070
1218
  });
1071
1219
 
1072
1220
  return uniquePrivileges;
@@ -1129,6 +1277,7 @@ export class Group extends ObjectBase {
1129
1277
  dbTransaction: any,
1130
1278
  GroupCode: string,
1131
1279
  GroupObjectPrivileges: GroupObjectPrivilege[],
1280
+ SystemCode: string,
1132
1281
  ): Promise<string> {
1133
1282
  try {
1134
1283
  //Part 1: Privilege Checking
@@ -1174,7 +1323,7 @@ export class Group extends ObjectBase {
1174
1323
  group.ParentGroupCode,
1175
1324
  1,
1176
1325
  Number.MAX_SAFE_INTEGER,
1177
- {},
1326
+ undefined,
1178
1327
  );
1179
1328
  }
1180
1329
 
@@ -1183,7 +1332,7 @@ export class Group extends ObjectBase {
1183
1332
  //Initialise existing System privilege
1184
1333
  const systemPrivilege = await SystemPrivilege.init(
1185
1334
  dbTransaction,
1186
- groupObjectPrivilege.PrivilegeCode,
1335
+ groupObjectPrivilege.SystemPrivilegeId,
1187
1336
  );
1188
1337
  //Check whether the system codes used by that privilege is exist inside the group system access
1189
1338
  const combinedSystemAccesses = {
@@ -1199,7 +1348,7 @@ export class Group extends ObjectBase {
1199
1348
  'Group',
1200
1349
  'GroupErrMsg13',
1201
1350
  'Failed to assign privilege ' +
1202
- groupObjectPrivilege.PrivilegeCode +
1351
+ groupObjectPrivilege.SystemPrivilegeId +
1203
1352
  ' due to non-existent system access.',
1204
1353
  );
1205
1354
  }
@@ -1209,7 +1358,7 @@ export class Group extends ObjectBase {
1209
1358
  await Group._GroupObjectPrivilegeRepo.findOne({
1210
1359
  where: {
1211
1360
  GroupCode,
1212
- PrivilegeCode: groupObjectPrivilege.PrivilegeCode,
1361
+ SystemPrivilegeId: groupObjectPrivilege.SystemPrivilegeId,
1213
1362
  ObjectId: groupObjectPrivilege.ObjectId,
1214
1363
  ObjectType: groupObjectPrivilege.ObjectType,
1215
1364
  },
@@ -1276,7 +1425,7 @@ export class Group extends ObjectBase {
1276
1425
 
1277
1426
  if (search) {
1278
1427
  Object.entries(search).forEach(([key, value]) => {
1279
- if (key === 'SystemCode') {
1428
+ if (key === 'SystemCode' || key === 'PrivilegeCode') {
1280
1429
  systemWhere[key] = {
1281
1430
  [Op.substring]: value,
1282
1431
  };
@@ -1311,9 +1460,11 @@ export class Group extends ObjectBase {
1311
1460
 
1312
1461
  //Remove duplicate
1313
1462
  const uniquePrivileges = Array.from(
1314
- new Set(privileges.map((a) => a.PrivilegeCode)),
1315
- ).map((PrivilegeCode) => {
1316
- return privileges.find((a) => a.PrivilegeCode === PrivilegeCode);
1463
+ new Set(privileges.map((a) => a.SystemPrivilegeId)),
1464
+ ).map((SystemPrivilegeId) => {
1465
+ return privileges.find(
1466
+ (a) => a.SystemPrivilegeId === SystemPrivilegeId,
1467
+ );
1317
1468
  });
1318
1469
 
1319
1470
  // Create the result based on the spec on return then returns it.
@@ -1327,7 +1478,7 @@ export class Group extends ObjectBase {
1327
1478
  loginUser: LoginUser,
1328
1479
  dbTransaction: any,
1329
1480
  GroupCode: string,
1330
- PrivilegeCodes: string[],
1481
+ SystemPrivilegeIds: string[],
1331
1482
  ) {
1332
1483
  try {
1333
1484
  // Part 1: Privilege Checking
@@ -1379,11 +1530,11 @@ export class Group extends ObjectBase {
1379
1530
  }
1380
1531
 
1381
1532
  // For each Params.PrivilegesCodes.
1382
- for (const PrivilegeCode of PrivilegeCodes) {
1533
+ for (const SystemPrivilegeId of SystemPrivilegeIds) {
1383
1534
  // Initialise existing System privilege by calling SystemPrivilege.init
1384
1535
  const systemPrivilege = await SystemPrivilege.init(
1385
1536
  dbTransaction,
1386
- PrivilegeCode,
1537
+ SystemPrivilegeId,
1387
1538
  );
1388
1539
  //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:
1389
1540
  // Classname: "Group"
@@ -1402,7 +1553,7 @@ export class Group extends ObjectBase {
1402
1553
  'Group',
1403
1554
  'GroupErrMsg13',
1404
1555
  'Failed to assign privilege ' +
1405
- PrivilegeCode +
1556
+ SystemPrivilegeId +
1406
1557
  ' due to non-existent system access.',
1407
1558
  );
1408
1559
  }
@@ -1411,8 +1562,13 @@ export class Group extends ObjectBase {
1411
1562
  const groupPrivilege = await Group._GroupPrivilegeRepo.findOne({
1412
1563
  where: {
1413
1564
  GroupCode,
1414
- PrivilegeCode,
1565
+ SystemPrivilegeId,
1415
1566
  },
1567
+ include: [
1568
+ {
1569
+ model: SystemPrivilegeModel,
1570
+ },
1571
+ ],
1416
1572
  transaction: dbTransaction,
1417
1573
  });
1418
1574
 
@@ -1423,7 +1579,7 @@ export class Group extends ObjectBase {
1423
1579
 
1424
1580
  let entityValueBefore = {};
1425
1581
  let entityValueAfter = {};
1426
- let action = ActionEnum.ADD;
1582
+ let action = ActionEnum.CREATE;
1427
1583
  let description = 'Create Group Privilege';
1428
1584
  let entityId = null;
1429
1585
  //If GroupPrivilege record exist and status is not "Active" do the following:
@@ -1431,7 +1587,7 @@ export class Group extends ObjectBase {
1431
1587
  //Set this GroupPrivilege entity as EntityValueBefore
1432
1588
  entityValueBefore = {
1433
1589
  GroupCode: groupPrivilege.GroupCode,
1434
- PrivilegeCode: groupPrivilege.PrivilegeCode,
1590
+ SystemPrivilegeId: groupPrivilege.SystemPrivilegeId,
1435
1591
  Status: groupPrivilege.Status,
1436
1592
  CreatedById: groupPrivilege.CreatedById,
1437
1593
  CreatedAt: groupPrivilege.CreatedAt,
@@ -1445,18 +1601,27 @@ export class Group extends ObjectBase {
1445
1601
  UpdatedById: loginUser.UserId,
1446
1602
  UpdatedAt: new Date(),
1447
1603
  };
1448
- await Group._GroupPrivilegeRepo.update(updatedPayload, {
1604
+ const data = await Group._GroupPrivilegeRepo.findOne({
1449
1605
  where: {
1450
1606
  GroupCode,
1451
- PrivilegeCode,
1607
+ SystemPrivilegeId,
1452
1608
  },
1609
+ include: [
1610
+ {
1611
+ model: SystemPrivilegeModel,
1612
+ },
1613
+ ],
1453
1614
  transaction: dbTransaction,
1454
1615
  });
1616
+ data.Status = 'Active';
1617
+ data.UpdatedById = updatedPayload.UpdatedById;
1618
+ data.UpdatedAt = updatedPayload.UpdatedAt;
1619
+ await data.save({ transaction: dbTransaction });
1455
1620
 
1456
1621
  //Set updated GroupPrivilege as EntityValueAfter
1457
1622
  entityValueAfter = {
1458
1623
  GroupCode: groupPrivilege.GroupCode,
1459
- PrivilegeCode: groupPrivilege.PrivilegeCode,
1624
+ SystemPrivilegeId: groupPrivilege.SystemPrivilegeId,
1460
1625
  Status: updatedPayload.Status,
1461
1626
  CreatedById: groupPrivilege.CreatedById,
1462
1627
  CreatedAt: groupPrivilege.CreatedAt,
@@ -1475,7 +1640,7 @@ export class Group extends ObjectBase {
1475
1640
  //Set the attributes
1476
1641
  newGroupPrivilege.setAttributes({
1477
1642
  GroupCode,
1478
- PrivilegeCode,
1643
+ SystemPrivilegeId,
1479
1644
  Status: 'Active',
1480
1645
  CreatedById: loginUser.UserId,
1481
1646
  CreatedAt: new Date(),
@@ -1486,7 +1651,7 @@ export class Group extends ObjectBase {
1486
1651
  // Set EntityValueAfter to above instance.
1487
1652
  entityValueAfter = {
1488
1653
  GroupCode: newGroupPrivilege.GroupCode,
1489
- PrivilegeCode: newGroupPrivilege.PrivilegeCode,
1654
+ SystemPrivilegeId: newGroupPrivilege.SystemPrivilegeId,
1490
1655
  Status: newGroupPrivilege.Status,
1491
1656
  CreatedById: newGroupPrivilege.CreatedById,
1492
1657
  CreatedAt: newGroupPrivilege.CreatedAt,
@@ -1501,7 +1666,7 @@ export class Group extends ObjectBase {
1501
1666
  transaction: dbTransaction,
1502
1667
  },
1503
1668
  );
1504
- action = ActionEnum.ADD;
1669
+ action = ActionEnum.CREATE;
1505
1670
  description = 'Create Group Privilege';
1506
1671
  entityId = groupPrivilege.GroupPrivilegeId;
1507
1672
  }
@@ -1530,7 +1695,7 @@ export class Group extends ObjectBase {
1530
1695
  loginUser: LoginUser,
1531
1696
  dbTransaction: any,
1532
1697
  GroupCode: string,
1533
- PrivilegeCodes: string[],
1698
+ SystemPrivilegeIds: string[],
1534
1699
  ) {
1535
1700
  try {
1536
1701
  // Part 1: Privilege Checking
@@ -1551,13 +1716,18 @@ export class Group extends ObjectBase {
1551
1716
 
1552
1717
  // Part 2: Validation, Create and Record Activity
1553
1718
  // For each Params.PrivilegesCodes.
1554
- for (const PrivilegeCode of PrivilegeCodes) {
1719
+ for (const SystemPrivilegeId of SystemPrivilegeIds) {
1555
1720
  //Check whether the record exist in database by calling Group._GroupPrivilegeRepo.findOne
1556
1721
  const groupPrivilege = await Group._GroupPrivilegeRepo.findOne({
1557
1722
  where: {
1558
1723
  GroupCode,
1559
- PrivilegeCode,
1724
+ SystemPrivilegeId,
1560
1725
  },
1726
+ include: [
1727
+ {
1728
+ model: SystemPrivilegeModel,
1729
+ },
1730
+ ],
1561
1731
  transaction: dbTransaction,
1562
1732
  });
1563
1733
 
@@ -1573,7 +1743,7 @@ export class Group extends ObjectBase {
1573
1743
  //Set the EntityValueBefore to the GroupPrivilegesValue from step 1.c.
1574
1744
  const entityValueBefore = {
1575
1745
  GroupCode: groupPrivilege.GroupCode,
1576
- PrivilegeCode: groupPrivilege.PrivilegeCode,
1746
+ SystemPrivilegeId: groupPrivilege.SystemPrivilegeId,
1577
1747
  Status: groupPrivilege.Status,
1578
1748
  CreatedById: groupPrivilege.CreatedById,
1579
1749
  CreatedAt: groupPrivilege.CreatedAt,
@@ -1584,9 +1754,10 @@ export class Group extends ObjectBase {
1584
1754
  //Call Group._GroupPrivilegeRepo.delete
1585
1755
  await Group._GroupPrivilegeRepo.delete(
1586
1756
  GroupCode,
1587
- PrivilegeCode,
1757
+ SystemPrivilegeId,
1588
1758
  dbTransaction,
1589
1759
  );
1760
+
1590
1761
  // Instantiate new activity from Activity class, call createId() method, then set:
1591
1762
  const activity = new Activity();
1592
1763
  activity.ActivityId = activity.createId();
@@ -11,7 +11,7 @@ export class GroupObjectPrivilege extends ObjectBase {
11
11
  ObjectName: string;
12
12
  GroupObjectPrivilegeId: number;
13
13
  GroupCode: string;
14
- PrivilegeCode: string;
14
+ SystemPrivilegeId: string;
15
15
  ObjectId: string;
16
16
  ObjectType: string;
17
17
  Status: ObjectStatus;
@@ -44,7 +44,7 @@ export class GroupObjectPrivilege extends ObjectBase {
44
44
  this.GroupObjectPrivilegeId =
45
45
  GroupObjectPrivilegeAttr.GroupObjectPrivilegeId;
46
46
  this.GroupCode = GroupObjectPrivilegeAttr.GroupCode;
47
- this.PrivilegeCode = GroupObjectPrivilegeAttr.PrivilegeCode;
47
+ this.SystemPrivilegeId = GroupObjectPrivilegeAttr.SystemPrivilegeId;
48
48
  this.ObjectId = GroupObjectPrivilegeAttr.ObjectId;
49
49
  this.ObjectType = GroupObjectPrivilegeAttr.ObjectType;
50
50
  this.Status = GroupObjectPrivilegeAttr.Status;
@@ -115,12 +115,12 @@ export class GroupObjectPrivilege extends ObjectBase {
115
115
  'GroupCode is missing.',
116
116
  );
117
117
  }
118
- // Params.groupObjectPrivilege.PrivilegeCode
119
- if (!groupObjectPrivilege.PrivilegeCode) {
118
+ // Params.groupObjectPrivilege.SystemPrivilegeId
119
+ if (!groupObjectPrivilege.SystemPrivilegeId) {
120
120
  throw new ClassError(
121
121
  'GroupObjectPrivilege',
122
122
  'GroupObjectPrivilegeErrMsg02',
123
- 'PrivilegeCode is missing.',
123
+ 'SystemPrivilegeId is missing.',
124
124
  );
125
125
  }
126
126
  // Params.groupObjectPrivilege.ObjectId
@@ -145,7 +145,7 @@ export class GroupObjectPrivilege extends ObjectBase {
145
145
  await GroupObjectPrivilege._Repository.findOne({
146
146
  where: {
147
147
  GroupCode: groupObjectPrivilege.GroupCode,
148
- PrivilegeCode: groupObjectPrivilege.PrivilegeCode,
148
+ SystemPrivilegeId: groupObjectPrivilege.SystemPrivilegeId,
149
149
  ObjectId: groupObjectPrivilege.ObjectId,
150
150
  ObjectType: groupObjectPrivilege.ObjectType,
151
151
  },
@@ -165,7 +165,7 @@ export class GroupObjectPrivilege extends ObjectBase {
165
165
  // Initialise new Group instance and populate the attributes
166
166
  const objectAttr = {
167
167
  GroupCode: groupObjectPrivilege.GroupCode,
168
- PrivilegeCode: groupObjectPrivilege.PrivilegeCode,
168
+ SystemPrivilegeId: groupObjectPrivilege.SystemPrivilegeId,
169
169
  ObjectId: groupObjectPrivilege.ObjectId,
170
170
  ObjectType: groupObjectPrivilege.ObjectType,
171
171
  Status: ObjectStatus.ACTIVE,
@@ -194,7 +194,7 @@ export class GroupObjectPrivilege extends ObjectBase {
194
194
  // Instantiate new activity from Activity class
195
195
  const activity = new Activity();
196
196
  activity.createId();
197
- activity.Action = ActionEnum.ADD;
197
+ activity.Action = ActionEnum.CREATE;
198
198
  activity.Description = `Create GroupObjectPrivilege for (${newGroupObjectPrivilege.GroupCode})`;
199
199
  activity.EntityType = 'GroupObjectPrivilege';
200
200
  activity.EntityId =
@@ -245,7 +245,7 @@ export class GroupObjectPrivilege extends ObjectBase {
245
245
  // Initialise EntityValueBefore variable and set GroupObjectPrivelege column properties
246
246
  const EntityValueBefore = {
247
247
  GroupCode: this.GroupCode,
248
- PrivilegeCode: this.PrivilegeCode,
248
+ SystemPrivilegeId: this.SystemPrivilegeId,
249
249
  ObjectId: this.ObjectId,
250
250
  ObjectType: this.ObjectType,
251
251
  Status: this.Status,
@@ -1,3 +1,4 @@
1
+ import SystemPrivilegeModel from '../../models/system-privilege.entity';
1
2
  import GroupPrivilegeModel from '../../models/group-privilege.entity';
2
3
  import { RepositoryBase, IRepositoryBase } from '@tomei/general';
3
4
 
@@ -9,12 +10,24 @@ export class GroupPrivilegeRepository
9
10
  super(GroupPrivilegeModel);
10
11
  }
11
12
 
12
- async delete(GroupCode: string, PrivilegeCode: string, dbTransaction?: any) {
13
+ async delete(
14
+ GroupCode: string,
15
+ PrivilegeCode: string,
16
+ SystemCode: string,
17
+ dbTransaction?: any,
18
+ ) {
13
19
  try {
14
- return await GroupPrivilegeModel.destroy({
20
+ const data = await GroupPrivilegeModel.findOne({
15
21
  where: { GroupCode, PrivilegeCode },
16
- transaction: dbTransaction,
22
+ include: [
23
+ {
24
+ model: SystemPrivilegeModel,
25
+ where: { SystemCode },
26
+ },
27
+ ],
17
28
  });
29
+ data.destroy({ transaction: dbTransaction });
30
+ return data;
18
31
  } catch (error) {
19
32
  throw error;
20
33
  }