Remplace l'unité texte libre de RecipeIngredient (max 20 caractères, "g"/"grammes"/"G"... jamais fiable à additionner) par une référence vers un nouveau catalogue Unit (id/key/type/toBaseFactor), même traitement que Diet/Allergy/Ingredient : GET /reference/units, seedé par reference-seed-data.ts (14 unités : gram/kilogram/milliliter/ centiliter/liter/tablespoon/teaspoon/piece/pinch/slice/clove/bunch/ sachet/sprig), sélectionnable uniquement via un <select> dans le formulaire recette (plus de saisie libre). `toBaseFactor` (combien d'unités de base — gramme pour MASS, millilitre pour VOLUME — vaut une unité) pose les bases d'une future fonctionnalité de conversion (ex. liste de courses additionnant "500g" + "0.5kg") sans construire cette fonctionnalité elle-même — les unités COUNT restent à toBaseFactor=1, non convertibles entre elles (une "pincée" n'est pas une fraction fixe d'une "gousse"). Migration : recipe_ingredient.unit → unit_id (FK), breaking change sans backfill assumé (pas de recette réelle en prod actuellement, voir commentaire de migration) — mêmes garde-fous service-side que ingredientId (404 UNIT_NOT_FOUND) et mêmes tests de couverture.
81 lines
3.1 KiB
TypeScript
81 lines
3.1 KiB
TypeScript
import type { AllergyView, DietView, IngredientView, UnitView } from "@batch-cooking/shared";
|
|
import { prisma } from "../../db/prisma.js";
|
|
|
|
/**
|
|
* All reference dietary regimes, ordered by `key` — small, static list (see
|
|
* prisma/seed.ts). `key` is a stable slug, not the display label (see
|
|
* {@link DietView}), so this is an alphabetical-by-slug order rather than a
|
|
* true French alphabetical one — close enough for a 5-item list, and the
|
|
* server has no other order to offer now that the label itself only exists
|
|
* client-side (`apps/web`'s `locales/fr/translation.json`).
|
|
*/
|
|
export async function getDiets(): Promise<DietView[]> {
|
|
return prisma.diet.findMany({ orderBy: { key: "asc" } });
|
|
}
|
|
|
|
/**
|
|
* All reference allergens, ordered by key (see {@link getDiets} for why key,
|
|
* not label). `Allergy` carries no `key` of its own — it's the selectable
|
|
* instance of a keyed `Category` (see schema.prisma) — so this resolves
|
|
* each allergen's key from its category and flattens the split away for
|
|
* callers.
|
|
*/
|
|
export async function getAllergies(): Promise<AllergyView[]> {
|
|
const allergies = await prisma.allergy.findMany({
|
|
include: { category: { select: { key: true, kind: true } } },
|
|
orderBy: { category: { key: "asc" } },
|
|
});
|
|
return allergies.map((allergy) => ({
|
|
id: allergy.id,
|
|
key: allergy.category.key,
|
|
kind: allergy.category.kind,
|
|
}));
|
|
}
|
|
|
|
/**
|
|
* All reference recipe-ingredient units, ordered by key (see {@link getDiets}
|
|
* for why) — small, static list (see `reference-seed-data.ts`'s `UNITS`).
|
|
* `toBaseFactor` comes back as a Prisma `Decimal`, converted to a plain
|
|
* `number` here the same way `recipe.service.ts` does for
|
|
* `RecipeIngredient.quantity`.
|
|
*/
|
|
export async function getUnits(): Promise<UnitView[]> {
|
|
const units = await prisma.unit.findMany({ orderBy: { key: "asc" } });
|
|
return units.map((unit) => ({
|
|
id: unit.id,
|
|
key: unit.key,
|
|
type: unit.type,
|
|
toBaseFactor: Number(unit.toBaseFactor),
|
|
}));
|
|
}
|
|
|
|
/**
|
|
* All reference ingredients, ordered by key (see {@link getDiets} for why),
|
|
* each resolved to its allergens (see `IngredientAllergy` in schema.prisma)
|
|
* and compatible diet regimes (see `IngredientDiet`) — same flattening
|
|
* approach as {@link getAllergies}. Ingredients with no linked
|
|
* allergen/diet come back with `allergens: []`/`diets: []`.
|
|
*/
|
|
export async function getIngredients(): Promise<IngredientView[]> {
|
|
const ingredients = await prisma.ingredient.findMany({
|
|
include: {
|
|
allergies: { include: { allergy: { include: { category: true } } } },
|
|
diets: { include: { diet: true } },
|
|
},
|
|
orderBy: { key: "asc" },
|
|
});
|
|
return ingredients.map((ingredient) => ({
|
|
id: ingredient.id,
|
|
key: ingredient.key,
|
|
icon: ingredient.icon,
|
|
category: ingredient.category,
|
|
subcategory: ingredient.subcategory,
|
|
reproducible: ingredient.reproducible,
|
|
allergens: ingredient.allergies.map(({ allergy }) => ({
|
|
id: allergy.id,
|
|
key: allergy.category.key,
|
|
kind: allergy.category.kind,
|
|
})),
|
|
diets: ingredient.diets.map(({ diet }) => ({ id: diet.id, key: diet.key })),
|
|
}));
|
|
}
|