feat: edit the reference week from the admin

The first administration page, built for the actual use case: a phone
held in one hand behind the counter. One card per day, native time
inputs so the platform keyboard does the work, and the consequences
shown before the save rather than after — the footer names which days
are about to change, and the button stays disabled until something
actually has.

"Duplicate onto the other open days" leaves closed days closed. Someone
copying Tuesday's hours means "the days I open, I open like this", not
"open seven days a week".

Validation runs in the browser for the feedback and again in the action
before the write: the client is a convenience, not a guarantee, and this
is the schedule the shop window shows. A day being closed drops its
leftover slots rather than failing on them.

A save that changes nothing writes nothing — no rows, no audit entry,
and so no needless panel redraw. Reordering slots does not count as a
change. The audit diff stores one readable line per day in French, so
the log can be read without cross-referencing the schema.

The editing helpers are pure and tested, and the write path is tested
against a real database including the read-only refusal.

Test files now run sequentially: the integration files share one
database and each truncates it, so parallel files raced. The suite takes
six seconds; giving every file its own database would buy nothing.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012cSY9pVhZmJUKNN7wf1Myd
This commit is contained in:
2026-09-20 18:57:46 +02:00
co-authored by Claude Opus 5
parent 5c0b8119a4
commit eb23eb650b
8 changed files with 829 additions and 0 deletions
+94
View File
@@ -0,0 +1,94 @@
'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 { Slot } from '@/lib/schedule/types';
import {
changedDays,
DAY_NAMES_FR,
hasErrors,
normaliseWeek,
toDraft,
validateWeek,
type WeekDraft,
} from '@/lib/schedule/week';
export type SaveWeekResult =
| { ok: true; changed: number[] }
| { ok: false; error: string; dayErrors?: Record<number, string[]> };
/**
* Persists the reference week.
*
* Validation runs again here even though the form already checked: the client
* is a convenience, not a guarantee, and this is the schedule the shop window
* shows.
*/
export async function saveWeek(draft: WeekDraft): Promise<SaveWeekResult> {
const gate = await requireAdmin();
if (!gate.ok) {
return { ok: false, error: gate.error };
}
const week = normaliseWeek(draft);
const errors = validateWeek(week);
if (hasErrors(errors)) {
return { ok: false, error: 'Certains horaires sont invalides.', dayErrors: errors };
}
const existing = await prisma.weeklySchedule.findMany();
const before = toDraft(
existing.map((day) => ({
dayOfWeek: day.dayOfWeek,
isClosed: day.isClosed,
slots: (day.slots as Slot[] | null) ?? [],
})),
);
const changed = changedDays(before, week);
if (changed.length === 0) {
// Nothing moved: no write, no audit entry, no needless screen redraw.
return { ok: true, changed: [] };
}
await prisma.$transaction(
week.map((day) =>
prisma.weeklySchedule.upsert({
where: { dayOfWeek: day.dayOfWeek },
update: { isClosed: day.isClosed, slots: day.slots },
create: { dayOfWeek: day.dayOfWeek, isClosed: day.isClosed, slots: day.slots },
}),
),
);
await recordAudit({
userEmail: gate.value.email,
action: 'weekly_schedule.update',
entity: 'WeeklySchedule',
diff: diffOf(summary(before), summary(week)),
});
revalidatePath('/admin');
revalidatePath('/admin/horaires');
return { ok: true, changed };
}
/**
* One readable line per day for the audit trail. Storing the raw rows would
* make the diff accurate and unreadable; the point of the log is that a human
* can see what changed.
*/
function summary(week: WeekDraft): Record<string, string> {
return Object.fromEntries(
week.map((day) => [
DAY_NAMES_FR[day.dayOfWeek] ?? String(day.dayOfWeek),
day.isClosed || day.slots.length === 0
? 'Fermé'
: day.slots.map((slot) => `${slot.open}–${slot.close}`).join(' · '),
]),
);
}