@tomei/sso 0.30.0 → 0.31.0

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tomei/sso",
3
- "version": "0.30.0",
3
+ "version": "0.31.0",
4
4
  "description": "Tomei SSO Package",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -1,7 +1,12 @@
1
- import { ClassError, HashTable, ObjectBase } from '@tomei/general';
1
+ import { ClassError, ObjectBase } from '@tomei/general';
2
2
  import { GroupRepository } from './group.repository';
3
3
  import { IGroupAttr } from '../../interfaces/group.interface';
4
4
  import { GroupTypeEnum } from 'enum';
5
+ import { LoginUser } from '../login-user/login-user';
6
+ import { IGroupSearchAttr } from '../../interfaces/group-search-attr.interface';
7
+ import { ApplicationConfig } from '@tomei/config';
8
+ import { Op } from 'sequelize';
9
+ import { ActionEnum, Activity } from '@tomei/activity-history';
5
10
 
6
11
  export class Group extends ObjectBase {
7
12
  ObjectId: string;
@@ -9,7 +14,6 @@ export class Group extends ObjectBase {
9
14
  TableName: 'sso_Group';
10
15
  ObjectType = 'Group';
11
16
 
12
- GroupCode: string;
13
17
  Name: string;
14
18
  Description: string;
15
19
  Type: GroupTypeEnum;
@@ -23,6 +27,14 @@ export class Group extends ObjectBase {
23
27
  private _UpdatedAt: Date;
24
28
  private static _Repo = new GroupRepository();
25
29
 
30
+ get GroupCode(): string {
31
+ return this.ObjectId;
32
+ }
33
+
34
+ set GroupCode(value: string) {
35
+ this.ObjectId = value;
36
+ }
37
+
26
38
  get CreatedById(): number {
27
39
  return this._CreatedById;
28
40
  }
@@ -78,4 +90,194 @@ export class Group extends ObjectBase {
78
90
  );
79
91
  }
80
92
  }
93
+
94
+ public static async findAll(
95
+ page: number,
96
+ row: number,
97
+ dbTransaction: any,
98
+ loginUser: LoginUser,
99
+ search?: IGroupSearchAttr,
100
+ ) {
101
+ //This method will list all group based on the query params.
102
+ //Part 1: Privilege Checking
103
+ const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
104
+ const isPrivileged = await loginUser.checkPrivileges(
105
+ systemCode,
106
+ 'GROUP_LIST',
107
+ );
108
+
109
+ if (!isPrivileged) {
110
+ throw new ClassError(
111
+ 'Group',
112
+ 'GroupErrMsg04',
113
+ 'User is not privileged to list group',
114
+ );
115
+ }
116
+
117
+ //Part 2: Retrieve listing
118
+ const queryObj: any = {};
119
+
120
+ let options: any = {
121
+ transaction: dbTransaction,
122
+ };
123
+
124
+ if (page && row) {
125
+ options = {
126
+ ...options,
127
+ limit: row,
128
+ offset: row * (page - 1),
129
+ order: [['CreatedAt', 'DESC']],
130
+ };
131
+ }
132
+
133
+ if (search) {
134
+ Object.entries(search).forEach(([key, value]) => {
135
+ queryObj[key] = {
136
+ [Op.substring]: value,
137
+ };
138
+ });
139
+
140
+ options = {
141
+ ...options,
142
+ where: queryObj,
143
+ };
144
+ return await Group._Repo.findAllWithPagination(options);
145
+ }
146
+ }
147
+
148
+ public static async create(
149
+ loginUser: LoginUser,
150
+ dbTransaction: any,
151
+ group: Group,
152
+ ) {
153
+ try {
154
+ //Part 1: Privilege Checking
155
+ const systemCode =
156
+ ApplicationConfig.getComponentConfigValue('system-code');
157
+ const isPrivileged = await loginUser.checkPrivileges(
158
+ systemCode,
159
+ 'GROUP_CREATE',
160
+ );
161
+ if (!isPrivileged) {
162
+ throw new Error('You do not have permission to create group');
163
+ }
164
+
165
+ //Part 2: Validation
166
+ if (!group.GroupCode) {
167
+ throw new ClassError(
168
+ 'Group',
169
+ 'GroupErrMsg02',
170
+ 'Group Code is required',
171
+ );
172
+ }
173
+
174
+ if (!group.Name) {
175
+ throw new ClassError(
176
+ 'Group',
177
+ 'GroupErrMsg02',
178
+ 'Group Name is required',
179
+ );
180
+ }
181
+
182
+ if (!group.Type) {
183
+ throw new ClassError(
184
+ 'Group',
185
+ 'GroupErrMsg02',
186
+ 'Group Type is required',
187
+ );
188
+ }
189
+
190
+ //Check if group code is unique
191
+ const existingGroupCode = await Group._Repo.findByPk(group.GroupCode, {
192
+ transaction: dbTransaction,
193
+ });
194
+
195
+ if (existingGroupCode) {
196
+ throw new ClassError(
197
+ 'Group',
198
+ 'GroupErrMsg03',
199
+ 'Duplicate GroupCode found.',
200
+ );
201
+ }
202
+
203
+ //Validate parent group code if passed. Call Group._Repo.findByPk
204
+ if (group.ParentGroupCode) {
205
+ const parentGroup = await Group._Repo.findByPk(group.ParentGroupCode, {
206
+ transaction: dbTransaction,
207
+ });
208
+
209
+ if (!parentGroup) {
210
+ throw new ClassError(
211
+ 'Group',
212
+ 'GroupErrMsg04',
213
+ 'ParentGroupCode is not found.',
214
+ );
215
+ }
216
+
217
+ //If Params.group.GroupCode = Params.group?.ParentGroupCode, throw new ClassError
218
+ if (group.GroupCode === group.ParentGroupCode) {
219
+ throw new ClassError(
220
+ 'Group',
221
+ 'GroupErrMsg05',
222
+ 'GroupCode and ParentGroupCode cannot be the same.',
223
+ );
224
+ }
225
+ }
226
+
227
+ //Part 3: Create Group
228
+ //Initialise new Group instance and populate
229
+ const newGroup = new Group(group);
230
+ newGroup.ObjectId = group.GroupCode;
231
+ newGroup.Name = group.Name;
232
+ newGroup.Type = group.Type;
233
+ newGroup.Description = group.Description;
234
+ newGroup.ParentGroupCode = group.ParentGroupCode;
235
+ newGroup.InheritParentPrivilegeYN = group.InheritParentPrivilegeYN;
236
+ newGroup.InheritParentSystemAccessYN = group.InheritParentSystemAccessYN;
237
+ newGroup.Status = 'Active';
238
+ newGroup._CreatedById = loginUser.UserId;
239
+ newGroup._UpdatedById = loginUser.UserId;
240
+
241
+ //Call Group._Repo create method
242
+ const entityGroupAfter = {
243
+ GroupCode: newGroup.ObjectId,
244
+ Name: newGroup.Name,
245
+ Type: newGroup.Type,
246
+ Description: newGroup.Description,
247
+ ParentGroupCode: newGroup.ParentGroupCode,
248
+ InheritParentPrivilegeYN: newGroup.InheritParentPrivilegeYN,
249
+ InheritParentSystemAccessYN: newGroup.InheritParentSystemAccessYN,
250
+ Status: newGroup.Status,
251
+ CreatedById: newGroup._CreatedById,
252
+ UpdatedById: newGroup._UpdatedById,
253
+ CreatedAt: newGroup._CreatedAt,
254
+ UpdatedAt: newGroup._UpdatedAt,
255
+ };
256
+
257
+ await Group._Repo.create(entityGroupAfter, {
258
+ transaction: dbTransaction,
259
+ });
260
+
261
+ //Part 4: Record Create Group Activity and return newGroup
262
+
263
+ const entityValueBefore = {};
264
+
265
+ //Instantiate new activity
266
+ const activity = new Activity();
267
+ activity.ActivityId = activity.createId();
268
+ activity.Action = ActionEnum.ADD;
269
+ activity.Description = 'Create Group';
270
+ activity.EntityType = 'Group';
271
+ activity.EntityId = newGroup.ObjectId;
272
+ activity.EntityValueBefore = JSON.stringify(entityValueBefore);
273
+ activity.EntityValueAfter = JSON.stringify(entityGroupAfter);
274
+
275
+ //Call Activity.create method
276
+ await activity.create(loginUser.ObjectId, dbTransaction);
277
+
278
+ return newGroup;
279
+ } catch (error) {
280
+ throw error;
281
+ }
282
+ }
81
283
  }
