Some checks failed
CI / lint (push) Successful in 54s
CI / notebooks-smoke (push) Successful in 1m43s
CI / test (push) Failing after 2m56s
Infra CI / zotero (push) Successful in 15s
Infra CI / notebooks (push) Successful in 1m1s
Infra CI / api (push) Successful in 1m7s
Infra CI / docs (push) Successful in 1m50s
Infra CI / llm (push) Successful in 49s
Infra CI / mc (push) Failing after 35s
Infra CI / bcda (push) Successful in 1m42s
CI / lint (pull_request) Successful in 45s
CI / notebooks-smoke (pull_request) Successful in 1m38s
CI / test (pull_request) Failing after 2m53s
Infra CI / mc (pull_request) Failing after 30s
Infra CI / zotero (pull_request) Successful in 16s
Infra CI / notebooks (pull_request) Successful in 58s
Infra CI / docs (pull_request) Successful in 16s
Infra CI / api (pull_request) Successful in 15s
Infra CI / bcda (pull_request) Successful in 21s
Infra CI / llm (pull_request) Successful in 17s
Closes #1293 #1294 #1295 #1296 #1297 #1298 #1299 #1300 (upstream review docs/superpowers/specs/2026-10-25-bcda-upstream-review.md). Partial exports (#1293). The manifest's `error` list is downloaded as OperationOutcome[.N].ndjson with resource_type=OperationOutcome; every URL is attempted and failures are recorded per file, then raised once as DownloadError. A job with error files is `partial`: it still reconciles (its rows are real) but never becomes the watermark, check_since warns `partial_job`, the Store page lists the issues and `stack bcda errors` prints them. Claim identity (#1294), per bcda.cms.gov "Matching multiple EOBs to the same claim": three new header columns — clm_cntl_num_match (VMS leading 1 stripped on SharedSystem DME), clm_natural_key (Part D events without a control number: bene · line from-date · provider qualifier · service provider · RX number · fill number) and superseded_by — plus prvdr_srvc_id_qlfyr_cd. bcda.sync keys claims on match → control number → natural key → id, marks every claim another current claim names in `related` as superseded (chain walked), counts `superseded`, and a new invariant forbids a live claim being named predecessor by another live claim. The connector dedupes on the same identity and drops superseded rows. Partitions and current tables flattened before these columns existed get them as NULLs. Attribution (#1295): `attribution_expired` (block) from expiration_dates, `attribution_floor` (warn) from the earliest last_attribution_update recorded on job rows (new column), and the server's 404/400/429 texts explained by explain_server_error. Adoption (#1296): completed_jobs() drops jobs older than the 24 h file retention; downloads no longer stop at the first failed file. _typeFilter (#1297): build_type_filter follows bcda/fhir/param.go — outcome lists, service-date earliest and latest bounds with eq/gt/ge/lt/le on YYYY[-MM[-DD]], no comma lists, no conflicting prefixes; CLI and Exports page expose them. Runout (#1298): `runout_window` warn after July; population column on the Sync page ledger. Vendoring (#1299): the manifest now pins bcda-app's param.go, bluebutton.go, service.go, constants, models and prod.yml, the docs source (CMSgov/bcda-static-site: filter/access/migrate/PAC/FAQ pages and announcements) and the PAC and unmapped-fields dictionaries. Docs (#1300): the reconciliation design record states the server's window `(since, transactionTime]`, the lookback bound, the silent exclusions, re-loads, partial exports and the identity rules. refs #1139
305 lines
9.6 KiB
Go
305 lines
9.6 KiB
Go
package fhir
|
|
|
|
import (
|
|
"fmt"
|
|
"slices"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/CMSgov/bcda-app/bcda/constants"
|
|
)
|
|
|
|
type TypeFilterSubquery struct {
|
|
ResourceType string
|
|
QueryParameters []TypeFilterSubqueryParam
|
|
}
|
|
|
|
type TypeFilterSubqueryParam struct {
|
|
Name string
|
|
Value string
|
|
}
|
|
|
|
type TypeFilterParamName string
|
|
|
|
const (
|
|
TypeFilterParamOutcome TypeFilterParamName = "outcome"
|
|
TypeFilterParamServiceDate TypeFilterParamName = "service-date"
|
|
TypeFilterParamTag TypeFilterParamName = "_tag"
|
|
)
|
|
|
|
type ParameterParsingError struct {
|
|
Details string
|
|
}
|
|
|
|
func (e ParameterParsingError) Error() string {
|
|
return fmt.Sprintf("malformed parameter: %s", e.Details)
|
|
}
|
|
|
|
type ParameterValidationError struct {
|
|
Details string
|
|
}
|
|
|
|
func (e ParameterValidationError) Error() string {
|
|
return fmt.Sprintf("invalid parameter: %s", e.Details)
|
|
}
|
|
|
|
func ParseTypeFilterSubquery(s string) (TypeFilterSubquery, error) {
|
|
// Expected format is: <resourceType>?<paramList>
|
|
resourceType, params, found := strings.Cut(s, "?")
|
|
if !found {
|
|
return TypeFilterSubquery{}, ParameterParsingError{Details: fmt.Sprintf("_typeFilter parameter missing question mark %s", s)}
|
|
} else if len(resourceType) == 0 {
|
|
return TypeFilterSubquery{}, ParameterParsingError{Details: fmt.Sprintf("_typeFilter parameter missing resource type %s", s)}
|
|
} else if len(params) == 0 {
|
|
return TypeFilterSubquery{}, ParameterParsingError{Details: fmt.Sprintf("_typeFilter parameter missing value %s", s)}
|
|
}
|
|
|
|
var subqueryParams []TypeFilterSubqueryParam
|
|
paramAry := strings.SplitSeq(params, "&")
|
|
for paramPair := range paramAry {
|
|
name, value, found := strings.Cut(paramPair, "=")
|
|
if !found {
|
|
return TypeFilterSubquery{}, ParameterParsingError{Details: fmt.Sprintf("_typeFilter value missing equals sign: %s", paramPair)}
|
|
}
|
|
subqueryParams = append(subqueryParams, TypeFilterSubqueryParam{Name: name, Value: value})
|
|
}
|
|
return TypeFilterSubquery{ResourceType: resourceType, QueryParameters: subqueryParams}, nil
|
|
}
|
|
|
|
func ValidateTypeFilterSubquery(subquery TypeFilterSubquery) error {
|
|
if subquery.ResourceType != "ExplanationOfBenefit" {
|
|
return ParameterValidationError{Details: fmt.Sprintf("invalid _typeFilter Resource Type (Only EOBs valid): %s", subquery.ResourceType)}
|
|
}
|
|
|
|
var serviceDateParams []DateParam
|
|
for _, param := range subquery.QueryParameters {
|
|
if strings.HasPrefix(param.Name, string(TypeFilterParamTag)) {
|
|
tag, err := ParseTokenParam(param)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = ValidateTag(tag)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
} else if strings.HasPrefix(param.Name, string(TypeFilterParamServiceDate)) {
|
|
date, err := ParseDateParam(param)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
serviceDateParams = append(serviceDateParams, date)
|
|
} else if strings.HasPrefix(param.Name, string(TypeFilterParamOutcome)) {
|
|
outcome, err := ParseTokenParam(param)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = ValidateOutcome(outcome)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
return ParameterValidationError{Details: fmt.Sprintf("invalid _typeFilter subquery parameter: %s", param.Name)}
|
|
}
|
|
}
|
|
err := ValidateServiceDates(serviceDateParams)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func GetTagParams(subquery TypeFilterSubquery) ([]TokenParam, error) {
|
|
tagParams := []TokenParam{}
|
|
for _, param := range subquery.QueryParameters {
|
|
if strings.HasPrefix(param.Name, string(TypeFilterParamTag)) {
|
|
tag, err := ParseTokenParam(param)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
tagParams = append(tagParams, tag)
|
|
}
|
|
}
|
|
return tagParams, nil
|
|
}
|
|
|
|
// https://hl7.org/fhir/R4/search.html#string
|
|
type StringParam struct {
|
|
Name string
|
|
Modifier string
|
|
Values []string
|
|
}
|
|
|
|
func ParseStringParam(subqueryParam TypeFilterSubqueryParam) (StringParam, error) {
|
|
s := StringParam{}
|
|
s.Name, s.Modifier, _ = strings.Cut(subqueryParam.Name, ":")
|
|
if len(s.Name) == 0 {
|
|
return s, ParameterParsingError{Details: "string parameter missing name"}
|
|
}
|
|
if len(subqueryParam.Value) == 0 {
|
|
return s, ParameterParsingError{Details: fmt.Sprintf("string parameter missing value: %s", s.Name)}
|
|
}
|
|
s.Values = strings.Split(subqueryParam.Value, ",")
|
|
return s, nil
|
|
}
|
|
|
|
// https://hl7.org/fhir/R4/search.html#date
|
|
type DateParam struct {
|
|
Name string
|
|
Prefix string
|
|
Datetimes []string
|
|
}
|
|
|
|
func ParseDateParam(subqueryParam TypeFilterSubqueryParam) (DateParam, error) {
|
|
d := DateParam{}
|
|
if len(subqueryParam.Name) == 0 {
|
|
return d, ParameterParsingError{Details: "date parameter missing key"}
|
|
}
|
|
d.Name = subqueryParam.Name
|
|
|
|
if len(subqueryParam.Value) == 0 {
|
|
return d, ParameterParsingError{Details: fmt.Sprintf("date parameter missing value: %s", d.Name)}
|
|
}
|
|
|
|
// BFD only supports eq, ge, gt, lt, le as of 2026-08-17. See: https://cmsgov.slack.com/archives/CMT1YS2KY/p1786716165942379
|
|
var prefixes = []string{"eq", "lt", "gt", "le", "ge"} // "ne", "sa", "eb", "ap"}
|
|
afterPrefix := ""
|
|
if len(subqueryParam.Value) >= 2 && slices.Contains(prefixes, subqueryParam.Value[:2]) {
|
|
d.Prefix = subqueryParam.Value[:2]
|
|
afterPrefix = subqueryParam.Value[2:]
|
|
} else {
|
|
afterPrefix = subqueryParam.Value
|
|
}
|
|
|
|
if len(afterPrefix) == 0 {
|
|
return d, ParameterParsingError{Details: fmt.Sprintf("value must include a valid FHIR date for date parameter %s", d.Name)}
|
|
}
|
|
|
|
d.Datetimes = strings.Split(afterPrefix, ",")
|
|
|
|
for _, datetime := range d.Datetimes {
|
|
_, err := ParseDateString(datetime)
|
|
if err != nil {
|
|
return d, ParameterParsingError{Details: fmt.Sprintf("invalid date parameter value: %s", datetime)}
|
|
}
|
|
}
|
|
return d, nil
|
|
}
|
|
|
|
func ParseDateString(datetime string) (time.Time, error) {
|
|
var datetimeFormats = []string{
|
|
"2006",
|
|
"2006-01",
|
|
"2006-01-02",
|
|
"2006-01-02T15:04:05",
|
|
"2006-01-02T15:04:05+07:00",
|
|
"2006-01-02T15:04:05-07:00",
|
|
"2006-01-02T15:04:05Z",
|
|
}
|
|
for _, format := range datetimeFormats {
|
|
if parsed, err := time.Parse(format, datetime); err == nil {
|
|
return parsed, nil
|
|
}
|
|
}
|
|
return time.Time{}, ParameterParsingError{Details: fmt.Sprintf("invalid date parameter value: %s", datetime)}
|
|
}
|
|
|
|
// ValidateServiceDates ensures that service-date parameters have valid keys, non-delimited values,
|
|
// and at most one earliest ("gt"/"ge") and one latest ("lt"/"le") condition.
|
|
// Exact date conditions ("eq" or "") establish both an earliest and a latest boundary.
|
|
func ValidateServiceDates(serviceDateParams []DateParam) error {
|
|
earliestBoundCount, latestBoundCount := 0, 0
|
|
for _, sd := range serviceDateParams {
|
|
if sd.Name != string(TypeFilterParamServiceDate) {
|
|
return ParameterValidationError{Details: fmt.Sprintf("invalid key for service-date parameter: %s", sd.Name)}
|
|
}
|
|
if len(sd.Datetimes) > 1 {
|
|
return ParameterValidationError{Details: "invalid service-date parameter value: comma-separated values are not supported"}
|
|
}
|
|
switch sd.Prefix {
|
|
case "lt", "le":
|
|
latestBoundCount = latestBoundCount + 1
|
|
case "gt", "ge":
|
|
earliestBoundCount = earliestBoundCount + 1
|
|
case "eq", "":
|
|
latestBoundCount = latestBoundCount + 1
|
|
earliestBoundCount = earliestBoundCount + 1
|
|
}
|
|
}
|
|
if earliestBoundCount > 1 || latestBoundCount > 1 {
|
|
return ParameterValidationError{Details: "invalid service-date parameter value: conflicting prefix conditions"}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// https://hl7.org/fhir/R4/search.html#token
|
|
type TokenParam struct {
|
|
Name string
|
|
Modifier string
|
|
Values []TokenValue
|
|
}
|
|
|
|
type TokenValue struct {
|
|
System string
|
|
Code string
|
|
}
|
|
|
|
func ParseTokenParam(subqueryParam TypeFilterSubqueryParam) (TokenParam, error) {
|
|
t := TokenParam{}
|
|
if len(subqueryParam.Name) == 0 {
|
|
return t, ParameterParsingError{Details: "token parameter missing key"}
|
|
}
|
|
t.Name, t.Modifier, _ = strings.Cut(subqueryParam.Name, ":")
|
|
if len(subqueryParam.Value) == 0 {
|
|
return t, ParameterParsingError{Details: fmt.Sprintf("token parameter missing value %s", t.Name)}
|
|
}
|
|
values := strings.SplitSeq(subqueryParam.Value, ",")
|
|
for value := range values {
|
|
before, after, found := strings.Cut(value, "|")
|
|
if found {
|
|
t.Values = append(t.Values, TokenValue{System: before, Code: after})
|
|
} else {
|
|
t.Values = append(t.Values, TokenValue{Code: before})
|
|
}
|
|
}
|
|
return t, nil
|
|
}
|
|
|
|
var validTagTokens = map[string][]string{
|
|
constants.BFDSystemTypeURL: {"SharedSystem", "NationalClaimsHistory", "DDPS"},
|
|
constants.BFDFinalActionURL: {"FinalAction", "NotFinalAction"},
|
|
}
|
|
|
|
func ValidateTag(t TokenParam) error {
|
|
if t.Name != string(TypeFilterParamTag) {
|
|
return ParameterValidationError{Details: fmt.Sprintf("invalid key for _tag parameter: %s", t.Name)}
|
|
}
|
|
if len(t.Modifier) > 0 {
|
|
return ParameterValidationError{Details: fmt.Sprintf("invalid _tag parameter; modifier %s not supported", t.Modifier)}
|
|
}
|
|
for _, value := range t.Values {
|
|
if len(value.System) == 0 || len(value.Code) == 0 {
|
|
return ParameterValidationError{Details: "invalid _tag parameter value. Searching by _tag requires a token (system|code) to be specified"}
|
|
}
|
|
validTagCodes, ok := validTagTokens[value.System]
|
|
if !ok || !slices.Contains(validTagCodes, value.Code) {
|
|
return ParameterValidationError{Details: fmt.Sprintf("invalid _tag parameter, unsupported code %s for system %s", value.Code, value.System)}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func ValidateOutcome(t TokenParam) error {
|
|
if t.Name != string(TypeFilterParamOutcome) {
|
|
return ParameterValidationError{Details: fmt.Sprintf("invalid key for outcome parameter: %s", t.Name)}
|
|
}
|
|
if len(t.Modifier) > 0 {
|
|
return ParameterValidationError{Details: fmt.Sprintf("invalid outcome parameter; modifier %s not supported", t.Modifier)}
|
|
}
|
|
for _, value := range t.Values {
|
|
if value.Code != "complete" && value.Code != "partial" {
|
|
return ParameterValidationError{Details: fmt.Sprintf("invalid outcome code: %s. Supported outcome codes are 'complete' and 'partial'", value.Code)}
|
|
}
|
|
}
|
|
return nil
|
|
}
|