feat : add appointment apis

This commit is contained in:
ARJUN S THAMPI
2026-03-16 10:16:27 +05:30
parent 9faa512c0b
commit 9ae190754a
5 changed files with 360 additions and 80 deletions

View File

@@ -0,0 +1,21 @@
-- CreateTable
CREATE TABLE "Appointment" (
"id" SERIAL NOT NULL,
"name" TEXT NOT NULL,
"mobileNumber" TEXT NOT NULL,
"email" TEXT,
"message" TEXT,
"date" TIMESTAMP(3) NOT NULL,
"doctorId" INTEGER NOT NULL,
"departmentId" INTEGER NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
CONSTRAINT "Appointment_pkey" PRIMARY KEY ("id")
);
-- AddForeignKey
ALTER TABLE "Appointment" ADD CONSTRAINT "Appointment_doctorId_fkey" FOREIGN KEY ("doctorId") REFERENCES "Doctor"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "Appointment" ADD CONSTRAINT "Appointment_departmentId_fkey" FOREIGN KEY ("departmentId") REFERENCES "Department"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

View File

@@ -0,0 +1,15 @@
-- DropForeignKey
ALTER TABLE "Appointment" DROP CONSTRAINT "Appointment_departmentId_fkey";
-- DropForeignKey
ALTER TABLE "Appointment" DROP CONSTRAINT "Appointment_doctorId_fkey";
-- AlterTable
ALTER TABLE "Appointment" ALTER COLUMN "doctorId" SET DATA TYPE TEXT,
ALTER COLUMN "departmentId" SET DATA TYPE TEXT;
-- AddForeignKey
ALTER TABLE "Appointment" ADD CONSTRAINT "Appointment_doctorId_fkey" FOREIGN KEY ("doctorId") REFERENCES "Doctor"("doctorId") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "Appointment" ADD CONSTRAINT "Appointment_departmentId_fkey" FOREIGN KEY ("departmentId") REFERENCES "Department"("departmentId") ON DELETE RESTRICT ON UPDATE CASCADE;

View File

@@ -1,4 +1,3 @@
generator client { generator client {
provider = "prisma-client-js" provider = "prisma-client-js"
} }
@@ -33,7 +32,6 @@ model Doctor {
updatedAt DateTime @updatedAt updatedAt DateTime @updatedAt
} }
model Department { model Department {
id Int @id @default(autoincrement()) id Int @id @default(autoincrement())
departmentId String @unique departmentId String @unique
@@ -88,7 +86,6 @@ model DoctorTiming {
updatedAt DateTime @updatedAt updatedAt DateTime @updatedAt
} }
model Blog { model Blog {
id Int @id @default(autoincrement()) id Int @id @default(autoincrement())
title String title String
@@ -141,11 +138,11 @@ model Appointment {
message String? message String?
date DateTime date DateTime
doctorId Int doctorId String
departmentId Int departmentId String
doctor Doctor @relation(fields: [doctorId], references: [id]) doctor Doctor @relation(fields: [doctorId], references: [doctorId])
department Department @relation(fields: [departmentId], references: [id]) department Department @relation(fields: [departmentId], references: [departmentId])
createdAt DateTime @default(now()) createdAt DateTime @default(now())
updatedAt DateTime @updatedAt updatedAt DateTime @updatedAt

View File

@@ -0,0 +1,224 @@
import prisma from "../prisma/client.js";
//CREATE APPOINTMENT
export const createAppointment = async (req, res) => {
try {
const {name, mobileNumber, email, message, date, doctorId, departmentId} =
req.body;
if (!name || !mobileNumber || !doctorId || !departmentId || !date) {
return res.status(400).json({
success: false,
message: "Required fields missing",
});
}
const appointment = await prisma.appointment.create({
data: {
name,
mobileNumber,
email,
message,
date: new Date(date),
doctorId,
departmentId,
},
include: {
doctor: true,
department: true,
},
});
res.status(201).json({
success: true,
message: "Appointment booked successfully",
data: appointment,
});
} catch (error) {
console.error(error);
res.status(500).json({
success: false,
message: "Failed to create appointment",
});
}
};
// GET ALL APPOINTMENTS
export const getAppointments = async (req, res) => {
try {
const appointments = await prisma.appointment.findMany({
include: {
doctor: true,
department: true,
},
orderBy: {
createdAt: "desc",
},
});
res.status(200).json({
success: true,
data: appointments,
});
} catch (error) {
console.error(error);
res.status(500).json({
success: false,
message: "Failed to fetch appointments",
});
}
};
// GET SINGLE APPOINTMENT
export const getAppointment = async (req, res) => {
try {
const {id} = req.params;
const appointment = await prisma.appointment.findUnique({
where: {
id: Number(id),
},
include: {
doctor: true,
department: true,
},
});
if (!appointment) {
return res.status(404).json({
success: false,
message: "Appointment not found",
});
}
res.status(200).json({
success: true,
data: appointment,
});
} catch (error) {
console.error(error);
res.status(500).json({
success: false,
message: "Failed to fetch appointment",
});
}
};
// GET APPOINTMENTS BY DOCTOR
export const getAppointmentsByDoctor = async (req, res) => {
try {
const {doctorId} = req.params;
const appointments = await prisma.appointment.findMany({
where: {
doctorId,
},
include: {
doctor: true,
department: true,
},
orderBy: {
date: "asc",
},
});
res.status(200).json({
success: true,
data: appointments,
});
} catch (error) {
console.error(error);
res.status(500).json({
success: false,
message: "Failed to fetch doctor appointments",
});
}
};
// GET APPOINTMENTS BY DEPARTMENT
export const getAppointmentsByDepartment = async (req, res) => {
try {
const {departmentId} = req.params;
const appointments = await prisma.appointment.findMany({
where: {
departmentId,
},
include: {
doctor: true,
department: true,
},
});
res.status(200).json({
success: true,
data: appointments,
});
} catch (error) {
console.error(error);
res.status(500).json({
success: false,
message: "Failed to fetch department appointments",
});
}
};
// UPDATE APPOINTMENT
export const updateAppointment = async (req, res) => {
try {
const {id} = req.params;
const appointment = await prisma.appointment.update({
where: {
id: Number(id),
},
data: req.body,
include: {
doctor: true,
department: true,
},
});
res.status(200).json({
success: true,
message: "Appointment updated successfully",
data: appointment,
});
} catch (error) {
console.error(error);
res.status(500).json({
success: false,
message: "Failed to update appointment",
});
}
};
//DELETE APPOINTMENT
export const deleteAppointment = async (req, res) => {
try {
const {id} = req.params;
await prisma.appointment.delete({
where: {
id: Number(id),
},
});
res.status(200).json({
success: true,
message: "Appointment deleted successfully",
});
} catch (error) {
console.error(error);
res.status(500).json({
success: false,
message: "Failed to delete appointment",
});
}
};

View File

@@ -0,0 +1,23 @@
import express from "express";
import {
createAppointment,
getAppointments,
getAppointment,
updateAppointment,
deleteAppointment,
} from "../controllers/appointment.controller.js";
import jwtAuthMiddleware from "../middleware/auth.js";
const router = express.Router();
/* PUBLIC */
router.get("/getall", getAppointments);
router.post("/", createAppointment);
router.get("/:id", getAppointment);
router.patch("/:id", updateAppointment);
router.delete("/:id", jwtAuthMiddleware, deleteAppointment);
export default router;