rez_core 2.0.46 → 2.0.48
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/dist/module/auth/strategies/jwt.strategy.js +1 -1
- package/dist/module/auth/strategies/jwt.strategy.js.map +1 -1
- package/dist/module/enterprise/entity/brand.entity.d.ts +1 -0
- package/dist/module/enterprise/entity/brand.entity.js +7 -0
- package/dist/module/enterprise/entity/brand.entity.js.map +1 -1
- package/dist/module/enterprise/entity/organization.entity.d.ts +1 -0
- package/dist/module/enterprise/entity/organization.entity.js +4 -0
- package/dist/module/enterprise/entity/organization.entity.js.map +1 -1
- package/dist/module/enterprise/entity/school.entity.d.ts +1 -0
- package/dist/module/enterprise/entity/school.entity.js +4 -0
- package/dist/module/enterprise/entity/school.entity.js.map +1 -1
- package/dist/module/enterprise/repository/school.repository.js +5 -0
- package/dist/module/enterprise/repository/school.repository.js.map +1 -1
- package/dist/module/user/controller/login.controller.d.ts +10 -1
- package/dist/module/user/controller/login.controller.js +22 -3
- package/dist/module/user/controller/login.controller.js.map +1 -1
- package/dist/module/user/dto/create-user.dto.d.ts +1 -0
- package/dist/module/user/dto/create-user.dto.js.map +1 -1
- package/dist/module/user/repository/role.repository.d.ts +6 -0
- package/dist/module/user/repository/role.repository.js +27 -0
- package/dist/module/user/repository/role.repository.js.map +1 -1
- package/dist/module/user/repository/user-role-mapping.repository.d.ts +5 -1
- package/dist/module/user/repository/user-role-mapping.repository.js +37 -2
- package/dist/module/user/repository/user-role-mapping.repository.js.map +1 -1
- package/dist/module/user/service/login.service.d.ts +6 -3
- package/dist/module/user/service/login.service.js +32 -10
- package/dist/module/user/service/login.service.js.map +1 -1
- package/dist/module/user/service/role.service.js +8 -5
- package/dist/module/user/service/role.service.js.map +1 -1
- package/dist/module/user/service/user-role-mapping.service.d.ts +3 -0
- package/dist/module/user/service/user-role-mapping.service.js +24 -1
- package/dist/module/user/service/user-role-mapping.service.js.map +1 -1
- package/dist/module/user/service/user-session.service.d.ts +11 -2
- package/dist/module/user/service/user-session.service.js +32 -4
- package/dist/module/user/service/user-session.service.js.map +1 -1
- package/dist/module/user/service/user.service.js +57 -23
- package/dist/module/user/service/user.service.js.map +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/module/auth/strategies/jwt.strategy.ts +2 -2
- package/src/module/enterprise/entity/brand.entity.ts +5 -2
- package/src/module/enterprise/entity/organization.entity.ts +3 -0
- package/src/module/enterprise/entity/school.entity.ts +3 -0
- package/src/module/enterprise/repository/school.repository.ts +5 -0
- package/src/module/user/controller/login.controller.ts +33 -2
- package/src/module/user/dto/create-user.dto.ts +2 -0
- package/src/module/user/repository/role.repository.ts +46 -2
- package/src/module/user/repository/user-role-mapping.repository.ts +54 -1
- package/src/module/user/service/login.service.ts +48 -29
- package/src/module/user/service/role.service.ts +10 -6
- package/src/module/user/service/user-role-mapping.service.ts +60 -0
- package/src/module/user/service/user-session.service.ts +144 -4
- package/src/module/user/service/user.service.ts +75 -65
package/package.json
CHANGED
|
@@ -26,7 +26,7 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
|
|
|
26
26
|
organization_id,
|
|
27
27
|
enterprise_id,
|
|
28
28
|
level_id,
|
|
29
|
-
level_type
|
|
29
|
+
level_type,
|
|
30
30
|
} = payload;
|
|
31
31
|
|
|
32
32
|
// Check if session exists and is valid
|
|
@@ -50,7 +50,7 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
|
|
|
50
50
|
organization_id: organization_id,
|
|
51
51
|
enterprise_id: enterprise_id,
|
|
52
52
|
level_id: level_id,
|
|
53
|
-
level_type: level_type,
|
|
53
|
+
level_type: level_type,
|
|
54
54
|
};
|
|
55
55
|
|
|
56
56
|
return { userData };
|
|
@@ -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
|
+
}
|
|
@@ -73,6 +73,7 @@ export class SchoolRepository {
|
|
|
73
73
|
org_id,
|
|
74
74
|
org_name,
|
|
75
75
|
org_address,
|
|
76
|
+
type: 'Organization',
|
|
76
77
|
brands: {},
|
|
77
78
|
};
|
|
78
79
|
}
|
|
@@ -80,6 +81,7 @@ export class SchoolRepository {
|
|
|
80
81
|
result[org_id].brands[brand_id] = {
|
|
81
82
|
brand_id,
|
|
82
83
|
brand_name,
|
|
84
|
+
type: 'Brand',
|
|
83
85
|
schools: [],
|
|
84
86
|
};
|
|
85
87
|
}
|
|
@@ -87,6 +89,7 @@ export class SchoolRepository {
|
|
|
87
89
|
school_id,
|
|
88
90
|
school_name,
|
|
89
91
|
school_location,
|
|
92
|
+
type: 'School',
|
|
90
93
|
});
|
|
91
94
|
}
|
|
92
95
|
|
|
@@ -148,6 +151,7 @@ export class SchoolRepository {
|
|
|
148
151
|
result[brand_id] = {
|
|
149
152
|
brand_id,
|
|
150
153
|
brand_name,
|
|
154
|
+
type: 'Brand',
|
|
151
155
|
schools: [],
|
|
152
156
|
};
|
|
153
157
|
}
|
|
@@ -161,6 +165,7 @@ export class SchoolRepository {
|
|
|
161
165
|
school_id,
|
|
162
166
|
school_name,
|
|
163
167
|
school_location,
|
|
168
|
+
type: 'School',
|
|
164
169
|
});
|
|
165
170
|
}
|
|
166
171
|
}
|
|
@@ -12,10 +12,15 @@ 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
|
+
import { UserSessionService } from '../service/user-session.service';
|
|
15
17
|
|
|
16
18
|
@Controller('auth')
|
|
17
19
|
export class LoginController {
|
|
18
|
-
constructor(
|
|
20
|
+
constructor(
|
|
21
|
+
private loginService: LoginService,
|
|
22
|
+
private userSessionService: UserSessionService,
|
|
23
|
+
) {}
|
|
19
24
|
|
|
20
25
|
@Post('login')
|
|
21
26
|
async login(@Body() body, @Res() res: Response) {
|
|
@@ -30,7 +35,7 @@ export class LoginController {
|
|
|
30
35
|
@Req() request: Request & { user: any },
|
|
31
36
|
@Res() response: Response,
|
|
32
37
|
) {
|
|
33
|
-
let requestUser: any = request.user;
|
|
38
|
+
let requestUser: any = request.user.userData;
|
|
34
39
|
response
|
|
35
40
|
.status(HttpStatus.OK)
|
|
36
41
|
.json(await this.loginService.logout(requestUser.sessionToken));
|
|
@@ -51,4 +56,30 @@ export class LoginController {
|
|
|
51
56
|
// Call LoginService to validate user, create session, and return JWT
|
|
52
57
|
return this.loginService.loginWithGoogle(email, name);
|
|
53
58
|
}
|
|
59
|
+
|
|
60
|
+
@UseGuards(JwtAuthGuard)
|
|
61
|
+
@Post('switch')
|
|
62
|
+
async switchCurrentLevel(
|
|
63
|
+
@Req() req: Request & { user: any },
|
|
64
|
+
@Body()
|
|
65
|
+
data: {
|
|
66
|
+
level_id: string;
|
|
67
|
+
level_type: string;
|
|
68
|
+
appcode: string;
|
|
69
|
+
},
|
|
70
|
+
): Promise<any> {
|
|
71
|
+
const userId = req.user.userData.id;
|
|
72
|
+
|
|
73
|
+
const currentUserLevelType = req.user.userData.level_type;
|
|
74
|
+
const currentUserLevelId = req.user.userData.level_id;
|
|
75
|
+
const currentUserOrganization_id = req.user.userData.organization_id;
|
|
76
|
+
|
|
77
|
+
return await this.userSessionService.switchCurrentLevelService(
|
|
78
|
+
userId,
|
|
79
|
+
currentUserLevelType,
|
|
80
|
+
currentUserLevelId,
|
|
81
|
+
currentUserOrganization_id,
|
|
82
|
+
data,
|
|
83
|
+
);
|
|
84
|
+
}
|
|
54
85
|
}
|
|
@@ -2,8 +2,6 @@ import { BadRequestException, Injectable } from '@nestjs/common';
|
|
|
2
2
|
import { Repository } from 'typeorm';
|
|
3
3
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
4
4
|
import { Role } from '../entity/role.entity';
|
|
5
|
-
import { ModuleAccess } from 'src/module/module/entity/module-access.entity';
|
|
6
|
-
import { UserData } from '../entity/user.entity';
|
|
7
5
|
|
|
8
6
|
@Injectable()
|
|
9
7
|
export class RoleRepository {
|
|
@@ -49,4 +47,50 @@ export class RoleRepository {
|
|
|
49
47
|
},
|
|
50
48
|
});
|
|
51
49
|
}
|
|
50
|
+
|
|
51
|
+
/*
|
|
52
|
+
Ensure role_code is unique within the same level_type and level_id.
|
|
53
|
+
* Confirm level_type and level_id correspond to existing org/school.
|
|
54
|
+
*/
|
|
55
|
+
|
|
56
|
+
public async isRoleNameWithLevelExists(
|
|
57
|
+
name: string,
|
|
58
|
+
options?: {
|
|
59
|
+
excludeRoleId?: number;
|
|
60
|
+
organization_id?: number;
|
|
61
|
+
level_type?: string;
|
|
62
|
+
level_id?: number;
|
|
63
|
+
},
|
|
64
|
+
): Promise<boolean> {
|
|
65
|
+
const query = this.roleRepo
|
|
66
|
+
.createQueryBuilder('role')
|
|
67
|
+
.where('role.name = :name', { name });
|
|
68
|
+
|
|
69
|
+
if (options?.excludeRoleId) {
|
|
70
|
+
query.andWhere('role.id != :excludeRoleId', {
|
|
71
|
+
excludeRoleId: options.excludeRoleId,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (options?.organization_id !== undefined) {
|
|
76
|
+
query.andWhere('role.organization_id = :organization_id', {
|
|
77
|
+
organization_id: options.organization_id,
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (options?.level_type) {
|
|
82
|
+
query.andWhere('role.level_type = :level_type', {
|
|
83
|
+
level_type: options.level_type,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (options?.level_id !== undefined) {
|
|
88
|
+
query.andWhere('role.level_id = :level_id', {
|
|
89
|
+
level_id: options.level_id,
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const existingRole = await query.getOne();
|
|
94
|
+
return !!existingRole;
|
|
95
|
+
}
|
|
52
96
|
}
|
|
@@ -16,6 +16,14 @@ export class UserRoleMappingRepository {
|
|
|
16
16
|
return await this.userRoleMappingRepository.save(userRoleMapping);
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
+
async update(
|
|
20
|
+
id: number,
|
|
21
|
+
userRoleMapping: UserRoleMapping,
|
|
22
|
+
): Promise<UserRoleMapping | null> {
|
|
23
|
+
await this.userRoleMappingRepository.update(id, userRoleMapping);
|
|
24
|
+
return await this.userRoleMappingRepository.findOneBy({ id });
|
|
25
|
+
}
|
|
26
|
+
|
|
19
27
|
async findByUserId(userId: number): Promise<UserRoleMapping[] | null> {
|
|
20
28
|
return await this.userRoleMappingRepository.find({
|
|
21
29
|
where: { user_id: userId },
|
|
@@ -31,9 +39,30 @@ export class UserRoleMappingRepository {
|
|
|
31
39
|
async findByUserIdAndRoleId(
|
|
32
40
|
userId: number,
|
|
33
41
|
roleId: number,
|
|
42
|
+
appcode?: string,
|
|
43
|
+
level_id?: string,
|
|
44
|
+
level_type?: string,
|
|
34
45
|
): Promise<UserRoleMapping | null> {
|
|
35
46
|
return await this.userRoleMappingRepository.findOne({
|
|
36
|
-
where: {
|
|
47
|
+
where: {
|
|
48
|
+
user_id: userId,
|
|
49
|
+
role_id: roleId,
|
|
50
|
+
appcode,
|
|
51
|
+
level_id,
|
|
52
|
+
level_type,
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async findByUSRId(usrId: number): Promise<UserRoleMapping | null> {
|
|
58
|
+
if (usrId) {
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return await this.userRoleMappingRepository.findOne({
|
|
63
|
+
where: {
|
|
64
|
+
id: usrId,
|
|
65
|
+
},
|
|
37
66
|
});
|
|
38
67
|
}
|
|
39
68
|
|
|
@@ -61,4 +90,28 @@ export class UserRoleMappingRepository {
|
|
|
61
90
|
|
|
62
91
|
return { appcode: appcodeArray };
|
|
63
92
|
}
|
|
93
|
+
async findByUserIdAndAppCode(
|
|
94
|
+
userId: number,
|
|
95
|
+
appCode: string,
|
|
96
|
+
): Promise<UserRoleMapping | null> {
|
|
97
|
+
return await this.userRoleMappingRepository.findOne({
|
|
98
|
+
where: { user_id: userId, appcode: appCode },
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async findByUserIdAndAppCodeAndLevel(
|
|
103
|
+
userId: number,
|
|
104
|
+
appCode: string,
|
|
105
|
+
levelType: string,
|
|
106
|
+
levelId: string,
|
|
107
|
+
): Promise<UserRoleMapping | null> {
|
|
108
|
+
return await this.userRoleMappingRepository.findOne({
|
|
109
|
+
where: {
|
|
110
|
+
user_id: userId,
|
|
111
|
+
appcode: appCode,
|
|
112
|
+
level_type: levelType,
|
|
113
|
+
level_id: levelId,
|
|
114
|
+
},
|
|
115
|
+
});
|
|
116
|
+
}
|
|
64
117
|
}
|
|
@@ -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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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);
|
|
@@ -8,7 +8,6 @@ import { RoleRepository } from '../repository/role.repository';
|
|
|
8
8
|
import { EntityManager, Repository } from 'typeorm';
|
|
9
9
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
10
10
|
import { ModuleAccess } from '../../module/entity/module-access.entity';
|
|
11
|
-
import { instanceToPlain } from 'class-transformer';
|
|
12
11
|
|
|
13
12
|
@Injectable()
|
|
14
13
|
export class RoleService extends EntityServiceImpl {
|
|
@@ -28,15 +27,20 @@ export class RoleService extends EntityServiceImpl {
|
|
|
28
27
|
appcode?: string,
|
|
29
28
|
): Promise<BaseEntity> {
|
|
30
29
|
let role = entityData as Role;
|
|
31
|
-
|
|
32
|
-
|
|
30
|
+
|
|
31
|
+
if (loggedInUser?.level_type && loggedInUser?.level_id) {
|
|
32
|
+
role.level_type = loggedInUser.level_type;
|
|
33
|
+
role.level_id = loggedInUser.level_id;
|
|
33
34
|
}
|
|
35
|
+
|
|
34
36
|
if (
|
|
35
|
-
await this.roleRepository.
|
|
36
|
-
organization_id:
|
|
37
|
+
await this.roleRepository.isRoleNameWithLevelExists(role.name, {
|
|
38
|
+
organization_id: loggedInUser?.organization_id || undefined,
|
|
39
|
+
level_type: loggedInUser?.level_type,
|
|
40
|
+
level_id: parseInt(loggedInUser?.level_id || '', 10),
|
|
37
41
|
})
|
|
38
42
|
) {
|
|
39
|
-
throw new BadRequestException('Role
|
|
43
|
+
throw new BadRequestException('Role code already exists.');
|
|
40
44
|
}
|
|
41
45
|
|
|
42
46
|
const sourceRole = role.copy_from_role_id
|
|
@@ -12,6 +12,9 @@ export class UserRoleMappingService {
|
|
|
12
12
|
const existing = await this.userRoleMappingRepository.findByUserIdAndRoleId(
|
|
13
13
|
userRoleMapping.user_id,
|
|
14
14
|
userRoleMapping.role_id,
|
|
15
|
+
userRoleMapping.appcode,
|
|
16
|
+
userRoleMapping.level_id,
|
|
17
|
+
userRoleMapping.level_type,
|
|
15
18
|
);
|
|
16
19
|
|
|
17
20
|
if (!existing) {
|
|
@@ -19,6 +22,39 @@ export class UserRoleMappingService {
|
|
|
19
22
|
}
|
|
20
23
|
}
|
|
21
24
|
|
|
25
|
+
async updateAssignedRole(userRoleMapping: UserRoleMapping) {
|
|
26
|
+
// 1. If ID is provided, try to fetch existing mapping by ID
|
|
27
|
+
if (userRoleMapping.id) {
|
|
28
|
+
const existing = await this.userRoleMappingRepository.findByUSRId(
|
|
29
|
+
userRoleMapping.id,
|
|
30
|
+
);
|
|
31
|
+
if (existing) {
|
|
32
|
+
existing.appcode = userRoleMapping.appcode;
|
|
33
|
+
existing.level_id = userRoleMapping.level_id;
|
|
34
|
+
existing.level_type = userRoleMapping.level_type;
|
|
35
|
+
existing.role_id = userRoleMapping.role_id;
|
|
36
|
+
|
|
37
|
+
return await this.userRoleMappingRepository.save(existing);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// 2. Check if a similar mapping already exists (same user-role-level-app)
|
|
42
|
+
const duplicate =
|
|
43
|
+
await this.userRoleMappingRepository.findByUserIdAndRoleId(
|
|
44
|
+
userRoleMapping.user_id,
|
|
45
|
+
userRoleMapping.role_id,
|
|
46
|
+
userRoleMapping.appcode,
|
|
47
|
+
userRoleMapping.level_id,
|
|
48
|
+
userRoleMapping.level_type,
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
if (duplicate) {
|
|
52
|
+
return duplicate; // or null, depending on how you want to handle it
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return await this.userRoleMappingRepository.save(userRoleMapping);
|
|
56
|
+
}
|
|
57
|
+
|
|
22
58
|
async findByUserId(userId: number): Promise<UserRoleMapping[] | null> {
|
|
23
59
|
return await this.userRoleMappingRepository.findByUserId(userId);
|
|
24
60
|
}
|
|
@@ -26,4 +62,28 @@ export class UserRoleMappingService {
|
|
|
26
62
|
async deleteByUserId(userId: number) {
|
|
27
63
|
await this.userRoleMappingRepository.deleteByUserId(userId);
|
|
28
64
|
}
|
|
65
|
+
|
|
66
|
+
async getUserByIdAndAppCode(
|
|
67
|
+
userId: number,
|
|
68
|
+
appCode: string,
|
|
69
|
+
): Promise<UserRoleMapping | null> {
|
|
70
|
+
return await this.userRoleMappingRepository.findByUserIdAndAppCode(
|
|
71
|
+
userId,
|
|
72
|
+
appCode,
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async getUserByIdAppCodeAndLevel(
|
|
77
|
+
userId: number,
|
|
78
|
+
appCode: string,
|
|
79
|
+
levelType: string,
|
|
80
|
+
levelId: string,
|
|
81
|
+
): Promise<UserRoleMapping | null> {
|
|
82
|
+
return await this.userRoleMappingRepository.findByUserIdAndAppCodeAndLevel(
|
|
83
|
+
userId,
|
|
84
|
+
appCode,
|
|
85
|
+
levelType,
|
|
86
|
+
levelId,
|
|
87
|
+
);
|
|
88
|
+
}
|
|
29
89
|
}
|