@tomei/sso 0.43.1 → 0.44.0

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tomei/sso",
3
- "version": "0.43.1",
3
+ "version": "0.44.0",
4
4
  "description": "Tomei SSO Package",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -8,4 +8,16 @@ export class GroupReportingUserRepository
8
8
  constructor() {
9
9
  super(GroupReportingUserModel);
10
10
  }
11
+
12
+ async destroy(
13
+ groupReportingUserId: number,
14
+ dbTransaction: any,
15
+ ): Promise<void> {
16
+ await GroupReportingUserModel.destroy({
17
+ where: {
18
+ GroupReportingUserId: groupReportingUserId,
19
+ },
20
+ transaction: dbTransaction,
21
+ });
22
+ }
11
23
  }
@@ -5,6 +5,7 @@ import { User } from '../login-user/user';
5
5
  import { Group } from '../group/group';
6
6
  import { ApplicationConfig } from '@tomei/config';
7
7
  import { ActionEnum, Activity } from '@tomei/activity-history';
8
+ import { Transaction } from 'sequelize';
8
9
 
9
10
  export class GroupReportingUser extends ObjectBase {
10
11
  ObjectId: string;
@@ -215,4 +216,146 @@ export class GroupReportingUser extends ObjectBase {
215
216
  throw error;
216
217
  }
217
218
  }
219
+
220
+ public static async findAllGroupReportingUsers(
221
+ loginUser: User, //The authenticated user requesting the information.
222
+ dbTransaction: any, //The database transaction to be used for this operation.
223
+ groupCode?: string, //The code of the group whose reporting users should be retrieved.
224
+ ) {
225
+ //This public static method retrieves all GroupReportingUser records for a given group from the sso_GroupReportingUser table.
226
+
227
+ try {
228
+ // Part 1: Privilege Check
229
+ // Call loginUser.checkPrivileges() method by passing:
230
+ // - SystemCode: Retrieve from the app config.
231
+ // - PrivilegeCode: GROUP_REPORTING_USER_VIEW.
232
+ // If the user does not have the required privilege, throw a ForbiddenError.
233
+ const systemCode =
234
+ ApplicationConfig.getComponentConfigValue('system-code');
235
+ const isPrivileged = await loginUser.checkPrivileges(
236
+ systemCode,
237
+ 'GROUP_REPORTING_USER_VIEW',
238
+ );
239
+
240
+ if (!isPrivileged) {
241
+ throw new ClassError(
242
+ 'Group',
243
+ 'GroupReportingUserErrMsg05',
244
+ 'You do not have the privilege to view group reporting user',
245
+ );
246
+ }
247
+
248
+ // Part 2: Group Existence Check
249
+ // Call Group.init(dbTransaction, groupCode) to verify the group exists.
250
+ // If the group does not exist, throw a NotFoundError.
251
+ await Group.init(dbTransaction, groupCode);
252
+
253
+ // Part 3: Retrieve Group Reporting Users
254
+ // Call GroupReportingUser._Repo.findAll() to retrieve all users associated with the provided groupCode.
255
+ // The users should be sorted by Rank in ascending order (Rank 1, Rank 2, and so on).
256
+ // Ensure the query is performed within the dbTransaction.
257
+ const result = await GroupReportingUser._Repo.findAll({
258
+ where: {
259
+ GroupCode: groupCode,
260
+ },
261
+ order: [
262
+ ['Rank', 'ASC'], // or 'DESC' for descending order
263
+ ],
264
+ transaction: dbTransaction,
265
+ });
266
+
267
+ // Part 4: Return Results
268
+ // Return the array of GroupReportingUser records found.
269
+ const reportingUser: GroupReportingUser[] = [];
270
+ if (result.length > 0) {
271
+ for (let i = 0; i < result.length; i++) {
272
+ reportingUser.push(
273
+ new GroupReportingUser(result[i].get({ plain: true })),
274
+ );
275
+ }
276
+ }
277
+ return reportingUser;
278
+ } catch (error) {
279
+ // Part 5: Handle Errors
280
+ // Catch and handle any errors during the execution. If an error occurs, ensure the transaction is rolled back.
281
+ throw error;
282
+ }
283
+ }
284
+
285
+ public static async removeGroupReportingUser(
286
+ loginUser: User, //The user performing the operation, used for privilege checking and logging.
287
+ dbTransaction: Transaction, // The database transaction object to ensure the operation's atomicity.
288
+ groupReportingUserId: number, //The ID of the GroupReportingUser to be removed.
289
+ ): Promise<void> {
290
+ // This method removes a GroupReportingUser record from the database.
291
+ try {
292
+ // Part 1: Privilege Checking
293
+ // Call loginUser.checkPrivileges() method by passing:
294
+ // SystemCode: Retrieve from app config.
295
+ // PrivilegeCode: "GROUP_REPORTING_USER_REMOVE".
296
+ const systemCode =
297
+ ApplicationConfig.getComponentConfigValue('system-code');
298
+ const isPrivileged = await loginUser.checkPrivileges(
299
+ systemCode,
300
+ 'GROUP_REPORTING_USER_REMOVE',
301
+ );
302
+ if (!isPrivileged) {
303
+ throw new ClassError(
304
+ 'GroupReportingUser',
305
+ 'GroupReportingUserErrMsg06',
306
+ 'Insufficient privileges to remove a user from the group',
307
+ );
308
+ }
309
+
310
+ // Part 2: Find User
311
+ // Call GroupReportingUser.init(dbTransaction, groupReportingUserId) to check if the user exists.
312
+ const groupReportingUser = await GroupReportingUser.init(
313
+ dbTransaction,
314
+ groupReportingUserId.toString(),
315
+ );
316
+
317
+ // Part 3: Remove User
318
+ // Call GroupReportingUser._Repo.destroy({ where: { GroupReportingUserId: groupReportingUserId }, transaction: dbTransaction }) to remove the user from the database.
319
+ await GroupReportingUser._Repo.destroy(
320
+ groupReportingUserId,
321
+ dbTransaction,
322
+ );
323
+
324
+ // Part 4: Record Create Activity
325
+ // Initialise EntityValueBefore variable and set it to the GroupReportingUser instance before destruction.
326
+ const entityValueBefore = {
327
+ GroupReportingUserId: groupReportingUser.GroupReportingUserId,
328
+ GroupCode: groupReportingUser.GroupCode,
329
+ UserId: groupReportingUser.UserId,
330
+ Rank: groupReportingUser.Rank,
331
+ Status: groupReportingUser.Status,
332
+ CreatedById: groupReportingUser.CreatedById,
333
+ CreatedAt: groupReportingUser.CreatedAt,
334
+ UpdatedById: groupReportingUser.UpdatedById,
335
+ UpdatedAt: groupReportingUser.UpdatedAt,
336
+ };
337
+ // Instantiate a new activity from the Activity class, and set:
338
+ const activity = new Activity();
339
+ // ActivityId: activity.createId()
340
+ // Action: ActionEnum.Delete
341
+ // Description: Remove Group Reporting User
342
+ // EntityType: GroupReportingUser
343
+ // EntityId: groupReportingUserId
344
+ // EntityValueBefore: Stringified representation of the GroupReportingUser instance before destroy
345
+ // EntityValueAfter: Stringified empty object ({})
346
+ activity.ActivityId = activity.createId();
347
+ activity.Action = ActionEnum.DELETE;
348
+ activity.Description = 'Remove Group Reporting User';
349
+ activity.EntityType = 'GroupReportingUser';
350
+ activity.EntityId = groupReportingUserId.toString();
351
+ activity.EntityValueBefore = JSON.stringify(entityValueBefore);
352
+ activity.EntityValueAfter = JSON.stringify({});
353
+ // Call the activity.create() method by passing:
354
+ // dbTransaction
355
+ // userId: loginUser.UserId
356
+ await activity.create(loginUser.ObjectId, dbTransaction);
357
+ } catch (error) {
358
+ throw error;
359
+ }
360
+ }
218
361
  }