claim page done

This commit is contained in:
2025-07-28 17:49:01 +05:30
parent d9ce19df80
commit 0930b70bfd
7 changed files with 152 additions and 206 deletions

View File

@@ -183,6 +183,8 @@ router.post(
});
const userId = req.user!.id;
const originalStartTime = appointmentData.startTime;
const MAX_END_TIME = "18:30";
// 1. Verify patient exists and belongs to user
const patient = await storage.getPatient(appointmentData.patientId);
@@ -196,39 +198,87 @@ router.post(
});
}
// 2. Check if patient already has an appointment on the same date and time.
const sameDayAppointment = await storage.getPatientAppointmentByDateTime(
appointmentData.patientId,
appointmentData.date,
appointmentData.startTime
);
// 3. Check if there's already an appointment at this time slot of Staff.
const staffConflict = await storage.getStaffAppointmentByDateTime(
appointmentData.staffId,
appointmentData.date,
appointmentData.startTime,
sameDayAppointment?.id
);
// 2. Attempt to find the next available slot
let [hour, minute] = originalStartTime.split(":").map(Number);
if (staffConflict) {
return res.status(409).json({
message:
"This time slot is already booked for the selected staff. Please choose another time or staff member.",
});
}
const pad = (n: number) => n.toString().padStart(2, "0");
// 4. If same-day appointment exists, update it
if (sameDayAppointment?.id !== undefined) {
const updatedAppointment = await storage.updateAppointment(
sameDayAppointment.id,
appointmentData
while (`${pad(hour)}:${pad(minute)}` <= MAX_END_TIME) {
const currentStartTime = `${pad(hour)}:${pad(minute)}`;
// Check patient appointment at this time
const sameDayAppointment =
await storage.getPatientAppointmentByDateTime(
appointmentData.patientId,
appointmentData.date,
currentStartTime
);
// Check staff conflict at this time
const staffConflict = await storage.getStaffAppointmentByDateTime(
appointmentData.staffId,
appointmentData.date,
currentStartTime,
sameDayAppointment?.id // Ignore self if updating
);
return res.status(200).json(updatedAppointment);
if (!staffConflict) {
const endMinute = minute + 30;
let endHour = hour + Math.floor(endMinute / 60);
let realEndMinute = endMinute % 60;
const currentEndTime = `${pad(endHour)}:${pad(realEndMinute)}`;
const payload = {
...appointmentData,
startTime: currentStartTime,
endTime: currentEndTime,
};
let responseData;
if (sameDayAppointment?.id !== undefined) {
const updated = await storage.updateAppointment(
sameDayAppointment.id,
payload
);
responseData = {
...updated,
originalRequestedTime: originalStartTime,
finalScheduledTime: currentStartTime,
message:
originalStartTime !== currentStartTime
? `Your requested time (${originalStartTime}) was unavailable. Appointment was updated to ${currentStartTime}.`
: `Appointment successfully updated at ${currentStartTime}.`,
};
return res.status(200).json(responseData);
}
const created = await storage.createAppointment(payload);
responseData = {
...created,
originalRequestedTime: originalStartTime,
finalScheduledTime: currentStartTime,
message:
originalStartTime !== currentStartTime
? `Your requested time (${originalStartTime}) was unavailable. Appointment was scheduled at ${currentStartTime}.`
: `Appointment successfully scheduled at ${currentStartTime}.`,
};
return res.status(201).json(responseData);
}
// Move to next 30-min slot
minute += 30;
if (minute >= 60) {
hour += 1;
minute = 0;
}
}
// 6. Otherwise, create a new appointment
const newAppointment = await storage.createAppointment(appointmentData);
return res.status(201).json(newAppointment);
return res.status(409).json({
message:
"No available slots remaining until 6:30 PM for this Staff. Please choose another day.",
});
} catch (error) {
console.error("Error in upsert appointment:", error);

View File

@@ -1,6 +1,6 @@
import { Router } from 'express';
import patientRoutes from './patients';
import appointmentRoutes from './appointements'
import appointmentRoutes from './appointments'
import userRoutes from './users'
import staffRoutes from './staffs'
import pdfExtractionRoutes from './pdfExtraction';

View File

@@ -159,6 +159,29 @@ router.get("/search", async (req: Request, res: Response): Promise<any> => {
}
});
// get patient by insurance id
router.get("/by-insurance-id", async (req: Request, res: Response): Promise<any> => {
const insuranceId = req.query.insuranceId?.toString();
if (!insuranceId) {
return res.status(400).json({ error: "Missing insuranceId" });
}
try {
const patient = await storage.getPatientByInsuranceId(insuranceId);
if (patient) {
return res.status(200).json(patient);
} else {
return res.status(404).json(null);
}
} catch (err) {
console.error("Failed to lookup patient:", err);
return res.status(500).json({ error: "Internal server error" });
}
});
// Get a single patient by ID
router.get(
"/:id",