rez_core 2.0.97 → 2.0.99

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.97",
3
+ "version": "2.0.99",
4
4
  "description": "",
5
5
  "author": "",
6
6
  "private": false,
@@ -89,7 +89,6 @@
89
89
  "js",
90
90
  "json",
91
91
  "ts"
92
-
93
92
  ],
94
93
  "rootDir": "src",
95
94
  "testRegex": ".*\\.spec\\.ts$",
@@ -6,8 +6,9 @@ import { UserRoleMapping } from 'src/module/user/entity/user-role-mapping.entity
6
6
 
7
7
  @Injectable()
8
8
  export class MenuService {
9
- constructor(private readonly menuRepository: MenuRepository,
10
- private readonly datasource:DataSource
9
+ constructor(
10
+ private readonly menuRepository: MenuRepository,
11
+ private readonly datasource: DataSource,
11
12
  ) {}
12
13
 
13
14
  /**
@@ -20,25 +21,34 @@ export class MenuService {
20
21
  levelId: number,
21
22
  ): Promise<{ menu: any[] }> {
22
23
  // Step 1: Resolve roles (with fallback logic inside)
23
- const roleCodes = await this.menuRepository.resolveUserRoles(userId, appcode, levelType, levelId);
24
- console.log(roleCodes)
24
+ const roleCodes = await this.menuRepository.resolveUserRoles(
25
+ userId,
26
+ appcode,
27
+ levelType,
28
+ levelId,
29
+ );
25
30
  if (!roleCodes.length) return { menu: [] };
26
-
31
+
27
32
  // Step 2: Get accessible modules
28
- const moduleCodes = await this.menuRepository.getAccessibleModules(roleCodes, appcode);
29
- console.log(moduleCodes)
33
+ const moduleCodes = await this.menuRepository.getAccessibleModules(
34
+ roleCodes,
35
+ appcode,
36
+ );
30
37
  if (!moduleCodes.length) return { menu: [] };
31
-
38
+
32
39
  // Step 3: Get menu items for the given level type
33
- const menuItems = await this.menuRepository.getMenuItems(moduleCodes, appcode, levelType);
34
- console.log(menuItems)
35
-
40
+ const menuItems = await this.menuRepository.getMenuItems(
41
+ moduleCodes,
42
+ appcode,
43
+ levelType,
44
+ );
45
+
36
46
  // Step 4: Build hierarchy
37
47
  const menuTree = this.buildMenuHierarchy(menuItems);
38
-
48
+
39
49
  return { menu: menuTree };
40
50
  }
41
-
51
+
42
52
  /**
43
53
  * Converts a flat list of menu items into a hierarchical tree.
44
54
  */
@@ -69,8 +79,4 @@ export class MenuService {
69
79
  async getUserRoles(userId: number, appcode: string): Promise<any> {
70
80
  return this.menuRepository.getUserRoles(userId, appcode);
71
81
  }
72
-
73
-
74
-
75
-
76
82
  }
@@ -1,4 +1,5 @@
1
1
  import {
2
+ BadRequestException,
2
3
  Body,
3
4
  Controller,
4
5
  Get,
@@ -68,6 +69,45 @@ export class LoginController {
68
69
  },
69
70
  ): Promise<any> {
70
71
  const currentUser = req.user.userData;
72
+ const currentAppcode = currentUser.appcode;
73
+
74
+ console.log(currentUser, 'DEBUG USER', data);
75
+
76
+ // If appcode is changing, validate the new level_id/level_type
77
+ if (data.appcode !== currentAppcode) {
78
+ console.log('inside if', data.appcode, currentAppcode);
79
+ const isValidAccess = await this.userSessionService.checkIfUserHasMapping(
80
+ currentUser.id,
81
+ data.appcode,
82
+ data.level_type,
83
+ data.level_id,
84
+ );
85
+
86
+ console.log('isValidAccess', isValidAccess);
87
+
88
+ if (!isValidAccess) {
89
+ // If not valid, fetch the default one
90
+ const userMapping =
91
+ await this.userSessionService.getUserRoleMappingForApp(
92
+ currentUser.id,
93
+ data.appcode,
94
+ );
95
+
96
+ if (!userMapping) {
97
+ throw new BadRequestException(
98
+ `No mapping found for user in app ${data.appcode}`,
99
+ );
100
+ }
101
+
102
+ data.level_type = userMapping.level_type;
103
+ data.level_id = userMapping.level_id;
104
+ }
105
+ console.log(
106
+ data.level_type,
107
+ data.level_id,
108
+ 'data.level_type, data.level_id',
109
+ );
110
+ }
71
111
 
72
112
  return await this.userSessionService.switchCurrentLevelService(
73
113
  currentUser,
@@ -6,6 +6,7 @@ import { JwtAuthService } from 'src/module/auth/services/jwt.service';
6
6
  import { ConfigService } from '@nestjs/config';
7
7
  import { UserRoleMappingService } from './user-role-mapping.service';
8
8
  import { DataSource } from 'typeorm';
9
+ import { UserRoleMapping } from '../entity/user-role-mapping.entity';
9
10
 
10
11
  @Injectable()
11
12
  export class UserSessionService {
@@ -96,4 +97,31 @@ export class UserSessionService {
96
97
  level_id: data.level_id,
97
98
  };
98
99
  }
100
+
101
+ async checkIfUserHasMapping(
102
+ userId: number,
103
+ appcode: string,
104
+ levelType: string,
105
+ levelId: string,
106
+ ): Promise<boolean> {
107
+ const count = await this.dataSource
108
+ .getRepository(UserRoleMapping)
109
+ .createQueryBuilder('cr_user_role_mapping')
110
+ .where('cr_user_role_mapping.user_id = :userId', { userId })
111
+ .andWhere('cr_user_role_mapping.appcode = :appcode', { appcode })
112
+ .andWhere('cr_user_role_mapping.level_type = :levelType', { levelType })
113
+ .andWhere('cr_user_role_mapping.level_id = :levelId', { levelId })
114
+ .getCount();
115
+
116
+ return count > 0;
117
+ }
118
+
119
+ async getUserRoleMappingForApp(userId: number, appcode: string) {
120
+ return await this.dataSource
121
+ .getRepository(UserRoleMapping)
122
+ .createQueryBuilder('cr_user_role_mapping')
123
+ .where('cr_user_role_mapping.user_id = :userId', { userId })
124
+ .andWhere('cr_user_role_mapping.appcode = :appcode', { appcode })
125
+ .getOne(); // You can return .getMany() if you want to let user choose
126
+ }
99
127
  }