@tomei/sso 0.61.0 → 0.62.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.
Files changed (257) hide show
  1. package/.commitlintrc.json +22 -22
  2. package/.gitlab-ci.yml +16 -16
  3. package/.husky/commit-msg +15 -15
  4. package/.husky/pre-commit +7 -7
  5. package/.prettierrc +4 -4
  6. package/Jenkinsfile +57 -57
  7. package/README.md +23 -23
  8. package/__tests__/unit/components/group/group.spec.ts +79 -79
  9. package/__tests__/unit/components/group-object-privilege/group-object-privilege.spec.ts +88 -88
  10. package/__tests__/unit/components/group-privilege/group-privilege.spec.ts +68 -68
  11. package/__tests__/unit/components/group-reporting-user/group-reporting-user.spec.ts +66 -66
  12. package/__tests__/unit/components/group-system-access/group-system-access.spec.ts +83 -83
  13. package/__tests__/unit/components/login-user/l.spec.ts +746 -746
  14. package/__tests__/unit/components/login-user/login.spec.ts +1164 -1164
  15. package/__tests__/unit/components/password-hash/password-hash.service.spec.ts +31 -31
  16. package/__tests__/unit/components/system/system.spec.ts +254 -254
  17. package/__tests__/unit/components/system-privilege/system-privilege.spec.ts +83 -83
  18. package/__tests__/unit/components/user-group/user-group.spec.ts +86 -86
  19. package/__tests__/unit/components/user-object-privilege/user-object-privilege.spec.ts +78 -78
  20. package/__tests__/unit/components/user-privilege/user-privilege.spec.ts +72 -72
  21. package/__tests__/unit/components/user-system-access/user-system-access.spec.ts +89 -89
  22. package/__tests__/unit/redis-client/redis.service.spec.ts +23 -23
  23. package/__tests__/unit/session/session.service.spec.ts +47 -47
  24. package/__tests__/unit/system-privilege/system-privilage.spec.ts +91 -91
  25. package/coverage/clover.xml +1452 -1452
  26. package/coverage/coverage-final.json +47 -47
  27. package/coverage/lcov-report/base.css +224 -224
  28. package/coverage/lcov-report/block-navigation.js +87 -87
  29. package/coverage/lcov-report/components/group/group.repository.ts.html +117 -117
  30. package/coverage/lcov-report/components/group/group.ts.html +327 -327
  31. package/coverage/lcov-report/components/group/index.html +130 -130
  32. package/coverage/lcov-report/components/group-object-privilege/group-object-privilege.repository.ts.html +117 -117
  33. package/coverage/lcov-report/components/group-object-privilege/group-object-privilege.ts.html +321 -321
  34. package/coverage/lcov-report/components/group-object-privilege/index.html +130 -130
  35. package/coverage/lcov-report/components/group-privilege/group-privilege.repository.ts.html +117 -117
  36. package/coverage/lcov-report/components/group-privilege/group-privilege.ts.html +303 -303
  37. package/coverage/lcov-report/components/group-privilege/index.html +130 -130
  38. package/coverage/lcov-report/components/group-reporting-user/group-reporting-user.repository.ts.html +117 -117
  39. package/coverage/lcov-report/components/group-reporting-user/group-reporting-user.ts.html +327 -327
  40. package/coverage/lcov-report/components/group-reporting-user/index.html +130 -130
  41. package/coverage/lcov-report/components/group-system-access/group-system-access.repository.ts.html +117 -117
  42. package/coverage/lcov-report/components/group-system-access/group-system-access.ts.html +309 -309
  43. package/coverage/lcov-report/components/group-system-access/index.html +130 -130
  44. package/coverage/lcov-report/components/login-history/index.html +115 -115
  45. package/coverage/lcov-report/components/login-history/login-history.repository.ts.html +117 -117
  46. package/coverage/lcov-report/components/login-user/index.html +130 -130
  47. package/coverage/lcov-report/components/login-user/login-user.ts.html +5007 -5007
  48. package/coverage/lcov-report/components/login-user/user.repository.ts.html +117 -117
  49. package/coverage/lcov-report/components/password-hash/index.html +115 -115
  50. package/coverage/lcov-report/components/password-hash/password-hash.service.ts.html +126 -126
  51. package/coverage/lcov-report/components/system/index.html +130 -130
  52. package/coverage/lcov-report/components/system/system.repository.ts.html +117 -117
  53. package/coverage/lcov-report/components/system/system.ts.html +909 -909
  54. package/coverage/lcov-report/components/system-privilege/index.html +130 -130
  55. package/coverage/lcov-report/components/system-privilege/system-privilege.repository.ts.html +120 -120
  56. package/coverage/lcov-report/components/system-privilege/system-privilege.ts.html +390 -390
  57. package/coverage/lcov-report/components/user-group/index.html +130 -130
  58. package/coverage/lcov-report/components/user-group/user-group.repository.ts.html +117 -117
  59. package/coverage/lcov-report/components/user-group/user-group.ts.html +354 -354
  60. package/coverage/lcov-report/components/user-object-privilege/index.html +130 -130
  61. package/coverage/lcov-report/components/user-object-privilege/user-object-privilege.repository.ts.html +117 -117
  62. package/coverage/lcov-report/components/user-object-privilege/user-object-privilege.ts.html +312 -312
  63. package/coverage/lcov-report/components/user-privilege/index.html +130 -130
  64. package/coverage/lcov-report/components/user-privilege/user-privilege.repository.ts.html +117 -117
  65. package/coverage/lcov-report/components/user-privilege/user-privilege.ts.html +306 -306
  66. package/coverage/lcov-report/components/user-system-access/index.html +130 -130
  67. package/coverage/lcov-report/components/user-system-access/user-system-access.repository.ts.html +117 -117
  68. package/coverage/lcov-report/components/user-system-access/user-system-access.ts.html +312 -312
  69. package/coverage/lcov-report/enum/group-type.enum.ts.html +108 -108
  70. package/coverage/lcov-report/enum/index.html +160 -160
  71. package/coverage/lcov-report/enum/index.ts.html +93 -93
  72. package/coverage/lcov-report/enum/user-status.enum.ts.html +105 -105
  73. package/coverage/lcov-report/enum/yn.enum.ts.html +96 -96
  74. package/coverage/lcov-report/index.html +370 -370
  75. package/coverage/lcov-report/models/group-object-privilege.entity.ts.html +333 -333
  76. package/coverage/lcov-report/models/group-privilege.entity.ts.html +315 -315
  77. package/coverage/lcov-report/models/group-reporting-user.entity.ts.html +339 -339
  78. package/coverage/lcov-report/models/group-system-access.entity.ts.html +324 -324
  79. package/coverage/lcov-report/models/group.entity.ts.html +435 -435
  80. package/coverage/lcov-report/models/index.html +310 -310
  81. package/coverage/lcov-report/models/login-history.entity.ts.html +252 -252
  82. package/coverage/lcov-report/models/staff.entity.ts.html +411 -411
  83. package/coverage/lcov-report/models/system-privilege.entity.ts.html +354 -354
  84. package/coverage/lcov-report/models/system.entity.ts.html +423 -423
  85. package/coverage/lcov-report/models/user-group.entity.ts.html +354 -354
  86. package/coverage/lcov-report/models/user-object-privilege.entity.ts.html +330 -330
  87. package/coverage/lcov-report/models/user-privilege.entity.ts.html +315 -315
  88. package/coverage/lcov-report/models/user-system-access.entity.ts.html +315 -315
  89. package/coverage/lcov-report/models/user.entity.ts.html +522 -522
  90. package/coverage/lcov-report/prettify.css +1 -1
  91. package/coverage/lcov-report/prettify.js +2 -2
  92. package/coverage/lcov-report/redis-client/index.html +115 -115
  93. package/coverage/lcov-report/redis-client/redis.service.ts.html +240 -240
  94. package/coverage/lcov-report/session/index.html +115 -115
  95. package/coverage/lcov-report/session/session.service.ts.html +246 -246
  96. package/coverage/lcov-report/sorter.js +196 -196
  97. package/coverage/lcov.info +2490 -2490
  98. package/coverage/test-report.xml +128 -128
  99. package/create-sso-user.sql +39 -39
  100. package/dist/src/components/login-history/login-history.d.ts +23 -0
  101. package/dist/src/components/login-history/login-history.js +88 -0
  102. package/dist/src/components/login-history/login-history.js.map +1 -0
  103. package/dist/src/components/login-user/user.js +4 -3
  104. package/dist/src/components/login-user/user.js.map +1 -1
  105. package/dist/src/interfaces/login-history-search-attr.interface.d.ts +8 -0
  106. package/dist/src/interfaces/login-history-search-attr.interface.js +3 -0
  107. package/dist/src/interfaces/login-history-search-attr.interface.js.map +1 -0
  108. package/dist/src/interfaces/login-history.interface.d.ts +11 -0
  109. package/dist/src/interfaces/login-history.interface.js +3 -0
  110. package/dist/src/interfaces/login-history.interface.js.map +1 -0
  111. package/dist/tsconfig.tsbuildinfo +1 -1
  112. package/eslint.config.mjs +58 -58
  113. package/jest.config.js +14 -14
  114. package/migrations/20240314080602-create-user-table.js +124 -124
  115. package/migrations/20240314080603-create-user-group-table.js +85 -85
  116. package/migrations/20240314080604-create-user-user-group-table.js +55 -55
  117. package/migrations/20240314080605-create-login-history-table.js +53 -53
  118. package/migrations/20240527064925-create-system-table.js +78 -78
  119. package/migrations/20240527064926-create-system-privilege-table.js +71 -71
  120. package/migrations/20240527065342-create-group-table.js +93 -93
  121. package/migrations/20240527065633-create-group-reporting-user-table.js +76 -76
  122. package/migrations/20240528011551-create-group-system-access-table.js +72 -72
  123. package/migrations/20240528023018-user-system-access-table.js +75 -75
  124. package/migrations/20240528032229-user-privilege-table.js +76 -76
  125. package/migrations/20240528063003-create-group-privilege-table.js +76 -76
  126. package/migrations/20240528063051-create-group-object-privilege-table.js +84 -84
  127. package/migrations/20240528063107-create-user-object-privilege-table.js +84 -84
  128. package/migrations/20240528063108-create-api-key-table.js +85 -85
  129. package/migrations/20241104104802-create-building-table.js +95 -95
  130. package/migrations/20250108091132-add-area-manager-user-id-to-building-table.js +14 -14
  131. package/migrations/20250108091133-add-passcode-to-user-table.js +36 -36
  132. package/migrations/20250210115636-create-user-reporting-hierarchy.js +76 -76
  133. package/migrations/20250326043818-crate-user-password-history.js +42 -42
  134. package/package.json +90 -90
  135. package/sampledotenv +7 -7
  136. package/sonar-project.properties +22 -22
  137. package/src/components/api-key/api-key.repository.ts +15 -15
  138. package/src/components/api-key/api-key.ts +448 -448
  139. package/src/components/api-key/index.ts +4 -4
  140. package/src/components/building/building.repository.ts +27 -27
  141. package/src/components/building/index.ts +2 -2
  142. package/src/components/group/group.repository.ts +26 -26
  143. package/src/components/group/group.ts +2284 -2284
  144. package/src/components/group/index.ts +3 -3
  145. package/src/components/group-object-privilege/group-object-privilege.repository.ts +25 -25
  146. package/src/components/group-object-privilege/group-object-privilege.ts +278 -278
  147. package/src/components/group-object-privilege/index.ts +2 -2
  148. package/src/components/group-privilege/group-privilege.repository.ts +29 -29
  149. package/src/components/group-privilege/group-privilege.ts +84 -84
  150. package/src/components/group-privilege/index.ts +2 -2
  151. package/src/components/group-reporting-user/group-reporting-user.repository.ts +23 -23
  152. package/src/components/group-reporting-user/group-reporting-user.ts +506 -506
  153. package/src/components/group-reporting-user/index.ts +3 -3
  154. package/src/components/group-system-access/group-system-access.repository.ts +43 -43
  155. package/src/components/group-system-access/group-system-access.ts +90 -90
  156. package/src/components/group-system-access/index.ts +2 -2
  157. package/src/components/index.ts +20 -20
  158. package/src/components/login-history/index.ts +1 -1
  159. package/src/components/login-history/login-history.repository.ts +11 -11
  160. package/src/components/login-user/index.ts +5 -5
  161. package/src/components/login-user/interfaces/check-user-info-duplicated.interface.ts +7 -7
  162. package/src/components/login-user/interfaces/index.ts +1 -1
  163. package/src/components/login-user/interfaces/system-access.interface.ts +13 -13
  164. package/src/components/login-user/interfaces/user-info.interface.ts +34 -34
  165. package/src/components/login-user/login-user.ts +362 -362
  166. package/src/components/login-user/user.repository.ts +11 -11
  167. package/src/components/login-user/user.ts +3143 -3142
  168. package/src/components/password-hash/index.ts +2 -2
  169. package/src/components/password-hash/interfaces/index.ts +1 -1
  170. package/src/components/password-hash/interfaces/password-hash-service.interface.ts +4 -4
  171. package/src/components/password-hash/password-hash.service.ts +14 -14
  172. package/src/components/staff/index.ts +2 -2
  173. package/src/components/staff/staff.repository.ts +27 -27
  174. package/src/components/system/index.ts +3 -3
  175. package/src/components/system/system.repository.ts +11 -11
  176. package/src/components/system/system.ts +456 -456
  177. package/src/components/system-privilege/index.ts +4 -4
  178. package/src/components/system-privilege/system-privilege.repository.ts +18 -18
  179. package/src/components/system-privilege/system-privilege.ts +541 -541
  180. package/src/components/user-group/index.ts +2 -2
  181. package/src/components/user-group/user-group.repository.ts +19 -19
  182. package/src/components/user-group/user-group.ts +764 -764
  183. package/src/components/user-object-privilege/index.ts +2 -2
  184. package/src/components/user-object-privilege/user-object-privilege.repository.ts +11 -11
  185. package/src/components/user-object-privilege/user-object-privilege.ts +79 -79
  186. package/src/components/user-password-history/index.ts +2 -2
  187. package/src/components/user-password-history/user-password-history.repository.ts +39 -39
  188. package/src/components/user-password-history/user-password-history.ts +187 -187
  189. package/src/components/user-privilege/index.ts +2 -2
  190. package/src/components/user-privilege/user-privilege.repository.ts +25 -25
  191. package/src/components/user-privilege/user-privilege.ts +662 -662
  192. package/src/components/user-reporting-hierarchy/index.ts +2 -2
  193. package/src/components/user-reporting-hierarchy/user-reporting-hierarchy.repository.ts +30 -30
  194. package/src/components/user-reporting-hierarchy/user-reporting-hierarchy.ts +505 -505
  195. package/src/components/user-system-access/index.ts +2 -2
  196. package/src/components/user-system-access/user-system-access.repository.ts +41 -41
  197. package/src/components/user-system-access/user-system-access.ts +717 -717
  198. package/src/database.ts +15 -15
  199. package/src/enum/api-key.enum.ts +5 -5
  200. package/src/enum/building-type.enum.ts +6 -6
  201. package/src/enum/group-type.enum.ts +8 -8
  202. package/src/enum/index.ts +6 -6
  203. package/src/enum/login-status.enum.ts +4 -4
  204. package/src/enum/object-status.enum.ts +4 -4
  205. package/src/enum/user-status.enum.ts +7 -7
  206. package/src/enum/yn.enum.ts +4 -4
  207. package/src/index.ts +8 -8
  208. package/src/interfaces/api-key-attr.interface.ts +16 -16
  209. package/src/interfaces/group-object-privilege.interface.ts +14 -14
  210. package/src/interfaces/group-privilege.interface.ts +10 -10
  211. package/src/interfaces/group-reporting-user.interface.ts +11 -11
  212. package/src/interfaces/group-search-attr.interface.ts +9 -9
  213. package/src/interfaces/group-system-access.interface.ts +10 -10
  214. package/src/interfaces/group.interface.ts +17 -17
  215. package/src/interfaces/index.ts +13 -13
  216. package/src/interfaces/system-login.interface.ts +6 -6
  217. package/src/interfaces/system-privilege-search.interface.ts +5 -5
  218. package/src/interfaces/system-privilege.interface.ts +11 -11
  219. package/src/interfaces/system-search-attr.interface.ts +5 -5
  220. package/src/interfaces/system.interface.ts +15 -15
  221. package/src/interfaces/user-group.interface.ts +12 -12
  222. package/src/interfaces/user-object-privilege.interface.ts +14 -14
  223. package/src/interfaces/user-password-history.interface.ts +6 -6
  224. package/src/interfaces/user-privilege.interface.ts +10 -10
  225. package/src/interfaces/user-reporting-hierarchy.interface.ts +11 -11
  226. package/src/interfaces/user-session.interface.ts +5 -5
  227. package/src/interfaces/user-system-access.interface.ts +10 -10
  228. package/src/models/api-key-entity.ts +101 -101
  229. package/src/models/building.entity.ts +103 -103
  230. package/src/models/group-object-privilege.entity.ts +91 -91
  231. package/src/models/group-privilege.entity.ts +78 -78
  232. package/src/models/group-reporting-user.entity.ts +95 -95
  233. package/src/models/group-system-access.entity.ts +81 -81
  234. package/src/models/group.entity.ts +127 -127
  235. package/src/models/login-history.entity.ts +63 -63
  236. package/src/models/staff.entity.ts +91 -91
  237. package/src/models/system-privilege.entity.ts +90 -90
  238. package/src/models/system.entity.ts +113 -113
  239. package/src/models/user-group.entity.ts +91 -91
  240. package/src/models/user-object-privilege.entity.ts +90 -90
  241. package/src/models/user-password-history.ts +51 -51
  242. package/src/models/user-privilege.entity.ts +78 -78
  243. package/src/models/user-reporting-hierarchy.entity.ts +102 -102
  244. package/src/models/user-system-access.entity.ts +87 -87
  245. package/src/models/user.entity.ts +193 -193
  246. package/src/redis-client/__mocks__/jest-initial-setup.ts +2 -2
  247. package/src/redis-client/__mocks__/redis-mock.ts +28 -28
  248. package/src/redis-client/index.ts +1 -1
  249. package/src/redis-client/redis.service.ts +75 -75
  250. package/src/session/index.ts +2 -2
  251. package/src/session/interfaces/index.ts +1 -1
  252. package/src/session/interfaces/session-service.interface.ts +26 -26
  253. package/src/session/session.service.ts +96 -96
  254. package/src/types/auth-context.ts +10 -10
  255. package/src/types/index.ts +1 -1
  256. package/tsconfig.build.json +5 -5
  257. package/tsconfig.json +23 -23
