@tomei/sso 0.61.0 → 0.61.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (134) hide show
  1. package/dist/__tests__/unit/components/group-privilege/group-privilege.test.d.ts +1 -0
  2. package/dist/__tests__/unit/components/group-privilege/group-privilege.test.js +71 -0
  3. package/dist/__tests__/unit/components/group-privilege/group-privilege.test.js.map +1 -0
  4. package/dist/__tests__/unit/components/login-user/login-user.spec.d.ts +0 -0
  5. package/dist/__tests__/unit/components/login-user/login-user.spec.js +6 -0
  6. package/dist/__tests__/unit/components/login-user/login-user.spec.js.map +1 -0
  7. package/dist/src/components/login-history/login-history.d.ts +23 -0
  8. package/dist/src/components/login-history/login-history.js +88 -0
  9. package/dist/src/components/login-history/login-history.js.map +1 -0
  10. package/dist/src/components/login-user/user.js +3 -2
  11. package/dist/src/components/login-user/user.js.map +1 -1
  12. package/dist/src/interfaces/login-history-search-attr.interface.d.ts +8 -0
  13. package/dist/src/interfaces/login-history-search-attr.interface.js +3 -0
  14. package/dist/src/interfaces/login-history-search-attr.interface.js.map +1 -0
  15. package/dist/src/interfaces/login-history.interface.d.ts +11 -0
  16. package/dist/src/interfaces/login-history.interface.js +3 -0
  17. package/dist/src/interfaces/login-history.interface.js.map +1 -0
  18. package/dist/tsconfig.tsbuildinfo +1 -1
  19. package/package.json +1 -1
  20. package/src/components/api-key/api-key.repository.ts +15 -15
  21. package/src/components/api-key/api-key.ts +448 -448
  22. package/src/components/api-key/index.ts +4 -4
  23. package/src/components/building/building.repository.ts +27 -27
  24. package/src/components/building/index.ts +2 -2
  25. package/src/components/group/group.repository.ts +26 -26
  26. package/src/components/group/group.ts +2284 -2284
  27. package/src/components/group/index.ts +3 -3
  28. package/src/components/group-object-privilege/group-object-privilege.repository.ts +25 -25
  29. package/src/components/group-object-privilege/group-object-privilege.ts +278 -278
  30. package/src/components/group-object-privilege/index.ts +2 -2
  31. package/src/components/group-privilege/group-privilege.repository.ts +29 -29
  32. package/src/components/group-privilege/group-privilege.ts +84 -84
  33. package/src/components/group-privilege/index.ts +2 -2
  34. package/src/components/group-reporting-user/group-reporting-user.repository.ts +23 -23
  35. package/src/components/group-reporting-user/group-reporting-user.ts +506 -506
  36. package/src/components/group-reporting-user/index.ts +3 -3
  37. package/src/components/group-system-access/group-system-access.repository.ts +43 -43
  38. package/src/components/group-system-access/group-system-access.ts +90 -90
  39. package/src/components/group-system-access/index.ts +2 -2
  40. package/src/components/index.ts +20 -20
  41. package/src/components/login-user/index.ts +5 -5
  42. package/src/components/login-user/interfaces/check-user-info-duplicated.interface.ts +7 -7
  43. package/src/components/login-user/interfaces/index.ts +1 -1
  44. package/src/components/login-user/interfaces/system-access.interface.ts +13 -13
  45. package/src/components/login-user/interfaces/user-info.interface.ts +34 -34
  46. package/src/components/login-user/login-user.ts +362 -362
  47. package/src/components/login-user/user.repository.ts +11 -11
  48. package/src/components/login-user/user.ts +3 -2
  49. package/src/components/password-hash/index.ts +2 -2
  50. package/src/components/password-hash/interfaces/index.ts +1 -1
  51. package/src/components/password-hash/interfaces/password-hash-service.interface.ts +4 -4
  52. package/src/components/password-hash/password-hash.service.ts +14 -14
  53. package/src/components/staff/index.ts +2 -2
  54. package/src/components/staff/staff.repository.ts +27 -27
  55. package/src/components/system/index.ts +3 -3
  56. package/src/components/system/system.repository.ts +11 -11
  57. package/src/components/system/system.ts +456 -456
  58. package/src/components/system-privilege/index.ts +4 -4
  59. package/src/components/system-privilege/system-privilege.repository.ts +18 -18
  60. package/src/components/system-privilege/system-privilege.ts +541 -541
  61. package/src/components/user-group/index.ts +2 -2
  62. package/src/components/user-group/user-group.repository.ts +19 -19
  63. package/src/components/user-group/user-group.ts +764 -764
  64. package/src/components/user-object-privilege/index.ts +2 -2
  65. package/src/components/user-object-privilege/user-object-privilege.repository.ts +11 -11
  66. package/src/components/user-object-privilege/user-object-privilege.ts +79 -79
  67. package/src/components/user-password-history/index.ts +2 -2
  68. package/src/components/user-password-history/user-password-history.repository.ts +39 -39
  69. package/src/components/user-password-history/user-password-history.ts +187 -187
  70. package/src/components/user-privilege/index.ts +2 -2
  71. package/src/components/user-privilege/user-privilege.repository.ts +25 -25
  72. package/src/components/user-privilege/user-privilege.ts +662 -662
  73. package/src/components/user-reporting-hierarchy/index.ts +2 -2
  74. package/src/components/user-reporting-hierarchy/user-reporting-hierarchy.repository.ts +30 -30
  75. package/src/components/user-reporting-hierarchy/user-reporting-hierarchy.ts +505 -505
  76. package/src/components/user-system-access/index.ts +2 -2
  77. package/src/components/user-system-access/user-system-access.repository.ts +41 -41
  78. package/src/database.ts +15 -15
  79. package/src/enum/api-key.enum.ts +5 -5
  80. package/src/enum/building-type.enum.ts +6 -6
  81. package/src/enum/group-type.enum.ts +8 -8
  82. package/src/enum/index.ts +6 -6
  83. package/src/enum/login-status.enum.ts +4 -4
  84. package/src/enum/object-status.enum.ts +4 -4
  85. package/src/enum/user-status.enum.ts +7 -7
  86. package/src/enum/yn.enum.ts +4 -4
  87. package/src/index.ts +8 -8
  88. package/src/interfaces/api-key-attr.interface.ts +16 -16
  89. package/src/interfaces/group-object-privilege.interface.ts +14 -14
  90. package/src/interfaces/group-privilege.interface.ts +10 -10
  91. package/src/interfaces/group-reporting-user.interface.ts +11 -11
  92. package/src/interfaces/group-search-attr.interface.ts +9 -9
  93. package/src/interfaces/group-system-access.interface.ts +10 -10
  94. package/src/interfaces/group.interface.ts +17 -17
  95. package/src/interfaces/index.ts +13 -13
  96. package/src/interfaces/system-login.interface.ts +6 -6
  97. package/src/interfaces/system-privilege-search.interface.ts +5 -5
  98. package/src/interfaces/system-privilege.interface.ts +11 -11
  99. package/src/interfaces/system-search-attr.interface.ts +5 -5
  100. package/src/interfaces/system.interface.ts +15 -15
  101. package/src/interfaces/user-group.interface.ts +12 -12
  102. package/src/interfaces/user-object-privilege.interface.ts +14 -14
  103. package/src/interfaces/user-password-history.interface.ts +6 -6
  104. package/src/interfaces/user-privilege.interface.ts +10 -10
  105. package/src/interfaces/user-reporting-hierarchy.interface.ts +11 -11
  106. package/src/interfaces/user-session.interface.ts +5 -5
  107. package/src/interfaces/user-system-access.interface.ts +10 -10
  108. package/src/models/api-key-entity.ts +101 -101
  109. package/src/models/building.entity.ts +103 -103
  110. package/src/models/group-object-privilege.entity.ts +91 -91
  111. package/src/models/group-privilege.entity.ts +78 -78
  112. package/src/models/group-reporting-user.entity.ts +95 -95
  113. package/src/models/group-system-access.entity.ts +81 -81
  114. package/src/models/group.entity.ts +127 -127
  115. package/src/models/staff.entity.ts +91 -91
  116. package/src/models/system-privilege.entity.ts +90 -90
  117. package/src/models/system.entity.ts +113 -113
  118. package/src/models/user-group.entity.ts +91 -91
  119. package/src/models/user-object-privilege.entity.ts +90 -90
  120. package/src/models/user-password-history.ts +51 -51
  121. package/src/models/user-privilege.entity.ts +78 -78
  122. package/src/models/user-reporting-hierarchy.entity.ts +102 -102
  123. package/src/models/user-system-access.entity.ts +87 -87
  124. package/src/models/user.entity.ts +193 -193
  125. package/src/redis-client/__mocks__/jest-initial-setup.ts +2 -2
  126. package/src/redis-client/__mocks__/redis-mock.ts +28 -28
  127. package/src/redis-client/index.ts +1 -1
  128. package/src/redis-client/redis.service.ts +75 -75
  129. package/src/session/index.ts +2 -2
  130. package/src/session/interfaces/index.ts +1 -1
  131. package/src/session/interfaces/session-service.interface.ts +26 -26
  132. package/src/session/session.service.ts +96 -96
  133. package/src/types/auth-context.ts +10 -10
  134. package/src/types/index.ts +1 -1
