Skip to content

Commit

Permalink
Merge pull request #218 from Shubham4026/backend_qa
Browse files Browse the repository at this point in the history
Issue PS-1024 fix: Updated auth.user API to get Role and privilege
  • Loading branch information
vaivk369 authored Jun 19, 2024
2 parents 9febfd3 + 0933680 commit e245a10
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 16 deletions.
8 changes: 6 additions & 2 deletions src/adapters/postgres/postgres-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { UserTenantMapping } from "src/userTenantMapping/entities/user-tenant-ma
import { Tenants } from "src/userTenantMapping/entities/tenant.entity";
import { UserRoleMapping } from "src/rbac/assign-role/entities/assign-role.entity";
import { Role } from "src/rbac/role/entities/role.entity";
import { PostgresRoleService } from "./rbac/role-adapter";
import { RolePrivilegeMapping } from "src/rbac/assign-privilege/entities/assign-privilege.entity";


@Module({
Expand All @@ -30,13 +32,15 @@ import { Role } from "src/rbac/role/entities/role.entity";
UserTenantMapping,
Tenants,
UserRoleMapping,
Role
Role,
RolePrivilegeMapping
])
],
providers: [
PostgresUserService,
PostgresAttendanceService,
PostgresFieldsService
PostgresFieldsService,
PostgresRoleService,
],
exports: [
PostgresUserService,
Expand Down
59 changes: 45 additions & 14 deletions src/adapters/postgres/user-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ import APIResponse from 'src/common/responses/response';
import { Response, query } from 'express';
import { APIID } from 'src/common/utils/api-id.config';
import { IServicelocator } from '../userservicelocator';
import { PostgresFieldsService } from "./fields-adapter";
import { CustomFieldsValidation } from "@utils/custom-field-validation";
import { PostgresFieldsService } from "./fields-adapter"
import { PostgresRoleService } from './rbac/role-adapter';
import { CustomFieldsValidation } from '@utils/custom-field-validation';
// import {PostgresS}

@Injectable()
export class PostgresUserService implements IServicelocator {
axios = require("axios");
Expand All @@ -52,6 +55,7 @@ export class PostgresUserService implements IServicelocator {
@InjectRepository(Role)
private roleRepository: Repository<Role>,
private fieldsService: PostgresFieldsService,
private readonly postgresRoleService: PostgresRoleService
) { }

async searchUser(tenantId: string,
Expand Down Expand Up @@ -202,9 +206,9 @@ export class PostgresUserService implements IServicelocator {
where: {
roleId: getRole.roleId,
},
select: ["title"]
select: ["title",'code']
})
return role
return role;
}

async findUserDetails(userId, username?: any) {
Expand All @@ -220,22 +224,49 @@ export class PostgresUserService implements IServicelocator {
if (!userDetails) {
return false;
}
const tenentDetails = await this.allUsersTenent(userDetails.userId)
const tenentDetails = await this.userTenantRoleData(userDetails.userId)
userDetails['tenantData'] = tenentDetails;
return userDetails;
}
async allUsersTenent(userId: string) {
const query = `
SELECT T.name AS tenantName, T."tenantId", UTM."Id" AS userTenantMappingId
FROM public."UserTenantMapping" UTM
LEFT JOIN public."Tenants" T
ON T."tenantId" = UTM."tenantId"
WHERE UTM."userId" = $1`;

async userTenantRoleData(userId: string) {
const query = `SELECT T.name AS tenantName, T."tenantId", UTM."Id" AS userTenantMappingId
FROM public."UserTenantMapping" UTM
LEFT JOIN public."Tenants" T
ON T."tenantId" = UTM."tenantId"
WHERE UTM."userId" = $1;`;

const result = await this.usersRepository.query(query, [userId]);
return result;
}

const combinedResult = [];
let roleArray = []
for (let data of result) {
const roleData = await this.postgresRoleService.findUserRoleData(userId, data.tenantId);
if (roleData.length > 0) {
roleArray.push(roleData[0].roleid)
const roleId = roleData[0].roleid;
const roleName = roleData[0].title;

const privilegeData = await this.postgresRoleService.findPrivilegeByRoleId(roleArray);
const privileges = privilegeData.map(priv => priv.name);

combinedResult.push({
tenantName: data.tenantname,
tenantId: data.tenantId,
userTenantMappingId: data.usertenantmappingid,
roleId: roleId,
roleName: roleName,
privileges: privileges
});
}
}

return combinedResult;
}




async updateUser(userDto, response: Response) {
const apiId = APIID.USER_UPDATE;
try {
Expand Down

0 comments on commit e245a10

Please sign in to comment.