route.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { getExpenses, createExpense } 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 category = searchParams.get("category") || undefined;
  6. const expenseList = await getExpenses({ status, category });
  7. return Response.json({
  8. data: expenseList,
  9. total: expenseList.length,
  10. summary: {
  11. totalAmount: expenseList.reduce(
  12. (sum, e) => sum + parseFloat(e.amount as string),
  13. 0,
  14. ),
  15. byStatus: {
  16. pending: expenseList.filter((e) => e.status === "pending").length,
  17. approved: expenseList.filter((e) => e.status === "approved").length,
  18. rejected: expenseList.filter((e) => e.status === "rejected").length,
  19. },
  20. },
  21. });
  22. }
  23. export async function POST(req: Request) {
  24. const body = await req.json();
  25. const expense = await createExpense({
  26. vendor: body.vendor,
  27. category: body.category,
  28. amount: body.amount,
  29. date: body.date,
  30. description: body.description,
  31. });
  32. return Response.json(expense, { status: 201 });
  33. }