Skip to content

Fix Meal Serving Sizing Logic#763

Merged
CodeWithCJ merged 3 commits intoCodeWithCJ:mainfrom
Gtt1229:main
Feb 28, 2026
Merged

Fix Meal Serving Sizing Logic#763
CodeWithCJ merged 3 commits intoCodeWithCJ:mainfrom
Gtt1229:main

Conversation

@Gtt1229
Copy link
Contributor

@Gtt1229 Gtt1229 commented Feb 26, 2026

Addresses #762

Restructures the meal handling by tracking the original template meal data, in order to utilize it during the serving logic. Previously, all units (grams, ml, serving) were being treated the same causing in multiplication of the original unit, rather than scaling logic.

@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 a critical bug where meal serving sizes were being incorrectly calculated, leading to inaccurate nutritional tracking. The solution involves restructuring meal handling to consistently track and utilize original meal template data across both the frontend and backend. This ensures that all units (grams, ml, servings) are scaled correctly, preventing erroneous multiplication of base units and providing precise nutritional information when logging or editing meals.

Highlights

  • Frontend: MealBuilder Component Enhancements: Introduced a new templateInfo state to store the original meal template's ID, size, and unit, which is crucial for accurate serving size calculations. The calculateMealNutrition function was updated to use this templateInfo for precise scaling of nutrients when a meal is logged in the food diary, ensuring correct nutritional totals based on the original template's definition.
  • Frontend: MealUnitSelector Logic Refinement: Adjusted the nutrient aggregation logic within the MealUnitSelector to correctly scale individual food item nutrients based on their quantity relative to their serving size. The multiplier calculation was also refined to handle 'serving' units more accurately, preventing incorrect multiplication of base units.
  • Backend: Food Entry Service Scaling Logic: Modified the createFoodEntryMeal and updateFoodEntryMeal functions to conditionally apply serving size scaling. Scaling is now only performed if a meal_template_id is present, ensuring that meals not based on a template are treated as having base quantities. The getFoodEntryMealWithComponents function was also updated to correctly unscale food quantities for editing, distinguishing between template-based and non-template-based meals.
Changelog
  • SparkyFitnessFrontend/src/components/MealBuilder.tsx
    • Added templateInfo state to track original meal template details (ID, size, unit).
    • Updated useEffect hooks to populate templateInfo when loading existing meals or initializing new ones.
    • Revised calculateMealNutrition to use templateInfo for accurate multiplier calculation in 'food-diary' mode.
    • Included templateInfo in the dependency array for calculateMealNutrition's useCallback.
  • SparkyFitnessFrontend/src/pages/Foods/MealUnitSelector.tsx
    • Modified nutrient aggregation to scale individual food item nutrients by their quantity relative to their serving size.
    • Refined the multiplier calculation to specifically check for 'serving' units matching the meal's serving unit.
  • SparkyFitnessServer/services/foodEntryService.js
    • Updated createFoodEntryMeal to apply serving size scaling only when a meal_template_id is present.
    • Changed updateFoodEntryMeal to set the multiplier to 1.0 when no template is present, ensuring base quantities are used.
    • Adjusted getFoodEntryMealWithComponents to correctly unscale food quantities for editing based on the presence of a meal_template_id.
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.

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 correctly addresses a flaw in the meal serving size logic by introducing tracking for the original meal template data. The backend changes to differentiate between template-based and custom meals are solid. However, I've identified a regression on the frontend where the nutrition calculation for custom meals in the food diary is now incorrect. I've also suggested a clarification in another component to improve code readability and maintainability. With these adjustments, the fix will be more robust.

@Gtt1229 Gtt1229 marked this pull request as draft February 26, 2026 22:37
@Gtt1229
Copy link
Contributor Author

Gtt1229 commented Feb 26, 2026

Switched to a draft, need further testing at the moment

@Gtt1229 Gtt1229 marked this pull request as ready for review February 26, 2026 22:43
@CodeWithCJ
Copy link
Owner

CodeWithCJ commented Feb 26, 2026

Also attach screenshot of before vs after fix.

@Gtt1229
Copy link
Contributor Author

Gtt1229 commented Feb 26, 2026

Also attach screenshot of before vs after fix.

I put it in the issue itself too: #762 (comment)


Example of issue:

Take the following:

Food item:

Image

Meal:

Image

Additing as a diary entry:

Before fix:

Scale was multiplying base values by 100 rather than an actual serving. Makes sense when the unit is "serving", but not how it should be handled for quantitative measurements.
Image

After Fix:

Image

@CodeWithCJ
Copy link
Owner

Thanks. review once if report charts & table also reflects correctly.

@Gtt1229 Gtt1229 marked this pull request as draft February 26, 2026 23:01
@Gtt1229
Copy link
Contributor Author

Gtt1229 commented Feb 26, 2026

Thanks. review once if report charts & table also reflects correctly.

Those work, but I've come across another issue with handling the scaling.

Will put this in draft again for now until it's fixed and fully tested. Likely more tomorrow.

…e relavent scaling/template id for the defined meal
@Gtt1229 Gtt1229 marked this pull request as ready for review February 28, 2026 02:40
@Gtt1229
Copy link
Contributor Author

Gtt1229 commented Feb 28, 2026

I've done the following tests with the new logic to pull info from backend when editing a template meal:

  • Add meal and adjust calories
  • Edit meal and adjust calories
  • Edit meal again and adjust calories
  • All of the above properly update the calories for reporting.

@CodeWithCJ CodeWithCJ merged commit 9160b26 into CodeWithCJ:main Feb 28, 2026
6 checks passed
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