package services import ( "fmt" "strings" "github.com/99designs/gqlgen/graphql" ) // The job-status vocabulary: its ranking, the forward-only rule, and how // GraphQL sees it. // // The schema declares `enum JobStatus` (graph/schema/job_application.graphql) // and gqlgen.yml binds it to MarshalJobStatus and UnmarshalJobStatus instead // of letting gqlgen generate a Go enum type. That keeps // models.JobApplication.Status a plain string column — no migration, and the // capture endpoint goes on writing strings — while the GraphQL boundary still // refuses a status nobody ranked. // // statusOrder is the vocabulary; the enum in the schema has to list the same // values, and TestJobStatusEnumMatchesStatusOrder fails when they differ. The // functions live in this package, rather than in graph, because the capture // path needs them too and graph already imports services. // statusOrder ranks the application lifecycle so an update can only move an // application forwards (see StatusAdvances). A status missing from this map is // treated as unknown and never applied. Note that "rejected" and "withdrawn" // sit at the top, so once an application reaches them nothing can move it // back. // // "saved" marks a posting captured from the browser, or promoted from the // leads inbox, but not applied to yet. It ranks lowest so that capturing the // posting again as "applied" advances the row. // // This map is the vocabulary. It is written down in three other places, which // must list the same statuses: `enum JobStatus` in // graph/schema/job_application.graphql (enforced — // TestJobStatusEnumMatchesStatusOrder fails on any difference), the tracker // page (web/src/views/jobStatus.js) and the capture userscript // (STATUS_LABELS in web/public/job-capture.user.js). var statusOrder = map[string]int{ "saved": 0, "applied": 1, "screening": 2, "assessment": 3, "interviewing": 4, "offer": 5, "rejected": 6, "withdrawn": 7, } // StatusAdvances reports whether next is a forward move from current through // statusOrder. It is what stops a repeat capture of a posting marked "saved" // dragging an interviewing application backwards. An unknown next status is // never applied; an unknown current status is treated as "anywhere before the // start", so any recognised status can take over from it. func StatusAdvances(current, next string) bool { nextOrder, nextExists := statusOrder[next] currentOrder, currentExists := statusOrder[current] return nextExists && (!currentExists || nextOrder > currentOrder) } // NormalizeStatus coerces a status that arrives outside GraphQL — the capture // endpoint's body — into the tracked vocabulary, trimming and lowering it and // rejecting anything outside statusOrder. An unrecognised status stored // verbatim would disable the forward-only guard for that row (StatusAdvances // allows any recognised status over an unknown one), so the safe fallback is // "applied". // // The GraphQL mutations do not use it — their status is a JobStatus, which // UnmarshalJobStatus refuses outright when it is not in the vocabulary rather // than quietly storing "applied". func NormalizeStatus(s string) string { lowered := strings.ToLower(strings.TrimSpace(s)) if _, ok := statusOrder[lowered]; ok { return lowered } return "applied" } // KnownStatus reports whether s is exactly one of the tracked statuses. No // trimming or case folding: see NormalizeStatus for the forgiving version the // capture endpoint uses on values that did not come through GraphQL. func KnownStatus(s string) bool { _, ok := statusOrder[s] return ok } // MarshalJobStatus writes a stored status to a GraphQL response. // // It is deliberately lenient, where the input side is strict. The column is // older than the enum, so a row can hold a status outside the vocabulary: the // tracker's early Title Case values ("Rejected"), or something written by // hand. A generated enum type would fail to serialise such a row, and because // JobApplication.status is non-null that one error would null out the whole // jobApplications list — one stale row hiding every application. So: // // - a value that is in the vocabulary once trimmed and lowered is emitted // in that canonical form, which also means an edit form sends back // something the input side accepts; // - anything else is emitted exactly as stored. That is not a legal // JobStatus, strictly, but the response is JSON and the tracker page // shows an unlisted status as-is so that it can be corrected, which is // more use than an error or a made-up substitute. // // The value is written with gqlgen's own String marshaller, so it is the same // quoted JSON string the field produced when it was declared String. func MarshalJobStatus(s string) graphql.Marshaler { if canonical := strings.ToLower(strings.TrimSpace(s)); KnownStatus(canonical) { s = canonical } return graphql.MarshalString(s) } // UnmarshalJobStatus reads a status from a mutation's input, and is where // drift is refused: only the exact lowercase values are accepted, so nothing // outside the vocabulary can be written through GraphQL. // // The value arrives as a string whether the client sent a JSON string // variable ("applied", which is what the tracker page does) or wrote the enum // literal in the query document (applied). func UnmarshalJobStatus(v any) (string, error) { s, ok := v.(string) if !ok { return "", fmt.Errorf("status must be a string") } if !KnownStatus(s) { return "", fmt.Errorf("%q is not a valid JobStatus", s) } return s, nil }