Metrics → Customers: only customers who have spent more than $0

A customer counts when their paid invoices' amount_paid, less refunds,
is above zero. Trials ($0 invoices), fully discounted invoices, only-open
invoices and payments refunded in full no longer put someone on the list.

Also ticks the Market Fetch check in TEST.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-30 12:27:27 -07:00
co-authored by Claude Opus 5.5
parent 5451fe6d63
commit 6f2fbdf63a
5 changed files with 74 additions and 17 deletions
+18 -3
View File
@@ -39,9 +39,22 @@ func TestMatches(t *testing.T) {
} {
exec(`INSERT INTO stripe_objects (kind, id, customer, data) VALUES ('customer', ?, ?, ?)`, c.id, c.id, c.data)
if c.id != "cus_unbilled" {
exec(`INSERT INTO stripe_objects (kind, id, customer, status, currency, data) VALUES ('invoice', ?, ?, 'paid', 'usd', '{}')`, "in_"+c.id, c.id)
exec(`INSERT INTO stripe_objects (kind, id, customer, status, currency, data) VALUES ('invoice', ?, ?, 'paid', 'usd', '{"amount_paid":1000}')`, "in_"+c.id, c.id)
}
}
// Customers who never spent anything aren't listed: a $0 trial invoice,
// only an open invoice, or a payment refunded in full.
for _, c := range []struct{ id, status, data string }{
{"cus_trial", "paid", `{"amount_paid":0}`},
{"cus_open", "open", `{"amount_paid":0}`},
{"cus_refunded", "paid", `{"amount_paid":500}`},
} {
exec(`INSERT INTO stripe_objects (kind, id, customer, data) VALUES ('customer', ?, ?, '{"name":"X","email":"x@x.example"}')`, c.id, c.id)
exec(`INSERT INTO stripe_objects (kind, id, customer, status, currency, data) VALUES ('invoice', ?, ?, ?, 'usd', ?)`, "in_"+c.id, c.id, c.status, c.data)
}
exec(`INSERT INTO stripe_objects (kind, id, customer, status, currency, amount, data) VALUES ('refund', 're_1', 'cus_refunded', 'succeeded', 'usd', 500, '{}')`)
// A partial refund leaves them a customer.
exec(`INSERT INTO stripe_objects (kind, id, customer, status, currency, amount, data) VALUES ('refund', 're_2', 'cus_beta', 'succeeded', 'usd', 400, '{}')`)
exec(`INSERT INTO crm_actions (key, kind, entity, fields, status, espo_id) VALUES ('stripe-customer:cus_new', 'create', 'Account', '{"name":"Newco"}', 'done', 'a9')`)
ms, err := Matches(d, nil)
@@ -52,8 +65,10 @@ func TestMatches(t *testing.T) {
for _, m := range ms {
got[m.Customer.ID] = m
}
if _, ok := got["cus_unbilled"]; ok {
t.Error("a customer never billed is listed")
for _, id := range []string{"cus_unbilled", "cus_trial", "cus_open", "cus_refunded"} {
if _, ok := got[id]; ok {
t.Errorf("%s spent nothing, but is listed", id)
}
}
for id, want := range map[string]struct{ account, how string }{
"cus_ann": {"a1", MatchEmail}, "cus_beta": {"a2", MatchStoredID}, "cus_meta": {"a1", MatchStoredID},