Skip to content

Commit

Permalink
Issue PS-1195 feat: Adddition of Deassign Cohort from User
Browse files Browse the repository at this point in the history
  • Loading branch information
Shubham4026 committed Jun 26, 2024
1 parent 350fac9 commit 0194839
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 13 deletions.
49 changes: 38 additions & 11 deletions src/adapters/postgres/cohortMembers-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -485,7 +486,7 @@ export class PostgresCohortMembersService {
const mappingExist = await this.cohortMembersRepository.findOne({
where: {
userId: userId,
cohortId: cohortId
cohortId: cohortId,
}
})
if (!mappingExist) {
Expand All @@ -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
) {
Expand All @@ -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,
Expand All @@ -526,27 +550,30 @@ 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) {
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");
}



}
11 changes: 10 additions & 1 deletion src/cohortMembers/dto/bulkMember-create.dto.ts
Original file line number Diff line number Diff line change
@@ -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[];

}
2 changes: 1 addition & 1 deletion src/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 0194839

Please sign in to comment.