Skip to content

Conversation

@Uli-Z
Copy link
Contributor

@Uli-Z Uli-Z commented Jul 1, 2025

Summary
This pull request addresses a critical data inconsistency bug that occurs when an expense is set to be split evenly, but a default (uneven) splitting configuration also exists. The bug caused incorrect amounts
to be calculated in features like the CSV export because the underlying shares for participants were not being correctly normalized.

This PR introduces a fix in the core API logic to ensure data is stored correctly and adds a database migration to clean up any existing inconsistent data.

Problem Description
When a user creates or updates an expense and selects the "Split Evenly" option, the application should divide the cost equally among all selected participants. However, if a default splitting configuration
with uneven shares was previously set for those participants, the application was incorrectly preserving these old, uneven shares in the database while setting the splitMode to EVENLY.

This resulted in a contradictory state where an expense was marked as "evenly split" but had data indicating an uneven distribution. This incorrect data was then used in the CSV export, leading to confusing and
inaccurate reports for users.

Solution Implemented
The solution is two-fold to ensure correctness for both new and existing data:

  1. API Logic Correction: The createExpense and updateExpense functions have been updated to enforce data consistency. Before creating or updating an expense, the code now checks if the splitMode is EVENLY. If it
    is, it programmatically overrides any incoming shares and sets them to 1 for all participants involved. This ensures that the data saved to the database is always correct from this point forward.

  2. Data Migration for Existing Entries: A new Prisma migration has been added to clean up existing records in the database. The migration runs a single SQL UPDATE statement that finds all expenses with splitMode
    = 'EVENLY' and normalizes the shares in the corresponding ExpensePaidFor table to 1. This is a one-time fix that ensures historical data is also corrected.

@Uli-Z
Copy link
Contributor Author

Uli-Z commented Jul 1, 2025

see also #375

@Uli-Z Uli-Z force-pushed the bugfix-incorrect-default-shares-evenly-mode branch from f42b279 to 5551676 Compare November 11, 2025 19:32
This commit addresses a data inconsistency issue where expenses with a split mode of 'EVENLY' could have non-normalized shares, leading to calculation errors in features like CSV exports.

This is a comprehensive fix that includes two parts:

1.  **Application-level fix:**
    The createExpense and updateExpense functions in src/lib/api.ts are updated to explicitly check if the split mode is 'EVENLY'. If it is, the shares for all participants are normalized to 1, ensuring that all new and updated expenses are stored with consistent data.

2.  **Data migration:**
    A new data migration is introduced to clean up existing inconsistent data. The migration updates the shares to 1 for all ExpensePaidFor entries associated with an Expense where splitMode is 'EVENLY'.

Together, these changes guarantee data integrity for all 'EVENLY' split expenses, both past and future.
@Uli-Z Uli-Z force-pushed the bugfix-incorrect-default-shares-evenly-mode branch from 5551676 to abd3df6 Compare December 7, 2025 19:31
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.

1 participant