Skip to content

Commit

Permalink
Merge pull request #149 from souravbhowmik1999/setValudation
Browse files Browse the repository at this point in the history
Task #218686 refactor findUserDetails to get tenants associated with user
  • Loading branch information
vaivk369 authored May 8, 2024
2 parents 743d3ab + faafadc commit 62cd67c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
20 changes: 18 additions & 2 deletions src/adapters/postgres/user-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,25 @@ export class PostgresUserService {
whereClause.username = username;
}
let userDetails = await this.usersRepository.findOne({
where: whereClause
where: whereClause,
select: ["userId", "username", "name", "role", "district","state","mobile"]
})

const tenentDetails = await this.allUsersTenent(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`;
const result = await this.usersRepository.query(query, [userId]);
return result;
}
async findCustomFields(userData, role) {
let customFields = await this.fieldsRepository.find({
Expand All @@ -284,7 +300,7 @@ export class PostgresUserService {
await this.updateBasicUserDetails(userDto.userId, userDto.userData);
updatedData['basicDetails'] = userDto.userData;
}
if (userDto.customFields.length > 0) {
if (userDto?.customFields?.length > 0) {
for (let data of userDto.customFields) {
const result = await this.updateCustomFields(userDto.userId, data);
if (result) {
Expand Down
11 changes: 10 additions & 1 deletion src/user/entities/user-entity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Entity, PrimaryColumn, Column, CreateDateColumn, UpdateDateColumn } from "typeorm";
import { Entity, PrimaryColumn, Column, CreateDateColumn, UpdateDateColumn, OneToMany } from "typeorm";
import { UserTenantMapping } from "src/userTenantMapping/entities/user-tenant-mapping.entity";

@Entity({ name: "Users" })
export class User {
Expand Down Expand Up @@ -53,4 +54,12 @@ export class User {
@Column({ default: "active" })
status: string;
userRoleMappings: User;


// @OneToMany(() => CohortMembers, cohortMember => cohortMember.cohort)
// cohortMembers: CohortMembers[];

@OneToMany(() => UserTenantMapping, userTenantMapping => userTenantMapping.user)
userTenantMapping: UserTenantMapping[];

}
12 changes: 11 additions & 1 deletion src/userTenantMapping/entities/user-tenant-mapping.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import {
Column,
CreateDateColumn,
UpdateDateColumn,
ManyToOne,
JoinColumn,
} from "typeorm";

import { User } from "src/user/entities/user-entity";

@Entity({ name: "UserTenantMapping" })
export class UserTenantMapping {
@PrimaryGeneratedColumn("uuid")
Expand Down Expand Up @@ -34,5 +37,12 @@ import {

@Column()
updatedBy: string;



@ManyToOne(() => User, user => user.userTenantMapping)
@JoinColumn({ name: 'userId' })
user: User;

}

0 comments on commit 62cd67c

Please sign in to comment.