Skip to content

bugfix: Fix share values being incorrectly divided by 100 in expense form#453

Merged
Petersmit27 merged 2 commits intospliit-app:mainfrom
derekl-beep:bugfix/fix-by-share-#424
Nov 8, 2025
Merged

bugfix: Fix share values being incorrectly divided by 100 in expense form#453
Petersmit27 merged 2 commits intospliit-app:mainfrom
derekl-beep:bugfix/fix-by-share-#424

Conversation

@derekl-beep
Copy link
Copy Markdown
Contributor

This PR builds on #429 by adding type assertions to address TypeScript errors in the expense form. It potentially resolves issue #424.


Problem

When creating or editing expenses with split modes BY_SHARES, or BY_PERCENTAGE, share values were sometimes incorrectly divided by 100 upon saving. For example:

  • A user enters a share value of 1
  • After saving and reopening, it displays as 0.01
  • Subsequent saves compound the error: 0.010.0001

This bug occurred specifically when:

  1. Editing an existing expense without modifying the share values
  2. Loading and using default splitting options from localStorage
  3. Creating a new expense with default share values (not manually edited)

Root Cause

The issue was caused by inconsistent type handling in the form and schema transformation:

  • String values (user input) → multiplied by 100 → correctly saved as basis points ✅
  • Number values (loaded from DB/localStorage) → bypassed transformation → incorrectly saved as-is ❌

The schema transformation used a type-based check that only applied the * 100 multiplication to string values, assuming numbers were already in storage format. However, form initialization converted storage format (basis points) to display format (decimals) as numbers, which then bypassed the transformation on save.

Solution

Ensure all share values in the form are consistently strings in display format:

  1. Convert DB values to strings when loading existing expenses
  2. Convert localStorage values to strings when loading default splitting options
  3. Assign default values as strings when initializing new forms or adding participants
  4. Keep schema transformation type-based to only transform string values (user input)

This guarantees that all share values follow the same flow:

  • Display format string (e.g., "1") → Schema transformation → Storage format number (e.g., 100)

Changes

src/app/groups/[groupId]/expenses/expense-form.tsx

  • Convert DB-loaded share values to strings: (shares / 100).toString()
  • Convert localStorage-loaded share values to strings: (paidFor.shares / 100).toString()
  • Changed default share assignments from 1 to '1' in:
    • Initial default splitting options
    • Reimbursement defaults
    • "Select All/None" button functionality
    • Individual participant checkbox selection

Testing

  • Build succeeds with no TypeScript errors
  • Manually tested scenarios:
    • Create expense with BY_SHARES, save without editing → values preserved ✅
    • Edit existing expense without modifying shares → values preserved ✅
    • Edit existing expense by modifying shares → values preserved ✅

yllar and others added 2 commits September 28, 2025 00:28
Proposing fix to spliit-app#424

The issue is in the data flow between the form and the schema transform function:

When editing existing expenses: Form loads shares by dividing database values by 100 (e.g., 200 / 100 = 2), but loads them as numbers
When users change values: Input fields return strings via enforceCurrencyPattern
Schema transform: Only multiplies by 100 for string values, not number values
Result: Modified shares (strings) get multiplied by 100, unmodified shares (numbers) stay as-is

Proposed fix: handle all shares consistently as strings throughout the form
@Petersmit27
Copy link
Copy Markdown
Collaborator

Thanks for the fix @yllar and @derekl-beep!

I'm not really a fan of all the as anys, but I guess that this is a problem with the schema and the fact that the same schema is used in both the frontend and backend.

I'd love to have this restructured and have a test suite to make sure these things don't break in the future...

@Petersmit27 Petersmit27 merged commit 157ed4f into spliit-app:main Nov 8, 2025
1 check passed
@derekl-beep derekl-beep deleted the bugfix/fix-by-share-#424 branch November 8, 2025 22:27
@derekl-beep
Copy link
Copy Markdown
Contributor Author

agree this should be a quick workaround for the current design. look forward to the restructuring 😄

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.

3 participants