Skip to content

Commit

Permalink
Merge pull request #229 from Shubham4026/cohort_hierarchy
Browse files Browse the repository at this point in the history
Isssue PS-1209 feat: API to fetch mycohort list
  • Loading branch information
vaivk369 authored Jun 28, 2024
2 parents 809818d + 9202cf3 commit 8cd3f1d
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/adapters/cohortservicelocator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export interface IServicelocatorcohort {
searchCohort(tenantid, request: any, cohortSearchDto: CohortSearchDto,response);
updateCohort(cohortId: string, request: any, cohortUpdateDto: CohortUpdateDto,response);
updateCohortStatus(cohortId: string, request: any,response);
getCohortHierarchyData(requiredData,response)
}
3 changes: 3 additions & 0 deletions src/adapters/hasura/cohort.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export class HasuraCohortService implements IServicelocatorcohort {
private httpService: HttpService,
private fieldsService: FieldsService
) {}
getCohortHierarchyData(requiredData: any, response: any) {
throw new Error("Method not implemented.");
}
public async getCohortList(tenantid: any, id: any, request: any, response: any) {
}

Expand Down
154 changes: 142 additions & 12 deletions src/adapters/postgres/cohort-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class PostgresCohortService {
parentId: data.parentId,
customField: {}
};
const getDetails = await this.getCohortListDetails(data.cohortId);
const getDetails = await this.getCohortCustomFieldDetails(data.cohortId,true);
cohortData.customField = getDetails
result.cohortData.push(cohortData);
}
Expand Down Expand Up @@ -128,28 +128,37 @@ export class PostgresCohortService {


public async findCohortName(userId: any) {
let query = `SELECT c."name",c."cohortId",c."parentId"
let query = `SELECT c."name",c."cohortId",c."parentId",c."type"
FROM public."CohortMembers" AS cm
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]);
return result;
}

public async getCohortListDetails(userId) {
let query = `SELECT DISTINCT f."label", fv."value", f."type", f."fieldParams"
FROM public."CohortMembers" cm
LEFT JOIN (
public async getCohortCustomFieldDetails(userId: string, fieldOption?: boolean) {
const query = `
SELECT DISTINCT
f."label",
fv."value",
f."type",
CASE
WHEN $2 = TRUE THEN f."fieldParams"
ELSE NULL
END as "fieldParams"
FROM public."CohortMembers" cm
LEFT JOIN (
SELECT DISTINCT ON (fv."fieldId", fv."itemId") fv.*
FROM public."FieldValues" fv
) fv ON fv."itemId" = cm."cohortId"
INNER JOIN public."Fields" f ON fv."fieldId" = f."fieldId"
WHERE cm."cohortId" = $1;`;
let result = await this.cohortMembersRepository.query(query, [
userId
]);
) fv ON fv."itemId" = cm."cohortId"
INNER JOIN public."Fields" f ON fv."fieldId" = f."fieldId"
WHERE cm."cohortId" = $1;
`;

let result = await this.cohortMembersRepository.query(query, [userId, fieldOption || false]);
return result;
}

