fix: use .maybeSingle() in applications route to prevent 500 on not-found#444
fix: use .maybeSingle() in applications route to prevent 500 on not-found#444tankgxy wants to merge 1 commit into
Conversation
Greptile SummaryThis PR fixes a correctness bug where
Confidence Score: 4/5Safe to merge — the two changed calls are the correct fix for the described behaviour, and the remaining .single() on the insert is appropriate since an insert always yields exactly one row. The core change is small, well-targeted, and correct. One remaining .single() on the profiles lookup (line 124) has the same 0-row exposure as the two calls that were fixed, and neither maybeSingle() call checks the error field, which can obscure real DB failures as misleading 404s. Both are non-blocking quality issues on a non-critical code path. src/app/api/applications/route.ts — the profiles .single() on line 124 and the unhandled error fields on the two new .maybeSingle() calls are worth a second look. Important Files Changed
Sequence DiagramsequenceDiagram
participant Client
participant Route as POST /api/applications
participant DB as Supabase DB
Client->>Route: "POST { gig_id, ... }"
Route->>Route: Auth + rate-limit check
Route->>DB: "SELECT gig WHERE id=gig_id .maybeSingle()"
alt gig not found (null)
DB-->>Route: "{ data: null, error: null }"
Route-->>Client: 404 Gig not found
else gig found
DB-->>Route: "{ data: gig }"
Route->>Route: "Check gig.status === active"
Route->>Route: "Check gig.poster_id !== user.id"
Route->>DB: SELECT application WHERE gig_id AND applicant_id .maybeSingle()
alt already applied (non-null)
DB-->>Route: "{ data: existingApp }"
Route-->>Client: 400 Already applied
else not yet applied (null)
DB-->>Route: "{ data: null, error: null }"
Route->>DB: INSERT application .select().single()
DB-->>Route: "{ data: application }"
Route->>Route: Reputation, email, activity, webhook
Route-->>Client: "201 { application }"
end
end
|
Two .single() calls in the applications route would throw 500 errors when looking up a non-existent gig or checking for existing applications. Changed both to .maybeSingle() so the null-check logic that follows actually works correctly.