Source identity
| Field | Recorded value |
|---|---|
| Source type | GitHub source |
| Original record | Janady13/missionmatch / internal/modules/matching/access_filter.go |
| Date | Not specified in source |
| Repository | Janady13/missionmatch |
| Revision | 720d47fae476b0d38477ae4401697d372b1ea2bc |
| Branch | feature/opportunity-to-outcome |
| Path or archive member | internal/modules/matching/access_filter.go |
| Original SHA-256 | 04f33943acc7767af23b3001fcec363baef0c17cdd4d5e7e249cff84f8585e1c |
| Git blob SHA | 89c28bad4a74866e2d31fd96a4089e390a98cccd |
| Reading-copy SHA-256 | 04f33943acc7767af23b3001fcec363baef0c17cdd4d5e7e249cff84f8585e1c |
Recovered source transcript
Download complete source text ↓99 lines · 4.2 KB
package matching
import (
"fmt"
"strings"
"github.com/missionworkforce/missionmatch/internal/modules/matching/intelligence"
"github.com/missionworkforce/missionmatch/internal/platform/database/dbgen"
)
// accessFactsFrom lifts the access columns off a candidate row into the pure function's input shape.
//
// pgtype.Date carries a Valid flag rather than being nil, so an unset date reaches Go as a zero value that
// LOOKS like 1 January year one. Passing that through unchecked would compute a break in access of about two
// thousand years and report every candidate as needing an investigation. The Valid check is the whole reason
// this function exists rather than the caller building the struct inline.
func accessFactsFrom(c dbgen.Candidate) intelligence.AccessFacts {
f := intelligence.AccessFacts{
ClearanceLevel: c.ClearanceLevel,
ClearanceStatus: c.ClearanceStatus,
CVEnrolled: c.CvEnrolled,
}
if c.AccessEndedOn.Valid {
t := c.AccessEndedOn.Time
f.AccessEndedOn = &t
}
if c.LastAdjudicatedOn.Valid {
t := c.LastAdjudicatedOn.Time
f.LastAdjudicated = &t
}
return f
}
// accessPostureSQL narrows the result set to the rows a requested posture could possibly fall in.
//
// THE RULE IS NOT DUPLICATED HERE, and that distinction is the point. Writing the full posture logic in SQL
// would put the twenty four month rule in two languages, and the day one is edited and the other is not, a
// search and a candidate record start disagreeing about whether the same person is placeable. So the SQL is
// deliberately LOOSE, a coarse date filter that never excludes a row the pure function would have included,
// and the exact decision stays in the one place that owns it.
//
// An unrecognised posture contributes nothing rather than erroring: the vocabulary is validated at the
// boundary, and a filter that silently matched everything would be worse than one that matched nothing.
func accessPostureSQL(postures []string, _ int) string {
var clauses []string
for _, p := range postures {
switch intelligence.AccessPosture(strings.TrimSpace(p)) {
case intelligence.PostureInAccess:
clauses = append(clauses, "c.cv_enrolled IS TRUE")
case intelligence.PostureUnknown:
clauses = append(clauses, "(c.access_ended_on IS NULL AND c.cv_enrolled IS DISTINCT FROM true)")
case intelligence.PostureReinstatable, intelligence.PostureClosing:
// Anything still inside the window. The two are separated by how much of it remains, which the
// projection decides.
clauses = append(clauses, fmt.Sprintf(
"(c.access_ended_on IS NOT NULL AND c.access_ended_on > CURRENT_DATE - make_interval(months => %d))",
intelligence.ReinstatementWindowMonths))
case intelligence.PostureInvestigationLikely:
// Past the window OR a stale adjudication, which fails independently of the clock.
clauses = append(clauses, fmt.Sprintf(
"(c.access_ended_on IS NOT NULL AND (c.access_ended_on <= CURRENT_DATE - make_interval(months => %d) "+
"OR (c.last_adjudicated_on IS NOT NULL AND c.last_adjudicated_on <= CURRENT_DATE - make_interval(years => %d))))",
intelligence.ReinstatementWindowMonths, intelligence.AdjudicationValidYears))
}
}
if len(clauses) == 0 {
return "false"
}
return "(" + strings.Join(clauses, " OR ") + ")"
}
// keepPosture reports whether a row's DERIVED posture is one the caller asked for. This is the exact half of
// the filter, run on the page the loose SQL returned.
func keepPosture(want []string, got intelligence.AccessPosture) bool {
if len(want) == 0 {
return true
}
for _, w := range want {
if intelligence.AccessPosture(strings.TrimSpace(w)) == got {
return true
}
}
return false
}
// ValidAccessPostures is the closed set the boundary accepts, so an unknown string is a clean rejection
// rather than a filter that silently matches nothing.
func ValidAccessPostures(in []string) []string {
out := make([]string, 0, len(in))
for _, v := range in {
switch intelligence.AccessPosture(strings.TrimSpace(v)) {
case intelligence.PostureInAccess, intelligence.PostureReinstatable, intelligence.PostureClosing,
intelligence.PostureInvestigationLikely, intelligence.PostureUnknown:
out = append(out, strings.TrimSpace(v))
}
}
return out
}