forked from DistinctCodes/AssetsUp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreporting.service.ts
More file actions
86 lines (75 loc) · 2.24 KB
/
reporting.service.ts
File metadata and controls
86 lines (75 loc) · 2.24 KB
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
import { Injectable, NotFoundException } from '@nestjs/common';
import * as PDFDocument from 'pdfkit';
import * as Papa from 'papaparse';
import { Readable } from 'stream';
@Injectable()
export class ReportingService {
private async getReportData(jurisdiction: string): Promise<any[]> {
const mockData = {
USA: [
{ id: 'TX-123', amount: 5000, currency: 'USD', status: 'approved' },
{ id: 'NY-456', amount: 12000, currency: 'USD', status: 'pending' },
],
EU: [
{ id: 'DE-789', amount: 8000, currency: 'EUR', status: 'approved' },
{ id: 'FR-101', amount: 7500, currency: 'EUR', status: 'declined' },
],
};
const data = mockData[jurisdiction.toUpperCase()];
if (!data) {
throw new NotFoundException(
`No data found for jurisdiction: ${jurisdiction}`,
);
}
return data;
}
async generateReport(
jurisdiction: string,
format: 'csv' | 'pdf',
): Promise<Readable> {
const data = await this.getReportData(jurisdiction);
if (format === 'csv') {
return this.generateCsv(data);
} else {
return this.generatePdf(data, jurisdiction);
}
}
private generateCsv(data: any[]): Readable {
const csvString = Papa.unparse(data);
const stream = new Readable();
stream.push(csvString);
stream.push(null);
return stream;
}
private generatePdf(data: any[], jurisdiction: string): Readable {
const doc = new PDFDocument({ margin: 50 });
doc.fontSize(20).text(`Regulatory Report: ${jurisdiction.toUpperCase()}`, {
align: 'center',
});
doc.moveDown();
const tableTop = 150;
const itemX = 50;
const amountX = 250;
const currencyX = 350;
const statusX = 450;
doc
.fontSize(12)
.text('Transaction ID', itemX, tableTop)
.text('Amount', amountX, tableTop)
.text('Currency', currencyX, tableTop)
.text('Status', statusX, tableTop);
let i = 0;
for (const item of data) {
const y = tableTop + 25 + i * 25;
doc
.fontSize(10)
.text(item.id, itemX, y)
.text(item.amount.toString(), amountX, y)
.text(item.currency, currencyX, y)
.text(item.status, statusX, y);
i++;
}
doc.end();
return doc;
}
}