api for add,update, view, delete appointment; modified specialist schema

This commit is contained in:
MD ARIF KHAN 2023-04-07 14:41:24 +05:30
parent bf3b4ba50c
commit 68fb80f96d
5 changed files with 237 additions and 8 deletions

4
app.js
View File

@ -47,6 +47,8 @@ import TaxRouter from "./resources/Tax/tax_routes.js";
import SpecialtiesRouter from "./resources/Specialties/SpecialtiesRoute.js"; import SpecialtiesRouter from "./resources/Specialties/SpecialtiesRoute.js";
//specialist //specialist
import SpecialistRouter from "./resources/Specialist/SpecialistRoute.js"; import SpecialistRouter from "./resources/Specialist/SpecialistRoute.js";
//appointments
import AppointmentRouter from "./resources/Appointments/AppointmentRoute.js";
app.use("/api/v1/", user); app.use("/api/v1/", user);
@ -80,5 +82,7 @@ app.use("/api/config", ConfigRouter);
app.use("/api/config/specialty", SpecialtiesRouter); app.use("/api/config/specialty", SpecialtiesRouter);
//specialties //specialties
app.use("/api/specialist", SpecialistRouter); app.use("/api/specialist", SpecialistRouter);
//appointments
app.use("/api/appointment", AppointmentRouter);
export default app; export default app;

View File

@ -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,
});
}
};

View File

@ -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 };

View File

@ -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;

View File

@ -46,19 +46,15 @@ const specialistSchema = new mongoose.Schema({
trim: true, trim: true,
maxlength: [100, "Specialty name cannot exceed 100 characters"], maxlength: [100, "Specialty name cannot exceed 100 characters"],
}, },
location: { perPatientTime: {
type: String, type: String,
trim: true, required: [true, "Please enter per Patient Time"],
maxlength: [100, "Location cannot exceed 100 characters"],
}, },
daysAvailable: { daysAvailable: {
type: [daysAvailableSchema], type: [daysAvailableSchema],
}, },
phone: {
type: String,
trim: true,
maxlength: [100, "Phone number cannot exceed 100 characters"],
},
createdAt: { createdAt: {
type: Date, type: Date,
default: Date.now, default: Date.now,