@tomei/sso 0.60.4-staging.1 → 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.
- package/.commitlintrc.json +22 -22
- package/.gitlab-ci.yml +16 -16
- package/.husky/commit-msg +15 -9
- package/.husky/pre-commit +7 -7
- package/.prettierrc +4 -4
- package/Jenkinsfile +57 -57
- package/README.md +23 -23
- package/__tests__/unit/components/group/group.spec.ts +79 -79
- package/__tests__/unit/components/group-object-privilege/group-object-privilege.spec.ts +88 -88
- package/__tests__/unit/components/group-privilege/group-privilege.spec.ts +68 -68
- package/__tests__/unit/components/group-reporting-user/group-reporting-user.spec.ts +66 -66
- package/__tests__/unit/components/group-system-access/group-system-access.spec.ts +83 -83
- package/__tests__/unit/components/login-user/l.spec.ts +746 -746
- package/__tests__/unit/components/login-user/login.spec.ts +1164 -1164
- package/__tests__/unit/components/password-hash/password-hash.service.spec.ts +31 -31
- package/__tests__/unit/components/system/system.spec.ts +254 -254
- package/__tests__/unit/components/system-privilege/system-privilege.spec.ts +83 -83
- package/__tests__/unit/components/user-group/user-group.spec.ts +86 -86
- package/__tests__/unit/components/user-object-privilege/user-object-privilege.spec.ts +78 -78
- package/__tests__/unit/components/user-privilege/user-privilege.spec.ts +72 -72
- package/__tests__/unit/components/user-system-access/user-system-access.spec.ts +89 -89
- package/__tests__/unit/redis-client/redis.service.spec.ts +23 -23
- package/__tests__/unit/session/session.service.spec.ts +47 -47
- package/__tests__/unit/system-privilege/system-privilage.spec.ts +91 -91
- package/coverage/clover.xml +1452 -1452
- package/coverage/coverage-final.json +47 -47
- package/coverage/lcov-report/base.css +224 -224
- package/coverage/lcov-report/block-navigation.js +87 -87
- package/coverage/lcov-report/components/group/group.repository.ts.html +117 -117
- package/coverage/lcov-report/components/group/group.ts.html +327 -327
- package/coverage/lcov-report/components/group/index.html +130 -130
- package/coverage/lcov-report/components/group-object-privilege/group-object-privilege.repository.ts.html +117 -117
- package/coverage/lcov-report/components/group-object-privilege/group-object-privilege.ts.html +321 -321
- package/coverage/lcov-report/components/group-object-privilege/index.html +130 -130
- package/coverage/lcov-report/components/group-privilege/group-privilege.repository.ts.html +117 -117
- package/coverage/lcov-report/components/group-privilege/group-privilege.ts.html +303 -303
- package/coverage/lcov-report/components/group-privilege/index.html +130 -130
- package/coverage/lcov-report/components/group-reporting-user/group-reporting-user.repository.ts.html +117 -117
- package/coverage/lcov-report/components/group-reporting-user/group-reporting-user.ts.html +327 -327
- package/coverage/lcov-report/components/group-reporting-user/index.html +130 -130
- package/coverage/lcov-report/components/group-system-access/group-system-access.repository.ts.html +117 -117
- package/coverage/lcov-report/components/group-system-access/group-system-access.ts.html +309 -309
- package/coverage/lcov-report/components/group-system-access/index.html +130 -130
- package/coverage/lcov-report/components/login-history/index.html +115 -115
- package/coverage/lcov-report/components/login-history/login-history.repository.ts.html +117 -117
- package/coverage/lcov-report/components/login-user/index.html +130 -130
- package/coverage/lcov-report/components/login-user/login-user.ts.html +5007 -5007
- package/coverage/lcov-report/components/login-user/user.repository.ts.html +117 -117
- package/coverage/lcov-report/components/password-hash/index.html +115 -115
- package/coverage/lcov-report/components/password-hash/password-hash.service.ts.html +126 -126
- package/coverage/lcov-report/components/system/index.html +130 -130
- package/coverage/lcov-report/components/system/system.repository.ts.html +117 -117
- package/coverage/lcov-report/components/system/system.ts.html +909 -909
- package/coverage/lcov-report/components/system-privilege/index.html +130 -130
- package/coverage/lcov-report/components/system-privilege/system-privilege.repository.ts.html +120 -120
- package/coverage/lcov-report/components/system-privilege/system-privilege.ts.html +390 -390
- package/coverage/lcov-report/components/user-group/index.html +130 -130
- package/coverage/lcov-report/components/user-group/user-group.repository.ts.html +117 -117
- package/coverage/lcov-report/components/user-group/user-group.ts.html +354 -354
- package/coverage/lcov-report/components/user-object-privilege/index.html +130 -130
- package/coverage/lcov-report/components/user-object-privilege/user-object-privilege.repository.ts.html +117 -117
- package/coverage/lcov-report/components/user-object-privilege/user-object-privilege.ts.html +312 -312
- package/coverage/lcov-report/components/user-privilege/index.html +130 -130
- package/coverage/lcov-report/components/user-privilege/user-privilege.repository.ts.html +117 -117
- package/coverage/lcov-report/components/user-privilege/user-privilege.ts.html +306 -306
- package/coverage/lcov-report/components/user-system-access/index.html +130 -130
- package/coverage/lcov-report/components/user-system-access/user-system-access.repository.ts.html +117 -117
- package/coverage/lcov-report/components/user-system-access/user-system-access.ts.html +312 -312
- package/coverage/lcov-report/enum/group-type.enum.ts.html +108 -108
- package/coverage/lcov-report/enum/index.html +160 -160
- package/coverage/lcov-report/enum/index.ts.html +93 -93
- package/coverage/lcov-report/enum/user-status.enum.ts.html +105 -105
- package/coverage/lcov-report/enum/yn.enum.ts.html +96 -96
- package/coverage/lcov-report/index.html +370 -370
- package/coverage/lcov-report/models/group-object-privilege.entity.ts.html +333 -333
- package/coverage/lcov-report/models/group-privilege.entity.ts.html +315 -315
- package/coverage/lcov-report/models/group-reporting-user.entity.ts.html +339 -339
- package/coverage/lcov-report/models/group-system-access.entity.ts.html +324 -324
- package/coverage/lcov-report/models/group.entity.ts.html +435 -435
- package/coverage/lcov-report/models/index.html +310 -310
- package/coverage/lcov-report/models/login-history.entity.ts.html +252 -252
- package/coverage/lcov-report/models/staff.entity.ts.html +411 -411
- package/coverage/lcov-report/models/system-privilege.entity.ts.html +354 -354
- package/coverage/lcov-report/models/system.entity.ts.html +423 -423
- package/coverage/lcov-report/models/user-group.entity.ts.html +354 -354
- package/coverage/lcov-report/models/user-object-privilege.entity.ts.html +330 -330
- package/coverage/lcov-report/models/user-privilege.entity.ts.html +315 -315
- package/coverage/lcov-report/models/user-system-access.entity.ts.html +315 -315
- package/coverage/lcov-report/models/user.entity.ts.html +522 -522
- package/coverage/lcov-report/prettify.css +1 -1
- package/coverage/lcov-report/prettify.js +2 -2
- package/coverage/lcov-report/redis-client/index.html +115 -115
- package/coverage/lcov-report/redis-client/redis.service.ts.html +240 -240
- package/coverage/lcov-report/session/index.html +115 -115
- package/coverage/lcov-report/session/session.service.ts.html +246 -246
- package/coverage/lcov-report/sorter.js +196 -196
- package/coverage/lcov.info +2490 -2490
- package/coverage/test-report.xml +128 -128
- package/create-sso-user.sql +39 -39
- package/dist/src/components/login-user/user.js +9 -1
- package/dist/src/components/login-user/user.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/eslint.config.mjs +58 -58
- package/jest.config.js +14 -14
- package/migrations/20240314080602-create-user-table.js +124 -124
- package/migrations/20240314080603-create-user-group-table.js +85 -85
- package/migrations/20240314080604-create-user-user-group-table.js +55 -55
- package/migrations/20240314080605-create-login-history-table.js +53 -53
- package/migrations/20240527064925-create-system-table.js +78 -78
- package/migrations/20240527064926-create-system-privilege-table.js +71 -71
- package/migrations/20240527065342-create-group-table.js +93 -93
- package/migrations/20240527065633-create-group-reporting-user-table.js +76 -76
- package/migrations/20240528011551-create-group-system-access-table.js +72 -72
- package/migrations/20240528023018-user-system-access-table.js +75 -75
- package/migrations/20240528032229-user-privilege-table.js +76 -76
- package/migrations/20240528063003-create-group-privilege-table.js +76 -76
- package/migrations/20240528063051-create-group-object-privilege-table.js +84 -84
- package/migrations/20240528063107-create-user-object-privilege-table.js +84 -84
- package/migrations/20240528063108-create-api-key-table.js +85 -85
- package/migrations/20241104104802-create-building-table.js +95 -95
- package/migrations/20250108091132-add-area-manager-user-id-to-building-table.js +14 -14
- package/migrations/20250108091133-add-passcode-to-user-table.js +36 -36
- package/migrations/20250210115636-create-user-reporting-hierarchy.js +76 -76
- package/migrations/20250326043818-crate-user-password-history.js +42 -42
- package/package.json +90 -90
- package/sampledotenv +7 -7
- package/sonar-project.properties +22 -22
- package/src/components/api-key/api-key.repository.ts +15 -15
- package/src/components/api-key/api-key.ts +448 -448
- package/src/components/api-key/index.ts +4 -4
- package/src/components/building/building.repository.ts +27 -27
- package/src/components/building/index.ts +2 -2
- package/src/components/group/group.repository.ts +26 -26
- package/src/components/group/group.ts +2284 -2284
- package/src/components/group/index.ts +3 -3
- package/src/components/group-object-privilege/group-object-privilege.repository.ts +25 -25
- package/src/components/group-object-privilege/group-object-privilege.ts +278 -278
- package/src/components/group-object-privilege/index.ts +2 -2
- package/src/components/group-privilege/group-privilege.repository.ts +29 -29
- package/src/components/group-privilege/group-privilege.ts +84 -84
- package/src/components/group-privilege/index.ts +2 -2
- package/src/components/group-reporting-user/group-reporting-user.repository.ts +23 -23
- package/src/components/group-reporting-user/group-reporting-user.ts +506 -506
- package/src/components/group-reporting-user/index.ts +3 -3
- package/src/components/group-system-access/group-system-access.repository.ts +43 -43
- package/src/components/group-system-access/group-system-access.ts +90 -90
- package/src/components/group-system-access/index.ts +2 -2
- package/src/components/index.ts +20 -20
- package/src/components/login-history/index.ts +1 -1
- package/src/components/login-history/login-history.repository.ts +11 -11
- package/src/components/login-user/index.ts +5 -5
- package/src/components/login-user/interfaces/check-user-info-duplicated.interface.ts +7 -7
- package/src/components/login-user/interfaces/index.ts +1 -1
- package/src/components/login-user/interfaces/system-access.interface.ts +13 -13
- package/src/components/login-user/interfaces/user-info.interface.ts +34 -34
- package/src/components/login-user/login-user.ts +362 -362
- package/src/components/login-user/user.repository.ts +11 -11
- package/src/components/login-user/user.ts +3142 -3134
- package/src/components/password-hash/index.ts +2 -2
- package/src/components/password-hash/interfaces/index.ts +1 -1
- package/src/components/password-hash/interfaces/password-hash-service.interface.ts +4 -4
- package/src/components/password-hash/password-hash.service.ts +14 -14
- package/src/components/staff/index.ts +2 -2
- package/src/components/staff/staff.repository.ts +27 -27
- package/src/components/system/index.ts +3 -3
- package/src/components/system/system.repository.ts +11 -11
- package/src/components/system/system.ts +456 -456
- package/src/components/system-privilege/index.ts +4 -4
- package/src/components/system-privilege/system-privilege.repository.ts +18 -18
- package/src/components/system-privilege/system-privilege.ts +541 -541
- package/src/components/user-group/index.ts +2 -2
- package/src/components/user-group/user-group.repository.ts +19 -19
- package/src/components/user-group/user-group.ts +764 -764
- package/src/components/user-object-privilege/index.ts +2 -2
- package/src/components/user-object-privilege/user-object-privilege.repository.ts +11 -11
- package/src/components/user-object-privilege/user-object-privilege.ts +79 -79
- package/src/components/user-password-history/index.ts +2 -2
- package/src/components/user-password-history/user-password-history.repository.ts +39 -39
- package/src/components/user-password-history/user-password-history.ts +187 -187
- package/src/components/user-privilege/index.ts +2 -2
- package/src/components/user-privilege/user-privilege.repository.ts +25 -25
- package/src/components/user-privilege/user-privilege.ts +662 -662
- package/src/components/user-reporting-hierarchy/index.ts +2 -2
- package/src/components/user-reporting-hierarchy/user-reporting-hierarchy.repository.ts +30 -30
- package/src/components/user-reporting-hierarchy/user-reporting-hierarchy.ts +505 -505
- package/src/components/user-system-access/index.ts +2 -2
- package/src/components/user-system-access/user-system-access.repository.ts +41 -41
- package/src/components/user-system-access/user-system-access.ts +717 -717
- package/src/database.ts +15 -15
- package/src/enum/api-key.enum.ts +5 -5
- package/src/enum/building-type.enum.ts +6 -6
- package/src/enum/group-type.enum.ts +8 -8
- package/src/enum/index.ts +6 -6
- package/src/enum/login-status.enum.ts +4 -4
- package/src/enum/object-status.enum.ts +4 -4
- package/src/enum/user-status.enum.ts +7 -7
- package/src/enum/yn.enum.ts +4 -4
- package/src/index.ts +8 -8
- package/src/interfaces/api-key-attr.interface.ts +16 -16
- package/src/interfaces/group-object-privilege.interface.ts +14 -14
- package/src/interfaces/group-privilege.interface.ts +10 -10
- package/src/interfaces/group-reporting-user.interface.ts +11 -11
- package/src/interfaces/group-search-attr.interface.ts +9 -9
- package/src/interfaces/group-system-access.interface.ts +10 -10
- package/src/interfaces/group.interface.ts +17 -17
- package/src/interfaces/index.ts +13 -13
- package/src/interfaces/system-login.interface.ts +6 -6
- package/src/interfaces/system-privilege-search.interface.ts +5 -5
- package/src/interfaces/system-privilege.interface.ts +11 -11
- package/src/interfaces/system-search-attr.interface.ts +5 -5
- package/src/interfaces/system.interface.ts +15 -15
- package/src/interfaces/user-group.interface.ts +12 -12
- package/src/interfaces/user-object-privilege.interface.ts +14 -14
- package/src/interfaces/user-password-history.interface.ts +6 -6
- package/src/interfaces/user-privilege.interface.ts +10 -10
- package/src/interfaces/user-reporting-hierarchy.interface.ts +11 -11
- package/src/interfaces/user-session.interface.ts +5 -5
- package/src/interfaces/user-system-access.interface.ts +10 -10
- package/src/models/api-key-entity.ts +101 -101
- package/src/models/building.entity.ts +103 -103
- package/src/models/group-object-privilege.entity.ts +91 -91
- package/src/models/group-privilege.entity.ts +78 -78
- package/src/models/group-reporting-user.entity.ts +95 -95
- package/src/models/group-system-access.entity.ts +81 -81
- package/src/models/group.entity.ts +127 -127
- package/src/models/login-history.entity.ts +63 -63
- package/src/models/staff.entity.ts +91 -91
- package/src/models/system-privilege.entity.ts +90 -90
- package/src/models/system.entity.ts +113 -113
- package/src/models/user-group.entity.ts +91 -91
- package/src/models/user-object-privilege.entity.ts +90 -90
- package/src/models/user-password-history.ts +51 -51
- package/src/models/user-privilege.entity.ts +78 -78
- package/src/models/user-reporting-hierarchy.entity.ts +102 -102
- package/src/models/user-system-access.entity.ts +87 -87
- package/src/models/user.entity.ts +193 -193
- package/src/redis-client/__mocks__/jest-initial-setup.ts +2 -2
- package/src/redis-client/__mocks__/redis-mock.ts +28 -28
- package/src/redis-client/index.ts +1 -1
- package/src/redis-client/redis.service.ts +75 -75
- package/src/session/index.ts +2 -2
- package/src/session/interfaces/index.ts +1 -1
- package/src/session/interfaces/session-service.interface.ts +26 -26
- package/src/session/session.service.ts +96 -96
- package/src/types/auth-context.ts +10 -10
- package/src/types/index.ts +1 -1
- package/tsconfig.build.json +5 -5
- package/tsconfig.json +23 -23
@@ -1,717 +1,717 @@
|
|
1
|
-
import { ClassError, ObjectBase } from '@tomei/general';
|
2
|
-
import { UserSystemAccessRepository } from './user-system-access.repository';
|
3
|
-
import { IUserSystemAccess } from '../../interfaces/user-system-access.interface';
|
4
|
-
import { User } from '../login-user/user';
|
5
|
-
import { ApplicationConfig } from '@tomei/config';
|
6
|
-
import SystemModel from '../../models/system.entity';
|
7
|
-
import SystemPrivilegeModel from '../../models/system-privilege.entity';
|
8
|
-
import UserSystemAccessModel from '../../models/user-system-access.entity';
|
9
|
-
import GroupModel from '../../models/group.entity';
|
10
|
-
import GroupSystemAccessModel from '../../models/group-system-access.entity';
|
11
|
-
import UserModel from '../../models/user.entity';
|
12
|
-
import { ActionEnum, Activity } from '@tomei/activity-history';
|
13
|
-
import { Op } from 'sequelize';
|
14
|
-
import { UserPrivilegeRepository } from '../user-privilege/user-privilege.repository';
|
15
|
-
import UserGroupModel from 'models/user-group.entity';
|
16
|
-
import { UserStatus } from '../../enum';
|
17
|
-
|
18
|
-
export class UserSystemAccess extends ObjectBase {
|
19
|
-
ObjectType = 'UserSystemAccess';
|
20
|
-
TableName = 'sso_UserSystemAccess';
|
21
|
-
ObjectName: string;
|
22
|
-
ObjectId: string;
|
23
|
-
UserSystemAccessId: number;
|
24
|
-
UserId: number;
|
25
|
-
SystemCode: string;
|
26
|
-
Status: string;
|
27
|
-
private _CreatedAt: Date;
|
28
|
-
private _UpdatedAt: Date;
|
29
|
-
private _CreatedById: number;
|
30
|
-
private _UpdatedById: number;
|
31
|
-
|
32
|
-
get CreatedAt() {
|
33
|
-
return this._CreatedAt;
|
34
|
-
}
|
35
|
-
|
36
|
-
get UpdatedAt() {
|
37
|
-
return this._UpdatedAt;
|
38
|
-
}
|
39
|
-
|
40
|
-
get CreatedById() {
|
41
|
-
return this._CreatedById;
|
42
|
-
}
|
43
|
-
|
44
|
-
get UpdatedById() {
|
45
|
-
return this._UpdatedById;
|
46
|
-
}
|
47
|
-
|
48
|
-
private static _Repository = new UserSystemAccessRepository();
|
49
|
-
private static _UserPrivilegeRepo = new UserPrivilegeRepository();
|
50
|
-
|
51
|
-
private constructor(userSystemAccessAttr?: IUserSystemAccess) {
|
52
|
-
super();
|
53
|
-
if (userSystemAccessAttr) {
|
54
|
-
this.UserSystemAccessId = userSystemAccessAttr.UserSystemAccessId;
|
55
|
-
this.UserId = userSystemAccessAttr.UserId;
|
56
|
-
this.SystemCode = userSystemAccessAttr.SystemCode;
|
57
|
-
this.Status = userSystemAccessAttr.Status;
|
58
|
-
this._CreatedById = userSystemAccessAttr.CreatedById;
|
59
|
-
this._CreatedAt = userSystemAccessAttr.CreatedAt;
|
60
|
-
this._UpdatedById = userSystemAccessAttr.UpdatedById;
|
61
|
-
this._UpdatedAt = userSystemAccessAttr.UpdatedAt;
|
62
|
-
}
|
63
|
-
}
|
64
|
-
|
65
|
-
static async init(dbTransaction: any, UserSystemAccessId?: number) {
|
66
|
-
try {
|
67
|
-
const userSystemAccess = new UserSystemAccess();
|
68
|
-
if (UserSystemAccessId) {
|
69
|
-
const userSystemAccessAttr = await this._Repository.findOne({
|
70
|
-
where: { UserSystemAccessId },
|
71
|
-
transaction: dbTransaction,
|
72
|
-
});
|
73
|
-
if (userSystemAccessAttr) {
|
74
|
-
return new UserSystemAccess(
|
75
|
-
userSystemAccessAttr.get({ plain: true }),
|
76
|
-
);
|
77
|
-
} else {
|
78
|
-
throw new ClassError(
|
79
|
-
'UserSystemAccess',
|
80
|
-
'UserSystemAccessErrMsg00',
|
81
|
-
'UserSystemAccess not found',
|
82
|
-
);
|
83
|
-
}
|
84
|
-
}
|
85
|
-
return userSystemAccess;
|
86
|
-
} catch (error) {
|
87
|
-
throw error;
|
88
|
-
}
|
89
|
-
}
|
90
|
-
|
91
|
-
public static async findAll(
|
92
|
-
loginUser: User, //The currently logged-in user initiating the request.
|
93
|
-
dbTransaction: any, //The active database transaction to ensure consistency during the query.
|
94
|
-
whereOption: {
|
95
|
-
//An object containing filter criteria, specifically:
|
96
|
-
UserId: number; //The ID of the user whose system access records are to be retrieved.
|
97
|
-
SystemCode?: string;
|
98
|
-
},
|
99
|
-
pagination: {
|
100
|
-
//An object containing pagination parameters:
|
101
|
-
page: number; //The current page number to retrieve.
|
102
|
-
limit: number; //The number of records to retrieve per page.
|
103
|
-
},
|
104
|
-
): Promise<{
|
105
|
-
records: {
|
106
|
-
SystemName: string;
|
107
|
-
SystemCode: string;
|
108
|
-
Status: string;
|
109
|
-
CreatedBy: string;
|
110
|
-
CreatedAt: Date;
|
111
|
-
UpdatedBy: string;
|
112
|
-
UpdatedAt: Date;
|
113
|
-
}[];
|
114
|
-
pagination: {
|
115
|
-
currentPage: number;
|
116
|
-
pageSize: number;
|
117
|
-
totalRecords: number;
|
118
|
-
};
|
119
|
-
}> {
|
120
|
-
try {
|
121
|
-
// Privilege Checking:
|
122
|
-
// Call loginUser.checkPrivileges() method by passing:
|
123
|
-
// SystemCode: Retrieve from app config.
|
124
|
-
// PrivilegeCode: 'USER_SYSTEM_ACCESS_LIST'.
|
125
|
-
const systemCode =
|
126
|
-
ApplicationConfig.getComponentConfigValue('system-code');
|
127
|
-
const privilegeCode = 'USER_SYSTEM_ACCESS_LIST';
|
128
|
-
const isPrivileged = await loginUser.checkPrivileges(
|
129
|
-
systemCode,
|
130
|
-
privilegeCode,
|
131
|
-
);
|
132
|
-
if (!isPrivileged) {
|
133
|
-
throw new ClassError(
|
134
|
-
'UserSystemAccess',
|
135
|
-
'UserSystemAccessErrMsg01',
|
136
|
-
'You do not have permission to access this resource.',
|
137
|
-
);
|
138
|
-
}
|
139
|
-
// Create a where condition using whereOption to filter by UserId.
|
140
|
-
// Set up pagination logic using the pagination parameter:
|
141
|
-
// Calculate offset based on page and limit.
|
142
|
-
const options: any = {
|
143
|
-
distinct: true,
|
144
|
-
where: {
|
145
|
-
UserId: whereOption.UserId,
|
146
|
-
},
|
147
|
-
offset: (pagination.page - 1) * pagination.limit,
|
148
|
-
limit: pagination.limit,
|
149
|
-
transaction: dbTransaction,
|
150
|
-
include: [
|
151
|
-
{
|
152
|
-
model: SystemModel,
|
153
|
-
attributes: ['Name', 'SystemCode'],
|
154
|
-
},
|
155
|
-
{
|
156
|
-
model: UserModel,
|
157
|
-
as: 'CreatedBy',
|
158
|
-
attributes: ['FullName'],
|
159
|
-
},
|
160
|
-
{
|
161
|
-
model: UserModel,
|
162
|
-
as: 'UpdatedBy',
|
163
|
-
attributes: ['FullName'],
|
164
|
-
},
|
165
|
-
],
|
166
|
-
};
|
167
|
-
const userSystemAccesses =
|
168
|
-
await this._Repository.findAllWithPagination(options);
|
169
|
-
return {
|
170
|
-
records: userSystemAccesses.rows.map((userSystemAccess) => {
|
171
|
-
return {
|
172
|
-
UserSystemAccessId: userSystemAccess.UserSystemAccessId,
|
173
|
-
SystemName: userSystemAccess.System.Name,
|
174
|
-
SystemCode: userSystemAccess.System.SystemCode,
|
175
|
-
Status: userSystemAccess.Status,
|
176
|
-
CreatedBy: userSystemAccess.CreatedBy.FullName,
|
177
|
-
CreatedAt: userSystemAccess.CreatedAt,
|
178
|
-
UpdatedBy: userSystemAccess.UpdatedBy.FullName,
|
179
|
-
UpdatedAt: userSystemAccess.UpdatedAt,
|
180
|
-
};
|
181
|
-
}),
|
182
|
-
pagination: {
|
183
|
-
currentPage: pagination.page,
|
184
|
-
pageSize: pagination.limit,
|
185
|
-
totalRecords: userSystemAccesses.count,
|
186
|
-
},
|
187
|
-
};
|
188
|
-
} catch (error) {
|
189
|
-
throw error;
|
190
|
-
}
|
191
|
-
}
|
192
|
-
|
193
|
-
public static async findAllUsers(
|
194
|
-
loginUser: User, //The currently logged-in user initiating the request.
|
195
|
-
dbTransaction: any, //The active database transaction to ensure consistency during the query.
|
196
|
-
SystemCode: string,
|
197
|
-
Page: number,
|
198
|
-
Rows: number,
|
199
|
-
Search: {
|
200
|
-
UserId?: string | number;
|
201
|
-
Status?: string;
|
202
|
-
},
|
203
|
-
) {
|
204
|
-
// Part 1: Privilege Checking
|
205
|
-
const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
|
206
|
-
const isPrivileged = await loginUser.checkPrivileges(
|
207
|
-
systemCode,
|
208
|
-
'USER_SYSTEM_ACCESS_LIST',
|
209
|
-
);
|
210
|
-
|
211
|
-
if (!isPrivileged) {
|
212
|
-
throw new ClassError(
|
213
|
-
'UserSystemAccessUser',
|
214
|
-
'UserSystemAccessUserErrMsg01',
|
215
|
-
'You do not have permission to view system access users.',
|
216
|
-
);
|
217
|
-
}
|
218
|
-
|
219
|
-
try {
|
220
|
-
// Part 2: Retrieve System Access Users and returns
|
221
|
-
const queryObj: any = { SystemCode: SystemCode };
|
222
|
-
|
223
|
-
if (Search) {
|
224
|
-
Object.entries(Search).forEach(([key, value]) => {
|
225
|
-
queryObj[key] = value;
|
226
|
-
});
|
227
|
-
}
|
228
|
-
|
229
|
-
let options: any = {
|
230
|
-
where: queryObj,
|
231
|
-
distinct: true,
|
232
|
-
transaction: dbTransaction,
|
233
|
-
};
|
234
|
-
|
235
|
-
if (Page && Rows) {
|
236
|
-
options = {
|
237
|
-
...options,
|
238
|
-
limit: Rows,
|
239
|
-
offset: Rows * (Page - 1),
|
240
|
-
order: [['CreatedAt', 'DESC']],
|
241
|
-
include: [
|
242
|
-
{
|
243
|
-
model: SystemModel,
|
244
|
-
attributes: ['Name', 'SystemCode'],
|
245
|
-
},
|
246
|
-
{
|
247
|
-
model: UserModel,
|
248
|
-
where: {
|
249
|
-
Status: UserStatus.ACTIVE,
|
250
|
-
},
|
251
|
-
as: 'User',
|
252
|
-
attributes: ['UserId', 'FullName'],
|
253
|
-
},
|
254
|
-
],
|
255
|
-
};
|
256
|
-
}
|
257
|
-
|
258
|
-
const userSystemAccesses =
|
259
|
-
await this._Repository.findAndCountAll(options);
|
260
|
-
return userSystemAccesses;
|
261
|
-
} catch (error) {
|
262
|
-
throw error;
|
263
|
-
}
|
264
|
-
}
|
265
|
-
|
266
|
-
public static async findAllUserPrivileges(
|
267
|
-
loginUser: User, //The currently logged-in user initiating the request.
|
268
|
-
dbTransaction: any, //The active database transaction to ensure consistency during the query.
|
269
|
-
SystemCode: string,
|
270
|
-
search?: {
|
271
|
-
UserId?: string[];
|
272
|
-
Status?: string;
|
273
|
-
},
|
274
|
-
) {
|
275
|
-
// Part 1: Privilege Checking
|
276
|
-
const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
|
277
|
-
const isPrivileged = await loginUser.checkPrivileges(
|
278
|
-
systemCode,
|
279
|
-
'USER_SYSTEM_ACCESS_LIST',
|
280
|
-
);
|
281
|
-
|
282
|
-
if (!isPrivileged) {
|
283
|
-
throw new ClassError(
|
284
|
-
'UserSystemAccessUser',
|
285
|
-
'UserSystemAccessUserErrMsg01',
|
286
|
-
'You do not have permission to view system access users.',
|
287
|
-
);
|
288
|
-
}
|
289
|
-
|
290
|
-
try {
|
291
|
-
//Part 2: Retrieve User System Access Based on Privileges
|
292
|
-
let systemWhere: any = {};
|
293
|
-
|
294
|
-
if (SystemCode) {
|
295
|
-
systemWhere = {
|
296
|
-
SystemCode: {
|
297
|
-
[Op.substring]: SystemCode,
|
298
|
-
},
|
299
|
-
};
|
300
|
-
}
|
301
|
-
|
302
|
-
const allSystemAccessUsers = await UserSystemAccessModel.findAll({
|
303
|
-
include: [
|
304
|
-
{
|
305
|
-
model: SystemModel,
|
306
|
-
where: systemWhere,
|
307
|
-
},
|
308
|
-
{
|
309
|
-
model: UserModel,
|
310
|
-
as: 'User',
|
311
|
-
attributes: ['UserId', 'FullName'],
|
312
|
-
},
|
313
|
-
],
|
314
|
-
transaction: dbTransaction,
|
315
|
-
});
|
316
|
-
|
317
|
-
const allPrivileges = await SystemPrivilegeModel.findAll({
|
318
|
-
where: systemWhere,
|
319
|
-
transaction: dbTransaction,
|
320
|
-
});
|
321
|
-
|
322
|
-
const systemAccessUserPrivileges = allPrivileges.map(
|
323
|
-
async (privilege) => {
|
324
|
-
const filteredUsers = allSystemAccessUsers
|
325
|
-
.map((userAccess) => userAccess.User)
|
326
|
-
.filter((user) => search.UserId.includes(String(user.UserId)));
|
327
|
-
|
328
|
-
return {
|
329
|
-
...privilege.get({ plain: true }),
|
330
|
-
Users: filteredUsers,
|
331
|
-
};
|
332
|
-
},
|
333
|
-
);
|
334
|
-
|
335
|
-
return systemAccessUserPrivileges;
|
336
|
-
} catch (error) {
|
337
|
-
throw error;
|
338
|
-
}
|
339
|
-
}
|
340
|
-
|
341
|
-
public static async findAllUserRoles(
|
342
|
-
loginUser: User, //The currently logged-in user initiating the request.
|
343
|
-
dbTransaction: any, //The active database transaction to ensure consistency during the query.
|
344
|
-
SystemCode: string,
|
345
|
-
search?: {
|
346
|
-
UserId?: string[];
|
347
|
-
Status?: string;
|
348
|
-
},
|
349
|
-
) {
|
350
|
-
// Part 1: Privilege Checking
|
351
|
-
const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
|
352
|
-
const isPrivileged = await loginUser.checkPrivileges(
|
353
|
-
systemCode,
|
354
|
-
'USER_SYSTEM_ACCESS_LIST',
|
355
|
-
);
|
356
|
-
|
357
|
-
if (!isPrivileged) {
|
358
|
-
throw new ClassError(
|
359
|
-
'UserSystemAccessUser',
|
360
|
-
'UserSystemAccessUserErrMsg01',
|
361
|
-
'You do not have permission to view system access users.',
|
362
|
-
);
|
363
|
-
}
|
364
|
-
|
365
|
-
try {
|
366
|
-
//Part 2: Retrieve User System Access Based on Privileges
|
367
|
-
let systemWhere: any = {};
|
368
|
-
|
369
|
-
if (SystemCode) {
|
370
|
-
systemWhere = {
|
371
|
-
SystemCode: {
|
372
|
-
[Op.substring]: SystemCode,
|
373
|
-
},
|
374
|
-
};
|
375
|
-
}
|
376
|
-
|
377
|
-
const allGroupSystemAccess = await GroupSystemAccessModel.findAll({
|
378
|
-
where: systemWhere,
|
379
|
-
include: [
|
380
|
-
{
|
381
|
-
model: GroupModel,
|
382
|
-
where: {
|
383
|
-
Type: 'Role',
|
384
|
-
},
|
385
|
-
},
|
386
|
-
],
|
387
|
-
transaction: dbTransaction,
|
388
|
-
});
|
389
|
-
|
390
|
-
const allSystemAccessUsers = await UserSystemAccessModel.findAll({
|
391
|
-
include: [
|
392
|
-
{
|
393
|
-
model: SystemModel,
|
394
|
-
where: systemWhere,
|
395
|
-
},
|
396
|
-
{
|
397
|
-
model: UserModel,
|
398
|
-
as: 'User',
|
399
|
-
attributes: ['UserId', 'FullName'],
|
400
|
-
},
|
401
|
-
],
|
402
|
-
transaction: dbTransaction,
|
403
|
-
});
|
404
|
-
|
405
|
-
const systemAccessUserRoles = allGroupSystemAccess.map(
|
406
|
-
(groupSystemAccess) => {
|
407
|
-
const filteredUsers = allSystemAccessUsers
|
408
|
-
.map((userAccess) => userAccess.User)
|
409
|
-
.filter((user) => search.UserId.includes(String(user.UserId)));
|
410
|
-
|
411
|
-
return {
|
412
|
-
...groupSystemAccess.Group.get({ plain: true }),
|
413
|
-
Users: filteredUsers,
|
414
|
-
};
|
415
|
-
},
|
416
|
-
);
|
417
|
-
|
418
|
-
return systemAccessUserRoles;
|
419
|
-
} catch (error) {
|
420
|
-
throw error;
|
421
|
-
}
|
422
|
-
}
|
423
|
-
|
424
|
-
public static async createAccess(
|
425
|
-
loginUser: User, //The currently logged-in user initiating the request.
|
426
|
-
dbTransaction: any, //The active database transaction to ensure consistency during the query.
|
427
|
-
UserId: string, //The user ID for whom system access is being created.
|
428
|
-
SystemCode: string, //The system code for which access is being granted.
|
429
|
-
Status: string, //The status of access ('Active' or 'Inactive').
|
430
|
-
) {
|
431
|
-
try {
|
432
|
-
// Part 1: Privilege Checking:
|
433
|
-
// Call loginUser.checkPrivileges() method by passing:
|
434
|
-
// SystemCode: Retrieve from app config.
|
435
|
-
// PrivilegeCode: 'USER_SYSTEM_ACCESS_CREATE'.
|
436
|
-
const systemCode =
|
437
|
-
ApplicationConfig.getComponentConfigValue('system-code');
|
438
|
-
const privilegeCode = 'USER_SYSTEM_ACCESS_CREATE';
|
439
|
-
const isPrivileged = await loginUser.checkPrivileges(
|
440
|
-
systemCode,
|
441
|
-
privilegeCode,
|
442
|
-
);
|
443
|
-
if (!isPrivileged) {
|
444
|
-
throw new ClassError(
|
445
|
-
'UserSystemAccess',
|
446
|
-
'UserSystemAccessErrMsg01',
|
447
|
-
'You do not have permission to access this resource.',
|
448
|
-
);
|
449
|
-
}
|
450
|
-
// Part 2: Validation for Existing Access
|
451
|
-
// Use the UserSystemAccess.findAll() method to check if the user already has access to the specified system:
|
452
|
-
// Pass the following parameters:
|
453
|
-
// loginUser
|
454
|
-
// dbTransaction
|
455
|
-
// whereOption: set to UserId = UserId and SystemCode = SystemCode.
|
456
|
-
// If a record is found, throw an error indicating that access for this user and system already exists.
|
457
|
-
|
458
|
-
const isExist = await UserSystemAccess._Repository.findAll({
|
459
|
-
where: { [Op.and]: [{ UserId: UserId }, { SystemCode: SystemCode }] },
|
460
|
-
transaction: dbTransaction,
|
461
|
-
});
|
462
|
-
|
463
|
-
if (isExist?.length > 0) {
|
464
|
-
throw new ClassError(
|
465
|
-
'UserSystemAccess',
|
466
|
-
'UserSystemAccessErrMsg01',
|
467
|
-
'User already have access to this system',
|
468
|
-
);
|
469
|
-
}
|
470
|
-
|
471
|
-
// Part 3: Insert System Access Record
|
472
|
-
// After successful validation, create a new instance of UserSystemAccess with the following fields:
|
473
|
-
// - UserId: set to the UserId parameter.
|
474
|
-
// - SystemCode: set to the SystemCode parameter.
|
475
|
-
// - Status: set to the Status parameter.
|
476
|
-
// - CreatedById: set to loginUser.UserId.
|
477
|
-
// - CreatedAt: set to the current timestamp.
|
478
|
-
// - UpdatedById: set to loginUser.UserId.
|
479
|
-
// - UpdatedAt: set to the current timestamp (same as CreatedAt).
|
480
|
-
// Save the new UserSystemAccess instance in the database within the dbTransaction.
|
481
|
-
|
482
|
-
const newUserSystemAccess = new UserSystemAccess();
|
483
|
-
newUserSystemAccess.UserId = parseInt(UserId);
|
484
|
-
newUserSystemAccess.SystemCode = SystemCode;
|
485
|
-
newUserSystemAccess.Status = Status;
|
486
|
-
newUserSystemAccess._CreatedById = loginUser.UserId;
|
487
|
-
newUserSystemAccess._CreatedAt = new Date();
|
488
|
-
newUserSystemAccess._UpdatedById = loginUser.UserId;
|
489
|
-
newUserSystemAccess._UpdatedAt = new Date();
|
490
|
-
|
491
|
-
const payload = {
|
492
|
-
UserId: newUserSystemAccess.UserId,
|
493
|
-
SystemCode: newUserSystemAccess.SystemCode,
|
494
|
-
Status: newUserSystemAccess.Status,
|
495
|
-
CreatedById: newUserSystemAccess.CreatedById,
|
496
|
-
CreatedAt: newUserSystemAccess.CreatedAt,
|
497
|
-
UpdatedById: newUserSystemAccess.UpdatedById,
|
498
|
-
UpdatedAt: newUserSystemAccess.UpdatedAt,
|
499
|
-
};
|
500
|
-
|
501
|
-
const systemAccess = await UserSystemAccess._Repository.create(payload, {
|
502
|
-
transaction: dbTransaction,
|
503
|
-
});
|
504
|
-
|
505
|
-
// Part 4: Record Activity History
|
506
|
-
// Initialize an empty object ({}) as EntityValueBefore.
|
507
|
-
// Set EntityValueAfter to the stringified version of the newly created UserSystemAccess instance.
|
508
|
-
// Create a new activity log entry:
|
509
|
-
// ActivityId: auto-generated by calling activity.createId().
|
510
|
-
// Action: set to ActionEnum.Create.
|
511
|
-
// Description: set to "Create User System Access".
|
512
|
-
// EntityType: set to UserSystemAccess.
|
513
|
-
// EntityId: set to the newly created UserSystemAccess.UserSystemAccessId.
|
514
|
-
// EntityValueBefore: set to {} (empty).
|
515
|
-
// EntityValueAfter: set to the stringified version of the new access record.
|
516
|
-
// Call the activity.create() method, passing:
|
517
|
-
// dbTransaction
|
518
|
-
// userId: set to loginUser.UserId.
|
519
|
-
|
520
|
-
const entityValueBefore = {};
|
521
|
-
|
522
|
-
//Instantiate new activity
|
523
|
-
const activity = new Activity();
|
524
|
-
activity.ActivityId = activity.createId();
|
525
|
-
activity.Action = ActionEnum.CREATE;
|
526
|
-
activity.Description = 'Create User System Access';
|
527
|
-
activity.EntityType = 'UserSystemAccess';
|
528
|
-
activity.EntityId = systemAccess.UserSystemAccessId?.toString();
|
529
|
-
activity.EntityValueBefore = JSON.stringify(entityValueBefore);
|
530
|
-
activity.EntityValueAfter = JSON.stringify(payload);
|
531
|
-
|
532
|
-
//Call Activity.create method
|
533
|
-
await activity.create(loginUser.ObjectId, dbTransaction);
|
534
|
-
|
535
|
-
// Part 5: Return Newly Created Record
|
536
|
-
// Return the newly created UserSystemAccess instance with all relevant fields, including UserSystemAccessId, SystemCode, Status, CreatedAt, and CreatedById.
|
537
|
-
newUserSystemAccess.UserSystemAccessId = systemAccess.UserSystemAccessId;
|
538
|
-
return newUserSystemAccess;
|
539
|
-
} catch (error) {
|
540
|
-
throw error;
|
541
|
-
}
|
542
|
-
}
|
543
|
-
|
544
|
-
public async update(
|
545
|
-
loginUser: User, //The user object representing the currently logged-in user.
|
546
|
-
dbTransaction: any, //The database transaction instance for managing the transaction scope.
|
547
|
-
Status: string, //The new access status (Active/Inactive) for the user system access.
|
548
|
-
) {
|
549
|
-
try {
|
550
|
-
// Part 1: Update Access
|
551
|
-
// Call the UserSystemAccess._Repo.update() method to perform the update operation, passing:
|
552
|
-
// - Status: The new access status.
|
553
|
-
// - UpdatedById: loginUser.UserId (to indicate who updated the record).
|
554
|
-
// - UpdatedAt: Set to the current date and time.
|
555
|
-
// - dbTransaction: The database transaction instance.
|
556
|
-
|
557
|
-
const entityValueBefore = {
|
558
|
-
UserId: this.UserId,
|
559
|
-
SystemCode: this.SystemCode,
|
560
|
-
Status: this.Status,
|
561
|
-
CreatedById: this.CreatedById,
|
562
|
-
CreatedAt: this.CreatedAt,
|
563
|
-
UpdatedById: this.UpdatedById,
|
564
|
-
UpdatedAt: this.UpdatedAt,
|
565
|
-
};
|
566
|
-
await UserSystemAccess._Repository.update(
|
567
|
-
{
|
568
|
-
Status: Status,
|
569
|
-
UpdatedById: loginUser.UserId,
|
570
|
-
UpdatedAt: new Date(),
|
571
|
-
},
|
572
|
-
{
|
573
|
-
where: {
|
574
|
-
UserSystemAccessId: this.UserSystemAccessId,
|
575
|
-
},
|
576
|
-
transaction: dbTransaction,
|
577
|
-
},
|
578
|
-
);
|
579
|
-
|
580
|
-
const entityValueAfter = {
|
581
|
-
UserId: this.UserId,
|
582
|
-
SystemCode: this.SystemCode,
|
583
|
-
Status: Status,
|
584
|
-
CreatedById: this.CreatedById,
|
585
|
-
CreatedAt: this.CreatedAt,
|
586
|
-
UpdatedById: loginUser.UserId,
|
587
|
-
UpdatedAt: new Date(),
|
588
|
-
};
|
589
|
-
|
590
|
-
// Part 2: Record Activity History
|
591
|
-
// Initialize a variable entityValueBefore to store the current state of the user system access record before the update.
|
592
|
-
// Create an instance of the Activity class and set the following properties:
|
593
|
-
// - ActivityId: Call activity.createId().
|
594
|
-
// - Action: Set to ActionEnum.Update.
|
595
|
-
// - Description: Set to Update User System Access.
|
596
|
-
// - EntityType: Set to UserSystemAccess.
|
597
|
-
// - EntityId: Use the ID of the updated user system access record.
|
598
|
-
// - EntityValueBefore: Stringify entityValueBefore to capture the state before the update.
|
599
|
-
// - EntityValueAfter: Stringify the updated user system access record to capture the new state after the update.
|
600
|
-
// Call the activity create method with the following parameters:
|
601
|
-
// - dbTransaction
|
602
|
-
// - userId: loginUser.UserId
|
603
|
-
const activity = new Activity();
|
604
|
-
activity.ActivityId = activity.createId();
|
605
|
-
activity.Action = ActionEnum.UPDATE;
|
606
|
-
activity.Description = 'Update User System Access';
|
607
|
-
activity.EntityType = 'UserSystemAccess';
|
608
|
-
activity.EntityId = this.UserSystemAccessId + '';
|
609
|
-
activity.EntityValueBefore = JSON.stringify(entityValueBefore);
|
610
|
-
activity.EntityValueAfter = JSON.stringify(entityValueAfter);
|
611
|
-
await activity.create(loginUser.ObjectId, dbTransaction);
|
612
|
-
|
613
|
-
// Part 3: Return Updated Record
|
614
|
-
// Retrieve the updated user system access record from the database or return the updated instance as needed.
|
615
|
-
|
616
|
-
// Part 5: Return Newly Created Record
|
617
|
-
// Return the newly created UserSystemAccess instance with all relevant fields, including UserSystemAccessId, SystemCode, Status, CreatedAt, and CreatedById.
|
618
|
-
return entityValueAfter;
|
619
|
-
} catch (error) {
|
620
|
-
throw error;
|
621
|
-
}
|
622
|
-
}
|
623
|
-
|
624
|
-
public static async remove(
|
625
|
-
loginUser: User, //The currently logged-in user initiating the request.
|
626
|
-
dbTransaction: any, //The active database transaction to ensure consistency during the query.
|
627
|
-
UserSystemAccessId: number, //The unique identifier of the User System Access record to be deleted.
|
628
|
-
) {
|
629
|
-
try {
|
630
|
-
// Part 1: Privilege Checking
|
631
|
-
// Call loginUser.checkPrivileges() method by passing:
|
632
|
-
// - SystemCode: Retrieve from app config.
|
633
|
-
// - PrivilegeCode: 'USER_SYSTEM_ACCESS_REMOVE'.
|
634
|
-
// If the user does not have the required privileges, throw an appropriate exception.
|
635
|
-
const systemCode =
|
636
|
-
ApplicationConfig.getComponentConfigValue('system-code');
|
637
|
-
const privilegeCode = 'USER_SYSTEM_ACCESS_REMOVE';
|
638
|
-
const isPrivileged = await loginUser.checkPrivileges(
|
639
|
-
systemCode,
|
640
|
-
privilegeCode,
|
641
|
-
);
|
642
|
-
if (!isPrivileged) {
|
643
|
-
throw new ClassError(
|
644
|
-
'UserSystemAccess',
|
645
|
-
'UserSystemAccessErrMsg01',
|
646
|
-
'You do not have permission to access this resource.',
|
647
|
-
);
|
648
|
-
}
|
649
|
-
|
650
|
-
// Part 2: Retrieve Record
|
651
|
-
// Use the UserSystemAccessRepo.findById(UserSystemAccessId) method to retrieve the record.
|
652
|
-
// If the record does not exist, throw an exception indicating the record was not found.
|
653
|
-
|
654
|
-
const userSystemAccess = await UserSystemAccess._Repository.findOne({
|
655
|
-
where: {
|
656
|
-
UserSystemAccessId: UserSystemAccessId,
|
657
|
-
},
|
658
|
-
transaction: dbTransaction,
|
659
|
-
});
|
660
|
-
|
661
|
-
if (!userSystemAccess) {
|
662
|
-
throw new ClassError(
|
663
|
-
'UserSystemAccess',
|
664
|
-
'UserSystemAccessErrMsg02',
|
665
|
-
'User System Access not Found',
|
666
|
-
);
|
667
|
-
}
|
668
|
-
|
669
|
-
// Part 3: Delete Record
|
670
|
-
// Call the UserSystemAccess._Repo.delete() method, passing:
|
671
|
-
// - UserSystemAccessId
|
672
|
-
// dbTransaction to permanently delete the record from the database.
|
673
|
-
await UserSystemAccess._Repository.delete(
|
674
|
-
UserSystemAccessId,
|
675
|
-
dbTransaction,
|
676
|
-
);
|
677
|
-
|
678
|
-
const entityValueBefore = {
|
679
|
-
UserId: userSystemAccess.UserId,
|
680
|
-
SystemCode: userSystemAccess.SystemCode,
|
681
|
-
Status: userSystemAccess.Status,
|
682
|
-
CreatedById: userSystemAccess.CreatedById,
|
683
|
-
CreatedAt: userSystemAccess.CreatedAt,
|
684
|
-
UpdatedById: userSystemAccess.UpdatedById,
|
685
|
-
UpdatedAt: userSystemAccess.UpdatedAt,
|
686
|
-
};
|
687
|
-
|
688
|
-
// Part 4: Record Activity History
|
689
|
-
// Instantiate a new activity from the Activity class, and set:
|
690
|
-
// - ActivityId: activity.createId()
|
691
|
-
// - Action: ActionEnum.Delete
|
692
|
-
// - Description: Delete User System Access
|
693
|
-
// - EntityType: UserSystemAccess
|
694
|
-
// - EntityId: UserSystemAccessId
|
695
|
-
// - EntityValueBefore: Stringified representation of the record before deletion.
|
696
|
-
// - EntityValueAfter: null.
|
697
|
-
// Call the activity.create() method by passing:
|
698
|
-
// - dbTransaction
|
699
|
-
// - userId: loginUser.UserId.
|
700
|
-
|
701
|
-
//Instantiate new activity
|
702
|
-
const activity = new Activity();
|
703
|
-
activity.ActivityId = activity.createId();
|
704
|
-
activity.Action = ActionEnum.DELETE;
|
705
|
-
activity.Description = 'Delete User System Access';
|
706
|
-
activity.EntityType = 'UserSystemAccess';
|
707
|
-
activity.EntityId = UserSystemAccessId?.toString();
|
708
|
-
activity.EntityValueBefore = JSON.stringify(entityValueBefore);
|
709
|
-
activity.EntityValueAfter = JSON.stringify({});
|
710
|
-
|
711
|
-
//Call Activity.create method
|
712
|
-
await activity.create(loginUser.ObjectId, dbTransaction);
|
713
|
-
} catch (error) {
|
714
|
-
throw error;
|
715
|
-
}
|
716
|
-
}
|
717
|
-
}
|
1
|
+
import { ClassError, ObjectBase } from '@tomei/general';
|
2
|
+
import { UserSystemAccessRepository } from './user-system-access.repository';
|
3
|
+
import { IUserSystemAccess } from '../../interfaces/user-system-access.interface';
|
4
|
+
import { User } from '../login-user/user';
|
5
|
+
import { ApplicationConfig } from '@tomei/config';
|
6
|
+
import SystemModel from '../../models/system.entity';
|
7
|
+
import SystemPrivilegeModel from '../../models/system-privilege.entity';
|
8
|
+
import UserSystemAccessModel from '../../models/user-system-access.entity';
|
9
|
+
import GroupModel from '../../models/group.entity';
|
10
|
+
import GroupSystemAccessModel from '../../models/group-system-access.entity';
|
11
|
+
import UserModel from '../../models/user.entity';
|
12
|
+
import { ActionEnum, Activity } from '@tomei/activity-history';
|
13
|
+
import { Op } from 'sequelize';
|
14
|
+
import { UserPrivilegeRepository } from '../user-privilege/user-privilege.repository';
|
15
|
+
import UserGroupModel from 'models/user-group.entity';
|
16
|
+
import { UserStatus } from '../../enum';
|
17
|
+
|
18
|
+
export class UserSystemAccess extends ObjectBase {
|
19
|
+
ObjectType = 'UserSystemAccess';
|
20
|
+
TableName = 'sso_UserSystemAccess';
|
21
|
+
ObjectName: string;
|
22
|
+
ObjectId: string;
|
23
|
+
UserSystemAccessId: number;
|
24
|
+
UserId: number;
|
25
|
+
SystemCode: string;
|
26
|
+
Status: string;
|
27
|
+
private _CreatedAt: Date;
|
28
|
+
private _UpdatedAt: Date;
|
29
|
+
private _CreatedById: number;
|
30
|
+
private _UpdatedById: number;
|
31
|
+
|
32
|
+
get CreatedAt() {
|
33
|
+
return this._CreatedAt;
|
34
|
+
}
|
35
|
+
|
36
|
+
get UpdatedAt() {
|
37
|
+
return this._UpdatedAt;
|
38
|
+
}
|
39
|
+
|
40
|
+
get CreatedById() {
|
41
|
+
return this._CreatedById;
|
42
|
+
}
|
43
|
+
|
44
|
+
get UpdatedById() {
|
45
|
+
return this._UpdatedById;
|
46
|
+
}
|
47
|
+
|
48
|
+
private static _Repository = new UserSystemAccessRepository();
|
49
|
+
private static _UserPrivilegeRepo = new UserPrivilegeRepository();
|
50
|
+
|
51
|
+
private constructor(userSystemAccessAttr?: IUserSystemAccess) {
|
52
|
+
super();
|
53
|
+
if (userSystemAccessAttr) {
|
54
|
+
this.UserSystemAccessId = userSystemAccessAttr.UserSystemAccessId;
|
55
|
+
this.UserId = userSystemAccessAttr.UserId;
|
56
|
+
this.SystemCode = userSystemAccessAttr.SystemCode;
|
57
|
+
this.Status = userSystemAccessAttr.Status;
|
58
|
+
this._CreatedById = userSystemAccessAttr.CreatedById;
|
59
|
+
this._CreatedAt = userSystemAccessAttr.CreatedAt;
|
60
|
+
this._UpdatedById = userSystemAccessAttr.UpdatedById;
|
61
|
+
this._UpdatedAt = userSystemAccessAttr.UpdatedAt;
|
62
|
+
}
|
63
|
+
}
|
64
|
+
|
65
|
+
static async init(dbTransaction: any, UserSystemAccessId?: number) {
|
66
|
+
try {
|
67
|
+
const userSystemAccess = new UserSystemAccess();
|
68
|
+
if (UserSystemAccessId) {
|
69
|
+
const userSystemAccessAttr = await this._Repository.findOne({
|
70
|
+
where: { UserSystemAccessId },
|
71
|
+
transaction: dbTransaction,
|
72
|
+
});
|
73
|
+
if (userSystemAccessAttr) {
|
74
|
+
return new UserSystemAccess(
|
75
|
+
userSystemAccessAttr.get({ plain: true }),
|
76
|
+
);
|
77
|
+
} else {
|
78
|
+
throw new ClassError(
|
79
|
+
'UserSystemAccess',
|
80
|
+
'UserSystemAccessErrMsg00',
|
81
|
+
'UserSystemAccess not found',
|
82
|
+
);
|
83
|
+
}
|
84
|
+
}
|
85
|
+
return userSystemAccess;
|
86
|
+
} catch (error) {
|
87
|
+
throw error;
|
88
|
+
}
|
89
|
+
}
|
90
|
+
|
91
|
+
public static async findAll(
|
92
|
+
loginUser: User, //The currently logged-in user initiating the request.
|
93
|
+
dbTransaction: any, //The active database transaction to ensure consistency during the query.
|
94
|
+
whereOption: {
|
95
|
+
//An object containing filter criteria, specifically:
|
96
|
+
UserId: number; //The ID of the user whose system access records are to be retrieved.
|
97
|
+
SystemCode?: string;
|
98
|
+
},
|
99
|
+
pagination: {
|
100
|
+
//An object containing pagination parameters:
|
101
|
+
page: number; //The current page number to retrieve.
|
102
|
+
limit: number; //The number of records to retrieve per page.
|
103
|
+
},
|
104
|
+
): Promise<{
|
105
|
+
records: {
|
106
|
+
SystemName: string;
|
107
|
+
SystemCode: string;
|
108
|
+
Status: string;
|
109
|
+
CreatedBy: string;
|
110
|
+
CreatedAt: Date;
|
111
|
+
UpdatedBy: string;
|
112
|
+
UpdatedAt: Date;
|
113
|
+
}[];
|
114
|
+
pagination: {
|
115
|
+
currentPage: number;
|
116
|
+
pageSize: number;
|
117
|
+
totalRecords: number;
|
118
|
+
};
|
119
|
+
}> {
|
120
|
+
try {
|
121
|
+
// Privilege Checking:
|
122
|
+
// Call loginUser.checkPrivileges() method by passing:
|
123
|
+
// SystemCode: Retrieve from app config.
|
124
|
+
// PrivilegeCode: 'USER_SYSTEM_ACCESS_LIST'.
|
125
|
+
const systemCode =
|
126
|
+
ApplicationConfig.getComponentConfigValue('system-code');
|
127
|
+
const privilegeCode = 'USER_SYSTEM_ACCESS_LIST';
|
128
|
+
const isPrivileged = await loginUser.checkPrivileges(
|
129
|
+
systemCode,
|
130
|
+
privilegeCode,
|
131
|
+
);
|
132
|
+
if (!isPrivileged) {
|
133
|
+
throw new ClassError(
|
134
|
+
'UserSystemAccess',
|
135
|
+
'UserSystemAccessErrMsg01',
|
136
|
+
'You do not have permission to access this resource.',
|
137
|
+
);
|
138
|
+
}
|
139
|
+
// Create a where condition using whereOption to filter by UserId.
|
140
|
+
// Set up pagination logic using the pagination parameter:
|
141
|
+
// Calculate offset based on page and limit.
|
142
|
+
const options: any = {
|
143
|
+
distinct: true,
|
144
|
+
where: {
|
145
|
+
UserId: whereOption.UserId,
|
146
|
+
},
|
147
|
+
offset: (pagination.page - 1) * pagination.limit,
|
148
|
+
limit: pagination.limit,
|
149
|
+
transaction: dbTransaction,
|
150
|
+
include: [
|
151
|
+
{
|
152
|
+
model: SystemModel,
|
153
|
+
attributes: ['Name', 'SystemCode'],
|
154
|
+
},
|
155
|
+
{
|
156
|
+
model: UserModel,
|
157
|
+
as: 'CreatedBy',
|
158
|
+
attributes: ['FullName'],
|
159
|
+
},
|
160
|
+
{
|
161
|
+
model: UserModel,
|
162
|
+
as: 'UpdatedBy',
|
163
|
+
attributes: ['FullName'],
|
164
|
+
},
|
165
|
+
],
|
166
|
+
};
|
167
|
+
const userSystemAccesses =
|
168
|
+
await this._Repository.findAllWithPagination(options);
|
169
|
+
return {
|
170
|
+
records: userSystemAccesses.rows.map((userSystemAccess) => {
|
171
|
+
return {
|
172
|
+
UserSystemAccessId: userSystemAccess.UserSystemAccessId,
|
173
|
+
SystemName: userSystemAccess.System.Name,
|
174
|
+
SystemCode: userSystemAccess.System.SystemCode,
|
175
|
+
Status: userSystemAccess.Status,
|
176
|
+
CreatedBy: userSystemAccess.CreatedBy.FullName,
|
177
|
+
CreatedAt: userSystemAccess.CreatedAt,
|
178
|
+
UpdatedBy: userSystemAccess.UpdatedBy.FullName,
|
179
|
+
UpdatedAt: userSystemAccess.UpdatedAt,
|
180
|
+
};
|
181
|
+
}),
|
182
|
+
pagination: {
|
183
|
+
currentPage: pagination.page,
|
184
|
+
pageSize: pagination.limit,
|
185
|
+
totalRecords: userSystemAccesses.count,
|
186
|
+
},
|
187
|
+
};
|
188
|
+
} catch (error) {
|
189
|
+
throw error;
|
190
|
+
}
|
191
|
+
}
|
192
|
+
|
193
|
+
public static async findAllUsers(
|
194
|
+
loginUser: User, //The currently logged-in user initiating the request.
|
195
|
+
dbTransaction: any, //The active database transaction to ensure consistency during the query.
|
196
|
+
SystemCode: string,
|
197
|
+
Page: number,
|
198
|
+
Rows: number,
|
199
|
+
Search: {
|
200
|
+
UserId?: string | number;
|
201
|
+
Status?: string;
|
202
|
+
},
|
203
|
+
) {
|
204
|
+
// Part 1: Privilege Checking
|
205
|
+
const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
|
206
|
+
const isPrivileged = await loginUser.checkPrivileges(
|
207
|
+
systemCode,
|
208
|
+
'USER_SYSTEM_ACCESS_LIST',
|
209
|
+
);
|
210
|
+
|
211
|
+
if (!isPrivileged) {
|
212
|
+
throw new ClassError(
|
213
|
+
'UserSystemAccessUser',
|
214
|
+
'UserSystemAccessUserErrMsg01',
|
215
|
+
'You do not have permission to view system access users.',
|
216
|
+
);
|
217
|
+
}
|
218
|
+
|
219
|
+
try {
|
220
|
+
// Part 2: Retrieve System Access Users and returns
|
221
|
+
const queryObj: any = { SystemCode: SystemCode };
|
222
|
+
|
223
|
+
if (Search) {
|
224
|
+
Object.entries(Search).forEach(([key, value]) => {
|
225
|
+
queryObj[key] = value;
|
226
|
+
});
|
227
|
+
}
|
228
|
+
|
229
|
+
let options: any = {
|
230
|
+
where: queryObj,
|
231
|
+
distinct: true,
|
232
|
+
transaction: dbTransaction,
|
233
|
+
};
|
234
|
+
|
235
|
+
if (Page && Rows) {
|
236
|
+
options = {
|
237
|
+
...options,
|
238
|
+
limit: Rows,
|
239
|
+
offset: Rows * (Page - 1),
|
240
|
+
order: [['CreatedAt', 'DESC']],
|
241
|
+
include: [
|
242
|
+
{
|
243
|
+
model: SystemModel,
|
244
|
+
attributes: ['Name', 'SystemCode'],
|
245
|
+
},
|
246
|
+
{
|
247
|
+
model: UserModel,
|
248
|
+
where: {
|
249
|
+
Status: UserStatus.ACTIVE,
|
250
|
+
},
|
251
|
+
as: 'User',
|
252
|
+
attributes: ['UserId', 'FullName'],
|
253
|
+
},
|
254
|
+
],
|
255
|
+
};
|
256
|
+
}
|
257
|
+
|
258
|
+
const userSystemAccesses =
|
259
|
+
await this._Repository.findAndCountAll(options);
|
260
|
+
return userSystemAccesses;
|
261
|
+
} catch (error) {
|
262
|
+
throw error;
|
263
|
+
}
|
264
|
+
}
|
265
|
+
|
266
|
+
public static async findAllUserPrivileges(
|
267
|
+
loginUser: User, //The currently logged-in user initiating the request.
|
268
|
+
dbTransaction: any, //The active database transaction to ensure consistency during the query.
|
269
|
+
SystemCode: string,
|
270
|
+
search?: {
|
271
|
+
UserId?: string[];
|
272
|
+
Status?: string;
|
273
|
+
},
|
274
|
+
) {
|
275
|
+
// Part 1: Privilege Checking
|
276
|
+
const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
|
277
|
+
const isPrivileged = await loginUser.checkPrivileges(
|
278
|
+
systemCode,
|
279
|
+
'USER_SYSTEM_ACCESS_LIST',
|
280
|
+
);
|
281
|
+
|
282
|
+
if (!isPrivileged) {
|
283
|
+
throw new ClassError(
|
284
|
+
'UserSystemAccessUser',
|
285
|
+
'UserSystemAccessUserErrMsg01',
|
286
|
+
'You do not have permission to view system access users.',
|
287
|
+
);
|
288
|
+
}
|
289
|
+
|
290
|
+
try {
|
291
|
+
//Part 2: Retrieve User System Access Based on Privileges
|
292
|
+
let systemWhere: any = {};
|
293
|
+
|
294
|
+
if (SystemCode) {
|
295
|
+
systemWhere = {
|
296
|
+
SystemCode: {
|
297
|
+
[Op.substring]: SystemCode,
|
298
|
+
},
|
299
|
+
};
|
300
|
+
}
|
301
|
+
|
302
|
+
const allSystemAccessUsers = await UserSystemAccessModel.findAll({
|
303
|
+
include: [
|
304
|
+
{
|
305
|
+
model: SystemModel,
|
306
|
+
where: systemWhere,
|
307
|
+
},
|
308
|
+
{
|
309
|
+
model: UserModel,
|
310
|
+
as: 'User',
|
311
|
+
attributes: ['UserId', 'FullName'],
|
312
|
+
},
|
313
|
+
],
|
314
|
+
transaction: dbTransaction,
|
315
|
+
});
|
316
|
+
|
317
|
+
const allPrivileges = await SystemPrivilegeModel.findAll({
|
318
|
+
where: systemWhere,
|
319
|
+
transaction: dbTransaction,
|
320
|
+
});
|
321
|
+
|
322
|
+
const systemAccessUserPrivileges = allPrivileges.map(
|
323
|
+
async (privilege) => {
|
324
|
+
const filteredUsers = allSystemAccessUsers
|
325
|
+
.map((userAccess) => userAccess.User)
|
326
|
+
.filter((user) => search.UserId.includes(String(user.UserId)));
|
327
|
+
|
328
|
+
return {
|
329
|
+
...privilege.get({ plain: true }),
|
330
|
+
Users: filteredUsers,
|
331
|
+
};
|
332
|
+
},
|
333
|
+
);
|
334
|
+
|
335
|
+
return systemAccessUserPrivileges;
|
336
|
+
} catch (error) {
|
337
|
+
throw error;
|
338
|
+
}
|
339
|
+
}
|
340
|
+
|
341
|
+
public static async findAllUserRoles(
|
342
|
+
loginUser: User, //The currently logged-in user initiating the request.
|
343
|
+
dbTransaction: any, //The active database transaction to ensure consistency during the query.
|
344
|
+
SystemCode: string,
|
345
|
+
search?: {
|
346
|
+
UserId?: string[];
|
347
|
+
Status?: string;
|
348
|
+
},
|
349
|
+
) {
|
350
|
+
// Part 1: Privilege Checking
|
351
|
+
const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
|
352
|
+
const isPrivileged = await loginUser.checkPrivileges(
|
353
|
+
systemCode,
|
354
|
+
'USER_SYSTEM_ACCESS_LIST',
|
355
|
+
);
|
356
|
+
|
357
|
+
if (!isPrivileged) {
|
358
|
+
throw new ClassError(
|
359
|
+
'UserSystemAccessUser',
|
360
|
+
'UserSystemAccessUserErrMsg01',
|
361
|
+
'You do not have permission to view system access users.',
|
362
|
+
);
|
363
|
+
}
|
364
|
+
|
365
|
+
try {
|
366
|
+
//Part 2: Retrieve User System Access Based on Privileges
|
367
|
+
let systemWhere: any = {};
|
368
|
+
|
369
|
+
if (SystemCode) {
|
370
|
+
systemWhere = {
|
371
|
+
SystemCode: {
|
372
|
+
[Op.substring]: SystemCode,
|
373
|
+
},
|
374
|
+
};
|
375
|
+
}
|
376
|
+
|
377
|
+
const allGroupSystemAccess = await GroupSystemAccessModel.findAll({
|
378
|
+
where: systemWhere,
|
379
|
+
include: [
|
380
|
+
{
|
381
|
+
model: GroupModel,
|
382
|
+
where: {
|
383
|
+
Type: 'Role',
|
384
|
+
},
|
385
|
+
},
|
386
|
+
],
|
387
|
+
transaction: dbTransaction,
|
388
|
+
});
|
389
|
+
|
390
|
+
const allSystemAccessUsers = await UserSystemAccessModel.findAll({
|
391
|
+
include: [
|
392
|
+
{
|
393
|
+
model: SystemModel,
|
394
|
+
where: systemWhere,
|
395
|
+
},
|
396
|
+
{
|
397
|
+
model: UserModel,
|
398
|
+
as: 'User',
|
399
|
+
attributes: ['UserId', 'FullName'],
|
400
|
+
},
|
401
|
+
],
|
402
|
+
transaction: dbTransaction,
|
403
|
+
});
|
404
|
+
|
405
|
+
const systemAccessUserRoles = allGroupSystemAccess.map(
|
406
|
+
(groupSystemAccess) => {
|
407
|
+
const filteredUsers = allSystemAccessUsers
|
408
|
+
.map((userAccess) => userAccess.User)
|
409
|
+
.filter((user) => search.UserId.includes(String(user.UserId)));
|
410
|
+
|
411
|
+
return {
|
412
|
+
...groupSystemAccess.Group.get({ plain: true }),
|
413
|
+
Users: filteredUsers,
|
414
|
+
};
|
415
|
+
},
|
416
|
+
);
|
417
|
+
|
418
|
+
return systemAccessUserRoles;
|
419
|
+
} catch (error) {
|
420
|
+
throw error;
|
421
|
+
}
|
422
|
+
}
|
423
|
+
|
424
|
+
public static async createAccess(
|
425
|
+
loginUser: User, //The currently logged-in user initiating the request.
|
426
|
+
dbTransaction: any, //The active database transaction to ensure consistency during the query.
|
427
|
+
UserId: string, //The user ID for whom system access is being created.
|
428
|
+
SystemCode: string, //The system code for which access is being granted.
|
429
|
+
Status: string, //The status of access ('Active' or 'Inactive').
|
430
|
+
) {
|
431
|
+
try {
|
432
|
+
// Part 1: Privilege Checking:
|
433
|
+
// Call loginUser.checkPrivileges() method by passing:
|
434
|
+
// SystemCode: Retrieve from app config.
|
435
|
+
// PrivilegeCode: 'USER_SYSTEM_ACCESS_CREATE'.
|
436
|
+
const systemCode =
|
437
|
+
ApplicationConfig.getComponentConfigValue('system-code');
|
438
|
+
const privilegeCode = 'USER_SYSTEM_ACCESS_CREATE';
|
439
|
+
const isPrivileged = await loginUser.checkPrivileges(
|
440
|
+
systemCode,
|
441
|
+
privilegeCode,
|
442
|
+
);
|
443
|
+
if (!isPrivileged) {
|
444
|
+
throw new ClassError(
|
445
|
+
'UserSystemAccess',
|
446
|
+
'UserSystemAccessErrMsg01',
|
447
|
+
'You do not have permission to access this resource.',
|
448
|
+
);
|
449
|
+
}
|
450
|
+
// Part 2: Validation for Existing Access
|
451
|
+
// Use the UserSystemAccess.findAll() method to check if the user already has access to the specified system:
|
452
|
+
// Pass the following parameters:
|
453
|
+
// loginUser
|
454
|
+
// dbTransaction
|
455
|
+
// whereOption: set to UserId = UserId and SystemCode = SystemCode.
|
456
|
+
// If a record is found, throw an error indicating that access for this user and system already exists.
|
457
|
+
|
458
|
+
const isExist = await UserSystemAccess._Repository.findAll({
|
459
|
+
where: { [Op.and]: [{ UserId: UserId }, { SystemCode: SystemCode }] },
|
460
|
+
transaction: dbTransaction,
|
461
|
+
});
|
462
|
+
|
463
|
+
if (isExist?.length > 0) {
|
464
|
+
throw new ClassError(
|
465
|
+
'UserSystemAccess',
|
466
|
+
'UserSystemAccessErrMsg01',
|
467
|
+
'User already have access to this system',
|
468
|
+
);
|
469
|
+
}
|
470
|
+
|
471
|
+
// Part 3: Insert System Access Record
|
472
|
+
// After successful validation, create a new instance of UserSystemAccess with the following fields:
|
473
|
+
// - UserId: set to the UserId parameter.
|
474
|
+
// - SystemCode: set to the SystemCode parameter.
|
475
|
+
// - Status: set to the Status parameter.
|
476
|
+
// - CreatedById: set to loginUser.UserId.
|
477
|
+
// - CreatedAt: set to the current timestamp.
|
478
|
+
// - UpdatedById: set to loginUser.UserId.
|
479
|
+
// - UpdatedAt: set to the current timestamp (same as CreatedAt).
|
480
|
+
// Save the new UserSystemAccess instance in the database within the dbTransaction.
|
481
|
+
|
482
|
+
const newUserSystemAccess = new UserSystemAccess();
|
483
|
+
newUserSystemAccess.UserId = parseInt(UserId);
|
484
|
+
newUserSystemAccess.SystemCode = SystemCode;
|
485
|
+
newUserSystemAccess.Status = Status;
|
486
|
+
newUserSystemAccess._CreatedById = loginUser.UserId;
|
487
|
+
newUserSystemAccess._CreatedAt = new Date();
|
488
|
+
newUserSystemAccess._UpdatedById = loginUser.UserId;
|
489
|
+
newUserSystemAccess._UpdatedAt = new Date();
|
490
|
+
|
491
|
+
const payload = {
|
492
|
+
UserId: newUserSystemAccess.UserId,
|
493
|
+
SystemCode: newUserSystemAccess.SystemCode,
|
494
|
+
Status: newUserSystemAccess.Status,
|
495
|
+
CreatedById: newUserSystemAccess.CreatedById,
|
496
|
+
CreatedAt: newUserSystemAccess.CreatedAt,
|
497
|
+
UpdatedById: newUserSystemAccess.UpdatedById,
|
498
|
+
UpdatedAt: newUserSystemAccess.UpdatedAt,
|
499
|
+
};
|
500
|
+
|
501
|
+
const systemAccess = await UserSystemAccess._Repository.create(payload, {
|
502
|
+
transaction: dbTransaction,
|
503
|
+
});
|
504
|
+
|
505
|
+
// Part 4: Record Activity History
|
506
|
+
// Initialize an empty object ({}) as EntityValueBefore.
|
507
|
+
// Set EntityValueAfter to the stringified version of the newly created UserSystemAccess instance.
|
508
|
+
// Create a new activity log entry:
|
509
|
+
// ActivityId: auto-generated by calling activity.createId().
|
510
|
+
// Action: set to ActionEnum.Create.
|
511
|
+
// Description: set to "Create User System Access".
|
512
|
+
// EntityType: set to UserSystemAccess.
|
513
|
+
// EntityId: set to the newly created UserSystemAccess.UserSystemAccessId.
|
514
|
+
// EntityValueBefore: set to {} (empty).
|
515
|
+
// EntityValueAfter: set to the stringified version of the new access record.
|
516
|
+
// Call the activity.create() method, passing:
|
517
|
+
// dbTransaction
|
518
|
+
// userId: set to loginUser.UserId.
|
519
|
+
|
520
|
+
const entityValueBefore = {};
|
521
|
+
|
522
|
+
//Instantiate new activity
|
523
|
+
const activity = new Activity();
|
524
|
+
activity.ActivityId = activity.createId();
|
525
|
+
activity.Action = ActionEnum.CREATE;
|
526
|
+
activity.Description = 'Create User System Access';
|
527
|
+
activity.EntityType = 'UserSystemAccess';
|
528
|
+
activity.EntityId = systemAccess.UserSystemAccessId?.toString();
|
529
|
+
activity.EntityValueBefore = JSON.stringify(entityValueBefore);
|
530
|
+
activity.EntityValueAfter = JSON.stringify(payload);
|
531
|
+
|
532
|
+
//Call Activity.create method
|
533
|
+
await activity.create(loginUser.ObjectId, dbTransaction);
|
534
|
+
|
535
|
+
// Part 5: Return Newly Created Record
|
536
|
+
// Return the newly created UserSystemAccess instance with all relevant fields, including UserSystemAccessId, SystemCode, Status, CreatedAt, and CreatedById.
|
537
|
+
newUserSystemAccess.UserSystemAccessId = systemAccess.UserSystemAccessId;
|
538
|
+
return newUserSystemAccess;
|
539
|
+
} catch (error) {
|
540
|
+
throw error;
|
541
|
+
}
|
542
|
+
}
|
543
|
+
|
544
|
+
public async update(
|
545
|
+
loginUser: User, //The user object representing the currently logged-in user.
|
546
|
+
dbTransaction: any, //The database transaction instance for managing the transaction scope.
|
547
|
+
Status: string, //The new access status (Active/Inactive) for the user system access.
|
548
|
+
) {
|
549
|
+
try {
|
550
|
+
// Part 1: Update Access
|
551
|
+
// Call the UserSystemAccess._Repo.update() method to perform the update operation, passing:
|
552
|
+
// - Status: The new access status.
|
553
|
+
// - UpdatedById: loginUser.UserId (to indicate who updated the record).
|
554
|
+
// - UpdatedAt: Set to the current date and time.
|
555
|
+
// - dbTransaction: The database transaction instance.
|
556
|
+
|
557
|
+
const entityValueBefore = {
|
558
|
+
UserId: this.UserId,
|
559
|
+
SystemCode: this.SystemCode,
|
560
|
+
Status: this.Status,
|
561
|
+
CreatedById: this.CreatedById,
|
562
|
+
CreatedAt: this.CreatedAt,
|
563
|
+
UpdatedById: this.UpdatedById,
|
564
|
+
UpdatedAt: this.UpdatedAt,
|
565
|
+
};
|
566
|
+
await UserSystemAccess._Repository.update(
|
567
|
+
{
|
568
|
+
Status: Status,
|
569
|
+
UpdatedById: loginUser.UserId,
|
570
|
+
UpdatedAt: new Date(),
|
571
|
+
},
|
572
|
+
{
|
573
|
+
where: {
|
574
|
+
UserSystemAccessId: this.UserSystemAccessId,
|
575
|
+
},
|
576
|
+
transaction: dbTransaction,
|
577
|
+
},
|
578
|
+
);
|
579
|
+
|
580
|
+
const entityValueAfter = {
|
581
|
+
UserId: this.UserId,
|
582
|
+
SystemCode: this.SystemCode,
|
583
|
+
Status: Status,
|
584
|
+
CreatedById: this.CreatedById,
|
585
|
+
CreatedAt: this.CreatedAt,
|
586
|
+
UpdatedById: loginUser.UserId,
|
587
|
+
UpdatedAt: new Date(),
|
588
|
+
};
|
589
|
+
|
590
|
+
// Part 2: Record Activity History
|
591
|
+
// Initialize a variable entityValueBefore to store the current state of the user system access record before the update.
|
592
|
+
// Create an instance of the Activity class and set the following properties:
|
593
|
+
// - ActivityId: Call activity.createId().
|
594
|
+
// - Action: Set to ActionEnum.Update.
|
595
|
+
// - Description: Set to Update User System Access.
|
596
|
+
// - EntityType: Set to UserSystemAccess.
|
597
|
+
// - EntityId: Use the ID of the updated user system access record.
|
598
|
+
// - EntityValueBefore: Stringify entityValueBefore to capture the state before the update.
|
599
|
+
// - EntityValueAfter: Stringify the updated user system access record to capture the new state after the update.
|
600
|
+
// Call the activity create method with the following parameters:
|
601
|
+
// - dbTransaction
|
602
|
+
// - userId: loginUser.UserId
|
603
|
+
const activity = new Activity();
|
604
|
+
activity.ActivityId = activity.createId();
|
605
|
+
activity.Action = ActionEnum.UPDATE;
|
606
|
+
activity.Description = 'Update User System Access';
|
607
|
+
activity.EntityType = 'UserSystemAccess';
|
608
|
+
activity.EntityId = this.UserSystemAccessId + '';
|
609
|
+
activity.EntityValueBefore = JSON.stringify(entityValueBefore);
|
610
|
+
activity.EntityValueAfter = JSON.stringify(entityValueAfter);
|
611
|
+
await activity.create(loginUser.ObjectId, dbTransaction);
|
612
|
+
|
613
|
+
// Part 3: Return Updated Record
|
614
|
+
// Retrieve the updated user system access record from the database or return the updated instance as needed.
|
615
|
+
|
616
|
+
// Part 5: Return Newly Created Record
|
617
|
+
// Return the newly created UserSystemAccess instance with all relevant fields, including UserSystemAccessId, SystemCode, Status, CreatedAt, and CreatedById.
|
618
|
+
return entityValueAfter;
|
619
|
+
} catch (error) {
|
620
|
+
throw error;
|
621
|
+
}
|
622
|
+
}
|
623
|
+
|
624
|
+
public static async remove(
|
625
|
+
loginUser: User, //The currently logged-in user initiating the request.
|
626
|
+
dbTransaction: any, //The active database transaction to ensure consistency during the query.
|
627
|
+
UserSystemAccessId: number, //The unique identifier of the User System Access record to be deleted.
|
628
|
+
) {
|
629
|
+
try {
|
630
|
+
// Part 1: Privilege Checking
|
631
|
+
// Call loginUser.checkPrivileges() method by passing:
|
632
|
+
// - SystemCode: Retrieve from app config.
|
633
|
+
// - PrivilegeCode: 'USER_SYSTEM_ACCESS_REMOVE'.
|
634
|
+
// If the user does not have the required privileges, throw an appropriate exception.
|
635
|
+
const systemCode =
|
636
|
+
ApplicationConfig.getComponentConfigValue('system-code');
|
637
|
+
const privilegeCode = 'USER_SYSTEM_ACCESS_REMOVE';
|
638
|
+
const isPrivileged = await loginUser.checkPrivileges(
|
639
|
+
systemCode,
|
640
|
+
privilegeCode,
|
641
|
+
);
|
642
|
+
if (!isPrivileged) {
|
643
|
+
throw new ClassError(
|
644
|
+
'UserSystemAccess',
|
645
|
+
'UserSystemAccessErrMsg01',
|
646
|
+
'You do not have permission to access this resource.',
|
647
|
+
);
|
648
|
+
}
|
649
|
+
|
650
|
+
// Part 2: Retrieve Record
|
651
|
+
// Use the UserSystemAccessRepo.findById(UserSystemAccessId) method to retrieve the record.
|
652
|
+
// If the record does not exist, throw an exception indicating the record was not found.
|
653
|
+
|
654
|
+
const userSystemAccess = await UserSystemAccess._Repository.findOne({
|
655
|
+
where: {
|
656
|
+
UserSystemAccessId: UserSystemAccessId,
|
657
|
+
},
|
658
|
+
transaction: dbTransaction,
|
659
|
+
});
|
660
|
+
|
661
|
+
if (!userSystemAccess) {
|
662
|
+
throw new ClassError(
|
663
|
+
'UserSystemAccess',
|
664
|
+
'UserSystemAccessErrMsg02',
|
665
|
+
'User System Access not Found',
|
666
|
+
);
|
667
|
+
}
|
668
|
+
|
669
|
+
// Part 3: Delete Record
|
670
|
+
// Call the UserSystemAccess._Repo.delete() method, passing:
|
671
|
+
// - UserSystemAccessId
|
672
|
+
// dbTransaction to permanently delete the record from the database.
|
673
|
+
await UserSystemAccess._Repository.delete(
|
674
|
+
UserSystemAccessId,
|
675
|
+
dbTransaction,
|
676
|
+
);
|
677
|
+
|
678
|
+
const entityValueBefore = {
|
679
|
+
UserId: userSystemAccess.UserId,
|
680
|
+
SystemCode: userSystemAccess.SystemCode,
|
681
|
+
Status: userSystemAccess.Status,
|
682
|
+
CreatedById: userSystemAccess.CreatedById,
|
683
|
+
CreatedAt: userSystemAccess.CreatedAt,
|
684
|
+
UpdatedById: userSystemAccess.UpdatedById,
|
685
|
+
UpdatedAt: userSystemAccess.UpdatedAt,
|
686
|
+
};
|
687
|
+
|
688
|
+
// Part 4: Record Activity History
|
689
|
+
// Instantiate a new activity from the Activity class, and set:
|
690
|
+
// - ActivityId: activity.createId()
|
691
|
+
// - Action: ActionEnum.Delete
|
692
|
+
// - Description: Delete User System Access
|
693
|
+
// - EntityType: UserSystemAccess
|
694
|
+
// - EntityId: UserSystemAccessId
|
695
|
+
// - EntityValueBefore: Stringified representation of the record before deletion.
|
696
|
+
// - EntityValueAfter: null.
|
697
|
+
// Call the activity.create() method by passing:
|
698
|
+
// - dbTransaction
|
699
|
+
// - userId: loginUser.UserId.
|
700
|
+
|
701
|
+
//Instantiate new activity
|
702
|
+
const activity = new Activity();
|
703
|
+
activity.ActivityId = activity.createId();
|
704
|
+
activity.Action = ActionEnum.DELETE;
|
705
|
+
activity.Description = 'Delete User System Access';
|
706
|
+
activity.EntityType = 'UserSystemAccess';
|
707
|
+
activity.EntityId = UserSystemAccessId?.toString();
|
708
|
+
activity.EntityValueBefore = JSON.stringify(entityValueBefore);
|
709
|
+
activity.EntityValueAfter = JSON.stringify({});
|
710
|
+
|
711
|
+
//Call Activity.create method
|
712
|
+
await activity.create(loginUser.ObjectId, dbTransaction);
|
713
|
+
} catch (error) {
|
714
|
+
throw error;
|
715
|
+
}
|
716
|
+
}
|
717
|
+
}
|