@tomei/sso 0.34.11 → 0.35.1

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.34.11",
3
+ "version": "0.35.1",
4
4
  "description": "Tomei SSO Package",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -2,7 +2,7 @@ import { ClassError, HashTable, ObjectBase } from '@tomei/general';
2
2
  import { SystemRepository } from './system.repository';
3
3
  import { ISystemAttr } from '../../interfaces/system.interface';
4
4
  import { LoginUser } from '../login-user/login-user';
5
- import { ApplicationConfig } from '@tomei/config';
5
+ import { ApplicationConfig, ComponentConfig } from '@tomei/config';
6
6
  import { ActionEnum, Activity } from '@tomei/activity-history';
7
7
  import { ISystemSearchAttr } from '../../interfaces/system-search-attr.interface';
8
8
  import { Op } from 'sequelize';
@@ -385,4 +385,71 @@ export class System extends ObjectBase {
385
385
  throw error;
386
386
  }
387
387
  }
388
+
389
+ public static async loadSystem(dbTransaction): Promise<string> {
390
+ try {
391
+ // Part 1: Retrieve System Info
392
+ // Load sso component config.loadComponentConfig Call Config. by passing:
393
+ // filepath: '/component-config/sso-config.json'
394
+ ComponentConfig.loadComponentConfig('./component-config/sso-config.json');
395
+
396
+ const config: {
397
+ name: string;
398
+ code: string;
399
+ description: string;
400
+ userId: string;
401
+ } = ComponentConfig.getComponentConfigValue('@tomei/sso', 'system');
402
+
403
+ // Make sure all required fields are provided in the config file.
404
+ if (
405
+ !config.name ||
406
+ !config.code ||
407
+ !config.description ||
408
+ !config.userId
409
+ ) {
410
+ throw new Error('Missing required fields in the config file.');
411
+ }
412
+
413
+ // Retrieve existing System. Call System._Repo findByPk method by passing:
414
+ // SystemCode: system.code
415
+ // dbTransaction
416
+ const system = await System._Repo.findByPk(config.code, {
417
+ transaction: dbTransaction,
418
+ });
419
+
420
+ // If system already exists, skip all steps below and return "System loaded."
421
+ if (system) {
422
+ return 'System loaded.';
423
+ }
424
+
425
+ //if system not exists. Call System._Repo create method by passing:
426
+ // SystemCode: system.code,
427
+ // Name: system.name
428
+ // Description: system.description,
429
+ // Status: 'Active',
430
+ // CreatedById: system.userId
431
+ // CreatedAt: current date & time
432
+ // UpdatedById: system.userId
433
+ // UpdatedAt: current date & time
434
+ await System._Repo.create(
435
+ {
436
+ SystemCode: config.code,
437
+ Name: config.name,
438
+ Description: config.description,
439
+ Status: 'Active',
440
+ CreatedById: config.userId,
441
+ CreatedAt: new Date(),
442
+ UpdatedById: config.userId,
443
+ UpdatedAt: new Date(),
444
+ },
445
+ {
446
+ transaction: dbTransaction,
447
+ },
448
+ );
449
+ // Return "System loaded."
450
+ return 'System loaded.';
451
+ } catch (error) {
452
+ throw error;
453
+ }
454
+ }
388
455
  }
@@ -3,7 +3,7 @@ import { SystemRepository } from '../system/system.repository';
3
3
  import { SystemPrivilegeRepository } from './system-privilege.repository';
4
4
  import { ISystemPrivilegeAttr } from '../../interfaces/system-privilege.interface';
5
5
  import { LoginUser } from '../login-user/login-user';
6
- import { ApplicationConfig } from '@tomei/config';
6
+ import { ApplicationConfig, ComponentConfig } from '@tomei/config';
7
7
  import { System } from '../system/system';
8
8
  import { ActionEnum, Activity } from '@tomei/activity-history';
9
9
  import { ISystemPrivilegeSearch } from '../../interfaces/system-privilege-search.interface';
@@ -289,4 +289,83 @@ export class SystemPrivilege extends ObjectBase {
289
289
  throw error;
290
290
  }
291
291
  }
292
+
293
+ public static async loadAllPrivileges(
294
+ dbTransaction: any,
295
+ systemCode: string,
296
+ ): Promise<string> {
297
+ try {
298
+ //Instantiate existing System by passing:
299
+ // dbTransaction
300
+ // SystemCode: Params.SystemCode
301
+ await System.init(dbTransaction, systemCode);
302
+
303
+ // Part 2: Load Privileges
304
+ // Load sso component config.loadComponentConfig Call Config. by passing:
305
+ // filepath: '/component-config/sso-config.json'
306
+
307
+ ComponentConfig.loadComponentConfig('./component-config/sso-config.json');
308
+
309
+ // Retrieve privileges array by call Config.getComponentConfigValue by passing:
310
+ // componentName: '@tomei/sso'
311
+ // configKey: 'privileges'
312
+ const privilegesConfig: {
313
+ privilegeCode: string;
314
+ description: string;
315
+ }[] = ComponentConfig.getComponentConfigValue('@tomei/sso', 'privileges');
316
+ // Retrieve system user id. Call Config.getComponentConfigValue by passing:
317
+ // componentName: '@tomei/sso'
318
+ // configKey: 'system'
319
+ const systemConfig: {
320
+ name: string;
321
+ code: string;
322
+ description: string;
323
+ userId: string;
324
+ } = ComponentConfig.getComponentConfigValue('@tomei/sso', 'system');
325
+
326
+ //Set systemUserId to system.userId.
327
+ const systemUserId = systemConfig.userId;
328
+ //Retrieve existing SystemPrivilege. Call SystemPrivilege._Repo findAll method by passing:
329
+ // where:
330
+ // SystemCode: Params.SystemCode
331
+ const existingSystemPrivileges = await this._Repository.findAll({
332
+ where: {
333
+ SystemCode: systemCode,
334
+ },
335
+ transaction: dbTransaction,
336
+ });
337
+
338
+ //Filter out existing privileges with the privileges array above to identify which privileges to be created and map it tobeCreatePrivileges.
339
+ const tobeCreatePrivileges = privilegesConfig.filter(
340
+ (privilegeConfig) =>
341
+ !existingSystemPrivileges.find(
342
+ (existingPrivilege) =>
343
+ existingPrivilege.PrivilegeCode === privilegeConfig.privilegeCode,
344
+ ),
345
+ );
346
+
347
+ //Call SystemPrivilege._Repo create method for each newPrivileges.
348
+ for (const privilegeConfig of tobeCreatePrivileges) {
349
+ await this._Repository.create(
350
+ {
351
+ PrivilegeCode: privilegeConfig.privilegeCode,
352
+ SystemCode: systemCode,
353
+ Description: privilegeConfig.description,
354
+ Status: 'Active',
355
+ CreatedById: parseInt(systemUserId),
356
+ UpdatedById: parseInt(systemUserId),
357
+ CreatedAt: new Date(),
358
+ UpdatedAt: new Date(),
359
+ },
360
+ {
361
+ transaction: dbTransaction,
362
+ },
363
+ );
364
+ }
365
+
366
+ return 'Privileges Loaded';
367
+ } catch (error) {
368
+ throw error;
369
+ }
370
+ }
292
371
  }