rez_core 2.0.45 → 2.0.47

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.
Files changed (39) hide show
  1. package/dist/module/enterprise/controller/organization.controller.js +0 -1
  2. package/dist/module/enterprise/controller/organization.controller.js.map +1 -1
  3. package/dist/module/enterprise/entity/brand.entity.d.ts +1 -0
  4. package/dist/module/enterprise/entity/brand.entity.js +7 -0
  5. package/dist/module/enterprise/entity/brand.entity.js.map +1 -1
  6. package/dist/module/enterprise/entity/organization.entity.d.ts +1 -0
  7. package/dist/module/enterprise/entity/organization.entity.js +4 -0
  8. package/dist/module/enterprise/entity/organization.entity.js.map +1 -1
  9. package/dist/module/enterprise/entity/school.entity.d.ts +1 -0
  10. package/dist/module/enterprise/entity/school.entity.js +4 -0
  11. package/dist/module/enterprise/entity/school.entity.js.map +1 -1
  12. package/dist/module/enterprise/repository/school.repository.js +5 -1
  13. package/dist/module/enterprise/repository/school.repository.js.map +1 -1
  14. package/dist/module/user/controller/login.controller.js +1 -1
  15. package/dist/module/user/controller/login.controller.js.map +1 -1
  16. package/dist/module/user/repository/role.repository.d.ts +1 -0
  17. package/dist/module/user/repository/role.repository.js +12 -1
  18. package/dist/module/user/repository/role.repository.js.map +1 -1
  19. package/dist/module/user/service/login.service.d.ts +6 -3
  20. package/dist/module/user/service/login.service.js +32 -10
  21. package/dist/module/user/service/login.service.js.map +1 -1
  22. package/dist/module/user/service/role.service.d.ts +6 -0
  23. package/dist/module/user/service/role.service.js +10 -2
  24. package/dist/module/user/service/role.service.js.map +1 -1
  25. package/dist/module/user/service/user-session.service.d.ts +1 -1
  26. package/dist/module/user/service/user-session.service.js +8 -2
  27. package/dist/module/user/service/user-session.service.js.map +1 -1
  28. package/dist/tsconfig.build.tsbuildinfo +1 -1
  29. package/package.json +1 -1
  30. package/src/module/enterprise/controller/organization.controller.ts +0 -2
  31. package/src/module/enterprise/entity/brand.entity.ts +5 -2
  32. package/src/module/enterprise/entity/organization.entity.ts +3 -0
  33. package/src/module/enterprise/entity/school.entity.ts +3 -0
  34. package/src/module/enterprise/repository/school.repository.ts +5 -1
  35. package/src/module/user/controller/login.controller.ts +1 -1
  36. package/src/module/user/repository/role.repository.ts +18 -8
  37. package/src/module/user/service/login.service.ts +48 -29
  38. package/src/module/user/service/role.service.ts +21 -6
  39. package/src/module/user/service/user-session.service.ts +19 -8
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rez_core",
3
- "version": "2.0.45",
3
+ "version": "2.0.47",
4
4
  "description": "",
5
5
  "author": "",
6
6
  "private": false,
@@ -27,8 +27,6 @@ export class OrganizationController {
27
27
  const userId = req.user.userData?.id;
28
28
  const appcode = req.user.userData?.appcode;
29
29
 
30
- console.log(userId, appcode, 'userId, appcode');
31
-
32
30
  return await this.orgService.getOrganizationHierarchy(userId, appcode);
33
31
  }
34
32
  }
@@ -1,5 +1,8 @@
1
1
  import { BaseEntity } from 'src/module/meta/entity/base-entity.entity';
2
- import { Entity } from 'typeorm';
2
+ import { Column, Entity } from 'typeorm';
3
3
 
4
4
  @Entity({ name: 'cr_brand' })
5
- export class BrandData extends BaseEntity {}
5
+ export class BrandData extends BaseEntity {
6
+ @Column({ name: 'type', nullable: true })
7
+ type: string;
8
+ }
@@ -80,4 +80,7 @@ export class OrganizationData {
80
80
 
81
81
  @Column({ name: 'is_brand', type: 'boolean', nullable: true })
82
82
  is_brand: boolean;
83
+
84
+ @Column({ name: 'type', nullable: true })
85
+ type: string;
83
86
  }
