Skip to content

Barcode Food Duplication Fix#777

Merged
CodeWithCJ merged 3 commits intoCodeWithCJ:mainfrom
apedley:barcode-fix
Feb 28, 2026
Merged

Barcode Food Duplication Fix#777
CodeWithCJ merged 3 commits intoCodeWithCJ:mainfrom
apedley:barcode-fix

Conversation

@apedley
Copy link
Contributor

@apedley apedley commented Feb 28, 2026

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:

  • Alignment: I have raised a GitHub issue and it was reviewed/approved by maintainers (required for new features).
  • Tests: I have included automated tests for my changes.
  • Screenshots: I have attached "Before" vs "After" screenshots below (required for UI changes).
  • Quality: I have run npm run lint and npm run format (especially for Frontend).
  • Translations: I have only updated the English (en) translation file (if applicable).
  • Architecture: My code follows the existing architecture standards.
  • Database Security: I have updated rls_policies.sql for any new user-specific tables.
  • Integrity: I certify this is my own work, free of malicious code(phishing, malware, etc.)
  • License: I agree to the License terms.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, 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 barcode field to food items and modify the food creation process to first check if a food with the given barcode already exists for the user. If an existing food is found, it is reused instead of creating a new one, thereby preventing data duplication and ensuring data consistency.

Highlights

  • Barcode Field Integration: Introduced a barcode field to food data structures across the frontend and backend to support unique identification.
  • Duplicate Food Prevention: Implemented logic to prevent duplicate food entries by checking for existing barcodes before creating new food items, reusing existing ones if found.
  • API Endpoint Refactoring: Removed the /create-or-get API endpoint and its associated service logic, as its functionality is now integrated into the createFood process.
  • New Repository Functionality: Added a new findFoodByBarcode function in the food repository to efficiently query for food items by their barcode.
  • Comprehensive Testing: Added comprehensive unit tests for the new barcode duplication prevention logic in the food core service.
Changelog
  • SparkyFitnessFrontend/src/api/Foods/enhancedCustomFoodFormService.ts
    • Included the barcode field when updating and creating food items.
  • SparkyFitnessFrontend/src/components/FoodSearch/CustomFoodForm.tsx
    • Passed the barcode from the food object to the saveFood function.
  • SparkyFitnessFrontend/src/types/food.ts
    • Added an optional barcode property to the Food interface.
  • SparkyFitnessFrontend/src/utils/foodSearch.ts
    • Mapped the product.code from OpenFoodFacts to the barcode field of the Food object.
  • SparkyFitnessServer/models/food.js
    • Added a new findFoodByBarcode function to query the database for food items by barcode and user ID.
    • Removed the findFoodByNameAndBrand function and its export.
  • SparkyFitnessServer/routes/foodCrudRoutes.js
    • Removed the /create-or-get API endpoint and its associated Swagger documentation.
  • SparkyFitnessServer/services/foodCoreService.js
    • Modified the createFood function to check for an existing food by barcode before proceeding with creation. If found, the existing food is returned.
    • Removed the createOrGetFood function.
  • SparkyFitnessServer/tests/foodCoreService.test.js
    • Added a new test file containing unit tests for the createFood function's barcode duplication prevention logic.
Activity
  • Automated tests were included for the changes.
  • npm run lint and npm run format were run.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@apedley apedley changed the title Barcode Duplication Fix Barcode Food Duplication Fix Feb 28, 2026
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

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,
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

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.

Comment on lines +275 to +305
`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`,
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

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

@CodeWithCJ CodeWithCJ merged commit 7464c1b into CodeWithCJ:main Feb 28, 2026
6 checks passed
@apedley apedley deleted the barcode-fix branch February 28, 2026 01:46
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