Skip to content

Commit

Permalink
Merge pull request #100 from Shubham4026/shishka_main_testing
Browse files Browse the repository at this point in the history
Task #216655 : Created Cohort Details API with User and Cohort ID Support
  • Loading branch information
vijaykhollam authored Apr 8, 2024
2 parents 9316a5f + d765261 commit 9b32fb3
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 87 deletions.
61 changes: 25 additions & 36 deletions src/cohort/cohort.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
UseGuards,
ValidationPipe,
UsePipes,
Query,
} from "@nestjs/common";
import { CohortSearchDto } from "./dto/cohort-search.dto";
import { Request } from "@nestjs/common";
Expand All @@ -35,6 +36,7 @@ import { CohortAdapter } from "./cohortadapter";
import { CohortCreateDto } from "./dto/cohort-create.dto";
import { CohortService } from "./cohort.service";
import { JwtAuthGuard } from "src/common/guards/keycloak.guard";
import { QueryParamsDto } from "./dto/query-params.dto";

@ApiTags("Cohort")
@Controller("cohort")
Expand Down Expand Up @@ -80,34 +82,8 @@ export class CohortController {
);
return response.status(result.statusCode).json(result);
}

@Get("cohortList/:id")
@ApiBasicAuth("access-token")
@ApiCreatedResponse({ description: "Cohort detail" })
@ApiForbiddenResponse({ description: "Forbidden" })
@SerializeOptions({
strategy: "excludeAll",
})
@ApiHeader({
name: "tenantid",
})
public async getCohortList(
@Headers() headers,
@Param("id") id: string,
@Req() request: Request,
@Res() response: Response
) {
let tenantid = headers["tenantid"];
const result = await this.cohortAdapter.buildCohortAdapter().getCohortList(
tenantid,
id,
request,
response
);
return response.status(result.statusCode).json(result);
}

@Get("cohortDetails/:id")

@Get("cohortDetails?")
@ApiBasicAuth("access-token")
@ApiCreatedResponse({ description: "Cohort details" })
@ApiForbiddenResponse({ description: "Forbidden" })
Expand All @@ -119,17 +95,30 @@ export class CohortController {
})
public async getCohortDetails(
@Headers() headers,
@Param("id") cohortId: string,
@Query() queryParams: QueryParamsDto,
@Req() request: Request,
@Res() response: Response
) {
let tenantid = headers["tenantid"];
return this.cohortService.getCohortsDetails(
tenantid,
cohortId,
request,
response
);
if(!Object.keys(queryParams).length){
return response.status(400).json({
message:"Please enter Query Params"
})
}
queryParams.name = queryParams.name.toLocaleLowerCase();
let data;
let tenantId = request.headers['tenantId']
if (queryParams?.name=== 'user') {
data=queryParams
return this.cohortService.getCohortsDetails(data,request,response)
} else if (queryParams?.name === 'cohort') {
data=queryParams
return this.cohortService.getCohortsDetails(data,request,response)
} else {
return response.status(400).json({
message:"Invaid Parameters.",
data:`Valid format should be name="cohortoruser" and id=value`
})
}
}

// search
Expand Down
103 changes: 52 additions & 51 deletions src/cohort/cohort.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,72 +37,70 @@ export class CohortService {
private fieldsService: FieldsService,
) { }

public async getCohortsDetails(tenantId: string,
cohortId: string,
public async getCohortsDetails(userData,
request: any,
response: any){
const apiId = "api.concept.cohortDetails";
let cohortName = await this.cohortRepository.findOne({
where:{cohortId}
})
// let result = {
// cohortData: [],
// };
let cohortData = {
cohortId: cohortId,
name:cohortName.name,
parentId:cohortName.parentId,
customField:{}
};
const getDetails = await this.getCohortListDetails(cohortId);
cohortData.customField=getDetails
// result.cohortData.push(cohortData);
return response
.status(HttpStatus.OK)
.send(
APIResponse.success(
apiId,
cohortData,
"OK"
)
);
}

public async getCohortList(
tenantId: string,
userId: string,
request: any,
response: any
) {
const apiId = "api.concept.editminiScreeningAnswer";
let apiId = 'api.concept.getCohortDetails'
try {
let findCohortId = await this.findCohortName(userId);
if(userData.name==='user'){
let findCohortId = await this.findCohortName(userData?.id);
let result = {
cohortData: [],
};

for (let data of findCohortId) {
let cohortData = {
cohortId: data.cohortId,
cohortId: data?.cohortId,
name:data.name,
parentId:data.parentId,
parentId:data?.parentId,
customField:{}
};
const getDetails = await this.getCohortListDetails(data.cohortId);
const getDetails = await this.getCohortListDetails(data?.cohortId);
cohortData.customField=getDetails
result.cohortData.push(cohortData);
}

return new SuccessResponse({
statusCode: HttpStatus.OK,
message: "Ok.",
data: result,
});
return response
.status(HttpStatus.OK)
.send(
APIResponse.success(
apiId,
result,
"OK"
)
);
}else{
let cohortName = await this.cohortRepository.findOne({
where:{cohortId:userData?.id},
select:['name','parentId']
})
let cohortData = {
cohortId: userData?.id,
name:cohortName?.name,
parentId:cohortName?.parentId,
customField:{}
};
const getDetails = await this.getCohortListDetails(userData?.id);
cohortData.customField=getDetails
return response
.status(HttpStatus.OK)
.send(
APIResponse.success(
apiId,
cohortData,
"OK"
)
);
}
} catch (error) {
return new ErrorResponseTypeOrm({
statusCode: HttpStatus.INTERNAL_SERVER_ERROR,
errorMessage: error,
});
return response
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.send(
APIResponse.error(
apiId,
"Something went wrong",
`Failure Retrieving Cohort Member. Error is: ${error}`,
"INTERNAL_SERVER_ERROR"
)
);
}
}

Expand All @@ -112,6 +110,9 @@ export class CohortService {
LEFT JOIN public."Cohort" AS c ON cm."cohortId" = c."cohortId"
WHERE cm."userId"=$1 AND c.status=true`;
let result = await this.cohortMembersRepository.query(query, [userId]);
// if(!result.length){
// return null;
// }
return result;
}

Expand Down
11 changes: 11 additions & 0 deletions src/cohort/dto/query-params.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { IsNotEmpty, IsString, IsInt } from 'class-validator';

export class QueryParamsDto {
@IsNotEmpty()
@IsString()
name: string;

@IsNotEmpty()
@IsString()
id: string;
}

0 comments on commit 9b32fb3

Please sign in to comment.