@@ -1,541 +1,541 @@
1
- import { ClassError, ObjectBase } from '@tomei/general';
2
- import { SystemRepository } from '../system/system.repository';
3
- import { SystemPrivilegeRepository } from './system-privilege.repository';
4
- import { ISystemPrivilegeAttr } from '../../interfaces/system-privilege.interface';
5
- import { LoginUser } from '../login-user/login-user';
6
- import { ApplicationConfig, ComponentConfig } from '@tomei/config';
7
- import { System } from '../system/system';
8
- import { ActionEnum, Activity } from '@tomei/activity-history';
9
- import { ISystemPrivilegeSearch } from '../../interfaces/system-privilege-search.interface';
10
- import { Op } from 'sequelize';
11
-
12
- export class SystemPrivilege extends ObjectBase {
13
- ObjectType = 'SystemPrivilege';
14
- TableName = 'sso_SystemPrivilege';
15
- ObjectId: string;
16
- PrivilegeCode: string;
17
- SystemCode: string;
18
- ObjectName: string;
19
- Description: string;
20
- Status: string;
21
- private _CreatedAt: Date;
22
- private _UpdatedAt: Date;
23
- private _CreatedById: number;
24
- private _UpdatedById: number;
25
-
26
- get SystemPrivilegeId() {
27
- return this.ObjectId;
28
- }
29
-
30
- set SystemPrivilegeId(value: string) {
31
- this.ObjectId = value;
32
- }
33
-
34
- get CreatedAt() {
35
- return this._CreatedAt;
36
- }
37
-
38
- get UpdatedAt() {
39
- return this._UpdatedAt;
40
- }
41
-
42
- get CreatedById() {
43
- return this._CreatedById;
44
- }
45
-
46
- get UpdatedById() {
47
- return this._UpdatedById;
48
- }
49
-
50
- private static _Repository = new SystemPrivilegeRepository();
51
- private static _SystemRepository = new SystemRepository();
52
-
53
- private constructor(systemPrivilegeAttr?: ISystemPrivilegeAttr) {
54
- super();
55
- if (systemPrivilegeAttr) {
56
- this.SystemPrivilegeId = systemPrivilegeAttr.SystemPrivilegeId;
57
- this.PrivilegeCode = systemPrivilegeAttr.PrivilegeCode;
58
- this.SystemCode = systemPrivilegeAttr.SystemCode;
59
- this.Description = systemPrivilegeAttr.Description;
60
- this.Status = systemPrivilegeAttr.Status;
61
- this._CreatedById = systemPrivilegeAttr.CreatedById;
62
- this._CreatedAt = systemPrivilegeAttr.CreatedAt;
63
- this._UpdatedById = systemPrivilegeAttr.UpdatedById;
64
- this._UpdatedAt = systemPrivilegeAttr.UpdatedAt;
65
- }
66
- }
67
-
68
- setAttributes(systemPrivilegeAttr: ISystemPrivilegeAttr) {
69
- this.PrivilegeCode = systemPrivilegeAttr.PrivilegeCode;
70
- this.SystemPrivilegeId = systemPrivilegeAttr.SystemPrivilegeId;
71
- this.SystemCode = systemPrivilegeAttr.SystemCode;
72
- this.Description = systemPrivilegeAttr.Description;
73
- this.Status = systemPrivilegeAttr.Status;
74
- this._CreatedAt = systemPrivilegeAttr.CreatedAt;
75
- this._UpdatedAt = systemPrivilegeAttr.UpdatedAt;
76
- this._CreatedById = systemPrivilegeAttr.CreatedById;
77
- this._UpdatedById = systemPrivilegeAttr.UpdatedById;
78
- }
79
-
80
- static async init(dbTransaction: any, SystemPrivilegeId?: string) {
81
- try {
82
- const systemPrivilege = new SystemPrivilege();
83
- if (SystemPrivilegeId) {
84
- const systemPrivilegeAttr = await this._Repository.findByPk(
85
- SystemPrivilegeId,
86
- {
87
- transaction: dbTransaction,
88
- },
89
- );
90
- if (systemPrivilegeAttr) {
91
- systemPrivilege.PrivilegeCode = systemPrivilegeAttr.PrivilegeCode;
92
- systemPrivilege.ObjectId = systemPrivilegeAttr.SystemPrivilegeId;
93
- systemPrivilege.SystemCode = systemPrivilegeAttr.SystemCode;
94
- systemPrivilege.Description = systemPrivilegeAttr.Description;
95
- systemPrivilege.Status = systemPrivilegeAttr.Status;
96
- systemPrivilege._CreatedById = systemPrivilegeAttr.CreatedById;
97
- systemPrivilege._CreatedAt = systemPrivilegeAttr.CreatedAt;
98
- systemPrivilege._UpdatedById = systemPrivilegeAttr.UpdatedById;
99
- systemPrivilege._UpdatedAt = systemPrivilegeAttr.UpdatedAt;
100
- } else {
101
- throw new ClassError(
102
- 'SystemPrivilege',
103
- 'SystemPrivilegeErrMsg00',
104
- 'System Privilege Not Found',
105
- );
106
- }
107
- }
108
- return systemPrivilege;
109
- } catch (error) {
110
- throw error;
111
- }
112
- }
113
-
114
- static async create(
115
- loginUser: LoginUser,
116
- dbTransaction: any,
117
- systemPrivilege: SystemPrivilege,
118
- ) {
119
- try {
120
- //Part 1: Privilege Checking
121
- const systemCode: string =
122
- ApplicationConfig.getComponentConfigValue('system-code');
123
- const isPrivileged = await loginUser.checkPrivileges(
124
- systemCode,
125
- 'PRIVILEGE_CREATE',
126
- );
127
- if (!isPrivileged) {
128
- throw new ClassError(
129
- 'SystemPrivilege',
130
- 'SystemPrivilegeErrMsg01',
131
- 'You do not have permission to create system privileges',
132
- );
133
- }
134
-
135
- //Part 2: Validation
136
- //Make sure systemCode and PrivilegeCode are not empty
137
- if (!systemPrivilege.SystemCode) {
138
- throw new ClassError(
139
- 'SystemPrivilege',
140
- 'SystemPrivilegeErrMsg02',
141
- 'System Code is required',
142
- );
143
- }
144
-
145
- if (!systemPrivilege.PrivilegeCode) {
146
- throw new ClassError(
147
- 'SystemPrivilege',
148
- 'SystemPrivilegeErrMsg02',
149
- 'Privilege Code is required',
150
- );
151
- }
152
-
153
- //Call System.init() method by passing systemCode
154
- await System.init(dbTransaction, systemPrivilege.SystemCode);
155
-
156
- //Call SystemPrivilege._Repo findByPk
157
- const existingSystemPrivilege = await this._Repository.findByPk(
158
- systemPrivilege.PrivilegeCode,
159
- {
160
- transaction: dbTransaction,
161
- },
162
- );
163
-
164
- //If PrivilegeCode found, throw new ClassError
165
- if (existingSystemPrivilege) {
166
- throw new ClassError(
167
- 'SystemPrivilege',
168
- 'SystemPrivilegeErrMsg03',
169
- 'System Privilege already exists',
170
- );
171
- }
172
-
173
- //Part 3: Create Privilege
174
- //Initialise new SystemPrivilege instance and populate below
175
- const newSystemPrivilege = new SystemPrivilege();
176
- newSystemPrivilege.ObjectId = newSystemPrivilege.createId();
177
- newSystemPrivilege.PrivilegeCode = systemPrivilege.PrivilegeCode;
178
- newSystemPrivilege.SystemCode = systemPrivilege.SystemCode;
179
- newSystemPrivilege.Description = systemPrivilege.Description;
180
- newSystemPrivilege.Status = 'Active';
181
- newSystemPrivilege._CreatedById = loginUser.UserId;
182
- newSystemPrivilege._UpdatedById = loginUser.UserId;
183
- newSystemPrivilege._CreatedAt = new Date();
184
- newSystemPrivilege._UpdatedAt = new Date();
185
-
186
- //Call SystemPrivilege._Repo create method
187
- await this._Repository.create(
188
- {
189
- SystemPrivilegeId: newSystemPrivilege.ObjectId,
190
- PrivilegeCode: newSystemPrivilege.PrivilegeCode,
191
- SystemCode: newSystemPrivilege.SystemCode,
192
- Description: newSystemPrivilege.Description,
193
- Status: newSystemPrivilege.Status,
194
- CreatedById: newSystemPrivilege._CreatedById,
195
- UpdatedById: newSystemPrivilege._UpdatedById,
196
- CreatedAt: newSystemPrivilege._CreatedAt,
197
- UpdatedAt: newSystemPrivilege._UpdatedAt,
198
- },
199
- {
200
- transaction: dbTransaction,
201
- },
202
- );
203
-
204
- //Part 4: Record Create Privilege Activity
205
- //Initialise EntityValueBefore variable and set to empty object.
206
- const EntityValueBefore = {};
207
- //Initialise EntityValueAfter variable and set to newSystemPrivilege object.
208
- const EntityValueAfter = newSystemPrivilege;
209
-
210
- //Instantiate new activity object and populate
211
- const activity = new Activity();
212
- activity.ActivityId = activity.createId();
213
- activity.Action = ActionEnum.CREATE;
214
- activity.Description = 'Add System Privilege';
215
- activity.EntityType = 'SystemPrivilege';
216
- activity.EntityId = newSystemPrivilege.SystemPrivilegeId;
217
- activity.EntityValueBefore = JSON.stringify(EntityValueBefore);
218
- activity.EntityValueAfter = JSON.stringify(EntityValueAfter);
219
-
220
- //Call Activity.create method
221
- await activity.create(loginUser.ObjectId, dbTransaction);
222
-
223
- return newSystemPrivilege;
224
- } catch (error) {
225
- throw error;
226
- }
227
- }
228
-
229
- public static async findAll(
230
- loginUser: LoginUser,
231
- dbTransaction: any,
232
- page?: number,
233
- row?: number,
234
- search?: ISystemPrivilegeSearch,
235
- ) {
236
- try {
237
- // Part 1: Privilege Checking
238
- const systemCode =
239
- ApplicationConfig.getComponentConfigValue('system-code');
240
- const isPrivileged = await loginUser.checkPrivileges(
241
- systemCode,
242
- 'PRIVILEGE_LIST',
243
- );
244
-
245
- if (!isPrivileged) {
246
- throw new ClassError(
247
- 'SystemPrivilege',
248
- 'SystemPrivilegeErrMsg01',
249
- 'You do not have permission to list system privileges',
250
- );
251
- }
252
-
253
- // Part 2: Retrieve listing
254
- const options: any = {
255
- distinct: true,
256
- order: [['createdAt', 'DESC']],
257
- transaction: dbTransaction,
258
- };
259
- //If page and row exist, please add limit and offset to the findAll options
260
- if (page && row) {
261
- const offset = row * (page - 1);
262
- const limit = row;
263
-
264
- options.offset = offset;
265
- options.limit = limit;
266
- }
267
-
268
- //If search query exists, please set it to sequelize where option.
269
- if (search) {
270
- const queryObj: any = {};
271
- Object.entries(search).forEach(([key, value]) => {
272
- queryObj[key] = {
273
- [Op.substring]: value,
274
- };
275
- });
276
-
277
- options.where = queryObj;
278
- }
279
-
280
- const result =
281
- await SystemPrivilege._Repository.findAllWithPagination(options);
282
-
283
- // Part 3: Return result
284
- // Map the result to SystemPrivilege object
285
- const systemPrivileges: SystemPrivilege[] = [];
286
- for (const systemPrivilegeAttr of result.rows) {
287
- systemPrivileges.push(
288
- new SystemPrivilege(systemPrivilegeAttr.get({ plain: true })),
289
- );
290
- }
291
-
292
- return {
293
- count: result.count,
294
- SystemPrivileges: systemPrivileges,
295
- };
296
- } catch (error) {
297
- throw error;
298
- }
299
- }
300
-
301
- public static async loadAllPrivileges(
302
- dbTransaction: any,
303
- systemCode: string,
304
- ): Promise<string> {
305
- try {
306
- //Instantiate existing System by passing:
307
- // dbTransaction
308
- // SystemCode: Params.SystemCode
309
- await System.init(dbTransaction, systemCode);
310
-
311
- // Part 2: Load Privileges
312
- // Load sso component config.loadComponentConfig Call Config. by passing:
313
- // filepath: '/component-config/sso-config.json'
314
-
315
- ComponentConfig.loadComponentConfig('./component-config/sso-config.json');
316
-
317
- // Retrieve privileges array by call Config.getComponentConfigValue by passing:
318
- // componentName: '@tomei/sso'
319
- // configKey: 'privileges'
320
- const privilegesConfig: {
321
- privilegeCode: string;
322
- description: string;
323
- }[] = ComponentConfig.getComponentConfigValue('@tomei/sso', 'privileges');
324
- // Retrieve system user id. Call Config.getComponentConfigValue by passing:
325
- // componentName: '@tomei/sso'
326
- // configKey: 'system'
327
- const systemConfig: {
328
- name: string;
329
- code: string;
330
- description: string;
331
- userId: string;
332
- } = ComponentConfig.getComponentConfigValue('@tomei/sso', 'system');
333
-
334
- //Set systemUserId to system.userId.
335
- const systemUserId = systemConfig.userId;
336
- //Retrieve existing SystemPrivilege. Call SystemPrivilege._Repo findAll method by passing:
337
- // where:
338
- // SystemCode: Params.SystemCode
339
- const existingSystemPrivileges = await this._Repository.findAll({
340
- where: {
341
- SystemCode: systemCode,
342
- },
343
- transaction: dbTransaction,
344
- });
345
-
346
- //Filter out existing privileges with the privileges array above to identify which privileges to be created and map it tobeCreatePrivileges.
347
- const tobeCreatePrivileges = privilegesConfig.filter(
348
- (privilegeConfig) =>
349
- !existingSystemPrivileges.find(
350
- (existingPrivilege) =>
351
- existingPrivilege.PrivilegeCode === privilegeConfig.privilegeCode,
352
- ),
353
- );
354
-
355
- //Call SystemPrivilege._Repo create method for each newPrivileges.
356
- const np = new SystemPrivilege();
357
- for (const privilegeConfig of tobeCreatePrivileges) {
358
- await this._Repository.create(
359
- {
360
- SystemPrivilegeId: np.createId(),
361
- PrivilegeCode: privilegeConfig.privilegeCode,
362
- SystemCode: systemCode,
363
- Description: privilegeConfig.description,
364
- Status: 'Active',
365
- CreatedById: parseInt(systemUserId),
366
- UpdatedById: parseInt(systemUserId),
367
- CreatedAt: new Date(),
368
- UpdatedAt: new Date(),
369
- },
370
- {
371
- transaction: dbTransaction,
372
- },
373
- );
374
- }
375
-
376
- return 'Privileges Loaded';
377
- } catch (error) {
378
- throw error;
379
- }
380
- }
381
-
382
- public async update(
383
- loginUser: LoginUser, //The user object representing the currently logged-in user.
384
- dbTransaction: any, //The database transaction instance for managing the transaction scope.
385
- privilege: {
386
- PrivilegeCode?: string;
387
- Description?: string;
388
- SystemCode?: string; //The System Code for System Privilege
389
- Status?: string; //The new privilege status (Active/Inactive) for the system privilege
390
- },
391
- ) {
392
- try {
393
- // Part 1: Update System Privilege
394
- // Call the SystemPrivilege._Repo.update() method to perform the update operation, passing:
395
- // - SystemCode: The new SystemCode.
396
- // - Status: The new status.
397
- // - UpdatedById: loginUser.UserId (to indicate who updated the record).
398
- // - UpdatedAt: Set to the current date and time.
399
- // - dbTransaction: The database transaction instance.
400
- const entityValueBefore = {
401
- SystemPrivilegeId: this.SystemPrivilegeId,
402
- Description: this.Description,
403
- PrivilegeCode: this.PrivilegeCode,
404
- SystemCode: this.SystemCode,
405
- Status: this.Status,
406
- CreatedById: this.CreatedById,
407
- CreatedAt: this.CreatedAt,
408
- UpdatedById: this.UpdatedById,
409
- UpdatedAt: this.UpdatedAt,
410
- };
411
-
412
- await SystemPrivilege._Repository.update(
413
- {
414
- PrivilegeCode: privilege.PrivilegeCode || this.PrivilegeCode,
415
- Description: privilege.Description || this.Description,
416
- SystemCode: privilege.SystemCode || this.SystemCode,
417
- Status: privilege.Status || this.Status,
418
- UpdatedById: loginUser.UserId,
419
- UpdatedAt: new Date(),
420
- },
421
- {
422
- where: {
423
- SystemPrivilegeId: this.SystemPrivilegeId,
424
- },
425
- transaction: dbTransaction,
426
- },
427
- );
428
-
429
- const entityValueAfter = {
430
- SystemPrivilegeId: this.SystemPrivilegeId,
431
- PrivilegeCode: privilege.PrivilegeCode || this.PrivilegeCode,
432
- Description: privilege.Description || this.Description,
433
- SystemCode: privilege.SystemCode || this.SystemCode,
434
- Status: privilege.Status || this.Status,
435
- CreatedById: this.CreatedById,
436
- CreatedAt: this.CreatedAt,
437
- UpdatedById: this.UpdatedById,
438
- UpdatedAt: this.UpdatedAt,
439
- };
440
-
441
- // Part 2: Record Activity History
442
- // Initialize a variable entityValueBefore to store the current state of the user privilege record before the update.
443
- // Create an instance of the Activity class and set the following properties:
444
- // - ActivityId: Call activity.createId().
445
- // - Action: Set to ActionEnum.Update.
446
- // - Description: Set to Update System Privilege.
447
- // - EntityType: Set to SystemPrivilege.
448
- // - EntityId: Use the ID of the updated user privilege record.
449
- // - EntityValueBefore: Stringify entityValueBefore to capture the state before the update.
450
- // - EntityValueAfter: Stringify the updated user privilege record to capture the new state after the update.
451
- // Call the activity create method with the following parameters:
452
- // - dbTransaction
453
- // - userId: loginUser.UserId
454
- const activity = new Activity();
455
- activity.ActivityId = activity.createId();
456
- activity.Action = ActionEnum.UPDATE;
457
- activity.Description = 'Update System Privilege';
458
- activity.EntityType = 'SystemPrivilege';
459
- activity.EntityId = this.SystemPrivilegeId + '';
460
- activity.EntityValueBefore = JSON.stringify(entityValueBefore);
461
- activity.EntityValueAfter = JSON.stringify(entityValueAfter);
462
- await activity.create(loginUser.ObjectId, dbTransaction);
463
-
464
- // Part 3: Return Updated Record
465
- // Retrieve the updated user system access record from the database or return the updated instance as needed.
466
- return entityValueAfter;
467
- } catch (error) {
468
- throw error;
469
- }
470
- }
471
-
472
- public async delete(dbTransaction: any, loginUser: LoginUser) {
473
- try {
474
- //Part 1: Privilege Checking
475
- const systemCode: string =
476
- ApplicationConfig.getComponentConfigValue('system-code');
477
- const isPrivileged = await loginUser.checkPrivileges(
478
- systemCode,
479
- 'PRIVILEGE_DELETE',
480
- );
481
- if (!isPrivileged) {
482
- throw new ClassError(
483
- 'SystemPrivilege',
484
- 'SystemPrivilegeErrMsg0X',
485
- 'You do not have permission to delete system privileges',
486
- );
487
- }
488
-
489
- //Part 2: Validation
490
- //Make sure SystemPrivilegeId is not empty
491
- if (!this.SystemPrivilegeId) {
492
- throw new ClassError(
493
- 'SystemPrivilege',
494
- 'SystemPrivilegeErrMsg02',
495
- 'System Privilege Id is required',
496
- );
497
- }
498
-
499
- //Part 3: Delete Privilege
500
-
501
- //Call SystemPrivilege._Repo delete method
502
- await SystemPrivilege._Repository.delete(
503
- this.SystemPrivilegeId,
504
- dbTransaction,
505
- );
506
-
507
- //Part 4: Record Create Privilege Activity
508
- //Initialise EntityValueBefore variable and set to empty object.
509
- const EntityValueBefore = {
510
- SystemPrivilegeId: this.ObjectId,
511
- PrivilegeCode: this.PrivilegeCode,
512
- SystemCode: this.SystemCode,
513
- Description: this.Description,
514
- Status: this.Status,
515
- CreatedById: this._CreatedById,
516
- UpdatedById: this._UpdatedById,
517
- CreatedAt: this._CreatedAt,
518
- UpdatedAt: this._UpdatedAt,
519
- };
520
- //Initialise EntityValueAfter variable and set to newSystemPrivilege object.
521
- const EntityValueAfter = {};
522
-
523
- //Instantiate new activity object and populate
524
- const activity = new Activity();
525
- activity.ActivityId = activity.createId();
526
- activity.Action = ActionEnum.DELETE;
527
- activity.Description = 'Delete System Privilege';
528
- activity.EntityType = 'SystemPrivilege';
529
- activity.EntityId = this.SystemPrivilegeId;
530
- activity.EntityValueBefore = JSON.stringify(EntityValueBefore);
531
- activity.EntityValueAfter = JSON.stringify(EntityValueAfter);
532
-
533
- //Call Activity.create method
534
- await activity.create(loginUser.ObjectId, dbTransaction);
535
-
536
- return this;
537
- } catch (error) {
538
- throw error;
539
- }
540
- }
541
- }
1
+ import { ClassError, ObjectBase } from '@tomei/general';
2
+ import { SystemRepository } from '../system/system.repository';
3
+ import { SystemPrivilegeRepository } from './system-privilege.repository';
4
+ import { ISystemPrivilegeAttr } from '../../interfaces/system-privilege.interface';
5
+ import { LoginUser } from '../login-user/login-user';
6
+ import { ApplicationConfig, ComponentConfig } from '@tomei/config';
7
+ import { System } from '../system/system';
8
+ import { ActionEnum, Activity } from '@tomei/activity-history';
9
+ import { ISystemPrivilegeSearch } from '../../interfaces/system-privilege-search.interface';
10
+ import { Op } from 'sequelize';
11
+
12
+ export class SystemPrivilege extends ObjectBase {
13
+ ObjectType = 'SystemPrivilege';
14
+ TableName = 'sso_SystemPrivilege';
15
+ ObjectId: string;
16
+ PrivilegeCode: string;
17
+ SystemCode: string;
18
+ ObjectName: string;
19
+ Description: string;
20
+ Status: string;
21
+ private _CreatedAt: Date;
22
+ private _UpdatedAt: Date;
23
+ private _CreatedById: number;
24
+ private _UpdatedById: number;
25
+
26
+ get SystemPrivilegeId() {
27
+ return this.ObjectId;
28
+ }
29
+
30
+ set SystemPrivilegeId(value: string) {
31
+ this.ObjectId = value;
32
+ }
33
+
34
+ get CreatedAt() {
35
+ return this._CreatedAt;
36
+ }
37
+
38
+ get UpdatedAt() {
39
+ return this._UpdatedAt;
40
+ }
41
+
42
+ get CreatedById() {
43
+ return this._CreatedById;
44
+ }
45
+
46
+ get UpdatedById() {
47
+ return this._UpdatedById;
48
+ }
49
+
50
+ private static _Repository = new SystemPrivilegeRepository();
51
+ private static _SystemRepository = new SystemRepository();
52
+
53
+ private constructor(systemPrivilegeAttr?: ISystemPrivilegeAttr) {
54
+ super();
55
+ if (systemPrivilegeAttr) {
56
+ this.SystemPrivilegeId = systemPrivilegeAttr.SystemPrivilegeId;
57
+ this.PrivilegeCode = systemPrivilegeAttr.PrivilegeCode;
58
+ this.SystemCode = systemPrivilegeAttr.SystemCode;
59
+ this.Description = systemPrivilegeAttr.Description;
60
+ this.Status = systemPrivilegeAttr.Status;
61
+ this._CreatedById = systemPrivilegeAttr.CreatedById;
62
+ this._CreatedAt = systemPrivilegeAttr.CreatedAt;
63
+ this._UpdatedById = systemPrivilegeAttr.UpdatedById;
64
+ this._UpdatedAt = systemPrivilegeAttr.UpdatedAt;
65
+ }
66
+ }
67
+
68
+ setAttributes(systemPrivilegeAttr: ISystemPrivilegeAttr) {
69
+ this.PrivilegeCode = systemPrivilegeAttr.PrivilegeCode;
70
+ this.SystemPrivilegeId = systemPrivilegeAttr.SystemPrivilegeId;
71
+ this.SystemCode = systemPrivilegeAttr.SystemCode;
72
+ this.Description = systemPrivilegeAttr.Description;
73
+ this.Status = systemPrivilegeAttr.Status;
74
+ this._CreatedAt = systemPrivilegeAttr.CreatedAt;
75
+ this._UpdatedAt = systemPrivilegeAttr.UpdatedAt;
76
+ this._CreatedById = systemPrivilegeAttr.CreatedById;
77
+ this._UpdatedById = systemPrivilegeAttr.UpdatedById;
78
+ }
79
+
80
+ static async init(dbTransaction: any, SystemPrivilegeId?: string) {
81
+ try {
82
+ const systemPrivilege = new SystemPrivilege();
83
+ if (SystemPrivilegeId) {
84
+ const systemPrivilegeAttr = await this._Repository.findByPk(
85
+ SystemPrivilegeId,
86
+ {
87
+ transaction: dbTransaction,
88
+ },
89
+ );
90
+ if (systemPrivilegeAttr) {
91
+ systemPrivilege.PrivilegeCode = systemPrivilegeAttr.PrivilegeCode;
92
+ systemPrivilege.ObjectId = systemPrivilegeAttr.SystemPrivilegeId;
93
+ systemPrivilege.SystemCode = systemPrivilegeAttr.SystemCode;
94
+ systemPrivilege.Description = systemPrivilegeAttr.Description;
95
+ systemPrivilege.Status = systemPrivilegeAttr.Status;
96
+ systemPrivilege._CreatedById = systemPrivilegeAttr.CreatedById;
97
+ systemPrivilege._CreatedAt = systemPrivilegeAttr.CreatedAt;
98
+ systemPrivilege._UpdatedById = systemPrivilegeAttr.UpdatedById;
99
+ systemPrivilege._UpdatedAt = systemPrivilegeAttr.UpdatedAt;
100
+ } else {
101
+ throw new ClassError(
102
+ 'SystemPrivilege',
103
+ 'SystemPrivilegeErrMsg00',
104
+ 'System Privilege Not Found',
105
+ );
106
+ }
107
+ }
108
+ return systemPrivilege;
109
+ } catch (error) {
110
+ throw error;
111
+ }
112
+ }
113
+
114
+ static async create(
115
+ loginUser: LoginUser,
116
+ dbTransaction: any,
117
+ systemPrivilege: SystemPrivilege,
118
+ ) {
119
+ try {
120
+ //Part 1: Privilege Checking
121
+ const systemCode: string =
122
+ ApplicationConfig.getComponentConfigValue('system-code');
123
+ const isPrivileged = await loginUser.checkPrivileges(
124
+ systemCode,
125
+ 'PRIVILEGE_CREATE',
126
+ );
127
+ if (!isPrivileged) {
128
+ throw new ClassError(
129
+ 'SystemPrivilege',
130
+ 'SystemPrivilegeErrMsg01',
131
+ 'You do not have permission to create system privileges',
132
+ );
133
+ }
134
+
135
+ //Part 2: Validation
136
+ //Make sure systemCode and PrivilegeCode are not empty
137
+ if (!systemPrivilege.SystemCode) {
138
+ throw new ClassError(
139
+ 'SystemPrivilege',
140
+ 'SystemPrivilegeErrMsg02',
141
+ 'System Code is required',
142
+ );
143
+ }
144
+
145
+ if (!systemPrivilege.PrivilegeCode) {
146
+ throw new ClassError(
147
+ 'SystemPrivilege',
148
+ 'SystemPrivilegeErrMsg02',
149
+ 'Privilege Code is required',
150
+ );
151
+ }
152
+
153
+ //Call System.init() method by passing systemCode
154
+ await System.init(dbTransaction, systemPrivilege.SystemCode);
155
+
156
+ //Call SystemPrivilege._Repo findByPk
157
+ const existingSystemPrivilege = await this._Repository.findByPk(
158
+ systemPrivilege.PrivilegeCode,
159
+ {
160
+ transaction: dbTransaction,
161
+ },
162
+ );
163
+
164
+ //If PrivilegeCode found, throw new ClassError
165
+ if (existingSystemPrivilege) {
166
+ throw new ClassError(
167
+ 'SystemPrivilege',
168
+ 'SystemPrivilegeErrMsg03',
169
+ 'System Privilege already exists',
170
+ );
171
+ }
172
+
173
+ //Part 3: Create Privilege
174
+ //Initialise new SystemPrivilege instance and populate below
175
+ const newSystemPrivilege = new SystemPrivilege();
176
+ newSystemPrivilege.ObjectId = newSystemPrivilege.createId();
177
+ newSystemPrivilege.PrivilegeCode = systemPrivilege.PrivilegeCode;
178
+ newSystemPrivilege.SystemCode = systemPrivilege.SystemCode;
179
+ newSystemPrivilege.Description = systemPrivilege.Description;
180
+ newSystemPrivilege.Status = 'Active';
181
+ newSystemPrivilege._CreatedById = loginUser.UserId;
182
+ newSystemPrivilege._UpdatedById = loginUser.UserId;
183
+ newSystemPrivilege._CreatedAt = new Date();
184
+ newSystemPrivilege._UpdatedAt = new Date();
185
+
186
+ //Call SystemPrivilege._Repo create method
187
+ await this._Repository.create(
188
+ {
189
+ SystemPrivilegeId: newSystemPrivilege.ObjectId,
190
+ PrivilegeCode: newSystemPrivilege.PrivilegeCode,
191
+ SystemCode: newSystemPrivilege.SystemCode,
192
+ Description: newSystemPrivilege.Description,
193
+ Status: newSystemPrivilege.Status,
194
+ CreatedById: newSystemPrivilege._CreatedById,
195
+ UpdatedById: newSystemPrivilege._UpdatedById,
196
+ CreatedAt: newSystemPrivilege._CreatedAt,
197
+ UpdatedAt: newSystemPrivilege._UpdatedAt,
198
+ },
199
+ {
200
+ transaction: dbTransaction,
201
+ },
202
+ );
203
+
204
+ //Part 4: Record Create Privilege Activity
205
+ //Initialise EntityValueBefore variable and set to empty object.
206
+ const EntityValueBefore = {};
207
+ //Initialise EntityValueAfter variable and set to newSystemPrivilege object.
208
+ const EntityValueAfter = newSystemPrivilege;
209
+
210
+ //Instantiate new activity object and populate
211
+ const activity = new Activity();
212
+ activity.ActivityId = activity.createId();
213
+ activity.Action = ActionEnum.CREATE;
214
+ activity.Description = 'Add System Privilege';
215
+ activity.EntityType = 'SystemPrivilege';
216
+ activity.EntityId = newSystemPrivilege.SystemPrivilegeId;
217
+ activity.EntityValueBefore = JSON.stringify(EntityValueBefore);
218
+ activity.EntityValueAfter = JSON.stringify(EntityValueAfter);
219
+
220
+ //Call Activity.create method
221
+ await activity.create(loginUser.ObjectId, dbTransaction);
222
+
223
+ return newSystemPrivilege;
224
+ } catch (error) {
225
+ throw error;
226
+ }
227
+ }
228
+
229
+ public static async findAll(
230
+ loginUser: LoginUser,
231
+ dbTransaction: any,
232
+ page?: number,
233
+ row?: number,
234
+ search?: ISystemPrivilegeSearch,
235
+ ) {
236
+ try {
237
+ // Part 1: Privilege Checking
238
+ const systemCode =
239
+ ApplicationConfig.getComponentConfigValue('system-code');
240
+ const isPrivileged = await loginUser.checkPrivileges(
241
+ systemCode,
242
+ 'PRIVILEGE_LIST',
243
+ );
244
+
245
+ if (!isPrivileged) {
246
+ throw new ClassError(
247
+ 'SystemPrivilege',
248
+ 'SystemPrivilegeErrMsg01',
249
+ 'You do not have permission to list system privileges',
250
+ );
251
+ }
252
+
253
+ // Part 2: Retrieve listing
254
+ const options: any = {
255
+ distinct: true,
256
+ order: [['createdAt', 'DESC']],
257
+ transaction: dbTransaction,
258
+ };
259
+ //If page and row exist, please add limit and offset to the findAll options
260
+ if (page && row) {
261
+ const offset = row * (page - 1);
262
+ const limit = row;
263
+
264
+ options.offset = offset;
265
+ options.limit = limit;
266
+ }
267
+
268
+ //If search query exists, please set it to sequelize where option.
269
+ if (search) {
270
+ const queryObj: any = {};
271
+ Object.entries(search).forEach(([key, value]) => {
272
+ queryObj[key] = {
273
+ [Op.substring]: value,
274
+ };
275
+ });
276
+
277
+ options.where = queryObj;
278
+ }
279
+
280
+ const result =
281
+ await SystemPrivilege._Repository.findAllWithPagination(options);
282
+
283
+ // Part 3: Return result
284
+ // Map the result to SystemPrivilege object
285
+ const systemPrivileges: SystemPrivilege[] = [];
286
+ for (const systemPrivilegeAttr of result.rows) {
287
+ systemPrivileges.push(
288
+ new SystemPrivilege(systemPrivilegeAttr.get({ plain: true })),
289
+ );
290
+ }
291
+
292
+ return {
293
+ count: result.count,
294
+ SystemPrivileges: systemPrivileges,
295
+ };
296
+ } catch (error) {
297
+ throw error;
298
+ }
299
+ }
300
+
301
+ public static async loadAllPrivileges(
302
+ dbTransaction: any,
303
+ systemCode: string,
304
+ ): Promise<string> {
305
+ try {
306
+ //Instantiate existing System by passing:
307
+ // dbTransaction
308
+ // SystemCode: Params.SystemCode
309
+ await System.init(dbTransaction, systemCode);
310
+
311
+ // Part 2: Load Privileges
312
+ // Load sso component config.loadComponentConfig Call Config. by passing:
313
+ // filepath: '/component-config/sso-config.json'
314
+
315
+ ComponentConfig.loadComponentConfig('./component-config/sso-config.json');
316
+
317
+ // Retrieve privileges array by call Config.getComponentConfigValue by passing:
318
+ // componentName: '@tomei/sso'
319
+ // configKey: 'privileges'
320
+ const privilegesConfig: {
321
+ privilegeCode: string;
322
+ description: string;
323
+ }[] = ComponentConfig.getComponentConfigValue('@tomei/sso', 'privileges');
324
+ // Retrieve system user id. Call Config.getComponentConfigValue by passing:
325
+ // componentName: '@tomei/sso'
326
+ // configKey: 'system'
327
+ const systemConfig: {
328
+ name: string;
329
+ code: string;
330
+ description: string;
331
+ userId: string;
332
+ } = ComponentConfig.getComponentConfigValue('@tomei/sso', 'system');
333
+
334
+ //Set systemUserId to system.userId.
335
+ const systemUserId = systemConfig.userId;
336
+ //Retrieve existing SystemPrivilege. Call SystemPrivilege._Repo findAll method by passing:
337
+ // where:
338
+ // SystemCode: Params.SystemCode
339
+ const existingSystemPrivileges = await this._Repository.findAll({
340
+ where: {
341
+ SystemCode: systemCode,
342
+ },
343
+ transaction: dbTransaction,
344
+ });
345
+
346
+ //Filter out existing privileges with the privileges array above to identify which privileges to be created and map it tobeCreatePrivileges.
347
+ const tobeCreatePrivileges = privilegesConfig.filter(
348
+ (privilegeConfig) =>
349
+ !existingSystemPrivileges.find(
350
+ (existingPrivilege) =>
351
+ existingPrivilege.PrivilegeCode === privilegeConfig.privilegeCode,
352
+ ),
353
+ );
354
+
355
+ //Call SystemPrivilege._Repo create method for each newPrivileges.
356
+ const np = new SystemPrivilege();
357
+ for (const privilegeConfig of tobeCreatePrivileges) {
358
+ await this._Repository.create(
359
+ {
360
+ SystemPrivilegeId: np.createId(),
361
+ PrivilegeCode: privilegeConfig.privilegeCode,
362
+ SystemCode: systemCode,
363
+ Description: privilegeConfig.description,
364
+ Status: 'Active',
365
+ CreatedById: parseInt(systemUserId),
366
+ UpdatedById: parseInt(systemUserId),
367
+ CreatedAt: new Date(),
368
+ UpdatedAt: new Date(),
369
+ },
370
+ {
371
+ transaction: dbTransaction,
372
+ },
373
+ );
374
+ }
375
+
376
+ return 'Privileges Loaded';
377
+ } catch (error) {
378
+ throw error;
379
+ }
380
+ }
381
+
382
+ public async update(
383
+ loginUser: LoginUser, //The user object representing the currently logged-in user.
384
+ dbTransaction: any, //The database transaction instance for managing the transaction scope.
385
+ privilege: {
386
+ PrivilegeCode?: string;
387
+ Description?: string;
388
+ SystemCode?: string; //The System Code for System Privilege
389
+ Status?: string; //The new privilege status (Active/Inactive) for the system privilege
390
+ },
391
+ ) {
392
+ try {
393
+ // Part 1: Update System Privilege
394
+ // Call the SystemPrivilege._Repo.update() method to perform the update operation, passing:
395
+ // - SystemCode: The new SystemCode.
396
+ // - Status: The new status.
397
+ // - UpdatedById: loginUser.UserId (to indicate who updated the record).
398
+ // - UpdatedAt: Set to the current date and time.
399
+ // - dbTransaction: The database transaction instance.
400
+ const entityValueBefore = {
401
+ SystemPrivilegeId: this.SystemPrivilegeId,
402
+ Description: this.Description,
403
+ PrivilegeCode: this.PrivilegeCode,
404
+ SystemCode: this.SystemCode,
405
+ Status: this.Status,
406
+ CreatedById: this.CreatedById,
407
+ CreatedAt: this.CreatedAt,
408
+ UpdatedById: this.UpdatedById,
409
+ UpdatedAt: this.UpdatedAt,
410
+ };
411
+
412
+ await SystemPrivilege._Repository.update(
413
+ {
414
+ PrivilegeCode: privilege.PrivilegeCode || this.PrivilegeCode,
415
+ Description: privilege.Description || this.Description,
416
+ SystemCode: privilege.SystemCode || this.SystemCode,
417
+ Status: privilege.Status || this.Status,
418
+ UpdatedById: loginUser.UserId,
419
+ UpdatedAt: new Date(),
420
+ },
421
+ {
422
+ where: {
423
+ SystemPrivilegeId: this.SystemPrivilegeId,
424
+ },
425
+ transaction: dbTransaction,
426
+ },
427
+ );
428
+
429
+ const entityValueAfter = {
430
+ SystemPrivilegeId: this.SystemPrivilegeId,
431
+ PrivilegeCode: privilege.PrivilegeCode || this.PrivilegeCode,
432
+ Description: privilege.Description || this.Description,
433
+ SystemCode: privilege.SystemCode || this.SystemCode,
434
+ Status: privilege.Status || this.Status,
435
+ CreatedById: this.CreatedById,
436
+ CreatedAt: this.CreatedAt,
437
+ UpdatedById: this.UpdatedById,
438
+ UpdatedAt: this.UpdatedAt,
439
+ };
440
+
441
+ // Part 2: Record Activity History
442
+ // Initialize a variable entityValueBefore to store the current state of the user privilege record before the update.
443
+ // Create an instance of the Activity class and set the following properties:
444
+ // - ActivityId: Call activity.createId().
445
+ // - Action: Set to ActionEnum.Update.
446
+ // - Description: Set to Update System Privilege.
447
+ // - EntityType: Set to SystemPrivilege.
448
+ // - EntityId: Use the ID of the updated user privilege record.
449
+ // - EntityValueBefore: Stringify entityValueBefore to capture the state before the update.
450
+ // - EntityValueAfter: Stringify the updated user privilege record to capture the new state after the update.
451
+ // Call the activity create method with the following parameters:
452
+ // - dbTransaction
453
+ // - userId: loginUser.UserId
454
+ const activity = new Activity();
455
+ activity.ActivityId = activity.createId();
456
+ activity.Action = ActionEnum.UPDATE;
457
+ activity.Description = 'Update System Privilege';
458
+ activity.EntityType = 'SystemPrivilege';
459
+ activity.EntityId = this.SystemPrivilegeId + '';
460
+ activity.EntityValueBefore = JSON.stringify(entityValueBefore);
461
+ activity.EntityValueAfter = JSON.stringify(entityValueAfter);
462
+ await activity.create(loginUser.ObjectId, dbTransaction);
463
+
464
+ // Part 3: Return Updated Record
465
+ // Retrieve the updated user system access record from the database or return the updated instance as needed.
466
+ return entityValueAfter;
467
+ } catch (error) {
468
+ throw error;
469
+ }
470
+ }
471
+
472
+ public async delete(dbTransaction: any, loginUser: LoginUser) {
473
+ try {
474
+ //Part 1: Privilege Checking
475
+ const systemCode: string =
476
+ ApplicationConfig.getComponentConfigValue('system-code');
477
+ const isPrivileged = await loginUser.checkPrivileges(
478
+ systemCode,
479
+ 'PRIVILEGE_DELETE',
480
+ );
481
+ if (!isPrivileged) {
482
+ throw new ClassError(
483
+ 'SystemPrivilege',
484
+ 'SystemPrivilegeErrMsg0X',
485
+ 'You do not have permission to delete system privileges',
486
+ );
487
+ }
488
+
489
+ //Part 2: Validation
490
+ //Make sure SystemPrivilegeId is not empty
491
+ if (!this.SystemPrivilegeId) {
492
+ throw new ClassError(
493
+ 'SystemPrivilege',
494
+ 'SystemPrivilegeErrMsg02',
495
+ 'System Privilege Id is required',
496
+ );
497
+ }
498
+
499
+ //Part 3: Delete Privilege
500
+
501
+ //Call SystemPrivilege._Repo delete method
502
+ await SystemPrivilege._Repository.delete(
503
+ this.SystemPrivilegeId,
504
+ dbTransaction,
505
+ );
506
+
507
+ //Part 4: Record Create Privilege Activity
508
+ //Initialise EntityValueBefore variable and set to empty object.
509
+ const EntityValueBefore = {
510
+ SystemPrivilegeId: this.ObjectId,
511
+ PrivilegeCode: this.PrivilegeCode,
512
+ SystemCode: this.SystemCode,
513
+ Description: this.Description,
514
+ Status: this.Status,
515
+ CreatedById: this._CreatedById,
516
+ UpdatedById: this._UpdatedById,
517
+ CreatedAt: this._CreatedAt,
518
+ UpdatedAt: this._UpdatedAt,
519
+ };
520
+ //Initialise EntityValueAfter variable and set to newSystemPrivilege object.
521
+ const EntityValueAfter = {};
522
+
523
+ //Instantiate new activity object and populate
524
+ const activity = new Activity();
525
+ activity.ActivityId = activity.createId();
526
+ activity.Action = ActionEnum.DELETE;
527
+ activity.Description = 'Delete System Privilege';
528
+ activity.EntityType = 'SystemPrivilege';
529
+ activity.EntityId = this.SystemPrivilegeId;
530
+ activity.EntityValueBefore = JSON.stringify(EntityValueBefore);
531
+ activity.EntityValueAfter = JSON.stringify(EntityValueAfter);
532
+
533
+ //Call Activity.create method
534
+ await activity.create(loginUser.ObjectId, dbTransaction);
535
+
536
+ return this;
537
+ } catch (error) {
538
+ throw error;
539
+ }
540
+ }
541
+ }