Files
Wabenhain/src/lib/harvest-slug-db.ts
T
Nick SzaunigandClaude Opus 5.5 641afa1ae4
Docker Image bauen und veroeffentlichen / build-and-push (push) Successful in 2m15s
fix(harvests): allow several harvests of the same type per year
The ApiCore sync failed with "Unique constraint failed on the constraint:
harvests_year_type_key" as soon as ApiCore had two harvests of the same
honey type in one year, which is normal (several extractions per season).
Every bottling of that harvest was then skipped too.

- Drop the (year, type) unique index. The slug is now the only unique,
  public identifier: /ernten/<slug>, e.g. bluetenhonig-2026, then
  bluetenhonig-2026-07-14 for a further harvest of the same type.
- New Harvest.apicoreHarvestId as the sync key, backfilled from the old
  "apicore-<id>" slugs; the sync gives those rows a readable slug.
- Slugs are generated by one shared slugify() that normalizes Unicode
  (decomposed umlauts) and writes out umlauts. The migration repairs
  manual slugs created by the old umlaut bug ("fr-htracht-2026").
- Slug collisions are resolved by the database unique index, so parallel
  writers cannot produce duplicates; known slugs are skipped up front to
  keep the logs free of handled P2002 errors.
- Old URLs /ernten/<year>/<type> answer with 308 to the new address.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
2026-10-02 04:25:02 +02:00

15 lines
483 B
TypeScript

import { prisma } from "@/lib/prisma";
/**
* Welche der vorgeschlagenen Ernten-Slugs schon vergeben sind — fuer
* writeWithFreeSlug (lib/harvest-slug.ts). Getrennt von dort, damit jene
* Datei ohne Datenbank testbar bleibt.
*/
export async function findTakenHarvestSlugs(candidates: string[]): Promise<string[]> {
const rows = await prisma.harvest.findMany({
where: { slug: { in: candidates } },
select: { slug: true },
});
return rows.map((row) => row.slug);
}