Files
ita-ito-horaires/scripts/sync-holidays.ts
T
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

48 lines
1.3 KiB
TypeScript

/**
* Synchronises the public-holiday cache from the command line.
*
* npm run holidays:sync
*
* The nightly job does this on its own; this exists for the first run after a
* deployment, and for checking the API is reachable from a new host without
* having to wait until three in the morning to find out.
*/
import 'dotenv/config';
import { prisma } from '../lib/db';
import { syncHolidays } from '../lib/holidays/sync';
async function main(): Promise<void> {
const result = await syncHolidays();
if (!result.ok) {
console.error(`Échec : ${result.error}`);
console.error('Le cache local est intact.');
process.exitCode = 1;
return;
}
console.log(
`Fenêtre ${result.from} → ${result.to} : ` +
`${result.created} ajouté(s), ${result.updated} corrigé(s), ${result.removed} retiré(s).`,
);
const upcoming = await prisma.publicHoliday.findMany({ orderBy: { date: 'asc' }, take: 6 });
for (const row of upcoming) {
console.log(
` ${row.date.toISOString().slice(0, 10)} ${row.nameFr.padEnd(24)} ${row.nameEn.padEnd(24)} ` +
`${row.isAutoClosed ? 'fermé' : 'ouvert'}`,
);
}
}
main()
.catch((error: unknown) => {
console.error(error);
process.exitCode = 1;
})
.finally(() => {
void prisma.$disconnect();
});