@tomei/sso 0.61.0 → 0.62.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (257) hide show
  1. package/.commitlintrc.json +22 -22
  2. package/.gitlab-ci.yml +16 -16
  3. package/.husky/commit-msg +15 -15
  4. package/.husky/pre-commit +7 -7
  5. package/.prettierrc +4 -4
  6. package/Jenkinsfile +57 -57
  7. package/README.md +23 -23
  8. package/__tests__/unit/components/group/group.spec.ts +79 -79
  9. package/__tests__/unit/components/group-object-privilege/group-object-privilege.spec.ts +88 -88
  10. package/__tests__/unit/components/group-privilege/group-privilege.spec.ts +68 -68
  11. package/__tests__/unit/components/group-reporting-user/group-reporting-user.spec.ts +66 -66
  12. package/__tests__/unit/components/group-system-access/group-system-access.spec.ts +83 -83
  13. package/__tests__/unit/components/login-user/l.spec.ts +746 -746
  14. package/__tests__/unit/components/login-user/login.spec.ts +1164 -1164
  15. package/__tests__/unit/components/password-hash/password-hash.service.spec.ts +31 -31
  16. package/__tests__/unit/components/system/system.spec.ts +254 -254
  17. package/__tests__/unit/components/system-privilege/system-privilege.spec.ts +83 -83
  18. package/__tests__/unit/components/user-group/user-group.spec.ts +86 -86
  19. package/__tests__/unit/components/user-object-privilege/user-object-privilege.spec.ts +78 -78
  20. package/__tests__/unit/components/user-privilege/user-privilege.spec.ts +72 -72
  21. package/__tests__/unit/components/user-system-access/user-system-access.spec.ts +89 -89
  22. package/__tests__/unit/redis-client/redis.service.spec.ts +23 -23
  23. package/__tests__/unit/session/session.service.spec.ts +47 -47
  24. package/__tests__/unit/system-privilege/system-privilage.spec.ts +91 -91
  25. package/coverage/clover.xml +1452 -1452
  26. package/coverage/coverage-final.json +47 -47
  27. package/coverage/lcov-report/base.css +224 -224
  28. package/coverage/lcov-report/block-navigation.js +87 -87
  29. package/coverage/lcov-report/components/group/group.repository.ts.html +117 -117
  30. package/coverage/lcov-report/components/group/group.ts.html +327 -327
  31. package/coverage/lcov-report/components/group/index.html +130 -130
  32. package/coverage/lcov-report/components/group-object-privilege/group-object-privilege.repository.ts.html +117 -117
  33. package/coverage/lcov-report/components/group-object-privilege/group-object-privilege.ts.html +321 -321
  34. package/coverage/lcov-report/components/group-object-privilege/index.html +130 -130
  35. package/coverage/lcov-report/components/group-privilege/group-privilege.repository.ts.html +117 -117
  36. package/coverage/lcov-report/components/group-privilege/group-privilege.ts.html +303 -303
  37. package/coverage/lcov-report/components/group-privilege/index.html +130 -130
  38. package/coverage/lcov-report/components/group-reporting-user/group-reporting-user.repository.ts.html +117 -117
  39. package/coverage/lcov-report/components/group-reporting-user/group-reporting-user.ts.html +327 -327
  40. package/coverage/lcov-report/components/group-reporting-user/index.html +130 -130
  41. package/coverage/lcov-report/components/group-system-access/group-system-access.repository.ts.html +117 -117
  42. package/coverage/lcov-report/components/group-system-access/group-system-access.ts.html +309 -309
  43. package/coverage/lcov-report/components/group-system-access/index.html +130 -130
  44. package/coverage/lcov-report/components/login-history/index.html +115 -115
  45. package/coverage/lcov-report/components/login-history/login-history.repository.ts.html +117 -117
  46. package/coverage/lcov-report/components/login-user/index.html +130 -130
  47. package/coverage/lcov-report/components/login-user/login-user.ts.html +5007 -5007
  48. package/coverage/lcov-report/components/login-user/user.repository.ts.html +117 -117
  49. package/coverage/lcov-report/components/password-hash/index.html +115 -115
  50. package/coverage/lcov-report/components/password-hash/password-hash.service.ts.html +126 -126
  51. package/coverage/lcov-report/components/system/index.html +130 -130
  52. package/coverage/lcov-report/components/system/system.repository.ts.html +117 -117
  53. package/coverage/lcov-report/components/system/system.ts.html +909 -909
  54. package/coverage/lcov-report/components/system-privilege/index.html +130 -130
  55. package/coverage/lcov-report/components/system-privilege/system-privilege.repository.ts.html +120 -120
  56. package/coverage/lcov-report/components/system-privilege/system-privilege.ts.html +390 -390
  57. package/coverage/lcov-report/components/user-group/index.html +130 -130
  58. package/coverage/lcov-report/components/user-group/user-group.repository.ts.html +117 -117
  59. package/coverage/lcov-report/components/user-group/user-group.ts.html +354 -354
  60. package/coverage/lcov-report/components/user-object-privilege/index.html +130 -130
  61. package/coverage/lcov-report/components/user-object-privilege/user-object-privilege.repository.ts.html +117 -117
  62. package/coverage/lcov-report/components/user-object-privilege/user-object-privilege.ts.html +312 -312
  63. package/coverage/lcov-report/components/user-privilege/index.html +130 -130
  64. package/coverage/lcov-report/components/user-privilege/user-privilege.repository.ts.html +117 -117
  65. package/coverage/lcov-report/components/user-privilege/user-privilege.ts.html +306 -306
  66. package/coverage/lcov-report/components/user-system-access/index.html +130 -130
  67. package/coverage/lcov-report/components/user-system-access/user-system-access.repository.ts.html +117 -117
  68. package/coverage/lcov-report/components/user-system-access/user-system-access.ts.html +312 -312
  69. package/coverage/lcov-report/enum/group-type.enum.ts.html +108 -108
  70. package/coverage/lcov-report/enum/index.html +160 -160
  71. package/coverage/lcov-report/enum/index.ts.html +93 -93
  72. package/coverage/lcov-report/enum/user-status.enum.ts.html +105 -105
  73. package/coverage/lcov-report/enum/yn.enum.ts.html +96 -96
  74. package/coverage/lcov-report/index.html +370 -370
  75. package/coverage/lcov-report/models/group-object-privilege.entity.ts.html +333 -333
  76. package/coverage/lcov-report/models/group-privilege.entity.ts.html +315 -315
  77. package/coverage/lcov-report/models/group-reporting-user.entity.ts.html +339 -339
  78. package/coverage/lcov-report/models/group-system-access.entity.ts.html +324 -324
  79. package/coverage/lcov-report/models/group.entity.ts.html +435 -435
  80. package/coverage/lcov-report/models/index.html +310 -310
  81. package/coverage/lcov-report/models/login-history.entity.ts.html +252 -252
  82. package/coverage/lcov-report/models/staff.entity.ts.html +411 -411
  83. package/coverage/lcov-report/models/system-privilege.entity.ts.html +354 -354
  84. package/coverage/lcov-report/models/system.entity.ts.html +423 -423
  85. package/coverage/lcov-report/models/user-group.entity.ts.html +354 -354
  86. package/coverage/lcov-report/models/user-object-privilege.entity.ts.html +330 -330
  87. package/coverage/lcov-report/models/user-privilege.entity.ts.html +315 -315
  88. package/coverage/lcov-report/models/user-system-access.entity.ts.html +315 -315
  89. package/coverage/lcov-report/models/user.entity.ts.html +522 -522
  90. package/coverage/lcov-report/prettify.css +1 -1
  91. package/coverage/lcov-report/prettify.js +2 -2
  92. package/coverage/lcov-report/redis-client/index.html +115 -115
  93. package/coverage/lcov-report/redis-client/redis.service.ts.html +240 -240
  94. package/coverage/lcov-report/session/index.html +115 -115
  95. package/coverage/lcov-report/session/session.service.ts.html +246 -246
  96. package/coverage/lcov-report/sorter.js +196 -196
  97. package/coverage/lcov.info +2490 -2490
  98. package/coverage/test-report.xml +128 -128
  99. package/create-sso-user.sql +39 -39
  100. package/dist/src/components/login-history/login-history.d.ts +23 -0
  101. package/dist/src/components/login-history/login-history.js +88 -0
  102. package/dist/src/components/login-history/login-history.js.map +1 -0
  103. package/dist/src/components/login-user/user.js +4 -3
  104. package/dist/src/components/login-user/user.js.map +1 -1
  105. package/dist/src/interfaces/login-history-search-attr.interface.d.ts +8 -0
  106. package/dist/src/interfaces/login-history-search-attr.interface.js +3 -0
  107. package/dist/src/interfaces/login-history-search-attr.interface.js.map +1 -0
  108. package/dist/src/interfaces/login-history.interface.d.ts +11 -0
  109. package/dist/src/interfaces/login-history.interface.js +3 -0
  110. package/dist/src/interfaces/login-history.interface.js.map +1 -0
  111. package/dist/tsconfig.tsbuildinfo +1 -1
  112. package/eslint.config.mjs +58 -58
  113. package/jest.config.js +14 -14
  114. package/migrations/20240314080602-create-user-table.js +124 -124
  115. package/migrations/20240314080603-create-user-group-table.js +85 -85
  116. package/migrations/20240314080604-create-user-user-group-table.js +55 -55
  117. package/migrations/20240314080605-create-login-history-table.js +53 -53
  118. package/migrations/20240527064925-create-system-table.js +78 -78
  119. package/migrations/20240527064926-create-system-privilege-table.js +71 -71
  120. package/migrations/20240527065342-create-group-table.js +93 -93
  121. package/migrations/20240527065633-create-group-reporting-user-table.js +76 -76
  122. package/migrations/20240528011551-create-group-system-access-table.js +72 -72
  123. package/migrations/20240528023018-user-system-access-table.js +75 -75
  124. package/migrations/20240528032229-user-privilege-table.js +76 -76
  125. package/migrations/20240528063003-create-group-privilege-table.js +76 -76
  126. package/migrations/20240528063051-create-group-object-privilege-table.js +84 -84
  127. package/migrations/20240528063107-create-user-object-privilege-table.js +84 -84
  128. package/migrations/20240528063108-create-api-key-table.js +85 -85
  129. package/migrations/20241104104802-create-building-table.js +95 -95
  130. package/migrations/20250108091132-add-area-manager-user-id-to-building-table.js +14 -14
  131. package/migrations/20250108091133-add-passcode-to-user-table.js +36 -36
  132. package/migrations/20250210115636-create-user-reporting-hierarchy.js +76 -76
  133. package/migrations/20250326043818-crate-user-password-history.js +42 -42
  134. package/package.json +90 -90
  135. package/sampledotenv +7 -7
  136. package/sonar-project.properties +22 -22
  137. package/src/components/api-key/api-key.repository.ts +15 -15
  138. package/src/components/api-key/api-key.ts +448 -448
  139. package/src/components/api-key/index.ts +4 -4
  140. package/src/components/building/building.repository.ts +27 -27
  141. package/src/components/building/index.ts +2 -2
  142. package/src/components/group/group.repository.ts +26 -26
  143. package/src/components/group/group.ts +2284 -2284
  144. package/src/components/group/index.ts +3 -3
  145. package/src/components/group-object-privilege/group-object-privilege.repository.ts +25 -25
  146. package/src/components/group-object-privilege/group-object-privilege.ts +278 -278
  147. package/src/components/group-object-privilege/index.ts +2 -2
  148. package/src/components/group-privilege/group-privilege.repository.ts +29 -29
  149. package/src/components/group-privilege/group-privilege.ts +84 -84
  150. package/src/components/group-privilege/index.ts +2 -2
  151. package/src/components/group-reporting-user/group-reporting-user.repository.ts +23 -23
  152. package/src/components/group-reporting-user/group-reporting-user.ts +506 -506
  153. package/src/components/group-reporting-user/index.ts +3 -3
  154. package/src/components/group-system-access/group-system-access.repository.ts +43 -43
  155. package/src/components/group-system-access/group-system-access.ts +90 -90
  156. package/src/components/group-system-access/index.ts +2 -2
  157. package/src/components/index.ts +20 -20
  158. package/src/components/login-history/index.ts +1 -1
  159. package/src/components/login-history/login-history.repository.ts +11 -11
  160. package/src/components/login-user/index.ts +5 -5
  161. package/src/components/login-user/interfaces/check-user-info-duplicated.interface.ts +7 -7
  162. package/src/components/login-user/interfaces/index.ts +1 -1
  163. package/src/components/login-user/interfaces/system-access.interface.ts +13 -13
  164. package/src/components/login-user/interfaces/user-info.interface.ts +34 -34
  165. package/src/components/login-user/login-user.ts +362 -362
  166. package/src/components/login-user/user.repository.ts +11 -11
  167. package/src/components/login-user/user.ts +3143 -3142
  168. package/src/components/password-hash/index.ts +2 -2
  169. package/src/components/password-hash/interfaces/index.ts +1 -1
  170. package/src/components/password-hash/interfaces/password-hash-service.interface.ts +4 -4
  171. package/src/components/password-hash/password-hash.service.ts +14 -14
  172. package/src/components/staff/index.ts +2 -2
  173. package/src/components/staff/staff.repository.ts +27 -27
  174. package/src/components/system/index.ts +3 -3
  175. package/src/components/system/system.repository.ts +11 -11
  176. package/src/components/system/system.ts +456 -456
  177. package/src/components/system-privilege/index.ts +4 -4
  178. package/src/components/system-privilege/system-privilege.repository.ts +18 -18
  179. package/src/components/system-privilege/system-privilege.ts +541 -541
  180. package/src/components/user-group/index.ts +2 -2
  181. package/src/components/user-group/user-group.repository.ts +19 -19
  182. package/src/components/user-group/user-group.ts +764 -764
  183. package/src/components/user-object-privilege/index.ts +2 -2
  184. package/src/components/user-object-privilege/user-object-privilege.repository.ts +11 -11
  185. package/src/components/user-object-privilege/user-object-privilege.ts +79 -79
  186. package/src/components/user-password-history/index.ts +2 -2
  187. package/src/components/user-password-history/user-password-history.repository.ts +39 -39
  188. package/src/components/user-password-history/user-password-history.ts +187 -187
  189. package/src/components/user-privilege/index.ts +2 -2
  190. package/src/components/user-privilege/user-privilege.repository.ts +25 -25
  191. package/src/components/user-privilege/user-privilege.ts +662 -662
  192. package/src/components/user-reporting-hierarchy/index.ts +2 -2
  193. package/src/components/user-reporting-hierarchy/user-reporting-hierarchy.repository.ts +30 -30
  194. package/src/components/user-reporting-hierarchy/user-reporting-hierarchy.ts +505 -505
  195. package/src/components/user-system-access/index.ts +2 -2
  196. package/src/components/user-system-access/user-system-access.repository.ts +41 -41
  197. package/src/components/user-system-access/user-system-access.ts +717 -717
  198. package/src/database.ts +15 -15
  199. package/src/enum/api-key.enum.ts +5 -5
  200. package/src/enum/building-type.enum.ts +6 -6
  201. package/src/enum/group-type.enum.ts +8 -8
  202. package/src/enum/index.ts +6 -6
  203. package/src/enum/login-status.enum.ts +4 -4
  204. package/src/enum/object-status.enum.ts +4 -4
  205. package/src/enum/user-status.enum.ts +7 -7
  206. package/src/enum/yn.enum.ts +4 -4
  207. package/src/index.ts +8 -8
  208. package/src/interfaces/api-key-attr.interface.ts +16 -16
  209. package/src/interfaces/group-object-privilege.interface.ts +14 -14
  210. package/src/interfaces/group-privilege.interface.ts +10 -10
  211. package/src/interfaces/group-reporting-user.interface.ts +11 -11
  212. package/src/interfaces/group-search-attr.interface.ts +9 -9
  213. package/src/interfaces/group-system-access.interface.ts +10 -10
  214. package/src/interfaces/group.interface.ts +17 -17
  215. package/src/interfaces/index.ts +13 -13
  216. package/src/interfaces/system-login.interface.ts +6 -6
  217. package/src/interfaces/system-privilege-search.interface.ts +5 -5
  218. package/src/interfaces/system-privilege.interface.ts +11 -11
  219. package/src/interfaces/system-search-attr.interface.ts +5 -5
  220. package/src/interfaces/system.interface.ts +15 -15
  221. package/src/interfaces/user-group.interface.ts +12 -12
  222. package/src/interfaces/user-object-privilege.interface.ts +14 -14
  223. package/src/interfaces/user-password-history.interface.ts +6 -6
  224. package/src/interfaces/user-privilege.interface.ts +10 -10
  225. package/src/interfaces/user-reporting-hierarchy.interface.ts +11 -11
  226. package/src/interfaces/user-session.interface.ts +5 -5
  227. package/src/interfaces/user-system-access.interface.ts +10 -10
  228. package/src/models/api-key-entity.ts +101 -101
  229. package/src/models/building.entity.ts +103 -103
  230. package/src/models/group-object-privilege.entity.ts +91 -91
  231. package/src/models/group-privilege.entity.ts +78 -78
  232. package/src/models/group-reporting-user.entity.ts +95 -95
  233. package/src/models/group-system-access.entity.ts +81 -81
  234. package/src/models/group.entity.ts +127 -127
  235. package/src/models/login-history.entity.ts +63 -63
  236. package/src/models/staff.entity.ts +91 -91
  237. package/src/models/system-privilege.entity.ts +90 -90
  238. package/src/models/system.entity.ts +113 -113
  239. package/src/models/user-group.entity.ts +91 -91
  240. package/src/models/user-object-privilege.entity.ts +90 -90
  241. package/src/models/user-password-history.ts +51 -51
  242. package/src/models/user-privilege.entity.ts +78 -78
  243. package/src/models/user-reporting-hierarchy.entity.ts +102 -102
  244. package/src/models/user-system-access.entity.ts +87 -87
  245. package/src/models/user.entity.ts +193 -193
  246. package/src/redis-client/__mocks__/jest-initial-setup.ts +2 -2
  247. package/src/redis-client/__mocks__/redis-mock.ts +28 -28
  248. package/src/redis-client/index.ts +1 -1
  249. package/src/redis-client/redis.service.ts +75 -75
  250. package/src/session/index.ts +2 -2
  251. package/src/session/interfaces/index.ts +1 -1
  252. package/src/session/interfaces/session-service.interface.ts +26 -26
  253. package/src/session/session.service.ts +96 -96
  254. package/src/types/auth-context.ts +10 -10
  255. package/src/types/index.ts +1 -1
  256. package/tsconfig.build.json +5 -5
  257. package/tsconfig.json +23 -23
