Phase 2 part 3: buying signals, the funnel and the scorecard

Schema 7 adds signals (hypothesis, how to spot it, angle, status, utm
slug), funnel_events (stage, channel, subject, campaign, source, deduped
by external ID) and outside_campaigns. Prospects -> Signals is the
scorecard: each signal's funnel from found to paid, people once per
stage, with revenue and outside spend, by period and channel. Each
signal's page shows its funnel by channel, its campaigns and its latest
steps. Prospects carry a signal (filter, change, "found" recorded) and
outcomes can be recorded by hand.

Settings -> Lead sources holds origami.chat and explee.com API keys. Every
6 hours (or Import now) Supermarket reads their campaigns, numbers and
spend, contacted people, replies and hot leads, read-only: interested
people and repliers become prospects, unsubscribes are suppressed, and
each campaign is assigned to a signal. The explee.com client follows its
OpenAPI schema; origami.chat's responses are undocumented, so its client
reads fields loosely and its Test shows the real response.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-21 18:06:58 -07:00
co-authored by Claude Opus 5.5
parent ee579ec9f4
commit b025a97e4a
27 changed files with 3031 additions and 53 deletions
+74 -12
View File
@@ -9,6 +9,7 @@ import (
"supermarket/internal/crm"
"supermarket/internal/prospects"
"supermarket/internal/signals"
)
// newProspects is how many companies and people wait for review.
@@ -31,11 +32,21 @@ func reviewTab(v string) string {
}
func (s *Server) prospectsPage(w http.ResponseWriter, r *http.Request) {
s.renderProspects(w, r, reviewTab(r.URL.Query().Get("status")), "")
sig, _ := strconv.ParseInt(r.URL.Query().Get("signal"), 10, 64)
s.renderProspects(w, r, reviewTab(r.URL.Query().Get("status")), sig, "")
}
func (s *Server) renderProspects(w http.ResponseWriter, r *http.Request, tab, errMsg string) {
companies, loose, err := prospects.Review(s.DB, tab, 200)
// reviewURL is the review screen for a tab and a signal filter.
func reviewURL(tab string, signalID int64) string {
u := "/prospects?status=" + tab
if signalID != 0 {
u += "&signal=" + strconv.FormatInt(signalID, 10)
}
return u
}
func (s *Server) renderProspects(w http.ResponseWriter, r *http.Request, tab string, signalID int64, errMsg string) {
companies, loose, err := prospects.Review(s.DB, tab, signalID, 200)
if err != nil {
s.serverError(w, err)
return
@@ -72,14 +83,53 @@ func (s *Server) renderProspects(w http.ResponseWriter, r *http.Request, tab, er
s.serverError(w, err)
return
}
list, err := signals.List(s.DB)
if err != nil {
s.serverError(w, err)
return
}
names := map[int64]string{}
for _, sg := range list {
names[sg.ID] = sg.Name
}
// Each prospect's funnel so far.
var subjects []string
for _, c := range companies {
subjects = append(subjects, prospects.Subject("company", c.ID))
for _, p := range c.Contacts {
subjects = append(subjects, prospects.Subject("contact", p.ID))
}
}
for _, p := range loose {
subjects = append(subjects, prospects.Subject("contact", p.ID))
}
evs, err := signals.SubjectEvents(s.DB, subjects...)
if err != nil {
s.serverError(w, err)
return
}
events := map[string][]signals.Event{}
for _, e := range evs {
if e.Stage != signals.StageFound {
events[e.Subject] = append(events[e.Subject], e)
}
}
s.render(w, r, "prospects", page{Title: "Review prospects", Nav: "prospects/review", Error: errMsg, Data: map[string]any{
"Tab": tab,
"Statuses": prospects.Statuses,
"Counts": counts,
"Companies": companies,
"Loose": loose,
"Matches": matches,
"Synced": sync.Last,
"Tab": tab,
"Statuses": prospects.Statuses,
"Counts": counts,
"Companies": companies,
"Loose": loose,
"Matches": matches,
"Synced": sync.Last,
"Signals": list,
"SignalNames": names,
"Signal": signalID,
"Events": events,
"Stages": signals.Stages[1:], // found is recorded on its own
"Channels": signals.Channels,
"Back": reviewURL(tab, signalID),
"Today": time.Now().Format("2006-01-02"),
}})
}
@@ -96,9 +146,15 @@ func (s *Server) prospectAction(w http.ResponseWriter, r *http.Request) {
return
}
tab := reviewTab(r.PostFormValue("tab"))
filter, _ := strconv.ParseInt(r.PostFormValue("filter"), 10, 64)
now := time.Now()
var msg string
switch kind + "/" + action {
case "company/signal", "contact/signal":
sig, _ := strconv.ParseInt(r.PostFormValue("signal"), 10, 64)
err, msg = prospects.SetSignal(s.DB, kind, id, sig), "saved"
case "company/outcome", "contact/outcome":
err, msg = s.prospectOutcome(w, r, kind, id, tab), "outcome"
case "company/accept":
err, msg = ignore(prospects.AcceptCompany(s.DB, id, now)), "accepted"
case "company/accept-all":
@@ -140,10 +196,10 @@ func (s *Server) prospectAction(w http.ResponseWriter, r *http.Request) {
return
}
if err != nil {
s.renderProspects(w, r, tab, "Couldn't do that: "+err.Error())
s.renderProspects(w, r, tab, filter, "Couldn't do that: "+err.Error())
return
}
redirect(w, r, "/prospects?status="+tab, msg)
redirect(w, r, reviewURL(tab, filter), msg)
}
// ignore drops a result and keeps the error.
@@ -152,6 +208,7 @@ func ignore[T any](_ T, err error) error { return err }
// --- Adding by hand ---
type prospectFormData struct {
Signals []signals.Signal
Company prospects.Company
Contact prospects.Contact
CompanyID int64 // adding a person at a company already in review
@@ -161,6 +218,8 @@ type prospectFormData struct {
func (s *Server) prospectForm(w http.ResponseWriter, r *http.Request) {
d := prospectFormData{}
d.Signals, _ = signals.List(s.DB)
d.Company.SignalID, _ = strconv.ParseInt(r.URL.Query().Get("signal"), 10, 64)
if id, err := strconv.ParseInt(r.URL.Query().Get("company"), 10, 64); err == nil {
c, err := prospects.GetCompany(s.DB, id)
if err != nil {
@@ -192,6 +251,9 @@ func (s *Server) prospectAdd(w http.ResponseWriter, r *http.Request) {
},
}
d.CompanyID, _ = strconv.ParseInt(f("company_id"), 10, 64)
d.Signals, _ = signals.List(s.DB)
d.Company.SignalID, _ = strconv.ParseInt(f("signal"), 10, 64)
d.Contact.SignalID = d.Company.SignalID
if d.CompanyID != 0 {
c, err := prospects.GetCompany(s.DB, d.CompanyID)
if err != nil {