Files
ita-ito-horaires/app/admin/vacances/actions.ts
T
vliaudatandClaude Opus 5 c430205bf2 feat: add the closure periods page
Create a period with a start, an end and a label, see the twelve months
ahead at a glance, delete one. The label is required and trimmed: it
goes straight onto the shop window.

Overlapping periods are refused, and the message names the one they
collide with. Two rows covering the same day would both be "in effect"
with no way to say which, and silently merging them would lose whichever
label the owner meant. Periods that merely touch — one ending the 10th,
the next starting the 11th — are fine.

The year view exists because a list of date ranges is precise and hard
to picture, while "have I left a gap in August?" is the question people
actually ask. It renders on the server; it only changes when the data
does.

A vacation period stays a source of truth and is never expanded into
exception rows, so a one-off exception placed inside one still wins and
nothing is overwritten. The page says so rather than leaving it to be
discovered.

The first version of the calendar built the months but never applied the
periods to them, so no closure would ever have shown. Caught before
commit.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012cSY9pVhZmJUKNN7wf1Myd
2026-09-20 20:10:58 +02:00

119 lines
3.0 KiB
TypeScript

'use server';
import { revalidatePath } from 'next/cache';
import { diffOf, recordAudit } from '@/lib/audit';
import { requireAdmin } from '@/lib/auth/actions';
import { prisma } from '@/lib/db';
import type { CivilDate } from '@/lib/schedule/civil';
import { countDays, findOverlap, validatePeriod } from '@/lib/schedule/vacations';
export type ActionResult = { ok: true } | { ok: false; error: string };
export async function createVacation(input: {
startDate: CivilDate;
endDate: CivilDate;
labelFr: string;
}): Promise<ActionResult> {
const gate = await requireAdmin();
if (!gate.ok) {
return { ok: false, error: gate.error };
}
const labelFr = input.labelFr.trim();
const problems = validatePeriod({ ...input, labelFr });
if (problems.length > 0) {
return { ok: false, error: problems.join(' ') };
}
// Two periods covering the same day would both be in effect with no way to
// say which; refuse rather than merge silently.
const existing = await prisma.vacationPeriod.findMany();
const clash = findOverlap(
{ startDate: input.startDate, endDate: input.endDate },
existing.map((period) => ({
id: period.id,
labelFr: period.labelFr,
startDate: asCivil(period.startDate),
endDate: asCivil(period.endDate),
})),
);
if (clash) {
return {
ok: false,
error: `Cette période chevauche « ${clash.labelFr} » (${clash.startDate} au ${clash.endDate}).`,
};
}
const created = await prisma.vacationPeriod.create({
data: {
startDate: asDate(input.startDate),
endDate: asDate(input.endDate),
labelFr,
createdBy: gate.value.email,
},
});
await recordAudit({
userEmail: gate.value.email,
action: 'vacation.create',
entity: 'VacationPeriod',
entityId: created.id,
diff: diffOf(null, {
libellé: labelFr,
du: input.startDate,
au: input.endDate,
jours: countDays(input),
}),
});
revalidateAll();
return { ok: true };
}
export async function deleteVacation(id: string): Promise<ActionResult> {
const gate = await requireAdmin();
if (!gate.ok) {
return { ok: false, error: gate.error };
}
const existing = await prisma.vacationPeriod.findUnique({ where: { id } });
if (!existing) {
return { ok: false, error: 'Cette période a déjà été supprimée.' };
}
await prisma.vacationPeriod.delete({ where: { id } });
await recordAudit({
userEmail: gate.value.email,
action: 'vacation.delete',
entity: 'VacationPeriod',
entityId: id,
diff: diffOf(
{
libellé: existing.labelFr,
du: asCivil(existing.startDate),
au: asCivil(existing.endDate),
},
null,
),
});
revalidateAll();
return { ok: true };
}
function asCivil(value: Date): CivilDate {
return value.toISOString().slice(0, 10);
}
function asDate(value: CivilDate): Date {
return new Date(`${value}T00:00:00.000Z`);
}
function revalidateAll(): void {
revalidatePath('/admin');
revalidatePath('/admin/vacances');
revalidatePath('/admin/exceptions');
}