Add application workflow and lead analytics
Test and publish / test (push) Successful in 2m33s
Test and publish / publish (push) Successful in 11m9s

This commit is contained in:
2026-10-23 20:45:48 +01:00
parent d0074aea46
commit 1147bc5697
35 changed files with 4337 additions and 131 deletions
+112
View File
@@ -36,6 +36,113 @@ const (
LeadPromoted = "promoted"
)
const maxBulkLeadIDs = 200
// BulkLeadState validates every row before changing any of them. This keeps a
// typo or a mixed-state selection from producing a partial bulk action.
func BulkLeadState(db *gorm.DB, ids []int, from, to string) ([]*models.JobLead, error) {
if err := validateBulkLeadIDs(ids); err != nil {
return nil, err
}
tx := db.Begin()
if tx.Error != nil {
return nil, tx.Error
}
var leads []*models.JobLead
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).Where("id IN ?", ids).Find(&leads).Error; err != nil {
tx.Rollback()
return nil, err
}
if len(leads) != len(ids) {
tx.Rollback()
return nil, fmt.Errorf("one or more leads do not exist")
}
for _, lead := range leads {
if lead.State != from {
tx.Rollback()
return nil, fmt.Errorf("lead %d is %s, expected %s", lead.ID, lead.State, from)
}
}
if err := tx.Model(&models.JobLead{}).Where("id IN ?", ids).Update("state", to).Error; err != nil {
tx.Rollback()
return nil, err
}
if err := tx.Commit().Error; err != nil {
return nil, err
}
return leadsWithIDs(db, ids)
}
func leadsWithIDs(db *gorm.DB, ids []int) ([]*models.JobLead, error) {
var leads []*models.JobLead
if err := db.Where("id IN ?", ids).Find(&leads).Error; err != nil {
return nil, err
}
byID := make(map[uint]*models.JobLead, len(leads))
for _, lead := range leads {
byID[lead.ID] = lead
}
ordered := make([]*models.JobLead, 0, len(ids))
for _, id := range ids {
ordered = append(ordered, byID[uint(id)])
}
return ordered, nil
}
// BulkPromoteJobLeads deliberately promotes one at a time: PromoteJobLead
// owns the existing application deduplication and transaction semantics.
func BulkPromoteJobLeads(db *gorm.DB, ids []int, now time.Time) ([]*models.JobLead, error) {
if err := validateBulkLeadIDs(ids); err != nil {
return nil, err
}
var leads []*models.JobLead
if err := db.Where("id IN ?", ids).Find(&leads).Error; err != nil {
return nil, err
}
if len(leads) != len(ids) {
return nil, fmt.Errorf("one or more leads do not exist")
}
for _, lead := range leads {
if lead.State != LeadNew {
return nil, fmt.Errorf("lead %d is %s, expected %s", lead.ID, lead.State, LeadNew)
}
}
tx := db.Begin()
if tx.Error != nil {
return nil, tx.Error
}
result := make([]*models.JobLead, 0, len(ids))
for _, id := range ids {
lead, err := PromoteJobLead(tx, uint(id), now)
if err != nil {
tx.Rollback()
return nil, err
}
result = append(result, lead)
}
if err := tx.Commit().Error; err != nil {
return nil, err
}
return result, nil
}
func validateBulkLeadIDs(ids []int) error {
if len(ids) == 0 || len(ids) > maxBulkLeadIDs {
return fmt.Errorf("select between 1 and %d leads", maxBulkLeadIDs)
}
seen := make(map[int]struct{}, len(ids))
for _, id := range ids {
if id <= 0 {
return fmt.Errorf("lead ids must be positive")
}
if _, ok := seen[id]; ok {
return fmt.Errorf("duplicate lead id %d", id)
}
seen[id] = struct{}{}
}
return nil
}
const (
// maxScoresPerRun bounds the Claude calls one Run can make, and so both
// its cost and its length. A first poll of a few boards can match
@@ -549,6 +656,11 @@ func PromoteJobLead(db *gorm.DB, id uint, now time.Time) (*models.JobLead, error
if err != nil {
return nil, err
}
if _, err := RecordApplicationEvent(db, app.ID, ApplicationEventInput{
Kind: "promoted", OccurredAt: now, Source: "lead",
}); err != nil {
return nil, fmt.Errorf("recording promotion event: %w", err)
}
lead.State = LeadPromoted
lead.JobAppID = &app.ID
if err := db.Model(&lead).Updates(map[string]any{"state": lead.State, "job_app_id": lead.JobAppID}).Error; err != nil {