@tomei/sso 0.29.3 → 0.30.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tomei/sso",
3
- "version": "0.29.3",
3
+ "version": "0.30.0",
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,101 @@ 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(newSystemPrivilege, dbTransaction);
168
+
169
+ //Part 4: Record Create Privilege Activity
170
+ //Initialise EntityValueBefore variable and set to empty object.
171
+ const EntityValueBefore = {};
172
+ //Initialise EntityValueAfter variable and set to newSystemPrivilege object.
173
+ const EntityValueAfter = newSystemPrivilege;
174
+
175
+ //Instantiate new activity object and populate
176
+ const activity = new Activity();
177
+ activity.ActivityId = activity.createId();
178
+ activity.Action = ActionEnum.ADD;
179
+ activity.Description = 'Add System Privilege';
180
+ activity.EntityType = 'SystemPrivilege';
181
+ activity.EntityId = newSystemPrivilege.SystemCode;
182
+ activity.EntityValueBefore = JSON.stringify(EntityValueBefore);
183
+ activity.EntityValueAfter = JSON.stringify(EntityValueAfter);
184
+
185
+ //Call Activity.create method
186
+ await activity.create(loginUser.ObjectId, dbTransaction);
187
+
188
+ return newSystemPrivilege;
189
+ } catch (error) {
190
+ throw error;
191
+ }
192
+ }
92
193
  }