Skip to content

[MPDX-10004] - Block savings fund transfers that would overdraw the account - #2059

Open
tjohnson009 wants to merge 6 commits into
mainfrom
MPDX-10004-Block-Negative-Transfers
Open

tjohnson009 wants to merge 6 commits into
mainfrom
MPDX-10004-Block-Negative-Transfers

Conversation

@tjohnson009

Copy link
Copy Markdown
Contributor

Description

Prevents savings fund transfers from taking an account balance below $0 (MPDX-10004).

Per Scott (via stakeholder Crystal): a transfer can never take an account below $0 — fund deficitLimits apply to salary, not savings fund transfers. Recurring transfers get a warning rather than a block, since their payments process in the future against future balances; each payment is enforced at processing time.

Behavior

  • New one-time transfer — hard-blocked when the amount exceeds the source fund's available balance: field error ("Amount cannot exceed the available balance of $X") and disabled Submit. Comparison is at cent precision, fails closed if the fund's balance can't be resolved, and transferring the exact full balance is allowed.
  • New recurring transfer — non-blocking warning showing the projected balance after the first scheduled payment.
  • Edits (recurring only) — same non-blocking warning, so an over-balance recurring transfer can still be wound down or stopped.
  • BalanceCard — the "Transfer From" button now gates on the same shared availableBalance helper (endBalance <= 0) instead of the old deficit-limit check, so the entry point and the form enforce the same rule.

The balance policy lives in one place (Helper/availableBalance.ts) with the $0 floor documented, including a warning about the two conflicting deficitLimit sign conventions found in the tree.

Not covered client-side (server-side enforcement question)

The form check can't catch a recurring transfer that overdraws in a later month, stacked pending transfers that jointly overdraw, or a balance that changed after page load. Whether SAA rejects these at processing time is pending confirmation with Ethan Tison — if it doesn't, a backend ticket is needed.

Checklist:

  • All 162 tests across the 16 SavingsFundTransfer suites pass; new coverage for the one-cent boundary (blocked), exact-full-balance (allowed), deficit-limit funds (still blocked below $0), recurring/edit warning paths (submittable), and the edit wind-down flow
  • eslint and tsc clean

🤖 Generated with Claude Code

…ount

One-time transfers over the source fund's available balance are now
blocked at the form level ($0 floor, cent-precision comparison,
fail-closed when the fund balance cannot be resolved). Recurring
transfers and edits show a non-blocking overdraft warning instead,
since their payments process in the future against future balances.
The BalanceCard transfer button now gates on the same shared
availableBalance helper instead of the old deficit-limit check.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@tjohnson009 tjohnson009 added On Staging Will be merged to the staging branch by Github Actions Preview Environment Add this label to create an Amplify Preview labels Sep 18, 2026
@github-actions

Copy link
Copy Markdown
Contributor

@github-actions

github-actions Bot commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

Bundle sizes [mpdx-react]

Compared against 06ca668

No significant changes found

@tjohnson009

Copy link
Copy Markdown
Contributor Author

Pre-PR code review findings (multi-agent review of the initial implementation; all addressed in this PR unless noted):

  1. Edit-flow deadlock — an over-balance recurring transfer couldn't be saved at all (even just to add an end date), with the error invisible when the amount field was untouched. Fixed: edits warn instead of block (matches Scott's ruling).
  2. Fail-open validation — the check silently passed when the source fund wasn't in the loaded funds list (query error, group-filtered fund types). A pre-existing test was green only via this hole. Fixed: fails closed for one-time creates; test fixture corrected.
  3. Recurring judged against today's balance — blocked legitimate future schedules, missed month-N overdrafts. Resolved by Scott: recurring warns on the first payment; per-payment enforcement is server-side.
  4. Entry point / form rule mismatch — BalanceCard still gated its button on the old deficit-limit rule. Fixed: both use the shared availableBalance helper.
  5. Float precision — a balance like 14999.9999999998 displays as $15,000.00 but rejected typing 15000. Fixed: cent-precision compare.
  6. deficitLimit sign convention — the tree contained both "negative floor" and "positive magnitude" readings. Documented in the helper; not otherwise touched.
  7. Test gaps — assertions that couldn't fail, no edit-mode coverage, unanchored boundary input. Fixed.
  8. Server-side parity (open) — the client check can't catch stale balances, stacked pending transfers, or later-month recurring overdrafts. Pending confirmation with Ethan Tison whether SAA enforces the floor at processing time; backend ticket if not.

