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:
@@ -45,6 +45,8 @@ interface ListIssuedOrdersParams {
|
||||
search?: string;
|
||||
status?: string;
|
||||
customer_id?: number;
|
||||
month?: number;
|
||||
year?: number;
|
||||
}
|
||||
|
||||
const VALID_TRANSITIONS: Record<string, string[]> = {
|
||||
@@ -92,8 +94,18 @@ export function computeIssuedOrderTotals(
|
||||
}
|
||||
|
||||
export async function listIssuedOrders(params: ListIssuedOrdersParams) {
|
||||
const { page, limit, skip, sort, order, search, status, customer_id } =
|
||||
params;
|
||||
const {
|
||||
page,
|
||||
limit,
|
||||
skip,
|
||||
sort,
|
||||
order,
|
||||
search,
|
||||
status,
|
||||
customer_id,
|
||||
month,
|
||||
year,
|
||||
} = params;
|
||||
const sortField = ALLOWED_SORT_FIELDS.includes(sort) ? sort : "id";
|
||||
|
||||
const where: Record<string, unknown> = {};
|
||||
@@ -106,6 +118,11 @@ export async function listIssuedOrders(params: ListIssuedOrdersParams) {
|
||||
{ customers: { company_id: { contains: search } } },
|
||||
];
|
||||
}
|
||||
if (month && year) {
|
||||
const from = new Date(year, month - 1, 1);
|
||||
const to = new Date(year, month, 1);
|
||||
where.order_date = { gte: from, lt: to };
|
||||
}
|
||||
|
||||
// Normalize the direction so an unexpected value can't reach Prisma at runtime.
|
||||
const dir: "asc" | "desc" = order === "asc" ? "asc" : "desc";
|
||||
|
||||
@@ -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" } },
|
||||
|
||||
Reference in New Issue
Block a user