order report by date
This commit is contained in:
@@ -26,6 +26,7 @@ import { OrderCreatedEvent, OrderStatusChangedEvent } from '../events/order.even
|
||||
import { OrderMessage } from 'src/common/enums/message.enum';
|
||||
import { AdminCreateOrderDto } from '../dto/admin-create-order.dto';
|
||||
import { FoodOrderReportDto, FoodOrderReportSortBy } from '../dto/food-order-report.dto';
|
||||
import { DailyOrderReportDto, DailyOrderReportSortBy } from '../dto/daily-order-report.dto';
|
||||
import { normalizePhone } from 'src/modules/utils/phone.util';
|
||||
type OrderItemData = { food: Food; quantity: number; unitPrice: number; discount: number };
|
||||
|
||||
@@ -817,4 +818,49 @@ console.log(cart);
|
||||
totalPrice: Number(row.total_price || 0),
|
||||
}));
|
||||
}
|
||||
|
||||
async getDailyOrderReport(restId: string, dto: DailyOrderReportDto) {
|
||||
const { from, to, sortBy = DailyOrderReportSortBy.Date } = dto;
|
||||
|
||||
const params: unknown[] = [restId];
|
||||
const dateFilters: string[] = [];
|
||||
|
||||
if (from) {
|
||||
params.push(new Date(from));
|
||||
dateFilters.push(`AND o.created_at >= ?`);
|
||||
}
|
||||
|
||||
if (to) {
|
||||
params.push(new Date(to));
|
||||
dateFilters.push(`AND o.created_at <= ?`);
|
||||
}
|
||||
|
||||
const orderColumn = {
|
||||
[DailyOrderReportSortBy.Date]: `DATE_TRUNC('day', CAST(o.created_at AS timestamp))`,
|
||||
[DailyOrderReportSortBy.TotalAmount]: 'total_amount',
|
||||
[DailyOrderReportSortBy.OrderCount]: 'order_count',
|
||||
}[sortBy];
|
||||
|
||||
const result = await this.em.execute(
|
||||
`
|
||||
SELECT
|
||||
TO_CHAR(DATE_TRUNC('day', CAST(o.created_at AS timestamp)), 'YYYY-MM-DD') as "date",
|
||||
COUNT(o.id)::int as order_count,
|
||||
COALESCE(SUM(o.total), 0)::numeric as total_amount
|
||||
FROM orders o
|
||||
WHERE o.restaurant_id = ?
|
||||
${dateFilters.join('\n ')}
|
||||
AND o.status NOT IN ('pendingPayment', 'canceled')
|
||||
GROUP BY DATE_TRUNC('day', CAST(o.created_at AS timestamp))
|
||||
ORDER BY ${orderColumn} DESC
|
||||
`,
|
||||
params,
|
||||
);
|
||||
|
||||
return result.map((row: Record<string, unknown>) => ({
|
||||
date: row.date,
|
||||
orderCount: Number(row.order_count || 0),
|
||||
totalAmount: Number(row.total_amount || 0),
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user