48 lines
1.4 KiB
TypeScript
48 lines
1.4 KiB
TypeScript
"use server";
|
|
|
|
import { prisma } from "@/lib/db/prisma";
|
|
import { getActingStaff } from "@/lib/staff/get-acting-staff";
|
|
import { setWorkingHours } from "@/services/staff/staff.service";
|
|
import type { WorkingHoursDayInput } from "@/services/staff/staff.service";
|
|
|
|
export interface WorkingHoursConflictItem {
|
|
id: string;
|
|
customerName: string;
|
|
serviceNames: string;
|
|
startAt: string;
|
|
}
|
|
|
|
export interface WorkingHoursActionResult {
|
|
success: boolean;
|
|
error?: string;
|
|
conflicts?: WorkingHoursConflictItem[];
|
|
}
|
|
|
|
export async function saveWorkingHoursAction(
|
|
days: WorkingHoursDayInput[],
|
|
acknowledgeConflicts = false,
|
|
): Promise<WorkingHoursActionResult> {
|
|
const staff = await getActingStaff();
|
|
if (!staff) return { success: false, error: "You must be signed in as staff." };
|
|
|
|
const result = await setWorkingHours(prisma, {
|
|
staffId: staff.id,
|
|
businessTimezone: staff.business.timezone,
|
|
days,
|
|
acknowledgeConflicts,
|
|
});
|
|
if (!result.success) {
|
|
return {
|
|
success: false,
|
|
error: result.validation.issues[0]?.message ?? "Couldn't save working hours.",
|
|
conflicts: result.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(),
|
|
})),
|
|
};
|
|
}
|
|
return { success: true };
|
|
}
|