Barcode Food Duplication Fix#777
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses an issue where scanning and saving the same barcode multiple times would create duplicate food entries in the database. The changes introduce a Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request effectively resolves the food duplication issue when scanning barcodes by checking for an existing food item before creation. The backend logic is sound, and the inclusion of new unit tests for the foodCoreService is a great addition that improves the robustness of the codebase. I've included a couple of minor suggestions to enhance code maintainability by reducing some duplication.
| method: 'PUT', | ||
| body: { | ||
| ...foodData, | ||
| barcode: foodData.barcode, |
There was a problem hiding this comment.
This barcode property is already included from spreading foodData on the previous line, making this explicit assignment redundant. The same redundancy exists for provider_external_id and provider_type on the following lines. The body for this PUT request could be simplified to just foodData to remove this redundancy and improve clarity.
| `SELECT | ||
| f.id, f.name, f.brand, f.is_custom, f.user_id, f.shared_with_public, f.provider_external_id, f.provider_type, | ||
| json_build_object( | ||
| 'id', fv.id, | ||
| 'serving_size', fv.serving_size, | ||
| 'serving_unit', fv.serving_unit, | ||
| 'calories', fv.calories, | ||
| 'protein', fv.protein, | ||
| 'carbs', fv.carbs, | ||
| 'fat', fv.fat, | ||
| 'saturated_fat', fv.saturated_fat, | ||
| 'polyunsaturated_fat', fv.polyunsaturated_fat, | ||
| 'monounsaturated_fat', fv.monounsaturated_fat, | ||
| 'trans_fat', fv.trans_fat, | ||
| 'cholesterol', fv.cholesterol, | ||
| 'sodium', fv.sodium, | ||
| 'potassium', fv.potassium, | ||
| 'dietary_fiber', fv.dietary_fiber, | ||
| 'sugars', fv.sugars, | ||
| 'vitamin_a', fv.vitamin_a, | ||
| 'vitamin_c', fv.vitamin_c, | ||
| 'calcium', fv.calcium, | ||
| 'iron', fv.iron, | ||
| 'is_default', fv.is_default, | ||
| 'glycemic_index', fv.glycemic_index, | ||
| 'custom_nutrients', fv.custom_nutrients | ||
| ) AS default_variant | ||
| FROM foods f | ||
| LEFT JOIN food_variants fv ON f.id = fv.food_id AND fv.is_default = TRUE | ||
| WHERE f.barcode = $1 AND f.user_id = $2 | ||
| LIMIT 1`, |
There was a problem hiding this comment.
The SELECT clause, particularly the large json_build_object for default_variant, is duplicated across several functions in this file (e.g., getFoodById, searchFoods). This creates a maintenance challenge, as any change to the food_variants structure requires updates in multiple locations.
Consider refactoring this into a reusable component to improve maintainability. Options include:
- A database view (e.g.,
v_foods_with_default_variant) that encapsulates this logic. - A JavaScript constant holding the common SQL fragment.
Using a view would simplify your queries significantly, for example:
SELECT * FROM v_foods_with_default_variant WHERE barcode = $1 AND user_id = $2 LIMIT 1
Description
Right now, if a user scans a barcode, saves the food, scans and saves it again they will end up with 2 rows in the db with the same info. This fix will check for existing barcodes on create and will reuse the existing food item.
Related Issue
PR type [x] Issue [ ] New Feature [ ] Documentation
Linked Issue: #
Checklist
Please check all that apply:
npm run lintandnpm run format(especially for Frontend).en) translation file (if applicable).rls_policies.sqlfor any new user-specific tables.