94 lines
3.1 KiB
TypeScript
94 lines
3.1 KiB
TypeScript
import type { Metadata } from "next";
|
|
import { notFound, redirect } from "next/navigation";
|
|
import { Card } from "@/components/ui/card";
|
|
import { TopNav } from "@/components/navigation/top-nav";
|
|
import { prisma } from "@/lib/db/prisma";
|
|
import { getActingStaff } from "@/lib/staff/get-acting-staff";
|
|
import { getAppointmentById } from "@/services/scheduling/appointment.service";
|
|
import { assertOwnAppointment, StaffPermissionDeniedError } from "@/features/staff/authorization";
|
|
import { rescheduleStaffAppointmentAction } from "@/features/staff/actions/appointment-actions";
|
|
import { BarberDateTimePicker } from "@/features/booking/components/barber-date-time-picker";
|
|
|
|
export const metadata: Metadata = { title: "Reschedule Appointment" };
|
|
export const dynamic = "force-dynamic";
|
|
|
|
const NON_RESCHEDULABLE = new Set(["COMPLETED", "CANCELLED", "NO_SHOW", "IN_PROGRESS"]);
|
|
|
|
export default async function StaffRescheduleAppointmentPage({
|
|
params,
|
|
}: {
|
|
params: Promise<{ id: string }>;
|
|
}) {
|
|
const staff = await getActingStaff();
|
|
if (!staff) redirect("/home");
|
|
|
|
const { id } = await params;
|
|
const appointment = await getAppointmentById(prisma, id);
|
|
if (!appointment) notFound();
|
|
|
|
try {
|
|
assertOwnAppointment(appointment, staff);
|
|
} catch (error) {
|
|
if (error instanceof StaffPermissionDeniedError) notFound();
|
|
throw error;
|
|
}
|
|
|
|
if (NON_RESCHEDULABLE.has(appointment.status)) {
|
|
redirect(`/staff/appointments/${id}`);
|
|
}
|
|
|
|
async function handleContinue(selection: { staffId?: string; date: string; startAt: string }) {
|
|
"use server";
|
|
const result = await rescheduleStaffAppointmentAction({
|
|
appointmentId: id,
|
|
staffId: selection.staffId,
|
|
startAt: selection.startAt,
|
|
});
|
|
if (result.success) {
|
|
redirect(`/staff/appointments/${id}`);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
return (
|
|
<div className="pb-6">
|
|
<TopNav title="Reschedule" onBack sticky className="bg-stone" />
|
|
|
|
<Card className="mx-6 mt-4 flex items-center gap-3 p-4">
|
|
<div className="flex-1">
|
|
<p className="text-sm font-bold">
|
|
{appointment.services.map((s) => s.service.name).join(" + ")} for{" "}
|
|
{appointment.customer.firstName} {appointment.customer.lastName}
|
|
</p>
|
|
<p className="text-muted text-xs">
|
|
Currently:{" "}
|
|
{appointment.startAt.toLocaleDateString("en-GB", {
|
|
weekday: "short",
|
|
day: "numeric",
|
|
month: "short",
|
|
timeZone: staff.business.timezone,
|
|
})}{" "}
|
|
·{" "}
|
|
{appointment.startAt.toLocaleTimeString("en-GB", {
|
|
hour: "2-digit",
|
|
minute: "2-digit",
|
|
timeZone: staff.business.timezone,
|
|
})}
|
|
</p>
|
|
</div>
|
|
</Card>
|
|
|
|
<div className="mt-4">
|
|
<BarberDateTimePicker
|
|
businessId={staff.businessId}
|
|
businessTimezone={staff.business.timezone}
|
|
serviceIds={appointment.services.map((s) => s.serviceId)}
|
|
initialStaffId={appointment.staffId}
|
|
onContinue={handleContinue}
|
|
continueLabel="Confirm Reschedule"
|
|
/>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|