Skip to content

Commit

Permalink
feat: fix gets
Browse files Browse the repository at this point in the history
  • Loading branch information
9sneha-n committed Jan 30, 2025
1 parent b6ba458 commit 1a595ba
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 690 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"@dhis2/ui": "6.12.0",
"@emotion/react": "^11.11.1",
"@emotion/styled": "^11.11.0",
"@eyeseetea/d2-api": "1.14.0",
"@eyeseetea/d2-api": "1.17.0-beta.4",
"@eyeseetea/d2-ui-components": "2.7.0",
"@eyeseetea/feedback-component": "0.0.3",
"@material-ui/core": "4.12.4",
Expand Down
33 changes: 22 additions & 11 deletions src/data/repositories/PaginatedSurveyD2Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ import {
SURVEY_PATIENT_ID_TEA_ID,
WARD_ID_TEA_ID,
} from "../entities/D2Survey";
import { mapEventToSurvey, mapTrackedEntityToSurvey } from "../utils/surveyListMappers";
import {
mapEventToSurvey,
mapTrackedEntityToSurvey,
trackedEntityFields,
} from "../utils/surveyListMappers";
import { getSurveyChildCount, SurveyChildCountType } from "../utils/surveyChildCountHelper";

export class PaginatedSurveyD2Repository implements PaginatedSurveyRepository {
Expand Down Expand Up @@ -57,13 +61,13 @@ export class PaginatedSurveyD2Repository implements PaginatedSurveyRepository {
page: number,
pageSize: number
): FutureData<PaginatedReponse<Survey[]>> {
const ouMode = undefined;
const ouMode = "SELECTED";

const filterParentDEId = getParentDataElementForProgram(programId);

return apiToFuture(
this.api.tracker.trackedEntities.get({
fields: { attributes: true, enrollments: true, trackedEntity: true, orgUnit: true },
fields: trackedEntityFields,
program: programId,
orgUnit: orgUnitId,
ouMode: ouMode,
Expand All @@ -73,7 +77,8 @@ export class PaginatedSurveyD2Repository implements PaginatedSurveyRepository {
filter: `${filterParentDEId}:eq:${parentId}`,
})
).flatMap(trackedEntities => {
const surveys = mapTrackedEntityToSurvey(trackedEntities, surveyFormType);
const instances = trackedEntities.instances;
const surveys = mapTrackedEntityToSurvey(instances, surveyFormType);

const paginatedSurveys: PaginatedReponse<Survey[]> = {
pager: {
Expand All @@ -96,7 +101,7 @@ export class PaginatedSurveyD2Repository implements PaginatedSurveyRepository {
page: number,
pageSize: number
): FutureData<PaginatedReponse<Survey[]>> {
const ouMode = undefined;
const ouMode = "SELECTED";
return apiToFuture(
this.api.tracker.events.get({
fields: { $all: true },
Expand Down Expand Up @@ -133,15 +138,17 @@ export class PaginatedSurveyD2Repository implements PaginatedSurveyRepository {
): FutureData<PaginatedReponse<Survey[]>> {
return apiToFuture(
this.api.tracker.trackedEntities.get({
fields: { attributes: true, enrollments: true, trackedEntity: true, orgUnit: true },
fields: trackedEntityFields,
program: PPS_PATIENT_REGISTER_ID,
orgUnit: orgUnitId,
ouMode: "SELECTED",
pageSize: 10,
totalPages: true,
filter: ` ${SURVEY_PATIENT_ID_TEA_ID}:like:${keyword}, ${WARD_ID_TEA_ID}:eq:${parentId}`,
})
).flatMap(trackedEntities => {
const surveys = mapTrackedEntityToSurvey(trackedEntities, "PPSPatientRegister");
const instances = trackedEntities.instances;
const surveys = mapTrackedEntityToSurvey(instances, "PPSPatientRegister");

const paginatedSurveys: PaginatedReponse<Survey[]> = {
pager: {
Expand All @@ -163,15 +170,17 @@ export class PaginatedSurveyD2Repository implements PaginatedSurveyRepository {
): FutureData<PaginatedReponse<Survey[]>> {
return apiToFuture(
this.api.tracker.trackedEntities.get({
fields: { attributes: true, enrollments: true, trackedEntity: true, orgUnit: true },
fields: trackedEntityFields,
program: PPS_PATIENT_REGISTER_ID,
orgUnit: orgUnitId,
ouMode: "SELECTED",
pageSize: 10,
totalPages: true,
filter: ` ${SURVEY_PATIENT_CODE_TEA_ID}:like:${keyword}, ${WARD_ID_TEA_ID}:eq:${parentId}`,
})
).flatMap(trackedEntities => {
const surveys = mapTrackedEntityToSurvey(trackedEntities, "PPSPatientRegister");
const instances = trackedEntities.instances;
const surveys = mapTrackedEntityToSurvey(instances, "PPSPatientRegister");

const paginatedSurveys: PaginatedReponse<Survey[]> = {
pager: {
Expand All @@ -193,15 +202,17 @@ export class PaginatedSurveyD2Repository implements PaginatedSurveyRepository {
): FutureData<PaginatedReponse<Survey[]>> {
return apiToFuture(
this.api.tracker.trackedEntities.get({
fields: { attributes: true, enrollments: true, trackedEntity: true, orgUnit: true },
fields: trackedEntityFields,
program: PREVALENCE_CASE_REPORT_FORM_ID,
orgUnit: orgUnitId,
ouMode: "SELECTED",
pageSize: 10,
totalPages: true,
filter: ` ${AMR_SURVEYS_PREVALENCE_TEA_UNIQUE_PATIENT_ID}:like:${keyword}, ${AMR_SURVEYS_PREVALENCE_TEA_SURVEY_ID_CRF}:eq:${parentId}`,
})
).flatMap(trackedEntities => {
const surveys = mapTrackedEntityToSurvey(trackedEntities, "PrevalenceCaseReportForm");
const instances = trackedEntities.instances;
const surveys = mapTrackedEntityToSurvey(instances, "PrevalenceCaseReportForm");

const paginatedSurveys: PaginatedReponse<Survey[]> = {
pager: {
Expand Down
46 changes: 28 additions & 18 deletions src/data/repositories/SurveyFormD2Repository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { D2Api } from "@eyeseetea/d2-api/2.36";
import { D2TrackerEvent } from "@eyeseetea/d2-api/api/trackerEvents";
import { Future } from "../../domain/entities/generic/Future";
import { Id } from "../../domain/entities/Ref";
import { SurveyRepository } from "../../domain/repositories/SurveyRepository";
Expand Down Expand Up @@ -27,19 +26,27 @@ import {
PREVALENCE_SURVEY_NAME_DATAELEMENT_ID,
AMR_SURVEYS_PREVALENCE_DEA_CUSTOM_AST_GUIDE,
AMR_SURVEYS_PREVALENCE_DEA_AST_GUIDELINES,
PPS_COUNTRY_QUESTIONNAIRE_ID,
PREVALENCE_SURVEY_FORM_ID,
} from "../entities/D2Survey";
import { ProgramDataElement, ProgramMetadata } from "../entities/D2Program";
import {
mapProgramToQuestionnaire,
mapQuestionnaireToEvent,
mapQuestionnaireToTrackedEntities,
} from "../utils/surveyFormMappers";
import { mapEventToSurvey, mapTrackedEntityToSurvey } from "../utils/surveyListMappers";
import {
D2TrackerEntity,
mapEventToSurvey,
mapTrackedEntityToSurvey,
trackedEntityFields,
} from "../utils/surveyListMappers";
import { Questionnaire } from "../../domain/entities/Questionnaire/Questionnaire";
import { ASTGUIDELINE_TYPES } from "../../domain/entities/ASTGuidelines";
import { getSurveyChildCount, SurveyChildCountType } from "../utils/surveyChildCountHelper";
import { TrackerPostResponse } from "@eyeseetea/d2-api/api/tracker";
import { Maybe } from "../../utils/ts-utils";
import { D2TrackerEvent } from "@eyeseetea/d2-api/api/trackerEvents";

const OU_CHUNK_SIZE = 500;

Expand Down Expand Up @@ -281,18 +288,19 @@ export class SurveyD2Repository implements SurveyRepository {
(orgUnitId !== "" && programId === PREVALENCE_FACILITY_LEVEL_FORM_ID) ||
programId === PPS_PATIENT_REGISTER_ID
? "DESCENDANTS"
: undefined;
: "SELECTED";

return apiToFuture(
this.api.tracker.trackedEntities.get({
fields: { attributes: true, enrollments: true, trackedEntity: true, orgUnit: true },
fields: trackedEntityFields,
program: programId,
orgUnit: orgUnitId,
ouMode: ouMode,
filter: filter ? `${filter.id}:eq:${filter.value}` : undefined,
})
).flatMap((trackedEntities: TrackedEntitiesGetResponse) => {
const surveys = mapTrackedEntityToSurvey(trackedEntities, surveyFormType);
).flatMap((trackedEntities: TrackedEntitiesGetResponse<typeof trackedEntityFields>) => {
const instances: D2TrackerEntity[] = trackedEntities.instances;
const surveys = mapTrackedEntityToSurvey(instances, surveyFormType);
return Future.success(surveys);
});
}
Expand All @@ -310,20 +318,19 @@ export class SurveyD2Repository implements SurveyRepository {
chunkedOUs.flatMap(ouChunk => {
return apiToFuture(
this.api.tracker.trackedEntities.get({
fields: {
attributes: true,
enrollments: true,
trackedEntity: true,
orgUnit: true,
},
fields: trackedEntityFields,
program: programId,
orgUnit: ouChunk.join(";"),
ouMode: "SELECTED",
filter: filter ? `${filter.id}:eq:${filter.value}` : undefined,
})
).flatMap((trackedEntities: TrackedEntitiesGetResponse) => {
const surveys = mapTrackedEntityToSurvey(trackedEntities, surveyFormType);
return Future.success(surveys);
});
).flatMap(
(trackedEntities: TrackedEntitiesGetResponse<typeof trackedEntityFields>) => {
const instances: D2TrackerEntity[] = trackedEntities.instances;
const surveys = mapTrackedEntityToSurvey(instances, surveyFormType);
return Future.success(surveys);
}
);
})
).flatMap(listOfSurveys => Future.success(_(listOfSurveys).flatten().value()));
}
Expand All @@ -336,9 +343,12 @@ export class SurveyD2Repository implements SurveyRepository {
): FutureData<Survey[]> {
const ouMode =
orgUnitId !== "" &&
(programId === PPS_WARD_REGISTER_ID || programId === PPS_HOSPITAL_FORM_ID)
(programId === PPS_WARD_REGISTER_ID ||
programId === PPS_HOSPITAL_FORM_ID ||
programId === PPS_COUNTRY_QUESTIONNAIRE_ID ||
programId === PREVALENCE_SURVEY_FORM_ID)
? "DESCENDANTS"
: undefined;
: "SELECTED";

return apiToFuture(
this.api.tracker.events.get({
Expand Down
17 changes: 10 additions & 7 deletions src/data/utils/surveyChildCountHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ const asyncGetEventSurveyChildCount = async (
programId: Id,
api: D2Api,
ouId: string,
ouMode: "SELECTED" | "CHILDREN" | "DESCENDANTS" | undefined,
ouMode: "SELECTED" | "CHILDREN" | "DESCENDANTS" | "ALL" | "ACCESSIBLE" | "CAPTURE",
filterStr: string
) => {
let response: TrackerEventsResponse;
let response: TrackerEventsResponse<{ event: true }>;
let count = 0;
const pageSize = 250;

Expand Down Expand Up @@ -142,8 +142,11 @@ const getEventSurveyCount = (
secondaryParentId: Id | undefined,
api: D2Api
): FutureData<number> => {
const ouId = programId === PPS_COUNTRY_QUESTIONNAIRE_ID ? "" : orgUnitId;
const ouMode = programId === PPS_HOSPITAL_FORM_ID ? "DESCENDANTS" : undefined;
// const ouId = programId === PPS_COUNTRY_QUESTIONNAIRE_ID ? "" : orgUnitId;
const ouMode =
programId === PPS_HOSPITAL_FORM_ID || programId === PPS_COUNTRY_QUESTIONNAIRE_ID
? "DESCENDANTS"
: "SELECTED";
const filterParentDEId = getParentDataElementForProgram(programId);

const filterStr =
Expand All @@ -155,7 +158,7 @@ const getEventSurveyCount = (
1,
programId,
api,
ouId,
orgUnitId,
ouMode,
filterStr
);
Expand All @@ -174,7 +177,7 @@ const getTrackerSurveyCount = (
const ouMode =
orgUnitId !== "" && programId === PREVALENCE_FACILITY_LEVEL_FORM_ID
? "DESCENDANTS"
: undefined;
: "SELECTED";

return apiToFuture(
api.tracker.trackedEntities.get({
Expand All @@ -186,7 +189,7 @@ const getTrackerSurveyCount = (
ouMode: ouMode,
filter: `${filterParentDEId}:eq:${parentSurveyId}`,
})
).flatMap((trackedEntities: TrackedEntitiesGetResponse) => {
).flatMap(trackedEntities => {
if (trackedEntities.total) return Future.success(trackedEntities.total);
else return Future.success(trackedEntities.instances.length);
});
Expand Down
55 changes: 38 additions & 17 deletions src/data/utils/surveyListMappers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TrackedEntitiesGetResponse } from "@eyeseetea/d2-api/api/trackerTrackedEntities";
import { D2TrackerTrackedEntitySchema } from "@eyeseetea/d2-api/api/trackerTrackedEntities";
import { SURVEY_FORM_TYPES, SURVEY_STATUSES, Survey } from "../../domain/entities/Survey";
import {
AMR_SURVEYS_PREVALENCE_TEA_SURVEY_ID_CRF,
Expand Down Expand Up @@ -26,17 +26,30 @@ import {
WARD_ID_TEA_ID,
SURVEY_PATIENT_CODE_TEA_ID,
} from "../entities/D2Survey";
import { D2TrackerEvent } from "@eyeseetea/d2-api/api/trackerEvents";
import { getSurveyNameBySurveyFormType } from "./surveyProgramHelper";
import { Id } from "../../domain/entities/Ref";
import { SelectedPick } from "@eyeseetea/d2-api/api";
import { D2TrackerEvent } from "@eyeseetea/d2-api/api/trackerEvents";

export const trackedEntityFields = {
attributes: true,
enrollments: true,
trackedEntity: true,
orgUnit: true,
} as const;

export type D2TrackerEntity = SelectedPick<
D2TrackerTrackedEntitySchema,
typeof trackedEntityFields
>;

export const mapTrackedEntityToSurvey = (
trackedEntities: TrackedEntitiesGetResponse,
trackedEntities: D2TrackerEntity[],
surveyFormType: SURVEY_FORM_TYPES
): Survey[] => {
return trackedEntities.instances.map(trackedEntity => {
return trackedEntities.map(trackedEntityInstance => {
const parentPrevalenceSurveyId =
trackedEntity.attributes?.find(
trackedEntityInstance.attributes?.find(
attribute =>
attribute.attribute === SURVEY_ID_FACILITY_LEVEL_DATAELEMENT_ID ||
attribute.attribute === AMR_SURVEYS_PREVALENCE_TEA_SURVEY_ID_SSTF ||
Expand All @@ -50,11 +63,12 @@ export const mapTrackedEntityToSurvey = (
)?.value ?? "";

const parentPPSSurveyId =
trackedEntity.attributes?.find(attr => attr.attribute === SURVEY_ID_PATIENT_TEA_ID)
?.value ?? "";
trackedEntityInstance.attributes?.find(
attr => attr.attribute === SURVEY_ID_PATIENT_TEA_ID
)?.value ?? "";

const patientId =
trackedEntity.attributes?.find(
trackedEntityInstance.attributes?.find(
attribute =>
attribute.attribute === AMR_SURVEYS_PREVALENCE_TEA_UNIQUE_PATIENT_ID ||
attribute.attribute === AMR_SURVEYS_PREVALENCE_TEA_PATIENT_ID ||
Expand All @@ -67,22 +81,23 @@ export const mapTrackedEntityToSurvey = (
)?.value ?? "";

const patientCode =
trackedEntity.attributes?.find(
trackedEntityInstance.attributes?.find(
attribute => attribute.attribute === SURVEY_PATIENT_CODE_TEA_ID
)?.value ?? "";

const facilityCode =
trackedEntity.attributes?.find(
trackedEntityInstance.attributes?.find(
attribute => attribute.attribute === AMR_SURVEYS_PREVALENCE_TEA_HOSPITAL_ID
)?.value ?? "";

const parentWardId =
trackedEntity.attributes?.find(attribute => attribute.attribute === WARD_ID_TEA_ID)
?.value ?? "";
trackedEntityInstance.attributes?.find(
attribute => attribute.attribute === WARD_ID_TEA_ID
)?.value ?? "";

const survey: Survey = {
id: trackedEntity.trackedEntity ?? "",
name: trackedEntity.trackedEntity ?? "",
id: trackedEntityInstance.trackedEntity ?? "",
name: trackedEntityInstance.trackedEntity ?? "",
rootSurvey: {
id:
surveyFormType === "PPSPatientRegister"
Expand All @@ -91,11 +106,17 @@ export const mapTrackedEntityToSurvey = (
name: "",
surveyType: "",
},
startDate: trackedEntity.createdAt ? new Date(trackedEntity.createdAt) : undefined,
//TO DO : Fix for upgrade
// startDate: trackedEntityInstance.createdAt
// ? new Date(trackedEntityInstance.createdAt)
// : undefined,
startDate: undefined,
status: "ACTIVE",
assignedOrgUnit: {
id: trackedEntity.orgUnit ?? "",
name: trackedEntity.enrollments?.[0]?.orgUnitName ?? "",
id: trackedEntityInstance.orgUnit ?? "",
//TO DO : Fix for upgrade
// name: trackedEntityInstance.enrollments?.[0]?.orgUnitName ?? "",
name: "",
},
surveyType: "",
parentWardRegisterId: parentWardId,
Expand Down
Loading

0 comments on commit 1a595ba

Please sign in to comment.