Skip to content

Commit

Permalink
Merge pull request #217 from souravbhowmik1999/multipleParentId
Browse files Browse the repository at this point in the history
Task PS-1027 Cohort Management - Search cohort with multiple Parent Id
  • Loading branch information
vaivk369 authored Jun 19, 2024
2 parents e245a10 + 0817a17 commit 917c26a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 32 deletions.
27 changes: 6 additions & 21 deletions src/adapters/postgres/cohort-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { CohortCreateDto } from "src/cohort/dto/cohort-create.dto";
import { CohortUpdateDto } from "src/cohort/dto/cohort-update.dto";
import { FieldValuesDto } from "src/fields/dto/field-values.dto";
import { FieldValuesUpdateDto } from "src/fields/dto/field-values-update.dto";
import { IsNull, Not, Repository, getConnection, getRepository } from "typeorm";
import { IsNull, Not, Repository, getConnection, getRepository, In } from "typeorm";
import { Cohort } from "src/cohort/entities/cohort.entity";
import { Fields } from "src/fields/entities/fields.entity";
import { InjectRepository } from "@nestjs/typeorm";
Expand Down Expand Up @@ -487,24 +487,8 @@ export class PostgresCohortService {
});
}

if (whereClause['userId'] && !isUUID(whereClause['userId'])) {
return APIResponse.error(
response,
apiId,
`Invalid User ID format. It must be a valid UUID`,
`Invalid userId`,
(HttpStatus.BAD_REQUEST)
)
}

if (whereClause['cohortId'] && !isUUID(whereClause['cohortId'])) {
return APIResponse.error(
response,
apiId,
`Invalid Cohort ID format. It must be a valid UUID`,
`Invalid cohortID`,
(HttpStatus.BAD_REQUEST)
)
if (whereClause['parentId']) {
whereClause['parentId'] = In(whereClause['parentId']);
}

let results = {
Expand Down Expand Up @@ -557,20 +541,21 @@ export class PostgresCohortService {

let customFieldsData = await this.getCohortDataWithCustomfield(data.cohortId);
cohortAllData['customFields'] = customFieldsData
results.cohortDetails.push({ cohortData: cohortAllData });
results.cohortDetails.push(cohortAllData);
}
} else {
const [data, totalcount] = await this.cohortRepository.findAndCount({
where: whereClause,
skip: offset
});

const cohortData = data.slice(offset, offset + (limit));
count = totalcount;

for (let data of cohortData) {
let customFieldsData = await this.getCohortDataWithCustomfield(data.cohortId, data.type);
data['customFields'] = customFieldsData || [];
results.cohortDetails.push({ cohortData: data })
results.cohortDetails.push(data)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/cohort/cohort.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export class CohortController {
@UseFilters(new AllExceptionsFilter(APIID.COHORT_LIST))
@Post("/search")
@ApiBasicAuth("access-token")
@ApiBody({ type: CohortSearchDto })
// @ApiBody({ type: CohortSearchDto })
@ApiOkResponse({ description: "Cohort list" })
@ApiBadRequestResponse({ description: "Bad request." })
@ApiInternalServerErrorResponse({ description: "Internal Server Error." })
Expand Down
21 changes: 11 additions & 10 deletions src/cohort/dto/cohort-search.dto.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ApiProperty, ApiPropertyOptional } from "@nestjs/swagger";
import { IsBoolean, IsNotEmpty, IsNumber, IsNumberString, IsObject, IsOptional, IsString, IsUUID, ValidationArguments, ValidationOptions, registerDecorator } from "class-validator";
import { IsArray, IsBoolean, IsNotEmpty, IsNumber, IsNumberString, IsObject, IsOptional, IsString, IsUUID, ValidationArguments, ValidationOptions, registerDecorator } from "class-validator";
import { CohortDto } from "./cohort.dto";
import { Expose } from "class-transformer";

export class setFilters {
export class filtersProperty {
//userIdBy
@ApiProperty({
type: String,
Expand Down Expand Up @@ -40,17 +40,18 @@ export class setFilters {
@IsNotEmpty()
name?: string;

//name
//parentId
@ApiProperty({
type: String,
type: [String],
description: "Parent Id",
default: "",
default: [],
})
@Expose()
@IsOptional()
@IsString()
@IsNotEmpty()
parentId?: string;
@IsArray()
@IsNotEmpty({ each: true })
@IsUUID(undefined, { each: true })
parentId?: string[];
}

export class CohortSearchDto {
Expand All @@ -69,11 +70,11 @@ export class CohortSearchDto {
page: number;

@ApiProperty({
type: setFilters,
type: filtersProperty,
description: "Filters",
})
@IsObject()
filters: setFilters;
filters: filtersProperty;

constructor(partial: Partial<CohortSearchDto>) {
Object.assign(this, partial);
Expand Down

0 comments on commit 917c26a

Please sign in to comment.