Files
stpweb/backend/graph/job_lead.resolvers.go
T
adamfandClaude Opus 5.5 8b902350ad
Test and deploy / test (push) Successful in 1m56s
Test and deploy / deploy (push) Successful in 1m8s
Add rescoring and a posting-date order to the leads inbox
rescoreJobLeads (RescoreAll, "Rescore all" on /cv/leads) scores every
matched inbox lead again, for after the profile or the model has
changed. Scores are not wiped first: a lead keeps its old score until
the new one is saved, and the new scored_at column marks which leads the
job has reached, so a job that dies partway leaves no gaps. Leads that
had used up their attempts get them back.

The inbox can also be ordered by when a posting went up, falling back to
when it was found where the source gave no date.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-12 15:52:33 +01:00

221 lines
7.7 KiB
Go

package graph
// This file will be automatically regenerated based on the schema, any resolver
// implementations
// will be copied through when generating and any unknown code will be moved to the end.
// Code generated by github.com/99designs/gqlgen version v0.17.95
import (
"context"
"fmt"
"strings"
"time"
"adam-french.co.uk/backend/graph/model"
"adam-french.co.uk/backend/models"
"adam-french.co.uk/backend/services"
)
// CreateJobSource is the resolver for the createJobSource field.
func (r *mutationResolver) CreateJobSource(ctx context.Context, input model.CreateJobSourceInput) (*models.JobSource, error) {
src := models.JobSource{Kind: input.Kind, Label: input.Label, Enabled: true}
if input.Token != nil {
src.Token = *input.Token
}
if input.Query != nil {
src.Query = *input.Query
}
if input.Location != nil {
src.Location = *input.Location
}
if input.Enabled != nil {
src.Enabled = *input.Enabled
}
if err := services.ValidateJobSource(&src); err != nil {
return nil, err
}
if err := r.Store.DB.Create(&src).Error; err != nil {
return nil, err
}
return &src, nil
}
// UpdateJobSource is the resolver for the updateJobSource field.
func (r *mutationResolver) UpdateJobSource(ctx context.Context, id int, input model.UpdateJobSourceInput) (*models.JobSource, error) {
// Partial update, as in UpdateJobApplication: a nil field is untouched.
// The merged row is validated as a whole, because whether a token or a
// query is required depends on the kind, which may itself be changing.
var src models.JobSource
if err := r.Store.DB.First(&src, id).Error; err != nil {
return nil, err
}
if input.Kind != nil {
src.Kind = *input.Kind
}
if input.Label != nil {
src.Label = *input.Label
}
if input.Token != nil {
src.Token = *input.Token
}
if input.Query != nil {
src.Query = *input.Query
}
if input.Location != nil {
src.Location = *input.Location
}
if input.Enabled != nil {
src.Enabled = *input.Enabled
}
if err := services.ValidateJobSource(&src); err != nil {
return nil, err
}
// Named columns rather than Save: the fetcher writes last_fetched_at,
// last_count and last_error on this row during a run, and saving the
// whole struct loaded above would put stale values back over them.
if err := r.Store.DB.Model(&src).
Select("Kind", "Label", "Token", "Query", "Location", "Enabled").
Updates(&src).Error; err != nil {
return nil, err
}
return &src, nil
}
// DeleteJobSource is the resolver for the deleteJobSource field.
func (r *mutationResolver) DeleteJobSource(ctx context.Context, id int) (bool, error) {
// Loaded first so a missing id is an error; see DeleteJobApplication.
// The source's leads are deliberately left alone: they are still real
// postings, and deleting them would let a re-added source file every one
// of them again, dismissed ones included.
var src models.JobSource
if err := r.Store.DB.First(&src, id).Error; err != nil {
return false, err
}
if err := r.Store.DB.Delete(&src).Error; err != nil {
return false, err
}
return true, nil
}
// DismissJobLead is the resolver for the dismissJobLead field.
func (r *mutationResolver) DismissJobLead(ctx context.Context, id int) (*models.JobLead, error) {
return r.setLeadState(id, services.LeadNew, services.LeadDismissed)
}
// RestoreJobLead is the resolver for the restoreJobLead field.
func (r *mutationResolver) RestoreJobLead(ctx context.Context, id int) (*models.JobLead, error) {
return r.setLeadState(id, services.LeadDismissed, services.LeadNew)
}
// PromoteJobLead is the resolver for the promoteJobLead field.
func (r *mutationResolver) PromoteJobLead(ctx context.Context, id int) (*models.JobLead, error) {
return services.PromoteJobLead(r.Store.DB, uint(id), time.Now())
}
// UpdateJobSearchProfile is the resolver for the updateJobSearchProfile field.
func (r *mutationResolver) UpdateJobSearchProfile(ctx context.Context, input model.UpdateJobSearchProfileInput) (*models.JobSearchProfile, error) {
profile, err := services.LoadJobSearchProfile(r.Store.DB)
if err != nil {
return nil, err
}
if input.Profile != nil {
profile.Profile = *input.Profile
}
if input.IncludeKeywords != nil {
profile.IncludeKeywords = strings.TrimSpace(*input.IncludeKeywords)
}
if input.ExcludeKeywords != nil {
profile.ExcludeKeywords = strings.TrimSpace(*input.ExcludeKeywords)
}
if input.Locations != nil {
profile.Locations = strings.TrimSpace(*input.Locations)
}
if input.MinScore != nil {
profile.MinScore = *input.MinScore
}
if err := services.SaveJobSearchProfile(r.Store.DB, profile); err != nil {
return nil, err
}
return profile, nil
}
// RunJobLeadSync is the resolver for the runJobLeadSync field.
func (r *mutationResolver) RunJobLeadSync(ctx context.Context) (bool, error) {
if r.Store.JobLeads == nil {
return false, fmt.Errorf("job leads are not configured")
}
return r.Store.JobLeads.RunInBackground(services.LeadJobSync), nil
}
// ScoreJobLeads is the resolver for the scoreJobLeads field.
func (r *mutationResolver) ScoreJobLeads(ctx context.Context) (bool, error) {
if r.Store.JobLeads == nil {
return false, fmt.Errorf("job leads are not configured")
}
return r.Store.JobLeads.RunInBackground(services.LeadJobScore), nil
}
// RescoreJobLeads is the resolver for the rescoreJobLeads field.
func (r *mutationResolver) RescoreJobLeads(ctx context.Context) (bool, error) {
if r.Store.JobLeads == nil {
return false, fmt.Errorf("job leads are not configured")
}
return r.Store.JobLeads.RunInBackground(services.LeadJobRescore), nil
}
// JobSources is the resolver for the jobSources field.
func (r *queryResolver) JobSources(ctx context.Context) ([]*models.JobSource, error) {
var sources []*models.JobSource
if err := r.Store.DB.Order("label ASC, id ASC").Find(&sources).Error; err != nil {
return nil, err
}
return sources, nil
}
// JobLeads is the resolver for the jobLeads field.
func (r *queryResolver) JobLeads(ctx context.Context, state *string, includeUnmatched *bool) ([]*models.JobLead, error) {
// Omit, not Select: every column but the description, which is tens of
// kilobytes a row and is fetched one lead at a time through jobLead. A
// client that asks for description here gets "".
query := r.Store.DB.Omit("description")
if state != nil {
query = query.Where("state = ?", *state)
}
if includeUnmatched == nil || !*includeUnmatched {
// A promoted or dismissed lead is shown whatever the filter now says
// about it; `matched` only decides what the inbox offers.
query = query.Where("(matched = ? OR state <> ?)", true, services.LeadNew)
}
var leads []*models.JobLead
// Best first, unscored after scored, newest first within a score. The
// limit is a backstop for includeUnmatched on a database holding several
// whole boards.
if err := query.Order("score DESC NULLS LAST, created_at DESC, id DESC").Limit(2000).Find(&leads).Error; err != nil {
return nil, err
}
return leads, nil
}
// JobLead is the resolver for the jobLead field.
func (r *queryResolver) JobLead(ctx context.Context, id int) (*models.JobLead, error) {
var lead models.JobLead
if err := r.Store.DB.First(&lead, id).Error; err != nil {
return nil, err
}
return &lead, nil
}
// JobSearchProfile is the resolver for the jobSearchProfile field.
func (r *queryResolver) JobSearchProfile(ctx context.Context) (*models.JobSearchProfile, error) {
return services.LoadJobSearchProfile(r.Store.DB)
}
// JobLeadSyncStatus is the resolver for the jobLeadSyncStatus field.
func (r *queryResolver) JobLeadSyncStatus(ctx context.Context) (*model.JobLeadSyncStatus, error) {
if r.Store.JobLeads == nil {
return &model.JobLeadSyncStatus{LastError: "job leads are not configured"}, nil
}
status := r.Store.JobLeads.Status()
return &model.JobLeadSyncStatus{Running: status.Running, LastRunAt: status.LastRunAt, LastError: status.LastError}, nil
}