Skip to content

feat(campaigns): ambassador referral links (?ref=), opt-in leaderboard & /me summary - #327

Merged
JOY (JOY) merged 1 commit into
devfrom
antigravity/give-319-ambassadors
Nov 22, 2026
Merged

JOY (JOY) merged 1 commit into
devfrom
antigravity/give-319-ambassadors

Conversation

@JOY

@JOY JOY (JOY) commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Refs #319

Depends on Phase 1 DB migration DOS/DOS.Me#1887 (20261202130000_give_campaign_ambassadors.sql, merged into dev at dd79c58eb4a813bda4ed608c77a86dd8764e0803, live on Beta oyajkbadsykigtfrpdpg).

Summary (Phase 2 — App)

  • apps/web/src/lib/ambassadors.ts: Pure validators (sanitizeAmbassadorRef, canCreateAmbassadorLink, ambassadorFormSchema, ambassadorLinkPath, campaignPledgeHref) and zero-PII RPC wrappers (getPublicAmbassadorRef, listCampaignAmbassadorLeaderboard, getMyCampaignAmbassador, listMyAmbassadorLinks, upsertMyCampaignAmbassador).
  • "Tạo link kêu gọi giúp chiến dịch" UI on /campaigns/[slug] (AmbassadorLinkModal.tsx + ambassador-actions.ts):
    • Reuses accessible ModalDialog.tsx for signed-in users to create/update their display name and leaderboard opt-in strictly under dbAs(session.accessToken) (zero dbService()), copy their shareable ?ref=<code> link, and review personal net matched stats.
    • Guests see a clean sign-in link (/auth/login?return_path=...).
    • Strictly gated to live (unended) & public pre-launch campaigns — never shown on draft, preview, suspended, cancelled, or ended campaigns.
  • Fail-open ?ref=<code> attribution flow:
    • Validates ?ref= via give.public_ambassador_ref to display a calm plain-text attribution banner on /campaigns/[slug] and /campaigns/[slug]/pledge, preserving ?ref= across pledge links, RewardTiers, and login return_path.
    • Passes sanitized p_ambassador_ref into create_donation (donations/actions.ts) and create_pledge (pledge/actions.ts). Invalid/malformed/stale refs are silently dropped and NEVER block or fail a donation or pledge.
  • Opt-in "Người đồng hành lan tỏa" leaderboard (AmbassadorLeaderboard.tsx):
    • Strictly gated on isPublicLiveOrEnded (never queried or rendered on publicPrelaunch or preview), displaying only display_name, matched_count, and matched_total_vnd (tabular-nums, zero user_id/PII), hidden cleanly when empty.
  • Personal ambassador summary on /me (MyAmbassadorLinksSection.tsx):
    • "Liên kết đồng hành của tôi" powered by give.list_my_ambassador_links() under user JWT for both non-organizers and organizers, with copy-link button, visibility badge, and net matched stats.

