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:
@@ -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,
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user