@@ -11,4 +11,7 @@ export class SchoolData extends BaseEntity {
11
11
 
12
12
  @Column({ name: 'location', nullable: true })
13
13
  location: string;
14
+
15
+ @Column({ name: 'type', nullable: true })
16
+ type: string;
14
17
  }
@@ -35,7 +35,6 @@ export class SchoolRepository {
35
35
  .andWhere('urm.level_type = :levelType', { levelType: 'ORG' })
36
36
  .getRawMany();
37
37
 
38
- console.log(hasOrgAccess);
39
38
  if (hasOrgAccess.length > 0) {
40
39
  // Return nested Org -> Brand -> School structure
41
40
  const schools = await this.dataSource
@@ -74,6 +73,7 @@ export class SchoolRepository {
74
73
  org_id,
75
74
  org_name,
76
75
  org_address,
76
+ type: 'Organization',
77
77
  brands: {},
78
78
  };
79
79
  }
@@ -81,6 +81,7 @@ export class SchoolRepository {
81
81
  result[org_id].brands[brand_id] = {
82
82
  brand_id,
83
83
  brand_name,
84
+ type: 'Brand',
84
85
  schools: [],
85
86
  };
86
87
  }
@@ -88,6 +89,7 @@ export class SchoolRepository {
88
89
  school_id,
89
90
  school_name,
90
91
  school_location,
92
+ type: 'School',
91
93
  });
92
94
  }
93
95
 
@@ -149,6 +151,7 @@ export class SchoolRepository {
149
151
  result[brand_id] = {
150
152
  brand_id,
151
153
  brand_name,
154
+ type: 'Brand',
152
155
  schools: [],
153
156
  };
154
157
  }
@@ -162,6 +165,7 @@ export class SchoolRepository {
162
165
  school_id,
163
166
  school_name,
164
167
  school_location,
168
+ type: 'School',
165
169
  });
166
170
  }
167
171
  }
