rez_core 1.0.60 → 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.
Files changed (29) hide show
  1. package/dist/module/module/controller/module-access.controller.d.ts +1 -37
  2. package/dist/module/module/controller/module-access.controller.js +2 -34
  3. package/dist/module/module/controller/module-access.controller.js.map +1 -1
  4. package/dist/module/module/repository/module-access.repository.d.ts +1 -35
  5. package/dist/module/module/repository/module-access.repository.js +2 -104
  6. package/dist/module/module/repository/module-access.repository.js.map +1 -1
  7. package/dist/module/module/service/module-access.service.d.ts +1 -36
  8. package/dist/module/module/service/module-access.service.js +2 -44
  9. package/dist/module/module/service/module-access.service.js.map +1 -1
  10. package/dist/module/user/controller/role.controller.d.ts +41 -0
  11. package/dist/module/user/controller/role.controller.js +63 -0
  12. package/dist/module/user/controller/role.controller.js.map +1 -0
  13. package/dist/module/user/repository/role.repository.d.ts +35 -1
  14. package/dist/module/user/repository/role.repository.js +105 -2
  15. package/dist/module/user/repository/role.repository.js.map +1 -1
  16. package/dist/module/user/service/role.service.d.ts +35 -1
  17. package/dist/module/user/service/role.service.js +39 -2
  18. package/dist/module/user/service/role.service.js.map +1 -1
  19. package/dist/module/user/user.module.js +4 -2
  20. package/dist/module/user/user.module.js.map +1 -1
  21. package/dist/tsconfig.build.tsbuildinfo +1 -1
  22. package/package.json +1 -1
  23. package/src/module/module/controller/module-access.controller.ts +2 -36
  24. package/src/module/module/repository/module-access.repository.ts +0 -151
  25. package/src/module/module/service/module-access.service.ts +1 -62
  26. package/src/module/user/controller/role.controller.ts +55 -0
  27. package/src/module/user/repository/role.repository.ts +157 -1
  28. package/src/module/user/service/role.service.ts +70 -2
  29. package/src/module/user/user.module.ts +4 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rez_core",
3
- "version": "1.0.60",
3
+ "version": "1.0.61",
4
4
  "description": "",
5
5
  "author": "",
6
6
  "private": false,
@@ -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
- @Post('create-role')
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
- async createRole(
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
  }
@@ -1,12 +1,19 @@
1
- import { Injectable } from '@nestjs/common';
1
+ import { BadRequestException, Injectable } from '@nestjs/common';
2
2
  import { EntityServiceImpl } from '../../meta/service/entity-service-impl.service';
3
3
  import { BaseEntity } from '../../meta/entity/base-entity.entity';
4
4
  import { UserData } from '../entity/user.entity';
5
5
  import { Role } from '../entity/role.entity';
6
+ import { ENTITYTYPE_ROLE } from 'src/constant/global.constant';
7
+ import { RoleRepository } from '../repository/role.repository';
8
+ import { EntityManager } from 'typeorm';
6
9
 
7
10
  @Injectable()
8
11
  export class RoleService extends EntityServiceImpl {
9
- constructor() {
12
+ constructor(
13
+ private readonly roleRepository: RoleRepository,
14
+ private readonly entityManager: EntityManager,
15
+
16
+ ) {
10
17
  super();
11
18
  }
12
19
 
@@ -18,4 +25,65 @@ export class RoleService extends EntityServiceImpl {
18
25
 
19
26
  return await super.createEntity(role, loggedInUser);
20
27
  }
28
+
29
+ async createRole(
30
+ body: {
31
+ name: string;
32
+ description?: string;
33
+ status?: 'ACTIVE' | 'INACTIVE';
34
+ copyFromRoleId?: number;
35
+ },
36
+ loggedInUser: UserData,
37
+ ) {
38
+ return this.roleRepository.createRole(body, loggedInUser);
39
+ }
40
+
41
+ async updateRole(
42
+ id: number,
43
+ body: {
44
+ name?: string;
45
+ description?: string;
46
+ status?: 'ACTIVE' | 'INACTIVE';
47
+ copyFromRoleId?: number;
48
+ },
49
+ loggedInUser: UserData,
50
+ ) {
51
+ if (!body.name) {
52
+ throw new BadRequestException('Role name is required');
53
+ }
54
+
55
+ if (body.status === 'INACTIVE') {
56
+ //get role by id
57
+ const entityData = await super.getEntityData(
58
+ ENTITYTYPE_ROLE,
59
+ id,
60
+ );
61
+
62
+ const associatedUsers = await this.entityManager.query(`SELECT map.*
63
+ FROM cr_user_role_mapping map
64
+ JOIN cr_user usr ON map.user_id = usr.id
65
+ WHERE usr.status = 'ACTIVE' and map.role_id = ${entityData?.id}`);
66
+
67
+ if (associatedUsers.length > 0) {
68
+ throw new BadRequestException(
69
+ 'Cannot deactivate role as it is associated with active users',
70
+ );
71
+ }
72
+
73
+ if (!entityData) {
74
+ throw new BadRequestException('Role not found');
75
+ }
76
+ const role = entityData as Role;
77
+ if (role.is_system === 1) {
78
+ throw new BadRequestException('Cannot deactivate system role');
79
+ }
80
+ }
81
+
82
+ return this.roleRepository.updateRole(id, {
83
+ name: body.name,
84
+ description: body.description,
85
+ status: body.status,
86
+ copyFromRoleId: body.copyFromRoleId,
87
+ }, loggedInUser);
88
+ }
21
89
  }
@@ -19,10 +19,12 @@ import { UserRoleMapping } from './entity/user-role-mapping.entity';
19
19
  import { UserRoleMappingRepository } from './repository/user-role-mapping.repository';
20
20
  import { UserRoleMappingService } from './service/user-role-mapping.service';
21
21
  import { RoleRepository } from './repository/role.repository';
22
+ import { ModuleAccess } from '../module/entity/module-access.entity';
23
+ import { RoleController } from './controller/role.controller';
22
24
 
23
25
  @Module({
24
26
  imports: [
25
- TypeOrmModule.forFeature([UserData, UserSession, Role, UserRoleMapping]),
27
+ TypeOrmModule.forFeature([UserData, UserSession, Role, UserRoleMapping,ModuleAccess]),
26
28
  EntityModule,
27
29
  UtilsModule,
28
30
  AuthModule,
@@ -47,6 +49,6 @@ import { RoleRepository } from './repository/role.repository';
47
49
  RoleRepository,
48
50
  LoginService,
49
51
  ],
50
- controllers: [LoginController, UserController],
52
+ controllers: [LoginController, UserController,RoleController],
51
53
  })
52
54
  export class UserModule {}