Return tracking link for existing affiliate applications - #161
Conversation
Greptile SummaryThis PR fixes a gap in the affiliate apply endpoint where reapplying affiliates received a 409 but no tracking link. The
Confidence Score: 4/5Safe 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
Sequence DiagramsequenceDiagram
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
Reviews (1): Last reviewed commit: "Return tracking link for existing affili..." | Re-trigger Greptile |
| 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", | ||
| }); | ||
| }); |
There was a problem hiding this comment.
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.
Summary
tracking_codein the existing affiliate application lookuptracking_codeandtracking_urlon the already-applied409responseCloses #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-checkgit 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:
27sdMYXofqoM9qR13bZhccRNYeEgYn5EoHXTSJn4QWKPPayment fallback: PayPal cultofrozen@gmail.com