diff --git a/app/api/auth/verifyotp/route.ts b/app/api/auth/verifyotp/route.ts index dd5bf7b..707b9df 100644 --- a/app/api/auth/verifyotp/route.ts +++ b/app/api/auth/verifyotp/route.ts @@ -18,7 +18,8 @@ export async function POST(req: Request) { if (!body || !body.email || !body.role || !body.action || !body.otp) { return Response.json({ - error: "Email, OTP, and role are required fields in the request body.", + error: + "Email, OTP, action and role are required fields in the request body.", }); } diff --git a/app/api/patient/appointment/pending/route.ts b/app/api/patient/appointment/pending/route.ts index 7712415..0df80f3 100644 --- a/app/api/patient/appointment/pending/route.ts +++ b/app/api/patient/appointment/pending/route.ts @@ -1,6 +1,7 @@ import dbConfig from "@utils/db"; import { ObjectId } from "mongodb"; import { decrypt } from "@sessions/sessionUtils"; +import { Patient, BookedAppointment } from "@models/index"; export async function POST(req: Request) { const session = req.headers.get("Authorization"); @@ -15,18 +16,16 @@ export async function POST(req: Request) { 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"); + await dbConfig(); - const patient = await patient_collection.findOne({ email }); + const patient = await Patient.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({ + const alreadyBookedAppointment = await BookedAppointment.findOne({ patient_id: patient._id, "hospital.id": new ObjectId(hospital_id), approved: "pending", diff --git a/app/api/patient/appointment/route.ts b/app/api/patient/appointment/route.ts index 693edc6..61f63b1 100644 --- a/app/api/patient/appointment/route.ts +++ b/app/api/patient/appointment/route.ts @@ -7,6 +7,7 @@ import { render } from "@react-email/render"; import { AppointmentBookedTemplate } from "@lib/emails/templates"; import { getFormattedDate } from "@utils/getDate"; import sendNotification from "@lib/novu"; +import { Patient, BookedAppointment, Doctor } from "@models/index"; type BookingAppointmentType = bookingAppointment & { transaction_id: string | null; @@ -24,9 +25,8 @@ export async function GET(request: Request) { const decryptedUser = await decrypt(token); const email = decryptedUser.user.email; - const db = await dbConfig(); - const patient_collection = db.collection("patient"); - const patient = await patient_collection.findOne({ email }); + await dbConfig(); + const patient = await Patient.findOne({ email }); if (!patient) { return new Response(JSON.stringify({ error: "Patient not found" }), { @@ -34,42 +34,35 @@ export async function GET(request: Request) { }); } - const bookedAppointments_collection = db.collection("bookedAppointments"); - const appointmentsCursor = bookedAppointments_collection.find({ + let appointments = await BookedAppointment.find({ patient_id: patient._id, approved: "approved", }); - const appointments = await appointmentsCursor.toArray(); // convert cursor to array - - const projection = { - _id: 1, - firstname: 1, - lastname: 1, - specialty: 1, - profile: 1, - }; const doctorIds = appointments.map((appointment) => appointment.doctor_id); - const doctor_collection = db.collection("doctor"); - const doctors = await doctor_collection - .find({ _id: { $in: doctorIds } }, { projection }) - .toArray(); - appointments.forEach((appointment) => { + const doctors = await Doctor.find( + { _id: { $in: doctorIds } }, + { firstname: 1, lastname: 1, specialty: 1, profile: 1 } + ); + + const updatedAppointments = appointments.map((appointment) => { + const appointmentObj = appointment.toObject(); const doctor = doctors.find( - (doc) => doc._id.toString() === appointment.doctor_id.toString() + (doc) => doc._id.toString() === appointmentObj.doctor_id.toString() ); if (doctor) { - appointment.doctor = { + appointmentObj.doctor = { name: `${doctor.firstname} ${doctor.lastname}`, profile: doctor.profile, specialty: doctor.specialty, }; } + return appointmentObj; }); - return new Response(JSON.stringify(appointments), { + return new Response(JSON.stringify(updatedAppointments), { status: 200, }); } catch (error) { @@ -103,15 +96,14 @@ export async function POST(req: Request) { 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"); + await dbConfig(); - const patient = await patient_collection.findOne({ email }); + const patient = await Patient.findOne({ email }); if (!patient) { return Response.json({ error: "Patient not found" }, { status: 404 }); } + console.log("=> " + hospital.hospital_id); const appointmentData = { date, @@ -129,7 +121,7 @@ export async function POST(req: Request) { receptionist_id: null, }; - const res = await appointment_collection.insertOne(appointmentData); + const res = await BookedAppointment.create(appointmentData); if (!res) return Response.json({ diff --git a/app/api/patient/notifications/route.ts b/app/api/patient/notifications/route.ts deleted file mode 100644 index 31b1401..0000000 --- a/app/api/patient/notifications/route.ts +++ /dev/null @@ -1,30 +0,0 @@ -import dbConfig from "@utils/db"; -import { decrypt } from "@sessions/sessionUtils"; - -export async function GET(request: Request) { - const session = request.headers.get("Authorization"); - if (!session) { - return Response.json({ error: "Unauthorized" }, { status: 401 }); - } - try { - const token = session.split("Bearer ")[1]; - const decryptedUser = await decrypt(token); - const email = decryptedUser.user.email; - - const db = await dbConfig(); - const collection = db.collection("patient"); - - const patient = await collection.findOne({ email }); - - if (!patient) { - return Response.json({ error: "Patient not found" }, { status: 404 }); - } - - const notificationsData = patient.notifications; - - return Response.json(notificationsData); - } catch (error) { - console.error("Error fetching notifications data:", error); - return Response.json({ error: "Internal Server Error" }, { status: 500 }); - } -} diff --git a/app/api/patient/route.ts b/app/api/patient/route.ts index 43e9e00..1f5f3ed 100644 --- a/app/api/patient/route.ts +++ b/app/api/patient/route.ts @@ -1,5 +1,6 @@ import dbConfig from "@utils/db"; import { decrypt } from "@sessions/sessionUtils"; +import Patient from "@models/patient"; export async function GET(request: Request) { const session = request.headers.get("Authorization"); @@ -12,18 +13,16 @@ export async function GET(request: Request) { const decryptedUser = await decrypt(token); const email = decryptedUser.user.email; - const db = await dbConfig(); - const collection = db.collection("patient"); + await dbConfig(); const projection = { role: 0, otp: 0, password: 0, current_hospital: 0, - notifications: 0, }; - const patientData = await collection.findOne({ email }, { projection }); + const patientData = await Patient.findOne({ email }, { projection }); if (!patientData) { return Response.json({ error: "Patient not found" }, { status: 404 }); diff --git a/app/api/patient/update-profile/profile/route.ts b/app/api/patient/update-profile/profile/route.ts index 8dd9681..4222c7c 100644 --- a/app/api/patient/update-profile/profile/route.ts +++ b/app/api/patient/update-profile/profile/route.ts @@ -1,5 +1,6 @@ import dbConfig from "@utils/db"; import { decrypt } from "@sessions/sessionUtils"; +import Patient from "@models/patient"; export async function PUT(request: Request) { // const session = request.headers.get("Authorization"); @@ -14,10 +15,9 @@ export async function PUT(request: Request) { // const decryptedUser = await decrypt(token); const patient_email = "anandsuthar956@gmail.com"; //decryptedUser.user.email; - const db = await dbConfig(); - const collection = db.collection("patient"); + await dbConfig(); - const result = await collection.updateOne( + const result = await Patient.updateOne( { email: patient_email }, { $set: { profile: profile_pic } } ); diff --git a/app/api/patient/update-profile/route.ts b/app/api/patient/update-profile/route.ts index f4d4510..2ae86df 100644 --- a/app/api/patient/update-profile/route.ts +++ b/app/api/patient/update-profile/route.ts @@ -1,6 +1,7 @@ import { User } from "@types"; import dbConfig from "@utils/db"; import { decrypt } from "@sessions/sessionUtils"; +import Patient from "@models/patient"; type UpdatedUserType = Omit; @@ -27,8 +28,7 @@ export async function PUT(request: Request) { const decryptedUser = await decrypt(token); const patient_email = decryptedUser.user.email; - const db = await dbConfig(); - const collection = db.collection("patient"); + await dbConfig(); const updatedFields: UpdatedUserType = { firstname: "", @@ -54,7 +54,7 @@ export async function PUT(request: Request) { // if (password) updatedFields.password = password; if (profile) updatedFields.profile = profile; - const result = await collection.updateOne( + const result = await Patient.updateOne( { email: patient_email }, { $set: updatedFields } ); diff --git a/app/lib/patient/bookAppointment.tsx b/app/lib/patient/bookAppointment.tsx index 6e060ad..01f080a 100644 --- a/app/lib/patient/bookAppointment.tsx +++ b/app/lib/patient/bookAppointment.tsx @@ -1,5 +1,5 @@ "use server"; -import { bookingAppointment } from "@/types"; +import { bookingAppointment } from "@types"; import { getSessionToken } from "../sessions/sessionUtils"; import getBaseUrl from "@utils/getBaseUrl";