Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion backend/src/invoices/invoices.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ export class InvoicesController {
@CurrentUser() user: User,
@Query("page") page?: string,
@Query("limit") limit?: string,
@Query("search") search?: string,
@Query("status") status?: string,
): Promise<Invoice[]> {
const p = page ? parseInt(page, 10) : 1;
const l = limit ? parseInt(limit, 10) : 20;
const result = await this.prisma.runWithMerchantScope(user.merchantId, () =>
this.invoicesService.findAll(user.merchantId, p, l),
this.invoicesService.findAll(user.merchantId, p, l, search, status),
);
return result.items;
}
Expand Down
24 changes: 22 additions & 2 deletions backend/src/invoices/invoices.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ export class InvoicesService implements OnModuleInit {
merchantId: string,
page = 1,
limit = 20,
search?: string,
status?: string,
): Promise<{
items: Invoice[];
total: number;
Expand All @@ -59,14 +61,32 @@ export class InvoicesService implements OnModuleInit {
hasMore: boolean;
}> {
const skip = (page - 1) * limit;

// Build where clause from filters
const where: Record<string, unknown> = { merchantId };

if (search && search.trim().length > 0) {
const term = search.trim();
where["OR"] = [
{ invoiceNumber: { contains: term, mode: "insensitive" } },
{ clientName: { contains: term, mode: "insensitive" } },
{ clientEmail: { contains: term, mode: "insensitive" } },
{ memo: { contains: term, mode: "insensitive" } },
];
}

if (status && status.trim().length > 0) {
where["status"] = status;
}

const [items, total] = await Promise.all([
this.prisma.invoice.findMany({
where: { merchantId },
where,
skip,
take: limit,
orderBy: { createdAt: "desc" },
}),
this.prisma.invoice.count({ where: { merchantId } }),
this.prisma.invoice.count({ where }),
]);

const normalizedItems = items.map((inv) => this.normalizeInvoice(inv));
Expand Down
Loading
Loading