253 lines
9.3 KiB
Go
253 lines
9.3 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/job-tracker/internal/graph/model"
|
|
"adam-french.co.uk/job-tracker/internal/models"
|
|
"adam-french.co.uk/job-tracker/internal/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())
|
|
}
|
|
|
|
// BulkDismissJobLeads is the resolver for the bulkDismissJobLeads field.
|
|
func (r *mutationResolver) BulkDismissJobLeads(ctx context.Context, ids []int) ([]*models.JobLead, error) {
|
|
return services.BulkLeadState(r.Store.DB, ids, services.LeadNew, services.LeadDismissed)
|
|
}
|
|
|
|
// BulkRestoreJobLeads is the resolver for the bulkRestoreJobLeads field.
|
|
func (r *mutationResolver) BulkRestoreJobLeads(ctx context.Context, ids []int) ([]*models.JobLead, error) {
|
|
return services.BulkLeadState(r.Store.DB, ids, services.LeadDismissed, services.LeadNew)
|
|
}
|
|
|
|
// BulkPromoteJobLeads is the resolver for the bulkPromoteJobLeads field.
|
|
func (r *mutationResolver) BulkPromoteJobLeads(ctx context.Context, ids []int) ([]*models.JobLead, error) {
|
|
return services.BulkPromoteJobLeads(r.Store.DB, ids, 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, filters *model.JobLeadFilters) ([]*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)
|
|
}
|
|
if filters != nil {
|
|
if filters.SourceKind != nil && strings.TrimSpace(*filters.SourceKind) != "" {
|
|
query = query.Where("source_kind = ?", strings.TrimSpace(*filters.SourceKind))
|
|
}
|
|
if filters.MinScore != nil {
|
|
query = query.Where("score >= ?", *filters.MinScore)
|
|
}
|
|
if filters.MaxScore != nil {
|
|
query = query.Where("score <= ?", *filters.MaxScore)
|
|
}
|
|
if filters.Location != nil && strings.TrimSpace(*filters.Location) != "" {
|
|
query = query.Where("location ILIKE ?", "%"+strings.TrimSpace(*filters.Location)+"%")
|
|
}
|
|
if filters.Company != nil && strings.TrimSpace(*filters.Company) != "" {
|
|
query = query.Where("company ILIKE ?", "%"+strings.TrimSpace(*filters.Company)+"%")
|
|
}
|
|
}
|
|
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
|
|
}
|