Skip to content

fix: match Amazon card charges and anchor Monarch discovery to the order - #99

Merged
eshaffer321 merged 1 commit into
mainfrom
fix/amazon-charge-matching
Sep 27, 2026
Merged

eshaffer321 merged 1 commit into
mainfrom
fix/amazon-charge-matching

Conversation

@eshaffer321

Copy link
Copy Markdown
Owner

Fixes two Amazon sync errors and a latent wrong-match bug found while tracing them.

What was wrong

Order Symptom Cause
112-3421348-5157859 Error: could not find all transactions: expected 2, found 2 Only $19.48 of the $13.77 + $19.48 charges had posted. The handler counted nil placeholders in MultiMatchResult.Matches as matches, and reported a partial match as an error.
112-7815140-3755432 Error: bank charges ($59.36) exceed expected ($55.31) Amazon charged the card $59.36; a $4.05 discount on the order wasn't applied to the charge. The handler never tried the reported card charge, which was in Monarch, and only searched for transactions summing to the order total.
112-7815140-3755432, synced alone Matched $25.44 + $29.87, charges from two other orders FindSubsetByTotal accepts any subset that sums to the total. The only thing preventing this in the full run was which orders happened to claim those transactions first.

Changes

  • Count only non-nil matches, and skip a partial multi-charge match as payment pending rather than an error.
  • When card charges meet or exceed the expected amount, match them against Monarch directly and allocate using the charged amount.
  • Add Matcher.FindSubsetByTotalIncluding: discovery must include at least one charge Amazon reported for the order. Walmart and the simple handler keep the old FindSubsetByTotal behavior.

Testing

  • Failing tests written first, using the real order amounts (see docs/bug-fixes.md); each failed against main for the reason above.
  • go test ./... -race and golangci-lint pass. Coverage: matcher 83.4% → 91.0%, handlers 74.0% → 75.0%.
  • itemize amazon -account wife -dry-run: Errors 2 → 0. 112-3421348-5157859 is pending (expected=2 found=1), and 112-7815140-3755432 matches the $59.36 transaction.

- Count only non-nil multi-charge matches (fixes 'expected 2, found 2').
- Treat a partial multi-charge match as payment pending instead of an error.
- When card charges exceed the order total, match them directly before
  falling back to discovery.
- Require discovered subsets to include a charge Amazon reported for the
  order, so unrelated orders' charges cannot be combined to fill the total.
@codecov

codecov Bot commented Sep 27, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 84.53608% with 15 lines in your changes missing coverage. Please review.
✅ Project coverage is 65.37%. Comparing base (82e976b) to head (8d654f4).

Files with missing lines Patch % Lines
internal/application/sync/handlers/amazon.go 83.11% 10 Missing and 3 partials ⚠️
internal/domain/matcher/subset.go 90.00% 2 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main      #99      +/-   ##
==========================================
+ Coverage   64.99%   65.37%   +0.38%     
==========================================
  Files          51       51              
  Lines        6990     7036      +46     
==========================================
+ Hits         4543     4600      +57     
+ Misses       2108     2094      -14     
- Partials      339      342       +3     
Flag Coverage Δ
unittests 65.37% <84.53%> (+0.38%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
internal/domain/matcher/subset.go 81.81% <90.00%> (+22.99%) ⬆️
internal/application/sync/handlers/amazon.go 70.71% <83.11%> (+4.44%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@eshaffer321
eshaffer321 merged commit 0d7a6aa into main Sep 27, 2026
14 checks passed
@eshaffer321
eshaffer321 deleted the fix/amazon-charge-matching branch September 27, 2026 03:09
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