Skip to content

Commit

Permalink
bug fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ad956 committed Jul 2, 2024
1 parent 898b0eb commit edb78b3
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 74 deletions.
3 changes: 2 additions & 1 deletion app/api/auth/verifyotp/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
});
}

Expand Down
9 changes: 4 additions & 5 deletions app/api/patient/appointment/pending/route.ts
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -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",
Expand Down
46 changes: 19 additions & 27 deletions app/api/patient/appointment/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,52 +25,44 @@ 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" }), {
status: 404,
});
}

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) {
Expand Down Expand Up @@ -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,
Expand All @@ -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({
Expand Down
30 changes: 0 additions & 30 deletions app/api/patient/notifications/route.ts

This file was deleted.

7 changes: 3 additions & 4 deletions app/api/patient/route.ts
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -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 });
Expand Down
6 changes: 3 additions & 3 deletions app/api/patient/update-profile/profile/route.ts
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -14,10 +15,9 @@ export async function PUT(request: Request) {
// const decryptedUser = await decrypt(token);
const patient_email = "[email protected]"; //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 } }
);
Expand Down
6 changes: 3 additions & 3 deletions app/api/patient/update-profile/route.ts
Original file line number Diff line number Diff line change
@@ -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<User, "_id">;

Expand All @@ -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: "",
Expand All @@ -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 }
);
Expand Down
2 changes: 1 addition & 1 deletion app/lib/patient/bookAppointment.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"use server";
import { bookingAppointment } from "@/types";
import { bookingAppointment } from "@types";
import { getSessionToken } from "../sessions/sessionUtils";
import getBaseUrl from "@utils/getBaseUrl";

Expand Down

0 comments on commit edb78b3

Please sign in to comment.