Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function to authorize user to register patient #9000

Open
wants to merge 13 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions src/components/Facility/FacilityHome.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down Expand Up @@ -460,17 +462,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
62 changes: 37 additions & 25 deletions src/components/Patient/PatientRegister.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import TransferPatientDialog from "@/components/Facility/TransferPatientDialog";
import {
DistrictModel,
DupPatientModel,
FacilityModel,
WardModel,
} from "@/components/Facility/models";
import {
Expand Down Expand Up @@ -94,6 +95,8 @@ import {
scrollTo,
} from "@/Utils/utils";

import { UserModel } from "../Users/models";

type PatientForm = PatientModel &
PatientMeta & { age?: number; is_postpartum?: boolean };

Expand Down Expand Up @@ -919,31 +922,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)
) {
JavidSumra marked this conversation as resolved.
Show resolved Hide resolved
return <Error404 />;
}

Expand Down Expand Up @@ -1889,3 +1873,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;
}
JavidSumra marked this conversation as resolved.
Show resolved Hide resolved
Loading