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>
110 lines
4.8 KiB
Go
110 lines
4.8 KiB
Go
package metrics
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestMatches(t *testing.T) {
|
|
d := openDB(t)
|
|
now := time.Now()
|
|
exec := func(q string, args ...any) {
|
|
t.Helper()
|
|
if _, err := d.Exec(q, args...); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
// The CRM: Acme (with a Contact), Beta (holding a Stripe ID), two
|
|
// Accounts at one domain, and one at gmail.com.
|
|
for _, r := range []struct{ entity, id, name, email, domain, data string }{
|
|
{"Account", "a1", "Acme", "", "acme.example", `{}`},
|
|
{"Contact", "c1", "Ann", "ann@acme.example", "acme.example", `{"accountId":"a1"}`},
|
|
{"Account", "a2", "Beta", "", "beta.example", `{"cStripeCustomerId":"cus_beta"}`},
|
|
{"Account", "a3", "Twin One", "", "twin.example", `{}`},
|
|
{"Account", "a4", "Twin Two", "", "twin.example", `{}`},
|
|
{"Account", "a5", "Solo", "solo@gmail.com", "gmail.com", `{}`},
|
|
} {
|
|
exec(`INSERT INTO crm_records (entity, espo_id, name, email, domain, data, synced_at) VALUES (?, ?, ?, ?, ?, ?, '')`,
|
|
r.entity, r.id, r.name, r.email, r.domain, r.data)
|
|
}
|
|
for _, c := range []struct{ id, data string }{
|
|
{"cus_ann", `{"name":"Ann","email":"ANN@acme.example"}`},
|
|
{"cus_beta", `{"name":"Beta Inc","email":"x@elsewhere.example"}`},
|
|
{"cus_meta", `{"name":"Meta","email":"m@meta.example","metadata":{"espocrm_id":"a1"}}`},
|
|
{"cus_twin", `{"name":"Twin","email":"t@twin.example"}`},
|
|
{"cus_gm", `{"name":"G","email":"other@gmail.com"}`},
|
|
{"cus_free", `{"name":"Free","email":"free@nowhere.example"}`},
|
|
{"cus_unbilled", `{"name":"Never billed","email":"ann@acme.example"}`},
|
|
{"cus_new", `{"name":"Newco","email":"hi@newco.example"}`},
|
|
} {
|
|
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', '{"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)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got := map[string]Match{}
|
|
for _, m := range ms {
|
|
got[m.Customer.ID] = m
|
|
}
|
|
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},
|
|
"cus_twin": {"", MatchAmbiguous}, "cus_gm": {"", MatchNone}, "cus_free": {"", MatchNone}, "cus_new": {"a9", MatchStoredID},
|
|
} {
|
|
if m := got[id]; m.Account.ID != want.account || m.How != want.how {
|
|
t.Errorf("%s: %+v, want %s by %q", id, m, want.account, want.how)
|
|
}
|
|
}
|
|
if len(got["cus_twin"].Candidates) != 2 || got["cus_new"].Account.Name != "Newco" {
|
|
t.Errorf("twin: %+v; new: %+v", got["cus_twin"], got["cus_new"])
|
|
}
|
|
// The user decides.
|
|
SetMatch(d, "cus_twin", "a4", false, now)
|
|
SetMatch(d, "cus_free", "", false, now)
|
|
SetMatch(d, "cus_ann", "a5", false, now)
|
|
ms, _ = Matches(d, nil)
|
|
for _, m := range ms {
|
|
got[m.Customer.ID] = m
|
|
}
|
|
if got["cus_twin"].Account.Name != "Twin Two" || got["cus_twin"].How != MatchManual || got["cus_free"].How != MatchNotInCRM || got["cus_ann"].Account.ID != "a5" {
|
|
t.Errorf("manual: %+v %+v %+v", got["cus_twin"], got["cus_free"], got["cus_ann"])
|
|
}
|
|
SetMatch(d, "cus_ann", "", true, now)
|
|
ms, _ = Matches(d, nil)
|
|
for _, m := range ms {
|
|
got[m.Customer.ID] = m
|
|
}
|
|
if got["cus_ann"].How != MatchEmail {
|
|
t.Errorf("back to automatic: %+v", got["cus_ann"])
|
|
}
|
|
if s := MatchSummary(ms); s[MatchStoredID] != 3 || s[MatchManual] != 1 || s[MatchNone] != 1 {
|
|
t.Errorf("summary: %v", s)
|
|
}
|
|
if as, _ := Accounts(d); len(as) != 5 || as[0].Name != "Acme" {
|
|
t.Errorf("accounts: %+v", as)
|
|
}
|
|
}
|