-
Notifications
You must be signed in to change notification settings - Fork 0
no repeat sightings #5
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -54,14 +54,28 @@ export async function POST(request: Request) { | |
| ); | ||
| } | ||
|
|
||
| // Check for existing sighting by this user for this book | ||
| const { data: existingSighting } = await supabase | ||
| .from('sightings') | ||
| .select('id') | ||
| .eq('book_id', bookId) | ||
| .eq('user_id', user.id) | ||
| .single(); | ||
|
|
||
| if (existingSighting) { | ||
| return NextResponse.json( | ||
| { error: 'You have already recorded a sighting for this book' }, | ||
| { status: 409 } | ||
| ); | ||
| } | ||
|
Comment on lines
+57
to
+70
|
||
|
|
||
| // Insert: Use User Client (respects RLS) | ||
| const { data, error } = await supabase | ||
| .from('sightings') | ||
| .insert({ | ||
| book_id: bookId, | ||
| user_id: user.id, | ||
| location, | ||
| // date: Date.now(), // Schema handles created_at | ||
| sighting_type: 'SIGHTING' | ||
| }) | ||
| .select() | ||
|
|
||
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 use of
.single()here will throw an error if no matching record is found, which is not properly handled. This will cause the catch block to execute with a generic "Failed to create sighting" message when the user hasn't created a duplicate. Instead, use.maybeSingle()which returns null when no record exists, or check the error response from the query.