public async validateFieldValues(field_value_array: string[]) {
let encounteredKeys = []
for (const fieldValue of field_value_array) {
Expand Down Expand Up @@ -649,4 +658,125 @@ export class PostgresCohortService {
}

}

private async getCohortHierarchy(parentId: string,customField?:Boolean): Promise<any> {
const childData = await this.cohortRepository.find({ where: { parentId } });
const hierarchy = [];
let customFieldDetails;
let childHierarchy
for (const data of childData) {
if(customField){
childHierarchy = await this.getCohortHierarchy(data.cohortId,customField);
customFieldDetails=await this.getCohortCustomFieldDetails(data.cohortId);
}else{
childHierarchy = await this.getCohortHierarchy(data.cohortId);
}
hierarchy.push({
cohortId: data.cohortId,
name: data.name,
parentId: data.parentId,
type: data.type,
customField:customFieldDetails || [],
childData: childHierarchy,
});
}
return hierarchy;
}

public async getCohortHierarchyData(requiredData, res) {
let apiId = APIID.COHORT_LIST;
if (!requiredData.getChildData) {
try {
let findCohortId = await this.findCohortName(requiredData.userId);
let result = {
cohortData: [],
};

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

return APIResponse.success(
res,
apiId,
result,
HttpStatus.OK,
"Cohort list fetched successfully"
);
} catch (error) {
const errorMessage = error.message || "Internal server error";
return APIResponse.error(
res,
apiId,
"Internal Server Error",
errorMessage,
HttpStatus.INTERNAL_SERVER_ERROR
);
}
}
if (requiredData.getChildData) {
let resultData = {
cohortName: "",
cohortId: "",
parentID: "",
type: "",
customField:[],
childData: [],
};
try {
let findCohortId = await this.findCohortName(requiredData.userId);
if (!findCohortId.length) {
return APIResponse.error(
res,
apiId,
"BAD_REQUEST",
`No Cohort Found for this User ID`,
HttpStatus.BAD_REQUEST
);
}
let resultDataList = [];

for (let cohort of findCohortId) {
resultData.cohortName = cohort.name;
resultData.cohortId = cohort.cohortId;
resultData.parentID = cohort.parentId;
resultData.type = cohort.type;
if(requiredData.customField){
resultData.childData = await this.getCohortHierarchy(cohort.cohortId,requiredData.customField);
resultData.customField= await this.getCohortCustomFieldDetails(cohort.cohortId)
}else{
resultData.childData = await this.getCohortHierarchy(cohort.cohortId,);
}
resultDataList.push(resultData);
}
return APIResponse.success(
res,
apiId,
resultData,
HttpStatus.OK,
"Cohort hierarchy fetched successfully"
);
} catch (error) {
const errorMessage = error.message || "Internal server error";
return APIResponse.error(
res,
apiId,
"Internal Server Error",
errorMessage,
HttpStatus.INTERNAL_SERVER_ERROR
);
}
}
}
}



31 changes: 31 additions & 0 deletions src/cohort/cohort.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
ApiOkResponse,
ApiNotFoundResponse,
ApiConflictResponse,
ApiQuery,
} from "@nestjs/swagger";
import {
Controller,
Expand All @@ -30,6 +31,8 @@ import {
UsePipes,
BadRequestException,
UseFilters,
ParseUUIDPipe,
Query,
} from "@nestjs/common";
import { CohortSearchDto } from "./dto/cohort-search.dto";
import { Request } from "@nestjs/common";
Expand Down Expand Up @@ -190,4 +193,32 @@ export class CohortController {
) {
return await this.cohortAdapter.buildCohortAdapter().updateCohortStatus(cohortId, request, response);
}

@UseFilters(new AllExceptionsFilter(APIID.COHORT_READ))
@Get("/mycohorts/:userId")
@ApiBasicAuth("access-token")
@ApiOkResponse({ description: "Cohort details Fetched Successfully" })
@ApiNotFoundResponse({ description: "User Not Found" })
@ApiInternalServerErrorResponse({ description: "Internal Server Error." })
@ApiBadRequestResponse({ description: "Bad Request" })
@ApiHeader({ name: "tenantid", })
@ApiQuery({ name: "children", required: false, type: Boolean })
@ApiQuery({ name: "customField", required: false, type: Boolean })
public async getCohortsHierarachyData(
@Request() request:Request,
@Param('userId', ParseUUIDPipe) userId: string,
@Query("children") children: string,
@Query("customField") customField: string | null = null,
@Res() response: Response
) {
const tenantId = request.headers["tenantid"];
const getChildDataValueBoolean = children === 'true';
let fieldValueBooelan = customField === 'true'
let requiredData = {
userId: userId,
getChildData:getChildDataValueBoolean,
customField: fieldValueBooelan
}
return await this.cohortAdapter.buildCohortAdapter().getCohortHierarchyData(requiredData,response)
}
}

0 comments on commit 8cd3f1d

Please sign in to comment.