Skip to content

Commit

Permalink
Merge branch 'dev' of github.com:Hello-Kitchen/Back-End into 50-add-f…
Browse files Browse the repository at this point in the history
…ood_ordered-id-when-forkds-param-is-true-in-order-route
  • Loading branch information
JulesGresset committed Oct 8, 2024
2 parents d39446c + 5979986 commit 647e3a0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 29 deletions.
60 changes: 32 additions & 28 deletions src/modules/orders/orders.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,41 +48,45 @@ export class OrdersController {
@Get(':id')
async getOneOrder(@Query('forKDS') forKDS: string, @Param('id') id: number, @Param('idRestaurant') idRestaurant: number) {
try {
let foodOrdered;
if (forKDS === 'true') {
const foodOrdered = await this.ordersService.findByIdWithParam(
foodOrdered = await this.ordersService.findByIdWithParam(
Number(idRestaurant),
Number(id),
);
const groupedFoodOrdered = foodOrdered[0].orders.reduce(
async (accPromise, itemPromise) => {
const acc = await accPromise;
const item = await itemPromise;
const name = await this.ordersService.findFoodByIdsWithParam(
Number(idRestaurant),
item.food
);

const foundItem = acc.find(
(el) =>
JSON.stringify(el.food) === JSON.stringify(item.food) &&
JSON.stringify(el.mods_ingredients) ===
JSON.stringify(item.mods_ingredients) &&
JSON.stringify(el.is_ready) === JSON.stringify(item.is_ready) &&
JSON.stringify(el.note) === JSON.stringify(item.note),
);
if (foundItem) foundItem.quantity += 1;
else acc.push({ ...item, quantity: 1, name: name.name });
return await acc;
},
Promise.resolve([]),
);
return groupedFoodOrdered;
foodOrdered = foodOrdered[0]
} else {
foodOrdered = await this.ordersService.findById(Number(idRestaurant), Number(id));
foodOrdered = foodOrdered.orders[0];
}
const order = await this.ordersService.findById(Number(idRestaurant), Number(id));
if (!order) {
if (!foodOrdered) {
throw new NotFoundException(`Order with id ${id} not found`);
}
return order.orders[0];
const groupedFoodOrdered = foodOrdered.food_ordered.reduce(
async (accPromise, itemPromise) => {
const acc = await accPromise;
const item = await itemPromise;
const name = await this.ordersService.findFoodByIdsWithParam(
Number(idRestaurant),
item.food
);

const foundItem = acc.find(
(el) =>
JSON.stringify(el.food) === JSON.stringify(item.food) &&
JSON.stringify(el.mods_ingredients) ===
JSON.stringify(item.mods_ingredients) &&
JSON.stringify(el.is_ready) === JSON.stringify(item.is_ready) &&
JSON.stringify(el.note) === JSON.stringify(item.note),
);
if (foundItem) foundItem.quantity += 1;
else acc.push({ ...item, quantity: 1, name: name.foods[0].name });
return await acc;
},
Promise.resolve([]),
);
foodOrdered.food_ordered = await groupedFoodOrdered
return foodOrdered;
} catch (error) {
throw new InternalServerErrorException(
`Error fetching order with id ${id}: ${error}`,
Expand Down
2 changes: 1 addition & 1 deletion src/modules/orders/orders.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export class OrdersService extends DB {
{
$group: {
_id: "$_id",
orders: {
food_ordered: {
$push: {
food: "$orders.food_ordered.food",
details: "$orders.food_ordered.details",
Expand Down

0 comments on commit 647e3a0

Please sign in to comment.