6.0 KiB
6.0 KiB
21. Backend Router — Security Report Generation
meta: id: shieldai-unified-restructure-21 feature: shieldai-unified-restructure priority: P1 depends_on: [shieldai-unified-restructure-20] tags: [backend, trpc, reports, pdf, api]
objective:
- Build the tRPC router for security report generation. Port logic from
packages/report/andpackages/api/src/routes/report.routes.tsinto a unifiedreportsrouter that generates branded PDF and HTML security reports.
deliverables:
web/src/server/api/routers/reports.ts— Reports router:reports.getReports—protectedProcedurereturning user's generated reportsreports.generateReport—protectedProceduretriggering report generationreports.getReport—protectedProcedurereturning single report with download URLreports.deleteReport—protectedProceduredeleting a reportreports.getScheduledReports—protectedProcedurereturning scheduled report settingsreports.updateSchedule—protectedProcedureupdating report schedule
web/src/server/services/reports.service.ts— Core business logic:generateReport(userId, reportType, periodStart, periodEnd)— compile data and generate report:- Collect alerts, exposures, detections, changes for period
- Calculate threat score trend
- Generate HTML content
- Convert to PDF
- Save to storage and update DB
getReports(userId, filters?)— query generated reportsdeleteReport(userId, reportId)— remove file and DB recordgetScheduledReports(userId)— read schedule preferencesupdateSchedule(userId, schedule)— write schedule preferences
web/src/server/services/reports/generator.ts— Report generator:compileData(userId, periodStart, periodEnd)— aggregate data from all servicesrenderHTML(data)— generate branded HTML using template enginegeneratePDF(html)— convert HTML to PDF using Puppeteer or PDFKituploadPDF(userId, pdfBuffer, filename)— save to storage, return URL
web/src/server/services/reports/templates/— HTML templates:monthly-plus.html— Monthly report for Plus tierannual-premium.html— Annual comprehensive report for Premium tierweekly-digest.html— Weekly summary email format- Shared partials: header, footer, alert list, exposure table, trend chart
steps:
- Create
web/src/server/api/routers/reports.ts. - Define Zod schemas:
generateReportSchema:reportType: z.enum(['MONTHLY_PLUS', 'ANNUAL_PREMIUM', 'WEEKLY_DIGEST']),periodStart: z.date().optional(),periodEnd: z.date().optional()updateScheduleSchema:enabled: z.boolean(),frequency: z.enum(['weekly', 'monthly', 'quarterly']),reportType: z.enum([...])
- Implement router procedures:
- Report listing with pagination
- Report generation (async, returns job ID or report ID)
- Report retrieval with presigned download URL
- Schedule CRUD
- Create
web/src/server/services/reports.service.ts:- Port from
packages/report/src/ - Implement report lifecycle: PENDING → GENERATING → COMPLETED/FAILED
- Port from
- Create report generator:
compileData: query normalized alerts, exposures, voice analyses, spam detections, property changes for the period- Calculate trends: compare current period to previous period
renderHTML: use Handlebars or EJS template engine with ShieldAI brandinggeneratePDF: usepuppeteer(headless Chrome) for high-fidelity HTML-to-PDF, orpdfkitfor programmatic generationuploadPDF: save to local disk or S3/R2, generate presigned URL
- Create HTML templates:
- Design consistent report layout with ShieldAI colors and logo
- Include: executive summary, threat score trend, alert breakdown by service, exposure details, recommendations
- Make templates responsive for HTML email format
- Implement scheduling:
- Store schedule preferences in DB (may need new table or extend existing)
- Background job (task 22) reads schedules and triggers generation
- Wire router into
web/src/server/api/root.ts. - Write unit tests for generator functions with mocked data.
steps:
- Unit:
compileDataaggregates correct data for a time period - Unit:
renderHTMLproduces valid HTML with all sections - Unit:
generatePDFcreates readable PDF from HTML - Unit:
uploadPDFsaves file and returns accessible URL - Integration: tRPC
generateReportcreates report record and triggers generation
acceptance_criteria:
- Reports can be generated for weekly, monthly, and annual periods
- Reports include data from all 5 services with trend analysis
- PDF output is branded with ShieldAI colors and logo
- HTML email format is responsive and readable
- Report generation status is trackable (PENDING → COMPLETED)
- Generated reports are stored securely with user-scoped access
- Scheduled reports can be configured per user
- Report deletion removes both file and DB record
validation:
- Generate a test report and verify PDF output
- Open PDF and check branding, data accuracy, and readability
- Test HTML email template by rendering in browser
- Verify scheduled report configuration persists in DB
- Run
cd web && pnpm testfor reports unit tests
notes:
- Reference legacy:
packages/report/src/,packages/api/src/routes/report.routes.ts - Puppeteer adds significant dependency weight (~100MB Chromium). For serverless environments, consider:
pdfkitorjsPDFfor lighter PDF generation (less HTML fidelity)@sparticuz/chromiumfor serverless-compatible Puppeteer- Offloading PDF generation to a background worker with full Chromium
- Report generation can be slow. Make the tRPC procedure return immediately with a report ID, and update status asynchronously.
- For Premium tier, offer annual comprehensive reports. For Plus, monthly. For Basic, only on-demand.
- Include actionable recommendations in reports (e.g., "Enable two-factor authentication", "Review exposed passwords").
- Consider adding a "share with advisor" feature that generates a shareable (but limited) report link.