Skip to content

Commit

Permalink
Add function to authorize user to register patient (ohcnetwork#9000)
Browse files Browse the repository at this point in the history
  • Loading branch information
JavidSumra authored Nov 20, 2024
1 parent f79a2a4 commit 85a445c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 36 deletions.
26 changes: 15 additions & 11 deletions src/components/Facility/FacilityHome.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down Expand Up @@ -458,17 +460,19 @@ export const FacilityHome = ({ facilityId }: Props) => {
{CameraFeedPermittedUserTypes.includes(authUser.user_type) && (
<LiveMonitoringButton />
)}
<ButtonV2
variant="primary"
ghost
border
className="mt-2 flex w-full flex-row justify-center md:w-auto"
onClick={() => navigate(`/facility/${facilityId}/patient`)}
authorizeFor={NonReadOnlyUsers}
>
<CareIcon icon="l-plus" className="text-lg" />
<span className="text-sm">{t("add_details_of_patient")}</span>
</ButtonV2>
{patientRegisterAuth(authUser, facilityData, facilityId) && (
<ButtonV2
variant="primary"
ghost
border
className="mt-2 flex w-full flex-row justify-center md:w-auto"
onClick={() => navigate(`/facility/${facilityId}/patient`)}
authorizeFor={NonReadOnlyUsers}
>
<CareIcon icon="l-plus" className="text-lg" />
<span className="text-sm">{t("add_details_of_patient")}</span>
</ButtonV2>
)}
<ButtonV2
id="view-patient-facility-list"
variant="primary"
Expand Down
61 changes: 36 additions & 25 deletions src/components/Patient/PatientRegister.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import TransferPatientDialog from "@/components/Facility/TransferPatientDialog";
import {
DistrictModel,
DupPatientModel,
FacilityModel,
WardModel,
} from "@/components/Facility/models";
import {
Expand Down Expand Up @@ -51,6 +52,7 @@ import {
PatientMeta,
PatientModel,
} from "@/components/Patient/models";
import { UserModel } from "@/components/Users/models";

import useAppHistory from "@/hooks/useAppHistory";
import useAuthUser from "@/hooks/useAuthUser";
Expand Down Expand Up @@ -818,31 +820,12 @@ export const PatientRegister = (props: PatientRegisterProps) => {
return <Loading />;
}

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 <Error404 />;
}

Expand Down Expand Up @@ -1717,3 +1700,31 @@ export const PatientRegister = (props: PatientRegisterProps) => {
</Form>
);
};

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;
}

0 comments on commit 85a445c

Please sign in to comment.