route.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { getInvoices, createInvoice } from "@/lib/db/store";
  2. export async function GET(req: Request) {
  3. const { searchParams } = new URL(req.url);
  4. const status = searchParams.get("status") || undefined;
  5. const customerId = searchParams.get("customerId") || undefined;
  6. const invoiceList = await getInvoices({ status, customerId });
  7. return Response.json({
  8. data: invoiceList,
  9. total: invoiceList.length,
  10. summary: {
  11. totalAmount: invoiceList.reduce(
  12. (sum, i) => sum + parseFloat(i.amount as string),
  13. 0,
  14. ),
  15. byStatus: {
  16. draft: invoiceList.filter((i) => i.status === "draft").length,
  17. sent: invoiceList.filter((i) => i.status === "sent").length,
  18. paid: invoiceList.filter((i) => i.status === "paid").length,
  19. overdue: invoiceList.filter((i) => i.status === "overdue").length,
  20. },
  21. },
  22. });
  23. }
  24. export async function POST(req: Request) {
  25. const body = await req.json();
  26. const invoice = await createInvoice({
  27. customerId: body.customerId,
  28. dueDate: body.dueDate,
  29. items: body.items,
  30. });
  31. if (!invoice) {
  32. return Response.json({ error: "Customer not found" }, { status: 400 });
  33. }
  34. return Response.json(invoice, { status: 201 });
  35. }