carlos / platform Public

console: rematch keeps a subscription learned for an unmatched row

Final review finding F (Astra 7, deferred Task 4 minor): RematchPayments
skipped every row whose account stayed 0, so a subscription rule 3 learned
from a charge recorded after its refund was thrown away. The rematch now
writes any change the match made and still counts only rows that gained
an account. TestRematchPaymentsKeepsLearnedSubscription covers it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 8fa2d1363d350c369bfdb77d7b198d8c8b4f6ab2 parent 294a1ea
2 files changed, +36 −4
  • internal/console/billingpayments_store.go +12 −4
  • internal/console/billingpayments_store_test.go +24 −0
diff --git a/internal/console/billingpayments_store.go b/internal/console/billingpayments_store.go
index 9d8f16bc6..2b9b65128 100644
--- a/internal/console/billingpayments_store.go
+++ b/internal/console/billingpayments_store.go
@@ -207,8 +207,10 @@ func (s *Store) HasPayment(movement string) (bool, error) {
}
// RematchPayments runs the account match again for unmatched rows (a
-// customer bound, or a charge recorded, after them) and returns how many
-// now have an account. Fees name no customer and stay unmatched.
+// customer bound, or a charge recorded, after them), keeps whatever the
+// match changed (an account, or a subscription learned from the charge
+// even when the account stays unknown), and returns how many now have an
+// account. Fees name no customer and stay unmatched.
func (s *Store) RematchPayments() (int, error) {
tx, err := s.db.Begin()
if err != nil {
@@ -237,10 +239,14 @@ func (s *Store) RematchPayments() (int, error) {
}
matched := 0
for _, r := range unmatched {
+ before := r
if err := matchPayment(tx, &r); err != nil {
return 0, err
}
- if r.AccountID == 0 {
+ // A row that stays unmatched may still have learned something (rule
+ // 3: its charge's subscription): keep any change, count only rows
+ // that gained an account.
+ if r == before {
continue
}
if _, err := tx.Exec(`UPDATE billing_payments SET account_id = ?, reason = ?, detail = ?, subscription = ?,
@@ -248,7 +254,9 @@ func (s *Store) RematchPayments() (int, error) {
r.AccountID, r.Reason, r.Detail, r.Subscription, r.Tax, r.SettledTax, r.Movement); err != nil {
return 0, err
}
- matched++
+ if r.AccountID != 0 {
+ matched++
+ }
}
return matched, tx.Commit()
}
diff --git a/internal/console/billingpayments_store_test.go b/internal/console/billingpayments_store_test.go
index 9870ddd65..a4cf4b94a 100644
--- a/internal/console/billingpayments_store_test.go
+++ b/internal/console/billingpayments_store_test.go
@@ -416,3 +416,27 @@ func TestRecordPaymentRefundTakesUnmatchedChargeSubscription(t *testing.T) {
t.Errorf("refund account %d subscription %q; want 0, sub_9", got.AccountID, got.Subscription)
}
}
+
+// A rematch keeps a subscription rule 3 learns from an unmatched charge
+// recorded after the refund, though the refund stays unmatched; it does
+// not count as a row that gained an account.
+func TestRematchPaymentsKeepsLearnedSubscription(t *testing.T) {
+ s, _ := planStore(t, "bab")
+ recordPayment(t, s, billing.Payment{
+ Movement: "mv_re", Source: "re_1", Kind: billing.PaymentRefund, Reason: billing.ReasonOther,
+ Charge: "ch_1", Amount: -600, Tax: -100, Currency: "eur",
+ Settled: -600, SettledTax: -100, SettledCurrency: "eur", At: paymentAt.Add(time.Hour),
+ })
+ c := testCharge("mv_1", "ch_1")
+ c.Customer, c.Subscription = "cus_nobody", "sub_9"
+ recordPayment(t, s, c)
+ if got := paymentByMovement(t, s, "mv_re"); got.Subscription != "" {
+ t.Fatalf("refund subscription %q before the rematch, want none", got.Subscription)
+ }
+ if n, err := s.RematchPayments(); err != nil || n != 0 {
+ t.Fatalf("rematch = %d, %v; want 0 (no row gained an account)", n, err)
+ }
+ if got := paymentByMovement(t, s, "mv_re"); got.AccountID != 0 || got.Subscription != "sub_9" {
+ t.Errorf("refund account %d subscription %q; want 0, sub_9", got.AccountID, got.Subscription)
+ }
+}