Files
ita-ito-horaires/app/admin/exceptions/page.tsx
T
vliaudatandClaude Opus 5 5f7aa951d3 feat: explain a schedule change on the panel, and preview it in advance
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
2026-09-23 09:34:14 +02:00

117 lines
3.9 KiB
TypeScript

import { auth } from '@/lib/auth';
import { prisma } from '@/lib/db';
import { addCivilDays, toCivilDate } from '@/lib/schedule/civil';
import { loadScheduleContext, loadSettings } from '@/lib/schedule/context';
import { formatDateFr, formatHoursText } from '@/lib/schedule/format';
import { resolveCivilDay } from '@/lib/schedule/resolver';
import { loadTemplates } from '@/lib/templates';
import type { ScheduleContext } from '@/lib/schedule/types';
import ExceptionsManager, { type SelectableDay, type UpcomingDay } from './ExceptionsManager';
export const metadata = { title: 'Changements ponctuels — ITA ITO' };
/** How far ahead the list looks. Far enough for a season, short enough to read. */
const HORIZON_DAYS = 60;
export default async function ExceptionsPage() {
const [session, settings, templates] = await Promise.all([
auth(),
loadSettings(),
loadTemplates(),
]);
const now = new Date();
const context = await loadScheduleContext(now, settings);
const today = toCivilDate(now, settings.timezone);
const stored = await prisma.scheduleException.findMany({
where: { date: { gte: new Date(`${today}T00:00:00.000Z`) } },
select: { date: true },
});
const deletable = new Set(stored.map((row) => row.date.toISOString().slice(0, 10)));
const upcoming = collectUpcoming(today, context, deletable);
const days = collectSelectableDays(today, context);
return (
<main className="mx-auto max-w-3xl px-4 py-10">
<h1 className="text-2xl">Changements ponctuels</h1>
<p className="mt-2 max-w-prose text-[var(--ink-muted)]">
Une fermeture ou un horaire différent pour une date précise. Ces changements priment sur les
horaires fixes.
</p>
<ExceptionsManager
canEdit={session?.user?.role === 'admin'}
today={today}
days={days}
upcoming={upcoming}
templates={templates}
/>
</main>
);
}
/**
* Each day the quick actions can target, with the hours it would normally
* have.
*
* "Opens later" is late relative to the day's usual hours, and those differ
* from one weekday to the next — so the whole window is resolved here rather
* than asking the server again every time the date changes. Sixty small
* entries cost less than a round trip per keystroke.
*/
function collectSelectableDays(today: string, context: ScheduleContext): SelectableDay[] {
const withoutExceptions: ScheduleContext = { ...context, exceptions: [] };
return Array.from({ length: HORIZON_DAYS + 1 }, (_, offset) => {
const date = addCivilDays(today, offset);
const usual = resolveCivilDay(date, withoutExceptions);
const effective = resolveCivilDay(date, context);
return {
date,
label: formatDateFr(date),
usualSlots: usual.slots,
usualText: usual.slots.length > 0 ? formatHoursText(usual.slots) : 'Fermé',
effectiveText: effective.slots.length > 0 ? formatHoursText(effective.slots) : 'Fermé',
isException: effective.isException,
};
});
}
/**
* Every day ahead that departs from the reference week, whatever the cause.
*
* Built by resolving each day rather than by listing the exception table,
* because vacations and public holidays are never materialised as rows — so
* the table alone would quietly omit most of what is actually in effect.
*/
function collectUpcoming(
today: string,
context: ScheduleContext,
deletable: Set<string>,
): UpcomingDay[] {
const days: UpcomingDay[] = [];
for (let offset = 0; offset <= HORIZON_DAYS; offset += 1) {
const date = addCivilDays(today, offset);
const day = resolveCivilDay(date, context);
if (!day.isException) {
continue;
}
days.push({
date,
label: formatDateFr(date),
isToday: offset === 0,
isClosed: !day.isOpen,
hoursText: day.slots.length > 0 ? formatHoursText(day.slots) : 'Fermé',
kind: day.exceptionKind ?? 'MANUAL',
note: day.noteFr,
canDelete: deletable.has(date),
});
}
return days;
}