discount-redirect Worker

Backs the personalised discount link in email 3 of Kit’s “Field Guide Pitch” sequence. See ../../_private/kit-email-sequence-plan.md (Stage 2) for the full mechanism and reasoning, and ../field-guide-purchase-sync/README.md for the companion Worker that actually mints the tokens this one reads.

Files

  • index.js - the Worker: GET /discount/:token logs the click (if the token resolves) and redirects to the field guide checkout with the shared discount code pre-applied via Lemon Squeezy’s checkout[discount_code] URL parameter. Fails open on any error (unknown token, Supabase down) - always redirects to a working checkout link, since a lost click log is a far smaller problem than a lost sale.
  • supabase.js - thin REST (PostgREST) helpers against discount_links. Read + increment only; this Worker never inserts rows.
  • wrangler.toml - deploy config. CHECKOUT_URL and DISCOUNT_CODE are plain (non-secret) vars - see inline comments for why.
  • ../../supabase/migrations/002_discount_links.sql - table schema.

Secrets (set via wrangler secret put <NAME>)

  • SUPABASE_URL, SUPABASE_SECRET_KEY - the same dedicated Supabase project used by principles-audit, not a new one. Apply supabase/migrations/002_discount_links.sql to that project before deploying.

Before this can go live

  1. Create the one-time shared discount code by hand in the Lemon Squeezy dashboard ($10 off $29 → $19) and update DISCOUNT_CODE in wrangler.toml to match exactly.
  2. Deploy this Worker and note its URL (wrangler deploy).
  3. Deploy ../field-guide-purchase-sync first (or alongside) - it’s what actually populates discount_links and the Kit custom field the email’s merge tag reads from. This Worker has nothing to redirect correctly until that one is running.
  4. Replace [DISCOUNT LINK] in ../../_private/kit-emails/02-field-guide-sequence/03-discount.md (and the live email in Kit) with https://<this-worker's-url>/discount/.

Local dev

wrangler dev in this directory. Needs SUPABASE_URL / SUPABASE_SECRET_KEY set locally (.dev.vars, gitignored) to exercise a real lookup - without them every request falls through the fail-open path and just redirects without logging.