Two things the shop asked for, and one it will notice. **A message on an exception.** A closure or a late opening can now carry a reason, and it reaches the panel. It shows while the door is shut and disappears the moment the shop opens — a notice explaining a late opening is worse than useless once the door is open. It outranks a free message on purpose: "closed this afternoon, back tomorrow" is what someone standing outside needs, and "new collection in store" can wait. Say so if you would rather it were the other way. The first version keyed this off the day's `isOpen`, which means "this day has opening hours" — so a day opening at 14:00 counted as open all morning, exactly when the reason is needed. A test caught it; the rule now reads the state at this minute. **Reusable phrases.** The same handful of notices get written over and over. /admin/modeles keeps them, translated once, and offers them wherever a message is composed — exceptions, closure periods, the banner. Picking one costs no translation at all: the saved English is reused directly, where the service takes the better part of ten seconds. Exception notes and holiday labels are translated too, which they were not before. After the save rather than during it, so a slow service never costs the shop its dates. **Previewing the future.** The dashboard can render the panel at any moment within about a year: "what will the window say while I'm away?" is worth answering before someone is standing in front of a locked door. The whole pipeline was already a function of "now", so this costs passing a different instant. An unparseable or absurd value falls back to the present rather than confidently rendering nonsense. Checked against a three-week holiday, which surfaced something worth knowing: the automatic "opens on…" line stays empty, because the resolver's search is bounded to fourteen days. During a long closure the message is the only thing that tells customers when the shop is back — which is a good reason for this feature to exist. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012cSY9pVhZmJUKNN7wf1Myd
155 lines
4.4 KiB
TypeScript
155 lines
4.4 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';
|
|
import { translateToEnglish } from '@/lib/translation/service';
|
|
|
|
export type ActionResult = { ok: true } | { ok: false; error: string };
|
|
|
|
export async function createVacation(input: {
|
|
startDate: CivilDate;
|
|
endDate: CivilDate;
|
|
labelFr: string;
|
|
// Returns the new id so the caller can translate it without having to ask
|
|
// which row was "the last one", which would be a race.
|
|
}): Promise<{ ok: true; id: string } | { ok: false; error: string }> {
|
|
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}).`,
|
|
};
|
|
}
|
|
|
|
// A phrase saved as a template is already translated; reusing it is instant
|
|
// where the service takes seconds. The label reaches the panel during the
|
|
// closure, so it needs its English.
|
|
const fromTemplate = await prisma.messageTemplate.findUnique({ where: { textFr: labelFr } });
|
|
|
|
const created = await prisma.vacationPeriod.create({
|
|
data: {
|
|
startDate: asDate(input.startDate),
|
|
endDate: asDate(input.endDate),
|
|
labelFr,
|
|
labelEn: fromTemplate?.textEn ?? null,
|
|
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, id: created.id };
|
|
}
|
|
|
|
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');
|
|
}
|
|
|
|
/**
|
|
* Translates a period's label, if it still needs one.
|
|
*
|
|
* Separate from the creation, so a slow translation service never costs the
|
|
* shop its holiday dates. A failure leaves the French alone on screen.
|
|
*/
|
|
export async function translateVacationLabel(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 || existing.labelEn) {
|
|
return { ok: true };
|
|
}
|
|
|
|
const outcome = await translateToEnglish(existing.labelFr);
|
|
if (!outcome.ok) {
|
|
return { ok: false, error: outcome.error };
|
|
}
|
|
|
|
await prisma.vacationPeriod.update({ where: { id }, data: { labelEn: outcome.text } });
|
|
revalidateAll();
|
|
return { ok: true };
|
|
}
|