Skip to content

Commit

Permalink
Minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ad956 committed May 23, 2024
1 parent 09cf235 commit 6bb776d
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 35 deletions.
44 changes: 44 additions & 0 deletions app/api/patient/appointment/pending/route.ts
Original file line number Diff line number Diff line change
@@ -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 });
}
}
57 changes: 22 additions & 35 deletions app/api/patient/appointment/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand Down

0 comments on commit 6bb776d

Please sign in to comment.