rez_core 2.1.31 → 2.1.32

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,8 +1,8 @@
1
1
  {
2
2
  "name": "rez_core",
3
- "version": "2.1.31",
3
+ "version": "2.1.32",
4
4
  "description": "",
5
- "author": "",
5
+ "author": "",
6
6
  "private": false,
7
7
  "license": "UNLICENSED",
8
8
  "scripts": {
@@ -4,11 +4,9 @@ import { UserData } from '../entity/user.entity';
4
4
  import { UserSessionService } from './user-session.service';
5
5
  import { UserService } from './user.service';
6
6
  import { ConfigService } from '@nestjs/config';
7
- import { UserAppMappingService } from 'src/module/meta/service/user-app-mapping.service';
8
- import { UserRoleMappingRepository } from '../repository/user-role-mapping.repository';
9
7
  import { InjectRepository } from '@nestjs/typeorm';
10
8
  import { UserRoleMapping } from '../entity/user-role-mapping.entity';
11
- import { Repository } from 'typeorm';
9
+ import { DataSource, Repository } from 'typeorm';
12
10
  import { Role } from '../entity/role.entity';
13
11
 
14
12
  @Injectable()
@@ -21,6 +19,7 @@ export class LoginService {
21
19
  private readonly userRoleMappingRepository: Repository<UserRoleMapping>,
22
20
  @InjectRepository(Role)
23
21
  private readonly roleRepo: Repository<Role>,
22
+ private readonly dataSource: DataSource,
24
23
  ) {}
25
24
 
26
25
  masterKey: string = this.configService.get('MASTER_KEY') || '';
@@ -59,58 +58,29 @@ export class LoginService {
59
58
  };
60
59
  }
61
60
 
62
- let defaultAccess;
63
- let appcode = user.last_app_access;
61
+ let appcode = user.last_app_access || 'ADM';
64
62
 
65
- if (!appcode) {
66
- appcode = 'ADM';
63
+ if (!user.last_app_access) {
67
64
  await this.userService.setDefaultLastAccess(user.id, appcode);
68
65
  }
69
66
 
70
- // ✅ Always fetch role mappings for the user + app
71
67
  const roleMappings = await this.userRoleMappingRepository.find({
72
- where: {
73
- user_id: user.id,
74
- appcode,
75
- },
68
+ where: { user_id: user.id, appcode },
76
69
  });
77
70
 
78
71
  if (!roleMappings.length) {
79
- throw new BadRequestException(
80
- 'User does not have any access mapped for this app.',
81
- );
72
+ throw new BadRequestException('User does not have any access mapped for this app.');
82
73
  }
83
74
 
84
- // 🔍 If user.last_level_type & level_id exist, validate them against URM
85
- const validURM = roleMappings.find(
86
- (r) =>
87
- r.level_type === user.last_level_type &&
88
- r.level_id === user.last_level_id,
75
+ const defaultAccess = await this.getDefaultAccess(user, roleMappings);
76
+
77
+ await this.userService.setLastLevelTypeAndId(
78
+ user.id,
79
+ defaultAccess.level_type,
80
+ defaultAccess.level_id,
89
81
  );
90
82
 
91
- if (user.last_level_type && user.last_level_id && validURM) {
92
- defaultAccess = {
93
- level_type: user.last_level_type,
94
- level_id: user.last_level_id,
95
- };
96
- } else {
97
- // ❌ Fallback: Pick default based on priority
98
- const priority = { org: 1, sch: 2, brn: 3 };
99
- defaultAccess = roleMappings.sort(
100
- (a, b) => priority[a.level_type] - priority[b.level_type],
101
- )[0];
102
-
103
- await this.userService.setLastLevelTypeAndId(
104
- user.id,
105
- defaultAccess.level_type,
106
- defaultAccess.level_id,
107
- );
108
- }
109
-
110
- const token = await this.userSessionService.createSession(user, appcode, {
111
- level_type: defaultAccess.level_type,
112
- level_id: defaultAccess.level_id,
113
- });
83
+ const token = await this.userSessionService.createSession(user, appcode, defaultAccess);
114
84
 
115
85
  if (user.is_firstlogin === 1) {
116
86
  user.invitation_status = 'ACCEPTED';
@@ -128,6 +98,52 @@ export class LoginService {
128
98
  };
129
99
  }
130
100
 
101
+ private async getDefaultAccess(
102
+ user: any,
103
+ roleMappings: any[],
104
+ ): Promise<{ level_type: string; level_id: string }> {
105
+ const orgAccess = roleMappings.find(r => r.level_type === 'ORG');
106
+ const brnAccesses = roleMappings.filter(r => r.level_type === 'BRN');
107
+ const schAccesses = roleMappings.filter(r => r.level_type === 'SCH');
108
+
109
+ const validURM = roleMappings.find(
110
+ r => r.level_type === user.last_level_type && r.level_id === user.last_level_id,
111
+ );
112
+
113
+ if (user.last_level_type && user.last_level_id && validURM) {
114
+ return { level_type: user.last_level_type, level_id: user.last_level_id };
115
+ }
116
+
117
+ if (orgAccess) {
118
+ return { level_type: 'ORG', level_id: orgAccess.level_id };
119
+ }
120
+
121
+ if (brnAccesses.length) {
122
+ const brandId = brnAccesses[0].level_id;
123
+
124
+ const rows = await this.dataSource.query(
125
+ `SELECT id FROM cr_school WHERE brand_id = ? ORDER BY id ASC LIMIT 1`,
126
+ [brandId],
127
+ );
128
+
129
+ if (rows.length) {
130
+ return { level_type: 'SCH', level_id: rows[0].id };
131
+ } else {
132
+ throw new BadRequestException(
133
+ 'User has BRN access but no schools found under that brand.',
134
+ );
135
+ }
136
+ }
137
+
138
+ if (schAccesses.length) {
139
+ return { level_type: 'SCH', level_id: schAccesses[0].level_id };
140
+ }
141
+
142
+ throw new BadRequestException(
143
+ 'User does not have ORG, BRN, or SCH access for this app.',
144
+ );
145
+ }
146
+
131
147
 
132
148
  async loginWithGoogle(email: string, name) {
133
149
  let user = await this.userService.findByEmailId(email);