Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

πŸ”’ Enhance Auth & Error Handling for Receptionist Routes #2

Merged
merged 3 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions app/api/admin/add-admin/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 1 addition & 4 deletions app/api/admin/dashboard/recent-users/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ export async function GET(request: Request): Promise<Response> {
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);
Expand Down
5 changes: 1 addition & 4 deletions app/api/admin/dashboard/tiles/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 1 addition & 4 deletions app/api/admin/hospitals/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 2 additions & 8 deletions app/api/admin/hospitals/users/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
Expand Down
5 changes: 1 addition & 4 deletions app/api/admin/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 1 addition & 4 deletions app/api/admin/transactions/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 1 addition & 4 deletions app/api/patient/appointment/pending/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 2 additions & 8 deletions app/api/patient/appointment/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 1 addition & 4 deletions app/api/patient/medicalhistory/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 1 addition & 4 deletions app/api/patient/paymenthistory/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 1 addition & 4 deletions app/api/patient/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
83 changes: 22 additions & 61 deletions app/api/receptionist/appointments/approve/route.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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);
}
}
37 changes: 13 additions & 24 deletions app/api/receptionist/appointments/pending/route.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -23,26 +23,21 @@ 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({
approved: "pending",
"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(
Expand All @@ -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) =>
Expand All @@ -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);
}
}
Loading
Loading