@@ -1,506 +1,506 @@
1
- import { Op, Transaction } from 'sequelize';
2
- import { ClassError, ObjectBase } from '@tomei/general';
3
- import { GroupReportingUserRepository } from './group-reporting-user.repository';
4
- import { IGroupReportingUserAttr } from '../../interfaces/group-reporting-user.interface';
5
- import { User } from '../login-user/user';
6
- import UserModel from '../../models/user.entity';
7
- import { Group } from '../group/group';
8
- import { ApplicationConfig } from '@tomei/config';
9
- import { ActionEnum, Activity } from '@tomei/activity-history';
10
-
11
- export class GroupReportingUser extends ObjectBase {
12
- ObjectId: string;
13
- ObjectName: string;
14
- TableName = 'sso_GroupReportingUser';
15
- ObjectType = 'GroupReportingUser';
16
-
17
- GroupReportingUserId: number;
18
- GroupCode: string;
19
- UserId: number;
20
- Rank: number;
21
- Status: string;
22
- private _CreatedById: number;
23
- private _CreatedAt: Date;
24
- private _UpdatedById: number;
25
- private _UpdatedAt: Date;
26
- private static _Repo = new GroupReportingUserRepository();
27
-
28
- get CreatedById(): number {
29
- return this._CreatedById;
30
- }
31
-
32
- get CreatedAt(): Date {
33
- return this._CreatedAt;
34
- }
35
-
36
- get UpdatedById(): number {
37
- return this._UpdatedById;
38
- }
39
-
40
- get UpdatedAt(): Date {
41
- return this._UpdatedAt;
42
- }
43
-
44
- private constructor(groupReportingUserAttr?: IGroupReportingUserAttr) {
45
- super();
46
- if (groupReportingUserAttr) {
47
- this.GroupReportingUserId = groupReportingUserAttr.GroupReportingUserId;
48
- this.GroupCode = groupReportingUserAttr.GroupCode;
49
- this.UserId = groupReportingUserAttr?.UserId;
50
- this.Rank = groupReportingUserAttr?.Rank;
51
- this.Status = groupReportingUserAttr?.Status;
52
- this._CreatedById = groupReportingUserAttr.CreatedById;
53
- this._CreatedAt = groupReportingUserAttr.CreatedAt;
54
- this._UpdatedById = groupReportingUserAttr.UpdatedById;
55
- this._UpdatedAt = groupReportingUserAttr.UpdatedAt;
56
- }
57
- }
58
-
59
- public static async init(dbTransaction: any, GroupReportingUserId?: string) {
60
- try {
61
- if (GroupReportingUserId) {
62
- const groupReportingUser = await GroupReportingUser._Repo.findByPk(
63
- GroupReportingUserId,
64
- {
65
- transaction: dbTransaction,
66
- },
67
- );
68
- if (groupReportingUser) {
69
- return new GroupReportingUser(groupReportingUser);
70
- } else {
71
- throw new ClassError(
72
- 'GroupReportingUser',
73
- 'GroupReportingUserErrMsg00',
74
- 'GroupReportingUser Not Found',
75
- 'init',
76
- 404,
77
- );
78
- }
79
- }
80
- return new GroupReportingUser();
81
- } catch (error) {
82
- throw error;
83
- }
84
- }
85
-
86
- async createGroupReportingUser(
87
- loginUser: User, //The user performing the operation(typically the logged -in user).
88
- dbTransaction: any, //Database transaction object to ensure the operation is atomic.
89
- groupCode: string, //The code of the group to which the user is being assigned.
90
- userId: number, //The ID of the user to be added to the group.
91
- rank: number, //The rank to be assigned to the user in the group.
92
- status: 'Active' | 'Inactive', //The initial status of the user in the group.
93
- ): Promise<GroupReportingUser> {
94
- // Returns a GroupReportingUser instance representing the newly created record.
95
- try {
96
- //Creates a new group reporting user entry in the sso_GroupReportingUser table.
97
-
98
- // Validate Input Parameters
99
- // Ensure groupCode exists in the sso_Group table by calling the Group.init() method.
100
- const group = await Group.init(dbTransaction, groupCode);
101
- // Ensure userId exists in the sso_User table by calling the User.init() method.
102
- const user = await User.init(dbTransaction, userId);
103
- // Privilege Checking
104
- // Call the loginUser.checkPrivileges() method by passing:
105
- // SystemCode: Retrieve from app config.
106
- // PrivilegeCode: "GROUP_REPORTING_USER_CREATE".
107
-
108
- const systemCode =
109
- ApplicationConfig.getComponentConfigValue('system-code');
110
- const isPrivileged = await loginUser.checkPrivileges(
111
- systemCode,
112
- 'GROUP_REPORTING_USER_CREATE',
113
- );
114
- if (!isPrivileged) {
115
- throw new ClassError(
116
- 'GroupReportingUser',
117
- 'GroupReportingUserErrMsg02',
118
- 'Insufficient privileges to add a user to the group',
119
- );
120
- }
121
-
122
- // Check for Duplicate User in Group
123
- // Query the sso_GroupReportingUser table to see if the userId already exists in the specified groupCode.
124
- const groupReportingUser = await GroupReportingUser._Repo.findOne({
125
- where: {
126
- GroupCode: groupCode,
127
- UserId: userId,
128
- },
129
- transaction: dbTransaction,
130
- });
131
- // If the user already exists in the group, throw an error indicating the user is already part of the group.
132
- if (groupReportingUser) {
133
- throw new ClassError(
134
- 'GroupReportingUser',
135
- 'GroupReportingUserErrMsg03',
136
- 'User already exists in the group',
137
- 'createGroupReportingUser',
138
- );
139
- }
140
- //Query the sso_GroupReportingUser table to see if the rank already exists in the specified groupCode.
141
- //If the rank already exists in the group, throw an error indicating the rank is already in of the group.
142
- const groupReportingUserRank = await GroupReportingUser._Repo.findOne({
143
- where: {
144
- GroupCode: groupCode,
145
- Rank: rank,
146
- },
147
- transaction: dbTransaction,
148
- });
149
- if (groupReportingUserRank) {
150
- throw new ClassError(
151
- 'GroupReportingUser',
152
- 'GroupReportingUserErrMsg04',
153
- 'Rank already exists in the group',
154
- 'createGroupReportingUser',
155
- );
156
- }
157
-
158
- // Create GroupReportingUser Entry
159
- // If validation and privilege checks pass, insert a new record in the sso_GroupReportingUser table with the provided groupCode, userId, rank, status, and loginUser.UserId.Automatically capture the current timestamp for CreatedAt.
160
- this.GroupCode = groupCode;
161
- this.UserId = userId;
162
- this.Rank = rank;
163
- this.Status = status;
164
- this._CreatedById = loginUser.UserId;
165
- this._CreatedAt = new Date();
166
- this._UpdatedAt = new Date();
167
- this._UpdatedById = loginUser.UserId;
168
-
169
- const entityValueAfter: any = {
170
- GroupCode: groupCode,
171
- UserId: userId,
172
- Rank: rank,
173
- Status: status,
174
- CreatedById: loginUser.UserId,
175
- CreatedAt: this._CreatedAt,
176
- UpdatedById: loginUser.UserId,
177
- UpdatedAt: this._UpdatedAt,
178
- };
179
-
180
- const newGroupReportingUser = await GroupReportingUser._Repo.create(
181
- entityValueAfter,
182
- {
183
- transaction: dbTransaction,
184
- },
185
- );
186
-
187
- entityValueAfter.GroupReportingUserId =
188
- newGroupReportingUser.GroupReportingUserId;
189
-
190
- // Record Create Activity
191
- // Instantiate a new activity from the Activity class, and set:\
192
- // ActivityId: activity.createId()
193
- // Action: ActionEnum.Create
194
- // Description: Create Group Reporting User
195
- // EntityType: GroupReportingUser
196
- // EntityId: newGroupReportingUser.GroupReportingUserId
197
- // EntityValueBefore: Stringified empty object({})
198
- // EntityValueAfter: EntityValueAfter(stringified representation of the newly created entity)
199
- const activity = new Activity();
200
- activity.ActivityId = activity.createId();
201
- activity.Action = ActionEnum.CREATE;
202
- activity.Description = 'Create Group Reporting User';
203
- activity.EntityType = 'GroupReportingUser';
204
- activity.EntityId = newGroupReportingUser.GroupReportingUserId.toString();
205
- activity.EntityValueBefore = JSON.stringify({});
206
- activity.EntityValueAfter = JSON.stringify(entityValueAfter);
207
- // Call the activity create() method by passing:
208
- // dbTransaction
209
- // userId: loginUser.UserId
210
- await activity.create(loginUser.ObjectId, dbTransaction);
211
- // Return the Created GroupReportingUser
212
- // Return the newly created GroupReportingUser instance, including all the relevant details like GroupReportingUserId, groupCode, userId, rank, status, and timestamps for CreatedAt.
213
- return this;
214
- } catch (error) {
215
- throw error;
216
- }
217
- }
218
-
219
- async updateGroupReportingUser(
220
- loginUser: User, //The user performing the operation(typically the logged -in user).
221
- dbTransaction: any, //Database transaction object to ensure the operation is atomic.
222
- groupCode: string, //The code of the group to which the user is being assigned.
223
- userId: number, //The ID of the user to be added to the group.
224
- rank: number, //The rank to be assigned to the user in the group.
225
- status: 'Active' | 'Inactive', //The initial status of the user in the group.
226
- ): Promise<GroupReportingUser> {
227
- // Returns a GroupReportingUser instance representing the updated record.
228
- try {
229
- //Update a group reporting user entry in the sso_GroupReportingUser table.
230
-
231
- // Validate Input Parameters
232
- // Ensure groupCode exists in the sso_Group table by calling the Group.init() method.
233
- await Group.init(dbTransaction, groupCode);
234
- // Ensure userId exists in the sso_User table by calling the User.init() method.
235
- await User.init(dbTransaction, userId);
236
- // Privilege Checking
237
- // Call the loginUser.checkPrivileges() method by passing:
238
- // SystemCode: Retrieve from app config.
239
- // PrivilegeCode: "GROUP_REPORTING_USER_UPDATE".
240
-
241
- const systemCode =
242
- ApplicationConfig.getComponentConfigValue('system-code');
243
- const isPrivileged = await loginUser.checkPrivileges(
244
- systemCode,
245
- 'GROUP_REPORTING_USER_UPDATE',
246
- );
247
- if (!isPrivileged) {
248
- throw new ClassError(
249
- 'GroupReportingUser',
250
- 'GroupReportingUserErrMsg02',
251
- 'Insufficient privileges to update a user to the group',
252
- );
253
- }
254
-
255
- //Get the current groupReportingUser
256
- const currentGroupReportingUser = await GroupReportingUser._Repo.findOne({
257
- where: {
258
- GroupCode: groupCode,
259
- GroupReportingUserId: this.GroupReportingUserId,
260
- },
261
- transaction: dbTransaction,
262
- });
263
-
264
- // Check for Duplicate User in Group
265
- // Query the sso_GroupReportingUser table to see if the userId already exists in the specified groupCode.
266
- const groupReportingUser = await GroupReportingUser._Repo.findOne({
267
- where: {
268
- GroupCode: groupCode,
269
- UserId: userId,
270
- GroupReportingUserId: {
271
- [Op.ne]: this.GroupReportingUserId,
272
- },
273
- },
274
- transaction: dbTransaction,
275
- });
276
- // If the user already exists in the group, throw an error indicating the user is already part of the group.
277
- if (groupReportingUser) {
278
- throw new ClassError(
279
- 'GroupReportingUser',
280
- 'GroupReportingUserErrMsg03',
281
- 'User already exists in the group',
282
- 'updateGroupReportingUser',
283
- );
284
- }
285
- //Query the sso_GroupReportingUser table to see if the rank already exists in the specified groupCode.
286
- //If the rank already exists in the group, throw an error indicating the rank is already in of the group.
287
- const groupReportingUserRank = await GroupReportingUser._Repo.findOne({
288
- where: {
289
- GroupCode: groupCode,
290
- Rank: rank,
291
- GroupReportingUserId: {
292
- [Op.ne]: this.GroupReportingUserId,
293
- },
294
- },
295
- transaction: dbTransaction,
296
- });
297
- if (groupReportingUserRank) {
298
- throw new ClassError(
299
- 'GroupReportingUser',
300
- 'GroupReportingUserErrMsg04',
301
- 'Rank already exists in the group',
302
- 'updateGroupReportingUser',
303
- );
304
- }
305
-
306
- // UPDATE GroupReportingUser Entry
307
- // If validation and privilege checks pass, insert a new record in the sso_GroupReportingUser table with the provided groupCode, userId, rank, status, and loginUser.UserId.Automatically capture the current timestamp for CreatedAt.
308
- this.GroupCode = groupCode;
309
- this.UserId = userId;
310
- this.Rank = rank;
311
- this.Status = status;
312
- this._CreatedById = currentGroupReportingUser.CreatedById;
313
- this._CreatedAt = currentGroupReportingUser.CreatedAt;
314
- this._UpdatedAt = new Date();
315
- this._UpdatedById = loginUser.UserId;
316
-
317
- const entityValueAfter: any = {
318
- GroupCode: groupCode,
319
- UserId: userId,
320
- Rank: rank,
321
- Status: status,
322
- CreatedById: currentGroupReportingUser.CreatedById,
323
- CreatedAt: this._CreatedAt,
324
- UpdatedById: loginUser.UserId,
325
- UpdatedAt: this._UpdatedAt,
326
- };
327
-
328
- await GroupReportingUser._Repo.update(entityValueAfter, {
329
- where: {
330
- GroupReportingUserId: this.GroupReportingUserId,
331
- },
332
- transaction: dbTransaction,
333
- });
334
-
335
- // Record Update Activity
336
- // Instantiate a new activity from the Activity class, and set:\
337
- // ActivityId: activity.createId()
338
- // Action: ActionEnum.Update
339
- // Description: Update Group Reporting User
340
- // EntityType: GroupReportingUser
341
- // EntityId: newGroupReportingUser.GroupReportingUserId
342
- // EntityValueBefore: Stringified empty object({})
343
- // EntityValueAfter: EntityValueAfter(stringified representation of the newly created entity)
344
- const activity = new Activity();
345
- activity.ActivityId = activity.createId();
346
- activity.Action = ActionEnum.UPDATE;
347
- activity.Description = 'Update Group Reporting User';
348
- activity.EntityType = 'GroupReportingUser';
349
- activity.EntityId = this.GroupReportingUserId.toString();
350
- activity.EntityValueBefore = JSON.stringify({});
351
- activity.EntityValueAfter = JSON.stringify(entityValueAfter);
352
- // Call the activity create() method by passing:
353
- // dbTransaction
354
- // userId: loginUser.UserId
355
- await activity.create(loginUser.ObjectId, dbTransaction);
356
- // Return the Updated GroupReportingUser
357
- // Return the updated GroupReportingUser instance, including all the relevant details like GroupReportingUserId, groupCode, userId, rank, status, and timestamps for CreatedAt.
358
- return this;
359
- } catch (error) {
360
- throw error;
361
- }
362
- }
363
-
364
- public static async findAllGroupReportingUsers(
365
- loginUser: User, //The authenticated user requesting the information.
366
- dbTransaction: any, //The database transaction to be used for this operation.
367
- groupCode?: string, //The code of the group whose reporting users should be retrieved.
368
- ) {
369
- //This public static method retrieves all GroupReportingUser records for a given group from the sso_GroupReportingUser table.
370
-
371
- try {
372
- // Part 1: Privilege Check
373
- // Call loginUser.checkPrivileges() method by passing:
374
- // - SystemCode: Retrieve from the app config.
375
- // - PrivilegeCode: GROUP_REPORTING_USER_VIEW.
376
- // If the user does not have the required privilege, throw a ForbiddenError.
377
- const systemCode =
378
- ApplicationConfig.getComponentConfigValue('system-code');
379
- const isPrivileged = await loginUser.checkPrivileges(
380
- systemCode,
381
- 'GROUP_REPORTING_USER_VIEW',
382
- );
383
-
384
- if (!isPrivileged) {
385
- throw new ClassError(
386
- 'Group',
387
- 'GroupReportingUserErrMsg05',
388
- 'You do not have the privilege to view group reporting user',
389
- );
390
- }
391
-
392
- // Part 2: Group Existence Check
393
- // Call Group.init(dbTransaction, groupCode) to verify the group exists.
394
- // If the group does not exist, throw a NotFoundError.
395
- await Group.init(dbTransaction, groupCode);
396
-
397
- // Part 3: Retrieve Group Reporting Users
398
- // Call GroupReportingUser._Repo.findAll() to retrieve all users associated with the provided groupCode.
399
- // The users should be sorted by Rank in ascending order (Rank 1, Rank 2, and so on).
400
- // Ensure the query is performed within the dbTransaction.
401
- const result = await GroupReportingUser._Repo.findAll({
402
- where: {
403
- GroupCode: groupCode,
404
- },
405
- include: [
406
- {
407
- model: UserModel,
408
- as: 'User',
409
- },
410
- ],
411
- order: [
412
- ['Rank', 'ASC'], // or 'DESC' for descending order
413
- ],
414
- transaction: dbTransaction,
415
- });
416
-
417
- // Part 4: Return Results
418
- // Return the array of GroupReportingUser records found.
419
-
420
- return result;
421
- } catch (error) {
422
- // Part 5: Handle Errors
423
- // Catch and handle any errors during the execution. If an error occurs, ensure the transaction is rolled back.
424
- throw error;
425
- }
426
- }
427
-
428
- public static async removeGroupReportingUser(
429
- loginUser: User, //The user performing the operation, used for privilege checking and logging.
430
- dbTransaction: Transaction, // The database transaction object to ensure the operation's atomicity.
431
- groupReportingUserId: number, //The ID of the GroupReportingUser to be removed.
432
- ): Promise<void> {
433
- // This method removes a GroupReportingUser record from the database.
434
- try {
435
- // Part 1: Privilege Checking
436
- // Call loginUser.checkPrivileges() method by passing:
437
- // SystemCode: Retrieve from app config.
438
- // PrivilegeCode: "GROUP_REPORTING_USER_REMOVE".
439
- const systemCode =
440
- ApplicationConfig.getComponentConfigValue('system-code');
441
- const isPrivileged = await loginUser.checkPrivileges(
442
- systemCode,
443
- 'GROUP_REPORTING_USER_REMOVE',
444
- );
445
- if (!isPrivileged) {
446
- throw new ClassError(
447
- 'GroupReportingUser',
448
- 'GroupReportingUserErrMsg06',
449
- 'Insufficient privileges to remove a user from the group',
450
- 'removeGroupReportingUser',
451
- 403,
452
- );
453
- }
454
-
455
- // Part 2: Find User
456
- // Call GroupReportingUser.init(dbTransaction, groupReportingUserId) to check if the user exists.
457
- const groupReportingUser = await GroupReportingUser.init(
458
- dbTransaction,
459
- groupReportingUserId.toString(),
460
- );
461
-
462
- // Part 3: Remove User
463
- // Call GroupReportingUser._Repo.destroy({ where: { GroupReportingUserId: groupReportingUserId }, transaction: dbTransaction }) to remove the user from the database.
464
- await GroupReportingUser._Repo.destroy(
465
- groupReportingUserId,
466
- dbTransaction,
467
- );
468
-
469
- // Part 4: Record Create Activity
470
- // Initialise EntityValueBefore variable and set it to the GroupReportingUser instance before destruction.
471
- const entityValueBefore = {
472
- GroupReportingUserId: groupReportingUser.GroupReportingUserId,
473
- GroupCode: groupReportingUser.GroupCode,
474
- UserId: groupReportingUser.UserId,
475
- Rank: groupReportingUser.Rank,
476
- Status: groupReportingUser.Status,
477
- CreatedById: groupReportingUser.CreatedById,
478
- CreatedAt: groupReportingUser.CreatedAt,
479
- UpdatedById: groupReportingUser.UpdatedById,
480
- UpdatedAt: groupReportingUser.UpdatedAt,
481
- };
482
- // Instantiate a new activity from the Activity class, and set:
483
- const activity = new Activity();
484
- // ActivityId: activity.createId()
485
- // Action: ActionEnum.Delete
486
- // Description: Remove Group Reporting User
487
- // EntityType: GroupReportingUser
488
- // EntityId: groupReportingUserId
489
- // EntityValueBefore: Stringified representation of the GroupReportingUser instance before destroy
490
- // EntityValueAfter: Stringified empty object ({})
491
- activity.ActivityId = activity.createId();
492
- activity.Action = ActionEnum.DELETE;
493
- activity.Description = 'Remove Group Reporting User';
494
- activity.EntityType = 'GroupReportingUser';
495
- activity.EntityId = groupReportingUserId.toString();
496
- activity.EntityValueBefore = JSON.stringify(entityValueBefore);
497
- activity.EntityValueAfter = JSON.stringify({});
498
- // Call the activity.create() method by passing:
499
- // dbTransaction
500
- // userId: loginUser.UserId
501
- await activity.create(loginUser.ObjectId, dbTransaction);
502
- } catch (error) {
503
- throw error;
504
- }
505
- }
506
- }
1
+ import { Op, Transaction } from 'sequelize';
2
+ import { ClassError, ObjectBase } from '@tomei/general';
3
+ import { GroupReportingUserRepository } from './group-reporting-user.repository';
4
+ import { IGroupReportingUserAttr } from '../../interfaces/group-reporting-user.interface';
5
+ import { User } from '../login-user/user';
6
+ import UserModel from '../../models/user.entity';
7
+ import { Group } from '../group/group';
8
+ import { ApplicationConfig } from '@tomei/config';
9
+ import { ActionEnum, Activity } from '@tomei/activity-history';
10
+
11
+ export class GroupReportingUser extends ObjectBase {
12
+ ObjectId: string;
13
+ ObjectName: string;
14
+ TableName = 'sso_GroupReportingUser';
15
+ ObjectType = 'GroupReportingUser';
16
+
17
+ GroupReportingUserId: number;
18
+ GroupCode: string;
19
+ UserId: number;
20
+ Rank: number;
21
+ Status: string;
22
+ private _CreatedById: number;
23
+ private _CreatedAt: Date;
24
+ private _UpdatedById: number;
25
+ private _UpdatedAt: Date;
26
+ private static _Repo = new GroupReportingUserRepository();
27
+
28
+ get CreatedById(): number {
29
+ return this._CreatedById;
30
+ }
31
+
32
+ get CreatedAt(): Date {
33
+ return this._CreatedAt;
34
+ }
35
+
36
+ get UpdatedById(): number {
37
+ return this._UpdatedById;
38
+ }
39
+
40
+ get UpdatedAt(): Date {
41
+ return this._UpdatedAt;
42
+ }
43
+
44
+ private constructor(groupReportingUserAttr?: IGroupReportingUserAttr) {
45
+ super();
46
+ if (groupReportingUserAttr) {
47
+ this.GroupReportingUserId = groupReportingUserAttr.GroupReportingUserId;
48
+ this.GroupCode = groupReportingUserAttr.GroupCode;
49
+ this.UserId = groupReportingUserAttr?.UserId;
50
+ this.Rank = groupReportingUserAttr?.Rank;
51
+ this.Status = groupReportingUserAttr?.Status;
52
+ this._CreatedById = groupReportingUserAttr.CreatedById;
53
+ this._CreatedAt = groupReportingUserAttr.CreatedAt;
54
+ this._UpdatedById = groupReportingUserAttr.UpdatedById;
55
+ this._UpdatedAt = groupReportingUserAttr.UpdatedAt;
56
+ }
57
+ }
58
+
59
+ public static async init(dbTransaction: any, GroupReportingUserId?: string) {
60
+ try {
61
+ if (GroupReportingUserId) {
62
+ const groupReportingUser = await GroupReportingUser._Repo.findByPk(
63
+ GroupReportingUserId,
64
+ {
65
+ transaction: dbTransaction,
66
+ },
67
+ );
68
+ if (groupReportingUser) {
69
+ return new GroupReportingUser(groupReportingUser);
70
+ } else {
71
+ throw new ClassError(
72
+ 'GroupReportingUser',
73
+ 'GroupReportingUserErrMsg00',
74
+ 'GroupReportingUser Not Found',
75
+ 'init',
76
+ 404,
77
+ );
78
+ }
79
+ }
80
+ return new GroupReportingUser();
81
+ } catch (error) {
82
+ throw error;
83
+ }
84
+ }
85
+
86
+ async createGroupReportingUser(
87
+ loginUser: User, //The user performing the operation(typically the logged -in user).
88
+ dbTransaction: any, //Database transaction object to ensure the operation is atomic.
89
+ groupCode: string, //The code of the group to which the user is being assigned.
90
+ userId: number, //The ID of the user to be added to the group.
91
+ rank: number, //The rank to be assigned to the user in the group.
92
+ status: 'Active' | 'Inactive', //The initial status of the user in the group.
93
+ ): Promise<GroupReportingUser> {
94
+ // Returns a GroupReportingUser instance representing the newly created record.
95
+ try {
96
+ //Creates a new group reporting user entry in the sso_GroupReportingUser table.
97
+
98
+ // Validate Input Parameters
99
+ // Ensure groupCode exists in the sso_Group table by calling the Group.init() method.
100
+ const group = await Group.init(dbTransaction, groupCode);
101
+ // Ensure userId exists in the sso_User table by calling the User.init() method.
102
+ const user = await User.init(dbTransaction, userId);
103
+ // Privilege Checking
104
+ // Call the loginUser.checkPrivileges() method by passing:
105
+ // SystemCode: Retrieve from app config.
106
+ // PrivilegeCode: "GROUP_REPORTING_USER_CREATE".
107
+
108
+ const systemCode =
109
+ ApplicationConfig.getComponentConfigValue('system-code');
110
+ const isPrivileged = await loginUser.checkPrivileges(
111
+ systemCode,
112
+ 'GROUP_REPORTING_USER_CREATE',
113
+ );
114
+ if (!isPrivileged) {
115
+ throw new ClassError(
116
+ 'GroupReportingUser',
117
+ 'GroupReportingUserErrMsg02',
118
+ 'Insufficient privileges to add a user to the group',
119
+ );
120
+ }
121
+
122
+ // Check for Duplicate User in Group
123
+ // Query the sso_GroupReportingUser table to see if the userId already exists in the specified groupCode.
124
+ const groupReportingUser = await GroupReportingUser._Repo.findOne({
125
+ where: {
126
+ GroupCode: groupCode,
127
+ UserId: userId,
128
+ },
129
+ transaction: dbTransaction,
130
+ });
131
+ // If the user already exists in the group, throw an error indicating the user is already part of the group.
132
+ if (groupReportingUser) {
133
+ throw new ClassError(
134
+ 'GroupReportingUser',
135
+ 'GroupReportingUserErrMsg03',
136
+ 'User already exists in the group',
137
+ 'createGroupReportingUser',
138
+ );
139
+ }
140
+ //Query the sso_GroupReportingUser table to see if the rank already exists in the specified groupCode.
141
+ //If the rank already exists in the group, throw an error indicating the rank is already in of the group.
142
+ const groupReportingUserRank = await GroupReportingUser._Repo.findOne({
143
+ where: {
144
+ GroupCode: groupCode,
145
+ Rank: rank,
146
+ },
147
+ transaction: dbTransaction,
148
+ });
149
+ if (groupReportingUserRank) {
150
+ throw new ClassError(
151
+ 'GroupReportingUser',
152
+ 'GroupReportingUserErrMsg04',
153
+ 'Rank already exists in the group',
154
+ 'createGroupReportingUser',
155
+ );
156
+ }
157
+
158
+ // Create GroupReportingUser Entry
159
+ // If validation and privilege checks pass, insert a new record in the sso_GroupReportingUser table with the provided groupCode, userId, rank, status, and loginUser.UserId.Automatically capture the current timestamp for CreatedAt.
160
+ this.GroupCode = groupCode;
161
+ this.UserId = userId;
162
+ this.Rank = rank;
163
+ this.Status = status;
164
+ this._CreatedById = loginUser.UserId;
165
+ this._CreatedAt = new Date();
166
+ this._UpdatedAt = new Date();
167
+ this._UpdatedById = loginUser.UserId;
168
+
169
+ const entityValueAfter: any = {
170
+ GroupCode: groupCode,
171
+ UserId: userId,
172
+ Rank: rank,
173
+ Status: status,
174
+ CreatedById: loginUser.UserId,
175
+ CreatedAt: this._CreatedAt,
176
+ UpdatedById: loginUser.UserId,
177
+ UpdatedAt: this._UpdatedAt,
178
+ };
179
+
180
+ const newGroupReportingUser = await GroupReportingUser._Repo.create(
181
+ entityValueAfter,
182
+ {
183
+ transaction: dbTransaction,
184
+ },
185
+ );
186
+
187
+ entityValueAfter.GroupReportingUserId =
188
+ newGroupReportingUser.GroupReportingUserId;
189
+
190
+ // Record Create Activity
191
+ // Instantiate a new activity from the Activity class, and set:\
192
+ // ActivityId: activity.createId()
193
+ // Action: ActionEnum.Create
194
+ // Description: Create Group Reporting User
195
+ // EntityType: GroupReportingUser
196
+ // EntityId: newGroupReportingUser.GroupReportingUserId
197
+ // EntityValueBefore: Stringified empty object({})
198
+ // EntityValueAfter: EntityValueAfter(stringified representation of the newly created entity)
199
+ const activity = new Activity();
200
+ activity.ActivityId = activity.createId();
201
+ activity.Action = ActionEnum.CREATE;
202
+ activity.Description = 'Create Group Reporting User';
203
+ activity.EntityType = 'GroupReportingUser';
204
+ activity.EntityId = newGroupReportingUser.GroupReportingUserId.toString();
205
+ activity.EntityValueBefore = JSON.stringify({});
206
+ activity.EntityValueAfter = JSON.stringify(entityValueAfter);
207
+ // Call the activity create() method by passing:
208
+ // dbTransaction
209
+ // userId: loginUser.UserId
210
+ await activity.create(loginUser.ObjectId, dbTransaction);
211
+ // Return the Created GroupReportingUser
212
+ // Return the newly created GroupReportingUser instance, including all the relevant details like GroupReportingUserId, groupCode, userId, rank, status, and timestamps for CreatedAt.
213
+ return this;
214
+ } catch (error) {
215
+ throw error;
216
+ }
217
+ }
218
+
219
+ async updateGroupReportingUser(
220
+ loginUser: User, //The user performing the operation(typically the logged -in user).
221
+ dbTransaction: any, //Database transaction object to ensure the operation is atomic.
222
+ groupCode: string, //The code of the group to which the user is being assigned.
223
+ userId: number, //The ID of the user to be added to the group.
224
+ rank: number, //The rank to be assigned to the user in the group.
225
+ status: 'Active' | 'Inactive', //The initial status of the user in the group.
226
+ ): Promise<GroupReportingUser> {
227
+ // Returns a GroupReportingUser instance representing the updated record.
228
+ try {
229
+ //Update a group reporting user entry in the sso_GroupReportingUser table.
230
+
231
+ // Validate Input Parameters
232
+ // Ensure groupCode exists in the sso_Group table by calling the Group.init() method.
233
+ await Group.init(dbTransaction, groupCode);
234
+ // Ensure userId exists in the sso_User table by calling the User.init() method.
235
+ await User.init(dbTransaction, userId);
236
+ // Privilege Checking
237
+ // Call the loginUser.checkPrivileges() method by passing:
238
+ // SystemCode: Retrieve from app config.
239
+ // PrivilegeCode: "GROUP_REPORTING_USER_UPDATE".
240
+
241
+ const systemCode =
242
+ ApplicationConfig.getComponentConfigValue('system-code');
243
+ const isPrivileged = await loginUser.checkPrivileges(
244
+ systemCode,
245
+ 'GROUP_REPORTING_USER_UPDATE',
246
+ );
247
+ if (!isPrivileged) {
248
+ throw new ClassError(
249
+ 'GroupReportingUser',
250
+ 'GroupReportingUserErrMsg02',
251
+ 'Insufficient privileges to update a user to the group',
252
+ );
253
+ }
254
+
255
+ //Get the current groupReportingUser
256
+ const currentGroupReportingUser = await GroupReportingUser._Repo.findOne({
257
+ where: {
258
+ GroupCode: groupCode,
259
+ GroupReportingUserId: this.GroupReportingUserId,
260
+ },
261
+ transaction: dbTransaction,
262
+ });
263
+
264
+ // Check for Duplicate User in Group
265
+ // Query the sso_GroupReportingUser table to see if the userId already exists in the specified groupCode.
266
+ const groupReportingUser = await GroupReportingUser._Repo.findOne({
267
+ where: {
268
+ GroupCode: groupCode,
269
+ UserId: userId,
270
+ GroupReportingUserId: {
271
+ [Op.ne]: this.GroupReportingUserId,
272
+ },
273
+ },
274
+ transaction: dbTransaction,
275
+ });
276
+ // If the user already exists in the group, throw an error indicating the user is already part of the group.
277
+ if (groupReportingUser) {
278
+ throw new ClassError(
279
+ 'GroupReportingUser',
280
+ 'GroupReportingUserErrMsg03',
281
+ 'User already exists in the group',
282
+ 'updateGroupReportingUser',
283
+ );
284
+ }
285
+ //Query the sso_GroupReportingUser table to see if the rank already exists in the specified groupCode.
286
+ //If the rank already exists in the group, throw an error indicating the rank is already in of the group.
287
+ const groupReportingUserRank = await GroupReportingUser._Repo.findOne({
288
+ where: {
289
+ GroupCode: groupCode,
290
+ Rank: rank,
291
+ GroupReportingUserId: {
292
+ [Op.ne]: this.GroupReportingUserId,
293
+ },
294
+ },
295
+ transaction: dbTransaction,
296
+ });
297
+ if (groupReportingUserRank) {
298
+ throw new ClassError(
299
+ 'GroupReportingUser',
300
+ 'GroupReportingUserErrMsg04',
301
+ 'Rank already exists in the group',
302
+ 'updateGroupReportingUser',
303
+ );
304
+ }
305
+
306
+ // UPDATE GroupReportingUser Entry
307
+ // If validation and privilege checks pass, insert a new record in the sso_GroupReportingUser table with the provided groupCode, userId, rank, status, and loginUser.UserId.Automatically capture the current timestamp for CreatedAt.
308
+ this.GroupCode = groupCode;
309
+ this.UserId = userId;
310
+ this.Rank = rank;
311
+ this.Status = status;
312
+ this._CreatedById = currentGroupReportingUser.CreatedById;
313
+ this._CreatedAt = currentGroupReportingUser.CreatedAt;
314
+ this._UpdatedAt = new Date();
315
+ this._UpdatedById = loginUser.UserId;
316
+
317
+ const entityValueAfter: any = {
318
+ GroupCode: groupCode,
319
+ UserId: userId,
320
+ Rank: rank,
321
+ Status: status,
322
+ CreatedById: currentGroupReportingUser.CreatedById,
323
+ CreatedAt: this._CreatedAt,
324
+ UpdatedById: loginUser.UserId,
325
+ UpdatedAt: this._UpdatedAt,
326
+ };
327
+
328
+ await GroupReportingUser._Repo.update(entityValueAfter, {
329
+ where: {
330
+ GroupReportingUserId: this.GroupReportingUserId,
331
+ },
332
+ transaction: dbTransaction,
333
+ });
334
+
335
+ // Record Update Activity
336
+ // Instantiate a new activity from the Activity class, and set:\
337
+ // ActivityId: activity.createId()
338
+ // Action: ActionEnum.Update
339
+ // Description: Update Group Reporting User
340
+ // EntityType: GroupReportingUser
341
+ // EntityId: newGroupReportingUser.GroupReportingUserId
342
+ // EntityValueBefore: Stringified empty object({})
343
+ // EntityValueAfter: EntityValueAfter(stringified representation of the newly created entity)
344
+ const activity = new Activity();
345
+ activity.ActivityId = activity.createId();
346
+ activity.Action = ActionEnum.UPDATE;
347
+ activity.Description = 'Update Group Reporting User';
348
+ activity.EntityType = 'GroupReportingUser';
349
+ activity.EntityId = this.GroupReportingUserId.toString();
350
+ activity.EntityValueBefore = JSON.stringify({});
351
+ activity.EntityValueAfter = JSON.stringify(entityValueAfter);
352
+ // Call the activity create() method by passing:
353
+ // dbTransaction
354
+ // userId: loginUser.UserId
355
+ await activity.create(loginUser.ObjectId, dbTransaction);
356
+ // Return the Updated GroupReportingUser
357
+ // Return the updated GroupReportingUser instance, including all the relevant details like GroupReportingUserId, groupCode, userId, rank, status, and timestamps for CreatedAt.
358
+ return this;
359
+ } catch (error) {
360
+ throw error;
361
+ }
362
+ }
363
+
364
+ public static async findAllGroupReportingUsers(
365
+ loginUser: User, //The authenticated user requesting the information.
366
+ dbTransaction: any, //The database transaction to be used for this operation.
367
+ groupCode?: string, //The code of the group whose reporting users should be retrieved.
368
+ ) {
369
+ //This public static method retrieves all GroupReportingUser records for a given group from the sso_GroupReportingUser table.
370
+
371
+ try {
372
+ // Part 1: Privilege Check
373
+ // Call loginUser.checkPrivileges() method by passing:
374
+ // - SystemCode: Retrieve from the app config.
375
+ // - PrivilegeCode: GROUP_REPORTING_USER_VIEW.
376
+ // If the user does not have the required privilege, throw a ForbiddenError.
377
+ const systemCode =
378
+ ApplicationConfig.getComponentConfigValue('system-code');
379
+ const isPrivileged = await loginUser.checkPrivileges(
380
+ systemCode,
381
+ 'GROUP_REPORTING_USER_VIEW',
382
+ );
383
+
384
+ if (!isPrivileged) {
385
+ throw new ClassError(
386
+ 'Group',
387
+ 'GroupReportingUserErrMsg05',
388
+ 'You do not have the privilege to view group reporting user',
389
+ );
390
+ }
391
+
392
+ // Part 2: Group Existence Check
393
+ // Call Group.init(dbTransaction, groupCode) to verify the group exists.
394
+ // If the group does not exist, throw a NotFoundError.
395
+ await Group.init(dbTransaction, groupCode);
396
+
397
+ // Part 3: Retrieve Group Reporting Users
398
+ // Call GroupReportingUser._Repo.findAll() to retrieve all users associated with the provided groupCode.
399
+ // The users should be sorted by Rank in ascending order (Rank 1, Rank 2, and so on).
400
+ // Ensure the query is performed within the dbTransaction.
401
+ const result = await GroupReportingUser._Repo.findAll({
402
+ where: {
403
+ GroupCode: groupCode,
404
+ },
405
+ include: [
406
+ {
407
+ model: UserModel,
408
+ as: 'User',
409
+ },
410
+ ],
411
+ order: [
412
+ ['Rank', 'ASC'], // or 'DESC' for descending order
413
+ ],
414
+ transaction: dbTransaction,
415
+ });
416
+
417
+ // Part 4: Return Results
418
+ // Return the array of GroupReportingUser records found.
419
+
420
+ return result;
421
+ } catch (error) {
422
+ // Part 5: Handle Errors
423
+ // Catch and handle any errors during the execution. If an error occurs, ensure the transaction is rolled back.
424
+ throw error;
425
+ }
426
+ }
427
+
428
+ public static async removeGroupReportingUser(
429
+ loginUser: User, //The user performing the operation, used for privilege checking and logging.
430
+ dbTransaction: Transaction, // The database transaction object to ensure the operation's atomicity.
431
+ groupReportingUserId: number, //The ID of the GroupReportingUser to be removed.
432
+ ): Promise<void> {
433
+ // This method removes a GroupReportingUser record from the database.
434
+ try {
435
+ // Part 1: Privilege Checking
436
+ // Call loginUser.checkPrivileges() method by passing:
437
+ // SystemCode: Retrieve from app config.
438
+ // PrivilegeCode: "GROUP_REPORTING_USER_REMOVE".
439
+ const systemCode =
440
+ ApplicationConfig.getComponentConfigValue('system-code');
441
+ const isPrivileged = await loginUser.checkPrivileges(
442
+ systemCode,
443
+ 'GROUP_REPORTING_USER_REMOVE',
444
+ );
445
+ if (!isPrivileged) {
446
+ throw new ClassError(
447
+ 'GroupReportingUser',
448
+ 'GroupReportingUserErrMsg06',
449
+ 'Insufficient privileges to remove a user from the group',
450
+ 'removeGroupReportingUser',
451
+ 403,
452
+ );
453
+ }
454
+
455
+ // Part 2: Find User
456
+ // Call GroupReportingUser.init(dbTransaction, groupReportingUserId) to check if the user exists.
457
+ const groupReportingUser = await GroupReportingUser.init(
458
+ dbTransaction,
459
+ groupReportingUserId.toString(),
460
+ );
461
+
462
+ // Part 3: Remove User
463
+ // Call GroupReportingUser._Repo.destroy({ where: { GroupReportingUserId: groupReportingUserId }, transaction: dbTransaction }) to remove the user from the database.
464
+ await GroupReportingUser._Repo.destroy(
465
+ groupReportingUserId,
466
+ dbTransaction,
467
+ );
468
+
469
+ // Part 4: Record Create Activity
470
+ // Initialise EntityValueBefore variable and set it to the GroupReportingUser instance before destruction.
471
+ const entityValueBefore = {
472
+ GroupReportingUserId: groupReportingUser.GroupReportingUserId,
473
+ GroupCode: groupReportingUser.GroupCode,
474
+ UserId: groupReportingUser.UserId,
475
+ Rank: groupReportingUser.Rank,
476
+ Status: groupReportingUser.Status,
477
+ CreatedById: groupReportingUser.CreatedById,
478
+ CreatedAt: groupReportingUser.CreatedAt,
479
+ UpdatedById: groupReportingUser.UpdatedById,
480
+ UpdatedAt: groupReportingUser.UpdatedAt,
481
+ };
482
+ // Instantiate a new activity from the Activity class, and set:
483
+ const activity = new Activity();
484
+ // ActivityId: activity.createId()
485
+ // Action: ActionEnum.Delete
486
+ // Description: Remove Group Reporting User
487
+ // EntityType: GroupReportingUser
488
+ // EntityId: groupReportingUserId
489
+ // EntityValueBefore: Stringified representation of the GroupReportingUser instance before destroy
490
+ // EntityValueAfter: Stringified empty object ({})
491
+ activity.ActivityId = activity.createId();
492
+ activity.Action = ActionEnum.DELETE;
493
+ activity.Description = 'Remove Group Reporting User';
494
+ activity.EntityType = 'GroupReportingUser';
495
+ activity.EntityId = groupReportingUserId.toString();
496
+ activity.EntityValueBefore = JSON.stringify(entityValueBefore);
497
+ activity.EntityValueAfter = JSON.stringify({});
498
+ // Call the activity.create() method by passing:
499
+ // dbTransaction
500
+ // userId: loginUser.UserId
501
+ await activity.create(loginUser.ObjectId, dbTransaction);
502
+ } catch (error) {
503
+ throw error;
504
+ }
505
+ }
506
+ }