Skip to content

feat(affiliates): public affiliate program page, applications and founder admin - #751

Merged
unjica merged 6 commits into
developfrom
feat/public-affiliate-program
Oct 11, 2026
Merged

unjica merged 6 commits into
developfrom
feat/public-affiliate-program

Conversation

@unjica

@unjica unjica commented Oct 6, 2026

Copy link
Copy Markdown
Collaborator

Base branch: develop.

Summary

This adds a public affiliate program: a landing page, terms, a no-account application form, and a founder admin UI on top of the existing native affiliate engine. The commission model is unchanged: 30% recurring on hosted Pro/Business, 60-day last-touch window locked at signup, 30-day hold, €50 minimum, manual payouts, no self-referrals, self-hosted never earns.

Everything public is gated on NEXT_PUBLIC_AFFILIATES_ENABLED (dashboard, build-time) plus the API flag. With the flag off, the pages 404, no links render, the sitemap leaves them out and the application endpoint 404s. No new NEXT_PUBLIC_* variable, so the Dockerfile is unchanged.

1. /affiliates (public, indexable)

  • H1 "Earn 30% recurring commission with Telemetry Tracker". Sections: audiences, earnings, how it works, rules, apply (#apply), FAQ. Uses the existing Nav, Footer and design tokens. Responsive (no horizontal overflow at 390px) and accessible (labelled fields, aria-invalid / aria-describedby, role="status" success state).

  • Metadata: title, description, absolute canonical, Open Graph with the site OG image, Twitter card, index, follow.

  • In the sitemap and not blocked by robots.txt. No FAQ JSON-LD, because the site doesn't use that pattern.

  • Pricing source: PLAN_LIST_PRICES_EUR in apps/dashboard/lib/plan-pricing.ts (the same source as /pricing): Pro 15 €/mo, Business 99 €/mo. Monthly billing only; there are no annual plans.

  • Commission per paid invoice is floor(price × 30%):

    Example Per month Per year
    1 Pro 4,50 € —
    1 Business 29,70 € —
    10 Pro 45 € 540 €
    50 Pro 225 € 2.700 €
    5 Business 148,50 € 1.782 €
    20 Pro + 3 Business 179,10 € 2.149,20 €
  • Footnote on the page: these are list prices; commission is on what the customer actually pays, excluding VAT and after discounts; billing is monthly only, so "per year" means 12 monthly invoices; Free and self-hosted never earn.

  • Constants live in apps/dashboard/lib/affiliate-program.ts. affiliate-program.test.ts is a parity test that reads the API engine sources (DEFAULT_COMMISSION_RATE_BPS, COMMISSION_HOLD_DAYS, PAYOUT_MINIMUM_CENTS, REFERRAL_ATTRIBUTION_WINDOW_DAYS, HOSTED_PAID_PLAN_TIERS) and fails if the marketing numbers drift from them.

2. Application form → POST /api/affiliate-applications

  • Fields: name, email, website/profile URL, how they will promote TT, and a required terms checkbox. The accepted terms_version is stored.
  • Abuse protection:
    • Server-side validation and length limits (mirrored on the client).
    • Honeypot company_website: gets a fake success and stores nothing.
    • Per-IP rate limit: 5 per 10 minutes by default, set with RATE_LIMIT_AFFILIATE_APPLICATION_MAX.
    • Global cap: 30 per hour by default, set with AFFILIATE_APPLICATIONS_MAX_PER_HOUR.
    • Dedupe: one pending application per normalized email, using Gmail dot/+tag rules. Duplicates get the same success message.
  • Success state says applicants are not emailed automatically. They never are. An optional founder notification is sent only when AFFILIATE_APPLICATION_NOTIFY_EMAILS is set.
  • No captcha vendor and no fingerprinting. Cookie consent and referral capture are untouched.

3. Founder admin: /dashboard/settings/affiliates

  • Server-side gate: the page calls GET /api/meta/affiliates/access and runs notFound() unless it gets a 200. The API gate is unchanged: 404 when the flag is off, 401 with no session, 403 when the user is not in AFFILIATE_ADMIN_EMAILS. The "Founder → Affiliates" settings nav group only renders when the same check passes. Nothing appears elsewhere in the product dashboard.
  • Applications: filter (pending / approved / rejected / all), details, approve with a suggested editable code, reject with a note.
    • The code format is checked on the client; uniqueness is checked by the API (409 code_taken).
    • Approval creates the Affiliate in the same transaction (SELECT … FOR UPDATE), then shows https://telemetry-tracker.com/?ref=CODE with a copy button plus copyable deep links.
  • Affiliates table: name/email/code, referred orgs, paying orgs, pending (hold), payable (net of open clawbacks, with a "payout ready" badge), paid. Each row has copy-link and disable / enable.
  • Create affiliate directly: for invited partners or a test affiliate used for a production attribution check.
  • Detail page (/dashboard/settings/affiliates/:id):
    • Referred orgs, with resolve needs-attention (reason required).
    • Commissions (payable / on hold / disputed).
    • Record payout: selected payable commissions plus open clawbacks, a generated idempotency key, paid date and reference.
    • Payouts and adjustments.
  • New admin API routes:
    • GET /api/meta/affiliates/access
    • GET /api/meta/affiliates/applications?status=
    • POST /api/meta/affiliates/applications/:id/approve
    • POST /api/meta/affiliates/applications/:id/reject
    • Approve and reject write audit events.

4. Referral persistence: verified destinations

ReferralCapture sits in the root layout, so ?ref= / ?via= is captured on every route. __tests__/referral-destinations.test.ts checks that each destination is a real page and that middleware passes the query through. A browser check against a production build confirmed /pricing?ref=… writes sessionStorage and a second ?ref= replaces it (last touch).

Destination URL
Homepage https://telemetry-tracker.com/?ref=CODE
Pricing https://telemetry-tracker.com/pricing?ref=CODE
Sentry alternative https://telemetry-tracker.com/sentry-alternative?ref=CODE
Migrate from Sentry https://telemetry-tracker.com/docs/migrate-from-sentry?ref=CODE
Next.js error tracking https://telemetry-tracker.com/error-tracking/nextjs?ref=CODE
Next.js SDK docs https://telemetry-tracker.com/docs/nextjs?ref=CODE
Self-hosted vs hosted https://telemetry-tracker.com/self-hosted-error-tracking?ref=CODE
Sign up https://telemetry-tracker.com/register?ref=CODE

Fixed: legacy /?signUp=1&ref=CODE and ?signIn=1 redirects cleared the whole query string and lost the referral. They now keep ref / via (checked live: /?signUp=1&ref=legacy → /register?ref=legacy). The www. → apex redirect already kept ?ref=.

5. Discoverability

  • Footer link "Affiliate Program — Earn 30%" in the Company column. The marketing site and docs share this footer.
  • One line under the pricing tiers.
  • One line on the docs home.
  • Nothing in the product dashboard.

6. /affiliates/terms

  • 11 sections; every number is rendered from the constants.
  • Privacy policy: a short, flag-gated "Affiliate program applications" paragraph (/privacy#affiliate-applications). Needs founder review.

Migration

20261006120000_add_affiliate_applications is additive only. It creates the AffiliateApplication table with:

  • status CHECK (pending / approved / rejected)
  • terms_version, terms_accepted_at
  • reviewed_by, reviewed_at, review_note
  • nullable affiliate_id FK → Affiliate, ON DELETE SET NULL
  • unique pending_email_key: non-NULL only while pending, which gives "one pending application per email" and still lets a rejected applicant re-apply
  • indexes on (status, created_at), email_normalized and affiliate_id

No existing table or column changes. It was tested only against a local Postgres; no production DB access.

How to test

# Local Postgres, TZ=UTC
cd apps/api && RUN_DB_INTEGRATION_TESTS=true pnpm test   # 1196 passed, 8 skipped (baseline 1164 → +32)
cd apps/dashboard && pnpm test                           # 282 passed (baseline 223 → +59)
pnpm lint && pnpm -r run build                           # green (built with the flag on and off)

New tests:

  • API unit (affiliate-application.test.ts, 15): parsing, limits, URL normalization, honeypot, IP key, notify-email parsing.
  • API integration (affiliate-applications.integration.test.ts, 14): flag off → 404, storage, validation, honeypot, Gmail dedupe and re-apply after rejection, concurrent dedupe, hourly cap, per-IP limit, admin gates (404 / 401 / 403), list/filter/suggested code, approve end-to-end (attribution and self-referral via the new affiliate), code_taken, reject, direct create, disable/enable.
  • Coverage audit (affiliates.integration.test.ts, +3):
    • A real invoice.paid gives payable_at = paid_at + 30 days.
    • No commission while the referral is on payout hold.
    • Creating an org for a referred user calls stripe.customers.create with organization_id / tt_org_id / tt_affiliate_id metadata. A plain user doesn't trigger it.
    • No engine bugs found.
  • Dashboard:
    • Parity and earning examples.
    • Last-touch: a second ?ref= replaces the first, for both sessionStorage and the consented cookie.
    • 18 destination and middleware tests.
    • Form rendering, validation, success state and honeypot.
    • Admin gate and nav visibility, referral-link panel, admin helpers.
    • Footer and sitemap gating, robots.

Manual check against a production build (flags on, local API and DB):

  • Approve, reject and create-directly all work.
  • Disable → re-enable works.
  • Resolve needs-attention (rejects an empty reason).
  • Record payout of 59,40 € (2 commissions); balances updated.
  • A non-allowlisted user gets the 404 page and no Founder nav.
  • Anonymous visitors are redirected to /login.

Screenshots

Captured from the local production build. They are on the box at /workspace/affiliates-public/screens/:

  • affiliates-desktop-1280.png (full page), affiliates-desktop-1280-hero.png
  • affiliates-mobile-390.png
  • affiliates-terms-1280.png
  • privacy-affiliate-paragraph.png
  • admin-overview.png, admin-applications-pending-review.png, admin-approved-referral-links.png
  • admin-affiliate-detail.png, admin-affiliate-detail-after-payout.png

Open founder questions

  1. VAT wording. Commission excludes tax (amount_paid − tax). Billing currently has no Stripe Tax, so tax is 0 and commission is 30% of what is charged. Is "excluding VAT / sales tax" the right promise?
  2. Custom / enterprise deals. The terms say they are not covered unless agreed in writing. Keep that?
  3. Terms changes. The terms promise notice of material changes (by email to approved affiliates and by updating the date on the page) before they take effect, but give no fixed notice period. Do you want a number of days?
  4. Disclosure clause. It asks affiliates to disclose the relationship, in line with FTC/ASA practice. Keep it?
  5. Privacy paragraph (#affiliate-applications). Please review it, and decide whether to bump the policy's effective date or version (I left both unchanged).
  6. Rate-limit IP. The API has no trustProxy, so the dashboard server action forwards a validated visitor IP in x-tt-client-ip. A direct API caller could spoof it; the global hourly cap is the backstop. Is that acceptable, or do you want trustProxy or a shared secret?
  7. Payout method details (SEPA / PayPal / Wise) aren't collected on the form. Should they be collected at approval time, out of band?
  8. Affiliate self-view: not built, to keep scope tight. Proposal: a read-only /affiliate page for a signed-in user whose email matches an active affiliate. It would show the code and links, referred and paying org counts, and pending / payable / paid totals, reusing computeAffiliateBalance. It needs a decision on whether affiliates should see org names (probably not).

Code review (Bugbot)

  • Considered: touches billing-adjacent admin flows only through existing APIs; the commission engine is unchanged.

Checklist

  • Tests pass (RUN_DB_INTEGRATION_TESTS=true)
  • Lint passes
  • Build succeeds
  • CHANGELOG updated under [Unreleased]
  • Documentation updated (docs/AFFILIATES.md, apps/api/.env.example)
  • Follows existing style
  • No breaking changes

Security / privacy

  • The admin is gated server-side by the existing API allowlist. The dashboard never decides access itself.
  • The public endpoint has a honeypot, limits, rate limiting and dedupe. The IP is not stored.
  • No captcha, no fingerprinting, no new cookies. Consent behaviour is unchanged. Applicants are never emailed.

unjica added 2 commits October 6, 2026 08:46
…r admin

- /affiliates landing page (earning examples from PLAN_LIST_PRICES_EUR) and /affiliates/terms
- No-account application form -> POST /api/affiliate-applications (validation, honeypot,
  per-IP rate limit, global hourly cap, pending dedupe by normalized email)
- Additive migration 20261006120000_add_affiliate_applications
- Founder admin at /dashboard/settings/affiliates: review/approve/reject, referral URL +
  deep links, create affiliate directly, disable/enable, payouts, resolve needs-attention
- Footer link, pricing + docs mentions, sitemap, privacy paragraph (all flag-gated)
- Fix: legacy ?signUp=1 / ?signIn=1 redirects kept ref/via
- Tests: hold dates, payout hold, Stripe Customer metadata, last-touch, destinations
@unjica
unjica merged commit ba6aa93 into develop Oct 11, 2026
7 checks passed
@unjica unjica mentioned this pull request Oct 11, 2026
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.

2 participants