Skip to content

feat(statement): formula-safe CSV export & print-friendly PDF view (Refs #336) - #340

Merged
JOY (JOY) merged 2 commits into
devfrom
antigravity/give-336-statement-export
Nov 23, 2026
Merged

JOY (JOY) merged 2 commits into
devfrom
antigravity/give-336-statement-export

Conversation

@JOY

@JOY JOY (JOY) commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Refs #336

Implements public transparency exports (formula-safe CSV download and print-friendly PDF statement view) for public campaign statements:

  • Shared formula-safe CSV builder (apps/web/src/lib/csv.ts + csv.test.ts):
    • Pure RFC-4180 CSV serializer with optional UTF-8 BOM (\uFEFF, enabled by default for Vietnamese Excel compatibility) and CRLF line endings.
    • Strict CSV formula injection neutralization prefixing ' on any cell starting with =, +, -, @, \t, or \r (including when preceded by leading whitespace or ASCII control characters), applied prior to RFC-4180 quoting while keeping finite non-negative numbers unquoted.
    • Safe csvFilenameSlug helper restricting filenames to [a-z0-9-].
  • Public statement CSV export route (apps/web/src/app/campaigns/[slug]/statement/export/route.ts + route.test.ts):
    • Dynamic GET route reading strictly via anonymous dbAs(), validating slug, enforcing isPublicStatus(campaign.status), and verifying getMoneyTotals(db, campaign.id) (404 for drafts, pre-launch approved teasers, suspended, cancelled, or missing campaigns).
    • Supports ?filter=all|in|out|unexplained via parseStatementParams, paginates give.public_statement cleanly up to 5,000 rows (with ID deduplication across concurrent shifts and X-Statement-Truncated: true when total > 5000), and exports strictly public fields shown on StatementPage.
    • Sets Content-Type: text/csv; charset=utf-8, Content-Disposition: attachment; filename="sao-ke-<slug>.csv", Cache-Control: public, s-maxage=60, stale-while-revalidate=300, X-Content-Type-Options: nosniff, and X-Robots-Tag: noindex, nofollow.
  • Statement header controls & print-friendly PDF layout (PrintStatementButton.tsx, statement/page.tsx, globals.css, PublicEvidence.tsx, icons.tsx + tests):
    • Added print:hidden action bar with Tải CSV sao kê link (preserving active filter) and In / Lưu PDF button.
    • Added compact .print-only masthead (hidden print:block) with campaign title, canonical statement URL, print timestamp, active filter/pagination context when applicable, and public totals summary.
    • Added @media print rules forcing light color tokens, display: block body layout, hiding site header/footer/nav/.print\:hidden, and break-inside: avoid on summary cards and statement rows.

Verification

  • pnpm format:check — pass
  • pnpm --filter @give/web typecheck — pass
  • pnpm --filter @give/web lint — pass
  • pnpm --filter @give/web exec vitest run --maxWorkers=2 --fileParallelism=false — 216 test files passed, 2967 tests passed
  • pnpm --filter @give/web build — pass

📌 TL;DR

This PR implements a public CSV export endpoint and print-optimized UI for campaign financial statements, featuring robust security measures against CSV formula injection and strict data privacy controls. It introduces a dedicated csv utility library, a new API route for paginated data retrieval, and print-specific CSS/HTML adjustments to ensure accurate offline reporting.

🎯 Type of Change

  • 🚀 New feature
  • 🐛 Bugfix
  • 🧹 Refactor
  • ⚡ Performance
  • 📚 Documentation
  • ⚙️ CI / Configuration

🔍 Changes Walkthrough

File Summary of Changes
apps/web/src/lib/csv.ts New: Core CSV serialization library. Implements RFC-4180 compliance, UTF-8 BOM support for Vietnamese diacritics, and neutralization of spreadsheet formula injection (DDE) by prefixing dangerous characters (=, +, -, @, etc.) with a single quote.
apps/web/src/lib/csv.test.ts New: Unit tests for the CSV library, verifying formula neutralization, RFC-4180 quoting, BOM handling, and filename slug sanitization.
apps/web/src/app/campaigns/[slug]/statement/export/route.ts New: API Route Handler for GET /campaigns/[slug]/statement/export. Validates campaign public status, fetches statement rows with pagination (capped at 5,000 rows), resolves returned-transfer pairs, and returns a secure CSV response with appropriate headers (Content-Disposition, Cache-Control, X-Statement-Truncated).
apps/web/src/app/campaigns/[slug]/statement/export/route.test.ts New: Integration tests for the export route. Verifies security invariants (no private table access, 404 for non-public campaigns), CSV content accuracy (formula neutralization, PII masking), pagination logic, and error handling.
apps/web/src/app/campaigns/[slug]/statement/page.tsx Modified: Updated the statement page to include a "Download CSV" link and a "Print/PDF" button. Added a print-only masthead section that displays campaign totals, canonical URL, and print timestamp, visible only when printing.
apps/web/src/app/campaigns/[slug]/statement/page.test.ts New: Tests for the statement page UI, verifying the presence of export controls, print masthead content, and 404 behavior for non-public campaigns.
apps/web/src/app/campaigns/[slug]/statement/PrintStatementButton.tsx New: Client component that triggers window.print() on click.
apps/web/src/app/campaigns/[slug]/statement/PrintStatementButton.test.ts New: Unit test for the print button component.
apps/web/src/app/globals.css Modified: Added @media print styles to hide navigation/headers, force light color scheme, and prevent page breaks inside cards.
apps/web/src/components/ui/icons.tsx Modified: Added PrinterIcon SVG component.
apps/web/src/components/PublicEvidence.tsx Modified: Added print:hidden class to evidence details to keep print output clean.
apps/web/src/lib/statement.test.ts Modified: Added tests for new statement export helpers (statementExportHref, statementCsvRow) and CSV header constants.

📊 Architectural Flow

sequenceDiagram
    participant User
    participant WebUI as Statement Page (React)
    participant API as Export Route (Next.js)
    participant DB as Supabase (Public)
    participant CSV as CSV Lib

    User->>WebUI: Click "Download CSV" or "Print"
    
    alt Download CSV
        WebUI->>API: GET /campaigns/[slug]/statement/export
        API->>DB: Validate Campaign Status (Public?)
        alt Not Public
            API-->>User: 404 Not Found
        else Public
            API->>DB: Fetch Statement Rows (Paginated)
            API->>DB: Fetch Returned Transfer Pairs
            API->>CSV: Build CSV (Neutralize Formulas, Add BOM)
            API-->>User: 200 OK (CSV Blob)
        end
    else Print
        WebUI->>User: Trigger window.print()
        Note over User: Browser applies @media print CSS
        Note over User: Hides nav, shows print-only masthead
    end
Loading

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Code review skipped — your organization has no extra usage available to pay for this review.

If your organization's extra usage balance is empty, an organization admin can add extra usage credits at claude.ai/admin-settings/usage. If its monthly spend limit was reached, an admin can raise it on the same page. If neither applies, contact Anthropic support.

Once extra usage is available, someone with write access to this repository can comment @claude review on this pull request to trigger a review.

@vercel

vercel Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
give Ready Ready Preview Nov 23, 2026 12:44pm UTC

Request Review

@coderabbitai

coderabbitai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f34f349a-3eba-406c-9b48-3d86d1874cdb

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

dos[bot]
dos Bot approved these changes Nov 23, 2026

@dos dos Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⏱️ Code Review completed (13 eligible files · 65,157 chars · 2 model call(s) · 3 PR unit(s))

Whole diff with bounded context at base 16a8357 / head 8de0582.

✅ CLEAN_PASS: Complete diff reviewed

Context investigation was bounded to the explicitly requested snapshot paths.

dos[bot]
dos Bot approved these changes Nov 23, 2026

@dos dos Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⏱️ Code Review completed (16 eligible files · 71,057 chars · 3 model call(s) · 3 PR unit(s))

Whole diff with bounded context at base 16a8357 / head 207701b.

✅ CLEAN_PASS: Complete diff reviewed

Context investigation was bounded to the explicitly requested snapshot paths.

@JOY

Copy link
Copy Markdown
Contributor Author

PO acceptance: merge at 207701b

@JOY
JOY (JOY) merged commit 5b6e52a into dev Nov 23, 2026
6 checks passed
@JOY
JOY (JOY) deleted the antigravity/give-336-statement-export branch November 23, 2026 12:46
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.

1 participant