diff --git a/src/modules/orders/controllers/orders.controller.ts b/src/modules/orders/controllers/orders.controller.ts index b69f374..c5a7df8 100644 --- a/src/modules/orders/controllers/orders.controller.ts +++ b/src/modules/orders/controllers/orders.controller.ts @@ -96,45 +96,10 @@ export class OrdersController { return this.ordersService.changeOrderStatus(orderId, restId, status, 'admin', dto?.desc); } - // @UseGuards(AdminAuthGuard) - // @Patch('admin/orders/:orderId/confirm') - // @ApiOperation({ summary: 'Accept an order' }) - // @ApiParam({ name: 'orderId', description: 'Order ID' }) - // acceptOrder(@Param('orderId') orderId: string, @RestId() restId: string) { - // return this.ordersService.confirmOrder(orderId, restId); - // } - - // @UseGuards(AdminAuthGuard) - // @Patch('admin/orders/:id/prepare') - // @ApiOperation({ summary: 'Prepare an order By Admin' }) - // @ApiParam({ name: 'id', description: 'Order ID' }) - // prepareOrder(@Param('id') id: string, @RestId() restId: string) { - // return this.ordersService.prepareOrder(id, restId); - // } - - // @UseGuards(AdminAuthGuard) - // @Patch('admin/orders/:orderId/reject') - // @ApiOperation({ summary: 'Reject an order' }) - // @ApiParam({ name: 'orderId', description: 'Order ID' }) - // rejectOrder(@Param('orderId') orderId: string, @RestId() restId: string) { - // return this.ordersService.rejectOrder(orderId, restId); - // } - - // @UseGuards(AdminAuthGuard) - // @ApiBearerAuth() - // @Patch('admin/orders/:orderId/ready') - // @ApiOperation({ summary: 'Ready for pickup or delivery ' }) - // @ApiParam({ name: 'orderId', description: 'Order ID' }) - // readyForPickup(@Param('orderId') orderId: string, @RestId() restId: string) { - // return this.ordersService.readyForDelivery(orderId, restId); - // } - - // @UseGuards(AdminAuthGuard) - // @ApiBearerAuth() - // @Patch('admin/orders/:orderId/delivered') - // @ApiOperation({ summary: 'Mark an order as delivered' }) - // @ApiParam({ name: 'orderId', description: 'Order ID' }) - // markAsDelivered(@Param('orderId') orderId: string, @RestId() restId: string) { - // return this.ordersService.markAsDelivered(orderId, restId); - // } + @UseGuards(AdminAuthGuard) + @ApiOperation({ summary: 'Get Stats for report page' }) + @Get('admin/orders/stats') + findStats( @RestId() restId: string) { + return this.ordersService.getStats(restId); + } } diff --git a/src/modules/orders/providers/orders.service.ts b/src/modules/orders/providers/orders.service.ts index fa9c3a9..e7e4351 100644 --- a/src/modules/orders/providers/orders.service.ts +++ b/src/modules/orders/providers/orders.service.ts @@ -458,4 +458,61 @@ export class OrdersService { ); } } + + + async getStats(restId: string) { + return this.em.transactional(async em => { + // 1. Total orders count (excluding pending and canceled) + const totalOrders = await em.count(Order, { + restaurant: { id: restId }, + status: { + $in: [ + OrderStatus.PAID, + OrderStatus.CONFIRMED, + OrderStatus.PREPARING, + OrderStatus.DELIVERED_TO_WAITER, + OrderStatus.DELIVERED_TO_RECEPTIONIST, + OrderStatus.SHIPPED, + OrderStatus.COMPLETED, + ], + }, + }); + + // 2. Active foods count + const activeFoods = await em.count(Food, { + restaurant: { id: restId }, + isActive: true, + }); + + // 3. Total clients count (distinct users with orders for this restaurant) + const clientsResult = await em.execute( + ` + SELECT COUNT(DISTINCT o.user_id) as count + FROM orders o + WHERE o.restaurant_id = ? + `, + [restId], + ); + const totalClients = Number(clientsResult[0]?.count || 0); + + // 4. Total revenue (sum of paid payments for orders of this restaurant) + const revenueResult = await em.execute( + ` + SELECT COALESCE(SUM(p.amount), 0)::numeric as total + FROM payments p + INNER JOIN orders o ON p.order_id = o.id + WHERE o.restaurant_id = ? AND p.status = 'paid' + `, + [restId], + ); + const totalRevenue = Number(revenueResult[0]?.total || 0); + + return { + totalOrders, + activeFoods, + totalClients, + totalRevenue, + }; + }); + } }