1 | import { NextRequest, NextResponse } from 'next/server';
|
---|
2 | import prisma from 'src/lib/prisma';
|
---|
3 | import { authenticateRequest } from 'src/lib/auth-middleware';
|
---|
4 |
|
---|
5 | type InvoiceStatus = 'processing' | 'paid' | 'pending' | 'overdue' | 'draft';
|
---|
6 | type CurrencyTotals = { EUR: number; USD: number };
|
---|
7 | type Results = Record<InvoiceStatus | 'total', CurrencyTotals>;
|
---|
8 |
|
---|
9 | export async function GET(request: NextRequest) {
|
---|
10 | try {
|
---|
11 | // Authenticate the request
|
---|
12 | const authResult = await authenticateRequest(request);
|
---|
13 |
|
---|
14 | if (!authResult || authResult instanceof NextResponse) {
|
---|
15 | return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
---|
16 | }
|
---|
17 |
|
---|
18 | const { userId, tenantId } = authResult;
|
---|
19 |
|
---|
20 | const searchParams = request.nextUrl.searchParams;
|
---|
21 | const startDate = searchParams.get('startDate')
|
---|
22 | ? new Date(searchParams.get('issueDate')!)
|
---|
23 | : null;
|
---|
24 |
|
---|
25 | if (!startDate) {
|
---|
26 | return NextResponse.json({ error: 'Start date is required' }, { status: 400 });
|
---|
27 | }
|
---|
28 |
|
---|
29 | // Calculate totals using raw SQL
|
---|
30 | const totals = await prisma.$queryRaw`
|
---|
31 | SELECT
|
---|
32 | status,
|
---|
33 | currency,
|
---|
34 | SUM("total_amount") as total
|
---|
35 | FROM "Invoice"
|
---|
36 | WHERE "issue_date" >= ${startDate}
|
---|
37 | AND "tenant_id" = ${tenantId}
|
---|
38 | GROUP BY status, currency
|
---|
39 | `;
|
---|
40 |
|
---|
41 | // Format the response
|
---|
42 | const results: Results = {
|
---|
43 | total: { EUR: 0, USD: 0 },
|
---|
44 | processing: { EUR: 0, USD: 0 },
|
---|
45 | paid: { EUR: 0, USD: 0 },
|
---|
46 | pending: { EUR: 0, USD: 0 },
|
---|
47 | overdue: { EUR: 0, USD: 0 },
|
---|
48 | draft: { EUR: 0, USD: 0 },
|
---|
49 | };
|
---|
50 |
|
---|
51 | // Process the results - adjusted for raw SQL response format
|
---|
52 | for (const row of totals as {
|
---|
53 | status: string;
|
---|
54 | currency: 'EUR' | 'USD';
|
---|
55 | total: string | number;
|
---|
56 | }[]) {
|
---|
57 | const { status, currency, total } = row;
|
---|
58 | const statusKey = status.toLowerCase() as InvoiceStatus;
|
---|
59 | if (statusKey in results && (currency === 'EUR' || currency === 'USD')) {
|
---|
60 | const amount = Number(total) || 0;
|
---|
61 | results[statusKey][currency] = amount;
|
---|
62 | results.total[currency] += amount;
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | return NextResponse.json(results);
|
---|
67 | } catch (error) {
|
---|
68 | console.error('Error calculating totals:', error);
|
---|
69 | return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
|
---|
70 | }
|
---|
71 | }
|
---|