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 <noreply@anthropic.com>
This commit is contained in:
2026-11-21 18:33:26 -07:00
co-authored by Claude Opus 5.5
parent 157902f7e2
commit e135288005
5 changed files with 128 additions and 37 deletions
+2 -2
View File
@@ -101,7 +101,7 @@ func (c *Client) Me(ctx context.Context) (string, error) {
return v, nil return v, nil
} }
} }
return "the key works", nil return "", nil
} }
// Campaign is an outreach campaign. // 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"` HasMore bool `json:"has_more"`
NextOffset *int `json:"next_offset"` 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() { if !since.IsZero() {
q.Set("since", since.UTC().Format(time.RFC3339)) q.Set("since", since.UTC().Format(time.RFC3339))
} }
+1 -1
View File
@@ -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"), 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"), email: email, linkedin: w.Str("linkedin_url", "identity.linkedin_url", "linkedin"),
company: w.Str("company", "company_name", "identity.company", "identity.company_name"), 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, source: signals.SourceOrigami, detail: detail, signalID: sig,
}) })
} }
+91 -33
View File
@@ -141,22 +141,20 @@ func AddCompany(d *db.DB, c Company, now time.Time) (id int64, added bool, err e
} }
return 0, false, err return 0, false, err
} }
if c.Domain != "" { if id, err = findCompany(d, c); err != nil {
err := d.QueryRow(`SELECT id FROM prospect_companies WHERE domain = ?`, c.Domain).Scan(&id) return 0, false, err
if err == nil { }
_, err = d.Exec(`UPDATE prospect_companies SET if id != 0 {
website = CASE WHEN website = '' THEN ? ELSE website END, _, err = d.Exec(`UPDATE prospect_companies SET
linkedin = CASE WHEN linkedin = '' THEN ? ELSE linkedin END, domain = CASE WHEN domain = '' THEN ? ELSE domain END,
industry = CASE WHEN industry = '' THEN ? ELSE industry END, website = CASE WHEN website = '' THEN ? ELSE website END,
size = CASE WHEN size = '' THEN ? ELSE size END, linkedin = CASE WHEN linkedin = '' THEN ? ELSE linkedin END,
location = CASE WHEN location = '' THEN ? ELSE location END, industry = CASE WHEN industry = '' THEN ? ELSE industry END,
why = CASE WHEN why = '' THEN ? ELSE why END size = CASE WHEN size = '' THEN ? ELSE size END,
WHERE id = ?`, c.Website, c.LinkedIn, c.Industry, c.Size, c.Location, c.Why, id) location = CASE WHEN location = '' THEN ? ELSE location END,
return id, false, err 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)
if !errors.Is(err, sql.ErrNoRows) { return id, false, err
return 0, false, err
}
} }
res, err := d.Exec(`INSERT INTO prospect_companies (name, domain, website, linkedin, industry, size, location, why, res, err := d.Exec(`INSERT INTO prospect_companies (name, domain, website, linkedin, industry, size, location, why,
source, source_detail, found_at, signal_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, 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 { if c.CompanyID != 0 {
company = c.CompanyID company = c.CompanyID
} }
if c.Email != "" { if id, err = findContact(d, c); err != nil {
err := d.QueryRow(`SELECT id FROM prospect_contacts WHERE email = ?`, c.Email).Scan(&id) return 0, false, err
if err == nil { }
_, err = d.Exec(`UPDATE prospect_contacts SET if id != 0 {
company_id = coalesce(company_id, ?), _, err = d.Exec(`UPDATE prospect_contacts SET
first_name = CASE WHEN first_name = '' THEN ? ELSE first_name END, company_id = coalesce(company_id, ?),
last_name = CASE WHEN last_name = '' THEN ? ELSE last_name END, email = CASE WHEN email = '' THEN ? ELSE email END,
title = CASE WHEN title = '' THEN ? ELSE title END, first_name = CASE WHEN first_name = '' THEN ? ELSE first_name END,
email_status = CASE WHEN ? != '' THEN ? ELSE email_status END, last_name = CASE WHEN last_name = '' THEN ? ELSE last_name END,
linkedin = CASE WHEN linkedin = '' THEN ? ELSE linkedin END, title = CASE WHEN title = '' THEN ? ELSE title END,
phone = CASE WHEN phone = '' THEN ? ELSE phone END email_status = CASE WHEN ? != '' THEN ? ELSE email_status END,
WHERE id = ?`, company, c.FirstName, c.LastName, c.Title, c.EmailStatus, c.EmailStatus, c.LinkedIn, c.Phone, id) linkedin = CASE WHEN linkedin = '' THEN ? ELSE linkedin END,
return id, false, err 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)
if !errors.Is(err, sql.ErrNoRows) { return id, false, err
return 0, false, err
}
} }
res, err := d.Exec(`INSERT INTO prospect_contacts (company_id, first_name, last_name, title, email, email_status, 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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, 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 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 --- // --- Reading ---
const companyColumns = `c.id, c.name, c.domain, c.website, c.linkedin, c.industry, c.size, c.location, c.why, const companyColumns = `c.id, c.name, c.domain, c.website, c.linkedin, c.industry, c.size, c.location, c.why,
+30
View File
@@ -218,3 +218,33 @@ func TestCRMMatches(t *testing.T) {
} }
func itoa(n int64) string { return strconv.FormatInt(n, 10) } 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")
}
}
+4 -1
View File
@@ -378,7 +378,10 @@ func (s *Server) sourcesTest(w http.ResponseWriter, r *http.Request) {
data["Error"] = err.Error() data["Error"] = err.Error()
break break
} }
data["OK"] = "The key works (" + who + ")." data["OK"] = "The key works."
if who != "" {
data["OK"] = "The key works (" + who + ")."
}
case signals.SourceOrigami: case signals.SourceOrigami:
key := s.Secrets.Get(secrets.OrigamiAPIKey) key := s.Secrets.Get(secrets.OrigamiAPIKey)
if key == "" { if key == "" {