fix: use .maybeSingle() in saved-gigs route#446
Conversation
Greptile SummaryFixes two
Confidence Score: 4/5Safe to merge — the core fix is correct and eliminates real 500 errors; the only concern is a pre-existing gap where .maybeSingle() errors are not checked, which could cause misleading responses under DB failure but is unlikely to cause data loss given the downstream unique constraint. Both .maybeSingle() changes are the right call for read-only existence checks and the surrounding logic already handles null correctly. The unguarded error field in both calls is a pre-existing issue — a DB error on the duplicate-check query would silently let execution reach the INSERT, but the unique constraint in saved_gigs would catch it there. The change is small, focused, and improves the API's observable behavior for the primary happy-path and not-found cases. src/app/api/saved-gigs/route.ts — both new .maybeSingle() calls omit error handling; worth adding error destructuring and an early 500 return to make DB failures distinguishable from not found. Important Files Changed
Sequence DiagramsequenceDiagram
participant Client
participant POST /api/saved-gigs
participant Supabase
Client->>POST /api/saved-gigs: POST { gig_id }
POST /api/saved-gigs->>Supabase: getAuthContext()
Supabase-->>POST /api/saved-gigs: user
POST /api/saved-gigs->>Supabase: gigs.select().eq(gig_id).maybeSingle()
Note over Supabase: Returns null if not found (was 500 before)
Supabase-->>POST /api/saved-gigs: gig | null
alt gig is null
POST /api/saved-gigs-->>Client: 404 Gig not found
else "gig.status !== active"
POST /api/saved-gigs-->>Client: 400 Can only save active gigs
else "gig.poster_id === user.id"
POST /api/saved-gigs-->>Client: 400 Cannot save your own gig
else gig is valid
POST /api/saved-gigs->>Supabase: saved_gigs.select().eq(user_id, gig_id).maybeSingle()
Note over Supabase: Returns null if not saved (was 500 before)
Supabase-->>POST /api/saved-gigs: existing | null
alt existing is not null
POST /api/saved-gigs-->>Client: 409 Gig already saved
else
POST /api/saved-gigs->>Supabase: saved_gigs.insert().single()
Supabase-->>POST /api/saved-gigs: savedGig
POST /api/saved-gigs-->>Client: 201 { saved: savedGig }
end
end
|
Two .single() calls would throw 500 when checking for non-existent gigs or duplicates. Changed to .maybeSingle().