rez_core 1.0.59 → 1.0.61
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/listmaster/controller/list-master.controller.d.ts +1 -1
- package/dist/module/listmaster/controller/list-master.controller.js +4 -3
- package/dist/module/listmaster/controller/list-master.controller.js.map +1 -1
- package/dist/module/listmaster/service/list-master.service.d.ts +1 -1
- package/dist/module/listmaster/service/list-master.service.js +8 -4
- package/dist/module/listmaster/service/list-master.service.js.map +1 -1
- package/dist/module/meta/service/entity-service-impl.service.d.ts +2 -2
- package/dist/module/meta/service/entity-service-impl.service.js +16 -7
- package/dist/module/meta/service/entity-service-impl.service.js.map +1 -1
- package/dist/module/module/controller/module-access.controller.d.ts +1 -37
- package/dist/module/module/controller/module-access.controller.js +2 -34
- package/dist/module/module/controller/module-access.controller.js.map +1 -1
- package/dist/module/module/repository/module-access.repository.d.ts +1 -35
- package/dist/module/module/repository/module-access.repository.js +2 -104
- package/dist/module/module/repository/module-access.repository.js.map +1 -1
- package/dist/module/module/service/module-access.service.d.ts +1 -36
- package/dist/module/module/service/module-access.service.js +2 -44
- package/dist/module/module/service/module-access.service.js.map +1 -1
- package/dist/module/user/controller/role.controller.d.ts +41 -0
- package/dist/module/user/controller/role.controller.js +63 -0
- package/dist/module/user/controller/role.controller.js.map +1 -0
- package/dist/module/user/repository/role.repository.d.ts +35 -1
- package/dist/module/user/repository/role.repository.js +105 -2
- package/dist/module/user/repository/role.repository.js.map +1 -1
- package/dist/module/user/service/role.service.d.ts +35 -1
- package/dist/module/user/service/role.service.js +39 -2
- package/dist/module/user/service/role.service.js.map +1 -1
- package/dist/module/user/user.module.js +4 -2
- package/dist/module/user/user.module.js.map +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/module/listmaster/controller/list-master.controller.ts +3 -3
- package/src/module/listmaster/service/list-master.service.ts +9 -6
- package/src/module/meta/service/entity-service-impl.service.ts +21 -14
- package/src/module/module/controller/module-access.controller.ts +2 -36
- package/src/module/module/repository/module-access.repository.ts +0 -151
- package/src/module/module/service/module-access.service.ts +1 -62
- package/src/module/user/controller/role.controller.ts +55 -0
- package/src/module/user/repository/role.repository.ts +157 -1
- package/src/module/user/service/role.service.ts +70 -2
- package/src/module/user/user.module.ts +4 -2
- package/dist/resources/dev.properties.yaml +0 -21
- package/dist/resources/uat.properties.yaml +0 -15
package/package.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Body, Controller, HttpCode, HttpStatus, Param, Post } from '@nestjs/common';
|
|
1
|
+
import { Body, Controller, HttpCode, HttpStatus, Param, Post, Query } from '@nestjs/common';
|
|
2
2
|
import { ListMasterService } from '../service/list-master.service';
|
|
3
3
|
|
|
4
4
|
@Controller('list-master')
|
|
@@ -7,7 +7,7 @@ export class ListMasterController {
|
|
|
7
7
|
|
|
8
8
|
@Post('/getDropdownData/:type')
|
|
9
9
|
@HttpCode(HttpStatus.OK)
|
|
10
|
-
async getDropdownData(@Param('type') type: string, @Body() params) {
|
|
11
|
-
return await this.service.getDropdownOptions(type, params);
|
|
10
|
+
async getDropdownData(@Param('type') type: string, @Body() params,@Query() query: Record<string, string>,) {
|
|
11
|
+
return await this.service.getDropdownOptions(type, params,query);
|
|
12
12
|
}
|
|
13
13
|
}
|
|
@@ -21,14 +21,14 @@ export class ListMasterService extends EntityServiceImpl {
|
|
|
21
21
|
super();
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
async getDropdownOptions(type: string, params: Record<string, string
|
|
24
|
+
async getDropdownOptions(type: string, params: Record<string, string>,option) {
|
|
25
25
|
const config = await this.listMasterRepo.findByType(type);
|
|
26
26
|
|
|
27
27
|
if (!config) throw new NotFoundException(`Type ${type} not found`);
|
|
28
28
|
|
|
29
29
|
switch (config.source) {
|
|
30
30
|
case 'entity':
|
|
31
|
-
return this.fetchFromEntity(type);
|
|
31
|
+
return this.fetchFromEntity(type,option);
|
|
32
32
|
|
|
33
33
|
case 'master':
|
|
34
34
|
return this.listItemsRepo.findItemsByType(type);
|
|
@@ -41,15 +41,18 @@ export class ListMasterService extends EntityServiceImpl {
|
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
private async fetchFromEntity(sourceList: string) {
|
|
44
|
+
private async fetchFromEntity(sourceList: string,option) {
|
|
45
45
|
let result: { label: string; value: number }[] = [];
|
|
46
46
|
if (sourceList) {
|
|
47
47
|
const entityMaster =
|
|
48
48
|
await this.entityMasterService.getEntityData(sourceList);
|
|
49
49
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
50
|
+
const filters: any = {};
|
|
51
|
+
if (option?.onlyActive === 'true') {
|
|
52
|
+
filters.status = 'ACTIVE';
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const baseEntities = await super.getEntityList(entityMaster.mapped_entity_type,filters);
|
|
53
56
|
baseEntities.forEach((entity) => {
|
|
54
57
|
result.push({
|
|
55
58
|
label: entity.name,
|
|
@@ -62,10 +62,13 @@ export class EntityServiceImpl implements EntityService<BaseEntity> {
|
|
|
62
62
|
entityData.created_date = new Date();
|
|
63
63
|
if (loggedInUser) {
|
|
64
64
|
entityData.created_by = loggedInUser.id;
|
|
65
|
+
if (!entityData.organization_id)
|
|
66
|
+
entityData.organization_id = loggedInUser.organization_id;
|
|
67
|
+
if (!entityData.enterprise_id)
|
|
68
|
+
entityData.enterprise_id = loggedInUser.enterprise_id;
|
|
65
69
|
}
|
|
66
70
|
|
|
67
|
-
|
|
68
|
-
return savedEntity;
|
|
71
|
+
return repo.save(entityData);
|
|
69
72
|
}
|
|
70
73
|
|
|
71
74
|
deleteEntity(
|
|
@@ -110,24 +113,24 @@ export class EntityServiceImpl implements EntityService<BaseEntity> {
|
|
|
110
113
|
return await repoService.findOne({ where: { id: id } });
|
|
111
114
|
}
|
|
112
115
|
|
|
113
|
-
async getEntityList(entityType: string): Promise<BaseEntity[]> {
|
|
116
|
+
async getEntityList(entityType: string, filters: Record<string, any> = {}): Promise<BaseEntity[]> {
|
|
114
117
|
const entityData = await this.entityMasterService.getEntityData(entityType);
|
|
115
|
-
const repoService = this.reflectionHelper.getRepoService(
|
|
116
|
-
|
|
117
|
-
);
|
|
118
|
+
const repoService = this.reflectionHelper.getRepoService(entityData.entity_data_class);
|
|
119
|
+
|
|
118
120
|
if (!repoService) {
|
|
119
|
-
throw new Error(
|
|
120
|
-
`Repository service not found for entityType: ${entityType}`,
|
|
121
|
-
);
|
|
121
|
+
throw new Error(`Repository service not found for entityType: ${entityType}`);
|
|
122
122
|
}
|
|
123
|
-
|
|
124
|
-
const result = await repoService.find(
|
|
123
|
+
|
|
124
|
+
const result = await repoService.find({
|
|
125
|
+
where: filters,
|
|
126
|
+
});
|
|
127
|
+
|
|
125
128
|
return result;
|
|
126
129
|
}
|
|
127
130
|
|
|
128
131
|
async updateEntity(
|
|
129
132
|
entityData: BaseEntity,
|
|
130
|
-
|
|
133
|
+
loggedInUser: UserData | null,
|
|
131
134
|
): Promise<BaseEntity> {
|
|
132
135
|
const entityMaster = await this.entityMasterService.getEntityData(
|
|
133
136
|
entityData.entity_type,
|
|
@@ -143,8 +146,12 @@ export class EntityServiceImpl implements EntityService<BaseEntity> {
|
|
|
143
146
|
}
|
|
144
147
|
|
|
145
148
|
entityData.modified_date = new Date();
|
|
146
|
-
if (
|
|
147
|
-
entityData.modified_by =
|
|
149
|
+
if (loggedInUser) {
|
|
150
|
+
entityData.modified_by = loggedInUser.id;
|
|
151
|
+
if (!entityData.organization_id)
|
|
152
|
+
entityData.organization_id = loggedInUser.organization_id;
|
|
153
|
+
if (!entityData.enterprise_id)
|
|
154
|
+
entityData.enterprise_id = loggedInUser.enterprise_id;
|
|
148
155
|
}
|
|
149
156
|
await repoService.update(entityData.id, entityData);
|
|
150
157
|
return await repoService.findOne({ where: { id: entityData.id } });
|
|
@@ -8,9 +8,7 @@ import { UserData } from '../../user/entity/user.entity';
|
|
|
8
8
|
@Controller('module-access')
|
|
9
9
|
@UseGuards(JwtAuthGuard)
|
|
10
10
|
export class ModuleAccessController {
|
|
11
|
-
constructor(private readonly moduleAccessService: ModuleAccessService
|
|
12
|
-
@Inject('UserService') private readonly userService: UserService,
|
|
13
|
-
) {}
|
|
11
|
+
constructor(private readonly moduleAccessService: ModuleAccessService) {}
|
|
14
12
|
|
|
15
13
|
@Get('roles')
|
|
16
14
|
async getRoles() {
|
|
@@ -39,39 +37,7 @@ export class ModuleAccessController {
|
|
|
39
37
|
return this.moduleAccessService.updateModuleAccess(moduleAccessData);
|
|
40
38
|
}
|
|
41
39
|
|
|
42
|
-
|
|
43
|
-
async createRole(
|
|
44
|
-
@Body()
|
|
45
|
-
body: {
|
|
46
|
-
name: string;
|
|
47
|
-
description?: string;
|
|
48
|
-
status?: 'ACTIVE' | 'INACTIVE';
|
|
49
|
-
copyFromRoleId?: number;
|
|
50
|
-
},
|
|
51
|
-
@Req() req: Request & {user: any}
|
|
52
|
-
) {
|
|
53
|
-
|
|
54
|
-
let requestedUser = req.user;
|
|
55
|
-
let loggedInUser = await this.userService.getEntityData(ENTITYTYPE_USER,requestedUser.id);
|
|
56
|
-
return this.moduleAccessService.createRole(body, loggedInUser as UserData);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
@Post('update-role/:id')
|
|
60
|
-
async updateRole(
|
|
61
|
-
@Param('id') id: number,
|
|
62
|
-
@Body()
|
|
63
|
-
body: {
|
|
64
|
-
name?: string;
|
|
65
|
-
description?: string;
|
|
66
|
-
status?: 'ACTIVE' | 'INACTIVE';
|
|
67
|
-
copyFromRoleId?: number; // <-- ADD THIS
|
|
68
|
-
},
|
|
69
|
-
@Req() req: Request & {user: any}
|
|
70
|
-
) {
|
|
71
|
-
let requestedUser = req.user;
|
|
72
|
-
let loggedInUser = await this.userService.getEntityData(ENTITYTYPE_USER,requestedUser.id);
|
|
73
|
-
return this.moduleAccessService.updateRole(id, body, loggedInUser as UserData);
|
|
74
|
-
}
|
|
40
|
+
|
|
75
41
|
|
|
76
42
|
|
|
77
43
|
}
|
|
@@ -19,7 +19,6 @@ export class ModuleAccessRepository {
|
|
|
19
19
|
private readonly moduleAccessRepo: Repository<ModuleAccess>,
|
|
20
20
|
@InjectRepository(ModuleAction)
|
|
21
21
|
private readonly moduleActionRepo: Repository<ModuleAction>,
|
|
22
|
-
private readonly clockIDGenService: ClockIDGenService,
|
|
23
22
|
) {}
|
|
24
23
|
|
|
25
24
|
async getRoles() {
|
|
@@ -141,155 +140,5 @@ export class ModuleAccessRepository {
|
|
|
141
140
|
}
|
|
142
141
|
}
|
|
143
142
|
|
|
144
|
-
async createRole(
|
|
145
|
-
{
|
|
146
|
-
name,
|
|
147
|
-
description,
|
|
148
|
-
status = 'ACTIVE',
|
|
149
|
-
copyFromRoleId,
|
|
150
|
-
}: {
|
|
151
|
-
name: string;
|
|
152
|
-
description?: string;
|
|
153
|
-
status?: 'ACTIVE' | 'INACTIVE';
|
|
154
|
-
copyFromRoleId?: number;
|
|
155
|
-
},
|
|
156
|
-
loggedInUser: UserData,
|
|
157
|
-
) {
|
|
158
|
-
if (await this.isRoleNameExists(name)) {
|
|
159
|
-
throw new BadRequestException('Role name already exists.');
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
const sourceRole = copyFromRoleId
|
|
163
|
-
? await this.roleRepo.findOne({ where: { id: copyFromRoleId } })
|
|
164
|
-
: null;
|
|
165
|
-
|
|
166
|
-
if (copyFromRoleId && !sourceRole) {
|
|
167
|
-
throw new BadRequestException('Source role not found.');
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
const newRole = this.roleRepo.create({
|
|
171
|
-
name,
|
|
172
|
-
description,
|
|
173
|
-
status,
|
|
174
|
-
created_by: loggedInUser.id,
|
|
175
|
-
created_date: new Date(),
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
const savedRole = await this.roleRepo.save(newRole);
|
|
179
|
-
savedRole.code = `ROL${savedRole.id}`;
|
|
180
|
-
await this.roleRepo.save(savedRole);
|
|
181
|
-
|
|
182
|
-
if (sourceRole) {
|
|
183
|
-
const sourcePermissions = await this.moduleAccessRepo.find({
|
|
184
|
-
where: { role_code: sourceRole.code },
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
const clonedPermissions = sourcePermissions.map(perm =>
|
|
188
|
-
this.moduleAccessRepo.create({
|
|
189
|
-
role_code: savedRole.code,
|
|
190
|
-
module_code: perm.module_code,
|
|
191
|
-
action_type: perm.action_type,
|
|
192
|
-
access_flag: perm.access_flag,
|
|
193
|
-
app_code: perm.app_code,
|
|
194
|
-
}),
|
|
195
|
-
);
|
|
196
|
-
|
|
197
|
-
await this.moduleAccessRepo.save(clonedPermissions);
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
return {
|
|
201
|
-
success: true,
|
|
202
|
-
msg: 'Role created successfully',
|
|
203
|
-
role: {
|
|
204
|
-
id: savedRole.id,
|
|
205
|
-
name: savedRole.name,
|
|
206
|
-
status: savedRole.status,
|
|
207
|
-
code: savedRole.code,
|
|
208
|
-
},
|
|
209
|
-
};
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
async updateRole(
|
|
213
|
-
roleId: number,
|
|
214
|
-
{
|
|
215
|
-
name,
|
|
216
|
-
description,
|
|
217
|
-
status = 'ACTIVE',
|
|
218
|
-
copyFromRoleId,
|
|
219
|
-
}: {
|
|
220
|
-
name: string;
|
|
221
|
-
description?: string;
|
|
222
|
-
status?: 'ACTIVE' | 'INACTIVE';
|
|
223
|
-
copyFromRoleId?: number;
|
|
224
|
-
},
|
|
225
|
-
loggedInUser: UserData,
|
|
226
|
-
) {
|
|
227
|
-
if (await this.isRoleNameExists(name, roleId)) {
|
|
228
|
-
throw new BadRequestException('Role name already exists.');
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
const role = await this.roleRepo.findOne({ where: { id: roleId } });
|
|
232
|
-
if (!role) {
|
|
233
|
-
throw new BadRequestException('Role not found');
|
|
234
|
-
}
|
|
235
143
|
|
|
236
|
-
Object.assign(role, {
|
|
237
|
-
name,
|
|
238
|
-
description: description || '',
|
|
239
|
-
status,
|
|
240
|
-
modified_by: loggedInUser.id,
|
|
241
|
-
modified_date: new Date(),
|
|
242
|
-
});
|
|
243
|
-
|
|
244
|
-
await this.roleRepo.save(role);
|
|
245
|
-
|
|
246
|
-
if (copyFromRoleId) {
|
|
247
|
-
const sourceRole = await this.roleRepo.findOne({ where: { id: copyFromRoleId } });
|
|
248
|
-
if (!sourceRole) {
|
|
249
|
-
throw new BadRequestException('Source role not found');
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
await this.moduleAccessRepo.delete({ role_code: role.code });
|
|
253
|
-
|
|
254
|
-
const sourcePermissions = await this.moduleAccessRepo.find({
|
|
255
|
-
where: { role_code: sourceRole.code },
|
|
256
|
-
});
|
|
257
|
-
|
|
258
|
-
const clonedPermissions = sourcePermissions.map(perm =>
|
|
259
|
-
this.moduleAccessRepo.create({
|
|
260
|
-
role_code: role.code,
|
|
261
|
-
module_code: perm.module_code,
|
|
262
|
-
action_type: perm.action_type,
|
|
263
|
-
access_flag: perm.access_flag,
|
|
264
|
-
app_code: perm.app_code,
|
|
265
|
-
}),
|
|
266
|
-
);
|
|
267
|
-
|
|
268
|
-
await this.moduleAccessRepo.save(clonedPermissions);
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
return {
|
|
272
|
-
success: true,
|
|
273
|
-
msg: 'Role updated successfully',
|
|
274
|
-
role: {
|
|
275
|
-
id: role.id,
|
|
276
|
-
name: role.name,
|
|
277
|
-
status: role.status,
|
|
278
|
-
code: role.code,
|
|
279
|
-
},
|
|
280
|
-
};
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
private async isRoleNameExists(name: string, excludeRoleId?: number): Promise<boolean> {
|
|
284
|
-
const query = this.roleRepo
|
|
285
|
-
.createQueryBuilder('role')
|
|
286
|
-
.where('role.name = :name', { name });
|
|
287
|
-
|
|
288
|
-
if (excludeRoleId) {
|
|
289
|
-
query.andWhere('role.id != :excludeRoleId', { excludeRoleId });
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
const existingRole = await query.getOne();
|
|
293
|
-
return !!existingRole;
|
|
294
|
-
}
|
|
295
144
|
}
|
|
@@ -11,8 +11,6 @@ import { UserData } from 'src/module/user/entity/user.entity';
|
|
|
11
11
|
export class ModuleAccessService {
|
|
12
12
|
constructor(
|
|
13
13
|
private readonly moduleAccessRepository: ModuleAccessRepository,
|
|
14
|
-
@Inject('RoleService') private readonly roleService: RoleService,
|
|
15
|
-
private readonly entityManager: EntityManager,
|
|
16
14
|
) {}
|
|
17
15
|
|
|
18
16
|
async getRoles() {
|
|
@@ -48,64 +46,5 @@ export class ModuleAccessService {
|
|
|
48
46
|
}
|
|
49
47
|
}
|
|
50
48
|
|
|
51
|
-
|
|
52
|
-
body: {
|
|
53
|
-
name: string;
|
|
54
|
-
description?: string;
|
|
55
|
-
status?: 'ACTIVE' | 'INACTIVE';
|
|
56
|
-
copyFromRoleId?: number;
|
|
57
|
-
},
|
|
58
|
-
loggedInUser: UserData,
|
|
59
|
-
) {
|
|
60
|
-
return this.moduleAccessRepository.createRole(body, loggedInUser);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
async updateRole(
|
|
64
|
-
id: number,
|
|
65
|
-
body: {
|
|
66
|
-
name?: string;
|
|
67
|
-
description?: string;
|
|
68
|
-
status?: 'ACTIVE' | 'INACTIVE';
|
|
69
|
-
copyFromRoleId?: number;
|
|
70
|
-
},
|
|
71
|
-
loggedInUser: UserData,
|
|
72
|
-
) {
|
|
73
|
-
if (!body.name) {
|
|
74
|
-
throw new BadRequestException('Role name is required');
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
if (body.status === 'INACTIVE') {
|
|
78
|
-
//get role by id
|
|
79
|
-
const entityData = await this.roleService.getEntityData(
|
|
80
|
-
ENTITYTYPE_ROLE,
|
|
81
|
-
id,
|
|
82
|
-
);
|
|
83
|
-
|
|
84
|
-
const associatedUsers = await this.entityManager.query(`SELECT map.*
|
|
85
|
-
FROM cr_user_role_mapping map
|
|
86
|
-
JOIN cr_user usr ON map.user_id = usr.id
|
|
87
|
-
WHERE usr.status = 'ACTIVE' and map.role_id = ${entityData?.id}`);
|
|
88
|
-
|
|
89
|
-
if (associatedUsers.length > 0) {
|
|
90
|
-
throw new BadRequestException(
|
|
91
|
-
'Cannot deactivate role as it is associated with active users',
|
|
92
|
-
);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
if (!entityData) {
|
|
96
|
-
throw new BadRequestException('Role not found');
|
|
97
|
-
}
|
|
98
|
-
const role = entityData as Role;
|
|
99
|
-
if (role.is_system === 1) {
|
|
100
|
-
throw new BadRequestException('Cannot deactivate system role');
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
return this.moduleAccessRepository.updateRole(id, {
|
|
105
|
-
name: body.name,
|
|
106
|
-
description: body.description,
|
|
107
|
-
status: body.status,
|
|
108
|
-
copyFromRoleId: body.copyFromRoleId,
|
|
109
|
-
}, loggedInUser);
|
|
110
|
-
}
|
|
49
|
+
|
|
111
50
|
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { Body, Controller, Inject, Param, Post, Req, UseGuards } from '@nestjs/common';
|
|
2
|
+
import { UserService } from '../service/user.service';
|
|
3
|
+
import { ENTITYTYPE_USER } from 'src/constant/global.constant';
|
|
4
|
+
import { RoleService } from '../service/role.service';
|
|
5
|
+
import { UserData } from '../entity/user.entity';
|
|
6
|
+
import { request } from 'express';
|
|
7
|
+
import { AuthGuard } from '@nestjs/passport';
|
|
8
|
+
import { JwtAuthGuard } from 'src/module/auth/guards/jwt.guard';
|
|
9
|
+
@Controller('role')
|
|
10
|
+
@UseGuards(JwtAuthGuard)
|
|
11
|
+
export class RoleController {
|
|
12
|
+
constructor(
|
|
13
|
+
@Inject('UserService') private readonly userService: UserService,
|
|
14
|
+
@Inject('RoleService') private readonly roleService: RoleService,
|
|
15
|
+
) {}
|
|
16
|
+
|
|
17
|
+
@Post('create')
|
|
18
|
+
async createRole(
|
|
19
|
+
@Body()
|
|
20
|
+
body: {
|
|
21
|
+
name: string;
|
|
22
|
+
description?: string;
|
|
23
|
+
status?: 'ACTIVE' | 'INACTIVE';
|
|
24
|
+
copyFromRoleId?: number;
|
|
25
|
+
},
|
|
26
|
+
@Req() request: Request & { user: any },
|
|
27
|
+
) {
|
|
28
|
+
let requestedUser = request.user;
|
|
29
|
+
let loggedInUser = await this.userService.getEntityData(
|
|
30
|
+
ENTITYTYPE_USER,
|
|
31
|
+
requestedUser.id,
|
|
32
|
+
);
|
|
33
|
+
return this.roleService.createRole(body, loggedInUser as UserData);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
@Post('update/:id')
|
|
37
|
+
async updateRole(
|
|
38
|
+
@Param('id') id: number,
|
|
39
|
+
@Body()
|
|
40
|
+
body: {
|
|
41
|
+
name?: string;
|
|
42
|
+
description?: string;
|
|
43
|
+
status?: 'ACTIVE' | 'INACTIVE';
|
|
44
|
+
copyFromRoleId?: number; // <-- ADD THIS
|
|
45
|
+
},
|
|
46
|
+
@Req() req: Request & { user: any },
|
|
47
|
+
) {
|
|
48
|
+
let requestedUser = req.user;
|
|
49
|
+
let loggedInUser = await this.userService.getEntityData(
|
|
50
|
+
ENTITYTYPE_USER,
|
|
51
|
+
requestedUser.id,
|
|
52
|
+
);
|
|
53
|
+
return this.roleService.updateRole(id, body, loggedInUser as UserData);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -1,16 +1,172 @@
|
|
|
1
|
-
import { Injectable } from '@nestjs/common';
|
|
1
|
+
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';
|
|
5
7
|
|
|
6
8
|
@Injectable()
|
|
7
9
|
export class RoleRepository {
|
|
8
10
|
constructor(
|
|
9
11
|
@InjectRepository(Role)
|
|
10
12
|
private readonly roleRepo: Repository<Role>,
|
|
13
|
+
@InjectRepository(ModuleAccess)
|
|
14
|
+
private readonly moduleAccessRepo: Repository<ModuleAccess>
|
|
11
15
|
) {}
|
|
12
16
|
|
|
13
17
|
async findByCode(code: string): Promise<Role | null> {
|
|
14
18
|
return this.roleRepo.findOne({ where: { code } });
|
|
15
19
|
}
|
|
20
|
+
|
|
21
|
+
async createRole(
|
|
22
|
+
{
|
|
23
|
+
name,
|
|
24
|
+
description,
|
|
25
|
+
status = 'ACTIVE',
|
|
26
|
+
copyFromRoleId,
|
|
27
|
+
}: {
|
|
28
|
+
name: string;
|
|
29
|
+
description?: string;
|
|
30
|
+
status?: 'ACTIVE' | 'INACTIVE';
|
|
31
|
+
copyFromRoleId?: number;
|
|
32
|
+
},
|
|
33
|
+
loggedInUser: UserData,
|
|
34
|
+
) {
|
|
35
|
+
if (await this.isRoleNameExists(name)) {
|
|
36
|
+
throw new BadRequestException('Role name already exists.');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const sourceRole = copyFromRoleId
|
|
40
|
+
? await this.roleRepo.findOne({ where: { id: copyFromRoleId } })
|
|
41
|
+
: null;
|
|
42
|
+
|
|
43
|
+
if (copyFromRoleId && !sourceRole) {
|
|
44
|
+
throw new BadRequestException('Source role not found.');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const newRole = this.roleRepo.create({
|
|
48
|
+
name,
|
|
49
|
+
description,
|
|
50
|
+
status,
|
|
51
|
+
created_by: loggedInUser.id,
|
|
52
|
+
created_date: new Date(),
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
const savedRole = await this.roleRepo.save(newRole);
|
|
56
|
+
savedRole.code = `ROL${savedRole.id}`;
|
|
57
|
+
await this.roleRepo.save(savedRole);
|
|
58
|
+
|
|
59
|
+
if (sourceRole) {
|
|
60
|
+
const sourcePermissions = await this.moduleAccessRepo.find({
|
|
61
|
+
where: { role_code: sourceRole.code },
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
const clonedPermissions = sourcePermissions.map(perm =>
|
|
65
|
+
this.moduleAccessRepo.create({
|
|
66
|
+
role_code: savedRole.code,
|
|
67
|
+
module_code: perm.module_code,
|
|
68
|
+
action_type: perm.action_type,
|
|
69
|
+
access_flag: perm.access_flag,
|
|
70
|
+
app_code: perm.app_code,
|
|
71
|
+
}),
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
await this.moduleAccessRepo.save(clonedPermissions);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return {
|
|
78
|
+
success: true,
|
|
79
|
+
msg: 'Role created successfully',
|
|
80
|
+
role: {
|
|
81
|
+
id: savedRole.id,
|
|
82
|
+
name: savedRole.name,
|
|
83
|
+
status: savedRole.status,
|
|
84
|
+
code: savedRole.code,
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async updateRole(
|
|
90
|
+
roleId: number,
|
|
91
|
+
{
|
|
92
|
+
name,
|
|
93
|
+
description,
|
|
94
|
+
status = 'ACTIVE',
|
|
95
|
+
copyFromRoleId,
|
|
96
|
+
}: {
|
|
97
|
+
name: string;
|
|
98
|
+
description?: string;
|
|
99
|
+
status?: 'ACTIVE' | 'INACTIVE';
|
|
100
|
+
copyFromRoleId?: number;
|
|
101
|
+
},
|
|
102
|
+
loggedInUser: UserData,
|
|
103
|
+
) {
|
|
104
|
+
if (await this.isRoleNameExists(name, roleId)) {
|
|
105
|
+
throw new BadRequestException('Role name already exists.');
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const role = await this.roleRepo.findOne({ where: { id: roleId } });
|
|
109
|
+
if (!role) {
|
|
110
|
+
throw new BadRequestException('Role not found');
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
Object.assign(role, {
|
|
114
|
+
name,
|
|
115
|
+
description: description || '',
|
|
116
|
+
status,
|
|
117
|
+
modified_by: loggedInUser.id,
|
|
118
|
+
modified_date: new Date(),
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
await this.roleRepo.save(role);
|
|
122
|
+
|
|
123
|
+
if (copyFromRoleId) {
|
|
124
|
+
const sourceRole = await this.roleRepo.findOne({ where: { id: copyFromRoleId } });
|
|
125
|
+
if (!sourceRole) {
|
|
126
|
+
throw new BadRequestException('Source role not found');
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
await this.moduleAccessRepo.delete({ role_code: role.code });
|
|
130
|
+
|
|
131
|
+
const sourcePermissions = await this.moduleAccessRepo.find({
|
|
132
|
+
where: { role_code: sourceRole.code },
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
const clonedPermissions = sourcePermissions.map(perm =>
|
|
136
|
+
this.moduleAccessRepo.create({
|
|
137
|
+
role_code: role.code,
|
|
138
|
+
module_code: perm.module_code,
|
|
139
|
+
action_type: perm.action_type,
|
|
140
|
+
access_flag: perm.access_flag,
|
|
141
|
+
app_code: perm.app_code,
|
|
142
|
+
}),
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
await this.moduleAccessRepo.save(clonedPermissions);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return {
|
|
149
|
+
success: true,
|
|
150
|
+
msg: 'Role updated successfully',
|
|
151
|
+
role: {
|
|
152
|
+
id: role.id,
|
|
153
|
+
name: role.name,
|
|
154
|
+
status: role.status,
|
|
155
|
+
code: role.code,
|
|
156
|
+
},
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
private async isRoleNameExists(name: string, excludeRoleId?: number): Promise<boolean> {
|
|
161
|
+
const query = this.roleRepo
|
|
162
|
+
.createQueryBuilder('role')
|
|
163
|
+
.where('role.name = :name', { name });
|
|
164
|
+
|
|
165
|
+
if (excludeRoleId) {
|
|
166
|
+
query.andWhere('role.id != :excludeRoleId', { excludeRoleId });
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const existingRole = await query.getOne();
|
|
170
|
+
return !!existingRole;
|
|
171
|
+
}
|
|
16
172
|
}
|