Skip to content

feat(reports): PDF report generator (rebased on main, conflict-free) — supersedes #8#9

Merged
D-source1602 merged 1 commit into
mainfrom
feat/pdf-report-generator-rebased
May 29, 2026
Merged

feat(reports): PDF report generator (rebased on main, conflict-free) — supersedes #8#9
D-source1602 merged 1 commit into
mainfrom
feat/pdf-report-generator-rebased

Conversation

@kiro-agent

@kiro-agent kiro-agent Bot commented May 29, 2026

Copy link
Copy Markdown

This pull request was created by @kiro-agent on behalf of @D-source1602 👻

Comment with /kiro fix to address specific feedback or /kiro all to address everything.
Learn about Kiro autonomous agent


Summary

This is the conflict-free version of PR #8 (PDF report generator), rebased on top of main after PR #7 (realtime AQI) was merged. Same content as PR #8, just with the conflicts in backend/package.json, backend/prisma/schema.prisma, and backend/src/routes/index.ts resolved.

Action needed: merge this PR, then close PR #8 (it's now superseded).

Why this exists

PRs #7 and #8 were developed in parallel. After PR #7 merged, PR #8 had merge conflicts because both PRs touched:

  • backend/package.json — both add deps (resolved: keep all of node-cron, socket.io, pdfkit, plus types)
  • backend/prisma/schema.prisma — both append a new model at the bottom (resolved: keep both AirQualityReading and Report models)
  • backend/src/routes/index.ts — both add a router import + mount (resolved: keep both aqiRouter and reportRouter)

src/App.tsx auto-merged cleanly because PR #7 added LiveAirQualityPanel while PR #8 modified the Reports component — different sections of the same file.

Conflict resolutions

backend/package.json (both sides kept)

   "jsonwebtoken": "^9.0.2",
+  "node-cron": "^3.0.3",
+  "pdfkit": "^0.15.1",
   "pino": "^9.6.0",

backend/src/routes/index.ts (both sides kept)

import { aqiRouter } from './aqi.routes';
import { reportRouter } from './report.routes';
// ...
apiRouter.use('/aqi', aqiRouter);
apiRouter.use('/reports', reportRouter);

backend/prisma/schema.prisma (both models kept)

The AirQualityReading model from PR #7 and the Report model + ReportType / ReportFormat enums from PR #8 are now stacked at the bottom of the schema, in that order.

What this PR ships (unchanged from PR #8)

PDF report generator wired end-to-end:

  • pdfkit-based generator (~2 MB, no Chromium) with brand-matching layout, KPI grid, color-coded status table
  • Report Prisma model + ReportType / ReportFormat enums + back-relations on User and Industry
  • 4 endpoints: POST /reports/generate, GET /reports, GET /reports/:id/download, DELETE /reports/:id — all auth-protected via requireAuth
  • INDUSTRY users scoped to their own reports; ADMIN/INSPECTOR see all
  • Frontend Reports page now lists real reports, generates real PDFs, downloads via authenticated fetch + Blob trigger
  • Files stored under backend/files/reports/<uuid>.pdf (gitignored)
  • AQI section in the PDF auto-includes data from PR feat(realtime): live air quality (OpenAQ + cron + Socket.io + polling fallback) #7's air_quality_readings table

To run after merging

git checkout main && git pull origin main
cd backend
npm install                                     # adds pdfkit
npx prisma migrate dev --name add_reports       # creates `reports` table
npm run dev

# in another terminal
cd CLIMATRIX
npm run dev

Open http://localhost:5173, log in, click ReportsGENERATE REPORTDOWNLOAD. PDF lands in your Downloads folder.

Diff stat

Same as PR #8: 9 files, +1065 / -21.

…nloadable)

BACKEND
- New Report model + ReportType / ReportFormat enums; back-relations on
  User and Industry. Indexed on (createdAt DESC), (industryId, createdAt DESC).
- services/reportService.ts: pdfkit-based generation, ~2 MB dependency
  (vs Puppeteer's ~300 MB). Brand palette + dashed separators match the
  frontend's sketch-on-paper aesthetic. KPI grid + striped data tables
  with status color-coding (green/amber/red). Auto-paginates long lists.
- AQI section is gated behind a runtime model probe — this PR is
  independent of PR #7. Once #7 is merged, reports auto-include an AQI
  summary; otherwise the section is silently skipped.
- POST /api/v1/reports/generate    create + persist report + write PDF
- GET  /api/v1/reports             list (scoped: INDUSTRY -> own only)
- GET  /api/v1/reports/:id/download   stream PDF with auth + ownership
- DELETE /api/v1/reports/:id       remove DB row + best-effort file
- All routes auth-protected via requireAuth middleware (PR #5).
- Files stored under backend/files/reports/<uuid>.pdf, .gitignored.
- New deps: pdfkit, @types/pdfkit.

FRONTEND
- src/lib/reports.ts: typed list / generate / delete / download helpers,
  uiTypeToBackend mapper for the existing UI label strings, formatBytes.
- App.tsx Reports component:
    -> Loads real reports from /api/v1/reports on mount (with loading state)
    -> 'GENERATE REPORT' button now hits backend; period selector mapped
       to ISO date range (Current Month, Last Month, Q1/Q2 2024, Full Year)
    -> Report cards show real metadata (title, generatedAt, sizeBytes,
       pageCount, industry)
    -> 'DOWNLOAD' button per row uses authenticated fetch + Blob trigger
       (browsers can't send Authorization on plain anchor downloads)
    -> Inline error band for both load and generate failures
    -> Removed the localStorage S.get/S.set scaffolding for reports

Migration required after pulling:
  cd backend && npm install
  npx prisma migrate dev --name add_reports

Independent of PR #7 (realtime AQI). Either merge order works. AQI section
of the PDF appears once both this PR and #7 are merged.
@D-source1602 D-source1602 merged commit c877aea into main May 29, 2026
NischalaV-4 pushed a commit to NischalaV-4/CLIMATRIX that referenced this pull request May 29, 2026
Original work by Anshika Sharma (PR D-source1602#10) — rebased on top of current main.

Original PR was based on an old main (only PR D-source1602#5 merged) and the diff
showed a 2300-line 'deletion' of PRs D-source1602#6, D-source1602#7, D-source1602#9 as a side-effect.
This commit reconstructs only the additive changes:

BACKEND
- backend/src/routes/anomaly.routes.ts (new)
  Pure stats anomaly detector: rolling z-score (window=5) and sudden
  percentage spike. Stateless POST /api/v1/anomaly/detect endpoint.
  Severity grading (low/medium/high) by zScore and pct increase.
  Mounted via the established routes/index.ts pattern (consistent
  with /auth, /aqi, /reports), not via a parallel app.use in app.ts.

FRONTEND
- src/lib/anomaly.ts (new)
  Typed wrapper around the shared api() helper; uses VITE_API_URL.
- src/App.tsx
  Adds RUN SPIKE SCAN button on the AI Anomaly Detection capability
  card with severity-colored result panel.

BUG FIX vs original PR D-source1602#10:
- Original frontend hard-coded http://localhost:5000/api/v1/anomaly/detect.
  Backend runs on :4000 — the feature would have 100% failed in the demo.
  Switched to api() helper which reads VITE_API_URL.

Architecture preserved: the route shape mirrors what a future FastAPI
+ Isolation Forest microservice would expose, so swapping in real ML
later is contained to the detect() function body.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants