From 6a6a5218432c7302501d0185473d502a90162342 Mon Sep 17 00:00:00 2001 From: Ashraf Mohammed Date: Sat, 24 Feb 2024 20:57:25 +0530 Subject: [PATCH 1/6] add home facility check --- src/Components/Common/FacilitySelect.tsx | 27 ++++++++++++++++++- .../FacilitiesSelectDialogue.tsx | 1 + src/Components/Patient/PatientRegister.tsx | 10 +++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/Components/Common/FacilitySelect.tsx b/src/Components/Common/FacilitySelect.tsx index b207189d27d..c07d7cf4e0b 100644 --- a/src/Components/Common/FacilitySelect.tsx +++ b/src/Components/Common/FacilitySelect.tsx @@ -3,6 +3,7 @@ import AutoCompleteAsync from "../Form/AutoCompleteAsync"; import { FacilityModel } from "../Facility/models"; import request from "../../Utils/request/request"; import routes from "../../Redux/api"; +import useAuthUser from "../../Common/hooks/useAuthUser"; interface FacilitySelectProps { name: string; @@ -16,6 +17,7 @@ interface FacilitySelectProps { showAll?: boolean; showNOptions?: number; freeText?: boolean; + homeFacility?: boolean; selected?: FacilityModel | FacilityModel[] | null; setSelected: (selected: FacilityModel | FacilityModel[] | null) => void; } @@ -34,8 +36,11 @@ export const FacilitySelect = (props: FacilitySelectProps) => { facilityType, district, freeText = false, + homeFacility = false, errors = "", } = props; + const authUser = useAuthUser(); + const showAllFacilityUsers = ["DistrictAdmin", "StateAdmin"]; const facilitySearch = useCallback( async (text: string) => { @@ -44,6 +49,7 @@ export const FacilitySelect = (props: FacilitySelectProps) => { offset: 0, search_text: text, all: searchAll, + name: "", facility_type: facilityType, exclude_user: exclude_user, district, @@ -54,6 +60,15 @@ export const FacilitySelect = (props: FacilitySelectProps) => { { query } ); + if ( + homeFacility && + !showAllFacilityUsers.includes(authUser.user_type) && + authUser.home_facility_object?.name + ) { + query["name"] = authUser.home_facility_object?.name; + query["limit"] = 1; + } + if (freeText) data?.results?.push({ id: -1, @@ -61,7 +76,17 @@ export const FacilitySelect = (props: FacilitySelectProps) => { }); return data?.results; }, - [searchAll, showAll, facilityType, district, exclude_user, freeText] + [ + searchAll, + showAll, + facilityType, + district, + exclude_user, + freeText, + authUser.home_facility_object?.name, + authUser.user_type, + homeFacility, + ] ); return ( diff --git a/src/Components/ExternalResult/FacilitiesSelectDialogue.tsx b/src/Components/ExternalResult/FacilitiesSelectDialogue.tsx index 7239e0b912d..b7153c62f32 100644 --- a/src/Components/ExternalResult/FacilitiesSelectDialogue.tsx +++ b/src/Components/ExternalResult/FacilitiesSelectDialogue.tsx @@ -29,6 +29,7 @@ const FacilitiesSelectDialog = (props: Props) => { errors="" showAll={false} multiple={false} + homeFacility={true} />
diff --git a/src/Components/Patient/PatientRegister.tsx b/src/Components/Patient/PatientRegister.tsx index c6b260efe87..73ee261999c 100644 --- a/src/Components/Patient/PatientRegister.tsx +++ b/src/Components/Patient/PatientRegister.tsx @@ -233,6 +233,16 @@ export const PatientRegister = (props: PatientRegisterProps) => { } }, [careExtId, formField]); + useEffect(() => { + const showAllFacilityUsers = ["DistrictAdmin", "StateAdmin"]; + if ( + !showAllFacilityUsers.includes(authUser.user_type) && + authUser.home_facility_object?.name + ) { + navigate("/facility"); + } + }, []); + const headerText = !id ? "Add Details of Patient" : "Update Patient Details"; const buttonText = !id ? "Add Patient" : "Save Details"; From 537e0b89a77b408acf3255162c7c3df72613dafe Mon Sep 17 00:00:00 2001 From: Ashraf Mohammed Date: Sat, 24 Feb 2024 21:44:40 +0530 Subject: [PATCH 2/6] fix bug --- src/Components/Common/FacilitySelect.tsx | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/src/Components/Common/FacilitySelect.tsx b/src/Components/Common/FacilitySelect.tsx index c07d7cf4e0b..6385e50fcfe 100644 --- a/src/Components/Common/FacilitySelect.tsx +++ b/src/Components/Common/FacilitySelect.tsx @@ -55,11 +55,6 @@ export const FacilitySelect = (props: FacilitySelectProps) => { district, }; - const { data } = await request( - showAll ? routes.getAllFacilities : routes.getPermittedFacilities, - { query } - ); - if ( homeFacility && !showAllFacilityUsers.includes(authUser.user_type) && @@ -69,6 +64,11 @@ export const FacilitySelect = (props: FacilitySelectProps) => { query["limit"] = 1; } + const { data } = await request( + showAll ? routes.getAllFacilities : routes.getPermittedFacilities, + { query } + ); + if (freeText) data?.results?.push({ id: -1, @@ -76,17 +76,7 @@ export const FacilitySelect = (props: FacilitySelectProps) => { }); return data?.results; }, - [ - searchAll, - showAll, - facilityType, - district, - exclude_user, - freeText, - authUser.home_facility_object?.name, - authUser.user_type, - homeFacility, - ] + [searchAll, showAll, facilityType, district, exclude_user, freeText] ); return ( From 2af047f61379bf87aaebdda63e6df149c9847c3d Mon Sep 17 00:00:00 2001 From: Ashraf Mohammed Date: Sun, 3 Mar 2024 12:13:45 +0530 Subject: [PATCH 3/6] change logic when clicked button --- src/Components/Common/FacilitySelect.tsx | 15 --------------- .../ExternalResult/FacilitiesSelectDialogue.tsx | 1 - src/Components/Patient/ManagePatients.tsx | 10 +++++++++- src/Components/Patient/PatientRegister.tsx | 2 +- 4 files changed, 10 insertions(+), 18 deletions(-) diff --git a/src/Components/Common/FacilitySelect.tsx b/src/Components/Common/FacilitySelect.tsx index 6385e50fcfe..b207189d27d 100644 --- a/src/Components/Common/FacilitySelect.tsx +++ b/src/Components/Common/FacilitySelect.tsx @@ -3,7 +3,6 @@ import AutoCompleteAsync from "../Form/AutoCompleteAsync"; import { FacilityModel } from "../Facility/models"; import request from "../../Utils/request/request"; import routes from "../../Redux/api"; -import useAuthUser from "../../Common/hooks/useAuthUser"; interface FacilitySelectProps { name: string; @@ -17,7 +16,6 @@ interface FacilitySelectProps { showAll?: boolean; showNOptions?: number; freeText?: boolean; - homeFacility?: boolean; selected?: FacilityModel | FacilityModel[] | null; setSelected: (selected: FacilityModel | FacilityModel[] | null) => void; } @@ -36,11 +34,8 @@ export const FacilitySelect = (props: FacilitySelectProps) => { facilityType, district, freeText = false, - homeFacility = false, errors = "", } = props; - const authUser = useAuthUser(); - const showAllFacilityUsers = ["DistrictAdmin", "StateAdmin"]; const facilitySearch = useCallback( async (text: string) => { @@ -49,21 +44,11 @@ export const FacilitySelect = (props: FacilitySelectProps) => { offset: 0, search_text: text, all: searchAll, - name: "", facility_type: facilityType, exclude_user: exclude_user, district, }; - if ( - homeFacility && - !showAllFacilityUsers.includes(authUser.user_type) && - authUser.home_facility_object?.name - ) { - query["name"] = authUser.home_facility_object?.name; - query["limit"] = 1; - } - const { data } = await request( showAll ? routes.getAllFacilities : routes.getPermittedFacilities, { query } diff --git a/src/Components/ExternalResult/FacilitiesSelectDialogue.tsx b/src/Components/ExternalResult/FacilitiesSelectDialogue.tsx index b7153c62f32..7239e0b912d 100644 --- a/src/Components/ExternalResult/FacilitiesSelectDialogue.tsx +++ b/src/Components/ExternalResult/FacilitiesSelectDialogue.tsx @@ -29,7 +29,6 @@ const FacilitiesSelectDialog = (props: Props) => { errors="" showAll={false} multiple={false} - homeFacility={true} />
diff --git a/src/Components/Patient/ManagePatients.tsx b/src/Components/Patient/ManagePatients.tsx index d853a5cf71a..09077cb3828 100644 --- a/src/Components/Patient/ManagePatients.tsx +++ b/src/Components/Patient/ManagePatients.tsx @@ -734,13 +734,21 @@ export const PatientManager = () => { { + const showAllFacilityUsers = ["DistrictAdmin", "StateAdmin"]; if (qParams.facility) navigate(`/facility/${qParams.facility}/patient`); else if (permittedFacilities?.results.length === 1) navigate( `/facility/${permittedFacilities?.results[0].id}/patient` ); - else setShowDialog(true); + else if ( + !showAllFacilityUsers.includes(authUser.user_type) && + authUser.home_facility_object?.id + ) { + navigate( + `/facility/${authUser.home_facility_object?.id}/patient` + ); + } else setShowDialog(true); }} className="w-full lg:w-fit" > diff --git a/src/Components/Patient/PatientRegister.tsx b/src/Components/Patient/PatientRegister.tsx index 006757f7941..d44dd1cd3fa 100644 --- a/src/Components/Patient/PatientRegister.tsx +++ b/src/Components/Patient/PatientRegister.tsx @@ -222,7 +222,7 @@ export const PatientRegister = (props: PatientRegisterProps) => { const showAllFacilityUsers = ["DistrictAdmin", "StateAdmin"]; if ( !showAllFacilityUsers.includes(authUser.user_type) && - authUser.home_facility_object?.name + authUser.home_facility_object?.id !== facilityId ) { navigate("/facility"); } From 9d1db0a05fdc742aee2a15324535014ffeb77fea Mon Sep 17 00:00:00 2001 From: Ashraf Mohammed <98876115+AshrafMd-1@users.noreply.github.com> Date: Sun, 3 Mar 2024 12:57:08 +0530 Subject: [PATCH 4/6] Update ManagePatients.tsx --- src/Components/Patient/ManagePatients.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Components/Patient/ManagePatients.tsx b/src/Components/Patient/ManagePatients.tsx index 09077cb3828..3d15baa90fa 100644 --- a/src/Components/Patient/ManagePatients.tsx +++ b/src/Components/Patient/ManagePatients.tsx @@ -748,7 +748,8 @@ export const PatientManager = () => { navigate( `/facility/${authUser.home_facility_object?.id}/patient` ); - } else setShowDialog(true); + } + else setShowDialog(true); }} className="w-full lg:w-fit" > From fb885a4d35eab34d85c1b6b9b1382c8159bf438e Mon Sep 17 00:00:00 2001 From: Ashraf Mohammed Date: Sun, 3 Mar 2024 19:56:09 +0530 Subject: [PATCH 5/6] add linting --- src/Components/Patient/ManagePatients.tsx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Components/Patient/ManagePatients.tsx b/src/Components/Patient/ManagePatients.tsx index 3d15baa90fa..09077cb3828 100644 --- a/src/Components/Patient/ManagePatients.tsx +++ b/src/Components/Patient/ManagePatients.tsx @@ -748,8 +748,7 @@ export const PatientManager = () => { navigate( `/facility/${authUser.home_facility_object?.id}/patient` ); - } - else setShowDialog(true); + } else setShowDialog(true); }} className="w-full lg:w-fit" > From 78a458840e8d939d0b4fb21a7c72a6aadd15045e Mon Sep 17 00:00:00 2001 From: Ashraf Mohammed Date: Tue, 19 Mar 2024 20:52:52 +0530 Subject: [PATCH 6/6] add checks --- src/Components/Common/FacilitySelect.tsx | 7 +++- .../FacilitiesSelectDialogue.tsx | 12 ++++++ src/Components/Patient/PatientRegister.tsx | 42 ++++++++++++++----- 3 files changed, 50 insertions(+), 11 deletions(-) diff --git a/src/Components/Common/FacilitySelect.tsx b/src/Components/Common/FacilitySelect.tsx index b207189d27d..196ac004aa2 100644 --- a/src/Components/Common/FacilitySelect.tsx +++ b/src/Components/Common/FacilitySelect.tsx @@ -18,6 +18,8 @@ interface FacilitySelectProps { freeText?: boolean; selected?: FacilityModel | FacilityModel[] | null; setSelected: (selected: FacilityModel | FacilityModel[] | null) => void; + districtCode?: string; + stateCode?: string; } export const FacilitySelect = (props: FacilitySelectProps) => { @@ -35,6 +37,8 @@ export const FacilitySelect = (props: FacilitySelectProps) => { district, freeText = false, errors = "", + districtCode = "", + stateCode = "", } = props; const facilitySearch = useCallback( @@ -46,7 +50,8 @@ export const FacilitySelect = (props: FacilitySelectProps) => { all: searchAll, facility_type: facilityType, exclude_user: exclude_user, - district, + district: districtCode, + state: stateCode, }; const { data } = await request( diff --git a/src/Components/ExternalResult/FacilitiesSelectDialogue.tsx b/src/Components/ExternalResult/FacilitiesSelectDialogue.tsx index 7239e0b912d..95dc3ed77b4 100644 --- a/src/Components/ExternalResult/FacilitiesSelectDialogue.tsx +++ b/src/Components/ExternalResult/FacilitiesSelectDialogue.tsx @@ -3,6 +3,7 @@ import DialogModal from "../Common/Dialog"; import { FacilitySelect } from "../Common/FacilitySelect"; import { FacilityModel } from "../Facility/models"; import { useTranslation } from "react-i18next"; +import useAuthUser from "../../Common/hooks/useAuthUser"; interface Props { show: boolean; @@ -15,6 +16,7 @@ interface Props { const FacilitiesSelectDialog = (props: Props) => { const { show, handleOk, handleCancel, selectedFacility, setSelected } = props; const { t } = useTranslation(); + const authUser = useAuthUser(); return ( { errors="" showAll={false} multiple={false} + {...(authUser?.user_type === "DistrictAdmin" + ? { + districtCode: authUser.district?.toString(), + } + : {})} + {...(authUser?.user_type === "StateAdmin" + ? { + stateCode: authUser.state?.toString(), + } + : {})} />
diff --git a/src/Components/Patient/PatientRegister.tsx b/src/Components/Patient/PatientRegister.tsx index d44dd1cd3fa..9b12eb5aa4a 100644 --- a/src/Components/Patient/PatientRegister.tsx +++ b/src/Components/Patient/PatientRegister.tsx @@ -60,6 +60,7 @@ import useAuthUser from "../../Common/hooks/useAuthUser.js"; import useQuery from "../../Utils/request/useQuery.js"; import routes from "../../Redux/api.js"; import request from "../../Utils/request/request.js"; +import Error404 from "../ErrorPages/404"; const Loading = lazy(() => import("../Common/Loading")); const PageTitle = lazy(() => import("../Common/PageTitle")); @@ -211,6 +212,8 @@ export const PatientRegister = (props: PatientRegisterProps) => { const [insuranceDetailsError, setInsuranceDetailsError] = useState(); + const [isAuthorized, setIsAuthorized] = useState(true); + useEffect(() => { if (extId && formField) { setCareExtId(extId); @@ -218,16 +221,6 @@ export const PatientRegister = (props: PatientRegisterProps) => { } }, [careExtId, formField]); - useEffect(() => { - const showAllFacilityUsers = ["DistrictAdmin", "StateAdmin"]; - if ( - !showAllFacilityUsers.includes(authUser.user_type) && - authUser.home_facility_object?.id !== facilityId - ) { - navigate("/facility"); - } - }, []); - const headerText = !id ? "Add Details of Patient" : "Update Patient Details"; const buttonText = !id ? "Add Patient" : "Save Details"; @@ -518,6 +511,31 @@ export const PatientRegister = (props: PatientRegisterProps) => { prefetch: !!facilityId, }); + useEffect(() => { + if (!facilityObject) return; + const showAllFacilityUsers = ["DistrictAdmin", "StateAdmin"]; + if ( + (!showAllFacilityUsers.includes(authUser.user_type) && + authUser.home_facility_object?.id !== facilityId) || + (authUser.user_type === "DistrictAdmin" && + authUser.district !== facilityObject?.district) || + (authUser.user_type === "StateAdmin" && + authUser.state !== facilityObject?.state) + ) { + setIsAuthorized(false); + } else { + setIsAuthorized(true); + } + }, [ + facilityObject?.district, + facilityObject?.state, + authUser.user_type, + authUser.home_facility_object?.id, + facilityId, + authUser.district, + authUser.state, + ]); + const validateForm = (form: any) => { const errors: Partial> = {}; @@ -1015,6 +1033,10 @@ export const PatientRegister = (props: PatientRegisterProps) => { return ; } + if (!isAuthorized) { + return ; + } + return (
{statusDialog.show && (