@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,506 +1,506 @@
1
- import { Op, Transaction } from 'sequelize';
2
- import { ClassError, ObjectBase } from '@tomei/general';
3
- import { GroupReportingUserRepository } from './group-reporting-user.repository';
4
- import { IGroupReportingUserAttr } from '../../interfaces/group-reporting-user.interface';
5
- import { User } from '../login-user/user';
6
- import UserModel from '../../models/user.entity';
7
- import { Group } from '../group/group';
8
- import { ApplicationConfig } from '@tomei/config';
9
- import { ActionEnum, Activity } from '@tomei/activity-history';
10
-
11
- export class GroupReportingUser extends ObjectBase {
12
- ObjectId: string;
13
- ObjectName: string;
14
- TableName = 'sso_GroupReportingUser';
15
- ObjectType = 'GroupReportingUser';
16
-
17
- GroupReportingUserId: number;
18
- GroupCode: string;
19
- UserId: number;
20
- Rank: number;
21
- Status: string;
22
- private _CreatedById: number;
23
- private _CreatedAt: Date;
24
- private _UpdatedById: number;
25
- private _UpdatedAt: Date;
26
- private static _Repo = new GroupReportingUserRepository();
27
-
28
- get CreatedById(): number {
29
- return this._CreatedById;
30
- }
31
-
32
- get CreatedAt(): Date {
33
- return this._CreatedAt;
34
- }
35
-
36
- get UpdatedById(): number {
37
- return this._UpdatedById;
38
- }
39
-
40
- get UpdatedAt(): Date {
41
- return this._UpdatedAt;
42
- }
43
-
44
- private constructor(groupReportingUserAttr?: IGroupReportingUserAttr) {
45
- super();
46
- if (groupReportingUserAttr) {
47
- this.GroupReportingUserId = groupReportingUserAttr.GroupReportingUserId;
48
- this.GroupCode = groupReportingUserAttr.GroupCode;
49
- this.UserId = groupReportingUserAttr?.UserId;
50
- this.Rank = groupReportingUserAttr?.Rank;
51
- this.Status = groupReportingUserAttr?.Status;
52
- this._CreatedById = groupReportingUserAttr.CreatedById;
53
- this._CreatedAt = groupReportingUserAttr.CreatedAt;
54
- this._UpdatedById = groupReportingUserAttr.UpdatedById;
55
- this._UpdatedAt = groupReportingUserAttr.UpdatedAt;
56
- }
57
- }
58
-
59
- public static async init(dbTransaction: any, GroupReportingUserId?: string) {
60
- try {
61
- if (GroupReportingUserId) {
62
- const groupReportingUser = await GroupReportingUser._Repo.findByPk(
63
- GroupReportingUserId,
64
- {
65
- transaction: dbTransaction,
66
- },
67
- );
68
- if (groupReportingUser) {
69
- return new GroupReportingUser(groupReportingUser);
70
- } else {
71
- throw new ClassError(
72
- 'GroupReportingUser',
73
- 'GroupReportingUserErrMsg00',
74
- 'GroupReportingUser Not Found',
75
- 'init',
76
- 404,
77
- );
78
- }
79
- }
80
- return new GroupReportingUser();
81
- } catch (error) {
82
- throw error;
83
- }
84
- }
85
-
86
- async createGroupReportingUser(
87
- loginUser: User, //The user performing the operation(typically the logged -in user).
88
- dbTransaction: any, //Database transaction object to ensure the operation is atomic.
89
- groupCode: string, //The code of the group to which the user is being assigned.
90
- userId: number, //The ID of the user to be added to the group.
91
- rank: number, //The rank to be assigned to the user in the group.
92
- status: 'Active' | 'Inactive', //The initial status of the user in the group.
93
- ): Promise<GroupReportingUser> {
94
- // Returns a GroupReportingUser instance representing the newly created record.
95
- try {
96
- //Creates a new group reporting user entry in the sso_GroupReportingUser table.
97
-
98
- // Validate Input Parameters
99
- // Ensure groupCode exists in the sso_Group table by calling the Group.init() method.
100
- const group = await Group.init(dbTransaction, groupCode);
101
- // Ensure userId exists in the sso_User table by calling the User.init() method.
102
- const user = await User.init(dbTransaction, userId);
103
- // Privilege Checking
104
- // Call the loginUser.checkPrivileges() method by passing:
105
- // SystemCode: Retrieve from app config.
106
- // PrivilegeCode: "GROUP_REPORTING_USER_CREATE".
107
-
108
- const systemCode =
109
- ApplicationConfig.getComponentConfigValue('system-code');
110
- const isPrivileged = await loginUser.checkPrivileges(
111
- systemCode,
112
- 'GROUP_REPORTING_USER_CREATE',
113
- );
114
- if (!isPrivileged) {
115
- throw new ClassError(
116
- 'GroupReportingUser',
117
- 'GroupReportingUserErrMsg02',
118
- 'Insufficient privileges to add a user to the group',
119
- );
120
- }
121
-
122
- // Check for Duplicate User in Group
123
- // Query the sso_GroupReportingUser table to see if the userId already exists in the specified groupCode.
124
- const groupReportingUser = await GroupReportingUser._Repo.findOne({
125
- where: {
126
- GroupCode: groupCode,
127
- UserId: userId,
128
- },
129
- transaction: dbTransaction,
130
- });
131
- // If the user already exists in the group, throw an error indicating the user is already part of the group.
132
- if (groupReportingUser) {
133
- throw new ClassError(
134
- 'GroupReportingUser',
135
- 'GroupReportingUserErrMsg03',
136
- 'User already exists in the group',
137
- 'createGroupReportingUser',
138
- );
139
- }
140
- //Query the sso_GroupReportingUser table to see if the rank already exists in the specified groupCode.
141
- //If the rank already exists in the group, throw an error indicating the rank is already in of the group.
142
- const groupReportingUserRank = await GroupReportingUser._Repo.findOne({
143
- where: {
144
- GroupCode: groupCode,
145
- Rank: rank,
146
- },
147
- transaction: dbTransaction,
148
- });
149
- if (groupReportingUserRank) {
150
- throw new ClassError(
151
- 'GroupReportingUser',
152
- 'GroupReportingUserErrMsg04',
153
- 'Rank already exists in the group',
154
- 'createGroupReportingUser',
155
- );
156
- }
157
-
158
- // Create GroupReportingUser Entry
159
- // If validation and privilege checks pass, insert a new record in the sso_GroupReportingUser table with the provided groupCode, userId, rank, status, and loginUser.UserId.Automatically capture the current timestamp for CreatedAt.
160
- this.GroupCode = groupCode;
161
- this.UserId = userId;
162
- this.Rank = rank;
163
- this.Status = status;
164
- this._CreatedById = loginUser.UserId;
165
- this._CreatedAt = new Date();
166
- this._UpdatedAt = new Date();
167
- this._UpdatedById = loginUser.UserId;
168
-
169
- const entityValueAfter: any = {
170
- GroupCode: groupCode,
171
- UserId: userId,
172
- Rank: rank,
173
- Status: status,
174
- CreatedById: loginUser.UserId,
175
- CreatedAt: this._CreatedAt,
176
- UpdatedById: loginUser.UserId,
177
- UpdatedAt: this._UpdatedAt,
178
- };
179
-
180
- const newGroupReportingUser = await GroupReportingUser._Repo.create(
181
- entityValueAfter,
182
- {
183
- transaction: dbTransaction,
184
- },
185
- );
186
-
187
- entityValueAfter.GroupReportingUserId =
188
- newGroupReportingUser.GroupReportingUserId;
189
-
190
- // Record Create Activity
191
- // Instantiate a new activity from the Activity class, and set:\
192
- // ActivityId: activity.createId()
193
- // Action: ActionEnum.Create
194
- // Description: Create Group Reporting User
195
- // EntityType: GroupReportingUser
196
- // EntityId: newGroupReportingUser.GroupReportingUserId
197
- // EntityValueBefore: Stringified empty object({})
198
- // EntityValueAfter: EntityValueAfter(stringified representation of the newly created entity)
199
- const activity = new Activity();
200
- activity.ActivityId = activity.createId();
201
- activity.Action = ActionEnum.CREATE;
202
- activity.Description = 'Create Group Reporting User';
203
- activity.EntityType = 'GroupReportingUser';
204
- activity.EntityId = newGroupReportingUser.GroupReportingUserId.toString();
205
- activity.EntityValueBefore = JSON.stringify({});
206
- activity.EntityValueAfter = JSON.stringify(entityValueAfter);
207
- // Call the activity create() method by passing:
208
- // dbTransaction
209
- // userId: loginUser.UserId
210
- await activity.create(loginUser.ObjectId, dbTransaction);
211
- // Return the Created GroupReportingUser
212
- // Return the newly created GroupReportingUser instance, including all the relevant details like GroupReportingUserId, groupCode, userId, rank, status, and timestamps for CreatedAt.
213
- return this;
214
- } catch (error) {
215
- throw error;
216
- }
217
- }
218
-
219
- async updateGroupReportingUser(
220
- loginUser: User, //The user performing the operation(typically the logged -in user).
221
- dbTransaction: any, //Database transaction object to ensure the operation is atomic.
222
- groupCode: string, //The code of the group to which the user is being assigned.
223
- userId: number, //The ID of the user to be added to the group.
224
- rank: number, //The rank to be assigned to the user in the group.
225
- status: 'Active' | 'Inactive', //The initial status of the user in the group.
226
- ): Promise<GroupReportingUser> {
227
- // Returns a GroupReportingUser instance representing the updated record.
228
- try {
229
- //Update a group reporting user entry in the sso_GroupReportingUser table.
230
-
231
- // Validate Input Parameters
232
- // Ensure groupCode exists in the sso_Group table by calling the Group.init() method.
233
- await Group.init(dbTransaction, groupCode);
234
- // Ensure userId exists in the sso_User table by calling the User.init() method.
235
- await User.init(dbTransaction, userId);
236
- // Privilege Checking
237
- // Call the loginUser.checkPrivileges() method by passing:
238
- // SystemCode: Retrieve from app config.
239
- // PrivilegeCode: "GROUP_REPORTING_USER_UPDATE".
240
-
241
- const systemCode =
242
- ApplicationConfig.getComponentConfigValue('system-code');
243
- const isPrivileged = await loginUser.checkPrivileges(
244
- systemCode,
245
- 'GROUP_REPORTING_USER_UPDATE',
246
- );
247
- if (!isPrivileged) {
248
- throw new ClassError(
249
- 'GroupReportingUser',
250
- 'GroupReportingUserErrMsg02',
251
- 'Insufficient privileges to update a user to the group',
252
- );
253
- }
254
-
255
- //Get the current groupReportingUser
256
- const currentGroupReportingUser = await GroupReportingUser._Repo.findOne({
257
- where: {
258
- GroupCode: groupCode,
259
- GroupReportingUserId: this.GroupReportingUserId,
260
- },
261
- transaction: dbTransaction,
262
- });
263
-
264
- // Check for Duplicate User in Group
265
- // Query the sso_GroupReportingUser table to see if the userId already exists in the specified groupCode.
266
- const groupReportingUser = await GroupReportingUser._Repo.findOne({
267
- where: {
268
- GroupCode: groupCode,
269
- UserId: userId,
270
- GroupReportingUserId: {
271
- [Op.ne]: this.GroupReportingUserId,
272
- },
273
- },
274
- transaction: dbTransaction,
275
- });
276
- // If the user already exists in the group, throw an error indicating the user is already part of the group.
277
- if (groupReportingUser) {
278
- throw new ClassError(
279
- 'GroupReportingUser',
280
- 'GroupReportingUserErrMsg03',
281
- 'User already exists in the group',
282
- 'updateGroupReportingUser',
283
- );
284
- }
285
- //Query the sso_GroupReportingUser table to see if the rank already exists in the specified groupCode.
286
- //If the rank already exists in the group, throw an error indicating the rank is already in of the group.
287
- const groupReportingUserRank = await GroupReportingUser._Repo.findOne({
288
- where: {
289
- GroupCode: groupCode,
290
- Rank: rank,
291
- GroupReportingUserId: {
292
- [Op.ne]: this.GroupReportingUserId,
293
- },
294
- },
295
- transaction: dbTransaction,
296
- });
297
- if (groupReportingUserRank) {
298
- throw new ClassError(
299
- 'GroupReportingUser',
300
- 'GroupReportingUserErrMsg04',
301
- 'Rank already exists in the group',
302
- 'updateGroupReportingUser',
303
- );
304
- }
305
-
306
- // UPDATE GroupReportingUser Entry
307
- // If validation and privilege checks pass, insert a new record in the sso_GroupReportingUser table with the provided groupCode, userId, rank, status, and loginUser.UserId.Automatically capture the current timestamp for CreatedAt.
308
- this.GroupCode = groupCode;
309
- this.UserId = userId;
310
- this.Rank = rank;
311
- this.Status = status;
312
- this._CreatedById = currentGroupReportingUser.CreatedById;
313
- this._CreatedAt = currentGroupReportingUser.CreatedAt;
314
- this._UpdatedAt = new Date();
315
- this._UpdatedById = loginUser.UserId;
316
-
317
- const entityValueAfter: any = {
318
- GroupCode: groupCode,
319
- UserId: userId,
320
- Rank: rank,
321
- Status: status,
322
- CreatedById: currentGroupReportingUser.CreatedById,
323
- CreatedAt: this._CreatedAt,
324
- UpdatedById: loginUser.UserId,
325
- UpdatedAt: this._UpdatedAt,
326
- };
327
-
328
- await GroupReportingUser._Repo.update(entityValueAfter, {
329
- where: {
330
- GroupReportingUserId: this.GroupReportingUserId,
331
- },
332
- transaction: dbTransaction,
333
- });
334
-
335
- // Record Update Activity
336
- // Instantiate a new activity from the Activity class, and set:\
337
- // ActivityId: activity.createId()
338
- // Action: ActionEnum.Update
339
- // Description: Update Group Reporting User
340
- // EntityType: GroupReportingUser
341
- // EntityId: newGroupReportingUser.GroupReportingUserId
342
- // EntityValueBefore: Stringified empty object({})
343
- // EntityValueAfter: EntityValueAfter(stringified representation of the newly created entity)
344
- const activity = new Activity();
345
- activity.ActivityId = activity.createId();
346
- activity.Action = ActionEnum.UPDATE;
347
- activity.Description = 'Update Group Reporting User';
348
- activity.EntityType = 'GroupReportingUser';
349
- activity.EntityId = this.GroupReportingUserId.toString();
350
- activity.EntityValueBefore = JSON.stringify({});
351
- activity.EntityValueAfter = JSON.stringify(entityValueAfter);
352
- // Call the activity create() method by passing:
353
- // dbTransaction
354
- // userId: loginUser.UserId
355
- await activity.create(loginUser.ObjectId, dbTransaction);
356
- // Return the Updated GroupReportingUser
357
- // Return the updated GroupReportingUser instance, including all the relevant details like GroupReportingUserId, groupCode, userId, rank, status, and timestamps for CreatedAt.
358
- return this;
359
- } catch (error) {
360
- throw error;
361
- }
362
- }
363
-
364
- public static async findAllGroupReportingUsers(
365
- loginUser: User, //The authenticated user requesting the information.
366
- dbTransaction: any, //The database transaction to be used for this operation.
367
- groupCode?: string, //The code of the group whose reporting users should be retrieved.
368
- ) {
369
- //This public static method retrieves all GroupReportingUser records for a given group from the sso_GroupReportingUser table.
370
-
371
- try {
372
- // Part 1: Privilege Check
373
- // Call loginUser.checkPrivileges() method by passing:
374
- // - SystemCode: Retrieve from the app config.
375
- // - PrivilegeCode: GROUP_REPORTING_USER_VIEW.
376
- // If the user does not have the required privilege, throw a ForbiddenError.
377
- const systemCode =
378
- ApplicationConfig.getComponentConfigValue('system-code');
379
- const isPrivileged = await loginUser.checkPrivileges(
380
- systemCode,
381
- 'GROUP_REPORTING_USER_VIEW',
382
- );
383
-
384
- if (!isPrivileged) {
385
- throw new ClassError(
386
- 'Group',
387
- 'GroupReportingUserErrMsg05',
388
- 'You do not have the privilege to view group reporting user',
389
- );
390
- }
391
-
392
- // Part 2: Group Existence Check
393
- // Call Group.init(dbTransaction, groupCode) to verify the group exists.
394
- // If the group does not exist, throw a NotFoundError.
395
- await Group.init(dbTransaction, groupCode);
396
-
397
- // Part 3: Retrieve Group Reporting Users
398
- // Call GroupReportingUser._Repo.findAll() to retrieve all users associated with the provided groupCode.
399
- // The users should be sorted by Rank in ascending order (Rank 1, Rank 2, and so on).
400
- // Ensure the query is performed within the dbTransaction.
401
- const result = await GroupReportingUser._Repo.findAll({
402
- where: {
403
- GroupCode: groupCode,
404
- },
405
- include: [
406
- {
407
- model: UserModel,
408
- as: 'User',
409
- },
410
- ],
411
- order: [
412
- ['Rank', 'ASC'], // or 'DESC' for descending order
413
- ],
414
- transaction: dbTransaction,
415
- });
416
-
417
- // Part 4: Return Results
418
- // Return the array of GroupReportingUser records found.
419
-
420
- return result;
421
- } catch (error) {
422
- // Part 5: Handle Errors
423
- // Catch and handle any errors during the execution. If an error occurs, ensure the transaction is rolled back.
424
- throw error;
425
- }
426
- }
427
-
428
- public static async removeGroupReportingUser(
429
- loginUser: User, //The user performing the operation, used for privilege checking and logging.
430
- dbTransaction: Transaction, // The database transaction object to ensure the operation's atomicity.
431
- groupReportingUserId: number, //The ID of the GroupReportingUser to be removed.
432
- ): Promise<void> {
433
- // This method removes a GroupReportingUser record from the database.
434
- try {
435
- // Part 1: Privilege Checking
436
- // Call loginUser.checkPrivileges() method by passing:
437
- // SystemCode: Retrieve from app config.
438
- // PrivilegeCode: "GROUP_REPORTING_USER_REMOVE".
439
- const systemCode =
440
- ApplicationConfig.getComponentConfigValue('system-code');
441
- const isPrivileged = await loginUser.checkPrivileges(
442
- systemCode,
443
- 'GROUP_REPORTING_USER_REMOVE',
444
- );
445
- if (!isPrivileged) {
446
- throw new ClassError(
447
- 'GroupReportingUser',
448
- 'GroupReportingUserErrMsg06',
449
- 'Insufficient privileges to remove a user from the group',
450
- 'removeGroupReportingUser',
451
- 403,
452
- );
453
- }
454
-
455
- // Part 2: Find User
456
- // Call GroupReportingUser.init(dbTransaction, groupReportingUserId) to check if the user exists.
457
- const groupReportingUser = await GroupReportingUser.init(
458
- dbTransaction,
459
- groupReportingUserId.toString(),
460
- );
461
-
462
- // Part 3: Remove User
463
- // Call GroupReportingUser._Repo.destroy({ where: { GroupReportingUserId: groupReportingUserId }, transaction: dbTransaction }) to remove the user from the database.
464
- await GroupReportingUser._Repo.destroy(
465
- groupReportingUserId,
466
- dbTransaction,
467
- );
468
-
469
- // Part 4: Record Create Activity
470
- // Initialise EntityValueBefore variable and set it to the GroupReportingUser instance before destruction.
471
- const entityValueBefore = {
472
- GroupReportingUserId: groupReportingUser.GroupReportingUserId,
473
- GroupCode: groupReportingUser.GroupCode,
474
- UserId: groupReportingUser.UserId,
475
- Rank: groupReportingUser.Rank,
476
- Status: groupReportingUser.Status,
477
- CreatedById: groupReportingUser.CreatedById,
478
- CreatedAt: groupReportingUser.CreatedAt,
479
- UpdatedById: groupReportingUser.UpdatedById,
480
- UpdatedAt: groupReportingUser.UpdatedAt,
481
- };
482
- // Instantiate a new activity from the Activity class, and set:
483
- const activity = new Activity();
484
- // ActivityId: activity.createId()
485
- // Action: ActionEnum.Delete
486
- // Description: Remove Group Reporting User
487
- // EntityType: GroupReportingUser
488
- // EntityId: groupReportingUserId
489
- // EntityValueBefore: Stringified representation of the GroupReportingUser instance before destroy
490
- // EntityValueAfter: Stringified empty object ({})
491
- activity.ActivityId = activity.createId();
492
- activity.Action = ActionEnum.DELETE;
493
- activity.Description = 'Remove Group Reporting User';
494
- activity.EntityType = 'GroupReportingUser';
495
- activity.EntityId = groupReportingUserId.toString();
496
- activity.EntityValueBefore = JSON.stringify(entityValueBefore);
497
- activity.EntityValueAfter = JSON.stringify({});
498
- // Call the activity.create() method by passing:
499
- // dbTransaction
500
- // userId: loginUser.UserId
501
- await activity.create(loginUser.ObjectId, dbTransaction);
502
- } catch (error) {
503
- throw error;
504
- }
505
- }
506
- }
1
+ import { Op, Transaction } from 'sequelize';
2
+ import { ClassError, ObjectBase } from '@tomei/general';
3
+ import { GroupReportingUserRepository } from './group-reporting-user.repository';
4
+ import { IGroupReportingUserAttr } from '../../interfaces/group-reporting-user.interface';
5
+ import { User } from '../login-user/user';
6
+ import UserModel from '../../models/user.entity';
7
+ import { Group } from '../group/group';
8
+ import { ApplicationConfig } from '@tomei/config';
9
+ import { ActionEnum, Activity } from '@tomei/activity-history';
10
+
11
+ export class GroupReportingUser extends ObjectBase {
12
+ ObjectId: string;
13
+ ObjectName: string;
14
+ TableName = 'sso_GroupReportingUser';
15
+ ObjectType = 'GroupReportingUser';
16
+
17
+ GroupReportingUserId: number;
18
+ GroupCode: string;
19
+ UserId: number;
20
+ Rank: number;
21
+ Status: string;
22
+ private _CreatedById: number;
23
+ private _CreatedAt: Date;
24
+ private _UpdatedById: number;
25
+ private _UpdatedAt: Date;
26
+ private static _Repo = new GroupReportingUserRepository();
27
+
28
+ get CreatedById(): number {
29
+ return this._CreatedById;
30
+ }
31
+
32
+ get CreatedAt(): Date {
33
+ return this._CreatedAt;
34
+ }
35
+
36
+ get UpdatedById(): number {
37
+ return this._UpdatedById;
38
+ }
39
+
40
+ get UpdatedAt(): Date {
41
+ return this._UpdatedAt;
42
+ }
43
+
44
+ private constructor(groupReportingUserAttr?: IGroupReportingUserAttr) {
45
+ super();
46
+ if (groupReportingUserAttr) {
47
+ this.GroupReportingUserId = groupReportingUserAttr.GroupReportingUserId;
48
+ this.GroupCode = groupReportingUserAttr.GroupCode;
49
+ this.UserId = groupReportingUserAttr?.UserId;
50
+ this.Rank = groupReportingUserAttr?.Rank;
51
+ this.Status = groupReportingUserAttr?.Status;
52
+ this._CreatedById = groupReportingUserAttr.CreatedById;
53
+ this._CreatedAt = groupReportingUserAttr.CreatedAt;
54
+ this._UpdatedById = groupReportingUserAttr.UpdatedById;
55
+ this._UpdatedAt = groupReportingUserAttr.UpdatedAt;
56
+ }
57
+ }
58
+
59
+ public static async init(dbTransaction: any, GroupReportingUserId?: string) {
60
+ try {
61
+ if (GroupReportingUserId) {
62
+ const groupReportingUser = await GroupReportingUser._Repo.findByPk(
63
+ GroupReportingUserId,
64
+ {
65
+ transaction: dbTransaction,
66
+ },
67
+ );
68
+ if (groupReportingUser) {
69
+ return new GroupReportingUser(groupReportingUser);
70
+ } else {
71
+ throw new ClassError(
72
+ 'GroupReportingUser',
73
+ 'GroupReportingUserErrMsg00',
74
+ 'GroupReportingUser Not Found',
75
+ 'init',
76
+ 404,
77
+ );
78
+ }
79
+ }
80
+ return new GroupReportingUser();
81
+ } catch (error) {
82
+ throw error;
83
+ }
84
+ }
85
+
86
+ async createGroupReportingUser(
87
+ loginUser: User, //The user performing the operation(typically the logged -in user).
88
+ dbTransaction: any, //Database transaction object to ensure the operation is atomic.
89
+ groupCode: string, //The code of the group to which the user is being assigned.
90
+ userId: number, //The ID of the user to be added to the group.
91
+ rank: number, //The rank to be assigned to the user in the group.
92
+ status: 'Active' | 'Inactive', //The initial status of the user in the group.
93
+ ): Promise<GroupReportingUser> {
94
+ // Returns a GroupReportingUser instance representing the newly created record.
95
+ try {
96
+ //Creates a new group reporting user entry in the sso_GroupReportingUser table.
97
+
98
+ // Validate Input Parameters
99
+ // Ensure groupCode exists in the sso_Group table by calling the Group.init() method.
100
+ const group = await Group.init(dbTransaction, groupCode);
101
+ // Ensure userId exists in the sso_User table by calling the User.init() method.
102
+ const user = await User.init(dbTransaction, userId);
103
+ // Privilege Checking
104
+ // Call the loginUser.checkPrivileges() method by passing:
105
+ // SystemCode: Retrieve from app config.
106
+ // PrivilegeCode: "GROUP_REPORTING_USER_CREATE".
107
+
108
+ const systemCode =
109
+ ApplicationConfig.getComponentConfigValue('system-code');
110
+ const isPrivileged = await loginUser.checkPrivileges(
111
+ systemCode,
112
+ 'GROUP_REPORTING_USER_CREATE',
113
+ );
114
+ if (!isPrivileged) {
115
+ throw new ClassError(
116
+ 'GroupReportingUser',
117
+ 'GroupReportingUserErrMsg02',
118
+ 'Insufficient privileges to add a user to the group',
119
+ );
120
+ }
121
+
122
+ // Check for Duplicate User in Group
123
+ // Query the sso_GroupReportingUser table to see if the userId already exists in the specified groupCode.
124
+ const groupReportingUser = await GroupReportingUser._Repo.findOne({
125
+ where: {
126
+ GroupCode: groupCode,
127
+ UserId: userId,
128
+ },
129
+ transaction: dbTransaction,
130
+ });
131
+ // If the user already exists in the group, throw an error indicating the user is already part of the group.
132
+ if (groupReportingUser) {
133
+ throw new ClassError(
134
+ 'GroupReportingUser',
135
+ 'GroupReportingUserErrMsg03',
136
+ 'User already exists in the group',
137
+ 'createGroupReportingUser',
138
+ );
139
+ }
140
+ //Query the sso_GroupReportingUser table to see if the rank already exists in the specified groupCode.
141
+ //If the rank already exists in the group, throw an error indicating the rank is already in of the group.
142
+ const groupReportingUserRank = await GroupReportingUser._Repo.findOne({
143
+ where: {
144
+ GroupCode: groupCode,
145
+ Rank: rank,
146
+ },
147
+ transaction: dbTransaction,
148
+ });
149
+ if (groupReportingUserRank) {
150
+ throw new ClassError(
151
+ 'GroupReportingUser',
152
+ 'GroupReportingUserErrMsg04',
153
+ 'Rank already exists in the group',
154
+ 'createGroupReportingUser',
155
+ );
156
+ }
157
+
158
+ // Create GroupReportingUser Entry
159
+ // If validation and privilege checks pass, insert a new record in the sso_GroupReportingUser table with the provided groupCode, userId, rank, status, and loginUser.UserId.Automatically capture the current timestamp for CreatedAt.
160
+ this.GroupCode = groupCode;
161
+ this.UserId = userId;
162
+ this.Rank = rank;
163
+ this.Status = status;
164
+ this._CreatedById = loginUser.UserId;
165
+ this._CreatedAt = new Date();
166
+ this._UpdatedAt = new Date();
167
+ this._UpdatedById = loginUser.UserId;
168
+
169
+ const entityValueAfter: any = {
170
+ GroupCode: groupCode,
171
+ UserId: userId,
172
+ Rank: rank,
173
+ Status: status,
174
+ CreatedById: loginUser.UserId,
175
+ CreatedAt: this._CreatedAt,
176
+ UpdatedById: loginUser.UserId,
177
+ UpdatedAt: this._UpdatedAt,
178
+ };
179
+
180
+ const newGroupReportingUser = await GroupReportingUser._Repo.create(
181
+ entityValueAfter,
182
+ {
183
+ transaction: dbTransaction,
184
+ },
185
+ );
186
+
187
+ entityValueAfter.GroupReportingUserId =
188
+ newGroupReportingUser.GroupReportingUserId;
189
+
190
+ // Record Create Activity
191
+ // Instantiate a new activity from the Activity class, and set:\
192
+ // ActivityId: activity.createId()
193
+ // Action: ActionEnum.Create
194
+ // Description: Create Group Reporting User
195
+ // EntityType: GroupReportingUser
196
+ // EntityId: newGroupReportingUser.GroupReportingUserId
197
+ // EntityValueBefore: Stringified empty object({})
198
+ // EntityValueAfter: EntityValueAfter(stringified representation of the newly created entity)
199
+ const activity = new Activity();
200
+ activity.ActivityId = activity.createId();
201
+ activity.Action = ActionEnum.CREATE;
202
+ activity.Description = 'Create Group Reporting User';
203
+ activity.EntityType = 'GroupReportingUser';
204
+ activity.EntityId = newGroupReportingUser.GroupReportingUserId.toString();
205
+ activity.EntityValueBefore = JSON.stringify({});
206
+ activity.EntityValueAfter = JSON.stringify(entityValueAfter);
207
+ // Call the activity create() method by passing:
208
+ // dbTransaction
209
+ // userId: loginUser.UserId
210
+ await activity.create(loginUser.ObjectId, dbTransaction);
211
+ // Return the Created GroupReportingUser
212
+ // Return the newly created GroupReportingUser instance, including all the relevant details like GroupReportingUserId, groupCode, userId, rank, status, and timestamps for CreatedAt.
213
+ return this;
214
+ } catch (error) {
215
+ throw error;
216
+ }
217
+ }
218
+
219
+ async updateGroupReportingUser(
220
+ loginUser: User, //The user performing the operation(typically the logged -in user).
221
+ dbTransaction: any, //Database transaction object to ensure the operation is atomic.
222
+ groupCode: string, //The code of the group to which the user is being assigned.
223
+ userId: number, //The ID of the user to be added to the group.
224
+ rank: number, //The rank to be assigned to the user in the group.
225
+ status: 'Active' | 'Inactive', //The initial status of the user in the group.
226
+ ): Promise<GroupReportingUser> {
227
+ // Returns a GroupReportingUser instance representing the updated record.
228
+ try {
229
+ //Update a group reporting user entry in the sso_GroupReportingUser table.
230
+
231
+ // Validate Input Parameters
232
+ // Ensure groupCode exists in the sso_Group table by calling the Group.init() method.
233
+ await Group.init(dbTransaction, groupCode);
234
+ // Ensure userId exists in the sso_User table by calling the User.init() method.
235
+ await User.init(dbTransaction, userId);
236
+ // Privilege Checking
237
+ // Call the loginUser.checkPrivileges() method by passing:
238
+ // SystemCode: Retrieve from app config.
239
+ // PrivilegeCode: "GROUP_REPORTING_USER_UPDATE".
240
+
241
+ const systemCode =
242
+ ApplicationConfig.getComponentConfigValue('system-code');
243
+ const isPrivileged = await loginUser.checkPrivileges(
244
+ systemCode,
245
+ 'GROUP_REPORTING_USER_UPDATE',
246
+ );
247
+ if (!isPrivileged) {
248
+ throw new ClassError(
249
+ 'GroupReportingUser',
250
+ 'GroupReportingUserErrMsg02',
251
+ 'Insufficient privileges to update a user to the group',
252
+ );
253
+ }
254
+
255
+ //Get the current groupReportingUser
256
+ const currentGroupReportingUser = await GroupReportingUser._Repo.findOne({
257
+ where: {
258
+ GroupCode: groupCode,
259
+ GroupReportingUserId: this.GroupReportingUserId,
260
+ },
261
+ transaction: dbTransaction,
262
+ });
263
+
264
+ // Check for Duplicate User in Group
265
+ // Query the sso_GroupReportingUser table to see if the userId already exists in the specified groupCode.
266
+ const groupReportingUser = await GroupReportingUser._Repo.findOne({
267
+ where: {
268
+ GroupCode: groupCode,
269
+ UserId: userId,
270
+ GroupReportingUserId: {
271
+ [Op.ne]: this.GroupReportingUserId,
272
+ },
273
+ },
274
+ transaction: dbTransaction,
275
+ });
276
+ // If the user already exists in the group, throw an error indicating the user is already part of the group.
277
+ if (groupReportingUser) {
278
+ throw new ClassError(
279
+ 'GroupReportingUser',
280
+ 'GroupReportingUserErrMsg03',
281
+ 'User already exists in the group',
282
+ 'updateGroupReportingUser',
283
+ );
284
+ }
285
+ //Query the sso_GroupReportingUser table to see if the rank already exists in the specified groupCode.
286
+ //If the rank already exists in the group, throw an error indicating the rank is already in of the group.
287
+ const groupReportingUserRank = await GroupReportingUser._Repo.findOne({
288
+ where: {
289
+ GroupCode: groupCode,
290
+ Rank: rank,
291
+ GroupReportingUserId: {
292
+ [Op.ne]: this.GroupReportingUserId,
293
+ },
294
+ },
295
+ transaction: dbTransaction,
296
+ });
297
+ if (groupReportingUserRank) {
298
+ throw new ClassError(
299
+ 'GroupReportingUser',
300
+ 'GroupReportingUserErrMsg04',
301
+ 'Rank already exists in the group',
302
+ 'updateGroupReportingUser',
303
+ );
304
+ }
305
+
306
+ // UPDATE GroupReportingUser Entry
307
+ // If validation and privilege checks pass, insert a new record in the sso_GroupReportingUser table with the provided groupCode, userId, rank, status, and loginUser.UserId.Automatically capture the current timestamp for CreatedAt.
308
+ this.GroupCode = groupCode;
309
+ this.UserId = userId;
310
+ this.Rank = rank;
311
+ this.Status = status;
312
+ this._CreatedById = currentGroupReportingUser.CreatedById;
313
+ this._CreatedAt = currentGroupReportingUser.CreatedAt;
314
+ this._UpdatedAt = new Date();
315
+ this._UpdatedById = loginUser.UserId;
316
+
317
+ const entityValueAfter: any = {
318
+ GroupCode: groupCode,
319
+ UserId: userId,
320
+ Rank: rank,
321
+ Status: status,
322
+ CreatedById: currentGroupReportingUser.CreatedById,
323
+ CreatedAt: this._CreatedAt,
324
+ UpdatedById: loginUser.UserId,
325
+ UpdatedAt: this._UpdatedAt,
326
+ };
327
+
328
+ await GroupReportingUser._Repo.update(entityValueAfter, {
329
+ where: {
330
+ GroupReportingUserId: this.GroupReportingUserId,
331
+ },
332
+ transaction: dbTransaction,
333
+ });
334
+
335
+ // Record Update Activity
336
+ // Instantiate a new activity from the Activity class, and set:\
337
+ // ActivityId: activity.createId()
338
+ // Action: ActionEnum.Update
339
+ // Description: Update Group Reporting User
340
+ // EntityType: GroupReportingUser
341
+ // EntityId: newGroupReportingUser.GroupReportingUserId
342
+ // EntityValueBefore: Stringified empty object({})
343
+ // EntityValueAfter: EntityValueAfter(stringified representation of the newly created entity)
344
+ const activity = new Activity();
345
+ activity.ActivityId = activity.createId();
346
+ activity.Action = ActionEnum.UPDATE;
347
+ activity.Description = 'Update Group Reporting User';
348
+ activity.EntityType = 'GroupReportingUser';
349
+ activity.EntityId = this.GroupReportingUserId.toString();
350
+ activity.EntityValueBefore = JSON.stringify({});
351
+ activity.EntityValueAfter = JSON.stringify(entityValueAfter);
352
+ // Call the activity create() method by passing:
353
+ // dbTransaction
354
+ // userId: loginUser.UserId
355
+ await activity.create(loginUser.ObjectId, dbTransaction);
356
+ // Return the Updated GroupReportingUser
357
+ // Return the updated GroupReportingUser instance, including all the relevant details like GroupReportingUserId, groupCode, userId, rank, status, and timestamps for CreatedAt.
358
+ return this;
359
+ } catch (error) {
360
+ throw error;
361
+ }
362
+ }
363
+
364
+ public static async findAllGroupReportingUsers(
365
+ loginUser: User, //The authenticated user requesting the information.
366
+ dbTransaction: any, //The database transaction to be used for this operation.
367
+ groupCode?: string, //The code of the group whose reporting users should be retrieved.
368
+ ) {
369
+ //This public static method retrieves all GroupReportingUser records for a given group from the sso_GroupReportingUser table.
370
+
371
+ try {
372
+ // Part 1: Privilege Check
373
+ // Call loginUser.checkPrivileges() method by passing:
374
+ // - SystemCode: Retrieve from the app config.
375
+ // - PrivilegeCode: GROUP_REPORTING_USER_VIEW.
376
+ // If the user does not have the required privilege, throw a ForbiddenError.
377
+ const systemCode =
378
+ ApplicationConfig.getComponentConfigValue('system-code');
379
+ const isPrivileged = await loginUser.checkPrivileges(
380
+ systemCode,
381
+ 'GROUP_REPORTING_USER_VIEW',
382
+ );
383
+
384
+ if (!isPrivileged) {
385
+ throw new ClassError(
386
+ 'Group',
387
+ 'GroupReportingUserErrMsg05',
388
+ 'You do not have the privilege to view group reporting user',
389
+ );
390
+ }
391
+
392
+ // Part 2: Group Existence Check
393
+ // Call Group.init(dbTransaction, groupCode) to verify the group exists.
394
+ // If the group does not exist, throw a NotFoundError.
395
+ await Group.init(dbTransaction, groupCode);
396
+
397
+ // Part 3: Retrieve Group Reporting Users
398
+ // Call GroupReportingUser._Repo.findAll() to retrieve all users associated with the provided groupCode.
399
+ // The users should be sorted by Rank in ascending order (Rank 1, Rank 2, and so on).
400
+ // Ensure the query is performed within the dbTransaction.
401
+ const result = await GroupReportingUser._Repo.findAll({
402
+ where: {
403
+ GroupCode: groupCode,
404
+ },
405
+ include: [
406
+ {
407
+ model: UserModel,
408
+ as: 'User',
409
+ },
410
+ ],
411
+ order: [
412
+ ['Rank', 'ASC'], // or 'DESC' for descending order
413
+ ],
414
+ transaction: dbTransaction,
415
+ });
416
+
417
+ // Part 4: Return Results
418
+ // Return the array of GroupReportingUser records found.
419
+
420
+ return result;
421
+ } catch (error) {
422
+ // Part 5: Handle Errors
423
+ // Catch and handle any errors during the execution. If an error occurs, ensure the transaction is rolled back.
424
+ throw error;
425
+ }
426
+ }
427
+
428
+ public static async removeGroupReportingUser(
429
+ loginUser: User, //The user performing the operation, used for privilege checking and logging.
430
+ dbTransaction: Transaction, // The database transaction object to ensure the operation's atomicity.
431
+ groupReportingUserId: number, //The ID of the GroupReportingUser to be removed.
432
+ ): Promise<void> {
433
+ // This method removes a GroupReportingUser record from the database.
434
+ try {
435
+ // Part 1: Privilege Checking
436
+ // Call loginUser.checkPrivileges() method by passing:
437
+ // SystemCode: Retrieve from app config.
438
+ // PrivilegeCode: "GROUP_REPORTING_USER_REMOVE".
439
+ const systemCode =
440
+ ApplicationConfig.getComponentConfigValue('system-code');
441
+ const isPrivileged = await loginUser.checkPrivileges(
442
+ systemCode,
443
+ 'GROUP_REPORTING_USER_REMOVE',
444
+ );
445
+ if (!isPrivileged) {
446
+ throw new ClassError(
447
+ 'GroupReportingUser',
448
+ 'GroupReportingUserErrMsg06',
449
+ 'Insufficient privileges to remove a user from the group',
450
+ 'removeGroupReportingUser',
451
+ 403,
452
+ );
453
+ }
454
+
455
+ // Part 2: Find User
456
+ // Call GroupReportingUser.init(dbTransaction, groupReportingUserId) to check if the user exists.
457
+ const groupReportingUser = await GroupReportingUser.init(
458
+ dbTransaction,
459
+ groupReportingUserId.toString(),
460
+ );
461
+
462
+ // Part 3: Remove User
463
+ // Call GroupReportingUser._Repo.destroy({ where: { GroupReportingUserId: groupReportingUserId }, transaction: dbTransaction }) to remove the user from the database.
464
+ await GroupReportingUser._Repo.destroy(
465
+ groupReportingUserId,
466
+ dbTransaction,
467
+ );
468
+
469
+ // Part 4: Record Create Activity
470
+ // Initialise EntityValueBefore variable and set it to the GroupReportingUser instance before destruction.
471
+ const entityValueBefore = {
472
+ GroupReportingUserId: groupReportingUser.GroupReportingUserId,
473
+ GroupCode: groupReportingUser.GroupCode,
474
+ UserId: groupReportingUser.UserId,
475
+ Rank: groupReportingUser.Rank,
476
+ Status: groupReportingUser.Status,
477
+ CreatedById: groupReportingUser.CreatedById,
478
+ CreatedAt: groupReportingUser.CreatedAt,
479
+ UpdatedById: groupReportingUser.UpdatedById,
480
+ UpdatedAt: groupReportingUser.UpdatedAt,
481
+ };
482
+ // Instantiate a new activity from the Activity class, and set:
483
+ const activity = new Activity();
484
+ // ActivityId: activity.createId()
485
+ // Action: ActionEnum.Delete
486
+ // Description: Remove Group Reporting User
487
+ // EntityType: GroupReportingUser
488
+ // EntityId: groupReportingUserId
489
+ // EntityValueBefore: Stringified representation of the GroupReportingUser instance before destroy
490
+ // EntityValueAfter: Stringified empty object ({})
491
+ activity.ActivityId = activity.createId();
492
+ activity.Action = ActionEnum.DELETE;
493
+ activity.Description = 'Remove Group Reporting User';
494
+ activity.EntityType = 'GroupReportingUser';
495
+ activity.EntityId = groupReportingUserId.toString();
496
+ activity.EntityValueBefore = JSON.stringify(entityValueBefore);
497
+ activity.EntityValueAfter = JSON.stringify({});
498
+ // Call the activity.create() method by passing:
499
+ // dbTransaction
500
+ // userId: loginUser.UserId
501
+ await activity.create(loginUser.ObjectId, dbTransaction);
502
+ } catch (error) {
503
+ throw error;
504
+ }
505
+ }
506
+ }