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:
2026-11-22 12:22:05 -07:00
co-authored by Claude Opus 5.5
parent 5e799c7541
commit 6ba524295b
13 changed files with 675 additions and 2 deletions
+28
View File
@@ -9,6 +9,7 @@ import (
"testing"
"time"
"supermarket/internal/crm"
"supermarket/internal/profile"
"supermarket/internal/secrets"
"supermarket/internal/seo"
@@ -109,3 +110,30 @@ func TestMRRPages(t *testing.T) {
t.Errorf("bad month: %s", rec.Body)
}
}
func TestCustomersPage(t *testing.T) {
s, h := newTestServer(t, nil)
s.DB.Exec(`INSERT INTO crm_records (entity, espo_id, name, email, domain, data, synced_at) VALUES ('Account', 'a1', 'Acme', '', 'acme.example', '{}', '')`)
s.DB.Exec(`INSERT INTO stripe_objects (kind, id, customer, data) VALUES ('customer', 'cus_x', 'cus_x', '{"name":"Xco","email":"x@xco.example"}'),
('customer', 'cus_a', 'cus_a', '{"name":"Acme Ltd","email":"pay@acme.example"}')`)
s.DB.Exec(`INSERT INTO stripe_objects (kind, id, customer, status, currency, data) VALUES ('invoice', 'in_1', 'cus_x', 'paid', 'usd', '{}'), ('invoice', 'in_2', 'cus_a', 'paid', 'usd', '{}')`)
body := do(h, http.MethodGet, "/metrics/customers", nil, nil).Body.String()
if !strings.Contains(body, "Xco") || strings.Contains(body, "Acme Ltd") {
t.Fatal("the to-fix list should hold only the unmatched customer")
}
postForm(h, "/metrics/customers/cus_x", url.Values{"do": {"account"}, "account": {"Acme"}})
if body := do(h, http.MethodGet, "/metrics/customers?show=all", nil, nil).Body.String(); !strings.Contains(body, "Acme <span class=\"muted small\">by you</span>") {
t.Error("the manual match isn't shown")
}
postForm(h, "/metrics/customers/cus_x", url.Values{"do": {"auto"}})
postForm(h, "/metrics/customers/cus_x", url.Values{"do": {"add"}})
if acts, _ := crm.List(s.DB, 10); len(acts) != 1 || acts[0].Entity != "Account" || acts[0].Fields["website"] != "https://xco.example" {
t.Errorf("queued: %+v", acts)
}
if body := do(h, http.MethodGet, "/metrics/customers", nil, nil).Body.String(); !strings.Contains(body, "queued for the CRM") {
t.Error("the queued Account isn't shown")
}
if rec := postForm(h, "/metrics/customers/bad", url.Values{"do": {"none"}}); rec.Code != http.StatusNotFound {
t.Errorf("bad id: %d", rec.Code)
}
}