|
| 1 | +import { Loc } from '@core/decorators/location.decorator'; |
| 2 | +import { Rest } from '@core/decorators/restaurant.decorator'; |
| 3 | +import { OwnerAuthGuard } from '@core/guards/auth.guard'; |
| 4 | +import { OwnerGuard } from '@core/guards/owner.guard'; |
| 5 | +import { PermAct, PermOwner } from '@core/services/role.service'; |
| 6 | +import { OrderProduct } from '@db/entities/core/order-product.entity'; |
| 7 | +import { Order, OrderStatus } from '@db/entities/core/order.entity'; |
| 8 | +import { Location } from '@db/entities/owner/location.entity'; |
| 9 | +import { ProductVariant } from '@db/entities/owner/product-variant.entity'; |
| 10 | +import { Product } from '@db/entities/owner/product.entity'; |
| 11 | +import { getChartData } from '@lib/helpers/utils.helper'; |
| 12 | +import { Permissions } from '@lib/rbac'; |
| 13 | +import AppDataSource from '@lib/typeorm/datasource.typeorm'; |
| 14 | +import { uuid } from '@lib/uid/uuid.library'; |
| 15 | +import { Controller, Get, Res, UseGuards } from '@nestjs/common'; |
| 16 | + |
| 17 | +@Controller() |
| 18 | +@UseGuards(OwnerAuthGuard(), OwnerGuard) |
| 19 | +export class DashboardController { |
| 20 | + @Get('orders/total') |
| 21 | + @Permissions(`${PermOwner.Dashboard}@${PermAct.R}`) |
| 22 | + async orderTotal(@Rest() rest, @Loc() loc: Location, @Res() response) { |
| 23 | + const query = AppDataSource.createQueryBuilder(Order, 't1').where({ restaurant_id: rest.id }); |
| 24 | + |
| 25 | + if (loc) { |
| 26 | + query.andWhere({ location_id: loc.id }); |
| 27 | + } |
| 28 | + |
| 29 | + const data = await query.search().dateRange().getCount(); |
| 30 | + return response.data({ id: uuid(), data }); |
| 31 | + } |
| 32 | + |
| 33 | + @Get('sales/total') |
| 34 | + @Permissions(`${PermOwner.Dashboard}@${PermAct.R}`) |
| 35 | + async salesTotal(@Rest() rest, @Loc() loc: Location, @Res() response) { |
| 36 | + const query = AppDataSource.createQueryBuilder(Order, 't1').where({ restaurant_id: rest.id }); |
| 37 | + |
| 38 | + if (loc) { |
| 39 | + query.andWhere({ location_id: loc.id }); |
| 40 | + } |
| 41 | + |
| 42 | + query.selectWithAlias([ |
| 43 | + '_UUID() as id', |
| 44 | + '_IFNULL(SUM(t1.gross_total),0) AS total', |
| 45 | + '_IFNULL(SUM(t1.net_total),0) AS net_total', |
| 46 | + ]); |
| 47 | + |
| 48 | + const data = await query.search().dateRange().getRawOne(); |
| 49 | + return response.data({ id: uuid(), data }); |
| 50 | + } |
| 51 | + |
| 52 | + @Get('sales/chart') |
| 53 | + @Permissions(`${PermOwner.Dashboard}@${PermAct.R}`) |
| 54 | + async salesChart(@Rest() rest, @Loc() loc: Location, @Res() response) { |
| 55 | + const query = AppDataSource.createQueryBuilder(Order, 't1') |
| 56 | + .where({ restaurant_id: rest.id }) |
| 57 | + .andWhere('t1.status = :status', { status: OrderStatus.Completed }) |
| 58 | + .groupBy('t1.created_at') |
| 59 | + .selectWithAlias(['_IFNULL(SUM(t1.net_total),0) AS net_total', 'created_at']); |
| 60 | + |
| 61 | + if (loc) { |
| 62 | + query.andWhere({ location_id: loc.id }); |
| 63 | + } |
| 64 | + const orders = await query.dateRange().getRawMany(); |
| 65 | + |
| 66 | + const { start, end } = AppDataSource.createQueryBuilder(Order, 't1').dateRange().getParameters(); |
| 67 | + const results = await getChartData({ start, end }, orders, 'net_total'); |
| 68 | + |
| 69 | + return response.data(results); |
| 70 | + } |
| 71 | + |
| 72 | + @Get('top/menus') |
| 73 | + @Permissions(`${PermOwner.Dashboard}@${PermAct.R}`) |
| 74 | + async topMenus(@Rest() rest, @Loc() loc: Location, @Res() response) { |
| 75 | + const query = AppDataSource.createQueryBuilder(OrderProduct, 't1') |
| 76 | + .leftJoin(Order, 't2', 't1.order_id = t2.id') |
| 77 | + .leftJoin(ProductVariant, 't3', 't1.product_variant_id = t3.id') |
| 78 | + .leftJoin(Product, 't4', 't3.product_id = t4.id') |
| 79 | + .where('t2.restaurant_id = :restId', { restId: rest.id }) |
| 80 | + .andWhere('t2.status NOT IN (:status)', { status: [OrderStatus.Cancelled] }); |
| 81 | + |
| 82 | + if (loc) { |
| 83 | + query.andWhere('t2.location_id = :locId', { locId: loc.id }); |
| 84 | + } |
| 85 | + |
| 86 | + query |
| 87 | + .selectWithAlias(['_(t1.product_variant_id) AS id', 't4.name', 't4.sku', '_COUNT(t1.product_variant_id) AS count']) |
| 88 | + .groupBy('t1.product_variant_id') |
| 89 | + .orderBy('count', 'DESC') |
| 90 | + .limit(5); |
| 91 | + |
| 92 | + const results = await query.dateRange().getRawMany(); |
| 93 | + |
| 94 | + return response.data(results); |
| 95 | + } |
| 96 | + |
| 97 | + @Get('top/locations') |
| 98 | + @Permissions(`${PermOwner.Dashboard}@${PermAct.R}`) |
| 99 | + async topLocations(@Rest() rest, @Res() response) { |
| 100 | + const query = AppDataSource.createQueryBuilder(Order, 't1') |
| 101 | + .leftJoin(Location, 't2', 't1.location_id = t2.id') |
| 102 | + .where('t1.restaurant_id = :restId', { restId: rest.id }) |
| 103 | + .andWhere('t1.status NOT IN (:status)', { status: [OrderStatus.Cancelled] }) |
| 104 | + .selectWithAlias(['_(t2.id) AS id', 't2.name', '_COUNT(t1.id) AS count']) |
| 105 | + .groupBy('t2.id') |
| 106 | + .orderBy('count', 'DESC') |
| 107 | + .limit(5); |
| 108 | + |
| 109 | + const results = await query.dateRange().getRawMany(); |
| 110 | + |
| 111 | + return response.data(results); |
| 112 | + } |
| 113 | +} |
0 commit comments