@tomei/sso 0.51.8 → 0.51.9

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.51.8",
3
+ "version": "0.51.9",
4
4
  "description": "Tomei SSO Package",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -1,3 +1,4 @@
1
+ import { Op, Transaction } from 'sequelize';
1
2
  import { ClassError, ObjectBase } from '@tomei/general';
2
3
  import { GroupReportingUserRepository } from './group-reporting-user.repository';
3
4
  import { IGroupReportingUserAttr } from '../../interfaces/group-reporting-user.interface';
@@ -6,7 +7,6 @@ import UserModel from '../../models/user.entity';
6
7
  import { Group } from '../group/group';
7
8
  import { ApplicationConfig } from '@tomei/config';
8
9
  import { ActionEnum, Activity } from '@tomei/activity-history';
9
- import { Transaction } from 'sequelize';
10
10
 
11
11
  export class GroupReportingUser extends ObjectBase {
12
12
  ObjectId: string;
@@ -216,6 +216,151 @@ export class GroupReportingUser extends ObjectBase {
216
216
  }
217
217
  }
218
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_CREATE',
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
+
219
364
  public static async findAllGroupReportingUsers(
220
365
  loginUser: User, //The authenticated user requesting the information.
221
366
  dbTransaction: any, //The database transaction to be used for this operation.