discount-redirect Worker
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/:tokenlogs the click (if the token resolves) and redirects to the field guide checkout with the shared discount code pre-applied via Lemon Squeezy’scheckout[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 againstdiscount_links. Read + increment only; this Worker never inserts rows.wrangler.toml- deploy config.CHECKOUT_URLandDISCOUNT_CODEare 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 byprinciples-audit, not a new one. Applysupabase/migrations/002_discount_links.sqlto that project before deploying.
Before this can go live
- Create the one-time shared discount code by hand in the Lemon Squeezy
dashboard ($10 off $29 → $19) and update
DISCOUNT_CODEinwrangler.tomlto match exactly. - Deploy this Worker and note its URL (
wrangler deploy). - Deploy
../field-guide-purchase-syncfirst (or alongside) - it’s what actually populatesdiscount_linksand the Kit custom field the email’s merge tag reads from. This Worker has nothing to redirect correctly until that one is running. - Replace
[DISCOUNT LINK]in../../_private/kit-emails/02-field-guide-sequence/03-discount.md(and the live email in Kit) withhttps://<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.