@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,456 +1,456 @@
1
- import { ClassError, HashTable, ObjectBase } from '@tomei/general';
2
- import { SystemRepository } from './system.repository';
3
- import { ISystemAttr } from '../../interfaces/system.interface';
4
- import { LoginUser } from '../login-user/login-user';
5
- import { ApplicationConfig, ComponentConfig } from '@tomei/config';
6
- import { ActionEnum, Activity } from '@tomei/activity-history';
7
- import { ISystemSearchAttr } from '../../interfaces/system-search-attr.interface';
8
- import { Op } from 'sequelize';
9
- import { v4 as uuidv4 } from 'uuid';
10
-
11
- export class System extends ObjectBase {
12
- ObjectId: string;
13
- ObjectName: string;
14
- TableName = 'sso_System';
15
- ObjectType = 'System';
16
- private static _htSystem: HashTable;
17
-
18
- SystemCode: string;
19
- Name: string;
20
- Description: string;
21
- AccessURL: string;
22
- GooglePlayURL: string;
23
- AppleStoreURL: string;
24
- APIKey: string;
25
- APISecret: string;
26
- Status: string;
27
- private _CreatedById: number;
28
- private _CreatedAt: Date;
29
- private _UpdatedById: number;
30
- private _UpdatedAt: Date;
31
- private static _Repo = new SystemRepository();
32
-
33
- get CreatedById(): number {
34
- return this._CreatedById;
35
- }
36
-
37
- get CreatedAt(): Date {
38
- return this._CreatedAt;
39
- }
40
-
41
- get UpdatedById(): number {
42
- return this._UpdatedById;
43
- }
44
-
45
- get UpdatedAt(): Date {
46
- return this._UpdatedAt;
47
- }
48
-
49
- private constructor(systemAttr?: ISystemAttr) {
50
- super();
51
- if (systemAttr) {
52
- this.SystemCode = systemAttr.SystemCode;
53
- this.Name = systemAttr.Name;
54
- this.Description = systemAttr?.Description;
55
- this.AccessURL = systemAttr?.AccessURL;
56
- this.GooglePlayURL = systemAttr?.GooglePlayURL;
57
- this.AppleStoreURL = systemAttr?.AppleStoreURL;
58
- this.APIKey = systemAttr?.APIKey;
59
- this.APISecret = systemAttr?.APISecret;
60
- this.Status = systemAttr?.Status;
61
- this._CreatedById = systemAttr.CreatedById;
62
- this._CreatedAt = systemAttr.CreatedAt;
63
- this._UpdatedById = systemAttr.UpdatedById;
64
- this._UpdatedAt = systemAttr.UpdatedAt;
65
- }
66
- }
67
-
68
- public static async init(dbTransaction: any, SystemCode?: string) {
69
- try {
70
- if (SystemCode) {
71
- const system = await System._Repo.findByPk(SystemCode, {
72
- transaction: dbTransaction,
73
- });
74
- if (system) {
75
- return new System(system);
76
- } else {
77
- throw new ClassError('System', 'SystemErrMsg00', 'System Not Found');
78
- }
79
- }
80
- return new System();
81
- } catch (error) {
82
- throw new ClassError(
83
- 'System',
84
- 'SystemErrMsg01',
85
- 'Failed To Initialize System',
86
- );
87
- }
88
- }
89
-
90
- public async createSystem(
91
- loginUser: LoginUser,
92
- dbTransaction: any,
93
- ): Promise<void> {
94
- try {
95
- //Creates a new system in the database.
96
- //Part 1: Check Privilege
97
- //Call loginUser.checkPrivilege() method to check if the user has the privilege to create a system.
98
- const systemCode =
99
- ApplicationConfig.getComponentConfigValue('system-code');
100
- const isPrivileged = await loginUser.checkPrivileges(
101
- systemCode,
102
- 'System - Create',
103
- );
104
- if (!isPrivileged) {
105
- throw new Error('You do not have permission to list UserGroup.');
106
- }
107
-
108
- //Part 2: Validate Input Params
109
- //If this._SystemCode is missing, throw ClassError
110
- if (!this.SystemCode) {
111
- throw new ClassError(
112
- 'System',
113
- 'SystemErrMsg02',
114
- 'SystemCode must have value.',
115
- );
116
- }
117
-
118
- //If this._Name missing, throw ClassError
119
- if (!this.Name) {
120
- throw new ClassError(
121
- 'System',
122
- 'SystemErrMsg03',
123
- 'Name must have value.',
124
- );
125
- }
126
-
127
- //If this._Description missing, throw ClassError
128
- if (!this.Description) {
129
- throw new ClassError(
130
- 'System',
131
- 'SystemErrMsg04',
132
- 'Description must have value.',
133
- );
134
- }
135
-
136
- //Part 3: Prepare to Insert Data
137
- //Set private properties
138
- this._CreatedById = loginUser.UserId;
139
- this._CreatedAt = new Date();
140
- this._UpdatedById = loginUser.UserId;
141
- this._UpdatedAt = new Date();
142
-
143
- //Call System._Repo create method
144
- await System._Repo.create(
145
- {
146
- SystemCode: this.SystemCode,
147
- Name: this.Name,
148
- Description: this.Description,
149
- AccessURL: this.AccessURL,
150
- GooglePlayURL: this.GooglePlayURL,
151
- AppleStoreURL: this.AppleStoreURL,
152
- APIKey: this.APIKey,
153
- APISecret: this.APISecret,
154
- Status: this.Status,
155
- CreatedById: this._CreatedById,
156
- CreatedAt: this._CreatedAt,
157
- UpdatedById: this._UpdatedById,
158
- UpdatedAt: this._UpdatedAt,
159
- },
160
- {
161
- transaction: dbTransaction,
162
- },
163
- );
164
-
165
- //Part 4: Record Create System Activity
166
- //Initialise EntityValueAfter variable and set to this class.
167
- const entityValueAfter = {
168
- SystemCode: this.SystemCode,
169
- Name: this.Name,
170
- Description: this.Description,
171
- AccessURL: this.AccessURL,
172
- GooglePlayURL: this.GooglePlayURL,
173
- AppleStoreURL: this.AppleStoreURL,
174
- APIKey: this.APIKey,
175
- APISecret: this.APISecret,
176
- Status: this.Status,
177
- };
178
-
179
- //Instantiate new activity from Activity class, call createId() method, then set the properties.
180
- const activity = new Activity();
181
- activity.ActivityId = activity.createId();
182
- activity.Action = ActionEnum.CREATE;
183
- activity.Description = 'Add System';
184
- activity.EntityType = 'System';
185
- activity.EntityId = this.SystemCode;
186
- activity.EntityValueBefore = JSON.stringify({});
187
- activity.EntityValueAfter = JSON.stringify(entityValueAfter);
188
- } catch (error) {
189
- throw error;
190
- }
191
- }
192
-
193
- protected static async checkDuplicateSystemCode(
194
- dbTransaction: any,
195
- systemCode: string,
196
- ): Promise<void> {
197
- //This method will make sure there is no duplicate system code.
198
- try {
199
- //Call System._Repo findOne() method by passing Params.SystemCode and dbTransaction, if SystemCode already exists, throw ClassError.
200
- const system = await System._Repo.findOne({
201
- where: {
202
- SystemCode: systemCode,
203
- },
204
- transaction: dbTransaction,
205
- });
206
-
207
- if (system) {
208
- throw new ClassError(
209
- 'System',
210
- 'SystemErrMsg05',
211
- 'System Code already exists.',
212
- );
213
- }
214
- } catch (error) {
215
- throw error;
216
- }
217
- }
218
-
219
- public async setSystemCode(
220
- dbTransaction: any,
221
- systemCode: string,
222
- ): Promise<void> {
223
- //Custom setter method for SystemCode
224
- try {
225
- //Call checkDuplicateSystemCode() method to make sure there is no duplicate system code.
226
- await System.checkDuplicateSystemCode(dbTransaction, systemCode);
227
- //Set this._SystemCode to Params.SystemCode
228
- this.SystemCode = systemCode;
229
- } catch (error) {
230
- throw error;
231
- }
232
- }
233
-
234
- public static async findAll(
235
- dbTransaction: any,
236
- loginUser: LoginUser,
237
- page?: number,
238
- rows?: number,
239
- search?: ISystemSearchAttr,
240
- ): Promise<{ count: number; systems: System[] }> {
241
- //This method list all system records based on filter.
242
- try {
243
- //Part 1: Retrieve listing
244
- const queryObj: any = {};
245
- const whereObj: any = {};
246
- if (search) {
247
- Object.entries(search).forEach(([key, value]) => {
248
- if (value) {
249
- queryObj[key] = {
250
- [Op.substring]: value,
251
- };
252
- }
253
- });
254
- }
255
-
256
- if (page && rows) {
257
- whereObj.offset = (page - 1) * rows;
258
- whereObj.limit = rows;
259
- }
260
-
261
- //Call System._Repo findAll() method by passing queryObj and whereObj
262
- const result = await System._Repo.findAllWithPagination({
263
- distinct: true,
264
- where: queryObj,
265
- ...whereObj,
266
- order: [['CreatedAt', 'DESC']],
267
- transaction: dbTransaction,
268
- });
269
-
270
- //Return result
271
- const systems = result.rows.map((system) => new System(system));
272
- return { count: result.count, systems };
273
- } catch (error) {
274
- throw error;
275
- }
276
- }
277
-
278
- public static async renewApiKeyAndSecret(
279
- loginUser: LoginUser,
280
- dbTransaction: any,
281
- systemCode: string,
282
- ) {
283
- try {
284
- //Part 1: Privilege Checking
285
- //Call loginUser.checkPrivilege() method to check if the user has the privilege to renew API Key and Secret.
286
- const sc = ApplicationConfig.getComponentConfigValue('system-code');
287
- const isPrivileged = await loginUser.checkPrivileges(sc, 'SYSTEM_UPDATE');
288
-
289
- if (!isPrivileged) {
290
- throw new ClassError(
291
- 'System',
292
- 'SystemErrMsg06',
293
- 'You do not have permission to renew API Key and Secret.',
294
- );
295
- }
296
-
297
- //Part 2: Validation
298
- //Instantiate existing System
299
- const system = await System.init(dbTransaction, systemCode);
300
-
301
- //Check if system.AccessURL got value. If not, throw new ClassError
302
- if (!system.AccessURL) {
303
- throw new ClassError(
304
- 'System',
305
- 'SystemErrMsg07',
306
- 'AccessURL is required for callback',
307
- );
308
- }
309
-
310
- //Check if system.Status is "Active". If not, throw new ClassError
311
- if (system.Status !== 'Active') {
312
- throw new ClassError(
313
- 'System',
314
- 'SystemErrMsg08',
315
- 'Cannot do this operation on inactive system.',
316
- );
317
- }
318
-
319
- //Set EntityValueBefore to system instance.
320
- const entityValueBefore = {
321
- SystemCode: system.SystemCode,
322
- Name: system.Name,
323
- Description: system.Description,
324
- AccessURL: system.AccessURL,
325
- GooglePlayURL: system.GooglePlayURL,
326
- AppleStoreURL: system.AppleStoreURL,
327
- APIKey: system.APIKey,
328
- APISecret: system.APISecret,
329
- Status: system.Status,
330
- };
331
-
332
- //Part 3: Generate API key and secret
333
- //Use https://www.npmjs.com/package/uuid package to generate both the api key and api secret.
334
- const apiKey = uuidv4();
335
- const apiSecret = uuidv4();
336
-
337
- //Update the system instance with new API key and secret.
338
- system.APIKey = apiKey;
339
- system.APISecret = apiSecret;
340
- system._UpdatedById = loginUser.UserId;
341
- system._UpdatedAt = new Date();
342
- //Call System._Repo update() method to update the system record.
343
- await System._Repo.update(
344
- {
345
- APIKey: apiKey,
346
- APISecret: apiSecret,
347
- UpdatedById: system._UpdatedById,
348
- UpdatedAt: system._UpdatedAt,
349
- },
350
- {
351
- where: {
352
- SystemCode: systemCode,
353
- },
354
- transaction: dbTransaction,
355
- },
356
- );
357
-
358
- //Part 4: Record Renew API Key and Secret Activity
359
- //Set EntityValueAfter to system instance.
360
- const entityValueAfter = {
361
- SystemCode: system.SystemCode,
362
- Name: system.Name,
363
- Description: system.Description,
364
- AccessURL: system.AccessURL,
365
- GooglePlayURL: system.GooglePlayURL,
366
- AppleStoreURL: system.AppleStoreURL,
367
- APIKey: system.APIKey,
368
- APISecret: system.APISecret,
369
- Status: system.Status,
370
- };
371
-
372
- //Instantiate new activity from Activity class, call createId() method, then set the properties.
373
- const activity = new Activity();
374
- activity.ActivityId = activity.createId();
375
- activity.Action = ActionEnum.UPDATE;
376
- activity.Description = 'Renew API key and secret for a system';
377
- activity.EntityType = 'System';
378
- activity.EntityId = system.SystemCode;
379
- activity.EntityValueBefore = JSON.stringify(entityValueBefore);
380
- activity.EntityValueAfter = JSON.stringify(entityValueAfter);
381
-
382
- await activity.create(loginUser.ObjectId, dbTransaction);
383
- //Return the updated system instance.
384
- return system;
385
- } catch (error) {
386
- throw error;
387
- }
388
- }
389
-
390
- public static async loadSystem(dbTransaction): Promise<string> {
391
- try {
392
- // Part 1: Retrieve System Info
393
- // Load sso component config.loadComponentConfig Call Config. by passing:
394
- // filepath: '/component-config/sso-config.json'
395
- ComponentConfig.loadComponentConfig('./component-config/sso-config.json');
396
-
397
- const config: {
398
- name: string;
399
- code: string;
400
- description: string;
401
- userId: string;
402
- } = ComponentConfig.getComponentConfigValue('@tomei/sso', 'system');
403
-
404
- // Make sure all required fields are provided in the config file.
405
- if (
406
- !config.name ||
407
- !config.code ||
408
- !config.description ||
409
- !config.userId
410
- ) {
411
- throw new Error('Missing required fields in the config file.');
412
- }
413
-
414
- // Retrieve existing System. Call System._Repo findByPk method by passing:
415
- // SystemCode: system.code
416
- // dbTransaction
417
- const system = await System._Repo.findByPk(config.code, {
418
- transaction: dbTransaction,
419
- });
420
-
421
- // If system already exists, skip all steps below and return "System loaded."
422
- if (system) {
423
- return 'System loaded.';
424
- }
425
-
426
- //if system not exists. Call System._Repo create method by passing:
427
- // SystemCode: system.code,
428
- // Name: system.name
429
- // Description: system.description,
430
- // Status: 'Active',
431
- // CreatedById: system.userId
432
- // CreatedAt: current date & time
433
- // UpdatedById: system.userId
434
- // UpdatedAt: current date & time
435
- await System._Repo.create(
436
- {
437
- SystemCode: config.code,
438
- Name: config.name,
439
- Description: config.description,
440
- Status: 'Active',
441
- CreatedById: config.userId,
442
- CreatedAt: new Date(),
443
- UpdatedById: config.userId,
444
- UpdatedAt: new Date(),
445
- },
446
- {
447
- transaction: dbTransaction,
448
- },
449
- );
450
- // Return "System loaded."
451
- return 'System loaded.';
452
- } catch (error) {
453
- throw error;
454
- }
455
- }
456
- }
1
+ import { ClassError, HashTable, ObjectBase } from '@tomei/general';
2
+ import { SystemRepository } from './system.repository';
3
+ import { ISystemAttr } from '../../interfaces/system.interface';
4
+ import { LoginUser } from '../login-user/login-user';
5
+ import { ApplicationConfig, ComponentConfig } from '@tomei/config';
6
+ import { ActionEnum, Activity } from '@tomei/activity-history';
7
+ import { ISystemSearchAttr } from '../../interfaces/system-search-attr.interface';
8
+ import { Op } from 'sequelize';
9
+ import { v4 as uuidv4 } from 'uuid';
10
+
11
+ export class System extends ObjectBase {
12
+ ObjectId: string;
13
+ ObjectName: string;
14
+ TableName = 'sso_System';
15
+ ObjectType = 'System';
16
+ private static _htSystem: HashTable;
17
+
18
+ SystemCode: string;
19
+ Name: string;
20
+ Description: string;
21
+ AccessURL: string;
22
+ GooglePlayURL: string;
23
+ AppleStoreURL: string;
24
+ APIKey: string;
25
+ APISecret: string;
26
+ Status: string;
27
+ private _CreatedById: number;
28
+ private _CreatedAt: Date;
29
+ private _UpdatedById: number;
30
+ private _UpdatedAt: Date;
31
+ private static _Repo = new SystemRepository();
32
+
33
+ get CreatedById(): number {
34
+ return this._CreatedById;
35
+ }
36
+
37
+ get CreatedAt(): Date {
38
+ return this._CreatedAt;
39
+ }
40
+
41
+ get UpdatedById(): number {
42
+ return this._UpdatedById;
43
+ }
44
+
45
+ get UpdatedAt(): Date {
46
+ return this._UpdatedAt;
47
+ }
48
+
49
+ private constructor(systemAttr?: ISystemAttr) {
50
+ super();
51
+ if (systemAttr) {
52
+ this.SystemCode = systemAttr.SystemCode;
53
+ this.Name = systemAttr.Name;
54
+ this.Description = systemAttr?.Description;
55
+ this.AccessURL = systemAttr?.AccessURL;
56
+ this.GooglePlayURL = systemAttr?.GooglePlayURL;
57
+ this.AppleStoreURL = systemAttr?.AppleStoreURL;
58
+ this.APIKey = systemAttr?.APIKey;
59
+ this.APISecret = systemAttr?.APISecret;
60
+ this.Status = systemAttr?.Status;
61
+ this._CreatedById = systemAttr.CreatedById;
62
+ this._CreatedAt = systemAttr.CreatedAt;
63
+ this._UpdatedById = systemAttr.UpdatedById;
64
+ this._UpdatedAt = systemAttr.UpdatedAt;
65
+ }
66
+ }
67
+
68
+ public static async init(dbTransaction: any, SystemCode?: string) {
69
+ try {
70
+ if (SystemCode) {
71
+ const system = await System._Repo.findByPk(SystemCode, {
72
+ transaction: dbTransaction,
73
+ });
74
+ if (system) {
75
+ return new System(system);
76
+ } else {
77
+ throw new ClassError('System', 'SystemErrMsg00', 'System Not Found');
78
+ }
79
+ }
80
+ return new System();
81
+ } catch (error) {
82
+ throw new ClassError(
83
+ 'System',
84
+ 'SystemErrMsg01',
85
+ 'Failed To Initialize System',
86
+ );
87
+ }
88
+ }
89
+
90
+ public async createSystem(
91
+ loginUser: LoginUser,
92
+ dbTransaction: any,
93
+ ): Promise<void> {
94
+ try {
95
+ //Creates a new system in the database.
96
+ //Part 1: Check Privilege
97
+ //Call loginUser.checkPrivilege() method to check if the user has the privilege to create a system.
98
+ const systemCode =
99
+ ApplicationConfig.getComponentConfigValue('system-code');
100
+ const isPrivileged = await loginUser.checkPrivileges(
101
+ systemCode,
102
+ 'System - Create',
103
+ );
104
+ if (!isPrivileged) {
105
+ throw new Error('You do not have permission to list UserGroup.');
106
+ }
107
+
108
+ //Part 2: Validate Input Params
109
+ //If this._SystemCode is missing, throw ClassError
110
+ if (!this.SystemCode) {
111
+ throw new ClassError(
112
+ 'System',
113
+ 'SystemErrMsg02',
114
+ 'SystemCode must have value.',
115
+ );
116
+ }
117
+
118
+ //If this._Name missing, throw ClassError
119
+ if (!this.Name) {
120
+ throw new ClassError(
121
+ 'System',
122
+ 'SystemErrMsg03',
123
+ 'Name must have value.',
124
+ );
125
+ }
126
+
127
+ //If this._Description missing, throw ClassError
128
+ if (!this.Description) {
129
+ throw new ClassError(
130
+ 'System',
131
+ 'SystemErrMsg04',
132
+ 'Description must have value.',
133
+ );
134
+ }
135
+
136
+ //Part 3: Prepare to Insert Data
137
+ //Set private properties
138
+ this._CreatedById = loginUser.UserId;
139
+ this._CreatedAt = new Date();
140
+ this._UpdatedById = loginUser.UserId;
141
+ this._UpdatedAt = new Date();
142
+
143
+ //Call System._Repo create method
144
+ await System._Repo.create(
145
+ {
146
+ SystemCode: this.SystemCode,
147
+ Name: this.Name,
148
+ Description: this.Description,
149
+ AccessURL: this.AccessURL,
150
+ GooglePlayURL: this.GooglePlayURL,
151
+ AppleStoreURL: this.AppleStoreURL,
152
+ APIKey: this.APIKey,
153
+ APISecret: this.APISecret,
154
+ Status: this.Status,
155
+ CreatedById: this._CreatedById,
156
+ CreatedAt: this._CreatedAt,
157
+ UpdatedById: this._UpdatedById,
158
+ UpdatedAt: this._UpdatedAt,
159
+ },
160
+ {
161
+ transaction: dbTransaction,
162
+ },
163
+ );
164
+
165
+ //Part 4: Record Create System Activity
166
+ //Initialise EntityValueAfter variable and set to this class.
167
+ const entityValueAfter = {
168
+ SystemCode: this.SystemCode,
169
+ Name: this.Name,
170
+ Description: this.Description,
171
+ AccessURL: this.AccessURL,
172
+ GooglePlayURL: this.GooglePlayURL,
173
+ AppleStoreURL: this.AppleStoreURL,
174
+ APIKey: this.APIKey,
175
+ APISecret: this.APISecret,
176
+ Status: this.Status,
177
+ };
178
+
179
+ //Instantiate new activity from Activity class, call createId() method, then set the properties.
180
+ const activity = new Activity();
181
+ activity.ActivityId = activity.createId();
182
+ activity.Action = ActionEnum.CREATE;
183
+ activity.Description = 'Add System';
184
+ activity.EntityType = 'System';
185
+ activity.EntityId = this.SystemCode;
186
+ activity.EntityValueBefore = JSON.stringify({});
187
+ activity.EntityValueAfter = JSON.stringify(entityValueAfter);
188
+ } catch (error) {
189
+ throw error;
190
+ }
191
+ }
192
+
193
+ protected static async checkDuplicateSystemCode(
194
+ dbTransaction: any,
195
+ systemCode: string,
196
+ ): Promise<void> {
197
+ //This method will make sure there is no duplicate system code.
198
+ try {
199
+ //Call System._Repo findOne() method by passing Params.SystemCode and dbTransaction, if SystemCode already exists, throw ClassError.
200
+ const system = await System._Repo.findOne({
201
+ where: {
202
+ SystemCode: systemCode,
203
+ },
204
+ transaction: dbTransaction,
205
+ });
206
+
207
+ if (system) {
208
+ throw new ClassError(
209
+ 'System',
210
+ 'SystemErrMsg05',
211
+ 'System Code already exists.',
212
+ );
213
+ }
214
+ } catch (error) {
215
+ throw error;
216
+ }
217
+ }
218
+
219
+ public async setSystemCode(
220
+ dbTransaction: any,
221
+ systemCode: string,
222
+ ): Promise<void> {
223
+ //Custom setter method for SystemCode
224
+ try {
225
+ //Call checkDuplicateSystemCode() method to make sure there is no duplicate system code.
226
+ await System.checkDuplicateSystemCode(dbTransaction, systemCode);
227
+ //Set this._SystemCode to Params.SystemCode
228
+ this.SystemCode = systemCode;
229
+ } catch (error) {
230
+ throw error;
231
+ }
232
+ }
233
+
234
+ public static async findAll(
235
+ dbTransaction: any,
236
+ loginUser: LoginUser,
237
+ page?: number,
238
+ rows?: number,
239
+ search?: ISystemSearchAttr,
240
+ ): Promise<{ count: number; systems: System[] }> {
241
+ //This method list all system records based on filter.
242
+ try {
243
+ //Part 1: Retrieve listing
244
+ const queryObj: any = {};
245
+ const whereObj: any = {};
246
+ if (search) {
247
+ Object.entries(search).forEach(([key, value]) => {
248
+ if (value) {
249
+ queryObj[key] = {
250
+ [Op.substring]: value,
251
+ };
252
+ }
253
+ });
254
+ }
255
+
256
+ if (page && rows) {
257
+ whereObj.offset = (page - 1) * rows;
258
+ whereObj.limit = rows;
259
+ }
260
+
261
+ //Call System._Repo findAll() method by passing queryObj and whereObj
262
+ const result = await System._Repo.findAllWithPagination({
263
+ distinct: true,
264
+ where: queryObj,
265
+ ...whereObj,
266
+ order: [['CreatedAt', 'DESC']],
267
+ transaction: dbTransaction,
268
+ });
269
+
270
+ //Return result
271
+ const systems = result.rows.map((system) => new System(system));
272
+ return { count: result.count, systems };
273
+ } catch (error) {
274
+ throw error;
275
+ }
276
+ }
277
+
278
+ public static async renewApiKeyAndSecret(
279
+ loginUser: LoginUser,
280
+ dbTransaction: any,
281
+ systemCode: string,
282
+ ) {
283
+ try {
284
+ //Part 1: Privilege Checking
285
+ //Call loginUser.checkPrivilege() method to check if the user has the privilege to renew API Key and Secret.
286
+ const sc = ApplicationConfig.getComponentConfigValue('system-code');
287
+ const isPrivileged = await loginUser.checkPrivileges(sc, 'SYSTEM_UPDATE');
288
+
289
+ if (!isPrivileged) {
290
+ throw new ClassError(
291
+ 'System',
292
+ 'SystemErrMsg06',
293
+ 'You do not have permission to renew API Key and Secret.',
294
+ );
295
+ }
296
+
297
+ //Part 2: Validation
298
+ //Instantiate existing System
299
+ const system = await System.init(dbTransaction, systemCode);
300
+
301
+ //Check if system.AccessURL got value. If not, throw new ClassError
302
+ if (!system.AccessURL) {
303
+ throw new ClassError(
304
+ 'System',
305
+ 'SystemErrMsg07',
306
+ 'AccessURL is required for callback',
307
+ );
308
+ }
309
+
310
+ //Check if system.Status is "Active". If not, throw new ClassError
311
+ if (system.Status !== 'Active') {
312
+ throw new ClassError(
313
+ 'System',
314
+ 'SystemErrMsg08',
315
+ 'Cannot do this operation on inactive system.',
316
+ );
317
+ }
318
+
319
+ //Set EntityValueBefore to system instance.
320
+ const entityValueBefore = {
321
+ SystemCode: system.SystemCode,
322
+ Name: system.Name,
323
+ Description: system.Description,
324
+ AccessURL: system.AccessURL,
325
+ GooglePlayURL: system.GooglePlayURL,
326
+ AppleStoreURL: system.AppleStoreURL,
327
+ APIKey: system.APIKey,
328
+ APISecret: system.APISecret,
329
+ Status: system.Status,
330
+ };
331
+
332
+ //Part 3: Generate API key and secret
333
+ //Use https://www.npmjs.com/package/uuid package to generate both the api key and api secret.
334
+ const apiKey = uuidv4();
335
+ const apiSecret = uuidv4();
336
+
337
+ //Update the system instance with new API key and secret.
338
+ system.APIKey = apiKey;
339
+ system.APISecret = apiSecret;
340
+ system._UpdatedById = loginUser.UserId;
341
+ system._UpdatedAt = new Date();
342
+ //Call System._Repo update() method to update the system record.
343
+ await System._Repo.update(
344
+ {
345
+ APIKey: apiKey,
346
+ APISecret: apiSecret,
347
+ UpdatedById: system._UpdatedById,
348
+ UpdatedAt: system._UpdatedAt,
349
+ },
350
+ {
351
+ where: {
352
+ SystemCode: systemCode,
353
+ },
354
+ transaction: dbTransaction,
355
+ },
356
+ );
357
+
358
+ //Part 4: Record Renew API Key and Secret Activity
359
+ //Set EntityValueAfter to system instance.
360
+ const entityValueAfter = {
361
+ SystemCode: system.SystemCode,
362
+ Name: system.Name,
363
+ Description: system.Description,
364
+ AccessURL: system.AccessURL,
365
+ GooglePlayURL: system.GooglePlayURL,
366
+ AppleStoreURL: system.AppleStoreURL,
367
+ APIKey: system.APIKey,
368
+ APISecret: system.APISecret,
369
+ Status: system.Status,
370
+ };
371
+
372
+ //Instantiate new activity from Activity class, call createId() method, then set the properties.
373
+ const activity = new Activity();
374
+ activity.ActivityId = activity.createId();
375
+ activity.Action = ActionEnum.UPDATE;
376
+ activity.Description = 'Renew API key and secret for a system';
377
+ activity.EntityType = 'System';
378
+ activity.EntityId = system.SystemCode;
379
+ activity.EntityValueBefore = JSON.stringify(entityValueBefore);
380
+ activity.EntityValueAfter = JSON.stringify(entityValueAfter);
381
+
382
+ await activity.create(loginUser.ObjectId, dbTransaction);
383
+ //Return the updated system instance.
384
+ return system;
385
+ } catch (error) {
386
+ throw error;
387
+ }
388
+ }
389
+
390
+ public static async loadSystem(dbTransaction): Promise<string> {
391
+ try {
392
+ // Part 1: Retrieve System Info
393
+ // Load sso component config.loadComponentConfig Call Config. by passing:
394
+ // filepath: '/component-config/sso-config.json'
395
+ ComponentConfig.loadComponentConfig('./component-config/sso-config.json');
396
+
397
+ const config: {
398
+ name: string;
399
+ code: string;
400
+ description: string;
401
+ userId: string;
402
+ } = ComponentConfig.getComponentConfigValue('@tomei/sso', 'system');
403
+
404
+ // Make sure all required fields are provided in the config file.
405
+ if (
406
+ !config.name ||
407
+ !config.code ||
408
+ !config.description ||
409
+ !config.userId
410
+ ) {
411
+ throw new Error('Missing required fields in the config file.');
412
+ }
413
+
414
+ // Retrieve existing System. Call System._Repo findByPk method by passing:
415
+ // SystemCode: system.code
416
+ // dbTransaction
417
+ const system = await System._Repo.findByPk(config.code, {
418
+ transaction: dbTransaction,
419
+ });
420
+
421
+ // If system already exists, skip all steps below and return "System loaded."
422
+ if (system) {
423
+ return 'System loaded.';
424
+ }
425
+
426
+ //if system not exists. Call System._Repo create method by passing:
427
+ // SystemCode: system.code,
428
+ // Name: system.name
429
+ // Description: system.description,
430
+ // Status: 'Active',
431
+ // CreatedById: system.userId
432
+ // CreatedAt: current date & time
433
+ // UpdatedById: system.userId
434
+ // UpdatedAt: current date & time
435
+ await System._Repo.create(
436
+ {
437
+ SystemCode: config.code,
438
+ Name: config.name,
439
+ Description: config.description,
440
+ Status: 'Active',
441
+ CreatedById: config.userId,
442
+ CreatedAt: new Date(),
443
+ UpdatedById: config.userId,
444
+ UpdatedAt: new Date(),
445
+ },
446
+ {
447
+ transaction: dbTransaction,
448
+ },
449
+ );
450
+ // Return "System loaded."
451
+ return 'System loaded.';
452
+ } catch (error) {
453
+ throw error;
454
+ }
455
+ }
456
+ }