Files
vliaudatandClaude Opus 5 830e595393 feat: sync Geneva public holidays and let the shop choose which close it
The rolling twelve-month window is fetched from openholidaysapi.org each
night at 03:00 local, and can be triggered from the page, from
POST /api/admin/holidays/sync, or from `npm run holidays:sync` for the
first run after a deployment.

The calendar is fetched twice, once per language, and the two answers
joined on the entry id. Holiday names are proper nouns with established
English forms — "Jeûne genevois" is not something a translation model
should be improvising, and this costs one extra HTTP call.

Two properties are load-bearing and tested against a real database.
The sync is idempotent: running it twice leaves exactly what running it
once did, verified live as well as against a mock. And it never touches
`isAutoClosed` on an existing row — that is the shop's decision, not the
API's, and a nightly job quietly reopening a day the owner had closed
would be invisible until someone found the door locked.

When the API is down the local cache is left untouched and the failure
is recorded with its timestamp, so the page can say how stale the
calendar is rather than showing nothing. Retries widen the gap between
attempts; the nightly job can afford to wait, the shop cannot afford a
stale calendar for a day.

node-cron runs inside the application process rather than an external
cron hitting an endpoint: one container, one shop, no second instance to
coordinate with, and no trigger endpoint to protect and document. The
reasoning is recorded next to the schedule.

Verified against the live API: nine Geneva holidays, both languages,
including the cantonal Restauration de la République.

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

72 lines
2.4 KiB
TypeScript

/**
* Background jobs.
*
* node-cron inside the application process, rather than an external cron
* hitting an HTTP endpoint. The choice is deliberate and worth recording: this
* runs as a single container for a single shop, so there is no second instance
* to coordinate with, and an endpoint that triggers work is an endpoint that
* has to be protected, rate-limited and documented. The trade-off is that the
* jobs die with the process — acceptable for work that is idempotent and runs
* nightly, and the UI shows the last successful sync either way.
*/
export async function register(): Promise<void> {
// Runs in the edge runtime and during the build too; neither wants a cron.
if (process.env.NEXT_RUNTIME !== 'nodejs') {
return;
}
const [{ default: cron }, { syncHolidays }, { prisma }, { loadSettings }] = await Promise.all([
import('node-cron'),
import('@/lib/holidays/sync'),
import('@/lib/db'),
import('@/lib/schedule/context'),
]);
const settings = await loadSettings().catch(() => null);
const timezone = settings?.timezone ?? 'Europe/Zurich';
// 03:00 local: the rolling twelve-month holiday window.
cron.schedule(
'0 3 * * *',
() => {
void syncHolidays()
.then((result) => {
if (result.ok) {
console.info(
`[cron] fériés synchronisés : ${result.created} ajoutés, ${result.updated} corrigés, ${result.removed} retirés`,
);
} else {
// Not fatal: the local cache stands and the UI shows how stale it is.
console.warn(`[cron] synchronisation des fériés en échec : ${result.error}`);
}
})
.catch((error: unknown) => {
console.error('[cron] synchronisation des fériés interrompue', error);
});
},
{ timezone },
);
// 03:15 local: firmware logs are diagnostics, not data.
cron.schedule(
'15 3 * * *',
() => {
const cutoff = new Date(Date.now() - 30 * 86_400_000);
void prisma.deviceLog
.deleteMany({ where: { createdAt: { lt: cutoff } } })
.then(({ count }) => {
if (count > 0) {
console.info(`[cron] ${count} entrées de journal appareil purgées`);
}
})
.catch((error: unknown) => {
console.error('[cron] purge des journaux interrompue', error);
});
},
{ timezone },
);
console.info(`[cron] tâches planifiées (fuseau ${timezone})`);
}