Skip to content

Return tracking link for existing affiliate applications - #161

Closed
absalonCRC wants to merge 1 commit into
profullstack:masterfrom
absalonCRC:fix-affiliate-existing-application-tracking-url
Closed

Return tracking link for existing affiliate applications#161
absalonCRC wants to merge 1 commit into
profullstack:masterfrom
absalonCRC:fix-affiliate-existing-application-tracking-url

Conversation

@absalonCRC

@absalonCRC absalonCRC commented May 21, 2026

Copy link
Copy Markdown
Contributor

Summary

  • include tracking_code in the existing affiliate application lookup
  • return tracking_code and tracking_url on the already-applied 409 response
  • reuse one tracking URL helper for new and existing application responses
  • add regression coverage for approved affiliates recovering their tracking link

Closes #160

Validation

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

Paid task

Submitted for uGig paid task 4741218f-a723-46bb-82cb-6516120331ae.

Payment 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 fixes a gap in the affiliate apply endpoint where reapplying affiliates received a 409 but no tracking link. The tracking_code column is now included in the duplicate-check query and returned alongside tracking_url in the conflict response, with the URL construction extracted into a shared trackingUrl() helper.

  • route.ts: SELECT updated to include tracking_code; 409 response body now mirrors the 201 shape with tracking_code and tracking_url (null-safe when the code is absent); inline URL template replaced by the new helper.
  • route.test.ts: New test file with one regression case asserting the full 409 payload for an approved affiliate who reapplies.

Confidence Score: 4/5

Safe to merge — the route change is additive and well-guarded; the only gap is one missing test branch.

The implementation correctly handles both the non-null and null tracking_code cases in the 409 response. The new test covers the happy path for the new behaviour but leaves the null tracking_code branch (pending applications without a code) untested, so a future edit to that conditional could regress silently.

The test file would benefit from a second case covering a pending application with a null tracking_code; the route file itself has no issues.

Important Files Changed

Filename Overview
src/app/api/affiliates/offers/[id]/apply/route.ts Adds tracking_code to existing-application select, returns tracking_code + tracking_url on 409, and extracts a trackingUrl() helper to eliminate inline duplication. Logic is correct; the null-tracking_code guard is handled properly.
src/app/api/affiliates/offers/[id]/apply/route.test.ts New test file with one regression case covering an approved affiliate reapplying. The mock chain is correctly shaped for both single-eq and double-eq Supabase query paths; missing a complementary case for null tracking_code.

Sequence Diagram

sequenceDiagram
    participant Client
    participant POST as POST /api/affiliates/offers/[id]/apply
    participant Supabase

    Client->>POST: POST with offer id + auth

    POST->>Supabase: SELECT affiliate_offers (id, seller_id, status, slug)
    Supabase-->>POST: offer data

    POST->>Supabase: SELECT affiliate_applications (id, status, tracking_code)
    Supabase-->>POST: existing application (or null)

    alt existing application found
        POST-->>Client: "409 { error, application, tracking_code, tracking_url }"
        note over POST,Client: tracking_url is null when tracking_code is null
    else no existing application
        POST->>Supabase: INSERT affiliate_applications (with tracking_code)
        Supabase-->>POST: new application
        POST->>Supabase: UPDATE affiliate_offers (total_affiliates++)
        POST->>Supabase: INSERT notifications
        POST-->>Client: "201 { application, tracking_code, tracking_url }"
    end
Loading

Reviews (1): Last reviewed commit: "Return tracking link for existing affili..." | Re-trigger Greptile

Comment on lines +62 to +97
it("returns the existing tracking URL when an approved affiliate reapplies", async () => {
mockFrom.mockImplementation((table: string) => {
if (table === "affiliate_offers") {
return makeSingleResponse({
id: "offer-1",
seller_id: "seller-1",
status: "active",
slug: "test-offer",
});
}

if (table === "affiliate_applications") {
return makeSingleResponse({
id: "application-1",
status: "approved",
tracking_code: "alice-test123",
});
}

return {};
});

const res = await POST(makeRequest(), makeParams());

expect(res.status).toBe(409);
await expect(res.json()).resolves.toEqual({
error: "Already approved",
application: {
id: "application-1",
status: "approved",
tracking_code: "alice-test123",
},
tracking_code: "alice-test123",
tracking_url: "https://ugig.net/ref/alice-test123",
});
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Missing test for null tracking_code path

The new test only covers the case where existing.tracking_code is set. The route also handles existing.tracking_code === null (e.g. a pending application created before tracking codes were introduced) by returning tracking_url: null. A test asserting that shape would guard against a future regression where the ? …: null branch is accidentally removed or changed.

@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: reapplying affiliates lose their existing tracking link

2 participants