Bansari Akhani commited on
Commit
2f7395b
·
1 Parent(s): e737a5f

get user with associations - role & it's permissions

Browse files
src/controllers/user.controller.ts CHANGED
@@ -285,39 +285,30 @@ const getCurrentUser = async (req: AuthenticatedRequest, res: Response) => {
285
  return res.status(404).json({ error: 'User not found' });
286
  }
287
 
288
- const role = await Role.findByPk(user.role_id);
289
-
290
- if (!role) {
291
- return res.status(404).json({ error: 'Role not found' });
292
- }
293
-
294
- const rolePermissions = await RolePermission.findAll({
295
- where: { role_id: user.role_id },
296
- include: [{ model: Permission }],
 
 
 
 
 
 
 
297
  });
298
 
299
- if (!rolePermissions) {
300
- return res.status(404).json({ error: 'Role permissions not found' });
301
- }
302
-
303
- const permissions = JSON.parse(JSON.stringify(rolePermissions))
304
- const permissionNames = permissions.map((per: any) => per.Permission.permission_name);
305
-
306
- const userResponse = {
307
- id: user.id,
308
- name: user.name,
309
- email: user.email,
310
- role: {
311
- name: role.name,
312
- },
313
- permissions: permissionNames
314
- };
315
-
316
  return res.status(200).json({
317
- user: userResponse
318
  });
319
 
320
  } catch (error) {
 
321
  logger.error('Error fetching user:', error);
322
  return res.status(500).json({ error: 'Error while fetching User details.' });
323
  }
 
285
  return res.status(404).json({ error: 'User not found' });
286
  }
287
 
288
+ const userData = await User.findByPk(user.id, {
289
+ include: [
290
+ {
291
+ model: Role,
292
+ as: 'role',
293
+ include: [
294
+ {
295
+ model: Permission,
296
+ as : 'permissions',
297
+ through: {
298
+ attributes: [],
299
+ },
300
+ },
301
+ ],
302
+ },
303
+ ],
304
  });
305
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
306
  return res.status(200).json({
307
+ user: userData
308
  });
309
 
310
  } catch (error) {
311
+ console.log(error);
312
  logger.error('Error fetching user:', error);
313
  return res.status(500).json({ error: 'Error while fetching User details.' });
314
  }
src/models/permissions.ts CHANGED
@@ -8,6 +8,8 @@ import {
8
 
9
  import { sequelize } from './index';
10
  import { PermissionInterface } from '../shared/interfaces/permission.interface';
 
 
11
 
12
  class Permission extends Model<InferAttributes<Permission>, InferCreationAttributes<Permission>> implements PermissionInterface {
13
  declare id: CreationOptional<number>;
 
8
 
9
  import { sequelize } from './index';
10
  import { PermissionInterface } from '../shared/interfaces/permission.interface';
11
+ import Role from './roles';
12
+ import RolePermission from './rolePermissions';
13
 
14
  class Permission extends Model<InferAttributes<Permission>, InferCreationAttributes<Permission>> implements PermissionInterface {
15
  declare id: CreationOptional<number>;
src/models/rolePermissions.ts CHANGED
@@ -61,4 +61,4 @@ RolePermission.init(
61
  RolePermission.belongsTo(Role, { foreignKey: 'role_id' });
62
  RolePermission.belongsTo(Permission, { foreignKey: 'permission_id' });
63
 
64
- export default RolePermission
 
61
  RolePermission.belongsTo(Role, { foreignKey: 'role_id' });
62
  RolePermission.belongsTo(Permission, { foreignKey: 'permission_id' });
63
 
64
+ export default RolePermission
src/models/roles.ts CHANGED
@@ -7,6 +7,8 @@ import {
7
  } from 'sequelize';
8
  import { sequelize } from './index';
9
  import { RoleInterface } from '../shared/interfaces/role.interface';
 
 
10
 
11
  class Role extends Model<InferAttributes<Role>, InferCreationAttributes<Role>> implements RoleInterface {
12
  declare id: CreationOptional<number>;
@@ -39,4 +41,6 @@ Role.init(
39
  }
40
  );
41
 
 
 
42
  export default Role;
 
7
  } from 'sequelize';
8
  import { sequelize } from './index';
9
  import { RoleInterface } from '../shared/interfaces/role.interface';
10
+ import Permission from './permissions';
11
+ import RolePermission from './rolePermissions';
12
 
13
  class Role extends Model<InferAttributes<Role>, InferCreationAttributes<Role>> implements RoleInterface {
14
  declare id: CreationOptional<number>;
 
41
  }
42
  );
43
 
44
+ Role.belongsToMany(Permission, { through: 'role_permission', as : 'permissions' });
45
+
46
  export default Role;