From 85a445c4dfe0720c26523b35831d55c5ce16604c Mon Sep 17 00:00:00 2001 From: JavidSumra <112365664+JavidSumra@users.noreply.github.com> Date: Wed, 20 Nov 2024 15:34:55 +0530 Subject: [PATCH] Add function to authorize user to register patient (#9000) --- src/components/Facility/FacilityHome.tsx | 26 +++++---- src/components/Patient/PatientRegister.tsx | 61 +++++++++++++--------- 2 files changed, 51 insertions(+), 36 deletions(-) diff --git a/src/components/Facility/FacilityHome.tsx b/src/components/Facility/FacilityHome.tsx index 02c3f5386cb..e38c187590e 100644 --- a/src/components/Facility/FacilityHome.tsx +++ b/src/components/Facility/FacilityHome.tsx @@ -48,6 +48,8 @@ import uploadFile from "@/Utils/request/uploadFile"; import useQuery from "@/Utils/request/useQuery"; import { sleep } from "@/Utils/utils"; +import { patientRegisterAuth } from "../Patient/PatientRegister"; + type Props = { facilityId: string; }; @@ -458,17 +460,19 @@ export const FacilityHome = ({ facilityId }: Props) => { {CameraFeedPermittedUserTypes.includes(authUser.user_type) && ( )} - navigate(`/facility/${facilityId}/patient`)} - authorizeFor={NonReadOnlyUsers} - > - - {t("add_details_of_patient")} - + {patientRegisterAuth(authUser, facilityData, facilityId) && ( + navigate(`/facility/${facilityId}/patient`)} + authorizeFor={NonReadOnlyUsers} + > + + {t("add_details_of_patient")} + + )} { return ; } - const PatientRegisterAuth = () => { - const showAllFacilityUsers = ["DistrictAdmin", "StateAdmin"]; - if ( - !showAllFacilityUsers.includes(authUser.user_type) && - authUser.home_facility_object?.id === facilityId - ) { - return true; - } - if ( - authUser.user_type === "DistrictAdmin" && - authUser.district === facilityObject?.district - ) { - return true; - } - if ( - authUser.user_type === "StateAdmin" && - authUser.state === facilityObject?.state - ) { - return true; - } - - return false; - }; - - if (!isLoading && facilityId && facilityObject && !PatientRegisterAuth()) { + if ( + !isLoading && + facilityId && + facilityObject && + !patientRegisterAuth(authUser, facilityObject, facilityId) + ) { return ; } @@ -1717,3 +1700,31 @@ export const PatientRegister = (props: PatientRegisterProps) => { ); }; + +export function patientRegisterAuth( + authUser: UserModel, + facilityObject: FacilityModel | undefined, + facilityId: string, +) { + const showAllFacilityUsers = ["DistrictAdmin", "StateAdmin"]; + if ( + !showAllFacilityUsers.includes(authUser.user_type) && + authUser.home_facility_object?.id === facilityId + ) { + return true; + } + if ( + authUser.user_type === "DistrictAdmin" && + authUser.district === facilityObject?.district + ) { + return true; + } + if ( + authUser.user_type === "StateAdmin" && + authUser.state === facilityObject?.state + ) { + return true; + } + + return false; +}