feat(orders): month/year filter on both order lists + fix received-orders search

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
BOHA
2026-06-09 13:22:39 +02:00
parent 2b7b480144
commit 3b48bd0523
8 changed files with 186 additions and 5 deletions

View File

@@ -108,10 +108,13 @@ interface ListOrdersParams {
order: "asc" | "desc";
status?: string;
customer_id?: number;
search?: string;
month?: number;
year?: number;
}
export async function listOrders(params: ListOrdersParams) {
const { page, limit, skip, order } = params;
const { page, limit, skip, order, search, month, year } = params;
const sortField = ORDER_ALLOWED_SORT_FIELDS.includes(params.sort)
? params.sort
: "id";
@@ -119,13 +122,26 @@ export async function listOrders(params: ListOrdersParams) {
const where: Record<string, unknown> = {};
if (params.status) where.status = params.status;
if (params.customer_id) where.customer_id = params.customer_id;
if (search) {
where.OR = [
{ order_number: { contains: search } },
{ customer_order_number: { contains: search } },
{ customers: { name: { contains: search } } },
];
}
if (month && year) {
const from = new Date(year, month - 1, 1);
const to = new Date(year, month, 1);
where.created_at = { gte: from, lt: to };
}
const [orders, total] = await Promise.all([
prisma.orders.findMany({
where,
skip,
take: limit,
orderBy: { [sortField]: order },
// Tiebreak on id so same-second created_at rows sort deterministically.
orderBy: [{ [sortField]: order }, { id: order }],
include: {
customers: { select: { id: true, name: true } },
order_items: { orderBy: { position: "asc" } },