-
Notifications
You must be signed in to change notification settings - Fork 32
Handle affiliate approval notification failures #204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
morganschp
wants to merge
2
commits into
profullstack:master
from
morganschp:fix-affiliate-approval-notification-failure
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
144 changes: 144 additions & 0 deletions
144
src/app/api/affiliates/offers/[id]/applications/route.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
| import { PATCH } from "./route"; | ||
| import { NextRequest } from "next/server"; | ||
|
|
||
| const mockGetAuthContext = vi.fn(); | ||
| vi.mock("@/lib/auth/get-user", () => ({ | ||
| getAuthContext: (...args: unknown[]) => mockGetAuthContext(...args), | ||
| })); | ||
|
|
||
| const mockFrom = vi.fn(); | ||
| vi.mock("@/lib/supabase/service", () => ({ | ||
| createServiceClient: () => ({ | ||
| from: (...args: unknown[]) => mockFrom(...args), | ||
| }), | ||
| })); | ||
|
|
||
| function makePatchRequest(id: string, body: Record<string, unknown>) { | ||
| return new NextRequest(`http://localhost/api/affiliates/offers/${id}/applications`, { | ||
| method: "PATCH", | ||
| headers: { "Content-Type": "application/json" }, | ||
| body: JSON.stringify(body), | ||
| }); | ||
| } | ||
|
|
||
| function makeRawPatchRequest(id: string, body: string) { | ||
| return new NextRequest(`http://localhost/api/affiliates/offers/${id}/applications`, { | ||
| method: "PATCH", | ||
| headers: { "Content-Type": "application/json" }, | ||
| body, | ||
| }); | ||
| } | ||
|
|
||
| function makeParams(id: string) { | ||
| return { params: Promise.resolve({ id }) }; | ||
| } | ||
|
|
||
| function chainable(data: unknown, error: unknown = null) { | ||
| const result = { data, error }; | ||
| const handler: ProxyHandler<Record<string, unknown>> = { | ||
| get(_target, prop) { | ||
| if (prop === "then") return undefined; | ||
| if (prop === "data") return data; | ||
| if (prop === "error") return error; | ||
| return () => new Proxy(result, handler); | ||
| }, | ||
| }; | ||
| return new Proxy(result, handler); | ||
| } | ||
|
|
||
| describe("PATCH /api/affiliates/offers/[id]/applications", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it("rejects malformed JSON before updating the application", async () => { | ||
| mockGetAuthContext.mockResolvedValue({ | ||
| user: { id: "seller-1", authMethod: "session" }, | ||
| }); | ||
|
|
||
| const res = await PATCH( | ||
| makeRawPatchRequest("offer-1", "{not valid json"), | ||
| makeParams("offer-1") | ||
| ); | ||
|
|
||
| expect(res.status).toBe(400); | ||
| await expect(res.json()).resolves.toEqual({ error: "Invalid request body" }); | ||
| expect(mockFrom).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("rejects non-object JSON before updating the application", async () => { | ||
| mockGetAuthContext.mockResolvedValue({ | ||
| user: { id: "seller-1", authMethod: "session" }, | ||
| }); | ||
|
|
||
| const res = await PATCH(makeRawPatchRequest("offer-1", "null"), makeParams("offer-1")); | ||
|
|
||
| expect(res.status).toBe(400); | ||
| await expect(res.json()).resolves.toEqual({ error: "Invalid request body" }); | ||
| expect(mockFrom).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("still returns the updated application when notification insert fails", async () => { | ||
| mockGetAuthContext.mockResolvedValue({ | ||
| user: { id: "seller-1", authMethod: "session" }, | ||
| }); | ||
|
|
||
| const notificationInsert = vi.fn().mockResolvedValue({ | ||
| error: { message: "notification table unavailable" }, | ||
| }); | ||
| const consoleWarn = vi.spyOn(console, "warn").mockImplementation(() => {}); | ||
|
|
||
| mockFrom.mockImplementation((table: string) => { | ||
| if (table === "affiliate_offers") { | ||
| return chainable({ id: "offer-1", seller_id: "seller-1" }); | ||
| } | ||
|
|
||
| if (table === "affiliate_applications") { | ||
| return chainable({ | ||
| id: "app-1", | ||
| affiliate_id: "affiliate-1", | ||
| offer_id: "offer-1", | ||
| status: "approved", | ||
| profiles: { username: "alice" }, | ||
| }); | ||
| } | ||
|
|
||
| if (table === "notifications") { | ||
| return { insert: notificationInsert }; | ||
| } | ||
|
|
||
| return chainable(null); | ||
| }); | ||
|
|
||
| try { | ||
| const res = await PATCH( | ||
| makePatchRequest("offer-1", { | ||
| application_id: "app-1", | ||
| action: "approve", | ||
| }), | ||
| makeParams("offer-1") | ||
| ); | ||
|
|
||
| expect(res.status).toBe(200); | ||
| const body = await res.json(); | ||
| expect(body.application).toMatchObject({ | ||
| id: "app-1", | ||
| status: "approved", | ||
| }); | ||
| expect(notificationInsert).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| user_id: "affiliate-1", | ||
| type: "affiliate_approved", | ||
| data: { offer_id: "offer-1", application_id: "app-1" }, | ||
| }) | ||
| ); | ||
| expect(consoleWarn).toHaveBeenCalledWith( | ||
| "Failed to create affiliate application notification", | ||
| { message: "notification table unavailable" } | ||
| ); | ||
| } finally { | ||
| consoleWarn.mockRestore(); | ||
| } | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new notification wrapping has two distinct failure modes:
notificationErrorreturned in the result (tested here) and an exception thrown by.insert()itself (thecatch (error)branch at line 140 ofroute.ts). Only the first path is covered. If.insertwere to throw synchronously or reject, thecatchbranch would fire and the route would still return 200, but that invariant is never verified. A small additional test case — wherenotificationInsertrejects instead of resolving with an error — would complete the coverage for this new behavior.