🤖 Generated with Claude Code

@kegrimes
kegrimes removed their request for review September 24, 2026 13:05

@dr-bizz dr-bizz left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Good work on this! I think it's best that we use the fund's deficitLimit still, but the rest of the code looks great.

Comment thread src/components/HrTools/SavingsFundTransfer/Helper/availableBalance.ts Outdated
tjohnson009 and others added 4 commits October 7, 2026 13:22
…ance.ts

Co-authored-by: Bizz (Daniel Bisgrove) <56281168+dr-bizz@users.noreply.github.com>
…tive-Transfers

# Conflicts:
#	src/components/HrTools/SavingsFundTransfer/BalanceCard/BalanceCard.tsx
#	src/components/HrTools/SavingsFundTransfer/TransferModal/TransferModal.test.tsx
#	src/components/HrTools/SavingsFundTransfer/TransferModal/TransferModal.tsx
Completes the review suggestion: the applied suggestion block was an
arrow function with braces and no return, so minimumAllowedBalance
returned undefined and availableBalance was NaN, silently disabling the
block. Return the expression, update the helper comment, and rewrite
the deficit-limit test to assert the new floor (a -$1,000 limit makes
$16,000 available on a $15,000 balance). Also restore the `within`
import that the main merge needed for the HR-managed funds test.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
SAA stores deficit_limit as a positive magnitude and rejects a
withdrawal when balance - amount < -deficit_limit.abs
(staff_accounting_app Fund#withdrawal_would_violate_deficit?; pinned by
its spec suite, mirrored by mpdx_api's asr_max_calculation and by the
TransferModal warning on main). The previous floor used +deficitLimit,
which was too restrictive by twice the limit and rejected transfers SAA
itself accepts. Mirror SAA with a -Math.abs floor, add direct unit
tests for the helper, and cover both mock sign conventions in the
modal tests. BalanceCard's long-standing disable check is deliberately
left alone for a follow-up.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
// is the NEGATIVE of the limit; Math.abs keeps legacy negative-convention
// values correct too. Defaults to a $0 floor when the fund has no limit.
export const minimumAllowedBalance = (fund: FundFieldsFragment): number =>
fund.deficitLimit ? -Math.abs(fund.deficitLimit) : 0;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
fund.deficitLimit ? -Math.abs(fund.deficitLimit) : 0;
fund.deficitLimit ? Math.abs(fund.deficitLimit) : 0;


// The most that can be transferred out of a fund right now.
export const availableBalance = (fund: FundFieldsFragment): number =>
fund.endBalance - minimumAllowedBalance(fund);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If the primary account balance is $1,000 and the deficit is $600. Will have $1,600 available.

Suggested change
fund.endBalance - minimumAllowedBalance(fund);
fund.endBalance + minimumAllowedBalance(fund);


// The most that can be transferred out of a fund right now.
export const availableBalance = (fund: FundFieldsFragment): number =>
fund.endBalance - minimumAllowedBalance(fund);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You might also want to account for pending ASR amounts. This will be approved but not yet paid ASRs. But this might be an additional PR and out of scope for this PR.

Apply the review suggestions: minimumAllowedBalance returns the
positive limit magnitude and availableBalance adds it, matching the
shape asr_max_calculation uses on the API (mpdx_api#3666). Same
arithmetic as before — available = balance + |deficitLimit| — per
SAA's balance - amount >= -(deficit_limit).abs rule.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

On Staging Will be merged to the staging branch by Github Actions Preview Environment Add this label to create an Amplify Preview

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants