Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/app/api/applications/bulk-status/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,55 @@ describe("PUT /api/applications/bulk-status", () => {
await expect(res.json()).resolves.toEqual({ error: "Invalid JSON body" });
expect(mockFrom).not.toHaveBeenCalled();
});

it("returns 404 when any requested application id is missing", async () => {
const existingId = "11111111-1111-4111-8111-111111111111";
const missingId = "22222222-2222-4222-8222-222222222222";
const applicationsIn = vi.fn().mockResolvedValue({
data: [
{
id: existingId,
applicant_id: "worker-1",
gig_id: "gig-1",
gig: { poster_id: "poster-1" },
},
],
error: null,
});
const applicationsSelect = vi.fn(() => ({ in: applicationsIn }));
const applicationsUpdate = vi.fn();
const notificationsInsert = vi.fn();

mockFrom.mockImplementation((table: string) => {
if (table === "applications") {
return {
select: applicationsSelect,
update: applicationsUpdate,
};
}

if (table === "notifications") {
return { insert: notificationsInsert };
}

throw new Error(`Unexpected table: ${table}`);
});

const res = await PUT(
makeRequest(
JSON.stringify({
application_ids: [existingId, missingId],
status: "accepted",
})
)
);

expect(res.status).toBe(404);
await expect(res.json()).resolves.toEqual({
error: "Some applications were not found",
});
expect(applicationsIn).toHaveBeenCalledWith("id", [existingId, missingId]);
expect(applicationsUpdate).not.toHaveBeenCalled();
expect(notificationsInsert).not.toHaveBeenCalled();
});
});
11 changes: 11 additions & 0 deletions src/app/api/applications/bulk-status/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@ export async function PUT(request: NextRequest) {
);
}

const requestedIds = [...new Set(application_ids)];
const foundIds = new Set(applications.map((app) => app.id));
const missingIds = requestedIds.filter((id) => !foundIds.has(id));

if (missingIds.length > 0) {
return NextResponse.json(
{ error: "Some applications were not found" },
{ status: 404 }
);
}
Comment on lines +81 to +86

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 The 404 response tells the caller that some IDs were not found, but omits which ones. A client that receives this error has no way to distinguish a typo from a genuine access issue without re-querying; returning missingIds in the body makes retries and debugging straightforward.

Suggested change
if (missingIds.length > 0) {
return NextResponse.json(
{ error: "Some applications were not found" },
{ status: 404 }
);
}
if (missingIds.length > 0) {
return NextResponse.json(
{ error: "Some applications were not found", missing_ids: missingIds },
{ status: 404 }
);
}

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!


// Verify user owns all the gigs these applications belong to
const unauthorizedApplications = applications.filter(
(app) => (app.gig as { poster_id: string })?.poster_id !== user.id
Expand Down
Loading