@uniorganization/uni-lib 4.3.4 → 5.0.1

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.
@@ -0,0 +1,23 @@
1
+ export interface DivisionReference {
2
+ id: string;
3
+ name: string;
4
+ }
5
+ export interface LobReference {
6
+ id: string;
7
+ name: string;
8
+ division: DivisionReference;
9
+ }
10
+ export interface DivisionLobScope {
11
+ division: DivisionReference | null;
12
+ lob: LobReference | null;
13
+ }
14
+ export declare function toLobReference(assignment: {
15
+ lob?: {
16
+ id: string;
17
+ name: string;
18
+ division?: {
19
+ id: string;
20
+ name: string;
21
+ } | null;
22
+ } | null;
23
+ }): LobReference | null;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toLobReference = toLobReference;
4
+ function toLobReference(assignment) {
5
+ const lob = assignment.lob;
6
+ const division = lob === null || lob === void 0 ? void 0 : lob.division;
7
+ if (!lob || !division)
8
+ return null;
9
+ return {
10
+ id: String(lob.id),
11
+ name: lob.name,
12
+ division: { id: String(division.id), name: division.name },
13
+ };
14
+ }
@@ -9,6 +9,7 @@ export * from './config.entity';
9
9
  export * from './depreciation-categories.entity';
10
10
  export * from './depreciation-details.entity';
11
11
  export * from './division.entity';
12
+ export * from './division-lob.types';
12
13
  export * from './detail-type.entity';
13
14
  export * from './lob.entity';
14
15
  export * from './users-lobs.entity';
@@ -25,6 +25,7 @@ __exportStar(require("./config.entity"), exports);
25
25
  __exportStar(require("./depreciation-categories.entity"), exports);
26
26
  __exportStar(require("./depreciation-details.entity"), exports);
27
27
  __exportStar(require("./division.entity"), exports);
28
+ __exportStar(require("./division-lob.types"), exports);
28
29
  __exportStar(require("./detail-type.entity"), exports);
29
30
  __exportStar(require("./lob.entity"), exports);
30
31
  __exportStar(require("./users-lobs.entity"), exports);
@@ -16,8 +16,6 @@ export declare class Users {
16
16
  is_active: number;
17
17
  password_expired: number;
18
18
  ntgid: string;
19
- lob: string;
20
- division: string;
21
19
  profile: string;
22
20
  viewEvaluations: boolean;
23
21
  ern: string;
@@ -27,5 +25,5 @@ export declare class Users {
27
25
  rawRoster: RawRoster;
28
26
  usersLobs: UsersLobs[];
29
27
  fullName: string;
30
- applyDerivedScope(): void;
28
+ applyCalculatedProperties(): void;
31
29
  }
@@ -16,17 +16,8 @@ const user_roles_entity_1 = require("./user-roles.entity");
16
16
  const raw_roster_entity_1 = require("./raw_roster.entity");
17
17
  const users_lobs_entity_1 = require("./users-lobs.entity");
18
18
  let Users = class Users {
19
- applyDerivedScope() {
20
- var _a, _b, _c, _d, _e;
19
+ applyCalculatedProperties() {
21
20
  this.fullName = `${this.first_name} ${this.last_name}`;
22
- const assignments = [...((_a = this.usersLobs) !== null && _a !== void 0 ? _a : [])]
23
- .filter((assignment) => { var _a; return Boolean((_a = assignment.lob) === null || _a === void 0 ? void 0 : _a.name); })
24
- .sort((left, right) => { var _a, _b, _c, _d; return ((_b = (_a = left.lob) === null || _a === void 0 ? void 0 : _a.name) !== null && _b !== void 0 ? _b : '').localeCompare((_d = (_c = right.lob) === null || _c === void 0 ? void 0 : _c.name) !== null && _d !== void 0 ? _d : ''); });
25
- this.lob = assignments
26
- .map((assignment) => { var _a; return (_a = assignment.lob) === null || _a === void 0 ? void 0 : _a.name; })
27
- .filter((name) => Boolean(name))
28
- .join(',');
29
- this.division = (_e = (_d = (_c = (_b = assignments[0]) === null || _b === void 0 ? void 0 : _b.lob) === null || _c === void 0 ? void 0 : _c.division) === null || _d === void 0 ? void 0 : _d.name) !== null && _e !== void 0 ? _e : '';
30
21
  }
31
22
  };
32
23
  exports.Users = Users;
@@ -121,7 +112,7 @@ __decorate([
121
112
  __metadata("design:type", Function),
122
113
  __metadata("design:paramtypes", []),
123
114
  __metadata("design:returntype", void 0)
124
- ], Users.prototype, "applyDerivedScope", null);
115
+ ], Users.prototype, "applyCalculatedProperties", null);
125
116
  exports.Users = Users = __decorate([
126
117
  (0, typeorm_1.Entity)('USERS')
127
118
  ], Users);
