rez_core 2.0.50 → 2.0.52

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": "rez_core",
3
- "version": "2.0.50",
3
+ "version": "2.0.52",
4
4
  "description": "",
5
5
  "author": "",
6
6
  "private": false,
@@ -13,6 +13,6 @@ import { SchoolData } from '../enterprise/entity/school.entity';
13
13
  ],
14
14
  providers: [LeadService, LeadRepository],
15
15
  controllers: [LeadController],
16
- exports: [],
16
+ exports: [LeadService],
17
17
  })
18
18
  export class LeadModule {}
@@ -12,7 +12,6 @@ import { LoginService } from '../service/login.service';
12
12
  import { GoogleAuthGuard } from '../../../module/auth/guards/google-auth.guard';
13
13
  import { JwtAuthGuard } from '../../auth/guards/jwt.guard';
14
14
  import { Request, Response } from 'express';
15
- import { JwtAuthService } from 'src/module/auth/services/jwt.service';
16
15
  import { UserSessionService } from '../service/user-session.service';
17
16
 
18
17
  @Controller('auth')
@@ -24,8 +23,8 @@ export class LoginController {
24
23
 
25
24
  @Post('login')
26
25
  async login(@Body() body, @Res() res: Response) {
27
- const { email_id, password, appcode } = body;
28
- const result = await this.loginService.login(email_id, password, appcode);
26
+ const { email_id, password, appcode, organization_id } = body;
27
+ const result = await this.loginService.login(email_id, password);
29
28
  return res.status(HttpStatus.OK).json(result);
30
29
  }
31
30
 
@@ -41,4 +41,7 @@ export class UserData extends BaseEntity {
41
41
 
42
42
  @Column({ type: 'int', nullable: true })
43
43
  is_factory: number;
44
+
45
+ @Column({ type: 'varchar', nullable: true })
46
+ last_app_access: string;
44
47
  }
@@ -18,17 +18,17 @@ export class LoginService {
18
18
  private userSessionService: UserSessionService,
19
19
  private configService: ConfigService,
20
20
  @InjectRepository(UserRoleMapping)
21
- private readonly userRoleMappingRepository: Repository<UserRoleMapping>,
22
- @InjectRepository(Role)
23
- private readonly roleRepo: Repository<Role>,
21
+ private readonly userRoleMappingRepository: Repository<UserRoleMapping>,
22
+ @InjectRepository(Role)
23
+ private readonly roleRepo: Repository<Role>,
24
24
  ) {}
25
25
 
26
26
  masterKey: string = this.configService.get('MASTER_KEY') || '';
27
27
  masterIv: string = this.configService.get('MASTER_IV') || '';
28
28
 
29
- async login(email: string, password: string, appcode: string) {
29
+ async login(email: string, password: string) {
30
30
  const user = await this.userService.findByEmailId(email);
31
-
31
+
32
32
  if (!user) {
33
33
  return {
34
34
  success: false,
@@ -36,7 +36,7 @@ export class LoginService {
36
36
  type: 'email',
37
37
  };
38
38
  }
39
-
39
+
40
40
  if (user.status !== 'ACTIVE') {
41
41
  return {
42
42
  success: false,
@@ -44,9 +44,13 @@ export class LoginService {
44
44
  type: 'email',
45
45
  };
46
46
  }
47
-
48
- const encryptedPassword = EncryptUtilService.encryptGCM(password, this.masterKey, this.masterIv);
49
-
47
+
48
+ const encryptedPassword = EncryptUtilService.encryptGCM(
49
+ password,
50
+ this.masterKey,
51
+ this.masterIv,
52
+ );
53
+
50
54
  if (encryptedPassword !== user.password) {
51
55
  return {
52
56
  success: false,
@@ -54,50 +58,57 @@ export class LoginService {
54
58
  type: 'password',
55
59
  };
56
60
  }
57
-
61
+
58
62
  // ✅ Get user-role mappings for this app
59
63
  const roleMappings = await this.userRoleMappingRepository.find({
60
64
  where: {
61
65
  user_id: user.id,
62
- appcode: appcode,
63
66
  },
64
67
  });
65
-
68
+
66
69
  if (!roleMappings.length) {
67
- throw new BadRequestException('User does not have any access mapped for this app.');
70
+ throw new BadRequestException(
71
+ 'User does not have any access mapped for this app.',
72
+ );
68
73
  }
69
-
74
+
70
75
  // ✅ Pick default access based on priority
71
76
  const priority = { org: 1, sch: 2, brn: 3 };
72
77
  const defaultAccess = roleMappings.sort(
73
78
  (a, b) => priority[a.level_type] - priority[b.level_type],
74
79
  )[0];
75
-
76
- // ✅ Manually fetch role_code from cr_role table
77
- const role = await this.roleRepo.findOne({
78
- where: { id: defaultAccess.role_id },
79
- });
80
-
81
- const token = await this.userSessionService.createSession(user, appcode, {
82
- level_type: defaultAccess.level_type,
83
- level_id: defaultAccess.level_id,
84
- role_id: defaultAccess.role_id,
85
- role_code: role?.code || null, // fallback if role not found
86
- });
87
-
80
+
81
+ //appcode
82
+
83
+ let appcode = user.last_app_access;
84
+
85
+ if (!appcode) {
86
+ appcode = 'ADM';
87
+ await this.userService.setDefaultLastAccess(user.id, appcode);
88
+ }
89
+
90
+ const token = await this.userSessionService.createSession(
91
+ user,
92
+ user.appcode,
93
+ {
94
+ level_type: defaultAccess.level_type,
95
+ level_id: defaultAccess.level_id,
96
+ },
97
+ );
98
+
88
99
  if (user.is_firstlogin === 1) {
89
100
  user.invitation_status = 'ACCEPTED';
90
101
  user.is_firstlogin = 0;
91
102
  const { password, ...userWithoutPassword } = user;
92
103
  await this.userService.updateEntity(userWithoutPassword, user);
93
104
  }
94
-
105
+
95
106
  return {
96
107
  success: true,
97
108
  accessToken: token,
109
+ appcode,
98
110
  };
99
111
  }
100
-
101
112
 
102
113
  async loginWithGoogle(email: string, name) {
103
114
  let user = await this.userService.findByEmailId(email);
@@ -38,8 +38,6 @@ export class UserSessionService {
38
38
  sessionToken,
39
39
  appcode,
40
40
  email_id: user.email_id,
41
- level_type: user.level_type,
42
- level_id: user.level_id,
43
41
  organization_id: user.organization_id,
44
42
  enterprise_id: user.enterprise_id,
45
43
  };
@@ -48,8 +46,6 @@ export class UserSessionService {
48
46
  if (accessInfo) {
49
47
  payload.level_type = accessInfo.level_type;
50
48
  payload.level_id = accessInfo.level_id;
51
- payload.role_id = accessInfo.role_id;
52
- payload.role_code = accessInfo.role_code;
53
49
  }
54
50
 
55
51
  const accessToken = this.jwtAuthService.generateJwt(payload);
@@ -98,6 +94,12 @@ export class UserSessionService {
98
94
  level_type: currentUserLevelType,
99
95
  appcode: data.appcode,
100
96
  };
97
+
98
+ await this.dataSource.query(
99
+ `UPDATE cr_user SET last_app_access = ? WHERE id = ?`,
100
+ [data.appcode, userId],
101
+ );
102
+
101
103
  // }
102
104
 
103
105
  // Step 2: Level switch logic
@@ -252,4 +252,16 @@ export class UserService extends EntityServiceImpl {
252
252
  appCode,
253
253
  );
254
254
  }
255
+
256
+ async setDefaultLastAccess(userId: number, appcode: string): Promise<void> {
257
+ const user = await this.userRepository.findById(userId);
258
+
259
+ if (!user) {
260
+ throw new BadRequestException('User not found');
261
+ }
262
+
263
+ user.last_app_access = appcode;
264
+
265
+ await this.userRepository.saveUser(user); // This persists the updated field
266
+ }
255
267
  }