From 01948396a79844c28e1a532300e7e405b9695dbc Mon Sep 17 00:00:00 2001 From: Shubham Date: Wed, 26 Jun 2024 12:43:11 +0530 Subject: [PATCH] Issue PS-1195 feat: Adddition of Deassign Cohort from User --- .../postgres/cohortMembers-adapter.ts | 49 ++++++++++++++----- .../dto/bulkMember-create.dto.ts | 11 ++++- src/user/user.controller.ts | 2 +- 3 files changed, 49 insertions(+), 13 deletions(-) diff --git a/src/adapters/postgres/cohortMembers-adapter.ts b/src/adapters/postgres/cohortMembers-adapter.ts index bae48089..be6d6024 100644 --- a/src/adapters/postgres/cohortMembers-adapter.ts +++ b/src/adapters/postgres/cohortMembers-adapter.ts @@ -22,6 +22,7 @@ import { Cohort } from "src/cohort/entities/cohort.entity"; import APIResponse from "src/common/responses/response"; import { Response } from "express"; import { APIID } from 'src/common/utils/api-id.config'; +import { MemberStatus } from "src/cohortMembers/entities/cohort-member.entity"; @Injectable() export class PostgresCohortMembersService { @@ -485,7 +486,7 @@ export class PostgresCohortMembersService { const mappingExist = await this.cohortMembersRepository.findOne({ where: { userId: userId, - cohortId: cohortId + cohortId: cohortId, } }) if (!mappingExist) { @@ -496,7 +497,7 @@ export class PostgresCohortMembersService { public async createBulkCohortMembers( loginUser: any, - cohortMembersDto: { userId: string[], cohortId: string[] }, + cohortMembersDto: { userId: string[], cohortId: string[], removeCohortId?: string[] }, response: Response, tenantId: string ) { @@ -507,13 +508,36 @@ 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; } + + // Handling of Removing Cohort from user + if (cohortMembersDto.removeCohortId && cohortMembersDto.removeCohortId.length > 0) { + for (let removeCohortId of cohortMembersDto.removeCohortId) { + try { + const cohortExists = await this.checkCohortExist(removeCohortId); + if (!cohortExists) { + errors.push(`Cohort with cohortId ${removeCohortId} does not exist.`); + continue; + } + const updateCohort = await this.cohortMembersRepository.update({ userId, cohortId: removeCohortId }, { status: MemberStatus.ARCHIVED }); + if(updateCohort.affected === 0){ + results.push({message:`Cohort Id ${removeCohortId} is not mapped to user Id${userId}} `}); + }else{ + results.push({message:`Cohort Id ${removeCohortId} status updated for This user Id${userId}} `}); + } + } catch (error) { + errors.push(`Error updating cohort member with userId ${userId} and cohortId ${removeCohortId}: ${error.message}`); + } + } + } + + // Handling of Addition of User in Cohort for (let cohortId of cohortMembersDto.cohortId) { const cohortMembers = { ...cohortMembersBase, @@ -526,13 +550,17 @@ 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; + if (mappingExists.status === MemberStatus.ACTIVE) { + errors.push(`Mapping already exists for userId ${userId} and cohortId ${cohortId}.`); + continue; + } else if (mappingExists.status === MemberStatus.ARCHIVED) { + const updateCohort = await this.cohortMembersRepository.update({ userId, cohortId }, { status: MemberStatus.ACTIVE }); + results.push(updateCohort); + continue; + } } - const result = await this.cohortMembersRepository.save(cohortMembers); results.push(result); } catch (error) { @@ -540,13 +568,12 @@ export class PostgresCohortMembersService { } } } - + 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"); } - - + } diff --git a/src/cohortMembers/dto/bulkMember-create.dto.ts b/src/cohortMembers/dto/bulkMember-create.dto.ts index 72e8c421..ec95d434 100644 --- a/src/cohortMembers/dto/bulkMember-create.dto.ts +++ b/src/cohortMembers/dto/bulkMember-create.dto.ts @@ -1,13 +1,22 @@ -import { IsArray, IsUUID, ArrayNotEmpty } from 'class-validator'; +import { IsArray, IsUUID, ArrayNotEmpty, IsOptional, IsNotEmpty } from 'class-validator'; export class BulkCohortMember { @IsArray() @ArrayNotEmpty() + @IsNotEmpty() @IsUUID('4', { each: true }) userId: string[]; @IsArray() @ArrayNotEmpty() + @IsNotEmpty() @IsUUID('4', { each: true }) cohortId: string[]; + + @IsArray() + @ArrayNotEmpty() + @IsOptional() + @IsUUID('4', { each: true }) + removeCohortId: string[]; + } diff --git a/src/user/user.controller.ts b/src/user/user.controller.ts index e8457ff7..78d22be3 100644 --- a/src/user/user.controller.ts +++ b/src/user/user.controller.ts @@ -197,7 +197,7 @@ export class UserController { @SerializeOptions({ strategy: "excludeAll", }) - public async deleteUserById( + public async deleteUserById( @Headers() headers, @Param("userId") userId: string, @Req() request: Request,