Phase 4 part 3: Stripe customers matched to EspoCRM Accounts
Every customer with billing is matched to an Account copied from the CRM: the user's choice, then an Account Hermes created from here, a stored ID on either side, an email (the Account's or a Contact's), or the email's domain (not free mail). Several candidates wait for a choice. Metrics → Customers and the CRM fixes matches, marks customers not in the CRM, and queues paying ones as new Accounts. Schema 20. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
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', '{}')`, "in_"+c.id, c.id)
|
||||
}
|
||||
}
|
||||
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
|
||||
}
|
||||
if _, ok := got["cus_unbilled"]; ok {
|
||||
t.Error("a customer never billed is listed")
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user