diff --git a/src/components/Facility/FacilityHome.tsx b/src/components/Facility/FacilityHome.tsx index 843b958c67..85f0d30d22 100644 --- a/src/components/Facility/FacilityHome.tsx +++ b/src/components/Facility/FacilityHome.tsx @@ -47,6 +47,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; }; @@ -460,17 +462,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 ; } @@ -1889,3 +1873,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; +}