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 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user