Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"json2csv": "^6.0.0-alpha.2",
"multer": "^2.0.2",
"nestjs-i18n": "^10.5.1",
"papaparse": "^5.5.3",
"passport": "^0.7.0",
"passport-jwt": "^4.0.1",
"pdfkit": "^0.17.2",
Expand All @@ -55,6 +56,7 @@
"@types/jest": "^29.5.2",
"@types/json2csv": "^5.0.7",
"@types/node": "^20.3.1",
"@types/papaparse": "^5.3.16",
"@types/passport-jwt": "^4.0.1",
"@types/pdfkit": "^0.17.3",
"@types/pg": "^8.10.0",
Expand Down
2 changes: 2 additions & 0 deletions backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { UsersModule } from './users/users.module';
import { User } from './users/entities/user.entity';
import { SearchModule } from './search/search.module';
import { AuthModule } from './auth/auth.module';
import { ReportingModule } from './reporting/reporting.module';

@Module({
imports: [
Expand Down Expand Up @@ -46,6 +47,7 @@ import { AuthModule } from './auth/auth.module';
UsersModule,
SearchModule,
AuthModule,
ReportingModule,
],
controllers: [AppController, NotificationsController],
providers: [AppService, NotificationsService],
Expand Down
7 changes: 7 additions & 0 deletions backend/src/reporting/dto/report-query.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { IsIn, IsOptional } from 'class-validator';

export class ReportQueryDto {
@IsOptional()
@IsIn(['csv', 'pdf'])
format: 'csv' | 'pdf' = 'csv';
}
32 changes: 32 additions & 0 deletions backend/src/reporting/reporting.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Controller, Get, Param, Query, Res } from '@nestjs/common';
import { Response } from 'express';
import { ReportingService } from './reporting.service';
import { ReportQueryDto } from './dto/report-query.dto';

@Controller('reports')
export class ReportingController {
constructor(private readonly reportingService: ReportingService) {}

@Get(':jurisdiction')
async getReport(
@Param('jurisdiction') jurisdiction: string,
@Query() query: ReportQueryDto,
@Res() res: Response,
) {
const { format } = query;
const stream = await this.reportingService.generateReport(
jurisdiction,
format,
);
const filename = `report-${jurisdiction.toLowerCase()}-${Date.now()}.${format}`;

res.setHeader('Content-Disposition', `attachment; filename=${filename}`);
if (format === 'pdf') {
res.setHeader('Content-Type', 'application/pdf');
} else {
res.setHeader('Content-Type', 'text/csv');
}

stream.pipe(res);
}
}
9 changes: 9 additions & 0 deletions backend/src/reporting/reporting.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { ReportingController } from './reporting.controller';
import { ReportingService } from './reporting.service';

@Module({
controllers: [ReportingController],
providers: [ReportingService],
})
export class ReportingModule {}
86 changes: 86 additions & 0 deletions backend/src/reporting/reporting.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}
}