Skip to content
Open
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
16 changes: 11 additions & 5 deletions apps/backend/lambdas/reports/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
checkProjectAccess,
fetchReportData,
generatePdf,
generateDocx,
uploadToS3,
saveReportRecord,
} from './report-service';
Expand Down Expand Up @@ -40,6 +41,11 @@ export const handler = async (event: any): Promise<APIGatewayProxyResult> => {
return json(400, { message: 'project_id must be a positive integer' });
}

const fileType = body.file_type ?? 'pdf';
if (fileType !== 'pdf' && fileType !== 'docx') {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Can we have a constant with a list of file types?

return json(400, { message: 'file_type must be "pdf" or "docx"' });
}

const reportData = await fetchReportData(projectId);
if (!reportData) {
return json(404, { message: 'Project not found' });
Expand All @@ -50,17 +56,17 @@ export const handler = async (event: any): Promise<APIGatewayProxyResult> => {
return json(403, { message: 'You do not have access to generate reports for this project' });
}

let pdfBuffer: Buffer;
let fileBuffer: Buffer;
try {
pdfBuffer = await generatePdf(reportData);
fileBuffer = fileType === 'docx' ? await generateDocx(reportData) : await generatePdf(reportData);
} catch (err) {
console.error('PDF generation error:', err);
return json(500, { message: 'Failed to generate report PDF' });
console.error('Report generation error:', err);
return json(500, { message: 'Failed to generate report' });
}

let objectUrl: string;
try {
objectUrl = await uploadToS3(pdfBuffer, projectId);
objectUrl = await uploadToS3(fileBuffer, projectId, fileType);
} catch (err) {
console.error('S3 upload error:', err);
return json(500, { message: 'Failed to upload report' });
Expand Down
19 changes: 13 additions & 6 deletions apps/backend/lambdas/reports/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,13 @@ paths:
'401':
description: Unauthorized
post:
summary: Generate a project report PDF
summary: Generate a project report (PDF or DOCX)
description: >
Generates a PDF report for the given project containing project info,
participants and roles, donations, and expenditures. Uploads the PDF to
S3 and records it in the database. Requires the caller to be a member
of the project or a global admin.
Generates a report for the given project containing project info,
participants and roles, donations, and expenditures. Supports PDF and
DOCX output via the `file_type` field (defaults to `pdf`). Uploads the
file to S3 and records it in the database. Requires the caller to be a
member of the project or a global admin.
requestBody:
required: true
content:
Expand All @@ -102,9 +103,15 @@ paths:
type: integer
description: The ID of the project to generate a report for
example: 1
file_type:
type: string
enum: [pdf, docx]
default: pdf
description: Output format: "pdf" (default) or "docx"
example: docx
responses:
'201':
description: Report generated successfully
description: Report generated and uploaded successfully
content:
application/json:
schema:
Expand Down
Loading
Loading