From e135288005d851e065297df4f29803fc91129e8f Mon Sep 17 00:00:00 2001 From: David Young Date: Sat, 21 Nov 2026 18:33:26 -0700 Subject: [PATCH] Lead source imports: no duplicates without emails; explee hot leads origami.chat people have no email, so each import added them again. Prospects now match on email, then LinkedIn profile, then name at the same company; companies without a website match by name, and get the domain when one turns up. explee.com's hot-leads list allows at most 200 per page (500 was refused, so no hot leads became prospects). origami.chat people's company website is read from identity.website_url. explee.com's key test no longer says "the key works" twice. Co-Authored-By: Claude Opus 5.5 --- internal/api/explee/client.go | 4 +- internal/leadsource/leadsource.go | 2 +- internal/prospects/prospects.go | 124 ++++++++++++++++++++------- internal/prospects/prospects_test.go | 30 +++++++ internal/web/signals.go | 5 +- 5 files changed, 128 insertions(+), 37 deletions(-) diff --git a/internal/api/explee/client.go b/internal/api/explee/client.go index e22a284..e855048 100644 --- a/internal/api/explee/client.go +++ b/internal/api/explee/client.go @@ -101,7 +101,7 @@ func (c *Client) Me(ctx context.Context) (string, error) { return v, nil } } - return "the key works", nil + return "", nil } // Campaign is an outreach campaign. @@ -212,7 +212,7 @@ func (c *Client) HotLeads(ctx context.Context, since time.Time) ([]HotLead, erro HasMore bool `json:"has_more"` NextOffset *int `json:"next_offset"` } - q := url.Values{"limit": {"500"}, "offset": {strconv.Itoa(offset)}} + q := url.Values{"limit": {"200"}, "offset": {strconv.Itoa(offset)}} // the API allows at most 200 if !since.IsZero() { q.Set("since", since.UTC().Format(time.RFC3339)) } diff --git a/internal/leadsource/leadsource.go b/internal/leadsource/leadsource.go index 57b5f3c..3163259 100644 --- a/internal/leadsource/leadsource.go +++ b/internal/leadsource/leadsource.go @@ -450,7 +450,7 @@ func ImportOrigami(ctx context.Context, d *db.DB, c *origami.Client, now time.Ti name: w.Str("name", "full_name", "identity.name"), title: w.Str("title", "job_title", "identity.title"), email: email, linkedin: w.Str("linkedin_url", "identity.linkedin_url", "linkedin"), company: w.Str("company", "company_name", "identity.company", "identity.company_name"), - domain: w.Str("company_domain", "domain", "identity.company_domain", "identity.domain"), + domain: w.Str("company_domain", "domain", "identity.company_domain", "identity.domain", "identity.website_url"), source: signals.SourceOrigami, detail: detail, signalID: sig, }) } diff --git a/internal/prospects/prospects.go b/internal/prospects/prospects.go index 41280fc..df50c02 100644 --- a/internal/prospects/prospects.go +++ b/internal/prospects/prospects.go @@ -141,22 +141,20 @@ func AddCompany(d *db.DB, c Company, now time.Time) (id int64, added bool, err e } return 0, false, err } - if c.Domain != "" { - err := d.QueryRow(`SELECT id FROM prospect_companies WHERE domain = ?`, c.Domain).Scan(&id) - if err == nil { - _, err = d.Exec(`UPDATE prospect_companies SET - website = CASE WHEN website = '' THEN ? ELSE website END, - linkedin = CASE WHEN linkedin = '' THEN ? ELSE linkedin END, - industry = CASE WHEN industry = '' THEN ? ELSE industry END, - size = CASE WHEN size = '' THEN ? ELSE size END, - location = CASE WHEN location = '' THEN ? ELSE location END, - why = CASE WHEN why = '' THEN ? ELSE why END - WHERE id = ?`, c.Website, c.LinkedIn, c.Industry, c.Size, c.Location, c.Why, id) - return id, false, err - } - if !errors.Is(err, sql.ErrNoRows) { - return 0, false, err - } + if id, err = findCompany(d, c); err != nil { + return 0, false, err + } + if id != 0 { + _, err = d.Exec(`UPDATE prospect_companies SET + domain = CASE WHEN domain = '' THEN ? ELSE domain END, + website = CASE WHEN website = '' THEN ? ELSE website END, + linkedin = CASE WHEN linkedin = '' THEN ? ELSE linkedin END, + industry = CASE WHEN industry = '' THEN ? ELSE industry END, + size = CASE WHEN size = '' THEN ? ELSE size END, + location = CASE WHEN location = '' THEN ? ELSE location END, + why = CASE WHEN why = '' THEN ? ELSE why END + WHERE id = ?`, c.Domain, c.Website, c.LinkedIn, c.Industry, c.Size, c.Location, c.Why, id) + return id, false, err } res, err := d.Exec(`INSERT INTO prospect_companies (name, domain, website, linkedin, industry, size, location, why, source, source_detail, found_at, signal_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, @@ -249,23 +247,21 @@ func AddContact(d *db.DB, c Contact, now time.Time) (id int64, added bool, err e if c.CompanyID != 0 { company = c.CompanyID } - if c.Email != "" { - err := d.QueryRow(`SELECT id FROM prospect_contacts WHERE email = ?`, c.Email).Scan(&id) - if err == nil { - _, err = d.Exec(`UPDATE prospect_contacts SET - company_id = coalesce(company_id, ?), - first_name = CASE WHEN first_name = '' THEN ? ELSE first_name END, - last_name = CASE WHEN last_name = '' THEN ? ELSE last_name END, - title = CASE WHEN title = '' THEN ? ELSE title END, - email_status = CASE WHEN ? != '' THEN ? ELSE email_status END, - linkedin = CASE WHEN linkedin = '' THEN ? ELSE linkedin END, - phone = CASE WHEN phone = '' THEN ? ELSE phone END - WHERE id = ?`, company, c.FirstName, c.LastName, c.Title, c.EmailStatus, c.EmailStatus, c.LinkedIn, c.Phone, id) - return id, false, err - } - if !errors.Is(err, sql.ErrNoRows) { - return 0, false, err - } + if id, err = findContact(d, c); err != nil { + return 0, false, err + } + if id != 0 { + _, err = d.Exec(`UPDATE prospect_contacts SET + company_id = coalesce(company_id, ?), + email = CASE WHEN email = '' THEN ? ELSE email END, + first_name = CASE WHEN first_name = '' THEN ? ELSE first_name END, + last_name = CASE WHEN last_name = '' THEN ? ELSE last_name END, + title = CASE WHEN title = '' THEN ? ELSE title END, + email_status = CASE WHEN ? != '' THEN ? ELSE email_status END, + linkedin = CASE WHEN linkedin = '' THEN ? ELSE linkedin END, + phone = CASE WHEN phone = '' THEN ? ELSE phone END + WHERE id = ?`, company, c.Email, c.FirstName, c.LastName, c.Title, c.EmailStatus, c.EmailStatus, c.LinkedIn, c.Phone, id) + return id, false, err } 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, signal_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, @@ -329,6 +325,68 @@ func SetSignal(d *db.DB, kind string, id, signalID int64) error { return nil } +// findCompany is the company already stored as c, or 0: the same domain, +// or, when one of them has no domain, the same name. +func findCompany(d *db.DB, c Company) (int64, error) { + var id int64 + q := func(query string, args ...any) error { + err := d.QueryRow(query, args...).Scan(&id) + if errors.Is(err, sql.ErrNoRows) { + return nil + } + return err + } + if c.Domain != "" { + if err := q(`SELECT id FROM prospect_companies WHERE domain = ?`, c.Domain); err != nil || id != 0 { + return id, err + } + err := q(`SELECT id FROM prospect_companies WHERE domain = '' AND lower(name) = lower(?) ORDER BY id LIMIT 1`, c.Name) + return id, err + } + err := q(`SELECT id FROM prospect_companies WHERE lower(name) = lower(?) ORDER BY domain = '' DESC, id LIMIT 1`, c.Name) + return id, err +} + +// normalizeLinkedIn makes profile URLs comparable. +func normalizeLinkedIn(u string) string { + u = strings.ToLower(strings.TrimSpace(u)) + u = strings.TrimPrefix(strings.TrimPrefix(u, "https://"), "http://") + u = strings.TrimPrefix(u, "www.") + return strings.TrimRight(u, "/") +} + +// findContact is the person already stored as c, or 0: the same email; or, +// without one, the same LinkedIn profile; or the same name at the same +// company. Tools like origami.chat often have no email for a person. +func findContact(d *db.DB, c Contact) (int64, error) { + var id int64 + q := func(query string, args ...any) error { + err := d.QueryRow(query, args...).Scan(&id) + if errors.Is(err, sql.ErrNoRows) { + return nil + } + return err + } + if c.Email != "" { + if err := q(`SELECT id FROM prospect_contacts WHERE email = ?`, c.Email); err != nil || id != 0 { + return id, err + } + } + if li := normalizeLinkedIn(c.LinkedIn); li != "" { + if err := q(`SELECT id FROM prospect_contacts WHERE linkedin != '' AND (email = '' OR ? = '') + AND rtrim(replace(replace(replace(lower(linkedin), 'https://', ''), 'http://', ''), 'www.', ''), '/') = ? LIMIT 1`, + c.Email, li); err != nil || id != 0 { + return id, err + } + } + if c.Email != "" || (c.FirstName == "" && c.LastName == "") { + return 0, nil + } + err := q(`SELECT id FROM prospect_contacts WHERE email = '' AND lower(first_name) = lower(?) AND lower(last_name) = lower(?) + AND coalesce(company_id, 0) = ? LIMIT 1`, c.FirstName, c.LastName, c.CompanyID) + return id, err +} + // --- Reading --- const companyColumns = `c.id, c.name, c.domain, c.website, c.linkedin, c.industry, c.size, c.location, c.why, diff --git a/internal/prospects/prospects_test.go b/internal/prospects/prospects_test.go index ad69af9..5129bac 100644 --- a/internal/prospects/prospects_test.go +++ b/internal/prospects/prospects_test.go @@ -218,3 +218,33 @@ func TestCRMMatches(t *testing.T) { } func itoa(n int64) string { return strconv.FormatInt(n, 10) } + +// People from tools without emails (origami.chat) must not be added twice. +func TestAddWithoutEmails(t *testing.T) { + d := openDB(t) + c1, _, _ := AddCompany(d, Company{Name: "DATAGROUP", Source: "origami"}, now) + c2, added, _ := AddCompany(d, Company{Name: "Datagroup", Website: "https://www.datagroup.example/", Source: "origami"}, now) + if added || c2 != c1 { + t.Fatalf("same name, now with a website: %d %d %v", c1, c2, added) + } + if c, _ := GetCompany(d, c1); c.Domain != "datagroup.example" { + t.Errorf("the domain wasn't filled in: %+v", c) + } + p1, _, _ := AddContact(d, Contact{CompanyID: c1, FirstName: "Helge", LastName: "Viehof", LinkedIn: "https://www.linkedin.com/in/helge-v/", Source: "origami"}, now) + if p2, added, _ := AddContact(d, Contact{FirstName: "Helge", LastName: "Viehof", LinkedIn: "http://linkedin.com/in/helge-v", Source: "origami"}, now); added || p2 != p1 { + t.Errorf("same LinkedIn: %d %d %v", p1, p2, added) + } + if p3, added, _ := AddContact(d, Contact{CompanyID: c1, FirstName: "helge", LastName: "viehof", Source: "origami"}, now); added || p3 != p1 { + t.Errorf("same name at the same company: %d %d %v", p1, p3, added) + } + // A later email is filled in; a namesake elsewhere is someone else. + if p4, added, _ := AddContact(d, Contact{FirstName: "Helge", LastName: "Viehof", Email: "helge@datagroup.example", LinkedIn: "linkedin.com/in/helge-v", Source: "hunter"}, now); added || p4 != p1 { + t.Errorf("email added later: %d %d %v", p1, p4, added) + } + if p, _ := GetContact(d, p1); p.Email != "helge@datagroup.example" { + t.Errorf("email not filled in: %+v", p) + } + if _, added, _ := AddContact(d, Contact{FirstName: "Helge", LastName: "Viehof", Source: "origami"}, now); !added { + t.Error("a namesake without a company was merged") + } +} diff --git a/internal/web/signals.go b/internal/web/signals.go index fc14181..0facf3b 100644 --- a/internal/web/signals.go +++ b/internal/web/signals.go @@ -378,7 +378,10 @@ func (s *Server) sourcesTest(w http.ResponseWriter, r *http.Request) { data["Error"] = err.Error() break } - data["OK"] = "The key works (" + who + ")." + data["OK"] = "The key works." + if who != "" { + data["OK"] = "The key works (" + who + ")." + } case signals.SourceOrigami: key := s.Secrets.Get(secrets.OrigamiAPIKey) if key == "" {