Export fails from Post Inspection Revenue widget

Objective

  • Fix HomeBinder CSV export from the Concierge Adoption section on Admin Post-Inspection Revenue so staff can download the same data they already see in the widget.
  • Remove the generic “Export failed” dead-end when charts/KPIs load successfully for the same filters.
  • Align export authentication with the rest of the Admin dashboard (session must work in production on the Admin host).

Background

  • Reproduced by Ryan on Admin Attik: open Post-Inspection RevenueConcierge AdoptionExport (either option) → toast “Export failed” with no useful detail. Widget data for the same company/date filters loads normally.
  • Screen: Admin → Post-Inspection Revenue (/admin/post-inspection-revenue). Export lives in ConciergeAdoptionSection only (ProPair section has no export).
  • Two export modes (both fail today):
  • By Inspection Date — Attik inspections in range with a HomeBinder integration record.
  • Sent Within Date Range — HomeBinder records sent in range (spreadsheet column format).
  • Root cause hypothesis (code trace): Dashboard fetches use callServerAction, which forwards the authjs.session_token cookie from the Next server to the API. Export uses clientApiFetch (browser fetch with credentials: 'include' directly to NEXT_PUBLIC_SERVER_URL). On Admin production (cross-origin to API), the session cookie often does not reach the API → 401 from auth middleware in login.ts. The export error handler only reads message / error from JSON bodies; auth failures return { result: 'failed', reason: '...' }, so the UI shows a generic “Export failed” title with no description.
  • Backend export endpoints exist and are gated correctly (same permission as the widget): GET /homebinder/export/by-inspection-date and GET /homebinder/export/sent-spreadsheet, both behind requireAdminPerm('admin-post-inspection-revenue-view') in homebinder.ts. CSV builders in exportHomeBinderResults.ts and exportHomeBinderSentSpreadsheet.ts return validation errors as { message } or CSV on success (empty data → header-only CSV, not an error).
  • Page access already requires admin-post-inspection-revenue-view via adminLoginCheck in the Post-Inspection Revenue layout.

Product decisions (v1)

  • Export must work for users who can view the Post-Inspection Revenue widget with the same filter params (companyIds, startDate, endDate).
  • Both export menu items must succeed after fix.
  • Failed exports should surface a actionable error message when the API rejects the request (not a silent/generic toast).
  • Decision needed (dev): exact transport fix—likely match dashboard pattern (callServerAction or a server action returning CSV) rather than browser-direct clientApiFetch; dev owns approach.

Scope

Frontend (attik-frontend)

  • src/app/admin/post-inspection-revenue/_components/ConciergeAdoptionSection.tsxhandleExport calls clientApiFetch to homebinder/export/...; error parsing in catch block (lines ~252–282).
  • src/app/admin/post-inspection-revenue/PostInspectionRevenueClient.tsx — passes exportParams from applied filters; dashboard loads via callServerAction to post-inspection-revenue/concierge-adoption (working path to mirror).
  • src/util/functions/fetching/clientApiFetch.ts vs src/util/functions/fetching/callServerAction.ts — auth/cookie forwarding difference is the likely break.
  • src/app/admin/post-inspection-revenue/layout.tsx — page permission gate.

Backend (attik-backend)

  • src/routes/homebinder.ts — export route handlers + requireAdminPerm('admin-post-inspection-revenue-view').
  • src/util/functions/homebinder/exportHomeBinderResults.ts — by-inspection-date CSV aggregation.
  • src/util/functions/homebinder/exportHomeBinderSentSpreadsheet.ts — sent-spreadsheet CSV.
  • src/routes/login.ts — session auth middleware; 401 body shape { result, reason }.
  • src/routes/adminPostInspectionRevenue.ts — concierge-adoption data endpoint (reference for working auth path).
  • src/util/functions/admin/hasAdminPerm.ts — admin permission check.

Out of scope

  • Changing concierge-adoption chart/KPI aggregation logic.
  • New export formats or ProPair export (not requested).

Done when

  • [ ] Ryan (or any user with admin-post-inspection-revenue-view) can export By Inspection Date CSV from Concierge Adoption on Admin production.
  • [ ] Same user can export Sent Within Date Range CSV.
  • [ ] Export uses the same applied filters as the visible widget (companyIds, startDate, endDate).
  • [ ] Auth failures and API errors show a readable message in the toast (not generic “Export failed” only).
  • [ ] Network trace on repro shows 200 + CSV body (not 401) after fix.

References

  • Admin page: Post-Inspection Revenue → Concierge Adoption (/admin/post-inspection-revenue)
  • Slack report: attik-talk message
  • attik-frontend/src/app/admin/post-inspection-revenue/_components/ConciergeAdoptionSection.tsx
  • attik-backend/src/routes/homebinder.ts

Please authenticate to join the conversation.

Upvoters
Status

Planned

Board
🏠

Main App

Date

About 3 hours ago

Author

Linear

Subscribe to post

Get notified by email when there are changes.