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:
@@ -8,6 +8,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"supermarket/internal/crm"
|
||||
"supermarket/internal/metrics"
|
||||
)
|
||||
|
||||
@@ -194,3 +195,127 @@ func (s *Server) RunMetrics(ctx context.Context) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- Stripe customers and CRM Accounts ---
|
||||
|
||||
func (s *Server) metricsCustomers(w http.ResponseWriter, r *http.Request) {
|
||||
s.renderCustomers(w, r, "")
|
||||
}
|
||||
|
||||
func (s *Server) renderCustomers(w http.ResponseWriter, r *http.Request, errMsg string) {
|
||||
model, err := metrics.Build(s.DB, time.Now())
|
||||
if err != nil {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
all, err := metrics.Matches(s.DB, model)
|
||||
if err != nil {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
show := r.URL.Query().Get("show")
|
||||
var shown []metrics.Match
|
||||
for _, m := range all {
|
||||
open := m.How == metrics.MatchNone || m.How == metrics.MatchAmbiguous
|
||||
if show == "all" || (show == "" && open) || (show == "paying" && m.MRR > 0) {
|
||||
shown = append(shown, m)
|
||||
}
|
||||
}
|
||||
accounts, err := metrics.Accounts(s.DB)
|
||||
if err != nil {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
counts, _ := crm.RecordCounts(s.DB)
|
||||
sync, _ := crm.LoadSync(s.DB)
|
||||
queued := map[string]bool{}
|
||||
if acts, err := crm.List(s.DB, 1000); err == nil {
|
||||
for _, a := range acts {
|
||||
if strings.HasPrefix(a.Key, "stripe-customer:") && a.Status != "cancelled" {
|
||||
queued[strings.TrimPrefix(a.Key, "stripe-customer:")] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
s.render(w, r, "metrics-customers", page{Title: "Customers and the CRM", Nav: "metrics/customers", Error: errMsg, Data: map[string]any{
|
||||
"Matches": shown, "Total": len(all), "Summary": metrics.MatchSummary(all), "Accounts": accounts,
|
||||
"CRMCount": counts["Account"], "Synced": sync.Last, "Show": show, "Queued": queued,
|
||||
}})
|
||||
}
|
||||
|
||||
// metricsCustomerMatch handles a customer's buttons: choose an Account, say
|
||||
// it isn't in the CRM, go back to automatic, or add it to the CRM.
|
||||
func (s *Server) metricsCustomerMatch(w http.ResponseWriter, r *http.Request) {
|
||||
if err := r.ParseForm(); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
cust := r.PathValue("id")
|
||||
if !strings.HasPrefix(cust, "cus_") {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
now := time.Now()
|
||||
var err error
|
||||
switch r.PostFormValue("do") {
|
||||
case "account":
|
||||
acc := strings.TrimSpace(r.PostFormValue("account"))
|
||||
// The box takes a name; find its Account.
|
||||
if accounts, aerr := metrics.Accounts(s.DB); aerr == nil {
|
||||
for _, a := range accounts {
|
||||
if strings.EqualFold(a.Name, acc) || a.ID == acc {
|
||||
acc = a.ID
|
||||
}
|
||||
}
|
||||
}
|
||||
if acc == "" {
|
||||
err = errors.New("choose an Account")
|
||||
} else {
|
||||
err = metrics.SetMatch(s.DB, cust, acc, false, now)
|
||||
}
|
||||
case "none":
|
||||
err = metrics.SetMatch(s.DB, cust, "", false, now)
|
||||
case "auto":
|
||||
err = metrics.SetMatch(s.DB, cust, "", true, now)
|
||||
case "add":
|
||||
err = s.queueAccount(cust)
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
s.renderCustomers(w, r, err.Error())
|
||||
return
|
||||
}
|
||||
redirect(w, r, "/metrics/customers?show="+r.PostFormValue("show"), "saved")
|
||||
}
|
||||
|
||||
// queueAccount asks Hermes to add a paying Stripe customer to the CRM as an
|
||||
// Account (and remembers the match once it exists).
|
||||
func (s *Server) queueAccount(cust string) error {
|
||||
names, err := metrics.CustomerNames(s.DB)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c, ok := names[cust]
|
||||
if !ok {
|
||||
return errors.New("no such customer")
|
||||
}
|
||||
name := c.Name
|
||||
if name == "" {
|
||||
name = c.Email
|
||||
}
|
||||
if name == "" {
|
||||
return errors.New("the customer has no name or email to create an Account from")
|
||||
}
|
||||
fields := map[string]any{"name": name, "description": "Added by Supermarket: a paying Stripe customer (" + cust + ")."}
|
||||
match := map[string]string{"name": name}
|
||||
if dom := crm.EmailDomain(c.Email); dom != "" && !crm.IsFreeMail(dom) {
|
||||
fields["website"] = "https://" + dom
|
||||
match = map[string]string{"website": "https://" + dom}
|
||||
}
|
||||
if c.Email != "" {
|
||||
fields["emailAddress"] = c.Email
|
||||
}
|
||||
_, err = crm.Enqueue(s.DB, crm.Action{Key: "stripe-customer:" + cust, Entity: "Account", Fields: fields, Match: match, Origin: "stripe-customer:" + cust})
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user