@@ -30,7 +30,7 @@ export class LoginController {
30
30
  @Req() request: Request & { user: any },
31
31
  @Res() response: Response,
32
32
  ) {
33
- let requestUser: any = request.user;
33
+ let requestUser: any = request.user.userData;
34
34
  response
35
35
  .status(HttpStatus.OK)
36
36
  .json(await this.loginService.logout(requestUser.sessionToken));
@@ -9,34 +9,44 @@ import { UserData } from '../entity/user.entity';
9
9
  export class RoleRepository {
10
10
  constructor(
11
11
  @InjectRepository(Role)
12
- private readonly roleRepo: Repository<Role>
12
+ private readonly roleRepo: Repository<Role>,
13
13
  ) {}
14
14
 
15
15
  async findByCode(code: string): Promise<Role | null> {
16
16
  return this.roleRepo.findOne({ where: { code } });
17
17
  }
18
18
 
19
-
20
19
  public async isRoleNameExists(
21
20
  name: string,
22
- options?: { excludeRoleId?: number; organization_id?: number }
21
+ options?: { excludeRoleId?: number; organization_id?: number },
23
22
  ): Promise<boolean> {
24
23
  const query = this.roleRepo
25
24
  .createQueryBuilder('role')
26
25
  .where('role.name = :name', { name });
27
-
26
+
28
27
  if (options?.excludeRoleId) {
29
- query.andWhere('role.id != :excludeRoleId', { excludeRoleId: options.excludeRoleId });
28
+ query.andWhere('role.id != :excludeRoleId', {
29
+ excludeRoleId: options.excludeRoleId,
30
+ });
30
31
  }
31
-
32
+
32
33
  if (options?.organization_id !== undefined) {
33
34
  query.andWhere('role.organization_id = :organization_id', {
34
35
  organization_id: options.organization_id,
35
36
  });
36
37
  }
37
-
38
+
38
39
  const existingRole = await query.getOne();
39
40
  return !!existingRole;
40
41
  }
41
-
42
+
43
+ async find(data): Promise<Role[] | null> {
44
+ return this.roleRepo.find({
45
+ where: {
46
+ ...(data.is_factory !== undefined && { is_factory: data.is_factory }),
47
+ ...(data.level_id !== undefined && { level_id: data.level_id }),
48
+ ...(data.level_type !== undefined && { level_type: data.level_type }),
49
+ },
50
+ });
51
+ }
42
52
  }
@@ -5,6 +5,11 @@ import { UserSessionService } from './user-session.service';
5
5
  import { UserService } from './user.service';
6
6
  import { ConfigService } from '@nestjs/config';
7
7
  import { UserAppMappingService } from 'src/module/meta/service/user-app-mapping.service';
8
+ import { UserRoleMappingRepository } from '../repository/user-role-mapping.repository';
9
+ import { InjectRepository } from '@nestjs/typeorm';
10
+ import { UserRoleMapping } from '../entity/user-role-mapping.entity';
11
+ import { Repository } from 'typeorm';
12
+ import { Role } from '../entity/role.entity';
8
13
 
9
14
  @Injectable()
10
15
  export class LoginService {
@@ -12,7 +17,10 @@ export class LoginService {
12
17
  @Inject('UserService') private readonly userService: UserService,
13
18
  private userSessionService: UserSessionService,
14
19
  private configService: ConfigService,
15
- private readonly userAppMappingService: UserAppMappingService,
20
+ @InjectRepository(UserRoleMapping)
21
+ private readonly userRoleMappingRepository: Repository<UserRoleMapping>,
22
+ @InjectRepository(Role)
23
+ private readonly roleRepo: Repository<Role>,
16
24
  ) {}
17
25
 
18
26
  masterKey: string = this.configService.get('MASTER_KEY') || '';
@@ -20,27 +28,15 @@ export class LoginService {
20
28
 
21
29
  async login(email: string, password: string, appcode: string) {
22
30
  const user = await this.userService.findByEmailId(email);
23
-
31
+
24
32
  if (!user) {
25
33
  return {
26
34
  success: false,
27
- message: 'Your Email ID not registered.',
35
+ message: 'Your Email ID is not registered.',
28
36
  type: 'email',
29
37
  };
30
38
  }
31
-
32
- const userAppMapping = await this.userAppMappingService.getUserAppMappings(
33
- user.id,
34
- appcode,
35
- );
36
-
37
- if (!userAppMapping) {
38
- // If user is not mapped to the app, throw an error
39
- throw new BadRequestException(
40
- `User with email ${email} is not mapped to the app ${appcode}.`,
41
- );
42
- }
43
-
39
+
44
40
  if (user.status !== 'ACTIVE') {
45
41
  return {
46
42
  success: false,
@@ -48,13 +44,9 @@ export class LoginService {
48
44
  type: 'email',
49
45
  };
50
46
  }
51
-
52
- const encryptedPassword = EncryptUtilService.encryptGCM(
53
- password,
54
- this.masterKey,
55
- this.masterIv,
56
- );
57
-
47
+
48
+ const encryptedPassword = EncryptUtilService.encryptGCM(password, this.masterKey, this.masterIv);
49
+
58
50
  if (encryptedPassword !== user.password) {
59
51
  return {
60
52
  success: false,
@@ -62,23 +54,50 @@ export class LoginService {
62
54
  type: 'password',
63
55
  };
64
56
  }
65
-
66
- const token = await this.userSessionService.createSession(user, appcode);
67
-
57
+
58
+ // Get user-role mappings for this app
59
+ const roleMappings = await this.userRoleMappingRepository.find({
60
+ where: {
61
+ user_id: user.id,
62
+ appcode: appcode,
63
+ },
64
+ });
65
+
66
+ if (!roleMappings.length) {
67
+ throw new BadRequestException('User does not have any access mapped for this app.');
68
+ }
69
+
70
+ // ✅ Pick default access based on priority
71
+ const priority = { org: 1, sch: 2, brn: 3 };
72
+ const defaultAccess = roleMappings.sort(
73
+ (a, b) => priority[a.level_type] - priority[b.level_type],
74
+ )[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
+
68
88
  if (user.is_firstlogin === 1) {
69
89
  user.invitation_status = 'ACCEPTED';
70
90
  user.is_firstlogin = 0;
71
-
72
91
  const { password, ...userWithoutPassword } = user;
73
-
74
92
  await this.userService.updateEntity(userWithoutPassword, user);
75
93
  }
76
-
94
+
77
95
  return {
78
96
  success: true,
79
97
  accessToken: token,
80
98
  };
81
99
  }
100
+
82
101
 
83
102
  async loginWithGoogle(email: string, name) {
84
103
  let user = await this.userService.findByEmailId(email);
@@ -28,10 +28,14 @@ export class RoleService extends EntityServiceImpl {
28
28
  appcode?: string,
29
29
  ): Promise<BaseEntity> {
30
30
  let role = entityData as Role;
31
- if(appcode){
32
- role.appcode = appcode ;
31
+ if (appcode) {
32
+ role.appcode = appcode;
33
33
  }
34
- if (await this.roleRepository.isRoleNameExists(role.name,{organization_id:entityData.organization_id})) {
34
+ if (
35
+ await this.roleRepository.isRoleNameExists(role.name, {
36
+ organization_id: entityData.organization_id,
37
+ })
38
+ ) {
35
39
  throw new BadRequestException('Role name already exists.');
36
40
  }
37
41
 
@@ -102,13 +106,16 @@ export class RoleService extends EntityServiceImpl {
102
106
  }
103
107
 
104
108
  if (
105
- await this.roleRepository.isRoleNameExists(entityData.name, {excludeRoleId:entityData.id,organization_id:loggedInUser?.organization_id})
109
+ await this.roleRepository.isRoleNameExists(entityData.name, {
110
+ excludeRoleId: entityData.id,
111
+ organization_id: loggedInUser?.organization_id,
112
+ })
106
113
  ) {
107
114
  throw new BadRequestException('Role name already exists.');
108
115
  }
109
116
 
110
- const {copy_from_role_id, ...persistableData} = role;
111
- const updatedRole = await super.updateEntity(persistableData, loggedInUser);
117
+ const { copy_from_role_id, ...persistableData } = role;
118
+ const updatedRole = await super.updateEntity(persistableData, loggedInUser);
112
119
 
113
120
  if (role.copy_from_role_id) {
114
121
  const sourceRole = await this.getEntityData(
@@ -140,4 +147,12 @@ export class RoleService extends EntityServiceImpl {
140
147
  }
141
148
  return updatedRole;
142
149
  }
150
+
151
+ async getDefaultRole(data: {
152
+ is_factory?: boolean;
153
+ level_id?: number;
154
+ level_type?: string;
155
+ }): Promise<Role[] | null> {
156
+ return await this.roleRepository.find(data);
157
+ }
143
158
  }
@@ -13,36 +13,47 @@ export class UserSessionService {
13
13
  private readonly clockIDGenService: ClockIDGenService,
14
14
  private configService: ConfigService,
15
15
  ) {}
16
- async createSession(user, appcode?: string) {
16
+ async createSession(user, appcode?: string, accessInfo?: any) {
17
17
  const sessionToken = this.clockIDGenService.idGenerator('SES');
18
-
18
+
19
19
  const userSession: any = new UserSession();
20
20
  userSession.user_id = user.id;
21
21
  userSession.session_key = sessionToken;
22
22
  userSession.is_session_loggedout = 0;
23
23
  userSession.login_time = new Date();
24
+
24
25
  const expiryTokenInHours = this.configService.get('TOKEN_EXPIRY') || 1;
25
26
  userSession.expiry_date = new Date(
26
27
  Date.now() + expiryTokenInHours * 60 * 60 * 1000,
27
28
  );
28
-
29
+
29
30
  await this.userSessionRepository.saveSession(userSession);
30
-
31
- const payload = {
31
+
32
+ const payload: any = {
32
33
  userId: user.id,
33
34
  sessionToken,
34
- appcode: appcode,
35
+ appcode,
35
36
  email_id: user.email_id,
36
37
  organization_id: user.organization_id,
37
38
  enterprise_id: user.enterprise_id,
38
39
  };
40
+
41
+ // ✅ Add role access details
42
+ if (accessInfo) {
43
+ payload.level_type = accessInfo.level_type;
44
+ payload.level_id = accessInfo.level_id;
45
+ payload.role_id = accessInfo.role_id;
46
+ payload.role_code = accessInfo.role_code;
47
+ }
48
+
39
49
  const accessToken = this.jwtAuthService.generateJwt(payload);
40
-
50
+
41
51
  userSession.access_token = accessToken;
42
52
  await this.userSessionRepository.saveSession(userSession);
43
-
53
+
44
54
  return accessToken;
45
55
  }
56
+
46
57
 
47
58
  async findBySessionKey(sessionKey: string): Promise<UserSession | null> {
48
59
  return await this.userSessionRepository.findBySessionKey(sessionKey);