Skip to content

Commit

Permalink
Merge pull request #95 from EyeSeeTea/fix/breadcrumb-country-code
Browse files Browse the repository at this point in the history
Use country code in PPS breadcrumbs
  • Loading branch information
MiquelAdell authored Feb 27, 2025
2 parents 5241144 + 640eb8f commit 65a656e
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 37 deletions.
23 changes: 13 additions & 10 deletions src/data/repositories/SurveyFormD2Repository.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { D2Api } from "@eyeseetea/d2-api/2.36";
import { Future } from "../../domain/entities/generic/Future";
import { Id, NamedRef } from "../../domain/entities/Ref";
import { Id } from "../../domain/entities/Ref";
import { SurveyRepository } from "../../domain/repositories/SurveyRepository";
import { apiToFuture, FutureData } from "../api-futures";
import _ from "../../domain/entities/generic/Collection";
Expand Down Expand Up @@ -47,6 +47,7 @@ import { getSurveyChildCount } from "../utils/surveyChildCountHelper";
import { TrackerPostRequest, TrackerPostResponse } from "@eyeseetea/d2-api/api/tracker";
import { Maybe } from "../../utils/ts-utils";
import { D2TrackerEvent, D2TrackerEventToPost } from "@eyeseetea/d2-api/api/trackerEvents";
import { OrgUnitBasic } from "../../domain/entities/OrgUnit";

const OU_CHUNK_SIZE = 500;

