@tomei/sso 0.60.4 → 0.61.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 (677) 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-user/user.js +8 -0
  101. package/dist/src/components/login-user/user.js.map +1 -1
  102. package/dist/tsconfig.tsbuildinfo +1 -1
  103. package/eslint.config.mjs +58 -58
  104. package/jest.config.js +14 -14
  105. package/migrations/20240314080602-create-user-table.js +124 -124
  106. package/migrations/20240314080603-create-user-group-table.js +85 -85
  107. package/migrations/20240314080604-create-user-user-group-table.js +55 -55
  108. package/migrations/20240314080605-create-login-history-table.js +53 -53
  109. package/migrations/20240527064925-create-system-table.js +78 -78
  110. package/migrations/20240527064926-create-system-privilege-table.js +71 -71
  111. package/migrations/20240527065342-create-group-table.js +93 -93
  112. package/migrations/20240527065633-create-group-reporting-user-table.js +76 -76
  113. package/migrations/20240528011551-create-group-system-access-table.js +72 -72
  114. package/migrations/20240528023018-user-system-access-table.js +75 -75
  115. package/migrations/20240528032229-user-privilege-table.js +76 -76
  116. package/migrations/20240528063003-create-group-privilege-table.js +76 -76
  117. package/migrations/20240528063051-create-group-object-privilege-table.js +84 -84
  118. package/migrations/20240528063107-create-user-object-privilege-table.js +84 -84
  119. package/migrations/20240528063108-create-api-key-table.js +85 -85
  120. package/migrations/20241104104802-create-building-table.js +95 -95
  121. package/migrations/20250108091132-add-area-manager-user-id-to-building-table.js +14 -14
  122. package/migrations/20250108091133-add-passcode-to-user-table.js +36 -36
  123. package/migrations/20250210115636-create-user-reporting-hierarchy.js +76 -76
  124. package/migrations/20250326043818-crate-user-password-history.js +42 -42
  125. package/package.json +90 -90
  126. package/sampledotenv +7 -7
  127. package/sonar-project.properties +22 -22
  128. package/src/components/api-key/api-key.repository.ts +15 -15
  129. package/src/components/api-key/api-key.ts +448 -448
  130. package/src/components/api-key/index.ts +4 -4
  131. package/src/components/building/building.repository.ts +27 -27
  132. package/src/components/building/index.ts +2 -2
  133. package/src/components/group/group.repository.ts +26 -26
  134. package/src/components/group/group.ts +2284 -2284
  135. package/src/components/group/index.ts +3 -3
  136. package/src/components/group-object-privilege/group-object-privilege.repository.ts +25 -25
  137. package/src/components/group-object-privilege/group-object-privilege.ts +278 -278
  138. package/src/components/group-object-privilege/index.ts +2 -2
  139. package/src/components/group-privilege/group-privilege.repository.ts +29 -29
  140. package/src/components/group-privilege/group-privilege.ts +84 -84
  141. package/src/components/group-privilege/index.ts +2 -2
  142. package/src/components/group-reporting-user/group-reporting-user.repository.ts +23 -23
  143. package/src/components/group-reporting-user/group-reporting-user.ts +506 -506
  144. package/src/components/group-reporting-user/index.ts +3 -3
  145. package/src/components/group-system-access/group-system-access.repository.ts +43 -43
  146. package/src/components/group-system-access/group-system-access.ts +90 -90
  147. package/src/components/group-system-access/index.ts +2 -2
  148. package/src/components/index.ts +20 -20
  149. package/src/components/login-history/index.ts +1 -1
  150. package/src/components/login-history/login-history.repository.ts +11 -11
  151. package/src/components/login-user/index.ts +5 -5
  152. package/src/components/login-user/interfaces/check-user-info-duplicated.interface.ts +7 -7
  153. package/src/components/login-user/interfaces/index.ts +1 -1
  154. package/src/components/login-user/interfaces/system-access.interface.ts +13 -13
  155. package/src/components/login-user/interfaces/user-info.interface.ts +34 -34
  156. package/src/components/login-user/login-user.ts +362 -362
  157. package/src/components/login-user/user.repository.ts +11 -11
  158. package/src/components/login-user/user.ts +3142 -3134
  159. package/src/components/password-hash/index.ts +2 -2
  160. package/src/components/password-hash/interfaces/index.ts +1 -1
  161. package/src/components/password-hash/interfaces/password-hash-service.interface.ts +4 -4
  162. package/src/components/password-hash/password-hash.service.ts +14 -14
  163. package/src/components/staff/index.ts +2 -2
  164. package/src/components/staff/staff.repository.ts +27 -27
  165. package/src/components/system/index.ts +3 -3
  166. package/src/components/system/system.repository.ts +11 -11
  167. package/src/components/system/system.ts +456 -456
  168. package/src/components/system-privilege/index.ts +4 -4
  169. package/src/components/system-privilege/system-privilege.repository.ts +18 -18
  170. package/src/components/system-privilege/system-privilege.ts +541 -541
  171. package/src/components/user-group/index.ts +2 -2
  172. package/src/components/user-group/user-group.repository.ts +19 -19
  173. package/src/components/user-group/user-group.ts +764 -764
  174. package/src/components/user-object-privilege/index.ts +2 -2
  175. package/src/components/user-object-privilege/user-object-privilege.repository.ts +11 -11
  176. package/src/components/user-object-privilege/user-object-privilege.ts +79 -79
  177. package/src/components/user-password-history/index.ts +2 -2
  178. package/src/components/user-password-history/user-password-history.repository.ts +39 -39
  179. package/src/components/user-password-history/user-password-history.ts +187 -187
  180. package/src/components/user-privilege/index.ts +2 -2
  181. package/src/components/user-privilege/user-privilege.repository.ts +25 -25
  182. package/src/components/user-privilege/user-privilege.ts +662 -662
  183. package/src/components/user-reporting-hierarchy/index.ts +2 -2
  184. package/src/components/user-reporting-hierarchy/user-reporting-hierarchy.repository.ts +30 -30
  185. package/src/components/user-reporting-hierarchy/user-reporting-hierarchy.ts +505 -505
  186. package/src/components/user-system-access/index.ts +2 -2
  187. package/src/components/user-system-access/user-system-access.repository.ts +41 -41
  188. package/src/components/user-system-access/user-system-access.ts +717 -717
  189. package/src/database.ts +15 -15
  190. package/src/enum/api-key.enum.ts +5 -5
  191. package/src/enum/building-type.enum.ts +6 -6
  192. package/src/enum/group-type.enum.ts +8 -8
  193. package/src/enum/index.ts +6 -6
  194. package/src/enum/login-status.enum.ts +4 -4
  195. package/src/enum/object-status.enum.ts +4 -4
  196. package/src/enum/user-status.enum.ts +7 -7
  197. package/src/enum/yn.enum.ts +4 -4
  198. package/src/index.ts +8 -8
  199. package/src/interfaces/api-key-attr.interface.ts +16 -16
  200. package/src/interfaces/group-object-privilege.interface.ts +14 -14
  201. package/src/interfaces/group-privilege.interface.ts +10 -10
  202. package/src/interfaces/group-reporting-user.interface.ts +11 -11
  203. package/src/interfaces/group-search-attr.interface.ts +9 -9
  204. package/src/interfaces/group-system-access.interface.ts +10 -10
  205. package/src/interfaces/group.interface.ts +17 -17
  206. package/src/interfaces/index.ts +13 -13
  207. package/src/interfaces/system-login.interface.ts +6 -6
  208. package/src/interfaces/system-privilege-search.interface.ts +5 -5
  209. package/src/interfaces/system-privilege.interface.ts +11 -11
  210. package/src/interfaces/system-search-attr.interface.ts +5 -5
  211. package/src/interfaces/system.interface.ts +15 -15
  212. package/src/interfaces/user-group.interface.ts +12 -12
  213. package/src/interfaces/user-object-privilege.interface.ts +14 -14
  214. package/src/interfaces/user-password-history.interface.ts +6 -6
  215. package/src/interfaces/user-privilege.interface.ts +10 -10
  216. package/src/interfaces/user-reporting-hierarchy.interface.ts +11 -11
  217. package/src/interfaces/user-session.interface.ts +5 -5
  218. package/src/interfaces/user-system-access.interface.ts +10 -10
  219. package/src/models/api-key-entity.ts +101 -101
  220. package/src/models/building.entity.ts +103 -103
  221. package/src/models/group-object-privilege.entity.ts +91 -91
  222. package/src/models/group-privilege.entity.ts +78 -78
  223. package/src/models/group-reporting-user.entity.ts +95 -95
  224. package/src/models/group-system-access.entity.ts +81 -81
  225. package/src/models/group.entity.ts +127 -127
  226. package/src/models/login-history.entity.ts +63 -63
  227. package/src/models/staff.entity.ts +91 -91
  228. package/src/models/system-privilege.entity.ts +90 -90
  229. package/src/models/system.entity.ts +113 -113
  230. package/src/models/user-group.entity.ts +91 -91
  231. package/src/models/user-object-privilege.entity.ts +90 -90
  232. package/src/models/user-password-history.ts +51 -51
  233. package/src/models/user-privilege.entity.ts +78 -78
  234. package/src/models/user-reporting-hierarchy.entity.ts +102 -102
  235. package/src/models/user-system-access.entity.ts +87 -87
  236. package/src/models/user.entity.ts +193 -193
  237. package/src/redis-client/__mocks__/jest-initial-setup.ts +2 -2
  238. package/src/redis-client/__mocks__/redis-mock.ts +28 -28
  239. package/src/redis-client/index.ts +1 -1
  240. package/src/redis-client/redis.service.ts +75 -75
  241. package/src/session/index.ts +2 -2
  242. package/src/session/interfaces/index.ts +1 -1
  243. package/src/session/interfaces/session-service.interface.ts +26 -26
  244. package/src/session/session.service.ts +96 -96
  245. package/src/types/auth-context.ts +10 -10
  246. package/src/types/index.ts +1 -1
  247. package/tsconfig.build.json +5 -5
  248. package/tsconfig.json +23 -23
  249. package/dist/components/api-key/api-key.d.ts +0 -85
  250. package/dist/components/api-key/api-key.d.ts.map +0 -1
  251. package/dist/components/api-key/api-key.js +0 -245
  252. package/dist/components/api-key/api-key.js.map +0 -1
  253. package/dist/components/api-key/api-key.repository.d.ts +0 -7
  254. package/dist/components/api-key/api-key.repository.d.ts.map +0 -1
  255. package/dist/components/api-key/api-key.repository.js +0 -18
  256. package/dist/components/api-key/api-key.repository.js.map +0 -1
  257. package/dist/components/api-key/index.d.ts +0 -4
  258. package/dist/components/api-key/index.d.ts.map +0 -1
  259. package/dist/components/api-key/index.js +0 -8
  260. package/dist/components/api-key/index.js.map +0 -1
  261. package/dist/components/building/building.repository.d.ts +0 -7
  262. package/dist/components/building/building.repository.d.ts.map +0 -1
  263. package/dist/components/building/building.repository.js +0 -30
  264. package/dist/components/building/building.repository.js.map +0 -1
  265. package/dist/components/building/index.d.ts +0 -2
  266. package/dist/components/building/index.d.ts.map +0 -1
  267. package/dist/components/building/index.js +0 -18
  268. package/dist/components/building/index.js.map +0 -1
  269. package/dist/components/group/group.d.ts +0 -129
  270. package/dist/components/group/group.d.ts.map +0 -1
  271. package/dist/components/group/group.js +0 -1337
  272. package/dist/components/group/group.js.map +0 -1
  273. package/dist/components/group/group.repository.d.ts +0 -8
  274. package/dist/components/group/group.repository.d.ts.map +0 -1
  275. package/dist/components/group/group.repository.js +0 -29
  276. package/dist/components/group/group.repository.js.map +0 -1
  277. package/dist/components/group/index.d.ts +0 -3
  278. package/dist/components/group/index.d.ts.map +0 -1
  279. package/dist/components/group/index.js +0 -19
  280. package/dist/components/group/index.js.map +0 -1
  281. package/dist/components/group-object-privilege/group-object-privilege.d.ts +0 -28
  282. package/dist/components/group-object-privilege/group-object-privilege.d.ts.map +0 -1
  283. package/dist/components/group-object-privilege/group-object-privilege.js +0 -163
  284. package/dist/components/group-object-privilege/group-object-privilege.js.map +0 -1
  285. package/dist/components/group-object-privilege/group-object-privilege.repository.d.ts +0 -7
  286. package/dist/components/group-object-privilege/group-object-privilege.repository.d.ts.map +0 -1
  287. package/dist/components/group-object-privilege/group-object-privilege.repository.js +0 -26
  288. package/dist/components/group-object-privilege/group-object-privilege.repository.js.map +0 -1
  289. package/dist/components/group-object-privilege/index.d.ts +0 -3
  290. package/dist/components/group-object-privilege/index.d.ts.map +0 -1
  291. package/dist/components/group-object-privilege/index.js +0 -19
  292. package/dist/components/group-object-privilege/index.js.map +0 -1
  293. package/dist/components/group-privilege/group-privilege.d.ts +0 -25
  294. package/dist/components/group-privilege/group-privilege.d.ts.map +0 -1
  295. package/dist/components/group-privilege/group-privilege.js +0 -67
  296. package/dist/components/group-privilege/group-privilege.js.map +0 -1
  297. package/dist/components/group-privilege/group-privilege.repository.d.ts +0 -7
  298. package/dist/components/group-privilege/group-privilege.repository.d.ts.map +0 -1
  299. package/dist/components/group-privilege/group-privilege.repository.js +0 -28
  300. package/dist/components/group-privilege/group-privilege.repository.js.map +0 -1
  301. package/dist/components/group-privilege/index.d.ts +0 -3
  302. package/dist/components/group-privilege/index.d.ts.map +0 -1
  303. package/dist/components/group-privilege/index.js +0 -19
  304. package/dist/components/group-privilege/index.js.map +0 -1
  305. package/dist/components/group-reporting-user/group-reporting-user.d.ts +0 -30
  306. package/dist/components/group-reporting-user/group-reporting-user.d.ts.map +0 -1
  307. package/dist/components/group-reporting-user/group-reporting-user.js +0 -277
  308. package/dist/components/group-reporting-user/group-reporting-user.js.map +0 -1
  309. package/dist/components/group-reporting-user/group-reporting-user.repository.d.ts +0 -7
  310. package/dist/components/group-reporting-user/group-reporting-user.repository.d.ts.map +0 -1
  311. package/dist/components/group-reporting-user/group-reporting-user.repository.js +0 -23
  312. package/dist/components/group-reporting-user/group-reporting-user.repository.js.map +0 -1
  313. package/dist/components/group-reporting-user/index.d.ts +0 -3
  314. package/dist/components/group-reporting-user/index.d.ts.map +0 -1
  315. package/dist/components/group-reporting-user/index.js +0 -19
  316. package/dist/components/group-reporting-user/index.js.map +0 -1
  317. package/dist/components/group-system-access/group-system-access.d.ts +0 -28
  318. package/dist/components/group-system-access/group-system-access.d.ts.map +0 -1
  319. package/dist/components/group-system-access/group-system-access.js +0 -70
  320. package/dist/components/group-system-access/group-system-access.js.map +0 -1
  321. package/dist/components/group-system-access/group-system-access.repository.d.ts +0 -8
  322. package/dist/components/group-system-access/group-system-access.repository.d.ts.map +0 -1
  323. package/dist/components/group-system-access/group-system-access.repository.js +0 -46
  324. package/dist/components/group-system-access/group-system-access.repository.js.map +0 -1
  325. package/dist/components/group-system-access/index.d.ts +0 -3
  326. package/dist/components/group-system-access/index.d.ts.map +0 -1
  327. package/dist/components/group-system-access/index.js +0 -19
  328. package/dist/components/group-system-access/index.js.map +0 -1
  329. package/dist/components/index.d.ts +0 -18
  330. package/dist/components/index.d.ts.map +0 -1
  331. package/dist/components/index.js +0 -34
  332. package/dist/components/index.js.map +0 -1
  333. package/dist/components/login-history/index.d.ts +0 -2
  334. package/dist/components/login-history/index.d.ts.map +0 -1
  335. package/dist/components/login-history/index.js +0 -18
  336. package/dist/components/login-history/index.js.map +0 -1
  337. package/dist/components/login-history/login-history.repository.d.ts +0 -6
  338. package/dist/components/login-history/login-history.repository.d.ts.map +0 -1
  339. package/dist/components/login-history/login-history.repository.js +0 -15
  340. package/dist/components/login-history/login-history.repository.js.map +0 -1
  341. package/dist/components/login-user/index.d.ts +0 -5
  342. package/dist/components/login-user/index.d.ts.map +0 -1
  343. package/dist/components/login-user/index.js +0 -21
  344. package/dist/components/login-user/index.js.map +0 -1
  345. package/dist/components/login-user/interfaces/check-user-info-duplicated.interface.d.ts +0 -8
  346. package/dist/components/login-user/interfaces/check-user-info-duplicated.interface.d.ts.map +0 -1
  347. package/dist/components/login-user/interfaces/check-user-info-duplicated.interface.js +0 -3
  348. package/dist/components/login-user/interfaces/check-user-info-duplicated.interface.js.map +0 -1
  349. package/dist/components/login-user/interfaces/index.d.ts +0 -2
  350. package/dist/components/login-user/interfaces/index.d.ts.map +0 -1
  351. package/dist/components/login-user/interfaces/index.js +0 -18
  352. package/dist/components/login-user/interfaces/index.js.map +0 -1
  353. package/dist/components/login-user/interfaces/system-access.interface.d.ts +0 -14
  354. package/dist/components/login-user/interfaces/system-access.interface.d.ts.map +0 -1
  355. package/dist/components/login-user/interfaces/system-access.interface.js +0 -3
  356. package/dist/components/login-user/interfaces/system-access.interface.js.map +0 -1
  357. package/dist/components/login-user/interfaces/user-info.interface.d.ts +0 -31
  358. package/dist/components/login-user/interfaces/user-info.interface.d.ts.map +0 -1
  359. package/dist/components/login-user/interfaces/user-info.interface.js +0 -3
  360. package/dist/components/login-user/interfaces/user-info.interface.js.map +0 -1
  361. package/dist/components/login-user/login-user.d.ts +0 -14
  362. package/dist/components/login-user/login-user.d.ts.map +0 -1
  363. package/dist/components/login-user/login-user.js +0 -127
  364. package/dist/components/login-user/login-user.js.map +0 -1
  365. package/dist/components/login-user/user.d.ts +0 -155
  366. package/dist/components/login-user/user.d.ts.map +0 -1
  367. package/dist/components/login-user/user.js +0 -1753
  368. package/dist/components/login-user/user.js.map +0 -1
  369. package/dist/components/login-user/user.repository.d.ts +0 -6
  370. package/dist/components/login-user/user.repository.d.ts.map +0 -1
  371. package/dist/components/login-user/user.repository.js +0 -15
  372. package/dist/components/login-user/user.repository.js.map +0 -1
  373. package/dist/components/password-hash/index.d.ts +0 -3
  374. package/dist/components/password-hash/index.d.ts.map +0 -1
  375. package/dist/components/password-hash/index.js +0 -19
  376. package/dist/components/password-hash/index.js.map +0 -1
  377. package/dist/components/password-hash/interfaces/index.d.ts +0 -2
  378. package/dist/components/password-hash/interfaces/index.d.ts.map +0 -1
  379. package/dist/components/password-hash/interfaces/index.js +0 -18
  380. package/dist/components/password-hash/interfaces/index.js.map +0 -1
  381. package/dist/components/password-hash/interfaces/password-hash-service.interface.d.ts +0 -5
  382. package/dist/components/password-hash/interfaces/password-hash-service.interface.d.ts.map +0 -1
  383. package/dist/components/password-hash/interfaces/password-hash-service.interface.js +0 -3
  384. package/dist/components/password-hash/interfaces/password-hash-service.interface.js.map +0 -1
  385. package/dist/components/password-hash/password-hash.service.d.ts +0 -7
  386. package/dist/components/password-hash/password-hash.service.d.ts.map +0 -1
  387. package/dist/components/password-hash/password-hash.service.js +0 -48
  388. package/dist/components/password-hash/password-hash.service.js.map +0 -1
  389. package/dist/components/staff/index.d.ts +0 -2
  390. package/dist/components/staff/index.d.ts.map +0 -1
  391. package/dist/components/staff/index.js +0 -18
  392. package/dist/components/staff/index.js.map +0 -1
  393. package/dist/components/staff/staff.repository.d.ts +0 -7
  394. package/dist/components/staff/staff.repository.d.ts.map +0 -1
  395. package/dist/components/staff/staff.repository.js +0 -30
  396. package/dist/components/staff/staff.repository.js.map +0 -1
  397. package/dist/components/system/index.d.ts +0 -3
  398. package/dist/components/system/index.d.ts.map +0 -1
  399. package/dist/components/system/index.js +0 -19
  400. package/dist/components/system/index.js.map +0 -1
  401. package/dist/components/system/system.d.ts +0 -40
  402. package/dist/components/system/system.d.ts.map +0 -1
  403. package/dist/components/system/system.js +0 -283
  404. package/dist/components/system/system.js.map +0 -1
  405. package/dist/components/system/system.repository.d.ts +0 -6
  406. package/dist/components/system/system.repository.d.ts.map +0 -1
  407. package/dist/components/system/system.repository.js +0 -15
  408. package/dist/components/system/system.repository.js.map +0 -1
  409. package/dist/components/system-privilege/index.d.ts +0 -4
  410. package/dist/components/system-privilege/index.d.ts.map +0 -1
  411. package/dist/components/system-privilege/index.js +0 -8
  412. package/dist/components/system-privilege/index.js.map +0 -1
  413. package/dist/components/system-privilege/system-privilege.d.ts +0 -53
  414. package/dist/components/system-privilege/system-privilege.d.ts.map +0 -1
  415. package/dist/components/system-privilege/system-privilege.js +0 -315
  416. package/dist/components/system-privilege/system-privilege.js.map +0 -1
  417. package/dist/components/system-privilege/system-privilege.repository.d.ts +0 -7
  418. package/dist/components/system-privilege/system-privilege.repository.d.ts.map +0 -1
  419. package/dist/components/system-privilege/system-privilege.repository.js +0 -21
  420. package/dist/components/system-privilege/system-privilege.repository.js.map +0 -1
  421. package/dist/components/user-group/index.d.ts +0 -3
  422. package/dist/components/user-group/index.d.ts.map +0 -1
  423. package/dist/components/user-group/index.js +0 -19
  424. package/dist/components/user-group/index.js.map +0 -1
  425. package/dist/components/user-group/user-group.d.ts +0 -51
  426. package/dist/components/user-group/user-group.d.ts.map +0 -1
  427. package/dist/components/user-group/user-group.js +0 -335
  428. package/dist/components/user-group/user-group.js.map +0 -1
  429. package/dist/components/user-group/user-group.repository.d.ts +0 -6
  430. package/dist/components/user-group/user-group.repository.d.ts.map +0 -1
  431. package/dist/components/user-group/user-group.repository.js +0 -15
  432. package/dist/components/user-group/user-group.repository.js.map +0 -1
  433. package/dist/components/user-object-privilege/index.d.ts +0 -3
  434. package/dist/components/user-object-privilege/index.d.ts.map +0 -1
  435. package/dist/components/user-object-privilege/index.js +0 -19
  436. package/dist/components/user-object-privilege/index.js.map +0 -1
  437. package/dist/components/user-object-privilege/user-object-privilege.d.ts +0 -24
  438. package/dist/components/user-object-privilege/user-object-privilege.d.ts.map +0 -1
  439. package/dist/components/user-object-privilege/user-object-privilege.js +0 -59
  440. package/dist/components/user-object-privilege/user-object-privilege.js.map +0 -1
  441. package/dist/components/user-object-privilege/user-object-privilege.repository.d.ts +0 -6
  442. package/dist/components/user-object-privilege/user-object-privilege.repository.d.ts.map +0 -1
  443. package/dist/components/user-object-privilege/user-object-privilege.repository.js +0 -15
  444. package/dist/components/user-object-privilege/user-object-privilege.repository.js.map +0 -1
  445. package/dist/components/user-privilege/index.d.ts +0 -3
  446. package/dist/components/user-privilege/index.d.ts.map +0 -1
  447. package/dist/components/user-privilege/index.js +0 -19
  448. package/dist/components/user-privilege/index.js.map +0 -1
  449. package/dist/components/user-privilege/user-privilege.d.ts +0 -71
  450. package/dist/components/user-privilege/user-privilege.d.ts.map +0 -1
  451. package/dist/components/user-privilege/user-privilege.js +0 -388
  452. package/dist/components/user-privilege/user-privilege.js.map +0 -1
  453. package/dist/components/user-privilege/user-privilege.repository.d.ts +0 -7
  454. package/dist/components/user-privilege/user-privilege.repository.d.ts.map +0 -1
  455. package/dist/components/user-privilege/user-privilege.repository.js +0 -29
  456. package/dist/components/user-privilege/user-privilege.repository.js.map +0 -1
  457. package/dist/components/user-system-access/index.d.ts +0 -3
  458. package/dist/components/user-system-access/index.d.ts.map +0 -1
  459. package/dist/components/user-system-access/index.js +0 -19
  460. package/dist/components/user-system-access/index.js.map +0 -1
  461. package/dist/components/user-system-access/user-system-access.d.ts +0 -69
  462. package/dist/components/user-system-access/user-system-access.d.ts.map +0 -1
  463. package/dist/components/user-system-access/user-system-access.js +0 -413
  464. package/dist/components/user-system-access/user-system-access.js.map +0 -1
  465. package/dist/components/user-system-access/user-system-access.repository.d.ts +0 -8
  466. package/dist/components/user-system-access/user-system-access.repository.d.ts.map +0 -1
  467. package/dist/components/user-system-access/user-system-access.repository.js +0 -44
  468. package/dist/components/user-system-access/user-system-access.repository.js.map +0 -1
  469. package/dist/database.d.ts +0 -5
  470. package/dist/database.d.ts.map +0 -1
  471. package/dist/database.js +0 -14
  472. package/dist/database.js.map +0 -1
  473. package/dist/enum/api-key.enum.d.ts +0 -6
  474. package/dist/enum/api-key.enum.d.ts.map +0 -1
  475. package/dist/enum/api-key.enum.js +0 -10
  476. package/dist/enum/api-key.enum.js.map +0 -1
  477. package/dist/enum/group-type.enum.d.ts +0 -9
  478. package/dist/enum/group-type.enum.d.ts.map +0 -1
  479. package/dist/enum/group-type.enum.js +0 -13
  480. package/dist/enum/group-type.enum.js.map +0 -1
  481. package/dist/enum/index.d.ts +0 -7
  482. package/dist/enum/index.d.ts.map +0 -1
  483. package/dist/enum/index.js +0 -23
  484. package/dist/enum/index.js.map +0 -1
  485. package/dist/enum/login-status.enum.d.ts +0 -5
  486. package/dist/enum/login-status.enum.d.ts.map +0 -1
  487. package/dist/enum/login-status.enum.js +0 -9
  488. package/dist/enum/login-status.enum.js.map +0 -1
  489. package/dist/enum/object-status.enum.d.ts +0 -5
  490. package/dist/enum/object-status.enum.d.ts.map +0 -1
  491. package/dist/enum/object-status.enum.js +0 -9
  492. package/dist/enum/object-status.enum.js.map +0 -1
  493. package/dist/enum/user-status.enum.d.ts +0 -8
  494. package/dist/enum/user-status.enum.d.ts.map +0 -1
  495. package/dist/enum/user-status.enum.js +0 -12
  496. package/dist/enum/user-status.enum.js.map +0 -1
  497. package/dist/enum/yn.enum.d.ts +0 -5
  498. package/dist/enum/yn.enum.d.ts.map +0 -1
  499. package/dist/enum/yn.enum.js +0 -9
  500. package/dist/enum/yn.enum.js.map +0 -1
  501. package/dist/index.d.ts.map +0 -1
  502. package/dist/interfaces/api-key-attr.interface.d.ts +0 -16
  503. package/dist/interfaces/api-key-attr.interface.d.ts.map +0 -1
  504. package/dist/interfaces/api-key-attr.interface.js +0 -3
  505. package/dist/interfaces/api-key-attr.interface.js.map +0 -1
  506. package/dist/interfaces/group-object-privilege.interface.d.ts +0 -14
  507. package/dist/interfaces/group-object-privilege.interface.d.ts.map +0 -1
  508. package/dist/interfaces/group-object-privilege.interface.js +0 -3
  509. package/dist/interfaces/group-object-privilege.interface.js.map +0 -1
  510. package/dist/interfaces/group-privilege.interface.d.ts +0 -11
  511. package/dist/interfaces/group-privilege.interface.d.ts.map +0 -1
  512. package/dist/interfaces/group-privilege.interface.js +0 -3
  513. package/dist/interfaces/group-privilege.interface.js.map +0 -1
  514. package/dist/interfaces/group-reporting-user.interface.d.ts +0 -12
  515. package/dist/interfaces/group-reporting-user.interface.d.ts.map +0 -1
  516. package/dist/interfaces/group-reporting-user.interface.js +0 -3
  517. package/dist/interfaces/group-reporting-user.interface.js.map +0 -1
  518. package/dist/interfaces/group-search-attr.interface.d.ts +0 -9
  519. package/dist/interfaces/group-search-attr.interface.d.ts.map +0 -1
  520. package/dist/interfaces/group-search-attr.interface.js +0 -3
  521. package/dist/interfaces/group-search-attr.interface.js.map +0 -1
  522. package/dist/interfaces/group-system-access.interface.d.ts +0 -11
  523. package/dist/interfaces/group-system-access.interface.d.ts.map +0 -1
  524. package/dist/interfaces/group-system-access.interface.js +0 -3
  525. package/dist/interfaces/group-system-access.interface.js.map +0 -1
  526. package/dist/interfaces/group.interface.d.ts +0 -17
  527. package/dist/interfaces/group.interface.d.ts.map +0 -1
  528. package/dist/interfaces/group.interface.js +0 -3
  529. package/dist/interfaces/group.interface.js.map +0 -1
  530. package/dist/interfaces/index.d.ts +0 -14
  531. package/dist/interfaces/index.d.ts.map +0 -1
  532. package/dist/interfaces/index.js +0 -30
  533. package/dist/interfaces/index.js.map +0 -1
  534. package/dist/interfaces/system-login.interface.d.ts +0 -7
  535. package/dist/interfaces/system-login.interface.d.ts.map +0 -1
  536. package/dist/interfaces/system-login.interface.js +0 -3
  537. package/dist/interfaces/system-login.interface.js.map +0 -1
  538. package/dist/interfaces/system-privilege-search.interface.d.ts +0 -6
  539. package/dist/interfaces/system-privilege-search.interface.d.ts.map +0 -1
  540. package/dist/interfaces/system-privilege-search.interface.js +0 -3
  541. package/dist/interfaces/system-privilege-search.interface.js.map +0 -1
  542. package/dist/interfaces/system-privilege.interface.d.ts +0 -12
  543. package/dist/interfaces/system-privilege.interface.d.ts.map +0 -1
  544. package/dist/interfaces/system-privilege.interface.js +0 -3
  545. package/dist/interfaces/system-privilege.interface.js.map +0 -1
  546. package/dist/interfaces/system-search-attr.interface.d.ts +0 -6
  547. package/dist/interfaces/system-search-attr.interface.d.ts.map +0 -1
  548. package/dist/interfaces/system-search-attr.interface.js +0 -3
  549. package/dist/interfaces/system-search-attr.interface.js.map +0 -1
  550. package/dist/interfaces/system.interface.d.ts +0 -16
  551. package/dist/interfaces/system.interface.d.ts.map +0 -1
  552. package/dist/interfaces/system.interface.js +0 -3
  553. package/dist/interfaces/system.interface.js.map +0 -1
  554. package/dist/interfaces/user-group.interface.d.ts +0 -13
  555. package/dist/interfaces/user-group.interface.d.ts.map +0 -1
  556. package/dist/interfaces/user-group.interface.js +0 -3
  557. package/dist/interfaces/user-group.interface.js.map +0 -1
  558. package/dist/interfaces/user-object-privilege.interface.d.ts +0 -14
  559. package/dist/interfaces/user-object-privilege.interface.d.ts.map +0 -1
  560. package/dist/interfaces/user-object-privilege.interface.js +0 -3
  561. package/dist/interfaces/user-object-privilege.interface.js.map +0 -1
  562. package/dist/interfaces/user-privilege.interface.d.ts +0 -11
  563. package/dist/interfaces/user-privilege.interface.d.ts.map +0 -1
  564. package/dist/interfaces/user-privilege.interface.js +0 -3
  565. package/dist/interfaces/user-privilege.interface.js.map +0 -1
  566. package/dist/interfaces/user-session.interface.d.ts +0 -5
  567. package/dist/interfaces/user-session.interface.d.ts.map +0 -1
  568. package/dist/interfaces/user-session.interface.js +0 -3
  569. package/dist/interfaces/user-session.interface.js.map +0 -1
  570. package/dist/interfaces/user-system-access.interface.d.ts +0 -11
  571. package/dist/interfaces/user-system-access.interface.d.ts.map +0 -1
  572. package/dist/interfaces/user-system-access.interface.js +0 -3
  573. package/dist/interfaces/user-system-access.interface.js.map +0 -1
  574. package/dist/models/api-key-entity.d.ts +0 -22
  575. package/dist/models/api-key-entity.d.ts.map +0 -1
  576. package/dist/models/api-key-entity.js +0 -125
  577. package/dist/models/api-key-entity.js.map +0 -1
  578. package/dist/models/building.entity.d.ts +0 -30
  579. package/dist/models/building.entity.d.ts.map +0 -1
  580. package/dist/models/building.entity.js +0 -216
  581. package/dist/models/building.entity.js.map +0 -1
  582. package/dist/models/group-object-privilege.entity.d.ts +0 -22
  583. package/dist/models/group-object-privilege.entity.d.ts.map +0 -1
  584. package/dist/models/group-object-privilege.entity.js +0 -114
  585. package/dist/models/group-object-privilege.entity.js.map +0 -1
  586. package/dist/models/group-privilege.entity.d.ts +0 -19
  587. package/dist/models/group-privilege.entity.d.ts.map +0 -1
  588. package/dist/models/group-privilege.entity.js +0 -99
  589. package/dist/models/group-privilege.entity.js.map +0 -1
  590. package/dist/models/group-reporting-user.entity.d.ts +0 -18
  591. package/dist/models/group-reporting-user.entity.d.ts.map +0 -1
  592. package/dist/models/group-reporting-user.entity.js +0 -117
  593. package/dist/models/group-reporting-user.entity.js.map +0 -1
  594. package/dist/models/group-system-access.entity.d.ts +0 -19
  595. package/dist/models/group-system-access.entity.d.ts.map +0 -1
  596. package/dist/models/group-system-access.entity.js +0 -102
  597. package/dist/models/group-system-access.entity.js.map +0 -1
  598. package/dist/models/group.entity.d.ts +0 -33
  599. package/dist/models/group.entity.d.ts.map +0 -1
  600. package/dist/models/group.entity.js +0 -157
  601. package/dist/models/group.entity.js.map +0 -1
  602. package/dist/models/login-history.entity.d.ts +0 -15
  603. package/dist/models/login-history.entity.d.ts.map +0 -1
  604. package/dist/models/login-history.entity.js +0 -81
  605. package/dist/models/login-history.entity.js.map +0 -1
  606. package/dist/models/staff.entity.d.ts +0 -19
  607. package/dist/models/staff.entity.d.ts.map +0 -1
  608. package/dist/models/staff.entity.js +0 -113
  609. package/dist/models/staff.entity.js.map +0 -1
  610. package/dist/models/system-privilege.entity.d.ts +0 -19
  611. package/dist/models/system-privilege.entity.d.ts.map +0 -1
  612. package/dist/models/system-privilege.entity.js +0 -111
  613. package/dist/models/system-privilege.entity.js.map +0 -1
  614. package/dist/models/system.entity.d.ts +0 -26
  615. package/dist/models/system.entity.d.ts.map +0 -1
  616. package/dist/models/system.entity.js +0 -139
  617. package/dist/models/system.entity.js.map +0 -1
  618. package/dist/models/user-group.entity.d.ts +0 -20
  619. package/dist/models/user-group.entity.d.ts.map +0 -1
  620. package/dist/models/user-group.entity.js +0 -114
  621. package/dist/models/user-group.entity.js.map +0 -1
  622. package/dist/models/user-object-privilege.entity.d.ts +0 -21
  623. package/dist/models/user-object-privilege.entity.d.ts.map +0 -1
  624. package/dist/models/user-object-privilege.entity.js +0 -113
  625. package/dist/models/user-object-privilege.entity.js.map +0 -1
  626. package/dist/models/user-privilege.entity.d.ts +0 -18
  627. package/dist/models/user-privilege.entity.d.ts.map +0 -1
  628. package/dist/models/user-privilege.entity.js +0 -99
  629. package/dist/models/user-privilege.entity.js.map +0 -1
  630. package/dist/models/user-system-access.entity.d.ts +0 -18
  631. package/dist/models/user-system-access.entity.d.ts.map +0 -1
  632. package/dist/models/user-system-access.entity.js +0 -108
  633. package/dist/models/user-system-access.entity.js.map +0 -1
  634. package/dist/models/user.entity.d.ts +0 -41
  635. package/dist/models/user.entity.d.ts.map +0 -1
  636. package/dist/models/user.entity.js +0 -218
  637. package/dist/models/user.entity.js.map +0 -1
  638. package/dist/redis-client/__mocks__/jest-initial-setup.d.ts +0 -2
  639. package/dist/redis-client/__mocks__/jest-initial-setup.d.ts.map +0 -1
  640. package/dist/redis-client/__mocks__/jest-initial-setup.js +0 -8
  641. package/dist/redis-client/__mocks__/jest-initial-setup.js.map +0 -1
  642. package/dist/redis-client/__mocks__/redis-mock.d.ts +0 -3
  643. package/dist/redis-client/__mocks__/redis-mock.d.ts.map +0 -1
  644. package/dist/redis-client/__mocks__/redis-mock.js +0 -56
  645. package/dist/redis-client/__mocks__/redis-mock.js.map +0 -1
  646. package/dist/redis-client/index.d.ts +0 -2
  647. package/dist/redis-client/index.d.ts.map +0 -1
  648. package/dist/redis-client/index.js +0 -18
  649. package/dist/redis-client/index.js.map +0 -1
  650. package/dist/redis-client/redis.service.d.ts +0 -10
  651. package/dist/redis-client/redis.service.d.ts.map +0 -1
  652. package/dist/redis-client/redis.service.js +0 -69
  653. package/dist/redis-client/redis.service.js.map +0 -1
  654. package/dist/session/index.d.ts +0 -3
  655. package/dist/session/index.d.ts.map +0 -1
  656. package/dist/session/index.js +0 -19
  657. package/dist/session/index.js.map +0 -1
  658. package/dist/session/interfaces/index.d.ts +0 -2
  659. package/dist/session/interfaces/index.d.ts.map +0 -1
  660. package/dist/session/interfaces/index.js +0 -18
  661. package/dist/session/interfaces/index.js.map +0 -1
  662. package/dist/session/interfaces/session-service.interface.d.ts +0 -10
  663. package/dist/session/interfaces/session-service.interface.d.ts.map +0 -1
  664. package/dist/session/interfaces/session-service.interface.js +0 -3
  665. package/dist/session/interfaces/session-service.interface.js.map +0 -1
  666. package/dist/session/session.service.d.ts +0 -14
  667. package/dist/session/session.service.d.ts.map +0 -1
  668. package/dist/session/session.service.js +0 -73
  669. package/dist/session/session.service.js.map +0 -1
  670. package/dist/types/auth-context.d.ts +0 -7
  671. package/dist/types/auth-context.d.ts.map +0 -1
  672. package/dist/types/auth-context.js +0 -3
  673. package/dist/types/auth-context.js.map +0 -1
  674. package/dist/types/index.d.ts +0 -2
  675. package/dist/types/index.d.ts.map +0 -1
  676. package/dist/types/index.js +0 -18
  677. package/dist/types/index.js.map +0 -1
