File size: 2,407 Bytes
e218aaf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { NextRequest, NextResponse } from 'next/server';
import { db } from '@/lib/db';
import { getCookieToken, getSessionUser } from '@/lib/auth';
import { logActivity } from '@/lib/activity';

function escapeCsvField(value: unknown): string {
  const str = value === null || value === undefined ? '' : String(value);
  if (str.includes(',') || str.includes('"') || str.includes('\n')) {
    return `"${str.replace(/"/g, '""')}"`;
  }
  return str;
}

export async function GET(request: NextRequest) {
  try {
    const token = getCookieToken(request);
    if (!token) {
      return NextResponse.json({ error: 'Not authenticated' }, { status: 401 });
    }
    const user = await getSessionUser(token);
    if (!user) {
      return NextResponse.json({ error: 'Not authenticated' }, { status: 401 });
    }

    const invoices = await db.invoice.findMany({
      where: { userId: user.id },
      orderBy: { createdAt: 'desc' },
    });

    const headers = [
      'ID',
      'Filename',
      'Vendor',
      'Invoice Number',
      'Invoice Date',
      'Due Date',
      'Subtotal',
      'VAT Amount',
      'Total',
      'Currency',
      'Status',
      'Duplicate',
      'Confidence',
      'Created At',
    ];

    const rows = invoices.map((inv) =>
      [
        inv.id,
        inv.filename ?? '',
        inv.vendor ?? '',
        inv.invNumber ?? '',
        inv.invDate ?? '',
        inv.dueDate ?? '',
        inv.amount ?? '',
        inv.vatAmount ?? '',
        inv.total ?? '',
        inv.currency,
        inv.status,
        inv.isDuplicate ? 'Yes' : 'No',
        inv.confidence !== null ? (inv.confidence * 100).toFixed(1) + '%' : '',
        inv.createdAt.toISOString(),
      ].map(escapeCsvField).join(',')
    );

    const csvContent = [
      headers.map(escapeCsvField).join(','),
      ...rows,
    ].join('\n');

    const timestamp = new Date().toISOString().split('T')[0];

    logActivity(user.id, 'export', 'CSV exported', 'Exported all invoices as CSV');

    return new NextResponse(csvContent, {
      status: 200,
      headers: {
        'Content-Type': 'text/csv; charset=utf-8',
        'Content-Disposition': `attachment; filename="invoices-${timestamp}.csv"`,
      },
    });
  } catch (error) {
    console.error('[EXPORT_CSV_ERROR]', error);
    return NextResponse.json(
      { error: 'Internal server error' },
      { status: 500 }
    );
  }
}