diff --git a/app/api/admin/add-admin/route.ts b/app/api/admin/add-admin/route.ts index 3bcbe90..34d900e 100644 --- a/app/api/admin/add-admin/route.ts +++ b/app/api/admin/add-admin/route.ts @@ -14,10 +14,7 @@ export async function POST(request: Request) { const { id, role } = await authenticateUser(authHeader); if (!id || !role) { - return errorHandler( - "Missing user ID or role", - STATUS_CODES.VALIDATION_ERROR - ); + return errorHandler("Missing user ID or role", STATUS_CODES.BAD_REQUEST); } await dbConfig(); diff --git a/app/api/admin/dashboard/recent-users/route.ts b/app/api/admin/dashboard/recent-users/route.ts index c0a06b3..1cc2f46 100644 --- a/app/api/admin/dashboard/recent-users/route.ts +++ b/app/api/admin/dashboard/recent-users/route.ts @@ -15,10 +15,7 @@ export async function GET(request: Request): Promise { const { id, role } = await authenticateUser(authHeader); if (!id || !role) { - return errorHandler( - "Missing user ID or role", - STATUS_CODES.VALIDATION_ERROR - ); + return errorHandler("Missing user ID or role", STATUS_CODES.BAD_REQUEST); } const url = new URL(request.url); diff --git a/app/api/admin/dashboard/tiles/route.ts b/app/api/admin/dashboard/tiles/route.ts index 4d98887..5e05ffc 100644 --- a/app/api/admin/dashboard/tiles/route.ts +++ b/app/api/admin/dashboard/tiles/route.ts @@ -11,10 +11,7 @@ export async function GET(request: Request) { const { id, role } = await authenticateUser(authHeader); if (!id || !role) { - return errorHandler( - "Missing user ID or role", - STATUS_CODES.VALIDATION_ERROR - ); + return errorHandler("Missing user ID or role", STATUS_CODES.BAD_REQUEST); } await dbConfig(); diff --git a/app/api/admin/hospitals/route.ts b/app/api/admin/hospitals/route.ts index de4b6aa..233c9e2 100644 --- a/app/api/admin/hospitals/route.ts +++ b/app/api/admin/hospitals/route.ts @@ -12,10 +12,7 @@ export async function GET(request: Request) { const { id, role } = await authenticateUser(authHeader); if (!id || !role) { - return errorHandler( - "Missing user ID or role", - STATUS_CODES.VALIDATION_ERROR - ); + return errorHandler("Missing user ID or role", STATUS_CODES.BAD_REQUEST); } const admin_id = new Types.ObjectId(id); diff --git a/app/api/admin/hospitals/users/route.ts b/app/api/admin/hospitals/users/route.ts index 1b43e78..f46f0ca 100644 --- a/app/api/admin/hospitals/users/route.ts +++ b/app/api/admin/hospitals/users/route.ts @@ -12,10 +12,7 @@ export async function GET(request: Request) { const { id, role } = await authenticateUser(authHeader); if (!id || !role) { - return errorHandler( - "Missing user ID or role", - STATUS_CODES.VALIDATION_ERROR - ); + return errorHandler("Missing user ID or role", STATUS_CODES.BAD_REQUEST); } await dbConfig(); @@ -29,10 +26,7 @@ export async function GET(request: Request) { const hospitalId = url.searchParams.get("hospitalId"); if (!hospitalId) { - return errorHandler( - "hospitalId is required", - STATUS_CODES.VALIDATION_ERROR - ); + return errorHandler("hospitalId is required", STATUS_CODES.BAD_REQUEST); } // Convert hospitalId to ObjectId diff --git a/app/api/admin/route.ts b/app/api/admin/route.ts index ef2b09b..b704e93 100644 --- a/app/api/admin/route.ts +++ b/app/api/admin/route.ts @@ -11,10 +11,7 @@ export async function GET(request: Request) { const { id, role } = await authenticateUser(authHeader); if (!id || !role) { - return errorHandler( - "Missing user ID or role", - STATUS_CODES.VALIDATION_ERROR - ); + return errorHandler("Missing user ID or role", STATUS_CODES.BAD_REQUEST); } const admin_id = new Types.ObjectId(id); diff --git a/app/api/admin/transactions/route.ts b/app/api/admin/transactions/route.ts index 72b8254..eb7f2a0 100644 --- a/app/api/admin/transactions/route.ts +++ b/app/api/admin/transactions/route.ts @@ -12,10 +12,7 @@ export async function GET(request: Request) { const { id, role } = await authenticateUser(authHeader); if (!id || !role) { - return errorHandler( - "Missing user ID or role", - STATUS_CODES.VALIDATION_ERROR - ); + return errorHandler("Missing user ID or role", STATUS_CODES.BAD_REQUEST); } await dbConfig(); diff --git a/app/api/patient/appointment/pending/route.ts b/app/api/patient/appointment/pending/route.ts index babbdc5..4a8dbb6 100644 --- a/app/api/patient/appointment/pending/route.ts +++ b/app/api/patient/appointment/pending/route.ts @@ -12,10 +12,7 @@ export async function POST(req: Request) { const { id, role } = await authenticateUser(authHeader); if (!id || !role) { - return errorHandler( - "Missing user ID or role", - STATUS_CODES.VALIDATION_ERROR - ); + return errorHandler("Missing user ID or role", STATUS_CODES.BAD_REQUEST); } const patient_id = new Types.ObjectId(id); diff --git a/app/api/patient/appointment/route.ts b/app/api/patient/appointment/route.ts index 532e96d..9ba5c5e 100644 --- a/app/api/patient/appointment/route.ts +++ b/app/api/patient/appointment/route.ts @@ -16,10 +16,7 @@ export async function GET(request: Request) { const { id, role } = await authenticateUser(authHeader); if (!id || !role) { - return errorHandler( - "Missing user ID or role", - STATUS_CODES.VALIDATION_ERROR - ); + return errorHandler("Missing user ID or role", STATUS_CODES.BAD_REQUEST); } const patient_id = new Types.ObjectId(id); @@ -84,10 +81,7 @@ export async function POST(req: Request) { const { id, role } = await authenticateUser(authHeader); if (!id || !role) { - return errorHandler( - "Missing user ID or role", - STATUS_CODES.VALIDATION_ERROR - ); + return errorHandler("Missing user ID or role", STATUS_CODES.BAD_REQUEST); } const patient_id = new Types.ObjectId(id); diff --git a/app/api/patient/medicalhistory/route.ts b/app/api/patient/medicalhistory/route.ts index ee65dd6..2e3da3e 100644 --- a/app/api/patient/medicalhistory/route.ts +++ b/app/api/patient/medicalhistory/route.ts @@ -9,10 +9,7 @@ export async function GET(request: Request) { const role = request.headers.get("x-user-role"); if (!id || !role) { - return errorHandler( - "Missing user ID or role", - STATUS_CODES.VALIDATION_ERROR - ); + return errorHandler("Missing user ID or role", STATUS_CODES.BAD_REQUEST); } const patient_id = new Types.ObjectId(id); diff --git a/app/api/patient/paymenthistory/route.ts b/app/api/patient/paymenthistory/route.ts index a12204b..7346e7c 100644 --- a/app/api/patient/paymenthistory/route.ts +++ b/app/api/patient/paymenthistory/route.ts @@ -9,10 +9,7 @@ export async function GET(request: Request) { const role = request.headers.get("x-user-role"); if (!id || !role) { - return errorHandler( - "Missing user ID or role", - STATUS_CODES.VALIDATION_ERROR - ); + return errorHandler("Missing user ID or role", STATUS_CODES.BAD_REQUEST); } const patient_id = new Types.ObjectId(id); diff --git a/app/api/patient/route.ts b/app/api/patient/route.ts index 32a6b2e..1c5a9ff 100644 --- a/app/api/patient/route.ts +++ b/app/api/patient/route.ts @@ -11,10 +11,7 @@ export async function GET(request: Request) { const { id, role } = await authenticateUser(authHeader); if (!id || !role) { - return errorHandler( - "Missing user ID or role", - STATUS_CODES.VALIDATION_ERROR - ); + return errorHandler("Missing user ID or role", STATUS_CODES.BAD_REQUEST); } const patient_id = new Types.ObjectId(id); diff --git a/app/api/receptionist/appointments/approve/route.ts b/app/api/receptionist/appointments/approve/route.ts index 5538746..3efeb2b 100644 --- a/app/api/receptionist/appointments/approve/route.ts +++ b/app/api/receptionist/appointments/approve/route.ts @@ -1,76 +1,53 @@ import dbConfig from "@utils/db"; import { BookedAppointment, Receptionist } from "@models/index"; import { Types } from "mongoose"; +import { authenticateUser } from "@lib/auth/authenticateUser"; +import { NextResponse } from "next/server"; +import { errorHandler, STATUS_CODES } from "@utils/index"; -// get approved appointments +// Get approved appointments export async function GET(request: Request) { try { - const id = request.headers.get("x-user-id"); - const role = request.headers.get("x-user-role"); + const authHeader = request.headers.get("Authorization"); + const { id, role } = await authenticateUser(authHeader); if (!id || !role) { - return Response.json( - { error: "Missing user ID or role" }, - { status: 400 } - ); + return errorHandler("Missing user ID or role", STATUS_CODES.BAD_REQUEST); } const receptionist_id = new Types.ObjectId(id); - const { searchParams } = new URL(request.url); const patient_id = searchParams.get("patient_id"); if (!patient_id) { - return Response.json( - { error: "Patient id is required" }, - { - status: 400, - } - ); + return errorHandler("Patient ID is required", STATUS_CODES.BAD_REQUEST); } - // Convert the patient_id string to an ObjectId const patientObjectId = new Types.ObjectId(patient_id); await dbConfig(); - // Fetch the booked appointments for the specific patient and their receptionist const appointments = await BookedAppointment.find({ patient_id: patientObjectId, - // Add the condition to filter by receptionist_id receptionist_id: { $exists: true }, }); - return Response.json( - { appointments }, - { - status: 200, - } - ); + return NextResponse.json({ appointments }, { status: 200 }); } catch (error) { console.error("Error fetching patient appointments:", error); - return Response.json( - { error: "Internal Server Error" }, - { - status: 500, - } - ); + return errorHandler("Internal Server Error", STATUS_CODES.SERVER_ERROR); } } -// approving appointments +// Approving appointments export async function POST(request: Request) { try { const { patient_id } = await request.json(); - - const id = request.headers.get("x-user-id"); - const role = request.headers.get("x-user-role"); + const authHeader = request.headers.get("Authorization"); + const { id, role } = await authenticateUser(authHeader); if (!id || !role) { - return Response.json( - { error: "Missing user ID or role" }, - { status: 400 } - ); + return errorHandler("Missing user ID or role", STATUS_CODES.BAD_REQUEST); } const receptionist_id = new Types.ObjectId(id); @@ -80,44 +57,28 @@ export async function POST(request: Request) { const receptionist = await Receptionist.findById(receptionist_id); if (!receptionist) { - return Response.json( - { error: "Receptionist not found" }, - { - status: 404, - } - ); + return errorHandler("Receptionist not found", STATUS_CODES.NOT_FOUND); } - // update the approved status of the pending appointment for the specific patient to "approved" const updatedAppointment = await BookedAppointment.findOneAndUpdate( { approved: "pending", patient_id }, { $set: { approved: "approved", receptionist_id: receptionist._id } }, - { new: true } // returns the updated document instead of the original document + { new: true } ); - // check if any document was updated if (!updatedAppointment) { - return Response.json( - { - error: "Something went wrong while approving the appointment.", - }, - { status: 400 } + return errorHandler( + "Something went wrong while approving the appointment.", + STATUS_CODES.BAD_REQUEST ); } - return Response.json( + return NextResponse.json( { appointment: updatedAppointment }, - { - status: 200, - } + { status: 200 } ); } catch (error) { console.error("Error updating pending patient appointment:", error); - return Response.json( - { error: "Internal Server Error" }, - { - status: 500, - } - ); + return errorHandler("Internal Server Error", STATUS_CODES.SERVER_ERROR); } } diff --git a/app/api/receptionist/appointments/pending/route.ts b/app/api/receptionist/appointments/pending/route.ts index 6ea603e..a725183 100644 --- a/app/api/receptionist/appointments/pending/route.ts +++ b/app/api/receptionist/appointments/pending/route.ts @@ -1,17 +1,17 @@ import dbConfig from "@utils/db"; import { Patient, BookedAppointment, Receptionist } from "@models/index"; import { Types } from "mongoose"; +import { authenticateUser } from "@lib/auth/authenticateUser"; +import { NextResponse } from "next/server"; +import { errorHandler, STATUS_CODES } from "@utils/index"; export async function GET(request: Request) { try { - const id = request.headers.get("x-user-id"); - const role = request.headers.get("x-user-role"); + const authHeader = request.headers.get("Authorization"); + const { id, role } = await authenticateUser(authHeader); if (!id || !role) { - return Response.json( - { error: "Missing user ID or role" }, - { status: 400 } - ); + return errorHandler("Missing user ID or role", STATUS_CODES.BAD_REQUEST); } const receptionist_id = new Types.ObjectId(id); @@ -23,11 +23,12 @@ export async function GET(request: Request) { }); if (!currentHospitalResult) { - return Response.json( - { error: "Receptionist hospital isn't selected" }, - { status: 404 } + return errorHandler( + "Receptionist hospital isn't selected", + STATUS_CODES.NOT_FOUND ); } + const currentHospitalId = currentHospitalResult.current_hospital; const pendingAppointments = await BookedAppointment.find({ @@ -35,14 +36,8 @@ export async function GET(request: Request) { "hospital.id": currentHospitalId, }); - // Empty array returned if appointments are not found if (pendingAppointments.length === 0) { - return Response.json( - { patientDetails: [] }, - { - status: 200, - } - ); + return NextResponse.json({ patientDetails: [] }, { status: 200 }); } const patientIds = pendingAppointments.map( @@ -63,7 +58,6 @@ export async function GET(request: Request) { } ); - // Adding disease, note, date, and timing to each patient detail const patientDetailsWithAdditionalInfo = patientDetails.map((patient) => { const appointment = pendingAppointments.find( (appointment) => @@ -81,17 +75,12 @@ export async function GET(request: Request) { return patient.toObject(); }); - return Response.json( + return NextResponse.json( { patientDetails: patientDetailsWithAdditionalInfo }, { status: 200 } ); } catch (error) { console.error("Error fetching pending patient appointments:", error); - return Response.json( - { error: "Internal Server Error" }, - { - status: 500, - } - ); + return errorHandler("Internal Server Error", STATUS_CODES.SERVER_ERROR); } } diff --git a/app/api/receptionist/route.ts b/app/api/receptionist/route.ts index aaf7714..754ffb1 100644 --- a/app/api/receptionist/route.ts +++ b/app/api/receptionist/route.ts @@ -1,21 +1,19 @@ -import dbConfig from "@utils/db"; +import { authenticateUser } from "@lib/auth/authenticateUser"; +import { dbConfig, errorHandler, STATUS_CODES } from "@utils/index"; import Receptionist from "@models/receptionist"; import { Types } from "mongoose"; +import { NextResponse } from "next/server"; export async function GET(request: Request) { try { - const id = request.headers.get("x-user-id"); - const role = request.headers.get("x-user-role"); + const authHeader = request.headers.get("Authorization"); + const { id, role } = await authenticateUser(authHeader); if (!id || !role) { - return Response.json( - { error: "Missing user ID or role" }, - { status: 400 } - ); + return errorHandler("Missing user ID or role", STATUS_CODES.BAD_REQUEST); } const receptionist_id = new Types.ObjectId(id); - await dbConfig(); const projection = { @@ -25,19 +23,20 @@ export async function GET(request: Request) { current_hospital: 0, }; - const receptionistData = await Receptionist.findById(receptionist_id, { - projection, - }); + const receptionistData = await Receptionist.findById( + receptionist_id, + projection + ); if (!receptionistData) { - return Response.json( - { error: "receptionist not found" }, - { status: 404 } - ); + return errorHandler("Receptionist not found", STATUS_CODES.NOT_FOUND); } - return Response.json(receptionistData, { status: 200 }); - } catch (error) { + return NextResponse.json(receptionistData, { status: 200 }); + } catch (error: any) { console.error("Error fetching receptionist data:", error); - return Response.json({ error: "Internal Server Error" }, { status: 500 }); + return errorHandler( + error.message || "Internal Server Error", + STATUS_CODES.SERVER_ERROR + ); } } diff --git a/app/api/receptionist/scan/route.ts b/app/api/receptionist/scan/route.ts index 9d92bc9..a52fb61 100644 --- a/app/api/receptionist/scan/route.ts +++ b/app/api/receptionist/scan/route.ts @@ -1,26 +1,28 @@ import dbConfig from "@utils/db"; -import { BookedAppointment, Patient, Receptionist } from "@models/index"; +import { BookedAppointment, Patient } from "@models/index"; +import { authenticateUser } from "@lib/auth/authenticateUser"; import { Types } from "mongoose"; +import { NextResponse } from "next/server"; +import { errorHandler, STATUS_CODES } from "@utils/index"; export async function POST(req: Request) { try { - const { email } = await req.json(); + const authHeader = req.headers.get("Authorization"); + const { id, role } = await authenticateUser(authHeader); + + if (!id || !role) { + return errorHandler("Missing user ID or role", STATUS_CODES.BAD_REQUEST); + } + const { email } = await req.json(); console.log(email); await dbConfig(); - // const waitingCollection = db.collection("waiting"); - const patient = await Patient.findOne({ email }); if (!patient) { - return Response.json( - { error: "Patient not found" }, - { - status: 404, - } - ); + return errorHandler("Patient not found", STATUS_CODES.NOT_FOUND); } const appointment = await BookedAppointment.findOne({ @@ -35,21 +37,12 @@ export async function POST(req: Request) { ); } - // const patientId = patient._id; - - // await waitingCollection.insertOne({ patientId }); - - return Response.json( + return NextResponse.json( { message: "Successfully scanned QR" }, { status: 201 } ); - } catch (error) { - console.error("Error scanning patient qr code:", error); - return Response.json( - { error: "Internal Server Error" }, - { - status: 500, - } - ); + } catch (error: any) { + console.error("Error scanning patient QR code:", error); + return errorHandler("Internal Server Error", STATUS_CODES.SERVER_ERROR); } } diff --git a/app/utils/constants/index.ts b/app/utils/constants/index.ts index 63e85d1..8fe42d7 100644 --- a/app/utils/constants/index.ts +++ b/app/utils/constants/index.ts @@ -121,7 +121,7 @@ const receptionistadditionalDetails = { }; const STATUS_CODES = { - VALIDATION_ERROR: 400, + BAD_REQUEST: 400, UNAUTHORIZED: 401, FORBIDDEN: 403, NOT_FOUND: 404, diff --git a/app/utils/errorHandler.ts b/app/utils/errorHandler.ts index 085b2cc..085a1b9 100644 --- a/app/utils/errorHandler.ts +++ b/app/utils/errorHandler.ts @@ -18,7 +18,7 @@ function createErrorResponse(err: AppError) { function getErrorTitle(statusCode: number): string { const titles: Record = { - [STATUS_CODES.VALIDATION_ERROR]: "VALIDATION_ERROR", + [STATUS_CODES.BAD_REQUEST]: "BAD_REQUEST", [STATUS_CODES.UNAUTHORIZED]: "UNAUTHORIZED", [STATUS_CODES.FORBIDDEN]: "FORBIDDEN", [STATUS_CODES.NOT_FOUND]: "NOT_FOUND",