From 5094f368c68a40dcd87cae51c4c06e46e1c4b420 Mon Sep 17 00:00:00 2001 From: Xantas Date: Sun, 22 Jun 2025 12:49:22 +0200 Subject: [PATCH 1/3] fix: use the new format of ingredient into route '/food/:id?useCase=POS' --- src/modules/food/DTO/food.dto.ts | 12 +++++++++++- src/modules/food/food.controller.ts | 4 ++-- src/modules/food/food.service.ts | 8 ++++++-- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/modules/food/DTO/food.dto.ts b/src/modules/food/DTO/food.dto.ts index 4e6279a..9fd3bf8 100644 --- a/src/modules/food/DTO/food.dto.ts +++ b/src/modules/food/DTO/food.dto.ts @@ -6,6 +6,16 @@ import { IsOptional, } from 'class-validator'; +export class IngredientDto { + @IsNumber() + @IsNotEmpty() + id_ingredient: number; + + @IsNumber() + @IsNotEmpty() + quantity: number; +} + export class FoodDto { @IsString() @IsNotEmpty() @@ -22,7 +32,7 @@ export class FoodDto { @IsOptional() @IsArray() @IsNumber({}, { each: true }) - ingredients?: number[]; + ingredients?: IngredientDto[]; @IsOptional() @IsArray() diff --git a/src/modules/food/food.controller.ts b/src/modules/food/food.controller.ts index 28cdf77..182f6ee 100644 --- a/src/modules/food/food.controller.ts +++ b/src/modules/food/food.controller.ts @@ -98,7 +98,7 @@ export class FoodController { if (!food) { throw new NotFoundException(); } - return food[0].food; // Assuming food.foods is of type any[] + return food[0].food; } else { const food = await this.foodService.findById( Number(idRestaurant), @@ -107,7 +107,7 @@ export class FoodController { if (!food) { throw new NotFoundException(); } - return food.foods[0]; // Assuming food.foods is of type any[] + return food.foods[0]; } } catch (error) { if (error instanceof HttpException) { diff --git a/src/modules/food/food.service.ts b/src/modules/food/food.service.ts index 393615f..578a046 100644 --- a/src/modules/food/food.service.ts +++ b/src/modules/food/food.service.ts @@ -102,9 +102,13 @@ export class FoodService extends DB { ingredients: { $map: { input: '$foods.ingredients', - as: 'ingredientId', + as: 'ingredient', in: { - $arrayElemAt: ['$ingredients.name', '$$ingredientId'], + id_ingredient: '$$ingredient.id_ingredient', + quantity: '$$ingredient.quantity', + name: { + $arrayElemAt: ['$ingredients.name', '$$ingredient.id_ingredient'] // Assuming 'ingredients' is the array of ingredients + } }, }, }, From 2fb21444370653a6a9fdcf16e4a6815d237a888f Mon Sep 17 00:00:00 2001 From: Xantas Date: Sun, 22 Jun 2025 12:52:33 +0200 Subject: [PATCH 2/3] linter: fix error --- src/modules/food/food.controller.ts | 4 ++-- src/modules/food/food.service.ts | 7 +++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/modules/food/food.controller.ts b/src/modules/food/food.controller.ts index 182f6ee..ec72141 100644 --- a/src/modules/food/food.controller.ts +++ b/src/modules/food/food.controller.ts @@ -98,7 +98,7 @@ export class FoodController { if (!food) { throw new NotFoundException(); } - return food[0].food; + return food[0].food; } else { const food = await this.foodService.findById( Number(idRestaurant), @@ -107,7 +107,7 @@ export class FoodController { if (!food) { throw new NotFoundException(); } - return food.foods[0]; + return food.foods[0]; } } catch (error) { if (error instanceof HttpException) { diff --git a/src/modules/food/food.service.ts b/src/modules/food/food.service.ts index 578a046..9fff751 100644 --- a/src/modules/food/food.service.ts +++ b/src/modules/food/food.service.ts @@ -107,8 +107,11 @@ export class FoodService extends DB { id_ingredient: '$$ingredient.id_ingredient', quantity: '$$ingredient.quantity', name: { - $arrayElemAt: ['$ingredients.name', '$$ingredient.id_ingredient'] // Assuming 'ingredients' is the array of ingredients - } + $arrayElemAt: [ + '$ingredients.name', + '$$ingredient.id_ingredient', + ], // Assuming 'ingredients' is the array of ingredients + }, }, }, }, From 6c8dbbdadbd8bf76919119eb75b8e1b28d5799aa Mon Sep 17 00:00:00 2001 From: Xantas Date: Sun, 22 Jun 2025 12:56:30 +0200 Subject: [PATCH 3/3] test: fix test create with the new format of the field ingredient --- src/modules/food/food.controller.spec.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/modules/food/food.controller.spec.ts b/src/modules/food/food.controller.spec.ts index 243cb89..c99806c 100644 --- a/src/modules/food/food.controller.spec.ts +++ b/src/modules/food/food.controller.spec.ts @@ -117,7 +117,11 @@ describe('FoodController', () => { name: 'New Pizza', price: 12, id_category: 1, - ingredients: [1, 2, 3], + ingredients: [ + { id_ingredient: 1, quantity: 1 }, + { id_ingredient: 2, quantity: 2 }, + { id_ingredient: 3, quantity: 3 }, + ], details: [1, 2], }; @@ -155,7 +159,11 @@ describe('FoodController', () => { name: 'Updated Pizza', price: 15, id_category: 1, - ingredients: [1, 2, 3], + ingredients: [ + { id_ingredient: 1, quantity: 1 }, + { id_ingredient: 2, quantity: 2 }, + { id_ingredient: 3, quantity: 3 }, + ], details: [1, 2], };