From 68fb80f96dedfaf79a1084e832f95e415969d8a6 Mon Sep 17 00:00:00 2001 From: MD ARIF KHAN <55360458+arifthehappy@users.noreply.github.com> Date: Fri, 7 Apr 2023 14:41:24 +0530 Subject: [PATCH] api for add,update, view, delete appointment; modified specialist schema --- app.js | 4 + .../Appointments/AppointmentController.js | 124 ++++++++++++++++++ resources/Appointments/AppointmentModel.js | 82 ++++++++++++ resources/Appointments/AppointmentRoute.js | 23 ++++ resources/Specialist/SpecialistModel.js | 12 +- 5 files changed, 237 insertions(+), 8 deletions(-) create mode 100644 resources/Appointments/AppointmentController.js create mode 100644 resources/Appointments/AppointmentModel.js create mode 100644 resources/Appointments/AppointmentRoute.js diff --git a/app.js b/app.js index 6ee566b..db2359b 100644 --- a/app.js +++ b/app.js @@ -47,6 +47,8 @@ import TaxRouter from "./resources/Tax/tax_routes.js"; import SpecialtiesRouter from "./resources/Specialties/SpecialtiesRoute.js"; //specialist import SpecialistRouter from "./resources/Specialist/SpecialistRoute.js"; +//appointments +import AppointmentRouter from "./resources/Appointments/AppointmentRoute.js"; app.use("/api/v1/", user); @@ -80,5 +82,7 @@ app.use("/api/config", ConfigRouter); app.use("/api/config/specialty", SpecialtiesRouter); //specialties app.use("/api/specialist", SpecialistRouter); +//appointments +app.use("/api/appointment", AppointmentRouter); export default app; diff --git a/resources/Appointments/AppointmentController.js b/resources/Appointments/AppointmentController.js new file mode 100644 index 0000000..5498043 --- /dev/null +++ b/resources/Appointments/AppointmentController.js @@ -0,0 +1,124 @@ +import { Appointment } from "./AppointmentModel.js"; +import pkg from "mongoose"; +const { Types } = pkg; + +// create a new appointment +// POST /api/appointment/new +export const createAppointment = async (req, res, next) => { + try { + const doctorId = Types.ObjectId(req.body.doctorId); + const { doctorName, date, time, patientName, patientPhone } = req.body; + const appointment = await Appointment.create({ + doctorId, + doctorName, + date, + time, + patientName, + patientPhone, + }); + res.status(201).json({ + success: true, + appointment, + }); + } catch (error) { + res.status(400).json({ + success: false, + message: error.message, + }); + } +}; + +// update a single appointment +// PUT /api/appointment/update/:id +export const updateAppointment = async (req, res, next) => { + try { + const { id } = req.params; + + const doctorId = Types.ObjectId(req.body.doctorId); + + const { doctorName, date, time, patientName, patientPhone } = req.body; + const update = { + doctorId, + doctorName, + date, + time, + patientName, + patientPhone, + }; + const options = { new: true }; + + const updatedAppointment = await Appointment.findByIdAndUpdate( + id, + update, + options + ); + + res.status(200).json({ + success: true, + updatedAppointment, + }); + } catch (error) { + res.status(400).json({ + success: false, + message: error.message, + }); + } +}; + +// get all appointments +// GET /api/appointment/all +export const getAllAppointments = async (req, res, next) => { + try { + const appointments = await Appointment.find(); + res.status(200).json({ + success: true, + appointments, + }); + } catch (error) { + res.status(400).json({ + success: false, + message: error.message, + }); + } +}; + +// get a single appointment +// GET /api/appointment/:id +export const getSingleAppointment = async (req, res, next) => { + try { + const appointment = await Appointment.findById(req.params.id); + if (!appointment) { + return res.status(404).json({ + success: false, + message: "Appointment not found", + }); + } + res.status(200).json({ + success: true, + appointment, + }); + } catch (error) { + res.status(400).json({ + success: false, + message: error.message, + }); + } +}; + +// delete a single appointment +// DELETE /api/appointment/delete/:id +export const deleteAppointment = async (req, res, next) => { + try { + const { id } = req.params; + const appointment = await Appointment.findByIdAndDelete(id); + res.status(200).json({ + success: true, + appointment, + }); + } catch (error) { + res.status(400).json({ + success: false, + message: error.message, + }); + } +}; diff --git a/resources/Appointments/AppointmentModel.js b/resources/Appointments/AppointmentModel.js new file mode 100644 index 0000000..bf8f245 --- /dev/null +++ b/resources/Appointments/AppointmentModel.js @@ -0,0 +1,82 @@ +import { mongoose } from "mongoose"; + +import { Specialist } from "./../Specialist/SpecialistModel.js"; + +// { +// "doctorId": "642ab7610ea12ad09d27060e", +// "doctorName": "Dr Subbarao", +// "date": "2023-09-17T18:30:00.000Z", date in ISO format +// "time": "10:30 AM", +// "patientName": "huksda", +// "patientPhone": "0937824827" +// } + +const appointmentSchema = new mongoose.Schema({ + doctorId: { + type: mongoose.Schema.Types.ObjectId, + ref: Specialist, + required: true, + }, + doctorName: { + type: String, + required: true, + }, + date: { + type: Date, + required: true, + }, + time: { + type: String, + required: true, + }, + patientName: { + type: String, + required: true, + }, + patientPhone: { + type: String, + required: true, + }, + appointmentNumber: { + type: String, + }, + createdAt: { + type: Date, + default: Date.now, + }, +}); + +// Add a pre-save hook to generate the appointment number for each date for each doctor +appointmentSchema.pre("save", async function (next) { + const appointment = this; + const doctorId = appointment.doctorId; + const date = appointment.date; + //get only month and date without time and timezone info + const dateOnly = new Date(date).toLocaleDateString("en-IN", { + timeZone: "Asia/Kolkata", + }); + + const appointments = await Appointment.find({ + doctorId: doctorId, + date: date, + }); + + const appointmentNumber = appointments.length + 1; + // append the appointment number to the date + appointment.appointmentNumber = dateOnly + "-" + appointmentNumber; + //appointment.appointmentNumber = appointmentNumber; + next(); +}); + +// appointmentSchema.pre("save", async function (next) { +// const appointment = this; +// const date = appointment.date; +// const appointments = await Appointment.find({ date: date }) +// const appointmentNumber = appointments.length + 1; +// appointment.appointmentNumber = appointmentNumber; +// next(); +// }); + +const Appointment = mongoose.model("Appointment", appointmentSchema); + +export { Appointment }; diff --git a/resources/Appointments/AppointmentRoute.js b/resources/Appointments/AppointmentRoute.js new file mode 100644 index 0000000..4277a93 --- /dev/null +++ b/resources/Appointments/AppointmentRoute.js @@ -0,0 +1,23 @@ +import { Router } from "express"; +import { + isAuthenticatedUser, + authorizeRoles, + isBusinessAuthenticated, +} from "../../middlewares/auth.js"; +import { + createAppointment, + deleteAppointment, + getAllAppointments, + getSingleAppointment, + updateAppointment, +} from "./AppointmentController.js"; + +const router = Router(); + +router.route("/new").post(isBusinessAuthenticated, createAppointment); +router.route("/getall").get(getAllAppointments); +router.route("/get/:id").get(getSingleAppointment); +router.route("/update/:id").patch(isBusinessAuthenticated, updateAppointment); +router.route("/delete/:id").delete(isBusinessAuthenticated, deleteAppointment); + +export default router; diff --git a/resources/Specialist/SpecialistModel.js b/resources/Specialist/SpecialistModel.js index 577a4b2..cdff223 100644 --- a/resources/Specialist/SpecialistModel.js +++ b/resources/Specialist/SpecialistModel.js @@ -46,19 +46,15 @@ const specialistSchema = new mongoose.Schema({ trim: true, maxlength: [100, "Specialty name cannot exceed 100 characters"], }, - location: { + perPatientTime: { type: String, - trim: true, - maxlength: [100, "Location cannot exceed 100 characters"], + required: [true, "Please enter per Patient Time"], }, + daysAvailable: { type: [daysAvailableSchema], }, - phone: { - type: String, - trim: true, - maxlength: [100, "Phone number cannot exceed 100 characters"], - }, + createdAt: { type: Date, default: Date.now,