Files
job-tracker/internal/graph/job_application.resolvers.go
T
adamf 1147bc5697
Test and publish / test (push) Successful in 2m33s
Test and publish / publish (push) Successful in 11m9s
Add application workflow and lead analytics
2026-10-23 20:45:48 +01:00

246 lines
9.6 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"
"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"
)
// Events is the resolver for the events field.
func (r *jobApplicationResolver) Events(ctx context.Context, obj *models.JobApplication) ([]*models.JobApplicationEvent, error) {
return services.ListApplicationEvents(r.Store.DB, obj.ID)
}
// CreateJobApplication is the resolver for the createJobApplication field.
func (r *mutationResolver) CreateJobApplication(ctx context.Context, input model.CreateJobApplicationInput) (*models.JobApplication, error) {
// String! is non-null but not non-empty, and the columns are only
// `not null`, so "" stores happily and then never matches the capture
// upsert's LOWER(company) lookup — an orphan row no capture can find.
jobTitle := strings.TrimSpace(input.JobTitle)
company := strings.TrimSpace(input.Company)
if jobTitle == "" || company == "" {
return nil, fmt.Errorf("jobTitle and company are required")
}
// input.Status is already one of the JobStatus values: the enum's
// unmarshaller (services.UnmarshalJobStatus) refused anything else before
// this resolver was reached, so it is stored as it came.
app := models.JobApplication{
JobTitle: jobTitle,
Company: company,
Location: input.Location,
URL: input.URL,
Status: input.Status,
Notes: input.Notes,
AppliedAt: input.AppliedAt,
Description: input.Description,
NextAction: input.NextAction, NextActionAt: input.NextActionAt,
ContactName: input.ContactName, ContactEmail: input.ContactEmail,
ContactRole: input.ContactRole, LastContactedAt: input.LastContactedAt,
SalaryRange: input.SalaryRange, WorkArrangement: input.WorkArrangement,
ApplicationSource: input.ApplicationSource, ResumeVersion: input.ResumeVersion,
RequisitionID: input.RequisitionID,
}
if err := r.Store.DB.Create(&app).Error; err != nil {
return nil, err
}
if _, err := services.RecordApplicationEvent(r.Store.DB, app.ID, services.ApplicationEventInput{Kind: "created", Source: "graphql"}); err != nil {
return nil, err
}
return &app, nil
}
// UpdateJobApplication is the resolver for the updateJobApplication field.
func (r *mutationResolver) UpdateJobApplication(ctx context.Context, id int, input model.UpdateJobApplicationInput) (*models.JobApplication, error) {
// Partial update: every input field is a pointer so that "not supplied"
// is distinguishable from "set to empty". A nil field is left untouched.
// An explicit "" on a nullable column does clear it — that is how the UI
// empties a wrong URL or stale notes — but the same value on a required
// column is rejected rather than blanking a live row.
var app models.JobApplication
if err := r.Store.DB.First(&app, id).Error; err != nil {
return nil, err
}
oldStatus := app.Status
if input.JobTitle != nil {
if strings.TrimSpace(*input.JobTitle) == "" {
return nil, fmt.Errorf("jobTitle cannot be empty")
}
app.JobTitle = strings.TrimSpace(*input.JobTitle)
}
if input.Company != nil {
if strings.TrimSpace(*input.Company) == "" {
return nil, fmt.Errorf("company cannot be empty")
}
app.Company = strings.TrimSpace(*input.Company)
}
if input.Location != nil {
app.Location = input.Location
}
if input.URL != nil {
app.URL = input.URL
}
if input.Status != nil {
app.Status = *input.Status
}
if input.Notes != nil {
app.Notes = input.Notes
}
if input.AppliedAt != nil {
app.AppliedAt = input.AppliedAt
}
if input.Description != nil {
app.Description = input.Description
}
if input.NextAction != nil {
app.NextAction = input.NextAction
}
if input.NextActionAt != nil {
app.NextActionAt = input.NextActionAt
}
if input.ClearNextActionAt != nil && *input.ClearNextActionAt {
app.NextActionAt = nil
}
if input.ContactName != nil {
app.ContactName = input.ContactName
}
if input.ContactEmail != nil {
app.ContactEmail = input.ContactEmail
}
if input.ContactRole != nil {
app.ContactRole = input.ContactRole
}
if input.SalaryRange != nil {
app.SalaryRange = input.SalaryRange
}
if input.WorkArrangement != nil {
app.WorkArrangement = input.WorkArrangement
}
if input.ApplicationSource != nil {
app.ApplicationSource = input.ApplicationSource
}
if input.ResumeVersion != nil {
app.ResumeVersion = input.ResumeVersion
}
if input.RequisitionID != nil {
app.RequisitionID = input.RequisitionID
}
if input.LastContactedAt != nil {
app.LastContactedAt = input.LastContactedAt
}
if err := r.Store.DB.Save(&app).Error; err != nil {
return nil, err
}
if _, err := services.RecordApplicationEvent(r.Store.DB, app.ID, services.ApplicationEventInput{Kind: "updated", Source: "graphql"}); err != nil {
return nil, err
}
if oldStatus != app.Status {
if _, err := services.RecordApplicationEvent(r.Store.DB, app.ID, services.ApplicationEventInput{Kind: "status_changed", Source: "graphql", Note: stringPtr(oldStatus + " -> " + app.Status)}); err != nil {
return nil, err
}
}
return &app, nil
}
// DeleteJobApplication is the resolver for the deleteJobApplication field.
func (r *mutationResolver) DeleteJobApplication(ctx context.Context, id int) (bool, error) {
// Loaded first so a missing id is an error, as it is for the deletes that
// return the row (deletePost, deletePlace, deleteBookmark). Deleting by
// id alone reports success whether or not anything matched, which told
// the tracker a row was gone when it had never existed. The return type
// stays Boolean! for the front end's sake, so the only value is true.
//
// Soft delete: the row is kept with deleted_at set, not purged.
var app models.JobApplication
if err := r.Store.DB.First(&app, id).Error; err != nil {
return false, err
}
if err := r.Store.DB.Delete(&app).Error; err != nil {
return false, err
}
return true, nil
}
// AddJobApplicationEvent appends a user-entered follow-up or contact event.
func (r *mutationResolver) AddJobApplicationEvent(ctx context.Context, applicationID int, input model.AddJobApplicationEventInput) (*models.JobApplicationEvent, error) {
var app models.JobApplication
if err := r.Store.DB.First(&app, applicationID).Error; err != nil {
return nil, err
}
return services.RecordApplicationEvent(r.Store.DB, uint(applicationID), services.ApplicationEventInput{
Kind: input.Kind, OccurredAt: timeValue(input.OccurredAt), Note: input.Note,
Source: stringValue(input.Source), ContactName: input.ContactName,
ContactEmail: input.ContactEmail, ContactRole: input.ContactRole,
})
}
// JobApplications is the resolver for the jobApplications field.
func (r *queryResolver) JobApplications(ctx context.Context) ([]*models.JobApplication, error) {
// Admin-only even though it is a query (@admin in the schema): job
// applications are private.
//
// A row whose stored status is outside the JobStatus enum does not fail
// this list: services.MarshalJobStatus passes it through.
//
// Find can populate a slice of pointers directly, avoiding the
// copy-to-pointers loop the other list resolvers need.
var apps []*models.JobApplication
// id is the tie-break: one sync loop inserts several rows inside the same
// timestamp, and Postgres gives no stable order for equal sort keys, so
// without it the list can reshuffle between two identical page loads.
if err := r.Store.DB.Order("created_at desc, id desc").Find(&apps).Error; err != nil {
return nil, err
}
return apps, nil
}
// JobApplication is the resolver for the jobApplication field.
func (r *queryResolver) JobApplication(ctx context.Context, id int) (*models.JobApplication, error) {
var app models.JobApplication
if err := r.Store.DB.First(&app, id).Error; err != nil {
return nil, err
}
return &app, nil
}
// JobApplicationEvents is the explicit activity-history query for clients that
// do not need the rest of the application object.
func (r *queryResolver) JobApplicationEvents(ctx context.Context, applicationID int) ([]*models.JobApplicationEvent, error) {
return services.ListApplicationEvents(r.Store.DB, uint(applicationID))
}
// Analytics is calculated from the same application and activity history used
// by the tracker, so the dashboard does not maintain a second data model.
func (r *queryResolver) Analytics(ctx context.Context) (*model.Analytics, error) {
stats, err := services.LoadAnalytics(r.Store.DB)
if err != nil {
return nil, err
}
result := &model.Analytics{
TotalApplications: stats.TotalApplications, ResponseCount: stats.ResponseCount,
InterviewCount: stats.InterviewCount, OfferCount: stats.OfferCount,
ResponseRate: stats.ResponseRate, InterviewRate: stats.InterviewRate, OfferRate: stats.OfferRate,
AverageDaysToResponse: stats.AverageDaysToResponse, AverageDaysToInterview: stats.AverageDaysToInterview, AverageDaysToOffer: stats.AverageDaysToOffer,
}
for _, item := range stats.StatusCounts {
result.StatusCounts = append(result.StatusCounts, &model.AnalyticsStatusCount{Status: item.Status, Count: item.Count})
}
for _, item := range stats.Transitions {
result.Transitions = append(result.Transitions, &model.AnalyticsTransition{From: item.From, To: item.To, Count: item.Count})
}
return result, nil
}
// JobApplication returns JobApplicationResolver implementation.
func (r *Resolver) JobApplication() JobApplicationResolver { return &jobApplicationResolver{r} }
type jobApplicationResolver struct{ *Resolver }