@@ -2,8 +2,6 @@ export declare class VwEscalationAge {
2
2
  clientId: number;
3
3
  riskLevel: string;
4
4
  category: string;
5
- division: string;
6
- lob: string;
7
5
  divisionId: string | null;
8
6
  lobId: string | null;
9
7
  escalationStatus: string;
@@ -26,14 +26,6 @@ __decorate([
26
26
  (0, typeorm_1.ViewColumn)(),
27
27
  __metadata("design:type", String)
28
28
  ], VwEscalationAge.prototype, "category", void 0);
29
- __decorate([
30
- (0, typeorm_1.ViewColumn)(),
31
- __metadata("design:type", String)
32
- ], VwEscalationAge.prototype, "division", void 0);
33
- __decorate([
34
- (0, typeorm_1.ViewColumn)(),
35
- __metadata("design:type", String)
36
- ], VwEscalationAge.prototype, "lob", void 0);
37
29
  __decorate([
38
30
  (0, typeorm_1.ViewColumn)({ name: 'division_id' }),
39
31
  __metadata("design:type", String)
@@ -35,7 +35,7 @@ let JwtStrategy = class JwtStrategy extends (0, passport_1.PassportStrategy)(pas
35
35
  }
36
36
  validate(payload) {
37
37
  return __awaiter(this, void 0, void 0, function* () {
38
- var _a, _b, _c;
38
+ var _a, _b, _c, _d;
39
39
  const signedUser = payload === null || payload === void 0 ? void 0 : payload.user;
40
40
  const effectiveClientId = Number(signedUser === null || signedUser === void 0 ? void 0 : signedUser.clientId);
41
41
  const originalClientId = (_a = signedUser === null || signedUser === void 0 ? void 0 : signedUser.originalClientId) !== null && _a !== void 0 ? _a : null;
@@ -80,8 +80,7 @@ let JwtStrategy = class JwtStrategy extends (0, passport_1.PassportStrategy)(pas
80
80
  first_name: identity.firstName,
81
81
  username: identity.username,
82
82
  last_name: identity.lastName,
83
- division: identity.division,
84
- lob: identity.lob,
83
+ lobIds: (_d = identity.lobIds) !== null && _d !== void 0 ? _d : [],
85
84
  fullName: identity.fullName,
86
85
  ntgid: identity.ntgid,
87
86
  profile: identity.profile,
@@ -18,7 +18,7 @@ function tokenIdentifier(value) {
18
18
  return value;
19
19
  }
20
20
  function tenantAuthorizationKey(clientId, userId) {
21
- return `client:${positiveInteger(clientId, 'clientId')}:authz:user:${positiveInteger(userId, 'userId')}`;
21
+ return `client:${positiveInteger(clientId, 'clientId')}:authz:v2:user:${positiveInteger(userId, 'userId')}`;
22
22
  }
23
23
  function revokedAccessTokenKey(jti) {
24
24
  return `security:access-token:revoked:${tokenIdentifier(jti)}`;
@@ -124,7 +124,7 @@ let TenantAuthorizationService = TenantAuthorizationService_1 = class TenantAuth
124
124
  }
125
125
  loadFromDatabase(userId, clientId) {
126
126
  return __awaiter(this, void 0, void 0, function* () {
127
- var _a, _b, _c;
127
+ var _a, _b, _c, _d;
128
128
  const [user, client, assignments, accessRows] = yield Promise.all([
129
129
  this.userRepository.findOne({
130
130
  where: { id: userId },
@@ -136,7 +136,7 @@ let TenantAuthorizationService = TenantAuthorizationService_1 = class TenantAuth
136
136
  this.userRoleRepository
137
137
  .createQueryBuilder('assignment')
138
138
  .innerJoinAndSelect('assignment.role', 'role', 'role.client_id = assignment.client_id')
139
- .innerJoinAndSelect('role.module', 'module', 'module.is_active = true')
139
+ .innerJoinAndSelect('role.module', 'module', 'module.is_active = true AND (module.client_id = assignment.client_id OR module.client_id IS NULL)')
140
140
  .leftJoinAndSelect('role.privileges', 'definition', 'definition.client_id = assignment.client_id')
141
141
  .leftJoinAndSelect('definition.privilege', 'privilege', 'privilege.client_id = assignment.client_id AND privilege."IS_ACTIVE" <> 0')
142
142
  .where('assignment."USER_ID" = :userId', { userId })
@@ -158,6 +158,10 @@ let TenantAuthorizationService = TenantAuthorizationService_1 = class TenantAuth
158
158
  JOIN stm."ACCESS_ROLES" role
159
159
  ON role.id = membership.role_id
160
160
  AND role.is_active
161
+ AND (
162
+ role.scope = 'GLOBAL'
163
+ OR role.client_id = membership.client_id
164
+ )
161
165
  LEFT JOIN stm."ACCESS_ROLE_PERMISSIONS" definition
162
166
  ON definition.role_id = role.id
163
167
  LEFT JOIN stm."ACCESS_PERMISSIONS" permission
@@ -168,7 +172,6 @@ let TenantAuthorizationService = TenantAuthorizationService_1 = class TenantAuth
168
172
  ]);
169
173
  if (user) {
170
174
  user.usersLobs = ((_a = user.usersLobs) !== null && _a !== void 0 ? _a : []).filter((assignment) => Number(assignment.clientId) === clientId);
171
- user.applyDerivedScope();
172
175
  }
173
176
  if (!user ||
174
177
  !Number(user.is_active) ||
@@ -236,8 +239,9 @@ let TenantAuthorizationService = TenantAuthorizationService_1 = class TenantAuth
236
239
  firstName: user.first_name,
237
240
  username: user.username,
238
241
  lastName: user.last_name,
239
- division: user.division,
240
- lob: user.lob,
242
+ lobIds: ((_d = user.usersLobs) !== null && _d !== void 0 ? _d : [])
243
+ .map((assignment) => String(assignment.lobId))
244
+ .sort(),
241
245
  fullName: user.fullName,
242
246
  ntgid: user.ntgid,
243
247
  profile: user.profile,
@@ -30,8 +30,7 @@ export interface TenantAuthorizationIdentity {
30
30
  firstName?: string;
31
31
  username?: string;
32
32
  lastName?: string;
33
- division?: string;
34
- lob?: string;
33
+ lobIds?: string[];
35
34
  fullName?: string;
36
35
  ntgid?: string;
37
36
  profile?: string;
@@ -35,8 +35,7 @@ export interface AuthenticatedTenantUser {
35
35
  first_name?: string;
36
36
  username?: string;
37
37
  last_name?: string;
38
- division?: string;
39
- lob?: string;
38
+ lobIds?: string[];
40
39
  fullName?: string;
41
40
  ntgid?: string;
42
41
  profile?: string;
@@ -0,0 +1,9 @@
1
+ import { DataSource, EntityManager } from 'typeorm';
2
+ import { DivisionLobScope } from '../entities/division-lob.types';
3
+ type QueryExecutor = Pick<DataSource | EntityManager, 'query'>;
4
+ export declare function validateDivisionLobIds(executor: QueryExecutor, clientId: number, divisionIdValue: unknown, lobIdValue: unknown): Promise<{
5
+ divisionId: string;
6
+ lobId: string;
7
+ }>;
8
+ export declare function loadDivisionLobScope(executor: QueryExecutor, clientId: number, divisionIdValue: unknown, lobIdValue: unknown): Promise<DivisionLobScope>;
9
+ export {};
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.validateDivisionLobIds = validateDivisionLobIds;
13
+ exports.loadDivisionLobScope = loadDivisionLobScope;
14
+ const common_1 = require("@nestjs/common");
15
+ function bigintId(value, field) {
16
+ const normalized = String(value !== null && value !== void 0 ? value : '');
17
+ if (!/^[1-9]\d*$/.test(normalized)) {
18
+ throw new common_1.BadRequestException(`${field} must be a positive bigint identifier`);
19
+ }
20
+ return normalized;
21
+ }
22
+ function validateDivisionLobIds(executor, clientId, divisionIdValue, lobIdValue) {
23
+ return __awaiter(this, void 0, void 0, function* () {
24
+ const divisionId = bigintId(divisionIdValue, 'divisionId');
25
+ const lobId = bigintId(lobIdValue, 'lobId');
26
+ const rows = yield executor.query(`SELECT lob.id
27
+ FROM stm.lob lob
28
+ JOIN stm.division division
29
+ ON division.id = lob.division_id
30
+ AND division.client_id = lob.client_id
31
+ WHERE lob.id = $1 AND division.id = $2 AND lob.client_id = $3
32
+ AND lob.deleted_at IS NULL AND division.deleted_at IS NULL`, [lobId, divisionId, clientId]);
33
+ if (!rows.length) {
34
+ throw new common_1.BadRequestException('Division and LOB are not active for this client');
35
+ }
36
+ return { divisionId, lobId };
37
+ });
38
+ }
39
+ function loadDivisionLobScope(executor, clientId, divisionIdValue, lobIdValue) {
40
+ return __awaiter(this, void 0, void 0, function* () {
41
+ if (divisionIdValue == null && lobIdValue == null) {
42
+ return { division: null, lob: null };
43
+ }
44
+ const divisionId = bigintId(divisionIdValue, 'divisionId');
45
+ const lobId = bigintId(lobIdValue, 'lobId');
46
+ const rows = yield executor.query(`SELECT division.id::text AS "divisionId", division.name AS "divisionName",
47
+ lob.id::text AS "lobId", lob.name AS "lobName"
48
+ FROM stm.division division
49
+ JOIN stm.lob lob ON lob.division_id = division.id AND lob.client_id = division.client_id
50
+ WHERE division.id = $1 AND lob.id = $2 AND division.client_id = $3`, [divisionId, lobId, clientId]);
51
+ if (!rows.length)
52
+ return { division: null, lob: null };
53
+ const row = rows[0];
54
+ const division = { id: String(row.divisionId), name: row.divisionName };
55
+ return {
56
+ division,
57
+ lob: { id: String(row.lobId), name: row.lobName, division },
58
+ };
59
+ });
60
+ }
@@ -2,3 +2,4 @@ export * from './object.utils';
2
2
  export * from './string.utils';
3
3
  export * from './parse.util';
4
4
  export * from './ticket-service-center-match-progress.util';
5
+ export * from './division-lob-catalog';
@@ -18,3 +18,4 @@ __exportStar(require("./object.utils"), exports);
18
18
  __exportStar(require("./string.utils"), exports);
19
19
  __exportStar(require("./parse.util"), exports);
20
20
  __exportStar(require("./ticket-service-center-match-progress.util"), exports);
21
+ __exportStar(require("./division-lob-catalog"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uniorganization/uni-lib",
3
- "version": "4.3.4",
3
+ "version": "5.0.1",
4
4
  "description": "UNI Library",
5
5
  "author": "Jhomiguel",
6
6
  "main": "dist/index.js",