Dental Care
This commit is contained in:
75
app/api/appointments/[id]/edit/route.ts
Normal file
75
app/api/appointments/[id]/edit/route.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/types/prisma";
|
||||
import { auth } from "@/lib/auth-session/auth";
|
||||
import { Prisma } from "@prisma/client";
|
||||
|
||||
export async function PATCH(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
try {
|
||||
const session = await auth.api.getSession({
|
||||
headers: await import("next/headers").then((mod) => mod.headers()),
|
||||
});
|
||||
|
||||
if (!session?.user) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
// Only admin can edit appointments
|
||||
if (session.user.role !== "admin") {
|
||||
return NextResponse.json(
|
||||
{ error: "Forbidden: Admin access required" },
|
||||
{ status: 403 }
|
||||
);
|
||||
}
|
||||
|
||||
const { id } = await params;
|
||||
const body = await request.json();
|
||||
const { date, timeSlot, status, notes } = body;
|
||||
|
||||
// Validate that the appointment exists
|
||||
const existingAppointment = await prisma.appointment.findUnique({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
if (!existingAppointment) {
|
||||
return NextResponse.json(
|
||||
{ error: "Appointment not found" },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
// Build update data object
|
||||
const updateData: Prisma.AppointmentUpdateInput = {};
|
||||
|
||||
if (date !== undefined) updateData.date = new Date(date);
|
||||
if (timeSlot !== undefined) updateData.timeSlot = timeSlot;
|
||||
if (status !== undefined) updateData.status = status;
|
||||
if (notes !== undefined) updateData.notes = notes;
|
||||
|
||||
// Update the appointment
|
||||
const updatedAppointment = await prisma.appointment.update({
|
||||
where: { id },
|
||||
data: updateData,
|
||||
include: {
|
||||
patient: true,
|
||||
dentist: true,
|
||||
service: true,
|
||||
payment: true,
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
appointment: updatedAppointment,
|
||||
message: "Appointment updated successfully",
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error updating appointment:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to update appointment" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
139
app/api/appointments/[id]/route.ts
Normal file
139
app/api/appointments/[id]/route.ts
Normal file
@@ -0,0 +1,139 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/types/prisma";
|
||||
import { auth } from "@/lib/auth-session/auth";
|
||||
|
||||
export async function PATCH(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
try {
|
||||
const session = await auth.api.getSession({
|
||||
headers: request.headers,
|
||||
});
|
||||
|
||||
if (!session) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const { status, cancelReason, date, timeSlot } = body;
|
||||
const { id } = await params;
|
||||
|
||||
const appointment = await prisma.appointment.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
patient: true,
|
||||
dentist: true,
|
||||
service: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!appointment) {
|
||||
return NextResponse.json(
|
||||
{ error: "Appointment not found" },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
// Update appointment
|
||||
const updatedAppointment = await prisma.appointment.update({
|
||||
where: { id },
|
||||
data: {
|
||||
...(status && { status }),
|
||||
...(cancelReason && { cancelReason }),
|
||||
...(date && { date: new Date(date) }),
|
||||
...(timeSlot && { timeSlot }),
|
||||
},
|
||||
include: {
|
||||
patient: true,
|
||||
dentist: true,
|
||||
service: true,
|
||||
},
|
||||
});
|
||||
|
||||
// Create notifications based on action
|
||||
if (status === "cancelled") {
|
||||
await prisma.notification.create({
|
||||
data: {
|
||||
userId: appointment.patientId,
|
||||
title: "Appointment Cancelled",
|
||||
message: `Your appointment for ${appointment.service.name} on ${new Date(appointment.date).toLocaleDateString()} has been cancelled.`,
|
||||
type: "email",
|
||||
},
|
||||
});
|
||||
|
||||
await prisma.notification.create({
|
||||
data: {
|
||||
userId: appointment.dentistId,
|
||||
title: "Appointment Cancelled",
|
||||
message: `Appointment with ${appointment.patient.name} on ${new Date(appointment.date).toLocaleDateString()} has been cancelled.`,
|
||||
type: "email",
|
||||
},
|
||||
});
|
||||
} else if (status === "confirmed") {
|
||||
await prisma.notification.create({
|
||||
data: {
|
||||
userId: appointment.patientId,
|
||||
title: "Appointment Confirmed",
|
||||
message: `Your appointment for ${appointment.service.name} on ${new Date(appointment.date).toLocaleDateString()} has been confirmed.`,
|
||||
type: "email",
|
||||
},
|
||||
});
|
||||
} else if (date || timeSlot) {
|
||||
await prisma.notification.create({
|
||||
data: {
|
||||
userId: appointment.patientId,
|
||||
title: "Appointment Rescheduled",
|
||||
message: `Your appointment has been rescheduled to ${new Date(updatedAppointment.date).toLocaleDateString()} at ${updatedAppointment.timeSlot}.`,
|
||||
type: "email",
|
||||
},
|
||||
});
|
||||
|
||||
await prisma.notification.create({
|
||||
data: {
|
||||
userId: appointment.dentistId,
|
||||
title: "Appointment Rescheduled",
|
||||
message: `Appointment with ${appointment.patient.name} has been rescheduled to ${new Date(updatedAppointment.date).toLocaleDateString()} at ${updatedAppointment.timeSlot}.`,
|
||||
type: "email",
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return NextResponse.json(updatedAppointment);
|
||||
} catch (error) {
|
||||
console.error("Error updating appointment:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to update appointment" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
try {
|
||||
const session = await auth.api.getSession({
|
||||
headers: request.headers,
|
||||
});
|
||||
|
||||
if (!session) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const { id } = await params;
|
||||
|
||||
await prisma.appointment.delete({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
return NextResponse.json({ message: "Appointment deleted successfully" });
|
||||
} catch (error) {
|
||||
console.error("Error deleting appointment:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to delete appointment" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
270
app/api/appointments/book/route.ts
Normal file
270
app/api/appointments/book/route.ts
Normal file
@@ -0,0 +1,270 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/types/prisma";
|
||||
import { auth } from "@/lib/auth-session/auth";
|
||||
import { Resend } from "resend";
|
||||
import { createElement } from "react";
|
||||
import DentalInvoice from "@/components/emails/email-bookings";
|
||||
|
||||
const resend = new Resend(process.env.RESEND_API_KEY);
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const session = await auth.api.getSession({
|
||||
headers: request.headers,
|
||||
});
|
||||
|
||||
if (!session) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const { appointmentData } = body;
|
||||
|
||||
if (!appointmentData) {
|
||||
return NextResponse.json(
|
||||
{ error: "Appointment data is required" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
interface ServiceItem {
|
||||
qty: number;
|
||||
description: string;
|
||||
unitPrice: number;
|
||||
total: number;
|
||||
}
|
||||
|
||||
const { patientId, personalInfo, appointment, services, specialRequests } =
|
||||
appointmentData as {
|
||||
patientId: string;
|
||||
personalInfo: {
|
||||
firstName: string;
|
||||
lastName: string;
|
||||
email: string;
|
||||
address?: string;
|
||||
city?: string;
|
||||
contactNumber?: string;
|
||||
};
|
||||
appointment: {
|
||||
dentistId: string;
|
||||
dentistName: string;
|
||||
date: string;
|
||||
time: string;
|
||||
};
|
||||
services: ServiceItem[];
|
||||
specialRequests: string;
|
||||
};
|
||||
|
||||
// Validate required fields
|
||||
if (
|
||||
!patientId ||
|
||||
!appointment.dentistId ||
|
||||
!appointment.date ||
|
||||
!appointment.time
|
||||
) {
|
||||
return NextResponse.json(
|
||||
{ error: "Missing required appointment fields" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Check if time slot is already booked
|
||||
const existingAppointment = await prisma.appointment.findFirst({
|
||||
where: {
|
||||
dentistId: appointment.dentistId,
|
||||
date: new Date(appointment.date),
|
||||
timeSlot: appointment.time,
|
||||
status: {
|
||||
in: ["pending", "confirmed"],
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (existingAppointment) {
|
||||
return NextResponse.json(
|
||||
{ error: "This time slot is already booked" },
|
||||
{ status: 409 }
|
||||
);
|
||||
}
|
||||
|
||||
// Create appointments for each service
|
||||
const createdAppointments = [];
|
||||
|
||||
for (const service of services) {
|
||||
if (service.qty > 0 && service.description) {
|
||||
// Find the service in database
|
||||
const dbService = await prisma.service.findFirst({
|
||||
where: { name: service.description },
|
||||
});
|
||||
|
||||
if (dbService) {
|
||||
const newAppointment = await prisma.appointment.create({
|
||||
data: {
|
||||
patientId,
|
||||
dentistId: appointment.dentistId,
|
||||
serviceId: dbService.id,
|
||||
date: new Date(appointment.date),
|
||||
timeSlot: appointment.time,
|
||||
notes: specialRequests || null,
|
||||
status: "pending",
|
||||
},
|
||||
include: {
|
||||
patient: true,
|
||||
dentist: true,
|
||||
service: true,
|
||||
},
|
||||
});
|
||||
|
||||
createdAppointments.push(newAppointment);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (createdAppointments.length === 0) {
|
||||
return NextResponse.json(
|
||||
{ error: "No valid services selected" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Send confirmation email to patient with professional invoice template
|
||||
// Generate invoice number
|
||||
const invoiceNumber = `INV-${Date.now()}-${Math.random().toString(36).substring(7).toUpperCase()}`;
|
||||
const invoiceDate = new Date().toLocaleDateString("en-US", {
|
||||
year: "numeric",
|
||||
month: "long",
|
||||
day: "numeric",
|
||||
});
|
||||
const dueDate = new Date(
|
||||
Date.now() + 30 * 24 * 60 * 60 * 1000
|
||||
).toLocaleDateString("en-US", {
|
||||
year: "numeric",
|
||||
month: "long",
|
||||
day: "numeric",
|
||||
});
|
||||
|
||||
// Format appointment date and time
|
||||
const formattedAppointmentDate = new Date(
|
||||
appointment.date
|
||||
).toLocaleDateString("en-US", {
|
||||
weekday: "long",
|
||||
year: "numeric",
|
||||
month: "long",
|
||||
day: "numeric",
|
||||
});
|
||||
|
||||
// Calculate next appointment (6 months from now for regular checkup)
|
||||
const nextApptDate = new Date(appointment.date);
|
||||
nextApptDate.setMonth(nextApptDate.getMonth() + 6);
|
||||
const nextAppointmentDate = nextApptDate.toLocaleDateString("en-US", {
|
||||
year: "numeric",
|
||||
month: "long",
|
||||
day: "numeric",
|
||||
});
|
||||
|
||||
// Calculate total duration (assuming each service takes 60 minutes)
|
||||
const totalDuration = services
|
||||
.filter((s) => s.qty > 0)
|
||||
.reduce((sum, s) => sum + s.qty * 60, 0);
|
||||
|
||||
// Calculate financial totals
|
||||
const subtotal = services
|
||||
.filter((s) => s.qty > 0)
|
||||
.reduce((sum, s) => sum + s.total, 0);
|
||||
const tax = subtotal * 0.12; // 12% tax
|
||||
const totalDue = subtotal + tax;
|
||||
|
||||
// Filter services with qty > 0 for email
|
||||
const activeServices = services.filter((s) => s.qty > 0);
|
||||
|
||||
try {
|
||||
console.log("Attempting to send email to:", personalInfo.email);
|
||||
console.log(
|
||||
"From address:",
|
||||
`${process.env.EMAIL_SENDER_NAME} <${process.env.EMAIL_SENDER_ADDRESS}>`
|
||||
);
|
||||
|
||||
const emailResult = await resend.emails.send({
|
||||
from: `${process.env.EMAIL_SENDER_NAME} <${process.env.EMAIL_SENDER_ADDRESS}>`,
|
||||
to: personalInfo.email,
|
||||
subject: `Appointment Confirmation - Invoice #${invoiceNumber}`,
|
||||
react: createElement(DentalInvoice, {
|
||||
invoiceNumber,
|
||||
invoiceDate,
|
||||
dueDate,
|
||||
patientName: `${personalInfo.firstName} ${personalInfo.lastName}`,
|
||||
patientAddress: personalInfo.address || "N/A",
|
||||
patientCity: personalInfo.city || "N/A",
|
||||
patientPhone: personalInfo.contactNumber || "N/A",
|
||||
patientEmail: personalInfo.email,
|
||||
bookingId: createdAppointments[0]?.id || "PENDING",
|
||||
appointmentDate: formattedAppointmentDate,
|
||||
appointmentTime: appointment.time,
|
||||
doctorName: appointment.dentistName,
|
||||
treatmentRoom: "Room 1",
|
||||
appointmentDuration: `${totalDuration} minutes`,
|
||||
reasonForVisit:
|
||||
specialRequests ||
|
||||
services
|
||||
.filter((s) => s.qty > 0)
|
||||
.map((s) => s.description)
|
||||
.join(", "),
|
||||
pdfDownloadUrl: `${process.env.NEXT_PUBLIC_APP_URL}/patient/appointments`,
|
||||
paymentStatus: "Pending Payment",
|
||||
nextAppointmentDate,
|
||||
nextAppointmentTime: appointment.time,
|
||||
nextAppointmentPurpose: "Regular Dental Checkup & Cleaning",
|
||||
services: activeServices,
|
||||
subtotal,
|
||||
tax,
|
||||
totalDue,
|
||||
}),
|
||||
});
|
||||
|
||||
console.log("Email sent successfully:", emailResult);
|
||||
} catch (emailError) {
|
||||
console.error("Error sending email:", emailError);
|
||||
console.error(
|
||||
"Email error details:",
|
||||
JSON.stringify(emailError, null, 2)
|
||||
);
|
||||
// Don't fail the appointment creation if email fails
|
||||
}
|
||||
|
||||
// Create notification for patient
|
||||
await prisma.notification.create({
|
||||
data: {
|
||||
userId: patientId,
|
||||
title: "Appointment Booked",
|
||||
message: `Your appointment with Dr. ${appointment.dentistName} has been booked for ${new Date(appointment.date).toLocaleDateString()} at ${appointment.time}`,
|
||||
type: "email",
|
||||
},
|
||||
});
|
||||
|
||||
// Create notification for dentist
|
||||
await prisma.notification.create({
|
||||
data: {
|
||||
userId: appointment.dentistId,
|
||||
title: "New Appointment",
|
||||
message: `New appointment request from ${personalInfo.firstName} ${personalInfo.lastName} for ${new Date(appointment.date).toLocaleDateString()} at ${appointment.time}`,
|
||||
type: "email",
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: true,
|
||||
appointments: createdAppointments,
|
||||
message:
|
||||
"Appointment booked successfully! Check your email for confirmation.",
|
||||
},
|
||||
{ status: 201 }
|
||||
);
|
||||
} catch (error) {
|
||||
console.error("Error booking appointment:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to book appointment" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
165
app/api/appointments/route.ts
Normal file
165
app/api/appointments/route.ts
Normal file
@@ -0,0 +1,165 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/types/prisma";
|
||||
import { auth } from "@/lib/auth-session/auth";
|
||||
import { safeFindManyAppointments } from "@/lib/utils/appointment-helpers";
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const session = await auth.api.getSession({
|
||||
headers: request.headers,
|
||||
});
|
||||
|
||||
if (!session) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const { patientId, dentistId, serviceId, date, timeSlot, notes } = body;
|
||||
|
||||
// Validate required fields
|
||||
if (!patientId || !dentistId || !serviceId || !date || !timeSlot) {
|
||||
return NextResponse.json(
|
||||
{ error: "Missing required fields" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Check if time slot is already booked
|
||||
const existingAppointment = await prisma.appointment.findFirst({
|
||||
where: {
|
||||
dentistId,
|
||||
date: new Date(date),
|
||||
timeSlot,
|
||||
status: {
|
||||
in: ["pending", "confirmed"],
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (existingAppointment) {
|
||||
return NextResponse.json(
|
||||
{ error: "This time slot is already booked" },
|
||||
{ status: 409 }
|
||||
);
|
||||
}
|
||||
|
||||
// Create appointment
|
||||
const appointment = await prisma.appointment.create({
|
||||
data: {
|
||||
patientId,
|
||||
dentistId,
|
||||
serviceId,
|
||||
date: new Date(date),
|
||||
timeSlot,
|
||||
notes: notes || null,
|
||||
status: "pending",
|
||||
},
|
||||
include: {
|
||||
patient: true,
|
||||
dentist: true,
|
||||
service: true,
|
||||
},
|
||||
});
|
||||
|
||||
// Create notification for patient
|
||||
await prisma.notification.create({
|
||||
data: {
|
||||
userId: patientId,
|
||||
title: "Appointment Booked",
|
||||
message: `Your appointment for ${appointment.service.name} has been booked for ${new Date(date).toLocaleDateString()} at ${timeSlot}`,
|
||||
type: "email",
|
||||
},
|
||||
});
|
||||
|
||||
// Create notification for dentist
|
||||
await prisma.notification.create({
|
||||
data: {
|
||||
userId: dentistId,
|
||||
title: "New Appointment",
|
||||
message: `New appointment request from ${appointment.patient.name} for ${new Date(date).toLocaleDateString()} at ${timeSlot}`,
|
||||
type: "email",
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json(appointment, { status: 201 });
|
||||
} catch (error) {
|
||||
console.error("Error creating appointment:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to create appointment" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const session = await auth.api.getSession({
|
||||
headers: request.headers,
|
||||
});
|
||||
|
||||
if (!session) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const { searchParams } = new URL(request.url);
|
||||
const userId = searchParams.get("userId");
|
||||
const role = searchParams.get("role");
|
||||
|
||||
let appointments;
|
||||
|
||||
if (role === "patient") {
|
||||
appointments = await safeFindManyAppointments({
|
||||
take: 100, // Limit to 100 most recent appointments
|
||||
where: {
|
||||
patientId: userId || session.user.id,
|
||||
},
|
||||
include: {
|
||||
dentist: true,
|
||||
service: true,
|
||||
payment: true,
|
||||
},
|
||||
orderBy: {
|
||||
date: "desc",
|
||||
},
|
||||
});
|
||||
} else if (role === "dentist") {
|
||||
appointments = await safeFindManyAppointments({
|
||||
take: 100, // Limit to 100 most recent appointments
|
||||
where: {
|
||||
dentistId: userId || session.user.id,
|
||||
},
|
||||
include: {
|
||||
patient: true,
|
||||
service: true,
|
||||
payment: true,
|
||||
},
|
||||
orderBy: {
|
||||
date: "desc",
|
||||
},
|
||||
});
|
||||
} else {
|
||||
// Admin - get all appointments with pagination limit
|
||||
// Use safe find to filter out orphaned appointments
|
||||
appointments = await safeFindManyAppointments({
|
||||
take: 100, // Limit to 100 most recent appointments
|
||||
include: {
|
||||
patient: true,
|
||||
dentist: true,
|
||||
service: true,
|
||||
payment: true,
|
||||
},
|
||||
orderBy: {
|
||||
date: "desc",
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return NextResponse.json(appointments);
|
||||
} catch (error) {
|
||||
console.error("Error fetching appointments:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to fetch appointments" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user