batchCooking/apps/api/test-support/reset-db.ts
Nicolas aedeb257ce feat(recipes): relie les recettes à leur source (sourceId + externalId)
Répond au besoin identifié précédemment : la table `sources` devient
un vrai catalogue des sources implémentées, et une recette importée
pourra être reliée à l'item source dont elle provient.

- Source.key (unique) — même convention que Diet.key/Unit.key/
  TechStep.key. Le catalogue est désormais synchronisé depuis le
  registre d'adaptateurs (recipe-source-registry.ts) via
  syncRecipeSources() (nouveau apps/api/src/db/recipe-source-sync.ts),
  plutôt que maintenu à la main comme DIETS/UNITS — reste vide tant
  qu'aucun adaptateur concret n'est enregistré.
- Recipe.externalId (nullable) — l'identifiant de la recette côté
  source. Contrainte @@unique([sourceId, externalId]) : empêche
  d'importer deux fois la même recette (les recettes manuelles, aux
  deux colonnes nulles, ne sont jamais en conflit entre elles).
- findImportedExternalIds(prisma, sourceKey, externalIds) — le
  pendant DB de markAlreadyImported (recipe-source-adapter.ts),
  ferme la boucle commencée dans la PR précédente pour distinguer les
  recettes déjà intégrées lors du browse.
- syncRecipeSources() appelé après seedReferenceData() dans
  prisma/seed.ts et test-support/reset-db.ts.

Toujours pas de route HTTP ni de champ sourceId/externalId exposé
dans createRecipeSchema — la sauvegarde effective d'une recette
importée reste pour une PR ultérieure.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-20 07:43:51 +02:00

26 lines
1.3 KiB
TypeScript

import { prisma } from "../src/db/prisma.js";
import { syncRecipeSources } from "../src/db/recipe-source-sync.js";
import { seedReferenceData } from "../src/db/reference-seed-data.js";
// Single TRUNCATE ... CASCADE covers FK ordering and resets identity
// sequences — used between tests/scenarios to start from a clean slate.
// Re-seeds the Diet/Category/Allergy/Unit reference data right after
// truncating it, so every test starts from the same realistic reference
// data the real app seeds (`prisma/seed.ts`) rather than empty tables —
// tests exercising dietId/allergyIds/unitId need real rows to reference.
// `syncRecipeSources` runs last, for the same reason: `sources` should
// reflect whatever adapters this test run happens to have registered
// (usually none — see recipe-source-registry.ts).
export async function resetDatabase() {
await prisma.$executeRawUnsafe(`
TRUNCATE TABLE
"user_profile_allergy", "user_preference", "allergy", "category",
"planning_item", "planning",
"recipe_ingredient", "step", "tech_step_mapping", "tech_step",
"recipe", "ingredients", "sources", "unit",
"user_profiles", "diet", "house"
RESTART IDENTITY CASCADE;
`);
await seedReferenceData(prisma);
await syncRecipeSources(prisma);
}