Skip to content

Commit 6ad8bbd

Browse files
committed
feat: use user ou root id for survey ou form selector
1 parent 8a3fe22 commit 6ad8bbd

File tree

3 files changed

+65
-21
lines changed

3 files changed

+65
-21
lines changed

src/data/repositories/UserD2Repository.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ const userFields = {
282282
organisationUnits: {
283283
id: true,
284284
name: true,
285+
level: true,
285286
},
286287
dataViewOrganisationUnits: {
287288
id: true,

src/domain/entities/User.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ export interface UserSettings {
2525
keyMessageEmailNotification: boolean;
2626
keyMessageSmsNotification: boolean;
2727
}
28+
29+
export type UserOrgUnit = NamedRef & {
30+
level: number;
31+
};
2832
export interface UserAttrs {
2933
id: string;
3034
name: string;
@@ -44,7 +48,7 @@ export interface UserAttrs {
4448
interests: string;
4549
languages: string;
4650
settings: UserSettings;
47-
organisationUnits: NamedRef[];
51+
organisationUnits: UserOrgUnit[];
4852
dataViewOrganisationUnits: NamedRef[];
4953
}
5054

src/webapp/components/survey/SurveyFormOUSelector.tsx

Lines changed: 59 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import { OrgUnitsSelector } from "@eyeseetea/d2-ui-components";
2-
import { useEffect } from "react";
2+
import { useEffect, useMemo } from "react";
33
import { COUNTRY_OU_LEVEL, HOSPITAL_OU_LEVELS } from "../../../data/repositories/UserD2Repository";
44
import { Id } from "../../../domain/entities/Ref";
55
import { SURVEYS_WITH_ORG_UNIT_SELECTOR, SURVEY_FORM_TYPES } from "../../../domain/entities/Survey";
6-
import { OrgUnitAccess } from "../../../domain/entities/User";
6+
import { OrgUnitAccess, UserOrgUnit } from "../../../domain/entities/User";
77
import { GLOBAL_OU_ID } from "../../../domain/usecases/SaveFormDataUseCase";
88
import { getParentOUIdFromPath } from "../../../domain/utils/PPSProgramsHelper";
99
import { useAppContext } from "../../contexts/app-context";
1010
import { useCurrentSurveys } from "../../contexts/current-surveys-context";
1111
import { useSurveyFormOUSelector } from "./hook/useSurveyFormOUSelector";
1212
import { useOfflineSnackbar } from "../../hooks/useOfflineSnackbar";
13+
import _c from "../../../domain/entities/generic/Collection";
1314

1415
export interface SurveyFormOUSelectorProps {
1516
formType: SURVEY_FORM_TYPES;
@@ -24,7 +25,7 @@ export const SurveyFormOUSelector: React.FC<SurveyFormOUSelectorProps> = ({
2425
setCurrentOrgUnit,
2526
currentSurveyId,
2627
}) => {
27-
const { api } = useAppContext();
28+
const { api, currentUser } = useAppContext();
2829
const { currentPPSSurveyForm, currentCountryQuestionnaire, currentPrevalenceSurveyForm } =
2930
useCurrentSurveys();
3031
const { onOrgUnitChange, ouSelectorErrMsg, shouldRefresh } = useSurveyFormOUSelector(
@@ -40,6 +41,47 @@ export const SurveyFormOUSelector: React.FC<SurveyFormOUSelectorProps> = ({
4041
}
4142
}, [ouSelectorErrMsg, snackbar, shouldRefresh, offlineError]);
4243

44+
const rootIds = useMemo(() => {
45+
if (formType === "PPSHospitalForm") {
46+
// For HOSP PPS surveys, show all hospitals across all OUs
47+
if (currentPPSSurveyForm?.surveyType === "HOSP") {
48+
return [GLOBAL_OU_ID];
49+
}
50+
// For non-admin user, currentCountryQuestionnaire won't be set. Get parent id from path
51+
return currentCountryQuestionnaire?.orgUnitId
52+
? [currentCountryQuestionnaire.orgUnitId]
53+
: [getParentOUIdFromPath(currentOrgUnit?.orgUnitPath)];
54+
}
55+
56+
if (formType === "PrevalenceFacilityLevelForm") {
57+
return [currentPrevalenceSurveyForm?.orgUnitId];
58+
}
59+
60+
if (formType === "WardSummaryStatisticsForm") {
61+
return getRootIds(currentUser.organisationUnits);
62+
}
63+
64+
return [GLOBAL_OU_ID];
65+
}, [
66+
formType,
67+
currentPPSSurveyForm?.surveyType,
68+
currentCountryQuestionnaire?.orgUnitId,
69+
currentOrgUnit?.orgUnitPath,
70+
currentPrevalenceSurveyForm?.orgUnitId,
71+
currentUser.organisationUnits,
72+
]);
73+
74+
const selectableLevels = useMemo(() => {
75+
switch (formType) {
76+
case "PPSCountryQuestionnaire":
77+
case "PrevalenceSurveyForm":
78+
case "WardSummaryStatisticsForm":
79+
return HOSPITAL_OU_LEVELS;
80+
default:
81+
return [COUNTRY_OU_LEVEL];
82+
}
83+
}, [formType]);
84+
4385
return (
4486
<>
4587
{SURVEYS_WITH_ORG_UNIT_SELECTOR.includes(formType) && (
@@ -54,33 +96,30 @@ export const SurveyFormOUSelector: React.FC<SurveyFormOUSelectorProps> = ({
5496
singleSelection={true}
5597
typeInput={"radio"}
5698
hideMemberCount={false}
57-
selectableLevels={
58-
formType === "PPSCountryQuestionnaire" ||
59-
formType === "PrevalenceSurveyForm"
60-
? [COUNTRY_OU_LEVEL]
61-
: HOSPITAL_OU_LEVELS
62-
}
99+
selectableLevels={selectableLevels}
63100
controls={{
64101
filterByLevel: false,
65102
filterByGroup: false,
66103
filterByProgram: false,
67104
selectAll: false,
68105
}}
69-
rootIds={
70-
formType === "PPSHospitalForm"
71-
? currentPPSSurveyForm?.surveyType === "HOSP" //For HOSP PPS surveys, show all hospitals across all OUs
72-
? [GLOBAL_OU_ID]
73-
: currentCountryQuestionnaire?.orgUnitId
74-
? [currentCountryQuestionnaire?.orgUnitId] //For non-admin user, currentCountryQuestionnaire wont be set. Get parent id from path
75-
: [getParentOUIdFromPath(currentOrgUnit?.orgUnitPath)]
76-
: formType === "PrevalenceFacilityLevelForm"
77-
? [currentPrevalenceSurveyForm?.orgUnitId]
78-
: [GLOBAL_OU_ID]
79-
}
106+
rootIds={rootIds}
80107
showShortName={true}
81108
showNameSetting={true}
82109
/>
83110
)}
84111
</>
85112
);
86113
};
114+
115+
function getRoots(orgUnits: UserOrgUnit[]): UserOrgUnit[] {
116+
const minLevel = Math.min(...orgUnits.map(ou => ou.level));
117+
return _c(orgUnits)
118+
.filter(ou => ou.level === minLevel)
119+
.sortBy(ou => ou.name)
120+
.value();
121+
}
122+
123+
export function getRootIds(orgUnits: UserOrgUnit[]): Id[] {
124+
return getRoots(orgUnits).map(ou => ou.id);
125+
}

0 commit comments

Comments
 (0)