@tomei/sso 0.29.3 → 0.30.1

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.29.3",
3
+ "version": "0.30.1",
4
4
  "description": "Tomei SSO Package",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -2,6 +2,10 @@ import { ClassError, ObjectBase } from '@tomei/general';
2
2
  import { SystemRepository } from '../system/system.repository';
3
3
  import { SystemPrivilegeRepository } from './system-privilege.repository';
4
4
  import { ISystemPrivilegeAttr } from '../../interfaces/system-privilege.interface';
5
+ import { LoginUser } from '../login-user/login-user';
6
+ import { ApplicationConfig } from '@tomei/config';
7
+ import { System } from '../system/system';
8
+ import { ActionEnum, Activity } from '@tomei/activity-history';
5
9
 
6
10
  export class SystemPrivilege extends ObjectBase {
7
11
  ObjectType = 'SystemPrivilege';
@@ -89,4 +93,113 @@ export class SystemPrivilege extends ObjectBase {
89
93
  throw error;
90
94
  }
91
95
  }
96
+
97
+ static async create(
98
+ loginUser: LoginUser,
99
+ dbTransaction: any,
100
+ systemPrivilege: SystemPrivilege,
101
+ ) {
102
+ try {
103
+ //Part 1: Privilege Checking
104
+ const systemCode =
105
+ ApplicationConfig.getComponentConfigValue('system-code');
106
+ const isPrivileged = await loginUser.checkPrivileges(
107
+ systemCode,
108
+ 'PRIVILEGE_CREATE',
109
+ );
110
+ if (!isPrivileged) {
111
+ throw new Error(
112
+ 'You do not have permission to create system privilege',
113
+ );
114
+ }
115
+
116
+ //Part 2: Validation
117
+ //Make sure systemCode and PrivilegeCode are not empty
118
+ if (!systemPrivilege.SystemCode) {
119
+ throw new ClassError(
120
+ 'SystemPrivilege',
121
+ 'SystemPrivilegeErrMsg02',
122
+ 'System Code is required',
123
+ );
124
+ }
125
+
126
+ if (!systemPrivilege.PrivilegeCode) {
127
+ throw new ClassError(
128
+ 'SystemPrivilege',
129
+ 'SystemPrivilegeErrMsg02',
130
+ 'Privilege Code is required',
131
+ );
132
+ }
133
+
134
+ //Call System.init() method by passing systemCode
135
+ await System.init(dbTransaction, systemPrivilege.SystemCode);
136
+
137
+ //Call SystemPrivilege._Repo findByPk
138
+ const existingSystemPrivilege = await this._Repository.findByPk(
139
+ systemPrivilege.PrivilegeCode,
140
+ {
141
+ transaction: dbTransaction,
142
+ },
143
+ );
144
+
145
+ //If PrivilegeCode found, throw new ClassError
146
+ if (existingSystemPrivilege) {
147
+ throw new ClassError(
148
+ 'SystemPrivilege',
149
+ 'SystemPrivilegeErrMsg03',
150
+ 'System Privilege already exists',
151
+ );
152
+ }
153
+
154
+ //Part 3: Create Privilege
155
+ //Initialise new SystemPrivilege instance and populate below
156
+ const newSystemPrivilege = new SystemPrivilege();
157
+ newSystemPrivilege.ObjectId = systemPrivilege.PrivilegeCode;
158
+ newSystemPrivilege.SystemCode = systemPrivilege.SystemCode;
159
+ newSystemPrivilege.Description = systemPrivilege.Description;
160
+ newSystemPrivilege.Status = 'Active';
161
+ newSystemPrivilege._CreatedById = loginUser.UserId;
162
+ newSystemPrivilege._UpdatedById = loginUser.UserId;
163
+ newSystemPrivilege._CreatedAt = new Date();
164
+ newSystemPrivilege._UpdatedAt = new Date();
165
+
166
+ //Call SystemPrivilege._Repo create method
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
+ );
180
+
181
+ //Part 4: Record Create Privilege Activity
182
+ //Initialise EntityValueBefore variable and set to empty object.
183
+ const EntityValueBefore = {};
184
+ //Initialise EntityValueAfter variable and set to newSystemPrivilege object.
185
+ const EntityValueAfter = newSystemPrivilege;
186
+
187
+ //Instantiate new activity object and populate
188
+ const activity = new Activity();
189
+ activity.ActivityId = activity.createId();
190
+ activity.Action = ActionEnum.ADD;
191
+ activity.Description = 'Add System Privilege';
192
+ activity.EntityType = 'SystemPrivilege';
193
+ activity.EntityId = newSystemPrivilege.SystemCode;
194
+ activity.EntityValueBefore = JSON.stringify(EntityValueBefore);
195
+ activity.EntityValueAfter = JSON.stringify(EntityValueAfter);
196
+
197
+ //Call Activity.create method
198
+ await activity.create(loginUser.ObjectId, dbTransaction);
199
+
200
+ return newSystemPrivilege;
201
+ } catch (error) {
202
+ throw error;
203
+ }
204
+ }
92
205
  }