@tomei/sso 0.28.6 → 0.29.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.28.6",
3
+ "version": "0.29.0",
4
4
  "description": "Tomei SSO Package",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -225,6 +225,7 @@ export class LoginUser extends LoginUserBase {
225
225
 
226
226
  async getDetails(): Promise<{
227
227
  FullName: string;
228
+ UserName: string;
228
229
  IDNo: string;
229
230
  IDType: string;
230
231
  Email: string;
@@ -232,6 +233,7 @@ export class LoginUser extends LoginUserBase {
232
233
  }> {
233
234
  return {
234
235
  FullName: this.FullName,
236
+ UserName: this.UserName,
235
237
  IDNo: this.IDNo,
236
238
  IDType: this.IDType,
237
239
  Email: this.Email,
@@ -338,6 +340,36 @@ export class LoginUser extends LoginUserBase {
338
340
  return new LoginUser(sessionService, dbTransaction);
339
341
  }
340
342
 
343
+ async setEmail(email: string, dbTransaction): Promise<void> {
344
+ try {
345
+ //Check if email is not the same as the current email if it is, skip all the steps
346
+ if (this.Email === email) {
347
+ return;
348
+ }
349
+
350
+ //Check if email is duplicated, if yes, throw error
351
+ const user = await LoginUser._Repository.findOne({
352
+ where: {
353
+ Email: email,
354
+ },
355
+ transaction: dbTransaction,
356
+ });
357
+
358
+ if (user) {
359
+ throw new ClassError(
360
+ 'LoginUser',
361
+ 'LoginUserErrMsg0X',
362
+ 'Email already exists',
363
+ );
364
+ }
365
+
366
+ //Update the email
367
+ this.Email = email;
368
+ } catch (error) {
369
+ throw error;
370
+ }
371
+ }
372
+
341
373
  async login(
342
374
  systemCode: string,
343
375
  email: string,
@@ -1910,4 +1942,396 @@ export class LoginUser extends LoginUserBase {
1910
1942
  );
1911
1943
  return `${userId}:${systemLogin.sessionId}`;
1912
1944
  }
1945
+
1946
+ public async addUserGroup(
1947
+ GroupCode: string,
1948
+ loginUser: LoginUser,
1949
+ dbTransaction: any,
1950
+ ) {
1951
+ // 1. Retrieve group data by calling LoginUser._GroupRepo.findOne with GroupCode
1952
+ const group = await LoginUser._GroupRepo.findOne({
1953
+ where: {
1954
+ GroupCode,
1955
+ },
1956
+ transaction: dbTransaction,
1957
+ });
1958
+
1959
+ // 2. If group data not found then return throw Class Error
1960
+ if (!group) {
1961
+ throw new ClassError(
1962
+ 'LoginUser',
1963
+ 'LoginUserErrMsg0X',
1964
+ 'Invalid Group Code',
1965
+ );
1966
+ }
1967
+
1968
+ //3. Create new UserGroup record
1969
+ const entityValueAfter = {
1970
+ UserId: this.UserId,
1971
+ GroupCode: group.GroupCode,
1972
+ CreatedAt: new Date(),
1973
+ CreatedById: loginUser.UserId,
1974
+ UpdatedAt: new Date(),
1975
+ UpdatedById: loginUser.UserId,
1976
+ };
1977
+ await LoginUser._UserGroupRepo.create(entityValueAfter, {
1978
+ transaction: dbTransaction,
1979
+ });
1980
+
1981
+ //4. Record Create UserGroup Activity
1982
+ const activity = new Activity();
1983
+ activity.ActivityId = activity.createId();
1984
+ activity.Action = ActionEnum.ADD;
1985
+ activity.Description = 'Add User Group';
1986
+ activity.EntityType = 'UserGroup';
1987
+ activity.EntityId = group.GroupCode;
1988
+ activity.EntityValueBefore = JSON.stringify({});
1989
+ activity.EntityValueAfter = JSON.stringify(entityValueAfter);
1990
+
1991
+ await activity.create(loginUser.ObjectId, dbTransaction);
1992
+ }
1993
+
1994
+ public async update(
1995
+ data: {
1996
+ UserName: string;
1997
+ Email: string;
1998
+ Status: UserStatus;
1999
+ RecoveryEmail: string;
2000
+ BuildingCode?: string;
2001
+ CompanyCode?: string;
2002
+ DepartmentCode?: string;
2003
+ },
2004
+ loginUser: LoginUser,
2005
+ dbTransaction: any,
2006
+ ) {
2007
+ //Part 1: Privilege Checking
2008
+ const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
2009
+ const isPrivileged = await loginUser.checkPrivileges(
2010
+ systemCode,
2011
+ 'User - Update',
2012
+ );
2013
+
2014
+ //If user does not have privilege to update user, throw a ClassError
2015
+ if (!isPrivileged) {
2016
+ throw new ClassError(
2017
+ 'LoginUser',
2018
+ 'LoginUserErrMsg0X',
2019
+ 'You do not have the privilege to update user',
2020
+ );
2021
+ }
2022
+
2023
+ //Part 2: Validation
2024
+ //Make sure UserId got values. If not, throw new ClassError
2025
+ if (!this.UserId) {
2026
+ throw new ClassError(
2027
+ 'LoginUser',
2028
+ 'LoginUserErrMsg0X',
2029
+ 'UserId is required',
2030
+ );
2031
+ }
2032
+
2033
+ //Make sure email is unique, call LoginUser.CheckUserInfoDuplicated method
2034
+ if (data.Email !== this.Email || data.UserName !== this.UserName) {
2035
+ await LoginUser.checkUserInfoDuplicated(dbTransaction, {
2036
+ Email: data.Email,
2037
+ UserName: data.UserName,
2038
+ });
2039
+ }
2040
+
2041
+ //Part 3: Update Building, Company, Department
2042
+ //If Params.BuildingCode is not null,
2043
+ if (data.BuildingCode) {
2044
+ //Check if BuildingCode is valid, call GroupModel.findOne method
2045
+ const building = await GroupModel.findOne({
2046
+ where: {
2047
+ Type: 'Building',
2048
+ GroupCode: data.BuildingCode,
2049
+ },
2050
+ transaction: dbTransaction,
2051
+ });
2052
+
2053
+ //If BuildingCode is invalid, throw new ClassError
2054
+ if (!building) {
2055
+ throw new ClassError(
2056
+ 'LoginUser',
2057
+ 'LoginUserErrMsg0X',
2058
+ 'Invalid Building Code',
2059
+ );
2060
+ }
2061
+
2062
+ //If BuildingCode is valid, call UserGroup.findOne method to find the user building record
2063
+ const userBuilding = await LoginUser._UserGroupRepo.findOne({
2064
+ where: {
2065
+ UserId: this.UserId,
2066
+ },
2067
+ include: [
2068
+ {
2069
+ model: GroupModel,
2070
+ where: {
2071
+ Type: 'Building',
2072
+ },
2073
+ },
2074
+ ],
2075
+ transaction: dbTransaction,
2076
+ });
2077
+
2078
+ //If user building record found, call UserGroup.update method to update the record if not found, call UserGroup.create method to create new record
2079
+ if (userBuilding) {
2080
+ await LoginUser._UserGroupRepo.update(
2081
+ {
2082
+ GroupCode: data.BuildingCode,
2083
+ },
2084
+ {
2085
+ where: {
2086
+ UserId: this.UserId,
2087
+ GroupCode: userBuilding.GroupCode,
2088
+ UpdatedAt: new Date(),
2089
+ UpdatedById: loginUser.UserId,
2090
+ },
2091
+ transaction: dbTransaction,
2092
+ },
2093
+ );
2094
+ } else {
2095
+ await LoginUser._UserGroupRepo.create(
2096
+ {
2097
+ UserId: this.UserId,
2098
+ GroupCode: data.BuildingCode,
2099
+ CreatedAt: new Date(),
2100
+ CreatedById: loginUser.UserId,
2101
+ UpdatedAt: new Date(),
2102
+ UpdatedById: loginUser.UserId,
2103
+ },
2104
+ {
2105
+ transaction: dbTransaction,
2106
+ },
2107
+ );
2108
+ }
2109
+ }
2110
+
2111
+ //If Params.CompanyCode is not null,
2112
+ if (data.CompanyCode) {
2113
+ //Check if CompanyCode is valid, call GroupModel.findOne method
2114
+ const company = await GroupModel.findOne({
2115
+ where: {
2116
+ Type: 'Company',
2117
+ GroupCode: data.CompanyCode,
2118
+ },
2119
+ transaction: dbTransaction,
2120
+ });
2121
+
2122
+ //If CompanyCode is invalid, throw a ClassError
2123
+ if (!company) {
2124
+ throw new ClassError(
2125
+ 'LoginUser',
2126
+ 'LoginUserErrMsg0X',
2127
+ 'Invalid Company Code',
2128
+ );
2129
+ }
2130
+
2131
+ //If CompanyCode is valid, call UserGroup.findOne method to find the user company record
2132
+ const userCompany = await LoginUser._UserGroupRepo.findOne({
2133
+ where: {
2134
+ UserId: this.UserId,
2135
+ },
2136
+ include: [
2137
+ {
2138
+ model: GroupModel,
2139
+ where: {
2140
+ Type: 'Company',
2141
+ },
2142
+ },
2143
+ ],
2144
+ transaction: dbTransaction,
2145
+ });
2146
+
2147
+ //If user company record found, call UserGroup.update method to update the record if not found, call UserGroup.create method to create new record
2148
+ if (userCompany) {
2149
+ await LoginUser._UserGroupRepo.update(
2150
+ {
2151
+ GroupCode: data.CompanyCode,
2152
+ },
2153
+ {
2154
+ where: {
2155
+ UserId: this.UserId,
2156
+ GroupCode: userCompany.GroupCode,
2157
+ UpdatedAt: new Date(),
2158
+ UpdatedById: loginUser.UserId,
2159
+ },
2160
+ transaction: dbTransaction,
2161
+ },
2162
+ );
2163
+ } else {
2164
+ await LoginUser._UserGroupRepo.create(
2165
+ {
2166
+ UserId: this.UserId,
2167
+ GroupCode: data.CompanyCode,
2168
+ CreatedAt: new Date(),
2169
+ CreatedById: loginUser.UserId,
2170
+ UpdatedAt: new Date(),
2171
+ UpdatedById: loginUser.UserId,
2172
+ },
2173
+ {
2174
+ transaction: dbTransaction,
2175
+ },
2176
+ );
2177
+ }
2178
+ }
2179
+
2180
+ //If Params.DepartmentCode is not null,
2181
+ if (data.DepartmentCode) {
2182
+ //Check if DepartmentCode is valid, call GroupModel.findOne method
2183
+ const department = await GroupModel.findOne({
2184
+ where: {
2185
+ Type: 'Department',
2186
+ GroupCode: data.DepartmentCode,
2187
+ },
2188
+ transaction: dbTransaction,
2189
+ });
2190
+
2191
+ //If DepartmentCode is invalid, throw a ClassError
2192
+ if (!department) {
2193
+ throw new ClassError(
2194
+ 'LoginUser',
2195
+ 'LoginUserErrMsg0X',
2196
+ 'Invalid Department Code',
2197
+ );
2198
+ }
2199
+
2200
+ //If DepartmentCode is valid, call UserGroup.findOne method to find the user department record
2201
+ const userDepartment = await LoginUser._UserGroupRepo.findOne({
2202
+ where: {
2203
+ UserId: this.UserId,
2204
+ },
2205
+ include: [
2206
+ {
2207
+ model: GroupModel,
2208
+ where: {
2209
+ Type: 'Department',
2210
+ },
2211
+ },
2212
+ ],
2213
+ transaction: dbTransaction,
2214
+ });
2215
+
2216
+ //If user department record found, call UserGroup.update method to update the record if not found, call UserGroup.create method to create new record
2217
+ if (userDepartment) {
2218
+ await LoginUser._UserGroupRepo.update(
2219
+ {
2220
+ GroupCode: data.DepartmentCode,
2221
+ },
2222
+ {
2223
+ where: {
2224
+ UserId: this.UserId,
2225
+ GroupCode: userDepartment.GroupCode,
2226
+ UpdatedAt: new Date(),
2227
+ UpdatedById: loginUser.UserId,
2228
+ },
2229
+ transaction: dbTransaction,
2230
+ },
2231
+ );
2232
+ } else {
2233
+ await LoginUser._UserGroupRepo.create(
2234
+ {
2235
+ UserId: this.UserId,
2236
+ GroupCode: data.DepartmentCode,
2237
+ CreatedAt: new Date(),
2238
+ CreatedById: loginUser.UserId,
2239
+ UpdatedAt: new Date(),
2240
+ UpdatedById: loginUser.UserId,
2241
+ },
2242
+ {
2243
+ transaction: dbTransaction,
2244
+ },
2245
+ );
2246
+ }
2247
+ }
2248
+
2249
+ //Part 4: Update User Record
2250
+ //Set EntityValueBefore
2251
+ const entityValueBefore = {
2252
+ UserId: this.UserId,
2253
+ UserName: this.UserName,
2254
+ Email: this.Email,
2255
+ Password: this.Password,
2256
+ Status: this.Status,
2257
+ DefaultPasswordChangedYN: this.DefaultPasswordChangedYN,
2258
+ FirstLoginAt: this.FirstLoginAt,
2259
+ LastLoginAt: this.LastLoginAt,
2260
+ MFAEnabled: this.MFAEnabled,
2261
+ MFAConfig: this.MFAConfig,
2262
+ RecoveryEmail: this.RecoveryEmail,
2263
+ FailedLoginAttemptCount: this.FailedLoginAttemptCount,
2264
+ LastFailedLoginAt: this.LastFailedLoginAt,
2265
+ LastPasswordChangedAt: this.LastPasswordChangedAt,
2266
+ NeedToChangePasswordYN: this.NeedToChangePasswordYN,
2267
+ CreatedById: this.CreatedById,
2268
+ CreatedAt: this.CreatedAt,
2269
+ UpdatedById: this.UpdatedById,
2270
+ UpdatedAt: this.UpdatedAt,
2271
+ };
2272
+
2273
+ //Update user record
2274
+ this.UserName = data.UserName;
2275
+ this.Email = data.Email;
2276
+ this.Status = data.Status;
2277
+ this.RecoveryEmail = data.RecoveryEmail;
2278
+ this.UpdatedAt = new Date();
2279
+ this.UpdatedById = loginUser.UserId;
2280
+ //Call LoginUser._Repo update method to update user record
2281
+ await LoginUser._Repository.update(
2282
+ {
2283
+ UserName: this.UserName,
2284
+ Email: this.Email,
2285
+ Status: this.Status,
2286
+ RecoveryEmail: this.RecoveryEmail,
2287
+ UpdatedById: this.UpdatedById,
2288
+ UpdatedAt: this.UpdatedAt,
2289
+ },
2290
+ {
2291
+ where: {
2292
+ UserId: this.UserId,
2293
+ },
2294
+ transaction: dbTransaction,
2295
+ },
2296
+ );
2297
+
2298
+ //Part 5: Record Update User Activity
2299
+ //Set EntityValueAfter
2300
+ const entityValueAfter = {
2301
+ UserId: this.UserId,
2302
+ UserName: this.UserName,
2303
+ Email: this.Email,
2304
+ Password: this.Password,
2305
+ Status: this.Status,
2306
+ DefaultPasswordChangedYN: this.DefaultPasswordChangedYN,
2307
+ FirstLoginAt: this.FirstLoginAt,
2308
+ LastLoginAt: this.LastLoginAt,
2309
+ MFAEnabled: this.MFAEnabled,
2310
+ MFAConfig: this.MFAConfig,
2311
+ RecoveryEmail: this.RecoveryEmail,
2312
+ FailedLoginAttemptCount: this.FailedLoginAttemptCount,
2313
+ LastFailedLoginAt: this.LastFailedLoginAt,
2314
+ LastPasswordChangedAt: this.LastPasswordChangedAt,
2315
+ NeedToChangePasswordYN: this.NeedToChangePasswordYN,
2316
+ CreatedById: this.CreatedById,
2317
+ CreatedAt: this.CreatedAt,
2318
+ UpdatedById: this.UpdatedById,
2319
+ UpdatedAt: this.UpdatedAt,
2320
+ };
2321
+
2322
+ //Call Activity.create method to create new activity record
2323
+ const activity = new Activity();
2324
+ activity.ActivityId = activity.createId();
2325
+ activity.Action = ActionEnum.UPDATE;
2326
+ activity.Description = 'Update User';
2327
+ activity.EntityType = 'LoginUser';
2328
+ activity.EntityId = this.UserId.toString();
2329
+ activity.EntityValueBefore = JSON.stringify(entityValueBefore);
2330
+ activity.EntityValueAfter = JSON.stringify(entityValueAfter);
2331
+
2332
+ await activity.create(loginUser.ObjectId, dbTransaction);
2333
+
2334
+ //Return Updated User Instance
2335
+ return this;
2336
+ }
1913
2337
  }