diff --git a/app/api/patient/appointment/pending/route.ts b/app/api/patient/appointment/pending/route.ts new file mode 100644 index 00000000..4e15207d --- /dev/null +++ b/app/api/patient/appointment/pending/route.ts @@ -0,0 +1,44 @@ +import dbConfig from "@lib/db"; +import { ObjectId } from "mongodb"; +import { decrypt } from "@sessions/sessionUtils"; + +export async function POST(req: Request) { + const session = req.headers.get("Authorization"); + if (!session) { + return Response.json({ error: "Unauthorized" }, { status: 401 }); + } + + try { + const hospital_id: string = await req.json(); + + const token = session.split("Bearer ")[1]; + const decryptedUser = await decrypt(token); + const email = decryptedUser.user.email; + + const db = await dbConfig(); + const patient_collection = db.collection("patient"); + const appointment_collection = db.collection("bookedAppointments"); + + const patient = await patient_collection.findOne({ email }); + + if (!patient) { + return Response.json({ error: "Patient not found" }, { status: 404 }); + } + + // Checking if a pending appointment request exists with the hospital + const alreadyBookedAppointment = await appointment_collection.findOne({ + patient_id: patient._id, + "hospital.id": new ObjectId(hospital_id), + approved: "pending", + }); + + if (alreadyBookedAppointment) { + return Response.json({ hasPendingAppointment: true }, { status: 200 }); + } + + return Response.json({ hasPendingAppointment: false }, { status: 200 }); + } catch (error) { + console.error("Error checking pending appointments:", error); + return Response.json({ error: "Internal Server Error" }, { status: 500 }); + } +} diff --git a/app/api/patient/appointment/route.ts b/app/api/patient/appointment/route.ts index 74efb56b..44d4ffc4 100644 --- a/app/api/patient/appointment/route.ts +++ b/app/api/patient/appointment/route.ts @@ -102,41 +102,28 @@ export async function POST(req: Request) { return Response.json({ error: "Patient not found" }, { status: 404 }); } - // Checking if the patient already has a pending appointment - const alreadyBookedAppointment = await appointment_collection.findOne({ - patient_id: patient._id, - approved: "pending", - }); - - if (alreadyBookedAppointment) { - return Response.json( - { error: "You already have a pending appointment request" }, - { status: 400 } - ); - } - - const appointmentData = { - date, - state, - city, - hospital: { - id: new ObjectId(hospital.hospital_id), - name: hospital.hospital_name, - }, - disease, - note, - approved: "pending", - patient_id: patient._id, - doctor_id: null, - receptionist_id: null, - }; - - const res = await appointment_collection.insertOne(appointmentData); - - if (!res) - return Response.json({ - error: "Error saving appointment info", - }); + // const appointmentData = { + // date, + // state, + // city, + // hospital: { + // id: new ObjectId(hospital.hospital_id), + // name: hospital.hospital_name, + // }, + // disease, + // note, + // approved: "pending", + // patient_id: patient._id, + // doctor_id: null, + // receptionist_id: null, + // }; + + // const res = await appointment_collection.insertOne(appointmentData); + + // if (!res) + // return Response.json({ + // error: "Error saving appointment info", + // }); return Response.json( {