@@ -11,5 +11,6 @@ export * from './group-system-access';
11
11
  export * from './user-object-privilege';
12
12
  export * from './group-privilege';
13
13
  export * from './group-object-privilege';
14
+ export * from './system-privilege';
14
15
 
15
16
  //test ci
@@ -0,0 +1,4 @@
1
+ import { SystemPrivilegeRepository } from './system-privilege.repository';
2
+ import { SystemPrivilege } from './system-privilege';
3
+
4
+ export { SystemPrivilegeRepository, SystemPrivilege };
@@ -164,7 +164,19 @@ export class SystemPrivilege extends ObjectBase {
164
164
  newSystemPrivilege._UpdatedAt = new Date();
165
165
 
166
166
  //Call SystemPrivilege._Repo create method
167
- await this._Repository.create(newSystemPrivilege, dbTransaction);
167
+ await this._Repository.create(
168
+ {
169
+ PrivilegeCode: newSystemPrivilege.ObjectId,
170
+ SystemCode: newSystemPrivilege.SystemCode,
171
+ Description: newSystemPrivilege.Description,
172
+ Status: newSystemPrivilege.Status,
173
+ CreatedById: newSystemPrivilege._CreatedById,
174
+ UpdatedById: newSystemPrivilege._UpdatedById,
175
+ CreatedAt: newSystemPrivilege._CreatedAt,
176
+ UpdatedAt: newSystemPrivilege._UpdatedAt,
177
+ },
178
+ dbTransaction,
179
+ );
168
180
 
169
181
  //Part 4: Record Create Privilege Activity
170
182
  //Initialise EntityValueBefore variable and set to empty object.
@@ -0,0 +1,8 @@
1
+ import { GroupTypeEnum } from '../enum/group-type.enum';
2
+
3
+ export interface IGroupSearchAttr {
4
+ GroupCode?: string;
5
+ Name?: string;
6
+ Type?: string;
7
+ Status?: GroupTypeEnum;
8
+ }
@@ -9,3 +9,4 @@ export * from './user-object-privilege.interface';
9
9
  export * from './group-object-privilege.interface';
10
10
  export * from './group-privilege.interface';
11
11
  export * from './system-search-attr.interface';
12
+ export * from './group-search-attr.interface';