Expand Down Expand Up @@ -300,7 +301,7 @@ export class SurveyD2Repository implements SurveyRepository {
})
).flatMap((trackedEntities: TrackedEntitiesGetResponse<typeof trackedEntityFields>) => {
const instances: D2TrackerEntitySelectedPick[] = trackedEntities.instances;
return this.getOrgUnitNames(instances.map(instance => instance.orgUnit)).flatMap(
return this.getOrgUnitsBasicInfo(instances.map(instance => instance.orgUnit)).flatMap(
orgUnits => {
const surveys = mapTrackedEntityToSurvey(instances, surveyFormType, orgUnits);
return Future.success(surveys);
Expand Down Expand Up @@ -331,7 +332,7 @@ export class SurveyD2Repository implements SurveyRepository {
).flatMap(
(trackedEntities: TrackedEntitiesGetResponse<typeof trackedEntityFields>) => {
const instances: D2TrackerEntitySelectedPick[] = trackedEntities.instances;
return this.getOrgUnitNames(
return this.getOrgUnitsBasicInfo(
instances.map(instance => instance.orgUnit)
).flatMap(orgUnits => {
const surveys = mapTrackedEntityToSurvey(
Expand All @@ -347,16 +348,16 @@ export class SurveyD2Repository implements SurveyRepository {
).flatMap(listOfSurveys => Future.success(_(listOfSurveys).flatten().value()));
}

private getOrgUnitNames(orgUnitIds: Id[]): FutureData<NamedRef[]> {
private getOrgUnitsBasicInfo(orgUnitIds: Id[]): FutureData<OrgUnitBasic[]> {
return apiToFuture(
this.api.models.organisationUnits.get({
fields: { id: true, name: true },
fields: { id: true, name: true, code: true },
filter: { id: { in: orgUnitIds } },
paging: false,
})
).flatMap(orgUnitsResponse => {
const orgUnits = orgUnitsResponse.objects.map(ou => {
return { id: ou.id, name: ou.name };
return { id: ou.id, name: ou.name, code: ou.code };
});
return Future.success(orgUnits);
});
Expand Down Expand Up @@ -388,10 +389,12 @@ export class SurveyD2Repository implements SurveyRepository {
).flatMap(response => {
const events = response.instances;

return this.getOrgUnitNames(events.map(event => event.orgUnit)).flatMap(orgUnits => {
const surveys = mapEventToSurvey(events, surveyFormType, programId, orgUnits);
return Future.success(surveys);
});
return this.getOrgUnitsBasicInfo(events.map(event => event.orgUnit)).flatMap(
orgUnits => {
const surveys = mapEventToSurvey(events, surveyFormType, programId, orgUnits);
return Future.success(surveys);
}
);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export class SurveyTestRepository implements SurveyRepository {
id: "1",
startDate: new Date(),
status: "ACTIVE",
assignedOrgUnit: { id: orgUnitId, name: "OU1" },
assignedOrgUnit: { id: orgUnitId, name: "OrgUnit1", code: "OU1" },
surveyType: "SUPRANATIONAL",
rootSurvey: { id: "1", name: "TestSurvey1", surveyType: "" },
surveyFormType: "PPSSurveyForm",
Expand All @@ -106,7 +106,7 @@ export class SurveyTestRepository implements SurveyRepository {
id: "2",
startDate: new Date(),
status: "COMPLETED",
assignedOrgUnit: { id: "OU1234", name: "OU2" },
assignedOrgUnit: { id: "OU1234", name: "OrgUnit2", code: "OU2" },
surveyType: "NATIONAL",
rootSurvey: { id: "2", name: "TestSurvey1", surveyType: "" },
surveyFormType: "PPSSurveyForm",
Expand Down
18 changes: 13 additions & 5 deletions src/data/utils/surveyListMappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import {
patientIdList,
} from "../entities/D2Survey";
import { getSurveyNameBySurveyFormType } from "./surveyProgramHelper";
import { Id, NamedRef } from "../../domain/entities/Ref";
import { Id } from "../../domain/entities/Ref";
import { SelectedPick } from "@eyeseetea/d2-api/api";
import { D2TrackerEvent } from "@eyeseetea/d2-api/api/trackerEvents";
import { OrgUnitBasic } from "../../domain/entities/OrgUnit";

export const trackedEntityFields = {
attributes: true,
Expand All @@ -33,7 +34,7 @@ export type D2TrackerEntitySelectedPick = SelectedPick<
export const mapTrackedEntityToSurvey = (
trackedEntities: D2TrackerEntitySelectedPick[],
surveyFormType: SURVEY_FORM_TYPES,
orgUnits?: NamedRef[]
orgUnits?: OrgUnitBasic[]
): Survey[] => {
return trackedEntities.map(trackedEntityInstance => {
const parentPrevalenceSurveyId =
Expand Down Expand Up @@ -68,6 +69,8 @@ export const mapTrackedEntityToSurvey = (

const createdAt = trackedEntityInstance.enrollments[0]?.createdAt;

const orgUnit = orgUnits?.find(ou => ou.id === trackedEntityInstance.orgUnit);

const survey: Survey = {
id: trackedEntityInstance.trackedEntity ?? "",
name: trackedEntityInstance.trackedEntity ?? "",
Expand All @@ -83,7 +86,8 @@ export const mapTrackedEntityToSurvey = (
status: "ACTIVE",
assignedOrgUnit: {
id: trackedEntityInstance.orgUnit ?? "",
name: orgUnits?.find(ou => ou.id === trackedEntityInstance.orgUnit)?.name ?? "",
name: orgUnit?.name ?? "",
code: orgUnit?.code ?? "",
},
surveyType: "",
parentWardRegisterId: parentWardId,
Expand All @@ -100,7 +104,7 @@ export const mapEventToSurvey = (
events: D2TrackerEvent[],
surveyFormType: SURVEY_FORM_TYPES,
programId: Id,
orgUnits?: NamedRef[]
orgUnits?: OrgUnitBasic[]
): Survey[] => {
return events.map((event: D2TrackerEvent) => {
const surveyProperties = new Map(
Expand Down Expand Up @@ -133,6 +137,8 @@ export const mapEventToSurvey = (
: "ACTIVE"
: "COMPLETED";

const orgUnit = orgUnits?.find(ou => ou.id === event.orgUnit);

const survey: Survey = {
id: event.event,
name: getSurveyNameBySurveyFormType(surveyFormType, {
Expand Down Expand Up @@ -162,8 +168,10 @@ export const mapEventToSurvey = (
? ("COMPLETED" as SURVEY_STATUSES)
: ("ACTIVE" as SURVEY_STATUSES),
assignedOrgUnit: {
...orgUnit,
id: event.orgUnit,
name: orgUnits?.find(ou => ou.id === event.orgUnit)?.name ?? "",
name: orgUnit?.name ?? "",
code: orgUnit?.code ?? "",
},
surveyType: surveyType,
parentWardRegisterId: parentWardRegisterId,
Expand Down
6 changes: 6 additions & 0 deletions src/domain/entities/OrgUnit.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { NamedRef } from "./Ref";

export interface OrgUnit {
id: string;
shortName: string;
path: string;
}

export interface OrgUnitBasic extends NamedRef {
code: string;
}
10 changes: 8 additions & 2 deletions src/domain/entities/Survey.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ASTGUIDELINE_TYPES } from "./ASTGuidelines";
import { OrgUnitBasic } from "./OrgUnit";
import { ProgramCountMap, ProgramOptionCountMap } from "./Program";
import { NamedRef, Id } from "./Ref";
import { NamedRef, Id, Ref } from "./Ref";

export type SURVEY_FORM_TYPES =
| "PPSSurveyForm"
Expand Down Expand Up @@ -48,6 +49,11 @@ export interface OrgUnitNamedRef extends NamedRef {
orgUnitId: Id;
}

export interface OrgUnitWithCodeRef extends Ref {
orgUnitId: Id;
orgUnitCode: string;
}

export interface PrevalenceSurveyForm extends OrgUnitNamedRef {
astGuidelines: ASTGUIDELINE_TYPES | undefined;
}
Expand All @@ -73,7 +79,7 @@ export interface Survey extends SurveyBase {
rootSurvey: SurveyBase; // all surveys are associated with a given parent Survey Form instance.
startDate?: Date;
status: SURVEY_STATUSES;
assignedOrgUnit: NamedRef;
assignedOrgUnit: OrgUnitBasic;
surveyFormType: SURVEY_FORM_TYPES;
parentWardRegisterId?: Id;
uniquePatient?: { id: string; code: string };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export const PPSListBreadCrumbs: React.FC<PPSListBreadCrumbsProps> = ({ formType
to={`/survey/PPSCountryQuestionnaire/${currentCountryQuestionnaire?.id}`}
exact={true}
>
<span>{currentCountryQuestionnaire?.name}</span>
<span>{currentCountryQuestionnaire?.orgUnitCode}</span>
</Button>
<ChevronRightIcon />
</>
Expand Down
18 changes: 9 additions & 9 deletions src/webapp/components/survey-list/hook/useSurveyListActions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useState } from "react";
import { useHistory } from "react-router-dom";
import { Id } from "../../../../domain/entities/Ref";
import { Survey, SurveyBase, SURVEY_FORM_TYPES } from "../../../../domain/entities/Survey";
import { getChildSurveyType, getSurveyOptions } from "../../../../domain/utils/PPSProgramsHelper";
import _ from "../../../../domain/entities/generic/Collection";
Expand All @@ -13,6 +12,7 @@ import useReadOnlyAccess from "../../survey/hook/useReadOnlyAccess";
import useCaptureAccess from "../../survey/hook/useCaptureAccess";
import { GLOBAL_OU_ID } from "../../../../domain/usecases/SaveFormDataUseCase";
import { useCurrentASTGuidelinesContext } from "../../../contexts/current-ast-guidelines-context";
import { OrgUnitBasic } from "../../../../domain/entities/OrgUnit";

export type SortDirection = "asc" | "desc";
export function useSurveyListActions(surveyFormType: SURVEY_FORM_TYPES) {
Expand Down Expand Up @@ -49,7 +49,7 @@ export function useSurveyListActions(surveyFormType: SURVEY_FORM_TYPES) {
surveyType: survey.surveyType,
astGuideline: survey.astGuideline,
},
survey.assignedOrgUnit.id,
survey.assignedOrgUnit,
survey.rootSurvey
);
history.push({
Expand All @@ -65,7 +65,7 @@ export function useSurveyListActions(surveyFormType: SURVEY_FORM_TYPES) {
surveyType: survey.surveyType,
astGuideline: survey.astGuideline,
},
survey.assignedOrgUnit.id,
survey.assignedOrgUnit,
survey.rootSurvey
);
const childSurveyType = getChildSurveyType(surveyFormType, survey.surveyType, option);
Expand All @@ -86,7 +86,7 @@ export function useSurveyListActions(surveyFormType: SURVEY_FORM_TYPES) {
surveyType: survey.surveyType,
astGuideline: survey.astGuideline,
},
survey.assignedOrgUnit.id,
survey.assignedOrgUnit,
survey.rootSurvey
);
const childSurveyType = getChildSurveyType(surveyFormType, survey.surveyType, option);
Expand Down Expand Up @@ -152,23 +152,23 @@ export function useSurveyListActions(surveyFormType: SURVEY_FORM_TYPES) {

const updateSelectedSurveyDetails = (
survey: SurveyBase,
orgUnitId: Id,
orgUnit: OrgUnitBasic,
rootSurvey: SurveyBase
) => {
if (surveyFormType === "PPSSurveyForm") changeCurrentPPSSurveyForm(survey);
else if (surveyFormType === "PPSCountryQuestionnaire")
changeCurrentCountryQuestionnaire(survey.id, survey.name, orgUnitId);
changeCurrentCountryQuestionnaire(survey.id, orgUnit.code, orgUnit.id);
else if (surveyFormType === "PPSHospitalForm") {
if (!isAdmin) {
changeCurrentPPSSurveyForm(rootSurvey);
}
changeCurrentHospitalForm(survey.id, survey.name, orgUnitId);
changeCurrentHospitalForm(survey.id, survey.name, orgUnit.id);
} else if (surveyFormType === "PPSWardRegister") changeCurrentWardRegister(survey);
else if (surveyFormType === "PrevalenceSurveyForm") {
changeCurrentPrevalenceSurveyForm(
survey.id,
survey.name,
orgUnitId,
orgUnit.id,
survey.astGuideline
);
//when current astGuideline changes, fetch the corresponding ast guidelines from datstore
Expand Down Expand Up @@ -210,7 +210,7 @@ export function useSurveyListActions(surveyFormType: SURVEY_FORM_TYPES) {
}
);
}
changeCurrentFacilityLevelForm(survey.id, survey.name, orgUnitId);
changeCurrentFacilityLevelForm(survey.id, survey.name, orgUnit.id);
} else if (surveyFormType === "PrevalenceCaseReportForm")
changeCurrentCaseReportForm({ id: survey.id, name: survey.name });
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export const PPSSurveyFormBreadCrumb: React.FC<PPSSurveyFormBreadCrumbProps> = (
to={`/survey/PPSCountryQuestionnaire/${currentCountryQuestionnaire.id}`}
exact={true}
>
<span>{currentCountryQuestionnaire.name}</span>
<span>{currentCountryQuestionnaire.orgUnitCode}</span>
</Button>
) : (
<Button>
Expand Down
13 changes: 9 additions & 4 deletions src/webapp/contexts/CurrentSurveysContextProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import { PropsWithChildren, useState } from "react";
import { Id, NamedRef } from "../../domain/entities/Ref";
import { OrgUnitNamedRef, PrevalenceSurveyForm, SurveyBase } from "../../domain/entities/Survey";
import {
OrgUnitNamedRef,
PrevalenceSurveyForm,
SurveyBase,
OrgUnitWithCodeRef,
} from "../../domain/entities/Survey";
import { CurrentSurveysContext } from "./current-surveys-context";
import { ASTGUIDELINE_TYPES } from "../../domain/entities/ASTGuidelines";

export const CurrentSurveysContextProvider: React.FC<PropsWithChildren> = ({ children }) => {
//PPS Module states
const [currentPPSSurveyForm, setCurrentPPSSurveyForm] = useState<SurveyBase>();
const [currentCountryQuestionnaire, setCurrentCountryQuestionnaire] =
useState<OrgUnitNamedRef>();
useState<OrgUnitWithCodeRef>();
const [currentHospitalForm, setCurrentHospitalForm] = useState<OrgUnitNamedRef>();
const [currentWardRegister, setCurrentWardRegister] = useState<NamedRef>();

Expand All @@ -29,8 +34,8 @@ export const CurrentSurveysContextProvider: React.FC<PropsWithChildren> = ({ chi
resetCurrentWardRegister();
};

const changeCurrentCountryQuestionnaire = (id: Id, name: string, orgUnitId: Id) => {
setCurrentCountryQuestionnaire({ id: id, name: name, orgUnitId: orgUnitId });
const changeCurrentCountryQuestionnaire = (id: Id, orgUnitCode: string, orgUnitId: Id) => {
setCurrentCountryQuestionnaire({ id, orgUnitCode, orgUnitId });
};

const resetCurrentCountryQuestionnaire = () => {
Expand Down
11 changes: 8 additions & 3 deletions src/webapp/contexts/current-surveys-context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { useContext, createContext } from "react";
import { Id, NamedRef } from "../../domain/entities/Ref";
import { OrgUnitNamedRef, PrevalenceSurveyForm, SurveyBase } from "../../domain/entities/Survey";
import {
OrgUnitNamedRef,
PrevalenceSurveyForm,
SurveyBase,
OrgUnitWithCodeRef,
} from "../../domain/entities/Survey";
import { ASTGUIDELINE_TYPES } from "../../domain/entities/ASTGuidelines";

export interface CurrentSurveysContextProps {
Expand All @@ -9,8 +14,8 @@ export interface CurrentSurveysContextProps {
changeCurrentPPSSurveyForm: (survey: SurveyBase | undefined) => void;
resetCurrentPPSSurveyForm: () => void;

currentCountryQuestionnaire: OrgUnitNamedRef | undefined;
changeCurrentCountryQuestionnaire: (id: Id, name: string, orgUnitId: Id) => void;
currentCountryQuestionnaire: OrgUnitWithCodeRef | undefined;
changeCurrentCountryQuestionnaire: (id: Id, orgUnitCode: string, orgUnitId: Id) => void;
resetCurrentCountryQuestionnaire: () => void;

currentHospitalForm: OrgUnitNamedRef | undefined;
Expand Down

0 comments on commit 65a656e

Please # to comment.