order report by food
This commit is contained in:
@@ -25,6 +25,7 @@ import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { OrderCreatedEvent, OrderStatusChangedEvent } from '../events/order.events';
|
||||
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 { normalizePhone } from 'src/modules/utils/phone.util';
|
||||
type OrderItemData = { food: Food; quantity: number; unitPrice: number; discount: number };
|
||||
|
||||
@@ -766,4 +767,54 @@ console.log(cart);
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
async getFoodOrderReport(restId: string, dto: FoodOrderReportDto) {
|
||||
const { from, to, sortBy = FoodOrderReportSortBy.Count } = 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 = sortBy === FoodOrderReportSortBy.Price ? 'f.price' : 'count';
|
||||
|
||||
const result = await this.em.execute(
|
||||
`
|
||||
SELECT
|
||||
f.id as food_id,
|
||||
f.title as food_title,
|
||||
f.price as food_price,
|
||||
COALESCE(SUM(oi.quantity), 0)::int as count,
|
||||
COALESCE(SUM(oi.total_price), 0)::numeric as total_price
|
||||
FROM order_items oi
|
||||
INNER JOIN orders o ON oi.order_id = o.id
|
||||
INNER JOIN foods f ON oi.food_id = f.id
|
||||
WHERE o.restaurant_id = ?
|
||||
${dateFilters.join('\n ')}
|
||||
AND o.status NOT IN ('pendingPayment', 'canceled')
|
||||
GROUP BY f.id, f.title, f.price
|
||||
HAVING SUM(oi.quantity) > 0
|
||||
ORDER BY ${orderColumn} DESC
|
||||
`,
|
||||
params,
|
||||
);
|
||||
|
||||
return result.map((row: Record<string, unknown>) => ({
|
||||
food: {
|
||||
id: row.food_id,
|
||||
title: row.food_title,
|
||||
},
|
||||
count: Number(row.count || 0),
|
||||
price: Number(row.food_price || 0),
|
||||
totalPrice: Number(row.total_price || 0),
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user