"use server"; import { prisma } from "@/lib/db/prisma"; import { getActingStaff } from "@/lib/staff/get-acting-staff"; import { createBreak, deleteBreak, updateBreak } from "@/services/staff/staff.service"; import type { BreakInput } from "@/services/staff/staff.service"; export interface BreakConflictItem { id: string; customerName: string; serviceNames: string; startAt: string; } export interface BreakActionResult { success: boolean; error?: string; conflicts?: BreakConflictItem[]; } function mapConflicts(conflicts: { id: string; customer: { firstName: string; lastName: string }; services: { service: { name: string } }[]; startAt: Date }[]): BreakConflictItem[] { return conflicts.map((c) => ({ id: c.id, customerName: `${c.customer.firstName} ${c.customer.lastName}`, serviceNames: c.services.map((s) => s.service.name).join(" + "), startAt: c.startAt.toISOString(), })); } export async function createBreakAction(input: BreakInput, acknowledgeConflicts?: boolean): Promise { const staff = await getActingStaff(); if (!staff) return { success: false, error: "You must be signed in as staff." }; const result = await createBreak(prisma, { staffId: staff.id, businessTimezone: staff.business.timezone, input, acknowledgeConflicts }); if (!result.success) { return { success: false, error: result.validation.issues[0]?.message ?? "Couldn't create this break.", conflicts: mapConflicts(result.conflicts) }; } return { success: true }; } export async function updateBreakAction(breakId: string, input: BreakInput, acknowledgeConflicts?: boolean): Promise { const staff = await getActingStaff(); if (!staff) return { success: false, error: "You must be signed in as staff." }; const result = await updateBreak(prisma, { breakId, staffId: staff.id, businessTimezone: staff.business.timezone, input, acknowledgeConflicts }); if (!result.success) { return { success: false, error: result.validation.issues[0]?.message ?? "Couldn't update this break.", conflicts: mapConflicts(result.conflicts) }; } return { success: true }; } export async function deleteBreakAction(breakId: string): Promise { const staff = await getActingStaff(); if (!staff) return { success: false, error: "You must be signed in as staff." }; const result = await deleteBreak(prisma, { breakId, staffId: staff.id }); return result.success ? { success: true } : { success: false, error: "Couldn't delete this break." }; }