Skip to content

no repeat sightings - #5

Open
clates wants to merge 1 commit into
mainfrom
recordSighting
Open

no repeat sightings#5
clates wants to merge 1 commit into
mainfrom
recordSighting

Conversation

@clates

@clates clates commented Dec 14, 2025

Copy link
Copy Markdown
Owner

No description provided.

@clates
clates requested a review from Copilot December 14, 2025 15:06
@vercel

vercel Bot commented Dec 14, 2025

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Review Updated (UTC)
lfl-booktracker Ready Ready Preview, Comment Dec 14, 2025 3:07pm

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements duplicate sighting prevention by checking if a user has already recorded a sighting for a specific book before allowing a new one to be created. The change also includes cleanup of commented-out code.

  • Added a database query to check for existing sightings by user and book combination
  • Returns a 409 Conflict status when a duplicate sighting is attempted
  • Removed unnecessary commented code

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +57 to +70
// 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 }
);
}

Copilot AI Dec 14, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a potential race condition between checking for existing sightings and inserting a new one. If two requests are processed simultaneously for the same user and book, both could pass the duplicate check before either completes the insert. Consider using a unique constraint on (user_id, book_id) in the database schema and handling the constraint violation error, or use a database transaction if Supabase supports it.

Copilot uses AI. Check for mistakes.
Comment on lines +57 to +70
// 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 }
);
}

Copilot AI Dec 14, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new duplicate sighting prevention logic lacks test coverage. Since the repository has existing test coverage for other sightings endpoints (such as sightings/claim), tests should be added to verify that duplicate sightings are properly rejected and that the appropriate 409 status code and error message are returned.

Copilot uses AI. Check for mistakes.
Comment on lines +58 to +64
const { data: existingSighting } = await supabase
.from('sightings')
.select('id')
.eq('book_id', bookId)
.eq('user_id', user.id)
.single();

Copilot AI Dec 14, 2025

Copy link

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.

Suggested change
const { data: existingSighting } = await supabase
.from('sightings')
.select('id')
.eq('book_id', bookId)
.eq('user_id', user.id)
.single();
const { data: existingSighting, error: existingSightingError } = await supabase
.from('sightings')
.select('id')
.eq('book_id', bookId)
.eq('user_id', user.id)
.maybeSingle();
if (existingSightingError) {
throw existingSightingError;
}

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants