feat: export částečných receptů
This commit is contained in:
1 parent
5e76c404fb
commit
8377ce2205
16 files changed
+446
-147
No files matched your search
@@ -53,9 +53,11 @@ type StoredRecipe = StoredMealTemplate & { ingredients: RecipeIngredient[] };
|
||||
|
||||
/**
|
||||
* Šablony hotových jídel zanikly — opakovaně jedené jídlo se nabízí z historie.
|
||||
* Poznají se podle toho, že nemají suroviny; recept bez surovin uložit nejde.
|
||||
* Poznají se podle toho, že pole surovin vůbec nemají. Recept ho má vždy,
|
||||
* u neúplného receptu prázdné.
|
||||
*/
|
||||
const isRecipe = (stored: StoredMealTemplate): stored is StoredRecipe => !!stored.ingredients?.length;
|
||||
const isRecipe = (stored: StoredMealTemplate): stored is StoredRecipe =>
|
||||
Array.isArray(stored.ingredients) && stored.kind !== 'HOTOVE';
|
||||
|
||||
/**
|
||||
* Odstraní z receptu pole, která patřila jen šablonám hotových jídel, a zaniklý chod.
|
||||
@@ -310,13 +312,15 @@ export const saveMealTemplate = async (login: string, input: unknown): Promise<M
|
||||
if (!name) {
|
||||
throw new BadRequestError("Nebyl předán název receptu");
|
||||
}
|
||||
if (!Array.isArray(value.ingredients) || !value.ingredients.length) {
|
||||
throw new BadRequestError("Recept musí mít alespoň jednu surovinu");
|
||||
// Recept jde uložit i neúplný, bez surovin a postupu, a doplnit později
|
||||
const rawIngredients = value.ingredients ?? [];
|
||||
if (!Array.isArray(rawIngredients)) {
|
||||
throw new BadRequestError("Suroviny receptu musí být seznam");
|
||||
}
|
||||
if (value.ingredients.length > MAX_INGREDIENTS) {
|
||||
if (rawIngredients.length > MAX_INGREDIENTS) {
|
||||
throw new BadRequestError(`Recept může mít nejvýše ${MAX_INGREDIENTS} surovin`);
|
||||
}
|
||||
const ingredients = value.ingredients.map(normalizeIngredient);
|
||||
const ingredients = rawIngredients.map(normalizeIngredient);
|
||||
const servings = normalizeNumber(value.servings, 'servings');
|
||||
const cookedWeight = normalizeNumber(value.cookedWeight, 'cookedWeight') || undefined;
|
||||
const prepMinutes = normalizeNumber(value.prepMinutes, 'prepMinutes') || undefined;
|
||||
|
||||
@@ -184,9 +184,16 @@ describe('recepty', () => {
|
||||
expect((await getDay(LOGIN, DATE)).entries).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('odmítne recept bez názvu, bez surovin a neexistující id', async () => {
|
||||
it('uloží neúplný recept bez surovin a postupu', async () => {
|
||||
const [template] = await saveMealTemplate(LOGIN, { name: 'Babiččiny buchty', prepMinutes: 90 });
|
||||
|
||||
expect(template).toMatchObject({ name: 'Babiččiny buchty', ingredients: [], prepMinutes: 90 });
|
||||
expect(template).not.toHaveProperty('calories');
|
||||
expect(await getMealTemplates(LOGIN)).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('odmítne recept bez názvu a neexistující id', async () => {
|
||||
await expect(saveMealTemplate(LOGIN, { ...rizoto, name: ' ' })).rejects.toThrow(/název receptu/);
|
||||
await expect(saveMealTemplate(LOGIN, { ...rizoto, ingredients: [] })).rejects.toThrow(/surovinu/);
|
||||
await expect(applyMealTemplate(LOGIN, 'neexistuje', DATE)).rejects.toThrow(/nebyl nalezen/);
|
||||
await expect(deleteMealTemplate(LOGIN, 'neexistuje')).rejects.toThrow(/nebyl nalezen/);
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user