Skip to content

Commit

Permalink
Merge pull request #46 from Hello-Kitchen/43-orderstatus-route-integr…
Browse files Browse the repository at this point in the history
…ation

feat: add new route '/status/:idFoodOrdered' for updated status of th…
  • Loading branch information
Xantass authored Oct 8, 2024
2 parents d1d233f + 469cbff commit 8b1361b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/modules/orders/orders.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,19 @@ export class OrdersController {
);
}
}

@Put('status/:id')
async ChangeStatusFoodOrdered(@Param('idRestaurant') idRestaurant: number, @Param('id') id: number) {
try {
const result = await this.ordersService.markFoodOrderedReady(Number(idRestaurant), Number(id));
if (result.modifiedCount === 0) {
throw new NotFoundException(`Order with id ${id} not found`);
}
return { message: `FoodOrder with id ${id} updated successfully` };
} catch (error) {
throw new InternalServerErrorException(
`Error updating food_ordered with id ${id}: ${error}`,
);
}
}
}
35 changes: 35 additions & 0 deletions src/modules/orders/orders.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,15 @@ export class OrdersService extends DB {
);

body['id'] = id.sequence_value;
for (const food of body['food_ordered']) {
const id = await db.collection<Counter>('counter').findOneAndUpdate(
{ _id: 'foodOrderId' },
{ $inc: { sequence_value: 1 } },
{ returnDocument: ReturnDocument.AFTER }
);

food.id = id.sequence_value;
}
return db
.collection('restaurant')
.updateOne({ id: idRestaurant }, { $addToSet: { orders: body } });
Expand Down Expand Up @@ -236,4 +245,30 @@ export class OrdersService extends DB {
.collection<Restaurant>('restaurant')
.updateOne({ id: idRestaurant }, { $pull: { orders: { id: id } } });
}

async markFoodOrderedReady(idRestaurant: number, idFoodOrdered: number) {
const db = this.getDbConnection();
const res = await db.collection('restaurant').findOne({ id: idRestaurant }, { projection: { _id: 0, orders: 1 } });
let value: boolean;

for(const order of res.orders) {
for(const food of order.food_ordered) {
if (food.id === idFoodOrdered)
value = !food.is_ready;
}
}

return await db.collection('restaurant').findOneAndUpdate(
{
id: idRestaurant,
'orders.food_ordered.id': idFoodOrdered
},
{
$set: { 'orders.$[].food_ordered.$[food].is_ready': value }
},
{
arrayFilters: [{ 'food.id': idFoodOrdered }]
}
);
}
}
2 changes: 2 additions & 0 deletions src/shared/interfaces/restaurant.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { ObjectId } from 'mongodb';
import { Ingredient } from 'src/modules/ingredient/interfaces/ingredient.interface';
import { FoodCategory } from 'src/modules/food_category/interfaces/food_category.interface';
import { Food } from 'src/modules/food/interfaces/food.interface';
import { Order } from './order.interface';

export interface Restaurant {
_id: ObjectId;
id: number;
ingredients: Ingredient[];
food_category: FoodCategory[];
foods: Food[];
orders: Order[];
}

0 comments on commit 8b1361b

Please sign in to comment.