@@ -1,2284 +1,2284 @@
1
- import { ClassError, ObjectBase, TreeNodeBase } from '@tomei/general';
2
- import { GroupRepository } from './group.repository';
3
- import { IGroupAttr } from '../../interfaces/group.interface';
4
- import { GroupTypeEnum } from '../../enum';
5
- import { LoginUser } from '../login-user/login-user';
6
- import { IGroupSearchAttr } from '../../interfaces/group-search-attr.interface';
7
- import { ApplicationConfig } from '@tomei/config';
8
- import { Op, Transaction } from 'sequelize';
9
- import { ActionEnum, Activity } from '@tomei/activity-history';
10
- import { GroupSystemAccessRepository } from '../group-system-access/group-system-access.repository';
11
- import SystemModel from '../../models/system.entity';
12
- import { GroupSystemAccess } from '../group-system-access';
13
- import { RedisService } from '../../redis-client/redis.service';
14
- import SystemPrivilegeModel from '../../models/system-privilege.entity';
15
- import { GroupPrivilegeRepository } from '../group-privilege/group-privilege.repository';
16
- import { SystemPrivilege } from '../system-privilege/system-privilege';
17
- import GroupPrivilegeModel from '../../models/group-privilege.entity';
18
- import { GroupObjectPrivilegeRepository } from '../group-object-privilege/group-object-privilege.repository';
19
- import { GroupObjectPrivilege } from '../group-object-privilege/group-object-privilege';
20
- import { GroupPrivilege } from '../group-privilege/group-privilege';
21
- import { User } from '../login-user/user';
22
- import GroupReportingUserModel from '../../models/group-reporting-user.entity';
23
- import GroupModel from '../../models/group.entity';
24
- import UserModel from '../../models/user.entity';
25
- import { UserGroup } from '../user-group/user-group';
26
-
27
- export class Group extends TreeNodeBase<Group> {
28
- ObjectId: string;
29
- ObjectName: string;
30
- TableName: 'sso_Group';
31
- ObjectType = 'Group';
32
-
33
- Name: string;
34
- Description: string;
35
- Type: GroupTypeEnum;
36
- ParentGroupCode: string;
37
- InheritParentPrivilegeYN: string;
38
- InheritParentSystemAccessYN: string;
39
- Status: string;
40
- ParentGroup?: any;
41
- _Path: string = '';
42
- isChildrenLoaded = false;
43
- isParentLoaded = false;
44
-
45
- private _CreatedById: number;
46
- private _CreatedAt: Date;
47
- private _UpdatedById: number;
48
- private _UpdatedAt: Date;
49
- private static _Repo = new GroupRepository();
50
- private static _GroupSystemAccessRepo = new GroupSystemAccessRepository();
51
- private static _GroupPrivilegeRepo = new GroupPrivilegeRepository();
52
- private static _GroupObjectPrivilegeRepo =
53
- new GroupObjectPrivilegeRepository();
54
- private static _RedisService: RedisService;
55
-
56
- get GroupCode(): string {
57
- return this.ObjectId;
58
- }
59
-
60
- set GroupCode(value: string) {
61
- this.ObjectId = value;
62
- }
63
-
64
- get CreatedById(): number {
65
- return this._CreatedById;
66
- }
67
-
68
- get CreatedAt(): Date {
69
- return this._CreatedAt;
70
- }
71
-
72
- get UpdatedById(): number {
73
- return this._UpdatedById;
74
- }
75
-
76
- get UpdatedAt(): Date {
77
- return this._UpdatedAt;
78
- }
79
-
80
- get Path(): string {
81
- return this._Path;
82
- }
83
-
84
- set Path(value: string) {
85
- this._Path = value;
86
- }
87
-
88
- private constructor(groupAttr?: IGroupAttr) {
89
- super();
90
- if (groupAttr) {
91
- this.GroupCode = groupAttr.GroupCode;
92
- this.Name = groupAttr.Name;
93
- this.Description = groupAttr?.Description;
94
- this.Type = groupAttr?.Type;
95
- this.ParentGroupCode = groupAttr?.ParentGroupCode;
96
- this.InheritParentPrivilegeYN = groupAttr?.InheritParentPrivilegeYN;
97
- this.InheritParentSystemAccessYN = groupAttr?.InheritParentSystemAccessYN;
98
- this.Status = groupAttr?.Status;
99
- this._Path = groupAttr?.Path;
100
- this._CreatedById = groupAttr.CreatedById;
101
- this._CreatedAt = groupAttr.CreatedAt;
102
- this._UpdatedById = groupAttr.UpdatedById;
103
- this._UpdatedAt = groupAttr.UpdatedAt;
104
- }
105
- }
106
-
107
- public static async init(dbTransaction: any, GroupCode?: string) {
108
- try {
109
- Group._RedisService = await RedisService.init();
110
- if (GroupCode) {
111
- const group = await Group._Repo.findByPk(GroupCode, {
112
- transaction: dbTransaction,
113
- });
114
- if (group) {
115
- return new Group(group);
116
- } else {
117
- throw Error('Group not found');
118
- }
119
- }
120
- return new Group();
121
- } catch (error) {
122
- throw new ClassError(
123
- 'Group',
124
- 'GroupErrMsg01',
125
- 'Failed To Initialize Group',
126
- );
127
- }
128
- }
129
-
130
- async loadChildren(dbTransaction?: any) {
131
- if (!this.GroupCode) {
132
- throw Error('GroupCode is missing.');
133
- }
134
-
135
- const children = await Group._Repo.findAll({
136
- where: { ParentGroupCode: this.GroupCode },
137
- order: [['CreatedAt', 'ASC']],
138
- transaction: dbTransaction,
139
- });
140
-
141
- this.children = children.map((child) => {
142
- return new Group(child.get({ plain: true }));
143
- });
144
-
145
- this.isChildrenLoaded = true;
146
- }
147
-
148
- async loadParent(dbTransaction?: any) {
149
- if (!this.GroupCode) {
150
- throw Error('GroupCode are missing.');
151
- }
152
-
153
- if (this.ParentGroupCode) {
154
- if (this.ParentGroupCode !== this.GroupCode) {
155
- const parent = await Group._Repo.findByPk(this.ParentGroupCode, {
156
- transaction: dbTransaction,
157
- });
158
- this.parent = new Group(parent.get({ plain: true }));
159
- }
160
- }
161
-
162
- this.isParentLoaded = true;
163
- }
164
-
165
- async isLeaf(dbTransaction?: any): Promise<boolean> {
166
- if (!this.isChildrenLoaded) {
167
- await this.loadChildren(dbTransaction);
168
- }
169
-
170
- return this.children.length === 0;
171
- }
172
-
173
- async getPath(dbTransaction?: any): Promise<string> {
174
- if (!this.isParentLoaded) {
175
- await this.loadParent(dbTransaction);
176
- }
177
-
178
- if (this.parent) {
179
- this._Path =
180
- (await this.parent.getPath(dbTransaction)) + '/' + this.GroupCode;
181
- return this._Path;
182
- }
183
- this._Path = this.GroupCode;
184
- return this._Path;
185
- }
186
-
187
- protected async updatePath(dbTransaction?: any): Promise<void> {
188
- const path = await this.getPath(dbTransaction);
189
- this._Path = path;
190
- }
191
-
192
- async setParent(parent: Group, dbTransaction?: any): Promise<void> {
193
- this.parent = parent;
194
- await this.updatePath(dbTransaction);
195
- }
196
-
197
- async getPathDetail(dbTransaction?: any): Promise<Group[]> {
198
- const path = this._Path.split('/');
199
- const groups: Group[] = [];
200
- for (let i = 0; i < path.length; i++) {
201
- const group = await Group.init(dbTransaction, path[i]);
202
- groups.push(group);
203
- }
204
- return groups;
205
- }
206
-
207
- public static async findAll(
208
- page: number,
209
- row: number,
210
- dbTransaction: any,
211
- loginUser: LoginUser,
212
- search?: IGroupSearchAttr,
213
- ) {
214
- //This method will list all group based on the query params.
215
- //Part 1: Privilege Checking
216
- const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
217
- const isPrivileged = await loginUser.checkPrivileges(
218
- systemCode,
219
- 'GROUP_LIST',
220
- );
221
-
222
- if (!isPrivileged) {
223
- throw new ClassError(
224
- 'Group',
225
- 'GroupErrMsg04',
226
- 'User is not privileged to list group',
227
- );
228
- }
229
-
230
- //Part 2: Retrieve listing
231
- const queryObj: any = {};
232
-
233
- let options: any = {
234
- transaction: dbTransaction,
235
- };
236
-
237
- if (page && row) {
238
- options = {
239
- ...options,
240
- limit: row,
241
- offset: row * (page - 1),
242
- order: [['CreatedAt', 'DESC']],
243
- distinct: true,
244
- };
245
- }
246
-
247
- if (search) {
248
- Object.entries(search).forEach(([key, value]) => {
249
- queryObj[key] = {
250
- [Op.substring]: value,
251
- };
252
- });
253
-
254
- options = {
255
- ...options,
256
- where: queryObj,
257
- };
258
-
259
- const result = await Group._Repo.findAllWithPagination(options);
260
-
261
- //Map the result to Group instance
262
- return {
263
- Count: result.count,
264
- Groups: result.rows.map(
265
- (group) => new Group(group.get({ plain: true })),
266
- ),
267
- };
268
- }
269
- }
270
-
271
- public static async create(
272
- loginUser: LoginUser,
273
- dbTransaction: any,
274
- group: Group,
275
- ) {
276
- try {
277
- //Part 1: Privilege Checking
278
- const systemCode =
279
- ApplicationConfig.getComponentConfigValue('system-code');
280
- const isPrivileged = await loginUser.checkPrivileges(
281
- systemCode,
282
- 'GROUP_CREATE',
283
- );
284
- if (!isPrivileged) {
285
- throw new Error('You do not have permission to create group');
286
- }
287
-
288
- //Part 2: Validation
289
- if (!group.GroupCode) {
290
- throw new ClassError(
291
- 'Group',
292
- 'GroupErrMsg02',
293
- 'Group Code is required',
294
- );
295
- }
296
-
297
- if (!group.Name) {
298
- throw new ClassError(
299
- 'Group',
300
- 'GroupErrMsg02',
301
- 'Group Name is required',
302
- );
303
- }
304
-
305
- if (!group.Type) {
306
- throw new ClassError(
307
- 'Group',
308
- 'GroupErrMsg02',
309
- 'Group Type is required',
310
- );
311
- }
312
-
313
- //Check if group code is unique
314
- const existingGroupCode = await Group._Repo.findByPk(group.GroupCode, {
315
- transaction: dbTransaction,
316
- });
317
-
318
- if (existingGroupCode) {
319
- throw new ClassError(
320
- 'Group',
321
- 'GroupErrMsg03',
322
- 'Duplicate GroupCode found.',
323
- );
324
- }
325
-
326
- //Validate parent group code if passed. Call Group._Repo.findByPk
327
- if (group.ParentGroupCode) {
328
- const parentGroup = await Group._Repo.findByPk(group.ParentGroupCode, {
329
- transaction: dbTransaction,
330
- });
331
-
332
- if (!parentGroup) {
333
- throw new ClassError(
334
- 'Group',
335
- 'GroupErrMsg04',
336
- 'ParentGroupCode is not found.',
337
- );
338
- }
339
-
340
- //If Params.group.GroupCode = Params.group?.ParentGroupCode, throw new ClassError
341
- if (group.GroupCode === group.ParentGroupCode) {
342
- throw new ClassError(
343
- 'Group',
344
- 'GroupErrMsg05',
345
- 'GroupCode and ParentGroupCode cannot be the same.',
346
- );
347
- }
348
- }
349
-
350
- //Part 3: Create Group
351
- //Initialise new Group instance and populate
352
- const newGroup = new Group(group);
353
- newGroup.ObjectId = group.GroupCode;
354
- newGroup.Name = group.Name;
355
- newGroup.Type = group.Type;
356
- newGroup.Description = group.Description;
357
- newGroup.ParentGroupCode = group.ParentGroupCode;
358
- newGroup.InheritParentPrivilegeYN = group.InheritParentPrivilegeYN;
359
- newGroup.InheritParentSystemAccessYN = group.InheritParentSystemAccessYN;
360
- newGroup.Status = 'Active';
361
- newGroup._CreatedById = loginUser.UserId;
362
- newGroup._UpdatedById = loginUser.UserId;
363
- newGroup._Path = await newGroup.getPath(dbTransaction);
364
-
365
- //Call Group._Repo create method
366
- const entityGroupAfter = {
367
- GroupCode: newGroup.ObjectId,
368
- Name: newGroup.Name,
369
- Type: newGroup.Type,
370
- Description: newGroup.Description,
371
- ParentGroupCode: newGroup.ParentGroupCode,
372
- InheritParentPrivilegeYN: newGroup.InheritParentPrivilegeYN,
373
- InheritParentSystemAccessYN: newGroup.InheritParentSystemAccessYN,
374
- Path: newGroup._Path,
375
- Status: newGroup.Status,
376
- CreatedById: newGroup._CreatedById,
377
- UpdatedById: newGroup._UpdatedById,
378
- CreatedAt: newGroup._CreatedAt,
379
- UpdatedAt: newGroup._UpdatedAt,
380
- };
381
-
382
- await Group._Repo.create(entityGroupAfter, {
383
- transaction: dbTransaction,
384
- });
385
-
386
- //Part 4: Record Create Group Activity and return newGroup
387
-
388
- const entityValueBefore = {};
389
-
390
- //Instantiate new activity
391
- const activity = new Activity();
392
- activity.ActivityId = activity.createId();
393
- activity.Action = ActionEnum.CREATE;
394
- activity.Description = 'Create Group';
395
- activity.EntityType = 'Group';
396
- activity.EntityId = newGroup.ObjectId;
397
- activity.EntityValueBefore = JSON.stringify(entityValueBefore);
398
- activity.EntityValueAfter = JSON.stringify(entityGroupAfter);
399
-
400
- //Call Activity.create method
401
- await activity.create(loginUser.ObjectId, dbTransaction);
402
-
403
- return newGroup;
404
- } catch (error) {
405
- throw error;
406
- }
407
- }
408
-
409
- protected static async checkDuplicateGroupCode(
410
- dbTransaction: any,
411
- GroupCode,
412
- ) {
413
- const isGroupCodeExist = await Group._Repo.findOne({
414
- where: { GroupCode },
415
- transaction: dbTransaction,
416
- });
417
-
418
- if (isGroupCodeExist) {
419
- throw new ClassError(
420
- 'Group',
421
- 'GroupErrMsg07',
422
- 'GroupCode already exists.',
423
- );
424
- }
425
- }
426
-
427
- private async updateChildrenPath(oldGroupCode: string, dbTransaction: any) {
428
- try {
429
- const isLeaf = await this.isLeaf(dbTransaction);
430
- if (isLeaf) {
431
- return;
432
- }
433
-
434
- const childrens = await Group._Repo.findAll({
435
- where: {
436
- Path: {
437
- [Op.like]: `${oldGroupCode}/%`,
438
- },
439
- },
440
- transaction: dbTransaction,
441
- });
442
-
443
- childrens.forEach(async (children) => {
444
- //Break the path into array with oldGroupCode/ as separator;
445
- const path = children.Path.split(`${oldGroupCode}/`);
446
- //Retrive the last element of the array
447
- const childPath = path[1];
448
- //Combine the childPath with this.Path then save it to the children.Path
449
- await children.update(
450
- { Path: `${this._Path}/${childPath}` },
451
- { transaction: dbTransaction },
452
- );
453
- });
454
- } catch (error) {
455
- throw error;
456
- }
457
- }
458
-
459
- public async update(
460
- loginUser: LoginUser,
461
- dbTransaction: any,
462
- group: {
463
- GroupCode: string;
464
- NewGroupCode?: string;
465
- Name: string;
466
- Description: string;
467
- Type: GroupTypeEnum;
468
- ParentGroupCode: string;
469
- InheritParentPrivilegeYN: string;
470
- InheritParentSystemAccessYN: string;
471
- Status: string;
472
- },
473
- ) {
474
- //Part 1: Privilege Checking
475
- const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
476
- const isPrivileged = await loginUser.checkPrivileges(
477
- systemCode,
478
- 'GROUP_UPDATE',
479
- );
480
-
481
- if (!isPrivileged) {
482
- throw new ClassError(
483
- 'Group',
484
- 'GroupErrMsg06',
485
- 'You do not have the privilege to update Group',
486
- );
487
- }
488
- try {
489
- if (group.NewGroupCode) {
490
- await Group.checkDuplicateGroupCode(dbTransaction, group.NewGroupCode);
491
- }
492
-
493
- const entityValueBefore = {
494
- GroupCode: this.GroupCode,
495
- Name: this.Name,
496
- Type: this.Type,
497
- Description: this.Description,
498
- ParentGroupCode: this.ParentGroupCode,
499
- InheritParentPrivilegeYN: this.InheritParentPrivilegeYN,
500
- InheritParentSystemAccessYN: this.InheritParentSystemAccessYN,
501
- Path: this.Path,
502
- Status: this.Status,
503
- CreatedById: this._CreatedById,
504
- UpdatedById: this._UpdatedById,
505
- CreatedAt: this._CreatedAt,
506
- UpdatedAt: this._UpdatedAt,
507
- };
508
-
509
- let isPathChanged = false;
510
- const oldGroupCode = this.GroupCode;
511
- if (group.NewGroupCode) {
512
- this.GroupCode = group.NewGroupCode;
513
- isPathChanged = true;
514
- }
515
-
516
- //Check if ParentGroupCode is changed or added
517
- if (
518
- (group.ParentGroupCode &&
519
- this.ParentGroupCode !== group.ParentGroupCode) ||
520
- (group.ParentGroupCode && !this.ParentGroupCode)
521
- ) {
522
- const parentGroup = await Group.init(
523
- dbTransaction,
524
- group.ParentGroupCode,
525
- );
526
- if (!parentGroup) {
527
- throw new ClassError(
528
- 'Group',
529
- 'GroupErrMsg08',
530
- 'Parent Group Code not found',
531
- );
532
- }
533
- await this.setParent(parentGroup);
534
- //Check if ParentGroupCode is removed
535
- isPathChanged = true;
536
- } else if (!group.ParentGroupCode && this.ParentGroupCode) {
537
- await this.setParent(null);
538
- isPathChanged = true;
539
- }
540
-
541
- if (isPathChanged) {
542
- await this.updateChildrenPath(oldGroupCode, dbTransaction);
543
- }
544
-
545
- this.Name = group?.Name || this.Name;
546
- this.Type = group?.Type || this.Type;
547
- this.Description = group?.Description || this.Description;
548
- this.ParentGroupCode = group?.ParentGroupCode || this.ParentGroupCode;
549
- this.InheritParentPrivilegeYN =
550
- group?.InheritParentPrivilegeYN || this.InheritParentPrivilegeYN;
551
- this.InheritParentSystemAccessYN =
552
- group?.InheritParentSystemAccessYN || this.InheritParentSystemAccessYN;
553
- this.Status = group?.Status || this.Status;
554
- this._UpdatedById = loginUser.UserId;
555
- this._UpdatedAt = new Date();
556
-
557
- await Group._Repo.update(
558
- {
559
- GroupCode: this.GroupCode,
560
- Name: this.Name,
561
- Type: this.Type,
562
- Description: this.Description,
563
- ParentGroupCode: this.ParentGroupCode,
564
- InheritParentPrivilegeYN: this.InheritParentPrivilegeYN,
565
- InheritParentSystemAccessYN: this.InheritParentSystemAccessYN,
566
- Status: this.Status,
567
- Path: this._Path,
568
- UpdatedById: this._UpdatedById,
569
- UpdatedAt: this._UpdatedAt,
570
- },
571
- {
572
- where: {
573
- GroupCode: group.GroupCode,
574
- },
575
- transaction: dbTransaction,
576
- },
577
- );
578
-
579
- const entityValueAfter = {
580
- GroupCode: this.GroupCode,
581
- Name: this.Name,
582
- Type: this.Type,
583
- Description: this.Description,
584
- ParentGroupCode: this.ParentGroupCode,
585
- InheritParentPrivilegeYN: this.InheritParentPrivilegeYN,
586
- InheritParentSystemAccessYN: this.InheritParentSystemAccessYN,
587
- Status: this.Status,
588
- Path: this._Path,
589
- CreatedById: this._CreatedById,
590
- UpdatedById: this._UpdatedById,
591
- CreatedAt: this._CreatedAt,
592
- UpdatedAt: this._UpdatedAt,
593
- };
594
-
595
- const activity = new Activity();
596
- activity.ActivityId = activity.createId();
597
- activity.Action = ActionEnum.UPDATE;
598
- activity.Description = `Update Group ${group.Type}`;
599
- activity.EntityType = 'Group';
600
- activity.EntityId = group.GroupCode;
601
- activity.EntityValueBefore = JSON.stringify(entityValueBefore);
602
- activity.EntityValueAfter = JSON.stringify(entityValueAfter);
603
- await activity.create(loginUser.ObjectId, dbTransaction);
604
-
605
- return this;
606
- } catch (error) {
607
- throw error;
608
- }
609
- }
610
-
611
- public static async delete(
612
- loginUser: LoginUser,
613
- dbTransaction: any,
614
- GroupCode: string,
615
- ) {
616
- // Part 1: Privilege Checking
617
- const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
618
-
619
- const isPrivileged = await loginUser.checkPrivileges(
620
- systemCode,
621
- 'GROUP_DELETE',
622
- );
623
-
624
- if (!isPrivileged) {
625
- throw new ClassError(
626
- 'Group',
627
- 'GroupErrMsg03',
628
- 'You do not have the privilege to delete groups records.',
629
- );
630
- }
631
- try {
632
- const group = await Group.init(dbTransaction, GroupCode);
633
-
634
- if (group.Status === 'Active') {
635
- throw new ClassError(
636
- 'Group',
637
- 'GroupErrMsg03',
638
- 'Active Group cant be deleted',
639
- );
640
- }
641
-
642
- const relatedGroup = await Group.findAll(
643
- 1,
644
- Number.MAX_SAFE_INTEGER,
645
- dbTransaction,
646
- loginUser,
647
- {
648
- ParentGroupCode: GroupCode,
649
- },
650
- );
651
-
652
- if (relatedGroup.Count > 0) {
653
- const listOfRelatedGroup = relatedGroup.Groups.map((group) => {
654
- return group.GroupCode;
655
- });
656
- throw new ClassError(
657
- 'Group',
658
- 'GroupErrMsg03',
659
- `Group still has associated user group ${listOfRelatedGroup}`,
660
- );
661
- }
662
-
663
- await Group._Repo.delete(GroupCode, dbTransaction);
664
-
665
- const EntityValueBefore = {
666
- GroupCode: group.GroupCode,
667
- Name: group.Name,
668
- Type: group.Type,
669
- Description: group.Description,
670
- ParentGroupCode: group.ParentGroupCode,
671
- InheritParentPrivilegeYN: group.InheritParentPrivilegeYN,
672
- InheritParentSystemAccessYN: group.InheritParentSystemAccessYN,
673
- Status: group.Status,
674
- CreatedById: group._CreatedById,
675
- UpdatedById: group._UpdatedById,
676
- CreatedAt: group._CreatedAt,
677
- UpdatedAt: group._UpdatedAt,
678
- };
679
-
680
- const activity = new Activity();
681
- activity.ActivityId = activity.createId();
682
- activity.Action = ActionEnum.DELETE;
683
- activity.Description = 'Delete Group';
684
- activity.EntityType = 'Group';
685
- activity.EntityId = group.ObjectId;
686
- activity.EntityValueBefore = JSON.stringify(EntityValueBefore);
687
- activity.EntityValueAfter = JSON.stringify({});
688
-
689
- await activity.create(loginUser.ObjectId, dbTransaction);
690
-
691
- return { Message: 'Group removed.' };
692
- } catch (error) {
693
- throw error;
694
- }
695
- }
696
-
697
- public static async getSystemAccesses(
698
- loginUser: LoginUser,
699
- dbTransaction: any,
700
- GroupCode: string,
701
- Page: number,
702
- Rows: number,
703
- Search: {
704
- SystemCode?: string;
705
- Status?: string;
706
- },
707
- ) {
708
- // Part 1: Privilege Checking
709
- const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
710
- const isPrivileged = await loginUser.checkPrivileges(
711
- systemCode,
712
- 'SYSTEM_ACCESS_VIEW',
713
- );
714
-
715
- if (!isPrivileged) {
716
- throw new ClassError(
717
- 'Group',
718
- 'GroupErrMsg06',
719
- 'You do not have the privilege to view system access',
720
- );
721
- }
722
-
723
- try {
724
- // Part 2: Validation
725
- await Group.init(dbTransaction, GroupCode);
726
-
727
- // Part 3: Retrieve System Access and returns
728
- const queryObj: any = { GroupCode: GroupCode };
729
-
730
- if (Search) {
731
- Object.entries(Search).forEach(([key, value]) => {
732
- queryObj[key] = value;
733
- });
734
- }
735
-
736
- let options: any = {
737
- where: queryObj,
738
- distinct: true,
739
- transaction: dbTransaction,
740
- };
741
-
742
- if (Page && Rows) {
743
- options = {
744
- ...options,
745
- limit: Rows,
746
- offset: Rows * (Page - 1),
747
- order: [['CreatedAt', 'DESC']],
748
- };
749
- }
750
-
751
- const systemAccess =
752
- await Group._GroupSystemAccessRepo.findAndCountAll(options);
753
- return systemAccess;
754
- } catch (error) {
755
- return error;
756
- }
757
- }
758
-
759
- public static async getSystemAccessRoles(
760
- loginUser: LoginUser,
761
- dbTransaction: any,
762
- SystemCode: string,
763
- Page: number,
764
- Rows: number,
765
- Search: {
766
- GroupCode?: string;
767
- Status?: string;
768
- },
769
- ) {
770
- // Part 1: Privilege Checking
771
- const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
772
- const isPrivileged = await loginUser.checkPrivileges(
773
- systemCode,
774
- 'SYSTEM_ACCESS_VIEW',
775
- );
776
-
777
- if (!isPrivileged) {
778
- throw new ClassError(
779
- 'Group',
780
- 'GroupErrMsg06',
781
- 'You do not have the privilege to view system access',
782
- );
783
- }
784
-
785
- try {
786
- // Part 2: Retrieve System Access and returns
787
- const queryObj: any = { SystemCode: SystemCode };
788
-
789
- if (Search) {
790
- Object.entries(Search).forEach(([key, value]) => {
791
- queryObj[key] = value;
792
- });
793
- }
794
-
795
- let options: any = {
796
- where: queryObj,
797
- distinct: true,
798
- transaction: dbTransaction,
799
- };
800
-
801
- if (Page && Rows) {
802
- options = {
803
- ...options,
804
- limit: Rows,
805
- offset: Rows * (Page - 1),
806
- order: [['CreatedAt', 'DESC']],
807
- include: {
808
- model: GroupModel,
809
- where: {
810
- Type: 'Role',
811
- },
812
- },
813
- };
814
- }
815
-
816
- const systemAccess =
817
- await Group._GroupSystemAccessRepo.findAndCountAll(options);
818
- return systemAccess;
819
- } catch (error) {
820
- return error;
821
- }
822
- }
823
-
824
- private static async getInheritedSystemAccess(
825
- dbTransaction: any,
826
- group: Group,
827
- ): Promise<any[]> {
828
- const options: any = {
829
- where: {
830
- GroupCode: group.GroupCode,
831
- Status: 'Active',
832
- },
833
- include: [
834
- {
835
- model: SystemModel,
836
- },
837
- ],
838
- transaction: dbTransaction,
839
- };
840
- let systemAccess = await Group._GroupSystemAccessRepo.findAll(options);
841
-
842
- if (group.InheritParentSystemAccessYN === 'Y' && group.ParentGroupCode) {
843
- const parentGroup = await Group.init(
844
- dbTransaction,
845
- group.ParentGroupCode,
846
- );
847
- const parentSystemAccesses = await this.getInheritedSystemAccess(
848
- dbTransaction,
849
- parentGroup,
850
- );
851
- systemAccess = systemAccess.concat(parentSystemAccesses);
852
- }
853
- return systemAccess;
854
- }
855
-
856
- public static async isGroupCodeInHierarchy(
857
- dbTransaction: any,
858
- GroupCode: string,
859
- ListGroupCode: string[] = [],
860
- ): Promise<boolean> {
861
- ListGroupCode.push(GroupCode);
862
-
863
- const group = await Group._Repo.findOne({
864
- where: { GroupCode },
865
- transaction: dbTransaction,
866
- });
867
-
868
- if (group?.ParentGroupCode) {
869
- const isGroupCodeExist = ListGroupCode.includes(group.ParentGroupCode);
870
- if (!isGroupCodeExist) {
871
- await this.isGroupCodeInHierarchy(
872
- dbTransaction,
873
- group.ParentGroupCode,
874
- ListGroupCode,
875
- );
876
- }
877
- {
878
- return false;
879
- }
880
- } else {
881
- return true;
882
- }
883
- }
884
-
885
- public static async getParentSystemAccesses(
886
- loginUser: LoginUser,
887
- dbTransaction: any,
888
- GroupCode: string,
889
- ) {
890
- // Part 1: Privilege Checking
891
- const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
892
- const isPrivileged = await loginUser.checkPrivileges(
893
- systemCode,
894
- 'SYSTEM_ACCESS_VIEW',
895
- );
896
-
897
- if (!isPrivileged) {
898
- throw new ClassError(
899
- 'Group',
900
- 'GroupErrMsg06',
901
- 'You do not have the privilege to view system access',
902
- );
903
- }
904
-
905
- try {
906
- const group = await Group.init(dbTransaction, GroupCode);
907
- if (group.InheritParentSystemAccessYN !== 'Y' || !group.ParentGroupCode) {
908
- return [];
909
- } else {
910
- const parentGroup = await Group.init(
911
- dbTransaction,
912
- group.ParentGroupCode,
913
- );
914
- const inheritSystemAccess = await Group.getInheritedSystemAccess(
915
- dbTransaction,
916
- parentGroup,
917
- );
918
- return inheritSystemAccess;
919
- }
920
- } catch (error) {
921
- throw error;
922
- }
923
- }
924
-
925
- public static async addSystemAccesses(
926
- loginUser: LoginUser,
927
- dbTransaction: any,
928
- GroupCode: string,
929
- SystemCodes: string[],
930
- ) {
931
- // Part 1: Privilege Checking
932
- const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
933
- const isPrivileged = await loginUser.checkPrivileges(
934
- systemCode,
935
- 'SYSTEM_ACCESS_CREATE',
936
- );
937
-
938
- if (!isPrivileged) {
939
- throw new ClassError(
940
- 'Group',
941
- 'GroupErrMsg07',
942
- 'You do not have the privilege to create system access',
943
- );
944
- }
945
-
946
- try {
947
- if (SystemCodes.length > 0) {
948
- for (const element of SystemCodes) {
949
- const CurrentGroupSystemAccess = await Group.getSystemAccesses(
950
- loginUser,
951
- dbTransaction,
952
- GroupCode,
953
- 1,
954
- Number.MAX_SAFE_INTEGER,
955
- { SystemCode: element },
956
- );
957
-
958
- if (CurrentGroupSystemAccess?.count > 0) {
959
- throw new ClassError(
960
- 'Group',
961
- 'GroupErrMsg08',
962
- 'System access already exists',
963
- );
964
- }
965
-
966
- const groupSystemAccess = await GroupSystemAccess.init(dbTransaction);
967
- groupSystemAccess.createId();
968
- groupSystemAccess.GroupCode = GroupCode;
969
- groupSystemAccess.SystemCode = element;
970
- groupSystemAccess.Status = 'Active';
971
- groupSystemAccess.CreatedById = +loginUser.ObjectId;
972
- groupSystemAccess.CreatedAt = new Date();
973
- groupSystemAccess.UpdatedById = +loginUser.ObjectId;
974
- groupSystemAccess.UpdatedAt = new Date();
975
-
976
- const EntityValueAfter = {
977
- GroupCode: groupSystemAccess.GroupCode,
978
- SystemCode: groupSystemAccess.SystemCode,
979
- Status: groupSystemAccess.Status,
980
- CreatedById: groupSystemAccess.CreatedById,
981
- CreatedAt: groupSystemAccess.CreatedAt,
982
- UpdatedById: groupSystemAccess.UpdatedById,
983
- UpdatedAt: groupSystemAccess.UpdatedAt,
984
- };
985
-
986
- const systemAccess = await Group._GroupSystemAccessRepo.create(
987
- EntityValueAfter,
988
- {
989
- transaction: dbTransaction,
990
- },
991
- );
992
-
993
- const activity = new Activity();
994
- activity.ActivityId = activity.createId();
995
- activity.Action = ActionEnum.CREATE;
996
- activity.Description = 'Create Group System Access';
997
- activity.EntityType = 'GroupSystemAccess';
998
- activity.EntityId = systemAccess.GroupSystemAccessId?.toString();
999
- activity.EntityValueBefore = JSON.stringify({});
1000
- activity.EntityValueAfter = JSON.stringify(EntityValueAfter);
1001
-
1002
- await activity.create(loginUser.ObjectId, dbTransaction);
1003
- }
1004
-
1005
- return { Message: 'Successfully added.' };
1006
- }
1007
- } catch (error) {
1008
- throw error;
1009
- }
1010
- }
1011
-
1012
- public static async deleteSystemAccess(
1013
- loginUser: LoginUser,
1014
- dbTransaction: any,
1015
- GroupCode: string,
1016
- SystemCode: string,
1017
- ) {
1018
- // Part 1: Privilege Checking
1019
- const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
1020
- const isPrivileged = await loginUser.checkPrivileges(
1021
- systemCode,
1022
- 'SYSTEM_ACCESS_DELETE',
1023
- );
1024
-
1025
- if (!isPrivileged) {
1026
- throw new ClassError(
1027
- 'Group',
1028
- 'GroupErrMsg08',
1029
- 'You do not have the privilege to delete system access',
1030
- );
1031
- }
1032
-
1033
- try {
1034
- const currentGroupSystemAccess = await Group.getSystemAccesses(
1035
- loginUser,
1036
- dbTransaction,
1037
- GroupCode,
1038
- 1,
1039
- Number.MAX_SAFE_INTEGER,
1040
- { SystemCode: SystemCode },
1041
- );
1042
-
1043
- if (currentGroupSystemAccess.count < 1) {
1044
- throw new ClassError(
1045
- 'Group',
1046
- 'GroupErrMsg10',
1047
- 'No associated system access found.',
1048
- );
1049
- }
1050
-
1051
- await Group._GroupSystemAccessRepo.delete(
1052
- GroupCode,
1053
- SystemCode,
1054
- dbTransaction,
1055
- );
1056
-
1057
- const EntityValueBefore = {
1058
- GroupCode: currentGroupSystemAccess?.rows[0]?.GroupCode,
1059
- SystemCode: currentGroupSystemAccess?.rows[0]?.SystemCode,
1060
- Status: currentGroupSystemAccess?.rows[0]?.Status,
1061
- CreatedById: currentGroupSystemAccess?.rows[0]?.CreatedById,
1062
- CreatedAt: currentGroupSystemAccess?.rows[0]?.CreatedAt,
1063
- UpdatedById: currentGroupSystemAccess?.rows[0]?.UpdatedById,
1064
- UpdatedAt: currentGroupSystemAccess?.rows[0]?.UpdatedAt,
1065
- };
1066
-
1067
- const activity = new Activity();
1068
- activity.ActivityId = activity.createId();
1069
- activity.Action = ActionEnum.DELETE;
1070
- activity.Description = 'Delete Group System Access';
1071
- activity.EntityType = 'GroupSystemAccess';
1072
- activity.EntityId =
1073
- currentGroupSystemAccess?.rows[0]?.GroupSystemAccessId?.toString();
1074
- activity.EntityValueBefore = JSON.stringify(EntityValueBefore);
1075
- activity.EntityValueAfter = JSON.stringify({});
1076
-
1077
- await activity.create(loginUser.ObjectId, dbTransaction);
1078
-
1079
- return { Message: 'System access removed.', SystemCode: SystemCode };
1080
- } catch (error) {
1081
- throw error;
1082
- }
1083
- }
1084
-
1085
- public static async getSystemPrivileges(
1086
- loginUser: LoginUser,
1087
- dbTransaction: any,
1088
- GroupCode: string,
1089
- search?: {
1090
- SystemCode?: string;
1091
- Status?: string;
1092
- },
1093
- ) {
1094
- try {
1095
- //Part 1: Privilege Checking
1096
- const systemCode =
1097
- ApplicationConfig.getComponentConfigValue('system-code');
1098
- const isPrivileged = await loginUser.checkPrivileges(
1099
- systemCode,
1100
- 'GROUP_PRIVILEGE_VIEW',
1101
- );
1102
-
1103
- if (!isPrivileged) {
1104
- throw new ClassError(
1105
- 'Group',
1106
- 'GroupErrMsg11',
1107
- 'You do not have the privilege to view group privileges',
1108
- );
1109
- }
1110
-
1111
- //Set group to instantiation of existing Group
1112
- await Group.init(dbTransaction, GroupCode);
1113
-
1114
- //Part 3: Retrieve Group Own Privilege
1115
- //Retrieve group data and it's privileged by calling Group._Repo.findAll
1116
- let where: any = {
1117
- GroupCode,
1118
- };
1119
-
1120
- let systemWhere: any = {};
1121
-
1122
- if (search) {
1123
- if (search.Status) {
1124
- where = {
1125
- ...where,
1126
- Status: search.Status,
1127
- };
1128
- }
1129
-
1130
- if (search.SystemCode) {
1131
- systemWhere = {
1132
- SystemCode: {
1133
- [Op.substring]: search.SystemCode,
1134
- },
1135
- };
1136
- }
1137
- }
1138
-
1139
- const groupOwnPrivileges = await Group._GroupPrivilegeRepo.findAll({
1140
- where,
1141
- include: [
1142
- {
1143
- model: SystemPrivilegeModel,
1144
- where: systemWhere,
1145
- },
1146
- ],
1147
- transaction: dbTransaction,
1148
- });
1149
-
1150
- //Create variable called privileges and Map the SystemPrivilege data retrieved from 3.1 into SystemPrivilege object and push it to the privileges variables
1151
- const privileges: SystemPrivilege[] = [];
1152
-
1153
- for (const groupPrivilege of groupOwnPrivileges) {
1154
- const systemPrivilege = await SystemPrivilege.init(dbTransaction);
1155
- systemPrivilege.setAttributes(
1156
- groupPrivilege.Privilege.get({ plain: true }),
1157
- );
1158
- privileges.push(systemPrivilege);
1159
- }
1160
-
1161
- return privileges;
1162
- } catch (error) {
1163
- throw error;
1164
- }
1165
- }
1166
-
1167
- public static async getSystemPrivilegeRoles(
1168
- loginUser: LoginUser,
1169
- dbTransaction: any,
1170
- SystemCode: string,
1171
- search?: {
1172
- GroupCode?: string[];
1173
- Status?: string;
1174
- },
1175
- ) {
1176
- try {
1177
- //Part 1: Privilege Checking
1178
- const systemCode =
1179
- ApplicationConfig.getComponentConfigValue('system-code');
1180
- const isPrivileged = await loginUser.checkPrivileges(
1181
- systemCode,
1182
- 'GROUP_PRIVILEGE_VIEW',
1183
- );
1184
-
1185
- if (!isPrivileged) {
1186
- throw new ClassError(
1187
- 'Group',
1188
- 'GroupErrMsg11',
1189
- 'You do not have the privilege to view group privileges',
1190
- );
1191
- }
1192
-
1193
- //Part 2: Retrieve Roles Based on Privilege
1194
- //Retrieve Roles based on privilege on a system
1195
- let systemWhere: any = {};
1196
-
1197
- if (SystemCode) {
1198
- systemWhere = {
1199
- SystemCode: {
1200
- [Op.substring]: SystemCode,
1201
- },
1202
- };
1203
- }
1204
-
1205
- const groupCodesPrivileges: {
1206
- SystemPrivilegeId: string;
1207
- GroupCodes: { Code: string; Name: string }[];
1208
- }[] = [];
1209
-
1210
- const allGroupCodePrivileges = await Group._GroupPrivilegeRepo.findAll({
1211
- include: [
1212
- {
1213
- model: SystemPrivilegeModel,
1214
- where: systemWhere,
1215
- },
1216
- {
1217
- model: GroupModel,
1218
- where: {
1219
- Type: 'Role',
1220
- },
1221
- },
1222
- ],
1223
- transaction: dbTransaction,
1224
- });
1225
-
1226
- // Use a Map to group by SystemPrivilegeId
1227
- const privilegesMap = new Map<string, { Code: string; Name: string }[]>();
1228
-
1229
- for (const groupCodePrivilege of allGroupCodePrivileges) {
1230
- const { SystemPrivilegeId, GroupCode, Group } = groupCodePrivilege; // `Group` contains Name from GroupModel
1231
-
1232
- if (!privilegesMap.has(SystemPrivilegeId)) {
1233
- // Initialize with an empty array if not already present
1234
- privilegesMap.set(SystemPrivilegeId, []);
1235
- }
1236
-
1237
- // Add the GroupCode and Name to the array if it exists and is not already present
1238
- if (GroupCode && Group?.Name) {
1239
- const groupCodes = privilegesMap.get(SystemPrivilegeId);
1240
- const newGroupEntry = { Code: GroupCode, Name: Group.Name };
1241
-
1242
- // Ensure no duplicates
1243
- if (
1244
- groupCodes &&
1245
- !groupCodes.some(
1246
- (g) => g.Code === GroupCode && g.Name === Group.Name,
1247
- )
1248
- ) {
1249
- groupCodes.push(newGroupEntry);
1250
- }
1251
- }
1252
- }
1253
-
1254
- // Convert the Map to the desired array format
1255
- privilegesMap.forEach((groupCodes, SystemPrivilegeId) => {
1256
- groupCodesPrivileges.push({
1257
- SystemPrivilegeId,
1258
- GroupCodes: groupCodes,
1259
- });
1260
- });
1261
-
1262
- const allPrivileges = await SystemPrivilegeModel.findAll({
1263
- where: systemWhere,
1264
- transaction: dbTransaction,
1265
- });
1266
-
1267
- const groupPrivilegeRoles: {
1268
- SystemPrivilegeId: string;
1269
- PrivilegeCode: string;
1270
- Description: string;
1271
- GroupCodes: { Code: string; Name: string }[];
1272
- }[] = [];
1273
-
1274
- // Iterate through allPrivileges to check for matches in groupCodesPrivileges
1275
- for (const privilege of allPrivileges) {
1276
- const matchingGroupPrivilege = groupCodesPrivileges.find(
1277
- (groupPrivilege) =>
1278
- groupPrivilege.SystemPrivilegeId === privilege.SystemPrivilegeId,
1279
- );
1280
-
1281
- if (matchingGroupPrivilege) {
1282
- // If match is found, push to groupPrivilegeRoles with GroupCodes
1283
- groupPrivilegeRoles.push({
1284
- SystemPrivilegeId: privilege.SystemPrivilegeId,
1285
- PrivilegeCode: privilege.PrivilegeCode,
1286
- Description: privilege.Description,
1287
- GroupCodes: matchingGroupPrivilege.GroupCodes,
1288
- });
1289
- } else {
1290
- // If no match is found, push with an empty array of GroupCodes
1291
- groupPrivilegeRoles.push({
1292
- SystemPrivilegeId: privilege.SystemPrivilegeId,
1293
- PrivilegeCode: privilege.PrivilegeCode,
1294
- Description: privilege.Description,
1295
- GroupCodes: [],
1296
- });
1297
- }
1298
- }
1299
-
1300
- const filteredGroupPrivilegeRoles = groupPrivilegeRoles
1301
- .map((role) => {
1302
- if (search.GroupCode?.length) {
1303
- // Filter GroupCodes to only include matching Codes
1304
- const matchingGroupCodes = role.GroupCodes.filter((groupCode) =>
1305
- search.GroupCode.includes(groupCode.Code),
1306
- );
1307
-
1308
- // If there are no matching GroupCodes, exclude this role
1309
- if (matchingGroupCodes.length === 0) {
1310
- return null;
1311
- }
1312
-
1313
- // Return the role with filtered GroupCodes
1314
- return {
1315
- ...role,
1316
- GroupCodes: matchingGroupCodes,
1317
- };
1318
- }
1319
-
1320
- // If search.GroupCode is not provided, include all data
1321
- return role;
1322
- })
1323
- .filter(Boolean); // Remove any null values
1324
-
1325
- return filteredGroupPrivilegeRoles;
1326
- } catch (error) {
1327
- throw error;
1328
- }
1329
- }
1330
-
1331
- public static async getInheritedSystemPrivileges(
1332
- dbTransaction: any,
1333
- GroupCode: string,
1334
- search?: {
1335
- SystemCode?: string;
1336
- Status?: string;
1337
- PrivilegeCode?: string;
1338
- },
1339
- ): Promise<SystemPrivilege[]> {
1340
- try {
1341
- //Retrieve group data and it's privileges by calling Group._Repo.findAll
1342
- const where: any = {
1343
- GroupCode,
1344
- };
1345
-
1346
- let groupPrivilegeWhere: any = {};
1347
- let systemPrivilegeWhere: any = {};
1348
-
1349
- if (search) {
1350
- if (search.Status) {
1351
- groupPrivilegeWhere = {
1352
- Status: search.Status,
1353
- };
1354
- }
1355
-
1356
- if (search.SystemCode) {
1357
- systemPrivilegeWhere = {
1358
- SystemCode: {
1359
- [Op.substring]: search.SystemCode,
1360
- },
1361
- };
1362
- }
1363
-
1364
- if (search.PrivilegeCode) {
1365
- systemPrivilegeWhere = {
1366
- ...systemPrivilegeWhere,
1367
- PrivilegeCode: {
1368
- [Op.substring]: search.PrivilegeCode,
1369
- },
1370
- };
1371
- }
1372
- }
1373
- const group = await Group._Repo.findOne({
1374
- where: where,
1375
- include: [
1376
- {
1377
- model: GroupPrivilegeModel,
1378
- where: groupPrivilegeWhere,
1379
- separate: true,
1380
- include: [
1381
- {
1382
- model: SystemPrivilegeModel,
1383
- where: systemPrivilegeWhere,
1384
- },
1385
- ],
1386
- },
1387
- ],
1388
- transaction: dbTransaction,
1389
- });
1390
-
1391
- //Retrieve group object privileges by calling LoginUser._GroupObjectPrivilegeRepo.findAll
1392
- const objectWhere: any = {
1393
- GroupCode,
1394
- };
1395
- const systemWhere: any = {};
1396
- if (search) {
1397
- Object.entries(search).forEach(([key, value]) => {
1398
- if (key === 'Status') {
1399
- objectWhere[key] = {
1400
- [Op.substring]: value,
1401
- };
1402
- } else {
1403
- systemWhere[key] = {
1404
- [Op.substring]: value,
1405
- };
1406
- }
1407
- });
1408
- }
1409
- const groupObjectPrivileges =
1410
- await Group._GroupObjectPrivilegeRepo.findAll({
1411
- where: objectWhere,
1412
- include: [
1413
- {
1414
- model: SystemPrivilegeModel,
1415
- where: systemWhere,
1416
- },
1417
- ],
1418
- transaction: dbTransaction,
1419
- });
1420
-
1421
- //Map to SystemPrivilege object
1422
- let privileges: SystemPrivilege[] = [];
1423
- for (const groupPrivilege of group.GroupPrivileges) {
1424
- const systemPrivilege = await SystemPrivilege.init(dbTransaction);
1425
- systemPrivilege.setAttributes(
1426
- groupPrivilege.Privilege.get({ plain: true }),
1427
- );
1428
- privileges.push(systemPrivilege);
1429
- }
1430
-
1431
- for (const groupObjectPrivilege of groupObjectPrivileges) {
1432
- const systemPrivilege = await SystemPrivilege.init(dbTransaction);
1433
- systemPrivilege.setAttributes(
1434
- groupObjectPrivilege.Privilege.get({ plain: true }),
1435
- );
1436
- privileges.push(systemPrivilege);
1437
- }
1438
-
1439
- //Part 2: Retrieve Privileges Inherited from Parent Group
1440
- //if group data retrieved from 1.1 have InheritParentPrivilegeYN == "Y" and ParentGroupCode value is not empty. Call this method again
1441
- if (group.InheritParentPrivilegeYN === 'Y' && group.ParentGroupCode) {
1442
- const inheritedPrivileges = await Group.getInheritedSystemPrivileges(
1443
- dbTransaction,
1444
- group.ParentGroupCode,
1445
- search,
1446
- );
1447
- privileges = privileges.concat(inheritedPrivileges);
1448
- }
1449
-
1450
- //format to make sure no duplicate
1451
- const uniquePrivileges = Array.from(
1452
- new Set(privileges.map((a) => a.SystemPrivilegeId)),
1453
- ).map((SystemPrivilegeId) => {
1454
- return privileges.find(
1455
- (a) => a.SystemPrivilegeId === SystemPrivilegeId,
1456
- );
1457
- });
1458
-
1459
- return uniquePrivileges;
1460
- } catch (error) {
1461
- throw error;
1462
- }
1463
- }
1464
-
1465
- public static async getParentSystemPrivileges(
1466
- loginUser: LoginUser,
1467
- dbTransaction: any,
1468
- GroupCode: string,
1469
- search?: {
1470
- SystemCode?: string;
1471
- Status?: string;
1472
- PrivilegeCode?: string;
1473
- },
1474
- ): Promise<SystemPrivilege[]> {
1475
- try {
1476
- //Part 1: Privilege Checking
1477
- const systemCode =
1478
- ApplicationConfig.getComponentConfigValue('system-code');
1479
- const isPrivileged = await loginUser.checkPrivileges(
1480
- systemCode,
1481
- 'GROUP_PRIVILEGE_VIEW',
1482
- );
1483
-
1484
- if (!isPrivileged) {
1485
- throw new ClassError(
1486
- 'Group',
1487
- 'GroupErrMsg11',
1488
- 'You do not have the privilege to view group privileges',
1489
- );
1490
- }
1491
-
1492
- //Part 2: Validation
1493
- //Set group to instantiation of existing Group
1494
- const group = await Group.init(dbTransaction, GroupCode);
1495
- //Check if group.InheritParentPrivilegeYN == "Y" and ParentGroupCode value is not empty. if no then return an empty array
1496
- if (group.InheritParentPrivilegeYN !== 'Y' || !group.ParentGroupCode) {
1497
- return [];
1498
- }
1499
-
1500
- //Part 3: Retrieve Group Own Privilege
1501
- //Retrieve group data and it's privileged by calling Group.getIheritedSystemPrivileges
1502
- const privileges = await Group.getInheritedSystemPrivileges(
1503
- dbTransaction,
1504
- group.ParentGroupCode,
1505
- search,
1506
- );
1507
-
1508
- return privileges;
1509
- } catch (error) {
1510
- throw error;
1511
- }
1512
- }
1513
-
1514
- public static async assignGroupObjectPrivilege(
1515
- loginUser: LoginUser,
1516
- dbTransaction: any,
1517
- GroupCode: string,
1518
- GroupObjectPrivileges: GroupObjectPrivilege[],
1519
- SystemCode: string,
1520
- ): Promise<string> {
1521
- try {
1522
- //Part 1: Privilege Checking
1523
- const systemCode =
1524
- ApplicationConfig.getComponentConfigValue('system-code');
1525
- const isPrivileged = await loginUser.checkPrivileges(
1526
- systemCode,
1527
- 'GROUP_OBJECT_PRIVILEGE_ASSIGN',
1528
- );
1529
-
1530
- if (!isPrivileged) {
1531
- throw new ClassError(
1532
- 'Group',
1533
- 'GroupErrMsg12',
1534
- 'You do not have the privilege to assign group object privilege',
1535
- );
1536
- }
1537
-
1538
- //Part 2: Validation
1539
- //Initialise group with group init
1540
- const group = await Group.init(dbTransaction, GroupCode);
1541
- //Retrieve all group system access by calling Group.getSystemAccesses
1542
- const groupSystemAccesses = await Group.getSystemAccesses(
1543
- loginUser,
1544
- dbTransaction,
1545
- GroupCode,
1546
- 1,
1547
- Number.MAX_SAFE_INTEGER,
1548
- {},
1549
- );
1550
-
1551
- //If Group.InheritParentSystemAccess == "Y" and Group.ParentGroupCode exist, initialise parent group
1552
- let parentGroupSystemAccesses: any = {
1553
- rows: [],
1554
- count: 0,
1555
- };
1556
-
1557
- if (group.InheritParentSystemAccessYN === 'Y' && group.ParentGroupCode) {
1558
- //Retrieve all parent group system access by calling Group.getSystemAccesses
1559
- parentGroupSystemAccesses = await Group.getSystemAccesses(
1560
- loginUser,
1561
- dbTransaction,
1562
- group.ParentGroupCode,
1563
- 1,
1564
- Number.MAX_SAFE_INTEGER,
1565
- undefined,
1566
- );
1567
- }
1568
-
1569
- // For each Params.GroupObjectPrivileges.
1570
- for (const groupObjectPrivilege of GroupObjectPrivileges) {
1571
- //Initialise existing System privilege
1572
- const systemPrivilege = await SystemPrivilege.init(
1573
- dbTransaction,
1574
- groupObjectPrivilege.SystemPrivilegeId,
1575
- );
1576
- //Check whether the system codes used by that privilege is exist inside the group system access
1577
- const combinedSystemAccesses = {
1578
- ...groupSystemAccesses.rows,
1579
- ...parentGroupSystemAccesses.rows,
1580
- };
1581
- const systemAccess = combinedSystemAccesses.find(
1582
- (systemAccess) =>
1583
- systemAccess.SystemCode === systemPrivilege.SystemCode,
1584
- );
1585
- if (!systemAccess) {
1586
- throw new ClassError(
1587
- 'Group',
1588
- 'GroupErrMsg13',
1589
- 'Failed to assign privilege ' +
1590
- groupObjectPrivilege.SystemPrivilegeId +
1591
- ' due to non-existent system access.',
1592
- );
1593
- }
1594
-
1595
- //Check whether the group object privilege already exist by using Group._GroupObjectPrivilegesRepo.findOne
1596
- const groupObjectPrivilegeData =
1597
- await Group._GroupObjectPrivilegeRepo.findOne({
1598
- where: {
1599
- GroupCode,
1600
- SystemPrivilegeId: groupObjectPrivilege.SystemPrivilegeId,
1601
- ObjectId: groupObjectPrivilege.ObjectId,
1602
- ObjectType: groupObjectPrivilege.ObjectType,
1603
- },
1604
- transaction: dbTransaction,
1605
- });
1606
- //If GroupObjectPrivilege record exist. Skip this loop and proceed to the next privilege code
1607
- if (groupObjectPrivilegeData) {
1608
- continue;
1609
- } else {
1610
- //Call GroupObjectPrivilege.create
1611
- await GroupObjectPrivilege.create(
1612
- loginUser,
1613
- dbTransaction,
1614
- groupObjectPrivilege,
1615
- );
1616
- }
1617
- }
1618
-
1619
- return 'Successfully added.';
1620
- } catch (error) {
1621
- throw error;
1622
- }
1623
- }
1624
-
1625
- public static async getGroubObjectPrivileges(
1626
- loginUser: LoginUser,
1627
- dbTransaction: any,
1628
- GroupCode: string,
1629
- search?: {
1630
- PrivilegeCode?: string;
1631
- ObjectType?: string;
1632
- ObjectId?: string;
1633
- SystemCode?: string;
1634
- },
1635
- ): Promise<SystemPrivilege[]> {
1636
- try {
1637
- // Part 1: Privilege Checking
1638
- const systemCode =
1639
- ApplicationConfig.getComponentConfigValue('system-code');
1640
- const isPrivileged = await loginUser.checkPrivileges(
1641
- systemCode,
1642
- 'GROUP_PRIVILEGE_VIEW',
1643
- );
1644
-
1645
- if (!isPrivileged) {
1646
- throw new ClassError(
1647
- 'Group',
1648
- 'GroupErrMsg11',
1649
- 'You do not have the privilege to view group privileges',
1650
- );
1651
- }
1652
-
1653
- // Part 2: Validation
1654
- // Set group to instantiation of existing Group
1655
- await Group.init(dbTransaction, GroupCode);
1656
-
1657
- // Part 3: Retrieve Group Own Privilege
1658
- // Retrieve group object privileges by calling LoginUser._GroupObjectPrivilegeRepo.findAll
1659
- const where: any = {
1660
- GroupCode,
1661
- };
1662
-
1663
- const systemWhere: any = {};
1664
-
1665
- if (search) {
1666
- Object.entries(search).forEach(([key, value]) => {
1667
- if (key === 'SystemCode' || key === 'PrivilegeCode') {
1668
- systemWhere[key] = {
1669
- [Op.substring]: value,
1670
- };
1671
- } else {
1672
- where[key] = {
1673
- [Op.substring]: value,
1674
- };
1675
- }
1676
- });
1677
- }
1678
-
1679
- const groupObjectPrivileges =
1680
- await Group._GroupObjectPrivilegeRepo.findAll({
1681
- where,
1682
- include: [
1683
- {
1684
- model: SystemPrivilegeModel,
1685
- where: systemWhere,
1686
- },
1687
- ],
1688
- transaction: dbTransaction,
1689
- });
1690
- // Create variable called privileges and Map the SystemPrivilege data retrieved from 3.1 into SystemPrivilege object and push it to the privileges variables
1691
- const privileges: SystemPrivilege[] = [];
1692
- for (const groupObjectPrivilege of groupObjectPrivileges) {
1693
- const systemPrivilege = await SystemPrivilege.init(dbTransaction);
1694
- systemPrivilege.setAttributes(
1695
- groupObjectPrivilege.Privilege.get({ plain: true }),
1696
- );
1697
- privileges.push(systemPrivilege);
1698
- }
1699
-
1700
- //Remove duplicate
1701
- const uniquePrivileges = Array.from(
1702
- new Set(privileges.map((a) => a.SystemPrivilegeId)),
1703
- ).map((SystemPrivilegeId) => {
1704
- return privileges.find(
1705
- (a) => a.SystemPrivilegeId === SystemPrivilegeId,
1706
- );
1707
- });
1708
-
1709
- // Create the result based on the spec on return then returns it.
1710
- return uniquePrivileges;
1711
- } catch (error) {
1712
- throw error;
1713
- }
1714
- }
1715
-
1716
- public static async assignGroupPrivileges(
1717
- loginUser: LoginUser,
1718
- dbTransaction: any,
1719
- GroupCode: string,
1720
- SystemPrivilegeIds: string[],
1721
- ) {
1722
- try {
1723
- // Part 1: Privilege Checking
1724
- const systemCode =
1725
- ApplicationConfig.getComponentConfigValue('system-code');
1726
- const isPrivileged = await loginUser.checkPrivileges(
1727
- systemCode,
1728
- 'GROUP_PRIVILEGE_ASSIGN',
1729
- );
1730
-
1731
- if (!isPrivileged) {
1732
- throw new ClassError(
1733
- 'Group',
1734
- 'GroupErrMsg06',
1735
- 'You do not have the privilege to assign group privileges',
1736
- );
1737
- }
1738
-
1739
- // Part 2: Validation, Create and Record Activity
1740
- // Initialise group with group init
1741
-
1742
- const group = await Group.init(dbTransaction, GroupCode);
1743
-
1744
- // Retrieve all group system access by calling Group.getSystemAccess
1745
- const groupSystemAccesses = await Group.getSystemAccesses(
1746
- loginUser,
1747
- dbTransaction,
1748
- GroupCode,
1749
- 1,
1750
- Number.MAX_SAFE_INTEGER,
1751
- {},
1752
- );
1753
-
1754
- // If Group.InheritParentSystemAccess == "Y" and Group.ParentGroupCode exist
1755
- let parentGroupSystemAccesses: any = {
1756
- rows: [],
1757
- count: 0,
1758
- };
1759
- if (group.InheritParentSystemAccessYN === 'Y' && group.ParentGroupCode) {
1760
- // Retrieve all parent group system access by calling Group.getSystemAccess
1761
- parentGroupSystemAccesses = await Group.getSystemAccesses(
1762
- loginUser,
1763
- dbTransaction,
1764
- group.ParentGroupCode,
1765
- 1,
1766
- Number.MAX_SAFE_INTEGER,
1767
- {},
1768
- );
1769
- }
1770
-
1771
- // For each Params.PrivilegesCodes.
1772
- for (const SystemPrivilegeId of SystemPrivilegeIds) {
1773
- // Initialise existing System privilege by calling SystemPrivilege.init
1774
- const systemPrivilege = await SystemPrivilege.init(
1775
- dbTransaction,
1776
- SystemPrivilegeId,
1777
- );
1778
- //Check whether the system codes used by that privilege is exist inside the group system access retrieved from step 2.2 & 2.4. If system code does not exist in group system access, throw a new ClassError by passing:
1779
- // Classname: "Group"
1780
- // MessageCode: "GroupErrMsg0X"
1781
- // Message: "Failed to assign privilege <PrivilegeCode> due to non-existent system access."
1782
- const combinedSystemAccesses = [
1783
- ...groupSystemAccesses.rows,
1784
- ...parentGroupSystemAccesses.rows,
1785
- ];
1786
- const systemAccess = combinedSystemAccesses.find(
1787
- (systemAccess) =>
1788
- systemAccess.SystemCode === systemPrivilege.SystemCode,
1789
- );
1790
- if (!systemAccess) {
1791
- throw new ClassError(
1792
- 'Group',
1793
- 'GroupErrMsg13',
1794
- 'Failed to assign privilege ' +
1795
- SystemPrivilegeId +
1796
- ' due to non-existent system access.',
1797
- );
1798
- }
1799
-
1800
- //Check whether the group privilege exist by using Group._GroupPrivilegesRepo.findOne
1801
- const groupPrivilege = await Group._GroupPrivilegeRepo.findOne({
1802
- where: {
1803
- GroupCode,
1804
- SystemPrivilegeId,
1805
- },
1806
- include: [
1807
- {
1808
- model: SystemPrivilegeModel,
1809
- },
1810
- ],
1811
- transaction: dbTransaction,
1812
- });
1813
-
1814
- //If GroupPrivilege record exist and status is "Active". Skip this loop and proceed to the next privilege code
1815
- if (groupPrivilege && groupPrivilege.Status === 'Active') {
1816
- continue;
1817
- }
1818
-
1819
- let entityValueBefore = {};
1820
- let entityValueAfter = {};
1821
- let action = ActionEnum.CREATE;
1822
- let description = 'Create Group Privilege';
1823
- let entityId = null;
1824
- //If GroupPrivilege record exist and status is not "Active" do the following:
1825
- if (groupPrivilege && groupPrivilege.Status !== 'Active') {
1826
- //Set this GroupPrivilege entity as EntityValueBefore
1827
- entityValueBefore = {
1828
- GroupCode: groupPrivilege.GroupCode,
1829
- SystemPrivilegeId: groupPrivilege.SystemPrivilegeId,
1830
- Status: groupPrivilege.Status,
1831
- CreatedById: groupPrivilege.CreatedById,
1832
- CreatedAt: groupPrivilege.CreatedAt,
1833
- UpdatedById: groupPrivilege.UpdatedById,
1834
- UpdatedAt: groupPrivilege.UpdatedAt,
1835
- };
1836
-
1837
- //Update the status to active using Group._GroupPrivilegesRepo.Update.
1838
- const updatedPayload = {
1839
- Status: 'Active',
1840
- UpdatedById: loginUser.UserId,
1841
- UpdatedAt: new Date(),
1842
- };
1843
- const data = await Group._GroupPrivilegeRepo.findOne({
1844
- where: {
1845
- GroupCode,
1846
- SystemPrivilegeId,
1847
- },
1848
- include: [
1849
- {
1850
- model: SystemPrivilegeModel,
1851
- },
1852
- ],
1853
- transaction: dbTransaction,
1854
- });
1855
- data.Status = 'Active';
1856
- data.UpdatedById = updatedPayload.UpdatedById;
1857
- data.UpdatedAt = updatedPayload.UpdatedAt;
1858
- await data.save({ transaction: dbTransaction });
1859
-
1860
- //Set updated GroupPrivilege as EntityValueAfter
1861
- entityValueAfter = {
1862
- GroupCode: groupPrivilege.GroupCode,
1863
- SystemPrivilegeId: groupPrivilege.SystemPrivilegeId,
1864
- Status: updatedPayload.Status,
1865
- CreatedById: groupPrivilege.CreatedById,
1866
- CreatedAt: groupPrivilege.CreatedAt,
1867
- UpdatedById: updatedPayload.UpdatedById,
1868
- UpdatedAt: updatedPayload.UpdatedAt,
1869
- };
1870
-
1871
- //Instantiate new activity from Activity class
1872
- action = ActionEnum.UPDATE;
1873
- description = 'Update Group Privilege';
1874
- entityId = groupPrivilege.GroupPrivilegeId;
1875
- } else {
1876
- //If GroupPrivilege record does not exist, do the following:
1877
- //Initialise empty GroupPrivilege.
1878
- const newGroupPrivilege = await GroupPrivilege.init(dbTransaction);
1879
- //Set the attributes
1880
- newGroupPrivilege.setAttributes({
1881
- GroupCode,
1882
- SystemPrivilegeId,
1883
- Status: 'Active',
1884
- CreatedById: loginUser.UserId,
1885
- CreatedAt: new Date(),
1886
- UpdatedById: loginUser.UserId,
1887
- UpdatedAt: new Date(),
1888
- });
1889
-
1890
- // Set EntityValueAfter to above instance.
1891
- entityValueAfter = {
1892
- GroupCode: newGroupPrivilege.GroupCode,
1893
- SystemPrivilegeId: newGroupPrivilege.SystemPrivilegeId,
1894
- Status: newGroupPrivilege.Status,
1895
- CreatedById: newGroupPrivilege.CreatedById,
1896
- CreatedAt: newGroupPrivilege.CreatedAt,
1897
- UpdatedById: newGroupPrivilege.UpdatedById,
1898
- UpdatedAt: newGroupPrivilege.UpdatedAt,
1899
- };
1900
-
1901
- //Call Group._GroupPrivilegesRepo.create
1902
- const groupPrivilege = await Group._GroupPrivilegeRepo.create(
1903
- entityValueAfter,
1904
- {
1905
- transaction: dbTransaction,
1906
- },
1907
- );
1908
- action = ActionEnum.CREATE;
1909
- description = 'Create Group Privilege';
1910
- entityId = groupPrivilege.GroupPrivilegeId;
1911
- }
1912
-
1913
- //Instantiate new activity from Activity class, call createId() method, then set:
1914
- const activity = new Activity();
1915
- activity.ActivityId = activity.createId();
1916
- activity.Action = action;
1917
- activity.Description = description;
1918
- activity.EntityType = 'GroupPrivilege';
1919
- activity.EntityId = entityId;
1920
- activity.EntityValueBefore = JSON.stringify(entityValueBefore);
1921
- activity.EntityValueAfter = JSON.stringify(entityValueAfter);
1922
-
1923
- //Call new activity create method
1924
- await activity.create(loginUser.ObjectId, dbTransaction);
1925
- }
1926
-
1927
- return 'Successfully added.';
1928
- } catch (error) {
1929
- throw error;
1930
- }
1931
- }
1932
-
1933
- public static async deleteGroupPrivilege(
1934
- loginUser: LoginUser,
1935
- dbTransaction: any,
1936
- GroupCode: string,
1937
- SystemPrivilegeIds: string[],
1938
- ) {
1939
- try {
1940
- // Part 1: Privilege Checking
1941
- const systemCode =
1942
- ApplicationConfig.getComponentConfigValue('system-code');
1943
- const isPrivileged = await loginUser.checkPrivileges(
1944
- systemCode,
1945
- 'GROUP_PRIVILEGE_DELETE',
1946
- );
1947
-
1948
- if (!isPrivileged) {
1949
- throw new ClassError(
1950
- 'Group',
1951
- 'GroupErrMsg06',
1952
- 'You do not have the privilege to delete group privileges',
1953
- );
1954
- }
1955
-
1956
- // Part 2: Validation, Create and Record Activity
1957
- // For each Params.PrivilegesCodes.
1958
- for (const SystemPrivilegeId of SystemPrivilegeIds) {
1959
- //Check whether the record exist in database by calling Group._GroupPrivilegeRepo.findOne
1960
- const groupPrivilege = await Group._GroupPrivilegeRepo.findOne({
1961
- where: {
1962
- GroupCode,
1963
- SystemPrivilegeId,
1964
- },
1965
- include: [
1966
- {
1967
- model: SystemPrivilegeModel,
1968
- },
1969
- ],
1970
- transaction: dbTransaction,
1971
- });
1972
-
1973
- //If the record does not exist, throw a new ClassError
1974
- if (!groupPrivilege) {
1975
- throw new ClassError(
1976
- 'Group',
1977
- 'GroupErrMsg14',
1978
- 'GroupPrivilege not found.',
1979
- );
1980
- }
1981
-
1982
- //Set the EntityValueBefore to the GroupPrivilegesValue from step 1.c.
1983
- const entityValueBefore = {
1984
- GroupCode: groupPrivilege.GroupCode,
1985
- SystemPrivilegeId: groupPrivilege.SystemPrivilegeId,
1986
- Status: groupPrivilege.Status,
1987
- CreatedById: groupPrivilege.CreatedById,
1988
- CreatedAt: groupPrivilege.CreatedAt,
1989
- UpdatedById: groupPrivilege.UpdatedById,
1990
- UpdatedAt: groupPrivilege.UpdatedAt,
1991
- };
1992
-
1993
- //Call Group._GroupPrivilegeRepo.delete
1994
- await Group._GroupPrivilegeRepo.delete(
1995
- GroupCode,
1996
- SystemPrivilegeId,
1997
- dbTransaction,
1998
- );
1999
-
2000
- // Instantiate new activity from Activity class, call createId() method, then set:
2001
- const activity = new Activity();
2002
- activity.ActivityId = activity.createId();
2003
- activity.Action = ActionEnum.DELETE;
2004
- activity.Description = 'DELETE Group Privilege';
2005
- activity.EntityType = 'GroupPrivilege';
2006
- activity.EntityId = groupPrivilege.GroupPrivilegeId.toString();
2007
- activity.EntityValueBefore = JSON.stringify(entityValueBefore);
2008
- activity.EntityValueAfter = JSON.stringify({});
2009
- //Call new activity create method
2010
- await activity.create(loginUser.ObjectId, dbTransaction);
2011
- }
2012
- return 'Successfully deleted.';
2013
- } catch (error) {
2014
- throw error;
2015
- }
2016
- }
2017
-
2018
- public static async getHierarchy(
2019
- loginUser: LoginUser,
2020
- dbTransaction: any,
2021
- Status?: string,
2022
- Type?: GroupTypeEnum,
2023
- ParentGroupCode?: string,
2024
- ) {
2025
- // This method retrieves all group records from the sso_Group table in a hierarchical tree structure based on
2026
- // the ParentGroupCode. The hierarchy respects any filtering options passed, such as status or group type, and
2027
- // checks privileges of the requesting user.
2028
-
2029
- // Part 1: Privilege Checking
2030
- // Call loginUser.checkPrivileges() by passing:
2031
- // - SystemCode: Retrieved from the system configuration.
2032
- // - PrivilegeCode: 'GROUP_VIEW'.
2033
- // Ensure the user has privileges to view group information.
2034
- const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
2035
- const isPrivileged = await loginUser.checkPrivileges(
2036
- systemCode,
2037
- 'GROUP_VIEW',
2038
- );
2039
-
2040
- if (!isPrivileged) {
2041
- throw new ClassError(
2042
- 'Group',
2043
- 'GroupErrMsg04',
2044
- 'User is not privileged to view group',
2045
- );
2046
- }
2047
-
2048
- // Part 2: Prepare Filters
2049
- // Set the default filter for Status to "Active", unless specified.
2050
- // Use optional filters such as Type and ParentGroupCode if provided.
2051
- const queryObj: any = {};
2052
-
2053
- let options: any = {
2054
- transaction: dbTransaction,
2055
- };
2056
-
2057
- const search = {
2058
- Status: Status ? Status : 'Active',
2059
- Type: Type,
2060
- ParentGroupCode: ParentGroupCode,
2061
- };
2062
-
2063
- if (search) {
2064
- Object.entries(search).forEach(([key, value]) => {
2065
- if (value) {
2066
- queryObj[key] = {
2067
- [Op.substring]: value,
2068
- };
2069
- }
2070
- });
2071
-
2072
- options = {
2073
- ...options,
2074
- where: queryObj,
2075
- };
2076
-
2077
- // Part 3: Retrieve Group Data
2078
- // Use Sequelize's findAll() method with the provided filters (status, type, and parentGroupCode) to query the sso_Group table.
2079
- // Recursively build the group tree based on ParentGroupCode relationships.
2080
-
2081
- const result = await Group._Repo.findAll(options);
2082
- const data = result.map((group) => new Group(group.get({ plain: true })));
2083
-
2084
- const generateTree = async (groups) => {
2085
- // Create a map for easy lookup by code
2086
-
2087
- const data = groups.map((group) => {
2088
- return {
2089
- GroupCode: group.ObjectId,
2090
- Name: group.Name,
2091
- Description: group.Description,
2092
- Type: group.Type,
2093
- ParentGroupCode: group.ParentGroupCode,
2094
- InheritParentPrivilegeYN: group.InheritParentPrivilegeYN,
2095
- InheritParentSystemAccessYN: group.InheritParentSystemAccessYN,
2096
- Path: group._Path,
2097
- Status: group.Status,
2098
- CreatedById: group._CreatedById,
2099
- CreatedAt: group._CreatedAt,
2100
- UpdatedById: group._UpdatedById,
2101
- UpdatedAt: group._UpdatedAt,
2102
- childrens: [],
2103
- };
2104
- });
2105
- const groupMap = {};
2106
- data.forEach((group) => {
2107
- group.childrens = [];
2108
- groupMap[group.GroupCode] = group;
2109
- });
2110
-
2111
- // Initialize the root nodes (those without a parentCode)
2112
- const tree = [];
2113
-
2114
- data.forEach((group) => {
2115
- if (group.ParentGroupCode) {
2116
- // Add this group as a child of its parent
2117
- const parent = groupMap[group.ParentGroupCode];
2118
- if (parent) {
2119
- parent.childrens.push(group);
2120
- }
2121
- } else {
2122
- // If no parentCode, it's a root node
2123
- tree.push(group);
2124
- }
2125
- });
2126
-
2127
- return tree;
2128
- };
2129
-
2130
- const tree = await generateTree(data);
2131
-
2132
- // Part 4: Return Results
2133
- // Return the tree structure with child groups (under the childrens key) included only if a group has child groups.
2134
- return tree;
2135
- }
2136
- }
2137
-
2138
- public static async getGroupsWithReportingUser(
2139
- loginUser: User, //The user performing the action.
2140
- dbTransaction: Transaction, //Active database transaction.
2141
- whereOptions: any, //The filter criteria for selecting groups.
2142
- ) {
2143
- try {
2144
- // Part 1: Privilege Checking
2145
- // Call loginUser.checkPrivileges() by passing:
2146
- // SystemCode: Retrieve from app config.
2147
- // PrivilegeCode: 'GROUP_VIEW'.
2148
- const systemCode =
2149
- ApplicationConfig.getComponentConfigValue('system-code');
2150
- const isPrivileged = await loginUser.checkPrivileges(
2151
- systemCode,
2152
- 'GROUP_VIEW',
2153
- );
2154
- if (!isPrivileged) {
2155
- throw new ClassError(
2156
- 'Group',
2157
- 'GroupErrMsg04',
2158
- 'User is not privileged to view group',
2159
- );
2160
- }
2161
- // Part 2: Prepare Group Query
2162
- // Call Group._Repo.findAll() to fetch groups from the sso_Group table by passing:
2163
- // where: whereOptions
2164
- // include:
2165
- // Model: sso_GroupReportingUsers
2166
- // where: { Status: 'Active' }
2167
- // include:
2168
- // Model: sso_User
2169
- // attributes: ['UserId', 'FullName']
2170
- // attributes: ['GroupCode', 'UserId', 'Rank']
2171
- const options = {
2172
- where: whereOptions,
2173
- include: [
2174
- {
2175
- model: GroupReportingUserModel,
2176
- where: { Status: 'Active' },
2177
- include: [
2178
- {
2179
- model: UserModel,
2180
- as: 'User',
2181
- attributes: ['UserId', 'FullName'],
2182
- },
2183
- ],
2184
- },
2185
- ],
2186
- transaction: dbTransaction,
2187
- };
2188
- const groups = await Group._Repo.findAll(options);
2189
- // Part 3: Retrieve and Return
2190
- // Return the list of groups with nested reporting users.
2191
- return groups;
2192
- } catch (error) {
2193
- throw error;
2194
- }
2195
- }
2196
-
2197
- public async unassignUser(
2198
- UserId: number,
2199
- loginUser: LoginUser,
2200
- dbTransaction: Transaction,
2201
- ) {
2202
- try {
2203
- const systemCode =
2204
- ApplicationConfig.getComponentConfigValue('system-code');
2205
- const isPrivileged = await loginUser.checkPrivileges(
2206
- systemCode,
2207
- 'GROUP_UPDATE',
2208
- );
2209
-
2210
- if (!isPrivileged) {
2211
- throw new ClassError(
2212
- 'Group',
2213
- 'GroupErrMsg05',
2214
- 'You do not have the privilege to update group',
2215
- );
2216
- }
2217
-
2218
- const userGroup = await UserGroup.findOne(
2219
- dbTransaction,
2220
- loginUser,
2221
- this.GroupCode,
2222
- UserId,
2223
- );
2224
-
2225
- if (!userGroup) {
2226
- throw new ClassError(
2227
- 'Group',
2228
- 'GroupErrMsg07',
2229
- 'User is not assigned to this group',
2230
- );
2231
- }
2232
-
2233
- await userGroup.delete(loginUser, dbTransaction);
2234
- } catch (error) {
2235
- throw error;
2236
- }
2237
- }
2238
-
2239
- public static async getGroupTree(
2240
- groupCode: string | null,
2241
- loginUser: LoginUser,
2242
- dbTransaction: Transaction,
2243
- ): Promise<Group[]> {
2244
- try {
2245
- const systemCode =
2246
- ApplicationConfig.getComponentConfigValue('system-code');
2247
- const isPrivileged = await loginUser.checkPrivileges(
2248
- systemCode,
2249
- 'GROUP_VIEW',
2250
- );
2251
-
2252
- if (!isPrivileged) {
2253
- throw new ClassError(
2254
- 'Group',
2255
- 'GroupErrMsg04',
2256
- 'User is not privileged to view group',
2257
- );
2258
- }
2259
-
2260
- let groups: Group[] = [];
2261
- if (groupCode != null) {
2262
- let parentGroup = await Group.init(dbTransaction, groupCode);
2263
- await parentGroup.loadChildren(dbTransaction);
2264
- groups = parentGroup.children;
2265
- } else {
2266
- const data = await Group._Repo.findAll({
2267
- where: {
2268
- ParentGroupCode: null,
2269
- },
2270
- transaction: dbTransaction,
2271
- });
2272
-
2273
- for (const d of data) {
2274
- const group = new Group(d.get({ plain: true }));
2275
- groups.push(group);
2276
- }
2277
- }
2278
-
2279
- return groups;
2280
- } catch (error) {
2281
- throw error;
2282
- }
2283
- }
2284
- }
1
+ import { ClassError, ObjectBase, TreeNodeBase } from '@tomei/general';
2
+ import { GroupRepository } from './group.repository';
3
+ import { IGroupAttr } from '../../interfaces/group.interface';
4
+ import { GroupTypeEnum } from '../../enum';
5
+ import { LoginUser } from '../login-user/login-user';
6
+ import { IGroupSearchAttr } from '../../interfaces/group-search-attr.interface';
7
+ import { ApplicationConfig } from '@tomei/config';
8
+ import { Op, Transaction } from 'sequelize';
9
+ import { ActionEnum, Activity } from '@tomei/activity-history';
10
+ import { GroupSystemAccessRepository } from '../group-system-access/group-system-access.repository';
11
+ import SystemModel from '../../models/system.entity';
12
+ import { GroupSystemAccess } from '../group-system-access';
13
+ import { RedisService } from '../../redis-client/redis.service';
14
+ import SystemPrivilegeModel from '../../models/system-privilege.entity';
15
+ import { GroupPrivilegeRepository } from '../group-privilege/group-privilege.repository';
16
+ import { SystemPrivilege } from '../system-privilege/system-privilege';
17
+ import GroupPrivilegeModel from '../../models/group-privilege.entity';
18
+ import { GroupObjectPrivilegeRepository } from '../group-object-privilege/group-object-privilege.repository';
19
+ import { GroupObjectPrivilege } from '../group-object-privilege/group-object-privilege';
20
+ import { GroupPrivilege } from '../group-privilege/group-privilege';
21
+ import { User } from '../login-user/user';
22
+ import GroupReportingUserModel from '../../models/group-reporting-user.entity';
23
+ import GroupModel from '../../models/group.entity';
24
+ import UserModel from '../../models/user.entity';
25
+ import { UserGroup } from '../user-group/user-group';
26
+
27
+ export class Group extends TreeNodeBase<Group> {
28
+ ObjectId: string;
29
+ ObjectName: string;
30
+ TableName: 'sso_Group';
31
+ ObjectType = 'Group';
32
+
33
+ Name: string;
34
+ Description: string;
35
+ Type: GroupTypeEnum;
36
+ ParentGroupCode: string;
37
+ InheritParentPrivilegeYN: string;
38
+ InheritParentSystemAccessYN: string;
39
+ Status: string;
40
+ ParentGroup?: any;
41
+ _Path: string = '';
42
+ isChildrenLoaded = false;
43
+ isParentLoaded = false;
44
+
45
+ private _CreatedById: number;
46
+ private _CreatedAt: Date;
47
+ private _UpdatedById: number;
48
+ private _UpdatedAt: Date;
49
+ private static _Repo = new GroupRepository();
50
+ private static _GroupSystemAccessRepo = new GroupSystemAccessRepository();
51
+ private static _GroupPrivilegeRepo = new GroupPrivilegeRepository();
52
+ private static _GroupObjectPrivilegeRepo =
53
+ new GroupObjectPrivilegeRepository();
54
+ private static _RedisService: RedisService;
55
+
56
+ get GroupCode(): string {
57
+ return this.ObjectId;
58
+ }
59
+
60
+ set GroupCode(value: string) {
61
+ this.ObjectId = value;
62
+ }
63
+
64
+ get CreatedById(): number {
65
+ return this._CreatedById;
66
+ }
67
+
68
+ get CreatedAt(): Date {
69
+ return this._CreatedAt;
70
+ }
71
+
72
+ get UpdatedById(): number {
73
+ return this._UpdatedById;
74
+ }
75
+
76
+ get UpdatedAt(): Date {
77
+ return this._UpdatedAt;
78
+ }
79
+
80
+ get Path(): string {
81
+ return this._Path;
82
+ }
83
+
84
+ set Path(value: string) {
85
+ this._Path = value;
86
+ }
87
+
88
+ private constructor(groupAttr?: IGroupAttr) {
89
+ super();
90
+ if (groupAttr) {
91
+ this.GroupCode = groupAttr.GroupCode;
92
+ this.Name = groupAttr.Name;
93
+ this.Description = groupAttr?.Description;
94
+ this.Type = groupAttr?.Type;
95
+ this.ParentGroupCode = groupAttr?.ParentGroupCode;
96
+ this.InheritParentPrivilegeYN = groupAttr?.InheritParentPrivilegeYN;
97
+ this.InheritParentSystemAccessYN = groupAttr?.InheritParentSystemAccessYN;
98
+ this.Status = groupAttr?.Status;
99
+ this._Path = groupAttr?.Path;
100
+ this._CreatedById = groupAttr.CreatedById;
101
+ this._CreatedAt = groupAttr.CreatedAt;
102
+ this._UpdatedById = groupAttr.UpdatedById;
103
+ this._UpdatedAt = groupAttr.UpdatedAt;
104
+ }
105
+ }
106
+
107
+ public static async init(dbTransaction: any, GroupCode?: string) {
108
+ try {
109
+ Group._RedisService = await RedisService.init();
110
+ if (GroupCode) {
111
+ const group = await Group._Repo.findByPk(GroupCode, {
112
+ transaction: dbTransaction,
113
+ });
114
+ if (group) {
115
+ return new Group(group);
116
+ } else {
117
+ throw Error('Group not found');
118
+ }
119
+ }
120
+ return new Group();
121
+ } catch (error) {
122
+ throw new ClassError(
123
+ 'Group',
124
+ 'GroupErrMsg01',
125
+ 'Failed To Initialize Group',
126
+ );
127
+ }
128
+ }
129
+
130
+ async loadChildren(dbTransaction?: any) {
131
+ if (!this.GroupCode) {
132
+ throw Error('GroupCode is missing.');
133
+ }
134
+
135
+ const children = await Group._Repo.findAll({
136
+ where: { ParentGroupCode: this.GroupCode },
137
+ order: [['CreatedAt', 'ASC']],
138
+ transaction: dbTransaction,
139
+ });
140
+
141
+ this.children = children.map((child) => {
142
+ return new Group(child.get({ plain: true }));
143
+ });
144
+
145
+ this.isChildrenLoaded = true;
146
+ }
147
+
148
+ async loadParent(dbTransaction?: any) {
149
+ if (!this.GroupCode) {
150
+ throw Error('GroupCode are missing.');
151
+ }
152
+
153
+ if (this.ParentGroupCode) {
154
+ if (this.ParentGroupCode !== this.GroupCode) {
155
+ const parent = await Group._Repo.findByPk(this.ParentGroupCode, {
156
+ transaction: dbTransaction,
157
+ });
158
+ this.parent = new Group(parent.get({ plain: true }));
159
+ }
160
+ }
161
+
162
+ this.isParentLoaded = true;
163
+ }
164
+
165
+ async isLeaf(dbTransaction?: any): Promise<boolean> {
166
+ if (!this.isChildrenLoaded) {
167
+ await this.loadChildren(dbTransaction);
168
+ }
169
+
170
+ return this.children.length === 0;
171
+ }
172
+
173
+ async getPath(dbTransaction?: any): Promise<string> {
174
+ if (!this.isParentLoaded) {
175
+ await this.loadParent(dbTransaction);
176
+ }
177
+
178
+ if (this.parent) {
179
+ this._Path =
180
+ (await this.parent.getPath(dbTransaction)) + '/' + this.GroupCode;
181
+ return this._Path;
182
+ }
183
+ this._Path = this.GroupCode;
184
+ return this._Path;
185
+ }
186
+
187
+ protected async updatePath(dbTransaction?: any): Promise<void> {
188
+ const path = await this.getPath(dbTransaction);
189
+ this._Path = path;
190
+ }
191
+
192
+ async setParent(parent: Group, dbTransaction?: any): Promise<void> {
193
+ this.parent = parent;
194
+ await this.updatePath(dbTransaction);
195
+ }
196
+
197
+ async getPathDetail(dbTransaction?: any): Promise<Group[]> {
198
+ const path = this._Path.split('/');
199
+ const groups: Group[] = [];
200
+ for (let i = 0; i < path.length; i++) {
201
+ const group = await Group.init(dbTransaction, path[i]);
202
+ groups.push(group);
203
+ }
204
+ return groups;
205
+ }
206
+
207
+ public static async findAll(
208
+ page: number,
209
+ row: number,
210
+ dbTransaction: any,
211
+ loginUser: LoginUser,
212
+ search?: IGroupSearchAttr,
213
+ ) {
214
+ //This method will list all group based on the query params.
215
+ //Part 1: Privilege Checking
216
+ const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
217
+ const isPrivileged = await loginUser.checkPrivileges(
218
+ systemCode,
219
+ 'GROUP_LIST',
220
+ );
221
+
222
+ if (!isPrivileged) {
223
+ throw new ClassError(
224
+ 'Group',
225
+ 'GroupErrMsg04',
226
+ 'User is not privileged to list group',
227
+ );
228
+ }
229
+
230
+ //Part 2: Retrieve listing
231
+ const queryObj: any = {};
232
+
233
+ let options: any = {
234
+ transaction: dbTransaction,
235
+ };
236
+
237
+ if (page && row) {
238
+ options = {
239
+ ...options,
240
+ limit: row,
241
+ offset: row * (page - 1),
242
+ order: [['CreatedAt', 'DESC']],
243
+ distinct: true,
244
+ };
245
+ }
246
+
247
+ if (search) {
248
+ Object.entries(search).forEach(([key, value]) => {
249
+ queryObj[key] = {
250
+ [Op.substring]: value,
251
+ };
252
+ });
253
+
254
+ options = {
255
+ ...options,
256
+ where: queryObj,
257
+ };
258
+
259
+ const result = await Group._Repo.findAllWithPagination(options);
260
+
261
+ //Map the result to Group instance
262
+ return {
263
+ Count: result.count,
264
+ Groups: result.rows.map(
265
+ (group) => new Group(group.get({ plain: true })),
266
+ ),
267
+ };
268
+ }
269
+ }
270
+
271
+ public static async create(
272
+ loginUser: LoginUser,
273
+ dbTransaction: any,
274
+ group: Group,
275
+ ) {
276
+ try {
277
+ //Part 1: Privilege Checking
278
+ const systemCode =
279
+ ApplicationConfig.getComponentConfigValue('system-code');
280
+ const isPrivileged = await loginUser.checkPrivileges(
281
+ systemCode,
282
+ 'GROUP_CREATE',
283
+ );
284
+ if (!isPrivileged) {
285
+ throw new Error('You do not have permission to create group');
286
+ }
287
+
288
+ //Part 2: Validation
289
+ if (!group.GroupCode) {
290
+ throw new ClassError(
291
+ 'Group',
292
+ 'GroupErrMsg02',
293
+ 'Group Code is required',
294
+ );
295
+ }
296
+
297
+ if (!group.Name) {
298
+ throw new ClassError(
299
+ 'Group',
300
+ 'GroupErrMsg02',
301
+ 'Group Name is required',
302
+ );
303
+ }
304
+
305
+ if (!group.Type) {
306
+ throw new ClassError(
307
+ 'Group',
308
+ 'GroupErrMsg02',
309
+ 'Group Type is required',
310
+ );
311
+ }
312
+
313
+ //Check if group code is unique
314
+ const existingGroupCode = await Group._Repo.findByPk(group.GroupCode, {
315
+ transaction: dbTransaction,
316
+ });
317
+
318
+ if (existingGroupCode) {
319
+ throw new ClassError(
320
+ 'Group',
321
+ 'GroupErrMsg03',
322
+ 'Duplicate GroupCode found.',
323
+ );
324
+ }
325
+
326
+ //Validate parent group code if passed. Call Group._Repo.findByPk
327
+ if (group.ParentGroupCode) {
328
+ const parentGroup = await Group._Repo.findByPk(group.ParentGroupCode, {
329
+ transaction: dbTransaction,
330
+ });
331
+
332
+ if (!parentGroup) {
333
+ throw new ClassError(
334
+ 'Group',
335
+ 'GroupErrMsg04',
336
+ 'ParentGroupCode is not found.',
337
+ );
338
+ }
339
+
340
+ //If Params.group.GroupCode = Params.group?.ParentGroupCode, throw new ClassError
341
+ if (group.GroupCode === group.ParentGroupCode) {
342
+ throw new ClassError(
343
+ 'Group',
344
+ 'GroupErrMsg05',
345
+ 'GroupCode and ParentGroupCode cannot be the same.',
346
+ );
347
+ }
348
+ }
349
+
350
+ //Part 3: Create Group
351
+ //Initialise new Group instance and populate
352
+ const newGroup = new Group(group);
353
+ newGroup.ObjectId = group.GroupCode;
354
+ newGroup.Name = group.Name;
355
+ newGroup.Type = group.Type;
356
+ newGroup.Description = group.Description;
357
+ newGroup.ParentGroupCode = group.ParentGroupCode;
358
+ newGroup.InheritParentPrivilegeYN = group.InheritParentPrivilegeYN;
359
+ newGroup.InheritParentSystemAccessYN = group.InheritParentSystemAccessYN;
360
+ newGroup.Status = 'Active';
361
+ newGroup._CreatedById = loginUser.UserId;
362
+ newGroup._UpdatedById = loginUser.UserId;
363
+ newGroup._Path = await newGroup.getPath(dbTransaction);
364
+
365
+ //Call Group._Repo create method
366
+ const entityGroupAfter = {
367
+ GroupCode: newGroup.ObjectId,
368
+ Name: newGroup.Name,
369
+ Type: newGroup.Type,
370
+ Description: newGroup.Description,
371
+ ParentGroupCode: newGroup.ParentGroupCode,
372
+ InheritParentPrivilegeYN: newGroup.InheritParentPrivilegeYN,
373
+ InheritParentSystemAccessYN: newGroup.InheritParentSystemAccessYN,
374
+ Path: newGroup._Path,
375
+ Status: newGroup.Status,
376
+ CreatedById: newGroup._CreatedById,
377
+ UpdatedById: newGroup._UpdatedById,
378
+ CreatedAt: newGroup._CreatedAt,
379
+ UpdatedAt: newGroup._UpdatedAt,
380
+ };
381
+
382
+ await Group._Repo.create(entityGroupAfter, {
383
+ transaction: dbTransaction,
384
+ });
385
+
386
+ //Part 4: Record Create Group Activity and return newGroup
387
+
388
+ const entityValueBefore = {};
389
+
390
+ //Instantiate new activity
391
+ const activity = new Activity();
392
+ activity.ActivityId = activity.createId();
393
+ activity.Action = ActionEnum.CREATE;
394
+ activity.Description = 'Create Group';
395
+ activity.EntityType = 'Group';
396
+ activity.EntityId = newGroup.ObjectId;
397
+ activity.EntityValueBefore = JSON.stringify(entityValueBefore);
398
+ activity.EntityValueAfter = JSON.stringify(entityGroupAfter);
399
+
400
+ //Call Activity.create method
401
+ await activity.create(loginUser.ObjectId, dbTransaction);
402
+
403
+ return newGroup;
404
+ } catch (error) {
405
+ throw error;
406
+ }
407
+ }
408
+
409
+ protected static async checkDuplicateGroupCode(
410
+ dbTransaction: any,
411
+ GroupCode,
412
+ ) {
413
+ const isGroupCodeExist = await Group._Repo.findOne({
414
+ where: { GroupCode },
415
+ transaction: dbTransaction,
416
+ });
417
+
418
+ if (isGroupCodeExist) {
419
+ throw new ClassError(
420
+ 'Group',
421
+ 'GroupErrMsg07',
422
+ 'GroupCode already exists.',
423
+ );
424
+ }
425
+ }
426
+
427
+ private async updateChildrenPath(oldGroupCode: string, dbTransaction: any) {
428
+ try {
429
+ const isLeaf = await this.isLeaf(dbTransaction);
430
+ if (isLeaf) {
431
+ return;
432
+ }
433
+
434
+ const childrens = await Group._Repo.findAll({
435
+ where: {
436
+ Path: {
437
+ [Op.like]: `${oldGroupCode}/%`,
438
+ },
439
+ },
440
+ transaction: dbTransaction,
441
+ });
442
+
443
+ childrens.forEach(async (children) => {
444
+ //Break the path into array with oldGroupCode/ as separator;
445
+ const path = children.Path.split(`${oldGroupCode}/`);
446
+ //Retrive the last element of the array
447
+ const childPath = path[1];
448
+ //Combine the childPath with this.Path then save it to the children.Path
449
+ await children.update(
450
+ { Path: `${this._Path}/${childPath}` },
451
+ { transaction: dbTransaction },
452
+ );
453
+ });
454
+ } catch (error) {
455
+ throw error;
456
+ }
457
+ }
458
+
459
+ public async update(
460
+ loginUser: LoginUser,
461
+ dbTransaction: any,
462
+ group: {
463
+ GroupCode: string;
464
+ NewGroupCode?: string;
465
+ Name: string;
466
+ Description: string;
467
+ Type: GroupTypeEnum;
468
+ ParentGroupCode: string;
469
+ InheritParentPrivilegeYN: string;
470
+ InheritParentSystemAccessYN: string;
471
+ Status: string;
472
+ },
473
+ ) {
474
+ //Part 1: Privilege Checking
475
+ const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
476
+ const isPrivileged = await loginUser.checkPrivileges(
477
+ systemCode,
478
+ 'GROUP_UPDATE',
479
+ );
480
+
481
+ if (!isPrivileged) {
482
+ throw new ClassError(
483
+ 'Group',
484
+ 'GroupErrMsg06',
485
+ 'You do not have the privilege to update Group',
486
+ );
487
+ }
488
+ try {
489
+ if (group.NewGroupCode) {
490
+ await Group.checkDuplicateGroupCode(dbTransaction, group.NewGroupCode);
491
+ }
492
+
493
+ const entityValueBefore = {
494
+ GroupCode: this.GroupCode,
495
+ Name: this.Name,
496
+ Type: this.Type,
497
+ Description: this.Description,
498
+ ParentGroupCode: this.ParentGroupCode,
499
+ InheritParentPrivilegeYN: this.InheritParentPrivilegeYN,
500
+ InheritParentSystemAccessYN: this.InheritParentSystemAccessYN,
501
+ Path: this.Path,
502
+ Status: this.Status,
503
+ CreatedById: this._CreatedById,
504
+ UpdatedById: this._UpdatedById,
505
+ CreatedAt: this._CreatedAt,
506
+ UpdatedAt: this._UpdatedAt,
507
+ };
508
+
509
+ let isPathChanged = false;
510
+ const oldGroupCode = this.GroupCode;
511
+ if (group.NewGroupCode) {
512
+ this.GroupCode = group.NewGroupCode;
513
+ isPathChanged = true;
514
+ }
515
+
516
+ //Check if ParentGroupCode is changed or added
517
+ if (
518
+ (group.ParentGroupCode &&
519
+ this.ParentGroupCode !== group.ParentGroupCode) ||
520
+ (group.ParentGroupCode && !this.ParentGroupCode)
521
+ ) {
522
+ const parentGroup = await Group.init(
523
+ dbTransaction,
524
+ group.ParentGroupCode,
525
+ );
526
+ if (!parentGroup) {
527
+ throw new ClassError(
528
+ 'Group',
529
+ 'GroupErrMsg08',
530
+ 'Parent Group Code not found',
531
+ );
532
+ }
533
+ await this.setParent(parentGroup);
534
+ //Check if ParentGroupCode is removed
535
+ isPathChanged = true;
536
+ } else if (!group.ParentGroupCode && this.ParentGroupCode) {
537
+ await this.setParent(null);
538
+ isPathChanged = true;
539
+ }
540
+
541
+ if (isPathChanged) {
542
+ await this.updateChildrenPath(oldGroupCode, dbTransaction);
543
+ }
544
+
545
+ this.Name = group?.Name || this.Name;
546
+ this.Type = group?.Type || this.Type;
547
+ this.Description = group?.Description || this.Description;
548
+ this.ParentGroupCode = group?.ParentGroupCode || this.ParentGroupCode;
549
+ this.InheritParentPrivilegeYN =
550
+ group?.InheritParentPrivilegeYN || this.InheritParentPrivilegeYN;
551
+ this.InheritParentSystemAccessYN =
552
+ group?.InheritParentSystemAccessYN || this.InheritParentSystemAccessYN;
553
+ this.Status = group?.Status || this.Status;
554
+ this._UpdatedById = loginUser.UserId;
555
+ this._UpdatedAt = new Date();
556
+
557
+ await Group._Repo.update(
558
+ {
559
+ GroupCode: this.GroupCode,
560
+ Name: this.Name,
561
+ Type: this.Type,
562
+ Description: this.Description,
563
+ ParentGroupCode: this.ParentGroupCode,
564
+ InheritParentPrivilegeYN: this.InheritParentPrivilegeYN,
565
+ InheritParentSystemAccessYN: this.InheritParentSystemAccessYN,
566
+ Status: this.Status,
567
+ Path: this._Path,
568
+ UpdatedById: this._UpdatedById,
569
+ UpdatedAt: this._UpdatedAt,
570
+ },
571
+ {
572
+ where: {
573
+ GroupCode: group.GroupCode,
574
+ },
575
+ transaction: dbTransaction,
576
+ },
577
+ );
578
+
579
+ const entityValueAfter = {
580
+ GroupCode: this.GroupCode,
581
+ Name: this.Name,
582
+ Type: this.Type,
583
+ Description: this.Description,
584
+ ParentGroupCode: this.ParentGroupCode,
585
+ InheritParentPrivilegeYN: this.InheritParentPrivilegeYN,
586
+ InheritParentSystemAccessYN: this.InheritParentSystemAccessYN,
587
+ Status: this.Status,
588
+ Path: this._Path,
589
+ CreatedById: this._CreatedById,
590
+ UpdatedById: this._UpdatedById,
591
+ CreatedAt: this._CreatedAt,
592
+ UpdatedAt: this._UpdatedAt,
593
+ };
594
+
595
+ const activity = new Activity();
596
+ activity.ActivityId = activity.createId();
597
+ activity.Action = ActionEnum.UPDATE;
598
+ activity.Description = `Update Group ${group.Type}`;
599
+ activity.EntityType = 'Group';
600
+ activity.EntityId = group.GroupCode;
601
+ activity.EntityValueBefore = JSON.stringify(entityValueBefore);
602
+ activity.EntityValueAfter = JSON.stringify(entityValueAfter);
603
+ await activity.create(loginUser.ObjectId, dbTransaction);
604
+
605
+ return this;
606
+ } catch (error) {
607
+ throw error;
608
+ }
609
+ }
610
+
611
+ public static async delete(
612
+ loginUser: LoginUser,
613
+ dbTransaction: any,
614
+ GroupCode: string,
615
+ ) {
616
+ // Part 1: Privilege Checking
617
+ const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
618
+
619
+ const isPrivileged = await loginUser.checkPrivileges(
620
+ systemCode,
621
+ 'GROUP_DELETE',
622
+ );
623
+
624
+ if (!isPrivileged) {
625
+ throw new ClassError(
626
+ 'Group',
627
+ 'GroupErrMsg03',
628
+ 'You do not have the privilege to delete groups records.',
629
+ );
630
+ }
631
+ try {
632
+ const group = await Group.init(dbTransaction, GroupCode);
633
+
634
+ if (group.Status === 'Active') {
635
+ throw new ClassError(
636
+ 'Group',
637
+ 'GroupErrMsg03',
638
+ 'Active Group cant be deleted',
639
+ );
640
+ }
641
+
642
+ const relatedGroup = await Group.findAll(
643
+ 1,
644
+ Number.MAX_SAFE_INTEGER,
645
+ dbTransaction,
646
+ loginUser,
647
+ {
648
+ ParentGroupCode: GroupCode,
649
+ },
650
+ );
651
+
652
+ if (relatedGroup.Count > 0) {
653
+ const listOfRelatedGroup = relatedGroup.Groups.map((group) => {
654
+ return group.GroupCode;
655
+ });
656
+ throw new ClassError(
657
+ 'Group',
658
+ 'GroupErrMsg03',
659
+ `Group still has associated user group ${listOfRelatedGroup}`,
660
+ );
661
+ }
662
+
663
+ await Group._Repo.delete(GroupCode, dbTransaction);
664
+
665
+ const EntityValueBefore = {
666
+ GroupCode: group.GroupCode,
667
+ Name: group.Name,
668
+ Type: group.Type,
669
+ Description: group.Description,
670
+ ParentGroupCode: group.ParentGroupCode,
671
+ InheritParentPrivilegeYN: group.InheritParentPrivilegeYN,
672
+ InheritParentSystemAccessYN: group.InheritParentSystemAccessYN,
673
+ Status: group.Status,
674
+ CreatedById: group._CreatedById,
675
+ UpdatedById: group._UpdatedById,
676
+ CreatedAt: group._CreatedAt,
677
+ UpdatedAt: group._UpdatedAt,
678
+ };
679
+
680
+ const activity = new Activity();
681
+ activity.ActivityId = activity.createId();
682
+ activity.Action = ActionEnum.DELETE;
683
+ activity.Description = 'Delete Group';
684
+ activity.EntityType = 'Group';
685
+ activity.EntityId = group.ObjectId;
686
+ activity.EntityValueBefore = JSON.stringify(EntityValueBefore);
687
+ activity.EntityValueAfter = JSON.stringify({});
688
+
689
+ await activity.create(loginUser.ObjectId, dbTransaction);
690
+
691
+ return { Message: 'Group removed.' };
692
+ } catch (error) {
693
+ throw error;
694
+ }
695
+ }
696
+
697
+ public static async getSystemAccesses(
698
+ loginUser: LoginUser,
699
+ dbTransaction: any,
700
+ GroupCode: string,
701
+ Page: number,
702
+ Rows: number,
703
+ Search: {
704
+ SystemCode?: string;
705
+ Status?: string;
706
+ },
707
+ ) {
708
+ // Part 1: Privilege Checking
709
+ const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
710
+ const isPrivileged = await loginUser.checkPrivileges(
711
+ systemCode,
712
+ 'SYSTEM_ACCESS_VIEW',
713
+ );
714
+
715
+ if (!isPrivileged) {
716
+ throw new ClassError(
717
+ 'Group',
718
+ 'GroupErrMsg06',
719
+ 'You do not have the privilege to view system access',
720
+ );
721
+ }
722
+
723
+ try {
724
+ // Part 2: Validation
725
+ await Group.init(dbTransaction, GroupCode);
726
+
727
+ // Part 3: Retrieve System Access and returns
728
+ const queryObj: any = { GroupCode: GroupCode };
729
+
730
+ if (Search) {
731
+ Object.entries(Search).forEach(([key, value]) => {
732
+ queryObj[key] = value;
733
+ });
734
+ }
735
+
736
+ let options: any = {
737
+ where: queryObj,
738
+ distinct: true,
739
+ transaction: dbTransaction,
740
+ };
741
+
742
+ if (Page && Rows) {
743
+ options = {
744
+ ...options,
745
+ limit: Rows,
746
+ offset: Rows * (Page - 1),
747
+ order: [['CreatedAt', 'DESC']],
748
+ };
749
+ }
750
+
751
+ const systemAccess =
752
+ await Group._GroupSystemAccessRepo.findAndCountAll(options);
753
+ return systemAccess;
754
+ } catch (error) {
755
+ return error;
756
+ }
757
+ }
758
+
759
+ public static async getSystemAccessRoles(
760
+ loginUser: LoginUser,
761
+ dbTransaction: any,
762
+ SystemCode: string,
763
+ Page: number,
764
+ Rows: number,
765
+ Search: {
766
+ GroupCode?: string;
767
+ Status?: string;
768
+ },
769
+ ) {
770
+ // Part 1: Privilege Checking
771
+ const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
772
+ const isPrivileged = await loginUser.checkPrivileges(
773
+ systemCode,
774
+ 'SYSTEM_ACCESS_VIEW',
775
+ );
776
+
777
+ if (!isPrivileged) {
778
+ throw new ClassError(
779
+ 'Group',
780
+ 'GroupErrMsg06',
781
+ 'You do not have the privilege to view system access',
782
+ );
783
+ }
784
+
785
+ try {
786
+ // Part 2: Retrieve System Access and returns
787
+ const queryObj: any = { SystemCode: SystemCode };
788
+
789
+ if (Search) {
790
+ Object.entries(Search).forEach(([key, value]) => {
791
+ queryObj[key] = value;
792
+ });
793
+ }
794
+
795
+ let options: any = {
796
+ where: queryObj,
797
+ distinct: true,
798
+ transaction: dbTransaction,
799
+ };
800
+
801
+ if (Page && Rows) {
802
+ options = {
803
+ ...options,
804
+ limit: Rows,
805
+ offset: Rows * (Page - 1),
806
+ order: [['CreatedAt', 'DESC']],
807
+ include: {
808
+ model: GroupModel,
809
+ where: {
810
+ Type: 'Role',
811
+ },
812
+ },
813
+ };
814
+ }
815
+
816
+ const systemAccess =
817
+ await Group._GroupSystemAccessRepo.findAndCountAll(options);
818
+ return systemAccess;
819
+ } catch (error) {
820
+ return error;
821
+ }
822
+ }
823
+
824
+ private static async getInheritedSystemAccess(
825
+ dbTransaction: any,
826
+ group: Group,
827
+ ): Promise<any[]> {
828
+ const options: any = {
829
+ where: {
830
+ GroupCode: group.GroupCode,
831
+ Status: 'Active',
832
+ },
833
+ include: [
834
+ {
835
+ model: SystemModel,
836
+ },
837
+ ],
838
+ transaction: dbTransaction,
839
+ };
840
+ let systemAccess = await Group._GroupSystemAccessRepo.findAll(options);
841
+
842
+ if (group.InheritParentSystemAccessYN === 'Y' && group.ParentGroupCode) {
843
+ const parentGroup = await Group.init(
844
+ dbTransaction,
845
+ group.ParentGroupCode,
846
+ );
847
+ const parentSystemAccesses = await this.getInheritedSystemAccess(
848
+ dbTransaction,
849
+ parentGroup,
850
+ );
851
+ systemAccess = systemAccess.concat(parentSystemAccesses);
852
+ }
853
+ return systemAccess;
854
+ }
855
+
856
+ public static async isGroupCodeInHierarchy(
857
+ dbTransaction: any,
858
+ GroupCode: string,
859
+ ListGroupCode: string[] = [],
860
+ ): Promise<boolean> {
861
+ ListGroupCode.push(GroupCode);
862
+
863
+ const group = await Group._Repo.findOne({
864
+ where: { GroupCode },
865
+ transaction: dbTransaction,
866
+ });
867
+
868
+ if (group?.ParentGroupCode) {
869
+ const isGroupCodeExist = ListGroupCode.includes(group.ParentGroupCode);
870
+ if (!isGroupCodeExist) {
871
+ await this.isGroupCodeInHierarchy(
872
+ dbTransaction,
873
+ group.ParentGroupCode,
874
+ ListGroupCode,
875
+ );
876
+ }
877
+ {
878
+ return false;
879
+ }
880
+ } else {
881
+ return true;
882
+ }
883
+ }
884
+
885
+ public static async getParentSystemAccesses(
886
+ loginUser: LoginUser,
887
+ dbTransaction: any,
888
+ GroupCode: string,
889
+ ) {
890
+ // Part 1: Privilege Checking
891
+ const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
892
+ const isPrivileged = await loginUser.checkPrivileges(
893
+ systemCode,
894
+ 'SYSTEM_ACCESS_VIEW',
895
+ );
896
+
897
+ if (!isPrivileged) {
898
+ throw new ClassError(
899
+ 'Group',
900
+ 'GroupErrMsg06',
901
+ 'You do not have the privilege to view system access',
902
+ );
903
+ }
904
+
905
+ try {
906
+ const group = await Group.init(dbTransaction, GroupCode);
907
+ if (group.InheritParentSystemAccessYN !== 'Y' || !group.ParentGroupCode) {
908
+ return [];
909
+ } else {
910
+ const parentGroup = await Group.init(
911
+ dbTransaction,
912
+ group.ParentGroupCode,
913
+ );
914
+ const inheritSystemAccess = await Group.getInheritedSystemAccess(
915
+ dbTransaction,
916
+ parentGroup,
917
+ );
918
+ return inheritSystemAccess;
919
+ }
920
+ } catch (error) {
921
+ throw error;
922
+ }
923
+ }
924
+
925
+ public static async addSystemAccesses(
926
+ loginUser: LoginUser,
927
+ dbTransaction: any,
928
+ GroupCode: string,
929
+ SystemCodes: string[],
930
+ ) {
931
+ // Part 1: Privilege Checking
932
+ const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
933
+ const isPrivileged = await loginUser.checkPrivileges(
934
+ systemCode,
935
+ 'SYSTEM_ACCESS_CREATE',
936
+ );
937
+
938
+ if (!isPrivileged) {
939
+ throw new ClassError(
940
+ 'Group',
941
+ 'GroupErrMsg07',
942
+ 'You do not have the privilege to create system access',
943
+ );
944
+ }
945
+
946
+ try {
947
+ if (SystemCodes.length > 0) {
948
+ for (const element of SystemCodes) {
949
+ const CurrentGroupSystemAccess = await Group.getSystemAccesses(
950
+ loginUser,
951
+ dbTransaction,
952
+ GroupCode,
953
+ 1,
954
+ Number.MAX_SAFE_INTEGER,
955
+ { SystemCode: element },
956
+ );
957
+
958
+ if (CurrentGroupSystemAccess?.count > 0) {
959
+ throw new ClassError(
960
+ 'Group',
961
+ 'GroupErrMsg08',
962
+ 'System access already exists',
963
+ );
964
+ }
965
+
966
+ const groupSystemAccess = await GroupSystemAccess.init(dbTransaction);
967
+ groupSystemAccess.createId();
968
+ groupSystemAccess.GroupCode = GroupCode;
969
+ groupSystemAccess.SystemCode = element;
970
+ groupSystemAccess.Status = 'Active';
971
+ groupSystemAccess.CreatedById = +loginUser.ObjectId;
972
+ groupSystemAccess.CreatedAt = new Date();
973
+ groupSystemAccess.UpdatedById = +loginUser.ObjectId;
974
+ groupSystemAccess.UpdatedAt = new Date();
975
+
976
+ const EntityValueAfter = {
977
+ GroupCode: groupSystemAccess.GroupCode,
978
+ SystemCode: groupSystemAccess.SystemCode,
979
+ Status: groupSystemAccess.Status,
980
+ CreatedById: groupSystemAccess.CreatedById,
981
+ CreatedAt: groupSystemAccess.CreatedAt,
982
+ UpdatedById: groupSystemAccess.UpdatedById,
983
+ UpdatedAt: groupSystemAccess.UpdatedAt,
984
+ };
985
+
986
+ const systemAccess = await Group._GroupSystemAccessRepo.create(
987
+ EntityValueAfter,
988
+ {
989
+ transaction: dbTransaction,
990
+ },
991
+ );
992
+
993
+ const activity = new Activity();
994
+ activity.ActivityId = activity.createId();
995
+ activity.Action = ActionEnum.CREATE;
996
+ activity.Description = 'Create Group System Access';
997
+ activity.EntityType = 'GroupSystemAccess';
998
+ activity.EntityId = systemAccess.GroupSystemAccessId?.toString();
999
+ activity.EntityValueBefore = JSON.stringify({});
1000
+ activity.EntityValueAfter = JSON.stringify(EntityValueAfter);
1001
+
1002
+ await activity.create(loginUser.ObjectId, dbTransaction);
1003
+ }
1004
+
1005
+ return { Message: 'Successfully added.' };
1006
+ }
1007
+ } catch (error) {
1008
+ throw error;
1009
+ }
1010
+ }
1011
+
1012
+ public static async deleteSystemAccess(
1013
+ loginUser: LoginUser,
1014
+ dbTransaction: any,
1015
+ GroupCode: string,
1016
+ SystemCode: string,
1017
+ ) {
1018
+ // Part 1: Privilege Checking
1019
+ const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
1020
+ const isPrivileged = await loginUser.checkPrivileges(
1021
+ systemCode,
1022
+ 'SYSTEM_ACCESS_DELETE',
1023
+ );
1024
+
1025
+ if (!isPrivileged) {
1026
+ throw new ClassError(
1027
+ 'Group',
1028
+ 'GroupErrMsg08',
1029
+ 'You do not have the privilege to delete system access',
1030
+ );
1031
+ }
1032
+
1033
+ try {
1034
+ const currentGroupSystemAccess = await Group.getSystemAccesses(
1035
+ loginUser,
1036
+ dbTransaction,
1037
+ GroupCode,
1038
+ 1,
1039
+ Number.MAX_SAFE_INTEGER,
1040
+ { SystemCode: SystemCode },
1041
+ );
1042
+
1043
+ if (currentGroupSystemAccess.count < 1) {
1044
+ throw new ClassError(
1045
+ 'Group',
1046
+ 'GroupErrMsg10',
1047
+ 'No associated system access found.',
1048
+ );
1049
+ }
1050
+
1051
+ await Group._GroupSystemAccessRepo.delete(
1052
+ GroupCode,
1053
+ SystemCode,
1054
+ dbTransaction,
1055
+ );
1056
+
1057
+ const EntityValueBefore = {
1058
+ GroupCode: currentGroupSystemAccess?.rows[0]?.GroupCode,
1059
+ SystemCode: currentGroupSystemAccess?.rows[0]?.SystemCode,
1060
+ Status: currentGroupSystemAccess?.rows[0]?.Status,
1061
+ CreatedById: currentGroupSystemAccess?.rows[0]?.CreatedById,
1062
+ CreatedAt: currentGroupSystemAccess?.rows[0]?.CreatedAt,
1063
+ UpdatedById: currentGroupSystemAccess?.rows[0]?.UpdatedById,
1064
+ UpdatedAt: currentGroupSystemAccess?.rows[0]?.UpdatedAt,
1065
+ };
1066
+
1067
+ const activity = new Activity();
1068
+ activity.ActivityId = activity.createId();
1069
+ activity.Action = ActionEnum.DELETE;
1070
+ activity.Description = 'Delete Group System Access';
1071
+ activity.EntityType = 'GroupSystemAccess';
1072
+ activity.EntityId =
1073
+ currentGroupSystemAccess?.rows[0]?.GroupSystemAccessId?.toString();
1074
+ activity.EntityValueBefore = JSON.stringify(EntityValueBefore);
1075
+ activity.EntityValueAfter = JSON.stringify({});
1076
+
1077
+ await activity.create(loginUser.ObjectId, dbTransaction);
1078
+
1079
+ return { Message: 'System access removed.', SystemCode: SystemCode };
1080
+ } catch (error) {
1081
+ throw error;
1082
+ }
1083
+ }
1084
+
1085
+ public static async getSystemPrivileges(
1086
+ loginUser: LoginUser,
1087
+ dbTransaction: any,
1088
+ GroupCode: string,
1089
+ search?: {
1090
+ SystemCode?: string;
1091
+ Status?: string;
1092
+ },
1093
+ ) {
1094
+ try {
1095
+ //Part 1: Privilege Checking
1096
+ const systemCode =
1097
+ ApplicationConfig.getComponentConfigValue('system-code');
1098
+ const isPrivileged = await loginUser.checkPrivileges(
1099
+ systemCode,
1100
+ 'GROUP_PRIVILEGE_VIEW',
1101
+ );
1102
+
1103
+ if (!isPrivileged) {
1104
+ throw new ClassError(
1105
+ 'Group',
1106
+ 'GroupErrMsg11',
1107
+ 'You do not have the privilege to view group privileges',
1108
+ );
1109
+ }
1110
+
1111
+ //Set group to instantiation of existing Group
1112
+ await Group.init(dbTransaction, GroupCode);
1113
+
1114
+ //Part 3: Retrieve Group Own Privilege
1115
+ //Retrieve group data and it's privileged by calling Group._Repo.findAll
1116
+ let where: any = {
1117
+ GroupCode,
1118
+ };
1119
+
1120
+ let systemWhere: any = {};
1121
+
1122
+ if (search) {
1123
+ if (search.Status) {
1124
+ where = {
1125
+ ...where,
1126
+ Status: search.Status,
1127
+ };
1128
+ }
1129
+
1130
+ if (search.SystemCode) {
1131
+ systemWhere = {
1132
+ SystemCode: {
1133
+ [Op.substring]: search.SystemCode,
1134
+ },
1135
+ };
1136
+ }
1137
+ }
1138
+
1139
+ const groupOwnPrivileges = await Group._GroupPrivilegeRepo.findAll({
1140
+ where,
1141
+ include: [
1142
+ {
1143
+ model: SystemPrivilegeModel,
1144
+ where: systemWhere,
1145
+ },
1146
+ ],
1147
+ transaction: dbTransaction,
1148
+ });
1149
+
1150
+ //Create variable called privileges and Map the SystemPrivilege data retrieved from 3.1 into SystemPrivilege object and push it to the privileges variables
1151
+ const privileges: SystemPrivilege[] = [];
1152
+
1153
+ for (const groupPrivilege of groupOwnPrivileges) {
1154
+ const systemPrivilege = await SystemPrivilege.init(dbTransaction);
1155
+ systemPrivilege.setAttributes(
1156
+ groupPrivilege.Privilege.get({ plain: true }),
1157
+ );
1158
+ privileges.push(systemPrivilege);
1159
+ }
1160
+
1161
+ return privileges;
1162
+ } catch (error) {
1163
+ throw error;
1164
+ }
1165
+ }
1166
+
1167
+ public static async getSystemPrivilegeRoles(
1168
+ loginUser: LoginUser,
1169
+ dbTransaction: any,
1170
+ SystemCode: string,
1171
+ search?: {
1172
+ GroupCode?: string[];
1173
+ Status?: string;
1174
+ },
1175
+ ) {
1176
+ try {
1177
+ //Part 1: Privilege Checking
1178
+ const systemCode =
1179
+ ApplicationConfig.getComponentConfigValue('system-code');
1180
+ const isPrivileged = await loginUser.checkPrivileges(
1181
+ systemCode,
1182
+ 'GROUP_PRIVILEGE_VIEW',
1183
+ );
1184
+
1185
+ if (!isPrivileged) {
1186
+ throw new ClassError(
1187
+ 'Group',
1188
+ 'GroupErrMsg11',
1189
+ 'You do not have the privilege to view group privileges',
1190
+ );
1191
+ }
1192
+
1193
+ //Part 2: Retrieve Roles Based on Privilege
1194
+ //Retrieve Roles based on privilege on a system
1195
+ let systemWhere: any = {};
1196
+
1197
+ if (SystemCode) {
1198
+ systemWhere = {
1199
+ SystemCode: {
1200
+ [Op.substring]: SystemCode,
1201
+ },
1202
+ };
1203
+ }
1204
+
1205
+ const groupCodesPrivileges: {
1206
+ SystemPrivilegeId: string;
1207
+ GroupCodes: { Code: string; Name: string }[];
1208
+ }[] = [];
1209
+
1210
+ const allGroupCodePrivileges = await Group._GroupPrivilegeRepo.findAll({
1211
+ include: [
1212
+ {
1213
+ model: SystemPrivilegeModel,
1214
+ where: systemWhere,
1215
+ },
1216
+ {
1217
+ model: GroupModel,
1218
+ where: {
1219
+ Type: 'Role',
1220
+ },
1221
+ },
1222
+ ],
1223
+ transaction: dbTransaction,
1224
+ });
1225
+
1226
+ // Use a Map to group by SystemPrivilegeId
1227
+ const privilegesMap = new Map<string, { Code: string; Name: string }[]>();
1228
+
1229
+ for (const groupCodePrivilege of allGroupCodePrivileges) {
1230
+ const { SystemPrivilegeId, GroupCode, Group } = groupCodePrivilege; // `Group` contains Name from GroupModel
1231
+
1232
+ if (!privilegesMap.has(SystemPrivilegeId)) {
1233
+ // Initialize with an empty array if not already present
1234
+ privilegesMap.set(SystemPrivilegeId, []);
1235
+ }
1236
+
1237
+ // Add the GroupCode and Name to the array if it exists and is not already present
1238
+ if (GroupCode && Group?.Name) {
1239
+ const groupCodes = privilegesMap.get(SystemPrivilegeId);
1240
+ const newGroupEntry = { Code: GroupCode, Name: Group.Name };
1241
+
1242
+ // Ensure no duplicates
1243
+ if (
1244
+ groupCodes &&
1245
+ !groupCodes.some(
1246
+ (g) => g.Code === GroupCode && g.Name === Group.Name,
1247
+ )
1248
+ ) {
1249
+ groupCodes.push(newGroupEntry);
1250
+ }
1251
+ }
1252
+ }
1253
+
1254
+ // Convert the Map to the desired array format
1255
+ privilegesMap.forEach((groupCodes, SystemPrivilegeId) => {
1256
+ groupCodesPrivileges.push({
1257
+ SystemPrivilegeId,
1258
+ GroupCodes: groupCodes,
1259
+ });
1260
+ });
1261
+
1262
+ const allPrivileges = await SystemPrivilegeModel.findAll({
1263
+ where: systemWhere,
1264
+ transaction: dbTransaction,
1265
+ });
1266
+
1267
+ const groupPrivilegeRoles: {
1268
+ SystemPrivilegeId: string;
1269
+ PrivilegeCode: string;
1270
+ Description: string;
1271
+ GroupCodes: { Code: string; Name: string }[];
1272
+ }[] = [];
1273
+
1274
+ // Iterate through allPrivileges to check for matches in groupCodesPrivileges
1275
+ for (const privilege of allPrivileges) {
1276
+ const matchingGroupPrivilege = groupCodesPrivileges.find(
1277
+ (groupPrivilege) =>
1278
+ groupPrivilege.SystemPrivilegeId === privilege.SystemPrivilegeId,
1279
+ );
1280
+
1281
+ if (matchingGroupPrivilege) {
1282
+ // If match is found, push to groupPrivilegeRoles with GroupCodes
1283
+ groupPrivilegeRoles.push({
1284
+ SystemPrivilegeId: privilege.SystemPrivilegeId,
1285
+ PrivilegeCode: privilege.PrivilegeCode,
1286
+ Description: privilege.Description,
1287
+ GroupCodes: matchingGroupPrivilege.GroupCodes,
1288
+ });
1289
+ } else {
1290
+ // If no match is found, push with an empty array of GroupCodes
1291
+ groupPrivilegeRoles.push({
1292
+ SystemPrivilegeId: privilege.SystemPrivilegeId,
1293
+ PrivilegeCode: privilege.PrivilegeCode,
1294
+ Description: privilege.Description,
1295
+ GroupCodes: [],
1296
+ });
1297
+ }
1298
+ }
1299
+
1300
+ const filteredGroupPrivilegeRoles = groupPrivilegeRoles
1301
+ .map((role) => {
1302
+ if (search.GroupCode?.length) {
1303
+ // Filter GroupCodes to only include matching Codes
1304
+ const matchingGroupCodes = role.GroupCodes.filter((groupCode) =>
1305
+ search.GroupCode.includes(groupCode.Code),
1306
+ );
1307
+
1308
+ // If there are no matching GroupCodes, exclude this role
1309
+ if (matchingGroupCodes.length === 0) {
1310
+ return null;
1311
+ }
1312
+
1313
+ // Return the role with filtered GroupCodes
1314
+ return {
1315
+ ...role,
1316
+ GroupCodes: matchingGroupCodes,
1317
+ };
1318
+ }
1319
+
1320
+ // If search.GroupCode is not provided, include all data
1321
+ return role;
1322
+ })
1323
+ .filter(Boolean); // Remove any null values
1324
+
1325
+ return filteredGroupPrivilegeRoles;
1326
+ } catch (error) {
1327
+ throw error;
1328
+ }
1329
+ }
1330
+
1331
+ public static async getInheritedSystemPrivileges(
1332
+ dbTransaction: any,
1333
+ GroupCode: string,
1334
+ search?: {
1335
+ SystemCode?: string;
1336
+ Status?: string;
1337
+ PrivilegeCode?: string;
1338
+ },
1339
+ ): Promise<SystemPrivilege[]> {
1340
+ try {
1341
+ //Retrieve group data and it's privileges by calling Group._Repo.findAll
1342
+ const where: any = {
1343
+ GroupCode,
1344
+ };
1345
+
1346
+ let groupPrivilegeWhere: any = {};
1347
+ let systemPrivilegeWhere: any = {};
1348
+
1349
+ if (search) {
1350
+ if (search.Status) {
1351
+ groupPrivilegeWhere = {
1352
+ Status: search.Status,
1353
+ };
1354
+ }
1355
+
1356
+ if (search.SystemCode) {
1357
+ systemPrivilegeWhere = {
1358
+ SystemCode: {
1359
+ [Op.substring]: search.SystemCode,
1360
+ },
1361
+ };
1362
+ }
1363
+
1364
+ if (search.PrivilegeCode) {
1365
+ systemPrivilegeWhere = {
1366
+ ...systemPrivilegeWhere,
1367
+ PrivilegeCode: {
1368
+ [Op.substring]: search.PrivilegeCode,
1369
+ },
1370
+ };
1371
+ }
1372
+ }
1373
+ const group = await Group._Repo.findOne({
1374
+ where: where,
1375
+ include: [
1376
+ {
1377
+ model: GroupPrivilegeModel,
1378
+ where: groupPrivilegeWhere,
1379
+ separate: true,
1380
+ include: [
1381
+ {
1382
+ model: SystemPrivilegeModel,
1383
+ where: systemPrivilegeWhere,
1384
+ },
1385
+ ],
1386
+ },
1387
+ ],
1388
+ transaction: dbTransaction,
1389
+ });
1390
+
1391
+ //Retrieve group object privileges by calling LoginUser._GroupObjectPrivilegeRepo.findAll
1392
+ const objectWhere: any = {
1393
+ GroupCode,
1394
+ };
1395
+ const systemWhere: any = {};
1396
+ if (search) {
1397
+ Object.entries(search).forEach(([key, value]) => {
1398
+ if (key === 'Status') {
1399
+ objectWhere[key] = {
1400
+ [Op.substring]: value,
1401
+ };
1402
+ } else {
1403
+ systemWhere[key] = {
1404
+ [Op.substring]: value,
1405
+ };
1406
+ }
1407
+ });
1408
+ }
1409
+ const groupObjectPrivileges =
1410
+ await Group._GroupObjectPrivilegeRepo.findAll({
1411
+ where: objectWhere,
1412
+ include: [
1413
+ {
1414
+ model: SystemPrivilegeModel,
1415
+ where: systemWhere,
1416
+ },
1417
+ ],
1418
+ transaction: dbTransaction,
1419
+ });
1420
+
1421
+ //Map to SystemPrivilege object
1422
+ let privileges: SystemPrivilege[] = [];
1423
+ for (const groupPrivilege of group.GroupPrivileges) {
1424
+ const systemPrivilege = await SystemPrivilege.init(dbTransaction);
1425
+ systemPrivilege.setAttributes(
1426
+ groupPrivilege.Privilege.get({ plain: true }),
1427
+ );
1428
+ privileges.push(systemPrivilege);
1429
+ }
1430
+
1431
+ for (const groupObjectPrivilege of groupObjectPrivileges) {
1432
+ const systemPrivilege = await SystemPrivilege.init(dbTransaction);
1433
+ systemPrivilege.setAttributes(
1434
+ groupObjectPrivilege.Privilege.get({ plain: true }),
1435
+ );
1436
+ privileges.push(systemPrivilege);
1437
+ }
1438
+
1439
+ //Part 2: Retrieve Privileges Inherited from Parent Group
1440
+ //if group data retrieved from 1.1 have InheritParentPrivilegeYN == "Y" and ParentGroupCode value is not empty. Call this method again
1441
+ if (group.InheritParentPrivilegeYN === 'Y' && group.ParentGroupCode) {
1442
+ const inheritedPrivileges = await Group.getInheritedSystemPrivileges(
1443
+ dbTransaction,
1444
+ group.ParentGroupCode,
1445
+ search,
1446
+ );
1447
+ privileges = privileges.concat(inheritedPrivileges);
1448
+ }
1449
+
1450
+ //format to make sure no duplicate
1451
+ const uniquePrivileges = Array.from(
1452
+ new Set(privileges.map((a) => a.SystemPrivilegeId)),
1453
+ ).map((SystemPrivilegeId) => {
1454
+ return privileges.find(
1455
+ (a) => a.SystemPrivilegeId === SystemPrivilegeId,
1456
+ );
1457
+ });
1458
+
1459
+ return uniquePrivileges;
1460
+ } catch (error) {
1461
+ throw error;
1462
+ }
1463
+ }
1464
+
1465
+ public static async getParentSystemPrivileges(
1466
+ loginUser: LoginUser,
1467
+ dbTransaction: any,
1468
+ GroupCode: string,
1469
+ search?: {
1470
+ SystemCode?: string;
1471
+ Status?: string;
1472
+ PrivilegeCode?: string;
1473
+ },
1474
+ ): Promise<SystemPrivilege[]> {
1475
+ try {
1476
+ //Part 1: Privilege Checking
1477
+ const systemCode =
1478
+ ApplicationConfig.getComponentConfigValue('system-code');
1479
+ const isPrivileged = await loginUser.checkPrivileges(
1480
+ systemCode,
1481
+ 'GROUP_PRIVILEGE_VIEW',
1482
+ );
1483
+
1484
+ if (!isPrivileged) {
1485
+ throw new ClassError(
1486
+ 'Group',
1487
+ 'GroupErrMsg11',
1488
+ 'You do not have the privilege to view group privileges',
1489
+ );
1490
+ }
1491
+
1492
+ //Part 2: Validation
1493
+ //Set group to instantiation of existing Group
1494
+ const group = await Group.init(dbTransaction, GroupCode);
1495
+ //Check if group.InheritParentPrivilegeYN == "Y" and ParentGroupCode value is not empty. if no then return an empty array
1496
+ if (group.InheritParentPrivilegeYN !== 'Y' || !group.ParentGroupCode) {
1497
+ return [];
1498
+ }
1499
+
1500
+ //Part 3: Retrieve Group Own Privilege
1501
+ //Retrieve group data and it's privileged by calling Group.getIheritedSystemPrivileges
1502
+ const privileges = await Group.getInheritedSystemPrivileges(
1503
+ dbTransaction,
1504
+ group.ParentGroupCode,
1505
+ search,
1506
+ );
1507
+
1508
+ return privileges;
1509
+ } catch (error) {
1510
+ throw error;
1511
+ }
1512
+ }
1513
+
1514
+ public static async assignGroupObjectPrivilege(
1515
+ loginUser: LoginUser,
1516
+ dbTransaction: any,
1517
+ GroupCode: string,
1518
+ GroupObjectPrivileges: GroupObjectPrivilege[],
1519
+ SystemCode: string,
1520
+ ): Promise<string> {
1521
+ try {
1522
+ //Part 1: Privilege Checking
1523
+ const systemCode =
1524
+ ApplicationConfig.getComponentConfigValue('system-code');
1525
+ const isPrivileged = await loginUser.checkPrivileges(
1526
+ systemCode,
1527
+ 'GROUP_OBJECT_PRIVILEGE_ASSIGN',
1528
+ );
1529
+
1530
+ if (!isPrivileged) {
1531
+ throw new ClassError(
1532
+ 'Group',
1533
+ 'GroupErrMsg12',
1534
+ 'You do not have the privilege to assign group object privilege',
1535
+ );
1536
+ }
1537
+
1538
+ //Part 2: Validation
1539
+ //Initialise group with group init
1540
+ const group = await Group.init(dbTransaction, GroupCode);
1541
+ //Retrieve all group system access by calling Group.getSystemAccesses
1542
+ const groupSystemAccesses = await Group.getSystemAccesses(
1543
+ loginUser,
1544
+ dbTransaction,
1545
+ GroupCode,
1546
+ 1,
1547
+ Number.MAX_SAFE_INTEGER,
1548
+ {},
1549
+ );
1550
+
1551
+ //If Group.InheritParentSystemAccess == "Y" and Group.ParentGroupCode exist, initialise parent group
1552
+ let parentGroupSystemAccesses: any = {
1553
+ rows: [],
1554
+ count: 0,
1555
+ };
1556
+
1557
+ if (group.InheritParentSystemAccessYN === 'Y' && group.ParentGroupCode) {
1558
+ //Retrieve all parent group system access by calling Group.getSystemAccesses
1559
+ parentGroupSystemAccesses = await Group.getSystemAccesses(
1560
+ loginUser,
1561
+ dbTransaction,
1562
+ group.ParentGroupCode,
1563
+ 1,
1564
+ Number.MAX_SAFE_INTEGER,
1565
+ undefined,
1566
+ );
1567
+ }
1568
+
1569
+ // For each Params.GroupObjectPrivileges.
1570
+ for (const groupObjectPrivilege of GroupObjectPrivileges) {
1571
+ //Initialise existing System privilege
1572
+ const systemPrivilege = await SystemPrivilege.init(
1573
+ dbTransaction,
1574
+ groupObjectPrivilege.SystemPrivilegeId,
1575
+ );
1576
+ //Check whether the system codes used by that privilege is exist inside the group system access
1577
+ const combinedSystemAccesses = {
1578
+ ...groupSystemAccesses.rows,
1579
+ ...parentGroupSystemAccesses.rows,
1580
+ };
1581
+ const systemAccess = combinedSystemAccesses.find(
1582
+ (systemAccess) =>
1583
+ systemAccess.SystemCode === systemPrivilege.SystemCode,
1584
+ );
1585
+ if (!systemAccess) {
1586
+ throw new ClassError(
1587
+ 'Group',
1588
+ 'GroupErrMsg13',
1589
+ 'Failed to assign privilege ' +
1590
+ groupObjectPrivilege.SystemPrivilegeId +
1591
+ ' due to non-existent system access.',
1592
+ );
1593
+ }
1594
+
1595
+ //Check whether the group object privilege already exist by using Group._GroupObjectPrivilegesRepo.findOne
1596
+ const groupObjectPrivilegeData =
1597
+ await Group._GroupObjectPrivilegeRepo.findOne({
1598
+ where: {
1599
+ GroupCode,
1600
+ SystemPrivilegeId: groupObjectPrivilege.SystemPrivilegeId,
1601
+ ObjectId: groupObjectPrivilege.ObjectId,
1602
+ ObjectType: groupObjectPrivilege.ObjectType,
1603
+ },
1604
+ transaction: dbTransaction,
1605
+ });
1606
+ //If GroupObjectPrivilege record exist. Skip this loop and proceed to the next privilege code
1607
+ if (groupObjectPrivilegeData) {
1608
+ continue;
1609
+ } else {
1610
+ //Call GroupObjectPrivilege.create
1611
+ await GroupObjectPrivilege.create(
1612
+ loginUser,
1613
+ dbTransaction,
1614
+ groupObjectPrivilege,
1615
+ );
1616
+ }
1617
+ }
1618
+
1619
+ return 'Successfully added.';
1620
+ } catch (error) {
1621
+ throw error;
1622
+ }
1623
+ }
1624
+
1625
+ public static async getGroubObjectPrivileges(
1626
+ loginUser: LoginUser,
1627
+ dbTransaction: any,
1628
+ GroupCode: string,
1629
+ search?: {
1630
+ PrivilegeCode?: string;
1631
+ ObjectType?: string;
1632
+ ObjectId?: string;
1633
+ SystemCode?: string;
1634
+ },
1635
+ ): Promise<SystemPrivilege[]> {
1636
+ try {
1637
+ // Part 1: Privilege Checking
1638
+ const systemCode =
1639
+ ApplicationConfig.getComponentConfigValue('system-code');
1640
+ const isPrivileged = await loginUser.checkPrivileges(
1641
+ systemCode,
1642
+ 'GROUP_PRIVILEGE_VIEW',
1643
+ );
1644
+
1645
+ if (!isPrivileged) {
1646
+ throw new ClassError(
1647
+ 'Group',
1648
+ 'GroupErrMsg11',
1649
+ 'You do not have the privilege to view group privileges',
1650
+ );
1651
+ }
1652
+
1653
+ // Part 2: Validation
1654
+ // Set group to instantiation of existing Group
1655
+ await Group.init(dbTransaction, GroupCode);
1656
+
1657
+ // Part 3: Retrieve Group Own Privilege
1658
+ // Retrieve group object privileges by calling LoginUser._GroupObjectPrivilegeRepo.findAll
1659
+ const where: any = {
1660
+ GroupCode,
1661
+ };
1662
+
1663
+ const systemWhere: any = {};
1664
+
1665
+ if (search) {
1666
+ Object.entries(search).forEach(([key, value]) => {
1667
+ if (key === 'SystemCode' || key === 'PrivilegeCode') {
1668
+ systemWhere[key] = {
1669
+ [Op.substring]: value,
1670
+ };
1671
+ } else {
1672
+ where[key] = {
1673
+ [Op.substring]: value,
1674
+ };
1675
+ }
1676
+ });
1677
+ }
1678
+
1679
+ const groupObjectPrivileges =
1680
+ await Group._GroupObjectPrivilegeRepo.findAll({
1681
+ where,
1682
+ include: [
1683
+ {
1684
+ model: SystemPrivilegeModel,
1685
+ where: systemWhere,
1686
+ },
1687
+ ],
1688
+ transaction: dbTransaction,
1689
+ });
1690
+ // Create variable called privileges and Map the SystemPrivilege data retrieved from 3.1 into SystemPrivilege object and push it to the privileges variables
1691
+ const privileges: SystemPrivilege[] = [];
1692
+ for (const groupObjectPrivilege of groupObjectPrivileges) {
1693
+ const systemPrivilege = await SystemPrivilege.init(dbTransaction);
1694
+ systemPrivilege.setAttributes(
1695
+ groupObjectPrivilege.Privilege.get({ plain: true }),
1696
+ );
1697
+ privileges.push(systemPrivilege);
1698
+ }
1699
+
1700
+ //Remove duplicate
1701
+ const uniquePrivileges = Array.from(
1702
+ new Set(privileges.map((a) => a.SystemPrivilegeId)),
1703
+ ).map((SystemPrivilegeId) => {
1704
+ return privileges.find(
1705
+ (a) => a.SystemPrivilegeId === SystemPrivilegeId,
1706
+ );
1707
+ });
1708
+
1709
+ // Create the result based on the spec on return then returns it.
1710
+ return uniquePrivileges;
1711
+ } catch (error) {
1712
+ throw error;
1713
+ }
1714
+ }
1715
+
1716
+ public static async assignGroupPrivileges(
1717
+ loginUser: LoginUser,
1718
+ dbTransaction: any,
1719
+ GroupCode: string,
1720
+ SystemPrivilegeIds: string[],
1721
+ ) {
1722
+ try {
1723
+ // Part 1: Privilege Checking
1724
+ const systemCode =
1725
+ ApplicationConfig.getComponentConfigValue('system-code');
1726
+ const isPrivileged = await loginUser.checkPrivileges(
1727
+ systemCode,
1728
+ 'GROUP_PRIVILEGE_ASSIGN',
1729
+ );
1730
+
1731
+ if (!isPrivileged) {
1732
+ throw new ClassError(
1733
+ 'Group',
1734
+ 'GroupErrMsg06',
1735
+ 'You do not have the privilege to assign group privileges',
1736
+ );
1737
+ }
1738
+
1739
+ // Part 2: Validation, Create and Record Activity
1740
+ // Initialise group with group init
1741
+
1742
+ const group = await Group.init(dbTransaction, GroupCode);
1743
+
1744
+ // Retrieve all group system access by calling Group.getSystemAccess
1745
+ const groupSystemAccesses = await Group.getSystemAccesses(
1746
+ loginUser,
1747
+ dbTransaction,
1748
+ GroupCode,
1749
+ 1,
1750
+ Number.MAX_SAFE_INTEGER,
1751
+ {},
1752
+ );
1753
+
1754
+ // If Group.InheritParentSystemAccess == "Y" and Group.ParentGroupCode exist
1755
+ let parentGroupSystemAccesses: any = {
1756
+ rows: [],
1757
+ count: 0,
1758
+ };
1759
+ if (group.InheritParentSystemAccessYN === 'Y' && group.ParentGroupCode) {
1760
+ // Retrieve all parent group system access by calling Group.getSystemAccess
1761
+ parentGroupSystemAccesses = await Group.getSystemAccesses(
1762
+ loginUser,
1763
+ dbTransaction,
1764
+ group.ParentGroupCode,
1765
+ 1,
1766
+ Number.MAX_SAFE_INTEGER,
1767
+ {},
1768
+ );
1769
+ }
1770
+
1771
+ // For each Params.PrivilegesCodes.
1772
+ for (const SystemPrivilegeId of SystemPrivilegeIds) {
1773
+ // Initialise existing System privilege by calling SystemPrivilege.init
1774
+ const systemPrivilege = await SystemPrivilege.init(
1775
+ dbTransaction,
1776
+ SystemPrivilegeId,
1777
+ );
1778
+ //Check whether the system codes used by that privilege is exist inside the group system access retrieved from step 2.2 & 2.4. If system code does not exist in group system access, throw a new ClassError by passing:
1779
+ // Classname: "Group"
1780
+ // MessageCode: "GroupErrMsg0X"
1781
+ // Message: "Failed to assign privilege <PrivilegeCode> due to non-existent system access."
1782
+ const combinedSystemAccesses = [
1783
+ ...groupSystemAccesses.rows,
1784
+ ...parentGroupSystemAccesses.rows,
1785
+ ];
1786
+ const systemAccess = combinedSystemAccesses.find(
1787
+ (systemAccess) =>
1788
+ systemAccess.SystemCode === systemPrivilege.SystemCode,
1789
+ );
1790
+ if (!systemAccess) {
1791
+ throw new ClassError(
1792
+ 'Group',
1793
+ 'GroupErrMsg13',
1794
+ 'Failed to assign privilege ' +
1795
+ SystemPrivilegeId +
1796
+ ' due to non-existent system access.',
1797
+ );
1798
+ }
1799
+
1800
+ //Check whether the group privilege exist by using Group._GroupPrivilegesRepo.findOne
1801
+ const groupPrivilege = await Group._GroupPrivilegeRepo.findOne({
1802
+ where: {
1803
+ GroupCode,
1804
+ SystemPrivilegeId,
1805
+ },
1806
+ include: [
1807
+ {
1808
+ model: SystemPrivilegeModel,
1809
+ },
1810
+ ],
1811
+ transaction: dbTransaction,
1812
+ });
1813
+
1814
+ //If GroupPrivilege record exist and status is "Active". Skip this loop and proceed to the next privilege code
1815
+ if (groupPrivilege && groupPrivilege.Status === 'Active') {
1816
+ continue;
1817
+ }
1818
+
1819
+ let entityValueBefore = {};
1820
+ let entityValueAfter = {};
1821
+ let action = ActionEnum.CREATE;
1822
+ let description = 'Create Group Privilege';
1823
+ let entityId = null;
1824
+ //If GroupPrivilege record exist and status is not "Active" do the following:
1825
+ if (groupPrivilege && groupPrivilege.Status !== 'Active') {
1826
+ //Set this GroupPrivilege entity as EntityValueBefore
1827
+ entityValueBefore = {
1828
+ GroupCode: groupPrivilege.GroupCode,
1829
+ SystemPrivilegeId: groupPrivilege.SystemPrivilegeId,
1830
+ Status: groupPrivilege.Status,
1831
+ CreatedById: groupPrivilege.CreatedById,
1832
+ CreatedAt: groupPrivilege.CreatedAt,
1833
+ UpdatedById: groupPrivilege.UpdatedById,
1834
+ UpdatedAt: groupPrivilege.UpdatedAt,
1835
+ };
1836
+
1837
+ //Update the status to active using Group._GroupPrivilegesRepo.Update.
1838
+ const updatedPayload = {
1839
+ Status: 'Active',
1840
+ UpdatedById: loginUser.UserId,
1841
+ UpdatedAt: new Date(),
1842
+ };
1843
+ const data = await Group._GroupPrivilegeRepo.findOne({
1844
+ where: {
1845
+ GroupCode,
1846
+ SystemPrivilegeId,
1847
+ },
1848
+ include: [
1849
+ {
1850
+ model: SystemPrivilegeModel,
1851
+ },
1852
+ ],
1853
+ transaction: dbTransaction,
1854
+ });
1855
+ data.Status = 'Active';
1856
+ data.UpdatedById = updatedPayload.UpdatedById;
1857
+ data.UpdatedAt = updatedPayload.UpdatedAt;
1858
+ await data.save({ transaction: dbTransaction });
1859
+
1860
+ //Set updated GroupPrivilege as EntityValueAfter
1861
+ entityValueAfter = {
1862
+ GroupCode: groupPrivilege.GroupCode,
1863
+ SystemPrivilegeId: groupPrivilege.SystemPrivilegeId,
1864
+ Status: updatedPayload.Status,
1865
+ CreatedById: groupPrivilege.CreatedById,
1866
+ CreatedAt: groupPrivilege.CreatedAt,
1867
+ UpdatedById: updatedPayload.UpdatedById,
1868
+ UpdatedAt: updatedPayload.UpdatedAt,
1869
+ };
1870
+
1871
+ //Instantiate new activity from Activity class
1872
+ action = ActionEnum.UPDATE;
1873
+ description = 'Update Group Privilege';
1874
+ entityId = groupPrivilege.GroupPrivilegeId;
1875
+ } else {
1876
+ //If GroupPrivilege record does not exist, do the following:
1877
+ //Initialise empty GroupPrivilege.
1878
+ const newGroupPrivilege = await GroupPrivilege.init(dbTransaction);
1879
+ //Set the attributes
1880
+ newGroupPrivilege.setAttributes({
1881
+ GroupCode,
1882
+ SystemPrivilegeId,
1883
+ Status: 'Active',
1884
+ CreatedById: loginUser.UserId,
1885
+ CreatedAt: new Date(),
1886
+ UpdatedById: loginUser.UserId,
1887
+ UpdatedAt: new Date(),
1888
+ });
1889
+
1890
+ // Set EntityValueAfter to above instance.
1891
+ entityValueAfter = {
1892
+ GroupCode: newGroupPrivilege.GroupCode,
1893
+ SystemPrivilegeId: newGroupPrivilege.SystemPrivilegeId,
1894
+ Status: newGroupPrivilege.Status,
1895
+ CreatedById: newGroupPrivilege.CreatedById,
1896
+ CreatedAt: newGroupPrivilege.CreatedAt,
1897
+ UpdatedById: newGroupPrivilege.UpdatedById,
1898
+ UpdatedAt: newGroupPrivilege.UpdatedAt,
1899
+ };
1900
+
1901
+ //Call Group._GroupPrivilegesRepo.create
1902
+ const groupPrivilege = await Group._GroupPrivilegeRepo.create(
1903
+ entityValueAfter,
1904
+ {
1905
+ transaction: dbTransaction,
1906
+ },
1907
+ );
1908
+ action = ActionEnum.CREATE;
1909
+ description = 'Create Group Privilege';
1910
+ entityId = groupPrivilege.GroupPrivilegeId;
1911
+ }
1912
+
1913
+ //Instantiate new activity from Activity class, call createId() method, then set:
1914
+ const activity = new Activity();
1915
+ activity.ActivityId = activity.createId();
1916
+ activity.Action = action;
1917
+ activity.Description = description;
1918
+ activity.EntityType = 'GroupPrivilege';
1919
+ activity.EntityId = entityId;
1920
+ activity.EntityValueBefore = JSON.stringify(entityValueBefore);
1921
+ activity.EntityValueAfter = JSON.stringify(entityValueAfter);
1922
+
1923
+ //Call new activity create method
1924
+ await activity.create(loginUser.ObjectId, dbTransaction);
1925
+ }
1926
+
1927
+ return 'Successfully added.';
1928
+ } catch (error) {
1929
+ throw error;
1930
+ }
1931
+ }
1932
+
1933
+ public static async deleteGroupPrivilege(
1934
+ loginUser: LoginUser,
1935
+ dbTransaction: any,
1936
+ GroupCode: string,
1937
+ SystemPrivilegeIds: string[],
1938
+ ) {
1939
+ try {
1940
+ // Part 1: Privilege Checking
1941
+ const systemCode =
1942
+ ApplicationConfig.getComponentConfigValue('system-code');
1943
+ const isPrivileged = await loginUser.checkPrivileges(
1944
+ systemCode,
1945
+ 'GROUP_PRIVILEGE_DELETE',
1946
+ );
1947
+
1948
+ if (!isPrivileged) {
1949
+ throw new ClassError(
1950
+ 'Group',
1951
+ 'GroupErrMsg06',
1952
+ 'You do not have the privilege to delete group privileges',
1953
+ );
1954
+ }
1955
+
1956
+ // Part 2: Validation, Create and Record Activity
1957
+ // For each Params.PrivilegesCodes.
1958
+ for (const SystemPrivilegeId of SystemPrivilegeIds) {
1959
+ //Check whether the record exist in database by calling Group._GroupPrivilegeRepo.findOne
1960
+ const groupPrivilege = await Group._GroupPrivilegeRepo.findOne({
1961
+ where: {
1962
+ GroupCode,
1963
+ SystemPrivilegeId,
1964
+ },
1965
+ include: [
1966
+ {
1967
+ model: SystemPrivilegeModel,
1968
+ },
1969
+ ],
1970
+ transaction: dbTransaction,
1971
+ });
1972
+
1973
+ //If the record does not exist, throw a new ClassError
1974
+ if (!groupPrivilege) {
1975
+ throw new ClassError(
1976
+ 'Group',
1977
+ 'GroupErrMsg14',
1978
+ 'GroupPrivilege not found.',
1979
+ );
1980
+ }
1981
+
1982
+ //Set the EntityValueBefore to the GroupPrivilegesValue from step 1.c.
1983
+ const entityValueBefore = {
1984
+ GroupCode: groupPrivilege.GroupCode,
1985
+ SystemPrivilegeId: groupPrivilege.SystemPrivilegeId,
1986
+ Status: groupPrivilege.Status,
1987
+ CreatedById: groupPrivilege.CreatedById,
1988
+ CreatedAt: groupPrivilege.CreatedAt,
1989
+ UpdatedById: groupPrivilege.UpdatedById,
1990
+ UpdatedAt: groupPrivilege.UpdatedAt,
1991
+ };
1992
+
1993
+ //Call Group._GroupPrivilegeRepo.delete
1994
+ await Group._GroupPrivilegeRepo.delete(
1995
+ GroupCode,
1996
+ SystemPrivilegeId,
1997
+ dbTransaction,
1998
+ );
1999
+
2000
+ // Instantiate new activity from Activity class, call createId() method, then set:
2001
+ const activity = new Activity();
2002
+ activity.ActivityId = activity.createId();
2003
+ activity.Action = ActionEnum.DELETE;
2004
+ activity.Description = 'DELETE Group Privilege';
2005
+ activity.EntityType = 'GroupPrivilege';
2006
+ activity.EntityId = groupPrivilege.GroupPrivilegeId.toString();
2007
+ activity.EntityValueBefore = JSON.stringify(entityValueBefore);
2008
+ activity.EntityValueAfter = JSON.stringify({});
2009
+ //Call new activity create method
2010
+ await activity.create(loginUser.ObjectId, dbTransaction);
2011
+ }
2012
+ return 'Successfully deleted.';
2013
+ } catch (error) {
2014
+ throw error;
2015
+ }
2016
+ }
2017
+
2018
+ public static async getHierarchy(
2019
+ loginUser: LoginUser,
2020
+ dbTransaction: any,
2021
+ Status?: string,
2022
+ Type?: GroupTypeEnum,
2023
+ ParentGroupCode?: string,
2024
+ ) {
2025
+ // This method retrieves all group records from the sso_Group table in a hierarchical tree structure based on
2026
+ // the ParentGroupCode. The hierarchy respects any filtering options passed, such as status or group type, and
2027
+ // checks privileges of the requesting user.
2028
+
2029
+ // Part 1: Privilege Checking
2030
+ // Call loginUser.checkPrivileges() by passing:
2031
+ // - SystemCode: Retrieved from the system configuration.
2032
+ // - PrivilegeCode: 'GROUP_VIEW'.
2033
+ // Ensure the user has privileges to view group information.
2034
+ const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
2035
+ const isPrivileged = await loginUser.checkPrivileges(
2036
+ systemCode,
2037
+ 'GROUP_VIEW',
2038
+ );
2039
+
2040
+ if (!isPrivileged) {
2041
+ throw new ClassError(
2042
+ 'Group',
2043
+ 'GroupErrMsg04',
2044
+ 'User is not privileged to view group',
2045
+ );
2046
+ }
2047
+
2048
+ // Part 2: Prepare Filters
2049
+ // Set the default filter for Status to "Active", unless specified.
2050
+ // Use optional filters such as Type and ParentGroupCode if provided.
2051
+ const queryObj: any = {};
2052
+
2053
+ let options: any = {
2054
+ transaction: dbTransaction,
2055
+ };
2056
+
2057
+ const search = {
2058
+ Status: Status ? Status : 'Active',
2059
+ Type: Type,
2060
+ ParentGroupCode: ParentGroupCode,
2061
+ };
2062
+
2063
+ if (search) {
2064
+ Object.entries(search).forEach(([key, value]) => {
2065
+ if (value) {
2066
+ queryObj[key] = {
2067
+ [Op.substring]: value,
2068
+ };
2069
+ }
2070
+ });
2071
+
2072
+ options = {
2073
+ ...options,
2074
+ where: queryObj,
2075
+ };
2076
+
2077
+ // Part 3: Retrieve Group Data
2078
+ // Use Sequelize's findAll() method with the provided filters (status, type, and parentGroupCode) to query the sso_Group table.
2079
+ // Recursively build the group tree based on ParentGroupCode relationships.
2080
+
2081
+ const result = await Group._Repo.findAll(options);
2082
+ const data = result.map((group) => new Group(group.get({ plain: true })));
2083
+
2084
+ const generateTree = async (groups) => {
2085
+ // Create a map for easy lookup by code
2086
+
2087
+ const data = groups.map((group) => {
2088
+ return {
2089
+ GroupCode: group.ObjectId,
2090
+ Name: group.Name,
2091
+ Description: group.Description,
2092
+ Type: group.Type,
2093
+ ParentGroupCode: group.ParentGroupCode,
2094
+ InheritParentPrivilegeYN: group.InheritParentPrivilegeYN,
2095
+ InheritParentSystemAccessYN: group.InheritParentSystemAccessYN,
2096
+ Path: group._Path,
2097
+ Status: group.Status,
2098
+ CreatedById: group._CreatedById,
2099
+ CreatedAt: group._CreatedAt,
2100
+ UpdatedById: group._UpdatedById,
2101
+ UpdatedAt: group._UpdatedAt,
2102
+ childrens: [],
2103
+ };
2104
+ });
2105
+ const groupMap = {};
2106
+ data.forEach((group) => {
2107
+ group.childrens = [];
2108
+ groupMap[group.GroupCode] = group;
2109
+ });
2110
+
2111
+ // Initialize the root nodes (those without a parentCode)
2112
+ const tree = [];
2113
+
2114
+ data.forEach((group) => {
2115
+ if (group.ParentGroupCode) {
2116
+ // Add this group as a child of its parent
2117
+ const parent = groupMap[group.ParentGroupCode];
2118
+ if (parent) {
2119
+ parent.childrens.push(group);
2120
+ }
2121
+ } else {
2122
+ // If no parentCode, it's a root node
2123
+ tree.push(group);
2124
+ }
2125
+ });
2126
+
2127
+ return tree;
2128
+ };
2129
+
2130
+ const tree = await generateTree(data);
2131
+
2132
+ // Part 4: Return Results
2133
+ // Return the tree structure with child groups (under the childrens key) included only if a group has child groups.
2134
+ return tree;
2135
+ }
2136
+ }
2137
+
2138
+ public static async getGroupsWithReportingUser(
2139
+ loginUser: User, //The user performing the action.
2140
+ dbTransaction: Transaction, //Active database transaction.
2141
+ whereOptions: any, //The filter criteria for selecting groups.
2142
+ ) {
2143
+ try {
2144
+ // Part 1: Privilege Checking
2145
+ // Call loginUser.checkPrivileges() by passing:
2146
+ // SystemCode: Retrieve from app config.
2147
+ // PrivilegeCode: 'GROUP_VIEW'.
2148
+ const systemCode =
2149
+ ApplicationConfig.getComponentConfigValue('system-code');
2150
+ const isPrivileged = await loginUser.checkPrivileges(
2151
+ systemCode,
2152
+ 'GROUP_VIEW',
2153
+ );
2154
+ if (!isPrivileged) {
2155
+ throw new ClassError(
2156
+ 'Group',
2157
+ 'GroupErrMsg04',
2158
+ 'User is not privileged to view group',
2159
+ );
2160
+ }
2161
+ // Part 2: Prepare Group Query
2162
+ // Call Group._Repo.findAll() to fetch groups from the sso_Group table by passing:
2163
+ // where: whereOptions
2164
+ // include:
2165
+ // Model: sso_GroupReportingUsers
2166
+ // where: { Status: 'Active' }
2167
+ // include:
2168
+ // Model: sso_User
2169
+ // attributes: ['UserId', 'FullName']
2170
+ // attributes: ['GroupCode', 'UserId', 'Rank']
2171
+ const options = {
2172
+ where: whereOptions,
2173
+ include: [
2174
+ {
2175
+ model: GroupReportingUserModel,
2176
+ where: { Status: 'Active' },
2177
+ include: [
2178
+ {
2179
+ model: UserModel,
2180
+ as: 'User',
2181
+ attributes: ['UserId', 'FullName'],
2182
+ },
2183
+ ],
2184
+ },
2185
+ ],
2186
+ transaction: dbTransaction,
2187
+ };
2188
+ const groups = await Group._Repo.findAll(options);
2189
+ // Part 3: Retrieve and Return
2190
+ // Return the list of groups with nested reporting users.
2191
+ return groups;
2192
+ } catch (error) {
2193
+ throw error;
2194
+ }
2195
+ }
2196
+
2197
+ public async unassignUser(
2198
+ UserId: number,
2199
+ loginUser: LoginUser,
2200
+ dbTransaction: Transaction,
2201
+ ) {
2202
+ try {
2203
+ const systemCode =
2204
+ ApplicationConfig.getComponentConfigValue('system-code');
2205
+ const isPrivileged = await loginUser.checkPrivileges(
2206
+ systemCode,
2207
+ 'GROUP_UPDATE',
2208
+ );
2209
+
2210
+ if (!isPrivileged) {
2211
+ throw new ClassError(
2212
+ 'Group',
2213
+ 'GroupErrMsg05',
2214
+ 'You do not have the privilege to update group',
2215
+ );
2216
+ }
2217
+
2218
+ const userGroup = await UserGroup.findOne(
2219
+ dbTransaction,
2220
+ loginUser,
2221
+ this.GroupCode,
2222
+ UserId,
2223
+ );
2224
+
2225
+ if (!userGroup) {
2226
+ throw new ClassError(
2227
+ 'Group',
2228
+ 'GroupErrMsg07',
2229
+ 'User is not assigned to this group',
2230
+ );
2231
+ }
2232
+
2233
+ await userGroup.delete(loginUser, dbTransaction);
2234
+ } catch (error) {
2235
+ throw error;
2236
+ }
2237
+ }
2238
+
2239
+ public static async getGroupTree(
2240
+ groupCode: string | null,
2241
+ loginUser: LoginUser,
2242
+ dbTransaction: Transaction,
2243
+ ): Promise<Group[]> {
2244
+ try {
2245
+ const systemCode =
2246
+ ApplicationConfig.getComponentConfigValue('system-code');
2247
+ const isPrivileged = await loginUser.checkPrivileges(
2248
+ systemCode,
2249
+ 'GROUP_VIEW',
2250
+ );
2251
+
2252
+ if (!isPrivileged) {
2253
+ throw new ClassError(
2254
+ 'Group',
2255
+ 'GroupErrMsg04',
2256
+ 'User is not privileged to view group',
2257
+ );
2258
+ }
2259
+
2260
+ let groups: Group[] = [];
2261
+ if (groupCode != null) {
2262
+ let parentGroup = await Group.init(dbTransaction, groupCode);
2263
+ await parentGroup.loadChildren(dbTransaction);
2264
+ groups = parentGroup.children;
2265
+ } else {
2266
+ const data = await Group._Repo.findAll({
2267
+ where: {
2268
+ ParentGroupCode: null,
2269
+ },
2270
+ transaction: dbTransaction,
2271
+ });
2272
+
2273
+ for (const d of data) {
2274
+ const group = new Group(d.get({ plain: true }));
2275
+ groups.push(group);
2276
+ }
2277
+ }
2278
+
2279
+ return groups;
2280
+ } catch (error) {
2281
+ throw error;
2282
+ }
2283
+ }
2284
+ }