Verification

  • Fresh-context security, privacy, fail-open & DESIGN.md review: CLEAN (0 P0, 0 P1; all P2 a11y/UX polish items resolved).
  • Local quality gates (on top of latest origin/dev including #317):
    • pnpm format:check: PASS
    • pnpm lint: PASS
    • pnpm typecheck: PASS
    • pnpm test: 206/206 test files, 2,864/2,864 tests PASS (including client-boundary.test.ts)
    • pnpm build: PASS

📌 TL;DR

This PR implements the "Ambassador Referral" feature (#319), allowing users to generate shareable campaign links with unique reference codes. It integrates these references into donation and pledge flows, adds a public leaderboard for top ambassadors, and ensures fail-open behavior for invalid references to prevent donation failures.

🎯 Type of Change

  • 🚀 New feature
  • 🐛 Bugfix
  • 🧹 Refactor
  • ⚡ Performance
  • 📚 Documentation
  • ⚙️ CI / Configuration

🔍 Changes Walkthrough

File Summary of Changes
apps/web/src/app/(public)/donations/actions.ts Integrates sanitizeAmbassadorRef into the donate action. Passes p_ambassador_ref to the create_donation RPC only if valid, ensuring invalid refs fail open without blocking the transaction.
apps/web/src/app/(public)/donations/actions.test.ts Adds tests verifying that valid refs are sanitized and passed to the DB, while malformed refs (e.g., XSS attempts) are silently dropped.
apps/web/src/app/campaigns/[slug]/DonateForm.tsx Updates the donation form to accept ambassadorRef and ambassadorDisplayName. Renders a hidden input for the ref and a visual attribution note if present.
apps/web/src/app/campaigns/[slug]/ambassador-actions.ts New Server Action saveMyCampaignAmbassador. Handles creation/updating of ambassador links under the user's JWT. Includes validation, error handling for expired sessions, and cache revalidation.
apps/web/src/app/campaigns/[slug]/ambassador-actions.test.ts New Tests for the ambassador action, covering auth checks, input validation, JWT usage (ensuring no service role usage), and error mapping.
apps/web/src/app/campaigns/[slug]/page.tsx Major update to the campaign page. Fetches public ambassador ref, leaderboard data, and user's own ambassador stats. Renders AmbassadorLeaderboard and AmbassadorLinkModal based on campaign status (live/pre-launch). Preserves ?ref in login and pledge links.
apps/web/src/app/campaigns/[slug]/page.test.ts Extensive tests for the campaign page rendering ambassador UI, verifying leaderboard visibility rules (live only), ref preservation in links, and fail-open behavior for invalid refs.
apps/web/src/app/campaigns/[slug]/pledge/actions.ts Updates savePledge to accept and sanitize ambassador_ref. Passes it to create_pledge RPC. Ensures change_pledge does not overwrite existing attribution.
apps/web/src/app/campaigns/[slug]/pledge/actions.test.ts Tests verifying ref handling in pledge creation and ensuring it's ignored during pledge updates.
apps/web/src/app/campaigns/[slug]/pledge/page.tsx Updates pledge page to resolve ?ref param, display attribution note, and pass the ref to PledgeForm.
apps/web/src/app/campaigns/[slug]/pledge/PledgeForm.tsx Adds ambassadorRef prop to render a hidden input in the pledge form.
apps/web/src/app/campaigns/[slug]/pledge/page.test.ts Tests for pledge page ref resolution, attribution display, and fail-open behavior.
apps/web/src/app/me/page.tsx Updates the "My Profile" page to fetch and display the user's ambassador links via MyAmbassadorLinksSection.
apps/web/src/app/me/page.test.ts Tests verifying the rendering of the "My Ambassador Links" section on the profile page.
apps/web/src/components/RewardTiers.tsx Updates RewardTiers to accept ambassadorRef and append it to pledge tier links.
apps/web/src/components/campaign/AmbassadorLeaderboard.tsx New Component rendering the public leaderboard of top ambassadors (name, matched count, total amount). Hides if empty.
apps/web/src/components/campaign/AmbassadorLeaderboard.test.ts New Tests for the leaderboard component, including XSS safety checks.
apps/web/src/components/campaign/AmbassadorLinkModal.tsx New Client component for creating/managing ambassador links. Handles guest login redirect, modal state, link copying, and stats display.
apps/web/src/components/campaign/AmbassadorLinkModal.test.ts New Tests for the modal component, verifying guest vs. signed-in states and UI copy.

📊 Architectural Flow

sequenceDiagram
    participant User
    participant CampaignPage
    participant AmbassadorAction
    participant DB
    participant DonateAction

    User->>CampaignPage: Visits /campaigns/[slug]?ref=CODE
    CampaignPage->>DB: getPublicAmbassadorRef(CODE)
    DB-->>CampaignPage: { display_name, ref_code }
    CampaignPage->>User: Renders Attribution Note & Leaderboard
    
    alt User is Guest
        User->>CampaignPage: Clicks "Create Link"
        CampaignPage->>User: Redirects to /auth/login?return_path=...
    else User is Signed In
        User->>AmbassadorAction: saveMyCampaignAmbassador(form)
        AmbassadorAction->>DB: upsert_my_campaign_ambassador (User JWT)
        DB-->>AmbassadorAction: { ref_code, stats }
        AmbassadorAction-->>User: Updates UI with Link & Stats
    end

    User->>DonateAction: Submits Donation (with hidden ref)
    DonateAction->>DonateAction: sanitizeAmbassadorRef(ref)
    alt Ref is Valid
        DonateAction->>DB: create_donation (p_ambassador_ref)
    else Ref is Invalid
        DonateAction->>DB: create_donation (no ref)
    end
    DB-->>User: Donation Success
Loading

@JOY JOY (JOY) added line:campaigns Product line: campaigns po:agy Owned by Antigravity PO (mutual-exclusion parallel PO label) labels Nov 22, 2026

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Code review skipped — your organization has no extra usage available to pay for this review.

If your organization's extra usage balance is empty, an organization admin can add extra usage credits at claude.ai/admin-settings/usage. If its monthly spend limit was reached, an admin can raise it on the same page. If neither applies, contact Anthropic support.

Once extra usage is available, someone with write access to this repository can comment @claude review on this pull request to trigger a review.

@vercel

vercel Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
give Ready Ready Preview Nov 22, 2026 4:17pm UTC

Request Review

@coderabbitai

coderabbitai Bot commented Nov 22, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 2dc42490-14a4-44ea-a7dc-0edd8d85a66c

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

dos[bot]
dos Bot approved these changes Nov 22, 2026

@dos dos Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⏱️ Code Review completed (25 eligible files · 96,635 chars · 2 model call(s) · 4 PR unit(s))

Whole diff with bounded context at base d71631c / head 3cacb5c.

✅ CLEAN_PASS: Complete diff reviewed

Context investigation was bounded to the explicitly requested snapshot paths.

@JOY

Copy link
Copy Markdown
Contributor Author

PO acceptance: merge at 3cacb5c

Independent PO flagship review (6393ae22-2d70-445c-ba87-2c8e8b747212): APPROVE (0 P0/P1/P2 findings).

@JOY
JOY (JOY) merged commit 8fb20d3 into dev Nov 22, 2026
6 checks passed
@JOY
JOY (JOY) deleted the antigravity/give-319-ambassadors branch November 22, 2026 16:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

line:campaigns Product line: campaigns po:agy Owned by Antigravity PO (mutual-exclusion parallel PO label)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant