Phase 2 part 5: find people and verify emails with Hunter.io

Review gets Find people (Domain Search by the departments, seniority and
count set in Settings → Lead sources), Find their email (Email Finder for
a named person at a company), and Verify (one, or every unchecked address
in New). Every call is kept in enrich_runs (schema 9) with its result and
credits, and shown on the card. Nothing runs on a schedule. Sources are
shown by name (Hunter.io, origami.chat, explee.com).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-22 05:53:52 -07:00
co-authored by Claude Opus 5.5
parent efca73bcd4
commit 82d569da9c
18 changed files with 1348 additions and 11 deletions
+157
View File
@@ -0,0 +1,157 @@
package web
import (
"context"
"errors"
"net/http"
"strconv"
"time"
"supermarket/internal/api/hunter"
"supermarket/internal/enrich"
"supermarket/internal/prospects"
"supermarket/internal/secrets"
)
var hunterKey = secretField{"hunter", secrets.HunterAPIKey, "API key",
"In Hunter.io, API → API keys (hunter.io/api-keys).", "https://hunter.io/api-keys"}
// hunterBase is Hunter.io's API address; tests point it elsewhere.
var hunterBase = ""
// hunter is a client with the saved key, or nil without one.
func (s *Server) hunter() *hunter.Client {
key := s.Secrets.Get(secrets.HunterAPIKey)
if key == "" {
return nil
}
c := hunter.New(key)
if hunterBase != "" {
c.WithBase(hunterBase)
}
return c
}
var errNoHunter = errors.New("save a Hunter.io API key first, under Settings → Lead sources")
// enrichAction runs one Hunter.io call from the review screen.
func (s *Server) enrichAction(r *http.Request, kind, action string, id int64) error {
h := s.hunter()
if h == nil {
return errNoHunter
}
ctx, cancel := context.WithTimeout(r.Context(), 2*time.Minute)
defer cancel()
now := time.Now()
var err error
switch kind + "/" + action {
case "company/people":
_, err = enrich.FindPeople(ctx, s.DB, h, id, enrich.LoadSettings(s.DB), now)
case "contact/find-email":
_, err = enrich.FindEmail(ctx, s.DB, h, id, now)
case "contact/verify":
_, err = enrich.Verify(ctx, s.DB, h, id, now)
}
return err
}
// maxVerify is how many addresses one click verifies, so the page answers
// within a few minutes.
const maxVerify = 50
// unverified lists the people on the review screen whose address hasn't
// been checked.
func unverified(companies []prospects.Company, loose []prospects.Contact) []int64 {
var ids []int64
add := func(p prospects.Contact) {
if p.Email != "" && p.EmailStatus == "" && p.Status == prospects.StatusNew {
ids = append(ids, p.ID)
}
}
for _, c := range companies {
for _, p := range c.Contacts {
add(p)
}
}
for _, p := range loose {
add(p)
}
return ids
}
// prospectsVerify verifies the unchecked addresses in New (for one signal,
// if the screen is filtered).
func (s *Server) prospectsVerify(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
filter, _ := strconv.ParseInt(r.PostFormValue("filter"), 10, 64)
tab := prospects.StatusNew
h := s.hunter()
if h == nil {
s.renderProspects(w, r, tab, filter, errNoHunter.Error())
return
}
companies, loose, err := prospects.Review(s.DB, tab, filter, 200)
if err != nil {
s.serverError(w, err)
return
}
ids := unverified(companies, loose)
if len(ids) > maxVerify {
ids = ids[:maxVerify]
}
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Minute)
defer cancel()
if n, err := enrich.VerifyMany(ctx, s.DB, h, ids, time.Now()); err != nil {
s.renderProspects(w, r, tab, filter, "Verified "+strconv.Itoa(n)+", then stopped: "+err.Error())
return
}
redirect(w, r, reviewURL(tab, filter), "verified")
}
// hunterSettingsData is the Hunter.io card on Settings → Lead sources.
func (s *Server) hunterSettingsData() map[string]any {
used, _ := enrich.CreditsSince(s.DB, time.Now().AddDate(0, 0, -30))
return map[string]any{
"Key": hunterKey,
"Set": s.Secrets.Has(secrets.HunterAPIKey),
"Settings": enrich.LoadSettings(s.DB),
"Departments": enrich.Departments,
"Seniorities": enrich.Seniorities,
"PerCompany": enrich.PerCompanyChoices,
"Used": strconv.FormatFloat(used, 'f', -1, 64),
}
}
// saveHunterSettings stores whom to look for, from the settings form.
func (s *Server) saveHunterSettings(r *http.Request) error {
per, _ := strconv.Atoi(r.PostFormValue("hunter_per_company"))
return enrich.SaveSettings(s.DB, enrich.Settings{
Departments: r.PostForm["hunter_department"],
Seniority: r.PostForm["hunter_seniority"],
PerCompany: per,
})
}
// testHunter checks the key and shows what's left of the plan.
func (s *Server) testHunter(ctx context.Context) map[string]any {
h := s.hunter()
if h == nil {
return map[string]any{"Error": "Save the key first."}
}
a, err := h.Account(ctx)
if err != nil {
return map[string]any{"Error": err.Error()}
}
msg := "The key works"
if a.PlanName != "" {
msg += " (" + a.PlanName + " plan)"
}
msg += ": " + a.Left()
if a.ResetDate != "" {
msg += " until " + a.ResetDate
}
return map[string]any{"OK": msg + "."}
}