Skip to content

Commit

Permalink
Merge pull request #363 from NUS-Project-SaBai/alert-old-visit-edit-v…
Browse files Browse the repository at this point in the history
…itals

Alert old visit edit vitals
  • Loading branch information
pohanson authored Dec 12, 2024
2 parents c770a84 + 2aa99f5 commit 6419355
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 34 deletions.
14 changes: 14 additions & 0 deletions pages/records/patient-vitals/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import axiosInstance from '@/pages/api/_axiosInstance';
import CustomModal from '@/components/CustomModal';
import useWithLoading from '@/utils/loading';
import NoVisitPlaceholder from '@/components/records/NoVisitPlaceholder';
import { checkRecency } from '@/utils/check';

const PatientVitals = () => {
const [mounted, setMounted] = useState(false);
Expand Down Expand Up @@ -118,6 +119,19 @@ const PatientVitals = () => {
});

const submitVitalsForm = useWithLoading(async () => {
const THREE_DAY_IN_MIN = 4320; // 3 * 60 * 24

const toSubmit = await checkRecency(
patient.pk,
THREE_DAY_IN_MIN,
false,
`Are you sure you want to edit this vital?
Press OK to SUBMIT.
Press CANCEL to NOT SUBMIT.`
);
if (!toSubmit) {
return;
}
const formPayload = {
...vitalsFormDetails,
visit: selectedVisitID,
Expand Down
46 changes: 12 additions & 34 deletions pages/registration/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import CustomModal from '@/components/CustomModal';
import { PageTitle } from '@/components/TextComponents';
import { REGISTRATION_FORM_FIELDS } from '@/utils/constants';
import { VillageContext } from '@/context/VillageContext';
import { checkRecency } from '@/utils/check';

export const submitNewVisit = async patient => {
try {
Expand Down Expand Up @@ -205,41 +206,18 @@ const Registration = () => {
});

const handleNewVisit = useWithLoading(async patient => {
try {
const { data: patient_visits } = await axiosInstance.get(
`/visits?patient=${patient.pk}`
);
if (patient_visits.length > 0) {
const mostRecentVisit = patient_visits.reduce((latest, visit) => {
return new Date(visit.date) > new Date(latest.date) ? visit : latest;
}, patient_visits[0]);

const currentDate = new Date();
const mostRecentVisitDate = new Date(mostRecentVisit.date);
const timeDiffInMin =
Math.abs(currentDate.getTime() - mostRecentVisitDate.getTime()) /
(1000 * 60);

if (timeDiffInMin < 60) {
const isNotDuplicateVisit = confirm(
`It's only been ${Math.floor(timeDiffInMin)} min since a visit for this patient was created.
Are you sure you want to create a new visit?
(Note: duplicate visits will cause unnecessary complications)
Press CANCEL to NOT CREATE a new visit.
Press OK to CREATE a new visit.`
);

if (!isNotDuplicateVisit) {
return;
}
}
}
} catch (error) {
toast.error(`Error fetching patient visits: ${error.message}`);
console.error('Error fetching patient vists:', error);
const isNotRecent = await checkRecency(
patient.pk,
60,
true,
`Are you sure you want to create a new visit?
(Note: duplicate visits will cause unnecessary complications)
Press CANCEL to NOT CREATE a new visit.
Press OK to CREATE a new visit.`
);
if (isNotRecent) {
submitNewVisit(patient);
}

submitNewVisit(patient);
});

return (
Expand Down
52 changes: 52 additions & 0 deletions utils/check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import toast from 'react-hot-toast';
import axiosInstance from '@/pages/api/_axiosInstance';
/**
*
* @param {Number} patientPk patient primary key to check
* @param {Number} timeDiffCheck how much time difference between visit and current time in minute
* @param {boolean} confirmOnRecent whether to confirm on recent; true: current time diff is less than timeDiffCheck;
* @param {string} confirmMessage the message to confirm
* @returns {Promise<boolean>} if it is not recent. true: proceed OR error OR no visit; false: is recent;
*/
export async function checkRecency(
patientPk,
timeDiffCheck,
confirmOnRecent,
confirmMessage = ''
) {
try {
const { data: patient_visits } = await axiosInstance.get(
`/visits?patient=${patientPk}`
);
if (patient_visits.length <= 0) {
return true;
} else {
const mostRecentVisit = patient_visits.reduce((latest, visit) => {
return new Date(visit.date) > new Date(latest.date) ? visit : latest;
}, patient_visits[0]);

const currentDate = new Date();
const mostRecentVisitDate = new Date(mostRecentVisit.date);
const timeDiffInMin =
Math.abs(currentDate.getTime() - mostRecentVisitDate.getTime()) /
(1000 * 60);

if (
confirmOnRecent
? timeDiffInMin < timeDiffCheck
: timeDiffInMin > timeDiffCheck
) {
return confirm(
`It's been ${timeDiffInMin < 1440 ? `${Math.floor(timeDiffInMin)} min` : 'days'} since a visit for this patient was created.
${confirmMessage}
`
);
}
}
} catch (error) {
toast.error(`Error fetching patient visits: ${error.message}`);
console.error('Error fetching patient vists:', error);
return true;
}
return true;
}

0 comments on commit 6419355

Please sign in to comment.