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:
+112
-17
@@ -15,6 +15,7 @@ import (
|
||||
|
||||
"supermarket/internal/crm"
|
||||
"supermarket/internal/db"
|
||||
"supermarket/internal/signals"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -48,6 +49,7 @@ type Company struct {
|
||||
DecidedAt time.Time
|
||||
ActionID int64
|
||||
CRM CRMState
|
||||
SignalID int64 // the buying signal it was found for; 0: none
|
||||
|
||||
Contacts []Contact // filled in by Review and GetCompany
|
||||
}
|
||||
@@ -70,6 +72,7 @@ type Contact struct {
|
||||
DecidedAt time.Time
|
||||
ActionID int64
|
||||
CRM CRMState
|
||||
SignalID int64
|
||||
}
|
||||
|
||||
// CRMState is how the CRM action an accepted prospect queued is doing.
|
||||
@@ -156,13 +159,46 @@ func AddCompany(d *db.DB, c Company, now time.Time) (id int64, added bool, err e
|
||||
}
|
||||
}
|
||||
res, err := d.Exec(`INSERT INTO prospect_companies (name, domain, website, linkedin, industry, size, location, why,
|
||||
source, source_detail, found_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
c.Name, c.Domain, c.Website, c.LinkedIn, c.Industry, c.Size, c.Location, c.Why, c.Source, c.SourceDetail, formatTime(now))
|
||||
source, source_detail, found_at, signal_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
c.Name, c.Domain, c.Website, c.LinkedIn, c.Industry, c.Size, c.Location, c.Why, c.Source, c.SourceDetail, formatTime(now),
|
||||
nullID(c.SignalID))
|
||||
if err != nil {
|
||||
return 0, false, err
|
||||
}
|
||||
id, err = res.LastInsertId()
|
||||
return id, true, err
|
||||
if id, err = res.LastInsertId(); err != nil {
|
||||
return 0, false, err
|
||||
}
|
||||
return id, true, found(d, "company", id, c.SignalID, c.Source, "", now)
|
||||
}
|
||||
|
||||
func nullID(id int64) any {
|
||||
if id == 0 {
|
||||
return nil
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
// Subject is how funnel events refer to a prospect.
|
||||
func Subject(kind string, id int64) string { return kind + ":" + strconv.FormatInt(id, 10) }
|
||||
|
||||
// channelOf is the funnel channel a prospect source belongs to.
|
||||
func channelOf(source string) string {
|
||||
switch source {
|
||||
case signals.SourceOrigami, signals.SourceExplee:
|
||||
return source
|
||||
}
|
||||
return "other"
|
||||
}
|
||||
|
||||
// found records that a prospect turned up. A company counts once, with its
|
||||
// people; a person on their own counts once.
|
||||
func found(d *db.DB, kind string, id, signalID int64, source, email string, now time.Time) error {
|
||||
_, err := signals.Record(d, signals.Event{
|
||||
SignalID: signalID, Stage: signals.StageFound, Channel: channelOf(source),
|
||||
Subject: Subject(kind, id), Email: email, Source: signals.SourceSupermarket,
|
||||
ExternalID: "found:" + Subject(kind, id), At: now,
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
// AddContact stores a person for review. A contact with the same email is
|
||||
@@ -186,7 +222,8 @@ func AddContact(d *db.DB, c Contact, now time.Time) (id int64, added bool, err e
|
||||
domain := crm.EmailDomain(c.Email)
|
||||
if c.CompanyID != 0 {
|
||||
var cd string
|
||||
if err := d.QueryRow(`SELECT domain FROM prospect_companies WHERE id = ?`, c.CompanyID).Scan(&cd); err != nil {
|
||||
var companySignal int64
|
||||
if err := d.QueryRow(`SELECT domain, coalesce(signal_id, 0) FROM prospect_companies WHERE id = ?`, c.CompanyID).Scan(&cd, &companySignal); err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return 0, false, ErrNotFound
|
||||
}
|
||||
@@ -198,6 +235,9 @@ func AddContact(d *db.DB, c Contact, now time.Time) (id int64, added bool, err e
|
||||
}
|
||||
return 0, false, err
|
||||
}
|
||||
if c.SignalID == 0 {
|
||||
c.SignalID = companySignal
|
||||
}
|
||||
}
|
||||
if sup, err := IsSuppressed(d, c.Email, domain); err != nil || sup {
|
||||
if err == nil {
|
||||
@@ -228,20 +268,72 @@ func AddContact(d *db.DB, c Contact, now time.Time) (id int64, added bool, err e
|
||||
}
|
||||
}
|
||||
res, err := d.Exec(`INSERT INTO prospect_contacts (company_id, first_name, last_name, title, email, email_status,
|
||||
linkedin, phone, source, source_detail, found_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
company, c.FirstName, c.LastName, c.Title, c.Email, c.EmailStatus, c.LinkedIn, c.Phone, c.Source, c.SourceDetail, formatTime(now))
|
||||
linkedin, phone, source, source_detail, found_at, signal_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
company, c.FirstName, c.LastName, c.Title, c.Email, c.EmailStatus, c.LinkedIn, c.Phone, c.Source, c.SourceDetail, formatTime(now),
|
||||
nullID(c.SignalID))
|
||||
if err != nil {
|
||||
return 0, false, err
|
||||
}
|
||||
id, err = res.LastInsertId()
|
||||
if id, err = res.LastInsertId(); err != nil {
|
||||
return 0, false, err
|
||||
}
|
||||
if c.CompanyID == 0 {
|
||||
err = found(d, "contact", id, c.SignalID, c.Source, c.Email, now)
|
||||
}
|
||||
return id, true, err
|
||||
}
|
||||
|
||||
// SetSignal changes the signal a company or person is tagged with (0:
|
||||
// none), and moves their funnel events with them. A company's people who
|
||||
// had the company's old signal follow it.
|
||||
func SetSignal(d *db.DB, kind string, id, signalID int64) error {
|
||||
table, err := table(kind)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var old int64
|
||||
if err := d.QueryRow(`SELECT coalesce(signal_id, 0) FROM `+table+` WHERE id = ?`, id).Scan(&old); err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ErrNotFound
|
||||
}
|
||||
return err
|
||||
}
|
||||
if _, err := d.Exec(`UPDATE `+table+` SET signal_id = ? WHERE id = ?`, nullID(signalID), id); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := signals.RetagSubject(d, Subject(kind, id), signalID); err != nil {
|
||||
return err
|
||||
}
|
||||
if kind != "company" {
|
||||
return nil
|
||||
}
|
||||
rows, err := d.Query(`SELECT id FROM prospect_contacts WHERE company_id = ? AND coalesce(signal_id, 0) = ?`, id, old)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var people []int64
|
||||
for rows.Next() {
|
||||
var pid int64
|
||||
if err := rows.Scan(&pid); err != nil {
|
||||
rows.Close()
|
||||
return err
|
||||
}
|
||||
people = append(people, pid)
|
||||
}
|
||||
rows.Close()
|
||||
for _, pid := range people {
|
||||
if err := SetSignal(d, "contact", pid, signalID); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// --- Reading ---
|
||||
|
||||
const companyColumns = `c.id, c.name, c.domain, c.website, c.linkedin, c.industry, c.size, c.location, c.why,
|
||||
c.source, c.source_detail, c.found_at, c.status, c.decided_at, coalesce(c.action_id, 0),
|
||||
coalesce(a.status, ''), coalesce(a.outcome, ''), coalesce(a.espo_id, ''), coalesce(a.error, '')`
|
||||
coalesce(a.status, ''), coalesce(a.outcome, ''), coalesce(a.espo_id, ''), coalesce(a.error, ''), coalesce(c.signal_id, 0)`
|
||||
|
||||
const companyFrom = ` FROM prospect_companies c LEFT JOIN crm_actions a ON a.id = c.action_id`
|
||||
|
||||
@@ -254,7 +346,7 @@ func scanCompanies(rows *sql.Rows) ([]Company, error) {
|
||||
var decided sql.NullString
|
||||
if err := rows.Scan(&c.ID, &c.Name, &c.Domain, &c.Website, &c.LinkedIn, &c.Industry, &c.Size, &c.Location, &c.Why,
|
||||
&c.Source, &c.SourceDetail, &found, &c.Status, &decided, &c.ActionID,
|
||||
&c.CRM.Status, &c.CRM.Outcome, &c.CRM.EspoID, &c.CRM.Error); err != nil {
|
||||
&c.CRM.Status, &c.CRM.Outcome, &c.CRM.EspoID, &c.CRM.Error, &c.SignalID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.FoundAt, _ = time.Parse(timeFormat, found)
|
||||
@@ -266,7 +358,7 @@ func scanCompanies(rows *sql.Rows) ([]Company, error) {
|
||||
|
||||
const contactColumns = `p.id, coalesce(p.company_id, 0), p.first_name, p.last_name, p.title, p.email, p.email_status,
|
||||
p.linkedin, p.phone, p.source, p.source_detail, p.found_at, p.status, p.decided_at, coalesce(p.action_id, 0),
|
||||
coalesce(a.status, ''), coalesce(a.outcome, ''), coalesce(a.espo_id, ''), coalesce(a.error, '')`
|
||||
coalesce(a.status, ''), coalesce(a.outcome, ''), coalesce(a.espo_id, ''), coalesce(a.error, ''), coalesce(p.signal_id, 0)`
|
||||
|
||||
const contactFrom = ` FROM prospect_contacts p LEFT JOIN crm_actions a ON a.id = p.action_id`
|
||||
|
||||
@@ -279,7 +371,7 @@ func scanContacts(rows *sql.Rows) ([]Contact, error) {
|
||||
var decided sql.NullString
|
||||
if err := rows.Scan(&c.ID, &c.CompanyID, &c.FirstName, &c.LastName, &c.Title, &c.Email, &c.EmailStatus,
|
||||
&c.LinkedIn, &c.Phone, &c.Source, &c.SourceDetail, &found, &c.Status, &decided, &c.ActionID,
|
||||
&c.CRM.Status, &c.CRM.Outcome, &c.CRM.EspoID, &c.CRM.Error); err != nil {
|
||||
&c.CRM.Status, &c.CRM.Outcome, &c.CRM.EspoID, &c.CRM.Error, &c.SignalID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.FoundAt, _ = time.Parse(timeFormat, found)
|
||||
@@ -328,14 +420,16 @@ func GetContact(d *db.DB, id int64) (*Contact, error) {
|
||||
return &cs[0], nil
|
||||
}
|
||||
|
||||
// Review lists what the review screen shows for one status: companies
|
||||
// Review lists what the review screen shows for one status (and one
|
||||
// signal, unless signalID is 0): companies
|
||||
// with that status or with contacts that have it (with only those
|
||||
// contacts), and contacts with that status who aren't at a company.
|
||||
// Newest first.
|
||||
func Review(d *db.DB, status string, limit int) ([]Company, []Contact, error) {
|
||||
func Review(d *db.DB, status string, signalID int64, limit int) ([]Company, []Contact, error) {
|
||||
rows, err := d.Query(`SELECT `+companyColumns+companyFrom+`
|
||||
WHERE c.status = ? OR EXISTS (SELECT 1 FROM prospect_contacts p WHERE p.company_id = c.id AND p.status = ?)
|
||||
ORDER BY c.id DESC LIMIT ?`, status, status, limit)
|
||||
WHERE (c.status = ? OR EXISTS (SELECT 1 FROM prospect_contacts p WHERE p.company_id = c.id AND p.status = ?))
|
||||
AND (? = 0 OR c.signal_id = ?)
|
||||
ORDER BY c.id DESC LIMIT ?`, status, status, signalID, signalID, limit)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
@@ -365,7 +459,8 @@ func Review(d *db.DB, status string, limit int) ([]Company, []Contact, error) {
|
||||
}
|
||||
}
|
||||
rows, err = d.Query(`SELECT `+contactColumns+contactFrom+`
|
||||
WHERE p.status = ? AND p.company_id IS NULL ORDER BY p.id DESC LIMIT ?`, status, limit)
|
||||
WHERE p.status = ? AND p.company_id IS NULL AND (? = 0 OR p.signal_id = ?) ORDER BY p.id DESC LIMIT ?`,
|
||||
status, signalID, signalID, limit)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user