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
+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
}
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,