@@ -1,541 +1,541 @@
1
- import { ClassError, ObjectBase } from '@tomei/general';
2
- import { SystemRepository } from '../system/system.repository';
3
- import { SystemPrivilegeRepository } from './system-privilege.repository';
4
- import { ISystemPrivilegeAttr } from '../../interfaces/system-privilege.interface';
5
- import { LoginUser } from '../login-user/login-user';
6
- import { ApplicationConfig, ComponentConfig } from '@tomei/config';
7
- import { System } from '../system/system';
8
- import { ActionEnum, Activity } from '@tomei/activity-history';
9
- import { ISystemPrivilegeSearch } from '../../interfaces/system-privilege-search.interface';
10
- import { Op } from 'sequelize';
11
-
12
- export class SystemPrivilege extends ObjectBase {
13
- ObjectType = 'SystemPrivilege';
14
- TableName = 'sso_SystemPrivilege';
15
- ObjectId: string;
16
- PrivilegeCode: string;
17
- SystemCode: string;
18
- ObjectName: string;
19
- Description: string;
20
- Status: string;
21
- private _CreatedAt: Date;
22
- private _UpdatedAt: Date;
23
- private _CreatedById: number;
24
- private _UpdatedById: number;
25
-
26
- get SystemPrivilegeId() {
27
- return this.ObjectId;
28
- }
29
-
30
- set SystemPrivilegeId(value: string) {
31
- this.ObjectId = value;
32
- }
33
-
34
- get CreatedAt() {
35
- return this._CreatedAt;
36
- }
37
-
38
- get UpdatedAt() {
39
- return this._UpdatedAt;
40
- }
41
-
42
- get CreatedById() {
43
- return this._CreatedById;
44
- }
45
-
46
- get UpdatedById() {
47
- return this._UpdatedById;
48
- }
49
-
50
- private static _Repository = new SystemPrivilegeRepository();
51
- private static _SystemRepository = new SystemRepository();
52
-
53
- private constructor(systemPrivilegeAttr?: ISystemPrivilegeAttr) {
54
- super();
55
- if (systemPrivilegeAttr) {
56
- this.SystemPrivilegeId = systemPrivilegeAttr.SystemPrivilegeId;
57
- this.PrivilegeCode = systemPrivilegeAttr.PrivilegeCode;
58
- this.SystemCode = systemPrivilegeAttr.SystemCode;
59
- this.Description = systemPrivilegeAttr.Description;
60
- this.Status = systemPrivilegeAttr.Status;
61
- this._CreatedById = systemPrivilegeAttr.CreatedById;
62
- this._CreatedAt = systemPrivilegeAttr.CreatedAt;
63
- this._UpdatedById = systemPrivilegeAttr.UpdatedById;
64
- this._UpdatedAt = systemPrivilegeAttr.UpdatedAt;
65
- }
66
- }
67
-
68
- setAttributes(systemPrivilegeAttr: ISystemPrivilegeAttr) {
69
- this.PrivilegeCode = systemPrivilegeAttr.PrivilegeCode;
70
- this.SystemPrivilegeId = systemPrivilegeAttr.SystemPrivilegeId;
71
- this.SystemCode = systemPrivilegeAttr.SystemCode;
72
- this.Description = systemPrivilegeAttr.Description;
73
- this.Status = systemPrivilegeAttr.Status;
74
- this._CreatedAt = systemPrivilegeAttr.CreatedAt;
75
- this._UpdatedAt = systemPrivilegeAttr.UpdatedAt;
76
- this._CreatedById = systemPrivilegeAttr.CreatedById;
77
- this._UpdatedById = systemPrivilegeAttr.UpdatedById;
78
- }
79
-
80
- static async init(dbTransaction: any, SystemPrivilegeId?: string) {
81
- try {
82
- const systemPrivilege = new SystemPrivilege();
83
- if (SystemPrivilegeId) {
84
- const systemPrivilegeAttr = await this._Repository.findByPk(
85
- SystemPrivilegeId,
86
- {
87
- transaction: dbTransaction,
88
- },
89
- );
90
- if (systemPrivilegeAttr) {
91
- systemPrivilege.PrivilegeCode = systemPrivilegeAttr.PrivilegeCode;
92
- systemPrivilege.ObjectId = systemPrivilegeAttr.SystemPrivilegeId;
93
- systemPrivilege.SystemCode = systemPrivilegeAttr.SystemCode;
94
- systemPrivilege.Description = systemPrivilegeAttr.Description;
95
- systemPrivilege.Status = systemPrivilegeAttr.Status;
96
- systemPrivilege._CreatedById = systemPrivilegeAttr.CreatedById;
97
- systemPrivilege._CreatedAt = systemPrivilegeAttr.CreatedAt;
98
- systemPrivilege._UpdatedById = systemPrivilegeAttr.UpdatedById;
99
- systemPrivilege._UpdatedAt = systemPrivilegeAttr.UpdatedAt;
100
- } else {
101
- throw new ClassError(
102
- 'SystemPrivilege',
103
- 'SystemPrivilegeErrMsg00',
104
- 'System Privilege Not Found',
105
- );
106
- }
107
- }
108
- return systemPrivilege;
109
- } catch (error) {
110
- throw error;
111
- }
112
- }
113
-
114
- static async create(
115
- loginUser: LoginUser,
116
- dbTransaction: any,
117
- systemPrivilege: SystemPrivilege,
118
- ) {
119
- try {
120
- //Part 1: Privilege Checking
121
- const systemCode: string =
122
- ApplicationConfig.getComponentConfigValue('system-code');
123
- const isPrivileged = await loginUser.checkPrivileges(
124
- systemCode,
125
- 'PRIVILEGE_CREATE',
126
- );
127
- if (!isPrivileged) {
128
- throw new ClassError(
129
- 'SystemPrivilege',
130
- 'SystemPrivilegeErrMsg01',
131
- 'You do not have permission to create system privileges',
132
- );
133
- }
134
-
135
- //Part 2: Validation
136
- //Make sure systemCode and PrivilegeCode are not empty
137
- if (!systemPrivilege.SystemCode) {
138
- throw new ClassError(
139
- 'SystemPrivilege',
140
- 'SystemPrivilegeErrMsg02',
141
- 'System Code is required',
142
- );
143
- }
144
-
145
- if (!systemPrivilege.PrivilegeCode) {
146
- throw new ClassError(
147
- 'SystemPrivilege',
148
- 'SystemPrivilegeErrMsg02',
149
- 'Privilege Code is required',
150
- );
151
- }
152
-
153
- //Call System.init() method by passing systemCode
154
- await System.init(dbTransaction, systemPrivilege.SystemCode);
155
-
156
- //Call SystemPrivilege._Repo findByPk
157
- const existingSystemPrivilege = await this._Repository.findByPk(
158
- systemPrivilege.PrivilegeCode,
159
- {
160
- transaction: dbTransaction,
161
- },
162
- );
163
-
164
- //If PrivilegeCode found, throw new ClassError
165
- if (existingSystemPrivilege) {
166
- throw new ClassError(
167
- 'SystemPrivilege',
168
- 'SystemPrivilegeErrMsg03',
169
- 'System Privilege already exists',
170
- );
171
- }
172
-
173
- //Part 3: Create Privilege
174
- //Initialise new SystemPrivilege instance and populate below
175
- const newSystemPrivilege = new SystemPrivilege();
176
- newSystemPrivilege.ObjectId = newSystemPrivilege.createId();
177
- newSystemPrivilege.PrivilegeCode = systemPrivilege.PrivilegeCode;
178
- newSystemPrivilege.SystemCode = systemPrivilege.SystemCode;
179
- newSystemPrivilege.Description = systemPrivilege.Description;
180
- newSystemPrivilege.Status = 'Active';
181
- newSystemPrivilege._CreatedById = loginUser.UserId;
182
- newSystemPrivilege._UpdatedById = loginUser.UserId;
183
- newSystemPrivilege._CreatedAt = new Date();
184
- newSystemPrivilege._UpdatedAt = new Date();
185
-
186
- //Call SystemPrivilege._Repo create method
187
- await this._Repository.create(
188
- {
189
- SystemPrivilegeId: newSystemPrivilege.ObjectId,
190
- PrivilegeCode: newSystemPrivilege.PrivilegeCode,
191
- SystemCode: newSystemPrivilege.SystemCode,
192
- Description: newSystemPrivilege.Description,
193
- Status: newSystemPrivilege.Status,
194
- CreatedById: newSystemPrivilege._CreatedById,
195
- UpdatedById: newSystemPrivilege._UpdatedById,
196
- CreatedAt: newSystemPrivilege._CreatedAt,
197
- UpdatedAt: newSystemPrivilege._UpdatedAt,
198
- },
199
- {
200
- transaction: dbTransaction,
201
- },
202
- );
203
-
204
- //Part 4: Record Create Privilege Activity
205
- //Initialise EntityValueBefore variable and set to empty object.
206
- const EntityValueBefore = {};
207
- //Initialise EntityValueAfter variable and set to newSystemPrivilege object.
208
- const EntityValueAfter = newSystemPrivilege;
209
-
210
- //Instantiate new activity object and populate
211
- const activity = new Activity();
212
- activity.ActivityId = activity.createId();
213
- activity.Action = ActionEnum.CREATE;
214
- activity.Description = 'Add System Privilege';
215
- activity.EntityType = 'SystemPrivilege';
216
- activity.EntityId = newSystemPrivilege.SystemPrivilegeId;
217
- activity.EntityValueBefore = JSON.stringify(EntityValueBefore);
218
- activity.EntityValueAfter = JSON.stringify(EntityValueAfter);
219
-
220
- //Call Activity.create method
221
- await activity.create(loginUser.ObjectId, dbTransaction);
222
-
223
- return newSystemPrivilege;
224
- } catch (error) {
225
- throw error;
226
- }
227
- }
228
-
229
- public static async findAll(
230
- loginUser: LoginUser,
231
- dbTransaction: any,
232
- page?: number,
233
- row?: number,
234
- search?: ISystemPrivilegeSearch,
235
- ) {
236
- try {
237
- // Part 1: Privilege Checking
238
- const systemCode =
239
- ApplicationConfig.getComponentConfigValue('system-code');
240
- const isPrivileged = await loginUser.checkPrivileges(
241
- systemCode,
242
- 'PRIVILEGE_LIST',
243
- );
244
-
245
- if (!isPrivileged) {
246
- throw new ClassError(
247
- 'SystemPrivilege',
248
- 'SystemPrivilegeErrMsg01',
249
- 'You do not have permission to list system privileges',
250
- );
251
- }
252
-
253
- // Part 2: Retrieve listing
254
- const options: any = {
255
- distinct: true,
256
- order: [['createdAt', 'DESC']],
257
- transaction: dbTransaction,
258
- };
259
- //If page and row exist, please add limit and offset to the findAll options
260
- if (page && row) {
261
- const offset = row * (page - 1);
262
- const limit = row;
263
-
264
- options.offset = offset;
265
- options.limit = limit;
266
- }
267
-
268
- //If search query exists, please set it to sequelize where option.
269
- if (search) {
270
- const queryObj: any = {};
271
- Object.entries(search).forEach(([key, value]) => {
272
- queryObj[key] = {
273
- [Op.substring]: value,
274
- };
275
- });
276
-
277
- options.where = queryObj;
278
- }
279
-
280
- const result =
281
- await SystemPrivilege._Repository.findAllWithPagination(options);
282
-
283
- // Part 3: Return result
284
- // Map the result to SystemPrivilege object
285
- const systemPrivileges: SystemPrivilege[] = [];
286
- for (const systemPrivilegeAttr of result.rows) {
287
- systemPrivileges.push(
288
- new SystemPrivilege(systemPrivilegeAttr.get({ plain: true })),
289
- );
290
- }
291
-
292
- return {
293
- count: result.count,
294
- SystemPrivileges: systemPrivileges,
295
- };
296
- } catch (error) {
297
- throw error;
298
- }
299
- }
300
-
301
- public static async loadAllPrivileges(
302
- dbTransaction: any,
303
- systemCode: string,
304
- ): Promise<string> {
305
- try {
306
- //Instantiate existing System by passing:
307
- // dbTransaction
308
- // SystemCode: Params.SystemCode
309
- await System.init(dbTransaction, systemCode);
310
-
311
- // Part 2: Load Privileges
312
- // Load sso component config.loadComponentConfig Call Config. by passing:
313
- // filepath: '/component-config/sso-config.json'
314
-
315
- ComponentConfig.loadComponentConfig('./component-config/sso-config.json');
316
-
317
- // Retrieve privileges array by call Config.getComponentConfigValue by passing:
318
- // componentName: '@tomei/sso'
319
- // configKey: 'privileges'
320
- const privilegesConfig: {
321
- privilegeCode: string;
322
- description: string;
323
- }[] = ComponentConfig.getComponentConfigValue('@tomei/sso', 'privileges');
324
- // Retrieve system user id. Call Config.getComponentConfigValue by passing:
325
- // componentName: '@tomei/sso'
326
- // configKey: 'system'
327
- const systemConfig: {
328
- name: string;
329
- code: string;
330
- description: string;
331
- userId: string;
332
- } = ComponentConfig.getComponentConfigValue('@tomei/sso', 'system');
333
-
334
- //Set systemUserId to system.userId.
335
- const systemUserId = systemConfig.userId;
336
- //Retrieve existing SystemPrivilege. Call SystemPrivilege._Repo findAll method by passing:
337
- // where:
338
- // SystemCode: Params.SystemCode
339
- const existingSystemPrivileges = await this._Repository.findAll({
340
- where: {
341
- SystemCode: systemCode,
342
- },
343
- transaction: dbTransaction,
344
- });
345
-
346
- //Filter out existing privileges with the privileges array above to identify which privileges to be created and map it tobeCreatePrivileges.
347
- const tobeCreatePrivileges = privilegesConfig.filter(
348
- (privilegeConfig) =>
349
- !existingSystemPrivileges.find(
350
- (existingPrivilege) =>
351
- existingPrivilege.PrivilegeCode === privilegeConfig.privilegeCode,
352
- ),
353
- );
354
-
355
- //Call SystemPrivilege._Repo create method for each newPrivileges.
356
- const np = new SystemPrivilege();
357
- for (const privilegeConfig of tobeCreatePrivileges) {
358
- await this._Repository.create(
359
- {
360
- SystemPrivilegeId: np.createId(),
361
- PrivilegeCode: privilegeConfig.privilegeCode,
362
- SystemCode: systemCode,
363
- Description: privilegeConfig.description,
364
- Status: 'Active',
365
- CreatedById: parseInt(systemUserId),
366
- UpdatedById: parseInt(systemUserId),
367
- CreatedAt: new Date(),
368
- UpdatedAt: new Date(),
369
- },
370
- {
371
- transaction: dbTransaction,
372
- },
373
- );
374
- }
375
-
376
- return 'Privileges Loaded';
377
- } catch (error) {
378
- throw error;
379
- }
380
- }
381
-
382
- public async update(
383
- loginUser: LoginUser, //The user object representing the currently logged-in user.
384
- dbTransaction: any, //The database transaction instance for managing the transaction scope.
385
- privilege: {
386
- PrivilegeCode?: string;
387
- Description?: string;
388
- SystemCode?: string; //The System Code for System Privilege
389
- Status?: string; //The new privilege status (Active/Inactive) for the system privilege
390
- },
391
- ) {
392
- try {
393
- // Part 1: Update System Privilege
394
- // Call the SystemPrivilege._Repo.update() method to perform the update operation, passing:
395
- // - SystemCode: The new SystemCode.
396
- // - Status: The new status.
397
- // - UpdatedById: loginUser.UserId (to indicate who updated the record).
398
- // - UpdatedAt: Set to the current date and time.
399
- // - dbTransaction: The database transaction instance.
400
- const entityValueBefore = {
401
- SystemPrivilegeId: this.SystemPrivilegeId,
402
- Description: this.Description,
403
- PrivilegeCode: this.PrivilegeCode,
404
- SystemCode: this.SystemCode,
405
- Status: this.Status,
406
- CreatedById: this.CreatedById,
407
- CreatedAt: this.CreatedAt,
408
- UpdatedById: this.UpdatedById,
409
- UpdatedAt: this.UpdatedAt,
410
- };
411
-
412
- await SystemPrivilege._Repository.update(
413
- {
414
- PrivilegeCode: privilege.PrivilegeCode || this.PrivilegeCode,
415
- Description: privilege.Description || this.Description,
416
- SystemCode: privilege.SystemCode || this.SystemCode,
417
- Status: privilege.Status || this.Status,
418
- UpdatedById: loginUser.UserId,
419
- UpdatedAt: new Date(),
420
- },
421
- {
422
- where: {
423
- SystemPrivilegeId: this.SystemPrivilegeId,
424
- },
425
- transaction: dbTransaction,
426
- },
427
- );
428
-
429
- const entityValueAfter = {
430
- SystemPrivilegeId: this.SystemPrivilegeId,
431
- PrivilegeCode: privilege.PrivilegeCode || this.PrivilegeCode,
432
- Description: privilege.Description || this.Description,
433
- SystemCode: privilege.SystemCode || this.SystemCode,
434
- Status: privilege.Status || this.Status,
435
- CreatedById: this.CreatedById,
436
- CreatedAt: this.CreatedAt,
437
- UpdatedById: this.UpdatedById,
438
- UpdatedAt: this.UpdatedAt,
439
- };
440
-
441
- // Part 2: Record Activity History
442
- // Initialize a variable entityValueBefore to store the current state of the user privilege record before the update.
443
- // Create an instance of the Activity class and set the following properties:
444
- // - ActivityId: Call activity.createId().
445
- // - Action: Set to ActionEnum.Update.
446
- // - Description: Set to Update System Privilege.
447
- // - EntityType: Set to SystemPrivilege.
448
- // - EntityId: Use the ID of the updated user privilege record.
449
- // - EntityValueBefore: Stringify entityValueBefore to capture the state before the update.
450
- // - EntityValueAfter: Stringify the updated user privilege record to capture the new state after the update.
451
- // Call the activity create method with the following parameters:
452
- // - dbTransaction
453
- // - userId: loginUser.UserId
454
- const activity = new Activity();
455
- activity.ActivityId = activity.createId();
456
- activity.Action = ActionEnum.UPDATE;
457
- activity.Description = 'Update System Privilege';
458
- activity.EntityType = 'SystemPrivilege';
459
- activity.EntityId = this.SystemPrivilegeId + '';
460
- activity.EntityValueBefore = JSON.stringify(entityValueBefore);
461
- activity.EntityValueAfter = JSON.stringify(entityValueAfter);
462
- await activity.create(loginUser.ObjectId, dbTransaction);
463
-
464
- // Part 3: Return Updated Record
465
- // Retrieve the updated user system access record from the database or return the updated instance as needed.
466
- return entityValueAfter;
467
- } catch (error) {
468
- throw error;
469
- }
470
- }
471
-
472
- public async delete(dbTransaction: any, loginUser: LoginUser) {
473
- try {
474
- //Part 1: Privilege Checking
475
- const systemCode: string =
476
- ApplicationConfig.getComponentConfigValue('system-code');
477
- const isPrivileged = await loginUser.checkPrivileges(
478
- systemCode,
479
- 'PRIVILEGE_DELETE',
480
- );
481
- if (!isPrivileged) {
482
- throw new ClassError(
483
- 'SystemPrivilege',
484
- 'SystemPrivilegeErrMsg0X',
485
- 'You do not have permission to delete system privileges',
486
- );
487
- }
488
-
489
- //Part 2: Validation
490
- //Make sure SystemPrivilegeId is not empty
491
- if (!this.SystemPrivilegeId) {
492
- throw new ClassError(
493
- 'SystemPrivilege',
494
- 'SystemPrivilegeErrMsg02',
495
- 'System Privilege Id is required',
496
- );
497
- }
498
-
499
- //Part 3: Delete Privilege
500
-
501
- //Call SystemPrivilege._Repo delete method
502
- await SystemPrivilege._Repository.delete(
503
- this.SystemPrivilegeId,
504
- dbTransaction,
505
- );
506
-
507
- //Part 4: Record Create Privilege Activity
508
- //Initialise EntityValueBefore variable and set to empty object.
509
- const EntityValueBefore = {
510
- SystemPrivilegeId: this.ObjectId,
511
- PrivilegeCode: this.PrivilegeCode,
512
- SystemCode: this.SystemCode,
513
- Description: this.Description,
514
- Status: this.Status,
515
- CreatedById: this._CreatedById,
516
- UpdatedById: this._UpdatedById,
517
- CreatedAt: this._CreatedAt,
518
- UpdatedAt: this._UpdatedAt,
519
- };
520
- //Initialise EntityValueAfter variable and set to newSystemPrivilege object.
521
- const EntityValueAfter = {};
522
-
523
- //Instantiate new activity object and populate
524
- const activity = new Activity();
525
- activity.ActivityId = activity.createId();
526
- activity.Action = ActionEnum.DELETE;
527
- activity.Description = 'Delete System Privilege';
528
- activity.EntityType = 'SystemPrivilege';
529
- activity.EntityId = this.SystemPrivilegeId;
530
- activity.EntityValueBefore = JSON.stringify(EntityValueBefore);
531
- activity.EntityValueAfter = JSON.stringify(EntityValueAfter);
532
-
533
- //Call Activity.create method
534
- await activity.create(loginUser.ObjectId, dbTransaction);
535
-
536
- return this;
537
- } catch (error) {
538
- throw error;
539
- }
540
- }
541
- }
1
+ import { ClassError, ObjectBase } from '@tomei/general';
2
+ import { SystemRepository } from '../system/system.repository';
3
+ import { SystemPrivilegeRepository } from './system-privilege.repository';
4
+ import { ISystemPrivilegeAttr } from '../../interfaces/system-privilege.interface';
5
+ import { LoginUser } from '../login-user/login-user';
6
+ import { ApplicationConfig, ComponentConfig } from '@tomei/config';
7
+ import { System } from '../system/system';
8
+ import { ActionEnum, Activity } from '@tomei/activity-history';
9
+ import { ISystemPrivilegeSearch } from '../../interfaces/system-privilege-search.interface';
10
+ import { Op } from 'sequelize';
11
+
12
+ export class SystemPrivilege extends ObjectBase {
13
+ ObjectType = 'SystemPrivilege';
14
+ TableName = 'sso_SystemPrivilege';
15
+ ObjectId: string;
16
+ PrivilegeCode: string;
17
+ SystemCode: string;
18
+ ObjectName: string;
19
+ Description: string;
20
+ Status: string;
21
+ private _CreatedAt: Date;
22
+ private _UpdatedAt: Date;
23
+ private _CreatedById: number;
24
+ private _UpdatedById: number;
25
+
26
+ get SystemPrivilegeId() {
27
+ return this.ObjectId;
28
+ }
29
+
30
+ set SystemPrivilegeId(value: string) {
31
+ this.ObjectId = value;
32
+ }
33
+
34
+ get CreatedAt() {
35
+ return this._CreatedAt;
36
+ }
37
+
38
+ get UpdatedAt() {
39
+ return this._UpdatedAt;
40
+ }
41
+
42
+ get CreatedById() {
43
+ return this._CreatedById;
44
+ }
45
+
46
+ get UpdatedById() {
47
+ return this._UpdatedById;
48
+ }
49
+
50
+ private static _Repository = new SystemPrivilegeRepository();
51
+ private static _SystemRepository = new SystemRepository();
52
+
53
+ private constructor(systemPrivilegeAttr?: ISystemPrivilegeAttr) {
54
+ super();
55
+ if (systemPrivilegeAttr) {
56
+ this.SystemPrivilegeId = systemPrivilegeAttr.SystemPrivilegeId;
57
+ this.PrivilegeCode = systemPrivilegeAttr.PrivilegeCode;
58
+ this.SystemCode = systemPrivilegeAttr.SystemCode;
59
+ this.Description = systemPrivilegeAttr.Description;
60
+ this.Status = systemPrivilegeAttr.Status;
61
+ this._CreatedById = systemPrivilegeAttr.CreatedById;
62
+ this._CreatedAt = systemPrivilegeAttr.CreatedAt;
63
+ this._UpdatedById = systemPrivilegeAttr.UpdatedById;
64
+ this._UpdatedAt = systemPrivilegeAttr.UpdatedAt;
65
+ }
66
+ }
67
+
68
+ setAttributes(systemPrivilegeAttr: ISystemPrivilegeAttr) {
69
+ this.PrivilegeCode = systemPrivilegeAttr.PrivilegeCode;
70
+ this.SystemPrivilegeId = systemPrivilegeAttr.SystemPrivilegeId;
71
+ this.SystemCode = systemPrivilegeAttr.SystemCode;
72
+ this.Description = systemPrivilegeAttr.Description;
73
+ this.Status = systemPrivilegeAttr.Status;
74
+ this._CreatedAt = systemPrivilegeAttr.CreatedAt;
75
+ this._UpdatedAt = systemPrivilegeAttr.UpdatedAt;
76
+ this._CreatedById = systemPrivilegeAttr.CreatedById;
77
+ this._UpdatedById = systemPrivilegeAttr.UpdatedById;
78
+ }
79
+
80
+ static async init(dbTransaction: any, SystemPrivilegeId?: string) {
81
+ try {
82
+ const systemPrivilege = new SystemPrivilege();
83
+ if (SystemPrivilegeId) {
84
+ const systemPrivilegeAttr = await this._Repository.findByPk(
85
+ SystemPrivilegeId,
86
+ {
87
+ transaction: dbTransaction,
88
+ },
89
+ );
90
+ if (systemPrivilegeAttr) {
91
+ systemPrivilege.PrivilegeCode = systemPrivilegeAttr.PrivilegeCode;
92
+ systemPrivilege.ObjectId = systemPrivilegeAttr.SystemPrivilegeId;
93
+ systemPrivilege.SystemCode = systemPrivilegeAttr.SystemCode;
94
+ systemPrivilege.Description = systemPrivilegeAttr.Description;
95
+ systemPrivilege.Status = systemPrivilegeAttr.Status;
96
+ systemPrivilege._CreatedById = systemPrivilegeAttr.CreatedById;
97
+ systemPrivilege._CreatedAt = systemPrivilegeAttr.CreatedAt;
98
+ systemPrivilege._UpdatedById = systemPrivilegeAttr.UpdatedById;
99
+ systemPrivilege._UpdatedAt = systemPrivilegeAttr.UpdatedAt;
100
+ } else {
101
+ throw new ClassError(
102
+ 'SystemPrivilege',
103
+ 'SystemPrivilegeErrMsg00',
104
+ 'System Privilege Not Found',
105
+ );
106
+ }
107
+ }
108
+ return systemPrivilege;
109
+ } catch (error) {
110
+ throw error;
111
+ }
112
+ }
113
+
114
+ static async create(
115
+ loginUser: LoginUser,
116
+ dbTransaction: any,
117
+ systemPrivilege: SystemPrivilege,
118
+ ) {
119
+ try {
120
+ //Part 1: Privilege Checking
121
+ const systemCode: string =
122
+ ApplicationConfig.getComponentConfigValue('system-code');
123
+ const isPrivileged = await loginUser.checkPrivileges(
124
+ systemCode,
125
+ 'PRIVILEGE_CREATE',
126
+ );
127
+ if (!isPrivileged) {
128
+ throw new ClassError(
129
+ 'SystemPrivilege',
130
+ 'SystemPrivilegeErrMsg01',
131
+ 'You do not have permission to create system privileges',
132
+ );
133
+ }
134
+
135
+ //Part 2: Validation
136
+ //Make sure systemCode and PrivilegeCode are not empty
137
+ if (!systemPrivilege.SystemCode) {
138
+ throw new ClassError(
139
+ 'SystemPrivilege',
140
+ 'SystemPrivilegeErrMsg02',
141
+ 'System Code is required',
142
+ );
143
+ }
144
+
145
+ if (!systemPrivilege.PrivilegeCode) {
146
+ throw new ClassError(
147
+ 'SystemPrivilege',
148
+ 'SystemPrivilegeErrMsg02',
149
+ 'Privilege Code is required',
150
+ );
151
+ }
152
+
153
+ //Call System.init() method by passing systemCode
154
+ await System.init(dbTransaction, systemPrivilege.SystemCode);
155
+
156
+ //Call SystemPrivilege._Repo findByPk
157
+ const existingSystemPrivilege = await this._Repository.findByPk(
158
+ systemPrivilege.PrivilegeCode,
159
+ {
160
+ transaction: dbTransaction,
161
+ },
162
+ );
163
+
164
+ //If PrivilegeCode found, throw new ClassError
165
+ if (existingSystemPrivilege) {
166
+ throw new ClassError(
167
+ 'SystemPrivilege',
168
+ 'SystemPrivilegeErrMsg03',
169
+ 'System Privilege already exists',
170
+ );
171
+ }
172
+
173
+ //Part 3: Create Privilege
174
+ //Initialise new SystemPrivilege instance and populate below
175
+ const newSystemPrivilege = new SystemPrivilege();
176
+ newSystemPrivilege.ObjectId = newSystemPrivilege.createId();
177
+ newSystemPrivilege.PrivilegeCode = systemPrivilege.PrivilegeCode;
178
+ newSystemPrivilege.SystemCode = systemPrivilege.SystemCode;
179
+ newSystemPrivilege.Description = systemPrivilege.Description;
180
+ newSystemPrivilege.Status = 'Active';
181
+ newSystemPrivilege._CreatedById = loginUser.UserId;
182
+ newSystemPrivilege._UpdatedById = loginUser.UserId;
183
+ newSystemPrivilege._CreatedAt = new Date();
184
+ newSystemPrivilege._UpdatedAt = new Date();
185
+
186
+ //Call SystemPrivilege._Repo create method
187
+ await this._Repository.create(
188
+ {
189
+ SystemPrivilegeId: newSystemPrivilege.ObjectId,
190
+ PrivilegeCode: newSystemPrivilege.PrivilegeCode,
191
+ SystemCode: newSystemPrivilege.SystemCode,
192
+ Description: newSystemPrivilege.Description,
193
+ Status: newSystemPrivilege.Status,
194
+ CreatedById: newSystemPrivilege._CreatedById,
195
+ UpdatedById: newSystemPrivilege._UpdatedById,
196
+ CreatedAt: newSystemPrivilege._CreatedAt,
197
+ UpdatedAt: newSystemPrivilege._UpdatedAt,
198
+ },
199
+ {
200
+ transaction: dbTransaction,
201
+ },
202
+ );
203
+
204
+ //Part 4: Record Create Privilege Activity
205
+ //Initialise EntityValueBefore variable and set to empty object.
206
+ const EntityValueBefore = {};
207
+ //Initialise EntityValueAfter variable and set to newSystemPrivilege object.
208
+ const EntityValueAfter = newSystemPrivilege;
209
+
210
+ //Instantiate new activity object and populate
211
+ const activity = new Activity();
212
+ activity.ActivityId = activity.createId();
213
+ activity.Action = ActionEnum.CREATE;
214
+ activity.Description = 'Add System Privilege';
215
+ activity.EntityType = 'SystemPrivilege';
216
+ activity.EntityId = newSystemPrivilege.SystemPrivilegeId;
217
+ activity.EntityValueBefore = JSON.stringify(EntityValueBefore);
218
+ activity.EntityValueAfter = JSON.stringify(EntityValueAfter);
219
+
220
+ //Call Activity.create method
221
+ await activity.create(loginUser.ObjectId, dbTransaction);
222
+
223
+ return newSystemPrivilege;
224
+ } catch (error) {
225
+ throw error;
226
+ }
227
+ }
228
+
229
+ public static async findAll(
230
+ loginUser: LoginUser,
231
+ dbTransaction: any,
232
+ page?: number,
233
+ row?: number,
234
+ search?: ISystemPrivilegeSearch,
235
+ ) {
236
+ try {
237
+ // Part 1: Privilege Checking
238
+ const systemCode =
239
+ ApplicationConfig.getComponentConfigValue('system-code');
240
+ const isPrivileged = await loginUser.checkPrivileges(
241
+ systemCode,
242
+ 'PRIVILEGE_LIST',
243
+ );
244
+
245
+ if (!isPrivileged) {
246
+ throw new ClassError(
247
+ 'SystemPrivilege',
248
+ 'SystemPrivilegeErrMsg01',
249
+ 'You do not have permission to list system privileges',
250
+ );
251
+ }
252
+
253
+ // Part 2: Retrieve listing
254
+ const options: any = {
255
+ distinct: true,
256
+ order: [['createdAt', 'DESC']],
257
+ transaction: dbTransaction,
258
+ };
259
+ //If page and row exist, please add limit and offset to the findAll options
260
+ if (page && row) {
261
+ const offset = row * (page - 1);
262
+ const limit = row;
263
+
264
+ options.offset = offset;
265
+ options.limit = limit;
266
+ }
267
+
268
+ //If search query exists, please set it to sequelize where option.
269
+ if (search) {
270
+ const queryObj: any = {};
271
+ Object.entries(search).forEach(([key, value]) => {
272
+ queryObj[key] = {
273
+ [Op.substring]: value,
274
+ };
275
+ });
276
+
277
+ options.where = queryObj;
278
+ }
279
+
280
+ const result =
281
+ await SystemPrivilege._Repository.findAllWithPagination(options);
282
+
283
+ // Part 3: Return result
284
+ // Map the result to SystemPrivilege object
285
+ const systemPrivileges: SystemPrivilege[] = [];
286
+ for (const systemPrivilegeAttr of result.rows) {
287
+ systemPrivileges.push(
288
+ new SystemPrivilege(systemPrivilegeAttr.get({ plain: true })),
289
+ );
290
+ }
291
+
292
+ return {
293
+ count: result.count,
294
+ SystemPrivileges: systemPrivileges,
295
+ };
296
+ } catch (error) {
297
+ throw error;
298
+ }
299
+ }
300
+
301
+ public static async loadAllPrivileges(
302
+ dbTransaction: any,
303
+ systemCode: string,
304
+ ): Promise<string> {
305
+ try {
306
+ //Instantiate existing System by passing:
307
+ // dbTransaction
308
+ // SystemCode: Params.SystemCode
309
+ await System.init(dbTransaction, systemCode);
310
+
311
+ // Part 2: Load Privileges
312
+ // Load sso component config.loadComponentConfig Call Config. by passing:
313
+ // filepath: '/component-config/sso-config.json'
314
+
315
+ ComponentConfig.loadComponentConfig('./component-config/sso-config.json');
316
+
317
+ // Retrieve privileges array by call Config.getComponentConfigValue by passing:
318
+ // componentName: '@tomei/sso'
319
+ // configKey: 'privileges'
320
+ const privilegesConfig: {
321
+ privilegeCode: string;
322
+ description: string;
323
+ }[] = ComponentConfig.getComponentConfigValue('@tomei/sso', 'privileges');
324
+ // Retrieve system user id. Call Config.getComponentConfigValue by passing:
325
+ // componentName: '@tomei/sso'
326
+ // configKey: 'system'
327
+ const systemConfig: {
328
+ name: string;
329
+ code: string;
330
+ description: string;
331
+ userId: string;
332
+ } = ComponentConfig.getComponentConfigValue('@tomei/sso', 'system');
333
+
334
+ //Set systemUserId to system.userId.
335
+ const systemUserId = systemConfig.userId;
336
+ //Retrieve existing SystemPrivilege. Call SystemPrivilege._Repo findAll method by passing:
337
+ // where:
338
+ // SystemCode: Params.SystemCode
339
+ const existingSystemPrivileges = await this._Repository.findAll({
340
+ where: {
341
+ SystemCode: systemCode,
342
+ },
343
+ transaction: dbTransaction,
344
+ });
345
+
346
+ //Filter out existing privileges with the privileges array above to identify which privileges to be created and map it tobeCreatePrivileges.
347
+ const tobeCreatePrivileges = privilegesConfig.filter(
348
+ (privilegeConfig) =>
349
+ !existingSystemPrivileges.find(
350
+ (existingPrivilege) =>
351
+ existingPrivilege.PrivilegeCode === privilegeConfig.privilegeCode,
352
+ ),
353
+ );
354
+
355
+ //Call SystemPrivilege._Repo create method for each newPrivileges.
356
+ const np = new SystemPrivilege();
357
+ for (const privilegeConfig of tobeCreatePrivileges) {
358
+ await this._Repository.create(
359
+ {
360
+ SystemPrivilegeId: np.createId(),
361
+ PrivilegeCode: privilegeConfig.privilegeCode,
362
+ SystemCode: systemCode,
363
+ Description: privilegeConfig.description,
364
+ Status: 'Active',
365
+ CreatedById: parseInt(systemUserId),
366
+ UpdatedById: parseInt(systemUserId),
367
+ CreatedAt: new Date(),
368
+ UpdatedAt: new Date(),
369
+ },
370
+ {
371
+ transaction: dbTransaction,
372
+ },
373
+ );
374
+ }
375
+
376
+ return 'Privileges Loaded';
377
+ } catch (error) {
378
+ throw error;
379
+ }
380
+ }
381
+
382
+ public async update(
383
+ loginUser: LoginUser, //The user object representing the currently logged-in user.
384
+ dbTransaction: any, //The database transaction instance for managing the transaction scope.
385
+ privilege: {
386
+ PrivilegeCode?: string;
387
+ Description?: string;
388
+ SystemCode?: string; //The System Code for System Privilege
389
+ Status?: string; //The new privilege status (Active/Inactive) for the system privilege
390
+ },
391
+ ) {
392
+ try {
393
+ // Part 1: Update System Privilege
394
+ // Call the SystemPrivilege._Repo.update() method to perform the update operation, passing:
395
+ // - SystemCode: The new SystemCode.
396
+ // - Status: The new status.
397
+ // - UpdatedById: loginUser.UserId (to indicate who updated the record).
398
+ // - UpdatedAt: Set to the current date and time.
399
+ // - dbTransaction: The database transaction instance.
400
+ const entityValueBefore = {
401
+ SystemPrivilegeId: this.SystemPrivilegeId,
402
+ Description: this.Description,
403
+ PrivilegeCode: this.PrivilegeCode,
404
+ SystemCode: this.SystemCode,
405
+ Status: this.Status,
406
+ CreatedById: this.CreatedById,
407
+ CreatedAt: this.CreatedAt,
408
+ UpdatedById: this.UpdatedById,
409
+ UpdatedAt: this.UpdatedAt,
410
+ };
411
+
412
+ await SystemPrivilege._Repository.update(
413
+ {
414
+ PrivilegeCode: privilege.PrivilegeCode || this.PrivilegeCode,
415
+ Description: privilege.Description || this.Description,
416
+ SystemCode: privilege.SystemCode || this.SystemCode,
417
+ Status: privilege.Status || this.Status,
418
+ UpdatedById: loginUser.UserId,
419
+ UpdatedAt: new Date(),
420
+ },
421
+ {
422
+ where: {
423
+ SystemPrivilegeId: this.SystemPrivilegeId,
424
+ },
425
+ transaction: dbTransaction,
426
+ },
427
+ );
428
+
429
+ const entityValueAfter = {
430
+ SystemPrivilegeId: this.SystemPrivilegeId,
431
+ PrivilegeCode: privilege.PrivilegeCode || this.PrivilegeCode,
432
+ Description: privilege.Description || this.Description,
433
+ SystemCode: privilege.SystemCode || this.SystemCode,
434
+ Status: privilege.Status || this.Status,
435
+ CreatedById: this.CreatedById,
436
+ CreatedAt: this.CreatedAt,
437
+ UpdatedById: this.UpdatedById,
438
+ UpdatedAt: this.UpdatedAt,
439
+ };
440
+
441
+ // Part 2: Record Activity History
442
+ // Initialize a variable entityValueBefore to store the current state of the user privilege record before the update.
443
+ // Create an instance of the Activity class and set the following properties:
444
+ // - ActivityId: Call activity.createId().
445
+ // - Action: Set to ActionEnum.Update.
446
+ // - Description: Set to Update System Privilege.
447
+ // - EntityType: Set to SystemPrivilege.
448
+ // - EntityId: Use the ID of the updated user privilege record.
449
+ // - EntityValueBefore: Stringify entityValueBefore to capture the state before the update.
450
+ // - EntityValueAfter: Stringify the updated user privilege record to capture the new state after the update.
451
+ // Call the activity create method with the following parameters:
452
+ // - dbTransaction
453
+ // - userId: loginUser.UserId
454
+ const activity = new Activity();
455
+ activity.ActivityId = activity.createId();
456
+ activity.Action = ActionEnum.UPDATE;
457
+ activity.Description = 'Update System Privilege';
458
+ activity.EntityType = 'SystemPrivilege';
459
+ activity.EntityId = this.SystemPrivilegeId + '';
460
+ activity.EntityValueBefore = JSON.stringify(entityValueBefore);
461
+ activity.EntityValueAfter = JSON.stringify(entityValueAfter);
462
+ await activity.create(loginUser.ObjectId, dbTransaction);
463
+
464
+ // Part 3: Return Updated Record
465
+ // Retrieve the updated user system access record from the database or return the updated instance as needed.
466
+ return entityValueAfter;
467
+ } catch (error) {
468
+ throw error;
469
+ }
470
+ }
471
+
472
+ public async delete(dbTransaction: any, loginUser: LoginUser) {
473
+ try {
474
+ //Part 1: Privilege Checking
475
+ const systemCode: string =
476
+ ApplicationConfig.getComponentConfigValue('system-code');
477
+ const isPrivileged = await loginUser.checkPrivileges(
478
+ systemCode,
479
+ 'PRIVILEGE_DELETE',
480
+ );
481
+ if (!isPrivileged) {
482
+ throw new ClassError(
483
+ 'SystemPrivilege',
484
+ 'SystemPrivilegeErrMsg0X',
485
+ 'You do not have permission to delete system privileges',
486
+ );
487
+ }
488
+
489
+ //Part 2: Validation
490
+ //Make sure SystemPrivilegeId is not empty
491
+ if (!this.SystemPrivilegeId) {
492
+ throw new ClassError(
493
+ 'SystemPrivilege',
494
+ 'SystemPrivilegeErrMsg02',
495
+ 'System Privilege Id is required',
496
+ );
497
+ }
498
+
499
+ //Part 3: Delete Privilege
500
+
501
+ //Call SystemPrivilege._Repo delete method
502
+ await SystemPrivilege._Repository.delete(
503
+ this.SystemPrivilegeId,
504
+ dbTransaction,
505
+ );
506
+
507
+ //Part 4: Record Create Privilege Activity
508
+ //Initialise EntityValueBefore variable and set to empty object.
509
+ const EntityValueBefore = {
510
+ SystemPrivilegeId: this.ObjectId,
511
+ PrivilegeCode: this.PrivilegeCode,
512
+ SystemCode: this.SystemCode,
513
+ Description: this.Description,
514
+ Status: this.Status,
515
+ CreatedById: this._CreatedById,
516
+ UpdatedById: this._UpdatedById,
517
+ CreatedAt: this._CreatedAt,
518
+ UpdatedAt: this._UpdatedAt,
519
+ };
520
+ //Initialise EntityValueAfter variable and set to newSystemPrivilege object.
521
+ const EntityValueAfter = {};
522
+
523
+ //Instantiate new activity object and populate
524
+ const activity = new Activity();
525
+ activity.ActivityId = activity.createId();
526
+ activity.Action = ActionEnum.DELETE;
527
+ activity.Description = 'Delete System Privilege';
528
+ activity.EntityType = 'SystemPrivilege';
529
+ activity.EntityId = this.SystemPrivilegeId;
530
+ activity.EntityValueBefore = JSON.stringify(EntityValueBefore);
531
+ activity.EntityValueAfter = JSON.stringify(EntityValueAfter);
532
+
533
+ //Call Activity.create method
534
+ await activity.create(loginUser.ObjectId, dbTransaction);
535
+
536
+ return this;
537
+ } catch (error) {
538
+ throw error;
539
+ }
540
+ }
541
+ }