Skip to content

Fix affiliate tracking URL origin - #136

Closed
absalonCRC wants to merge 2 commits into
profullstack:masterfrom
absalonCRC:fix-affiliate-tracking-url-origin
Closed

Fix affiliate tracking URL origin#136
absalonCRC wants to merge 2 commits into
profullstack:masterfrom
absalonCRC:fix-affiliate-tracking-url-origin

Conversation

@absalonCRC

@absalonCRC absalonCRC commented May 21, 2026

Copy link
Copy Markdown
Contributor

Summary

  • build affiliate tracking URLs from NEXT_PUBLIC_APP_URL or the current request origin instead of hardcoding production links
  • return click-route tracking URLs from both the affiliate application response and seller affiliate list API
  • add focused URL-builder coverage and update the seller affiliate list regression test for non-production origins

Fixes #135

Validation

  • pnpm test:run src/lib/affiliates/tracking-url.test.ts 'src/app/api/affiliates/offers/[id]/affiliates/route.test.ts'
  • pnpm exec eslint src/lib/affiliates/tracking-url.ts src/lib/affiliates/tracking-url.test.ts 'src/app/api/affiliates/offers/[id]/affiliates/route.ts' 'src/app/api/affiliates/offers/[id]/affiliates/route.test.ts' 'src/app/api/affiliates/offers/[id]/apply/route.ts'
  • pnpm type-check
  • git diff --check

Bounty / payment

Submitted for the active uGig affiliate-program testing task. SOL receive address: 27sdMYXofqoM9qR13bZhccRNYeEgYn5EoHXTSJn4QWKP.

Payment fallback: PayPal cultofrozen@gmail.com

@greptile-apps

greptile-apps Bot commented May 23, 2026

Copy link
Copy Markdown

Greptile Summary

This PR extracts affiliate tracking URL construction into a shared getAffiliateBaseUrl / buildAffiliateTrackingUrl helper, replacing hardcoded https://ugig.net strings across three route handlers so that URLs are derived from NEXT_PUBLIC_APP_URL or the current request origin instead.

  • src/lib/affiliates/tracking-url.ts (new): centralises base-URL resolution and URLSearchParams-encoded click-URL construction; covered by new unit tests.
  • affiliates/route.ts and ref/[code]/route.ts: adopt the helper cleanly, with updated/new tests verifying the origin-based output.
  • apply/route.ts: also adopts the helper, but this silently changes the tracking_url shape in the apply response from the user-friendly short link (/ref/{code}) to the raw click-tracking endpoint (/api/affiliates/click?ugig_ref={code}); no test was added or updated for this route to document the new behaviour.

Confidence Score: 3/5

Safe to merge for non-production environments, but the apply endpoint now returns a different tracking_url shape that no test documents and no client migration was noted.

The helper extraction is clean and the two routes with updated tests behave correctly. The concern is apply/route.ts: it previously returned a shareable /ref/{code} short link as tracking_url, and now returns the raw /api/affiliates/click?ugig_ref={code} endpoint. Any affiliate dashboard or API consumer that stores and displays that URL will silently receive a less readable URL, and there is no test for this endpoint to assert the new format.

src/app/api/affiliates/offers/[id]/apply/route.ts — the tracking_url format changed without a corresponding test; confirm whether /ref/{code} or /api/affiliates/click?ugig_ref={code} is the intended shareable link for affiliates.

Important Files Changed

Filename Overview
src/lib/affiliates/tracking-url.ts New helper module centralising base-URL resolution and click-URL construction; clean and well-tested.
src/lib/affiliates/tracking-url.test.ts New unit tests covering env-var override, request-origin fallback, and URL encoding — adequate coverage for the helper.
src/app/api/affiliates/offers/[id]/apply/route.ts Switches tracking_url in the apply response from the shareable /ref/{code} short link to the raw /api/affiliates/click endpoint — a behavioural breaking change with no dedicated test update.
src/app/api/affiliates/offers/[id]/affiliates/route.ts Replaces hardcoded production URL with getAffiliateBaseUrl + buildAffiliateTrackingUrl; consistent with test update.
src/app/ref/[code]/route.ts Redirect now uses the shared helper; tests cover both the empty and configured NEXT_PUBLIC_APP_URL cases.
src/app/ref/[code]/route.test.ts New test file; correctly stubs the env var and verifies both redirect destinations.
src/app/api/affiliates/offers/[id]/affiliates/route.test.ts Updated test host to localhost:3000 and expected tracking_url to match new origin-based format.

Sequence Diagram

sequenceDiagram
    participant Client
    participant ApplyRoute as POST /offers/[id]/apply
    participant AffiliatesRoute as GET /offers/[id]/affiliates
    participant RefRoute as GET /ref/[code]
    participant ClickRoute as GET /api/affiliates/click
    participant Helper as tracking-url.ts

    Client->>ApplyRoute: POST apply
    ApplyRoute->>Helper: getAffiliateBaseUrl(request.url)
    Helper-->>ApplyRoute: "NEXT_PUBLIC_APP_URL || request origin"
    ApplyRoute->>Helper: buildAffiliateTrackingUrl(baseUrl, code)
    Helper-->>ApplyRoute: "baseUrl/api/affiliates/click?ugig_ref=code"
    ApplyRoute-->>Client: "{ tracking_url: .../api/affiliates/click?ugig_ref=... }"

    Client->>AffiliatesRoute: GET affiliates list
    AffiliatesRoute->>Helper: getAffiliateBaseUrl(request.url)
    AffiliatesRoute->>Helper: buildAffiliateTrackingUrl(baseUrl, code)
    Helper-->>AffiliatesRoute: "baseUrl/api/affiliates/click?ugig_ref=code"
    AffiliatesRoute-->>Client: "[{ tracking_url: .../api/affiliates/click?ugig_ref=... }]"

    Client->>RefRoute: GET /ref/[code]
    RefRoute->>Helper: getAffiliateBaseUrl(request.url)
    RefRoute->>Helper: buildAffiliateTrackingUrl(baseUrl, code)
    Helper-->>RefRoute: "baseUrl/api/affiliates/click?ugig_ref=code"
    RefRoute-->>Client: "307 Redirect to /api/affiliates/click?ugig_ref=code"
    Client->>ClickRoute: "GET /api/affiliates/click?ugig_ref=code"
Loading

Reviews (1): Last reviewed commit: "Fix affiliate short-link origin" | Re-trigger Greptile

Comment on lines 129 to 133
return NextResponse.json({
application,
tracking_code: trackingCode,
tracking_url: `${process.env.NEXT_PUBLIC_APP_URL || "https://ugig.net"}/ref/${trackingCode}`,
tracking_url: buildAffiliateTrackingUrl(getAffiliateBaseUrl(request.url), trackingCode),
}, { status: 201 });

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 tracking_url format changed from short /ref/ link to raw click endpoint

The old response returned …/ref/{trackingCode} — the purpose-built short link that affiliates share with customers. This PR changes it to …/api/affiliates/click?ugig_ref={trackingCode}, which is the internal click-tracking handler that /ref/[code] redirects to. Any client or affiliate dashboard that stores or displays the tracking_url from the apply response will now see and distribute the raw API URL instead of the clean shareable link. Both resolve to the same destination, but this is a breaking change to the response contract that isn't covered by a test for apply/route.ts.

@ralyodio ralyodio closed this May 23, 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.

bug: affiliate tracking URLs ignore current deployment origin

2 participants