@tomei/sso 0.46.7 → 0.46.8

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tomei/sso",
3
- "version": "0.46.7",
3
+ "version": "0.46.8",
4
4
  "description": "Tomei SSO Package",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -8,4 +8,18 @@ export class UserSystemAccessRepository
8
8
  constructor() {
9
9
  super(UserSystemAccessModel);
10
10
  }
11
+
12
+ async delete(UserSystemAccessId: number, dbTransaction?: any) {
13
+ try {
14
+ const options = {
15
+ where: {
16
+ UserSystemAccessId: UserSystemAccessId,
17
+ },
18
+ transaction: dbTransaction,
19
+ };
20
+ await UserSystemAccessModel.destroy(options);
21
+ } catch (error) {
22
+ throw new Error(`An Error occured when delete : ${error.message}`);
23
+ }
24
+ }
11
25
  }
@@ -162,6 +162,7 @@ export class UserSystemAccess extends ObjectBase {
162
162
  return {
163
163
  records: userSystemAccesses.rows.map((userSystemAccess) => {
164
164
  return {
165
+ UserSystemAccessId: userSystemAccess.UserSystemAccessId,
165
166
  SystemName: userSystemAccess.System.Name,
166
167
  SystemCode: userSystemAccess.System.SystemCode,
167
168
  Status: userSystemAccess.Status,
@@ -301,4 +302,178 @@ export class UserSystemAccess extends ObjectBase {
301
302
  throw error;
302
303
  }
303
304
  }
305
+
306
+ public async update(
307
+ loginUser: User, //The user object representing the currently logged-in user.
308
+ dbTransaction: any, //The database transaction instance for managing the transaction scope.
309
+ Status: string, //The new access status (Active/Inactive) for the user system access.
310
+ ) {
311
+ try {
312
+ // Part 1: Update Access
313
+ // Call the UserSystemAccess._Repo.update() method to perform the update operation, passing:
314
+ // - Status: The new access status.
315
+ // - UpdatedById: loginUser.UserId (to indicate who updated the record).
316
+ // - UpdatedAt: Set to the current date and time.
317
+ // - dbTransaction: The database transaction instance.
318
+
319
+ const entityValueBefore = {
320
+ UserId: this.UserId,
321
+ SystemCode: this.SystemCode,
322
+ Status: this.Status,
323
+ CreatedById: this.CreatedById,
324
+ CreatedAt: this.CreatedAt,
325
+ UpdatedById: this.UpdatedById,
326
+ UpdatedAt: this.UpdatedAt,
327
+ };
328
+ await UserSystemAccess._Repository.update(
329
+ {
330
+ Status: Status,
331
+ UpdatedById: loginUser.UserId,
332
+ UpdatedAt: new Date(),
333
+ },
334
+ {
335
+ where: {
336
+ UserSystemAccessId: this.UserSystemAccessId,
337
+ },
338
+ transaction: dbTransaction,
339
+ },
340
+ );
341
+
342
+ const entityValueAfter = {
343
+ UserId: this.UserId,
344
+ SystemCode: this.SystemCode,
345
+ Status: Status,
346
+ CreatedById: this.CreatedById,
347
+ CreatedAt: this.CreatedAt,
348
+ UpdatedById: loginUser.UserId,
349
+ UpdatedAt: new Date(),
350
+ };
351
+
352
+ // Part 2: Record Activity History
353
+ // Initialize a variable entityValueBefore to store the current state of the user system access record before the update.
354
+ // Create an instance of the Activity class and set the following properties:
355
+ // - ActivityId: Call activity.createId().
356
+ // - Action: Set to ActionEnum.Update.
357
+ // - Description: Set to Update User System Access.
358
+ // - EntityType: Set to UserSystemAccess.
359
+ // - EntityId: Use the ID of the updated user system access record.
360
+ // - EntityValueBefore: Stringify entityValueBefore to capture the state before the update.
361
+ // - EntityValueAfter: Stringify the updated user system access record to capture the new state after the update.
362
+ // Call the activity create method with the following parameters:
363
+ // - dbTransaction
364
+ // - userId: loginUser.UserId
365
+ const activity = new Activity();
366
+ activity.ActivityId = activity.createId();
367
+ activity.Action = ActionEnum.UPDATE;
368
+ activity.Description = 'Update User System Access';
369
+ activity.EntityType = 'UserSystemAccess';
370
+ activity.EntityId = this.UserSystemAccessId + '';
371
+ activity.EntityValueBefore = JSON.stringify(entityValueBefore);
372
+ activity.EntityValueAfter = JSON.stringify(entityValueAfter);
373
+ await activity.create(loginUser.ObjectId, dbTransaction);
374
+
375
+ // Part 3: Return Updated Record
376
+ // Retrieve the updated user system access record from the database or return the updated instance as needed.
377
+
378
+ // Part 5: Return Newly Created Record
379
+ // Return the newly created UserSystemAccess instance with all relevant fields, including UserSystemAccessId, SystemCode, Status, CreatedAt, and CreatedById.
380
+ return entityValueAfter;
381
+ } catch (error) {
382
+ throw error;
383
+ }
384
+ }
385
+
386
+ public static async remove(
387
+ loginUser: User, //The currently logged-in user initiating the request.
388
+ dbTransaction: any, //The active database transaction to ensure consistency during the query.
389
+ UserSystemAccessId: number, //The unique identifier of the User System Access record to be deleted.
390
+ ) {
391
+ try {
392
+ // Part 1: Privilege Checking
393
+ // Call loginUser.checkPrivileges() method by passing:
394
+ // - SystemCode: Retrieve from app config.
395
+ // - PrivilegeCode: 'USER_SYSTEM_ACCESS_REMOVE'.
396
+ // If the user does not have the required privileges, throw an appropriate exception.
397
+ const systemCode =
398
+ ApplicationConfig.getComponentConfigValue('system-code');
399
+ const privilegeCode = 'USER_SYSTEM_ACCESS_REMOVE';
400
+ const isPrivileged = await loginUser.checkPrivileges(
401
+ systemCode,
402
+ privilegeCode,
403
+ );
404
+ if (!isPrivileged) {
405
+ throw new ClassError(
406
+ 'UserSystemAccess',
407
+ 'UserSystemAccessErrMsg01',
408
+ 'You do not have permission to access this resource.',
409
+ );
410
+ }
411
+
412
+ // Part 2: Retrieve Record
413
+ // Use the UserSystemAccessRepo.findById(UserSystemAccessId) method to retrieve the record.
414
+ // If the record does not exist, throw an exception indicating the record was not found.
415
+
416
+ const userSystemAccess = await UserSystemAccess._Repository.findOne({
417
+ where: {
418
+ UserSystemAccessId: UserSystemAccessId,
419
+ },
420
+ transaction: dbTransaction,
421
+ });
422
+
423
+ if (!userSystemAccess) {
424
+ throw new ClassError(
425
+ 'UserSystemAccess',
426
+ 'UserSystemAccessErrMsg02',
427
+ 'User System Access not Found',
428
+ );
429
+ }
430
+
431
+ // Part 3: Delete Record
432
+ // Call the UserSystemAccess._Repo.delete() method, passing:
433
+ // - UserSystemAccessId
434
+ // dbTransaction to permanently delete the record from the database.
435
+ await UserSystemAccess._Repository.delete(
436
+ UserSystemAccessId,
437
+ dbTransaction,
438
+ );
439
+
440
+ const entityValueBefore = {
441
+ UserId: userSystemAccess.UserId,
442
+ SystemCode: userSystemAccess.SystemCode,
443
+ Status: userSystemAccess.Status,
444
+ CreatedById: userSystemAccess.CreatedById,
445
+ CreatedAt: userSystemAccess.CreatedAt,
446
+ UpdatedById: userSystemAccess.UpdatedById,
447
+ UpdatedAt: userSystemAccess.UpdatedAt,
448
+ };
449
+
450
+ // Part 4: Record Activity History
451
+ // Instantiate a new activity from the Activity class, and set:
452
+ // - ActivityId: activity.createId()
453
+ // - Action: ActionEnum.Delete
454
+ // - Description: Delete User System Access
455
+ // - EntityType: UserSystemAccess
456
+ // - EntityId: UserSystemAccessId
457
+ // - EntityValueBefore: Stringified representation of the record before deletion.
458
+ // - EntityValueAfter: null.
459
+ // Call the activity.create() method by passing:
460
+ // - dbTransaction
461
+ // - userId: loginUser.UserId.
462
+
463
+ //Instantiate new activity
464
+ const activity = new Activity();
465
+ activity.ActivityId = activity.createId();
466
+ activity.Action = ActionEnum.DELETE;
467
+ activity.Description = 'Delete User System Access';
468
+ activity.EntityType = 'UserSystemAccess';
469
+ activity.EntityId = UserSystemAccessId?.toString();
470
+ activity.EntityValueBefore = JSON.stringify(entityValueBefore);
471
+ activity.EntityValueAfter = JSON.stringify({});
472
+
473
+ //Call Activity.create method
474
+ await activity.create(loginUser.ObjectId, dbTransaction);
475
+ } catch (error) {
476
+ throw error;
477
+ }
478
+ }
304
479
  }