Skip to content

Commit

Permalink
Merge pull request #223 from poojakarma/change_in_update_and_get_coho…
Browse files Browse the repository at this point in the history
…rtMember

(PS-1036) Filter member list according to status [] passed
  • Loading branch information
vaivk369 authored Jun 21, 2024
2 parents 4280f5c + d6a8a9e commit 8079219
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 37 deletions.
60 changes: 35 additions & 25 deletions src/adapters/postgres/cohortMembers-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,9 @@ export class PostgresCohortMembersService {
if (whereClause["role"]) {
where.push(["role", whereClause["role"]]);
}
if (whereClause["status"] && Array.isArray(whereClause["status"])) {
where.push(["status", whereClause["status"]]);
}
let options = [];
if (limit) {
options.push(['limit', limit]);
Expand Down Expand Up @@ -283,7 +286,7 @@ export class PostgresCohortMembersService {

public async createCohortMembers(
loginUser: any,
cohortMembers:CohortMembersDto,
cohortMembers: CohortMembersDto,
res: Response,
tenantId: string
) {
Expand Down Expand Up @@ -338,28 +341,34 @@ export class PostgresCohortMembersService {
let optionsCase = ``;
let isRoleCondition = 0;
if (where.length > 0) {
whereCase = `where `;
whereCase = `WHERE `;
where.forEach((value, index) => {
if (value[0] == "role") {
if (value[0] === "role") {
isRoleCondition = 1;
whereCase += `R."name"='${value[1]}' `;
} else {
whereCase += `CM."${value[0]}"='${value[1]}' `;
whereCase += `R."name"='${value[1]}'`;
}
else {
if (value[0] === "status") {
const statusValues = value[1].map(status => `'${status}'`).join(', ');
whereCase += `CM."status" IN (${statusValues})`;
} else {
whereCase += `CM."${value[0]}"='${value[1]}'`;
}
}
if (index != (where.length - 1)) {
whereCase += ` AND `
if (index !== where.length - 1) {
whereCase += ` AND `;
}
})
});
}

if (options.length > 0) {
options.forEach((value, index) => {
optionsCase = `${value[0]} ${value[1]} `;
})
}

if (isRoleCondition == 0) {
query = `SELECT U."userId", U.username, U.name, R.name AS role, U.district, U.state,U.mobile, CM."memberStatus", CM."statusReason",CM."cohortMembershipId" FROM public."CohortMembers" CM
query = `SELECT U."userId", U.username, U.name, R.name AS role, U.district, U.state,U.mobile,
CM."status", CM."statusReason",CM."cohortMembershipId" FROM public."CohortMembers" CM
INNER JOIN public."Users" U
ON CM."userId" = U."userId"
INNER JOIN public."UserRolesMapping" UR
Expand All @@ -368,7 +377,8 @@ export class PostgresCohortMembersService {
ON R."roleId" = UR."roleId" ${whereCase} ${optionsCase}`;
}
else {
query = `SELECT U."userId", U.username, U.name, R.name AS role, U.district, U.state,U.mobile, CM."memberStatus", CM."statusReason",CM."cohortMembershipId" FROM public."CohortMembers" CM
query = `SELECT U."userId", U.username, U.name, R.name AS role, U.district, U.state,U.mobile,
CM."status", CM."statusReason",CM."cohortMembershipId" FROM public."CohortMembers" CM
INNER JOIN public."Users" U
ON CM."userId" = U."userId"
INNER JOIN public."UserRolesMapping" UR
Expand Down Expand Up @@ -446,10 +456,10 @@ export class PostgresCohortMembersService {
}
}

public async checkUserExist(userId){
public async checkUserExist(userId) {
const existUser = await this.usersRepository.findOne({
where: {
userId:userId,
userId: userId,
}
})
if (!existUser) {
Expand All @@ -458,11 +468,11 @@ export class PostgresCohortMembersService {
return existUser;
}

public async checkCohortExist(cohortId){
public async checkCohortExist(cohortId) {

const existCohort = await this.cohortRepository.findOne({
where: {
cohortId:cohortId
cohortId: cohortId
}
})
if (!existCohort) {
Expand All @@ -471,11 +481,11 @@ export class PostgresCohortMembersService {
return existCohort;
}

public async cohortUserMapping(userId,cohortId){
public async cohortUserMapping(userId, cohortId) {
const mappingExist = await this.cohortMembersRepository.findOne({
where: {
userId: userId,
cohortId:cohortId
cohortId: cohortId
}
})
if (!mappingExist) {
Expand All @@ -497,12 +507,12 @@ export class PostgresCohortMembersService {
updatedBy: loginUser,
tenantId: tenantId
};

for (let userId of cohortMembersDto.userId) {
const userExists = await this.checkUserExist(userId);
if (!userExists) {
errors.push(`User with userId ${userId} does not exist.`);
continue;
continue;
}
for (let cohortId of cohortMembersDto.cohortId) {
const cohortMembers = {
Expand All @@ -516,27 +526,27 @@ export class PostgresCohortMembersService {
errors.push(`Cohort with cohortId ${cohortId} does not exist.`);
continue;
}

const mappingExists = await this.cohortUserMapping(userId, cohortId);
if (mappingExists) {
errors.push(`Mapping already exists for userId ${userId} and cohortId ${cohortId}.`);
continue;
}

const result = await this.cohortMembersRepository.save(cohortMembers);
results.push(result);
} catch (error) {
errors.push(`Error saving cohort member with userId ${userId} and cohortId ${cohortId}: ${error.message}`);
}
}
}

if (errors.length > 0) {
return APIResponse.success(response, APIID.COHORT_MEMBER_CREATE, { results, errors }, HttpStatus.CREATED, "Cohort Members Created with some errors");
}

return APIResponse.success(response, APIID.COHORT_MEMBER_CREATE, results, HttpStatus.CREATED, "Cohort Members Created Successfully");
}


}
4 changes: 2 additions & 2 deletions src/cohortMembers/dto/cohortMember-update.dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Exclude, Expose } from "class-transformer";
import { ApiProperty } from "@nestjs/swagger";
import { IsEnum, IsOptional } from "class-validator";
import { IsEnum, IsOptional, IsString, ValidateIf } from "class-validator";
import { MemberStatus } from "../entities/cohort-member.entity";
export class CohortMembersUpdateDto {
@Expose()
Expand Down Expand Up @@ -38,7 +38,7 @@ export class CohortMembersUpdateDto {
description: "The status of the cohort members",
})
@IsEnum(MemberStatus)
memberStatus: string;
status: string;

@ApiProperty({
type: String,
Expand Down
20 changes: 10 additions & 10 deletions src/cohortMembers/entities/cohort-member.entity.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn } from 'typeorm';

export enum MemberStatus {
ACTIVE = 'active',
INACTIVE = 'inactive',
DROPOUT = 'dropout',
ARCHIVED = 'archived'
ACTIVE = 'active',
INACTIVE = 'inactive',
DROPOUT = 'dropout',
ARCHIVED = 'archived'
}

@Entity('CohortMembers')
export class CohortMembers {
@PrimaryGeneratedColumn('uuid')
cohortMembershipId: string;

@Column({ type: 'uuid'})
@Column({ type: 'uuid' })
cohortId: string;

@Column({ type: 'uuid'})
@Column({ type: 'uuid' })
userId: string;

@CreateDateColumn({ type: 'date', default: () => 'CURRENT_DATE' })
Expand All @@ -34,9 +34,9 @@ export class CohortMembers {
statusReason: string;

@Column({
type: 'enum',
enum: MemberStatus,
default: MemberStatus.ACTIVE
type: 'enum',
enum: MemberStatus,
default: MemberStatus.ACTIVE
})
memberStatus: MemberStatus;
status: MemberStatus;
}

0 comments on commit 8079219

Please sign in to comment.