* feat(import): saved sheet sources, daily cron sync, and email-in
Adds a "Connected Sources" foundation and three automation paths on top of the
existing import pipeline (parse -> map -> preview -> dedup -> execute), reusing
it rather than rebuilding.
Saved Google-Sheet sources
- New import_sources entity, profile-scoped, in both runtimes: D1 migration 0020
+ Worker CRUD routes, and idb v12 store + serverless handler + local router.
- Connected Sources UI on the Import page: save a published sheet once, then
Auto sync (headless fetch -> apply saved mapping -> import only new; dedup is
automatic) or Fetch & preview (reuses ImportPreviewStep in a modal). Two
brand-styled buttons (SVG icons, no emoji).
- Column mapping is persisted BY HEADER NAME (shared/importMapping.ts) so it
survives the sheet owner reordering columns; indices are re-resolved on fetch.
- Flow seams: fetchGoogleSheet({navigate:false}) + applySavedMapping.
Daily cron auto-sync
- A "Daily" toggle per source (self-hosted / cloud only). The existing daily
cron (runScheduledSheetSyncs) fetches each daily sheet, resolves its mapping,
and imports only new rows via the shared executeImport, logging the session.
Email-in via Cloudflare Email Routing
- A Worker email() handler: a statement forwarded to ingest+<secret>@<domain> is
parsed (postal-mime), its CSV/XLSX attachments auto-mapped and imported into a
configured profile with dedup. Secret-in-address auth + optional sender
allowlist. Configured via EMAIL_INGEST_* env; disabled when unset.
Shared core
- Extracted executeImport() and fetchGoogleSheetRows() from the HTTP handlers so
the routes, cron, and email handler share one insert+dedup engine (routes are
now thin wrappers; existing behaviour unchanged).
- Moved the column-mapping definitions to shared/ so the Worker and frontend use
the same field names, header variants, and auto-detection.
Docs: docs/plans/import-automation.md, bank-connectivity-and-encryption.md, and
import-automation-setup.md (daily-sync + email-in setup).
Tests: header-map round-trip, serverless source CRUD, email parseAttachment.
Frontend 848 + worker 227 green; typecheck, lint, prettier clean.
* fix(import): daily-sync records a session only when something imported
3.6 KiB
Import automation setup (daily sheet sync + email-in)
The two server-side automations from the import-automation plan. Both run on the Cloudflare Worker, so they apply in self-hosted or managed-cloud mode — not local-only (there's no server to run them). Design: plans/import-automation.md.
1. Daily auto-sync of a saved Google Sheet
Zero setup, no credentials — it reuses a saved source.
- Import page → Connected sources → add a Google-Sheet link (must be shared "anyone with the link can view", or File → Share → Publish).
- Toggle Daily on the source (the pill only appears in self-hosted / cloud mode).
- The Worker's existing daily cron (
worker/wrangler.jsonc→triggers.crons,0 8 * * *) runsrunScheduledSheetSyncs(worker/src/import-sync.ts): it fetches eachschedule='daily'sheet, resolves its saved by-header mapping against the current header row, and imports only new rows via the sharedexecuteImport(dedup is automatic — re-syncing a growing ledger never creates duplicates and never disturbs earlier transactions). Each run that lands or skips rows is recorded as "Google Sheet (daily sync)" in Recent Imports (and is undoable there).
Required columns must still resolve (date + amount); if the sheet owner renames/removes them, that source is skipped for the day (a manual Fetch & preview will surface the mismatch).
2. Email-in — forward a statement, it imports itself
Cloudflare Email Routing delivers a forwarded bank-statement email to the Worker's email()
handler (worker/src/import-email.ts), which parses CSV/XLSX attachments, auto-detects columns,
and imports them (dedup automatic) into a configured profile.
Configure the Worker
# The auth token — it lives in the ingest address's +tag, so keep it long + random.
wrangler secret put EMAIL_INGEST_SECRET --env prod
Set these as vars (in wrangler.jsonc env.prod.vars, or the dashboard):
EMAIL_INGEST_PROFILE_ID— the profile id that emailed statements import into.EMAIL_INGEST_ALLOWED_SENDERS— optional comma-separated sender allowlist (recommended: your own forwarding address). If unset, any sender that knows the address is accepted.
If EMAIL_INGEST_SECRET or EMAIL_INGEST_PROFILE_ID is unset, email-in is disabled (rejects).
Configure Cloudflare Email Routing
- Dashboard → your domain → Email Routing → enable it (adds the MX/TXT DNS records).
- Add a rule: a custom address (e.g.
ingest@yourdomain.com) or a catch-all → action "Send to a Worker" → select this Worker.
Wire up forwarding
- Your ingest address is
ingest+<EMAIL_INGEST_SECRET>@yourdomain.com. Only mail to that exact+tagis accepted (the secret is the address). - In Gmail/Proton, add a filter: when a bank statement arrives → forward to
ingest+<secret>@yourdomain.com. Add that forwarding address toEMAIL_INGEST_ALLOWED_SENDERS.
The Worker reads CSV/XLSX attachments, auto-maps columns (Date + Amount required; Description, Category, Currency, Type used when present), imports with dedup, and logs "Email import ()" in Recent Imports.
Limits (v1)
- Attachments must be tabular CSV/XLSX with a header row. Raw bank layouts that need bank-specific parsing aren't auto-mapped here — use the manual Bank Imports tab for those.
- Only attachments are read; the email body is ignored.
- A message with no importable attachment is rejected (bounced), so a misconfigured forward is visible rather than silently dropped.