Breast Cancer Screening and Management (BCSM) Clinical Decision Support (CDS) Implementation Guide
1.0.0 - ci-build

Breast Cancer Screening and Management (BCSM) Clinical Decision Support (CDS) Implementation Guide - Local Development build (v1.0.0) built by the FHIR (HL7® FHIR® Standard) Build Tools. See the Directory of published versions

Library: PertinentHistory

Official URL: http://cancerscreeningcds.github.io/bcsm-cds/Library/PertinentHistory Version: 1.0.0
Draft as of 2025-02-28 Computable Name: PertinentHistory
Id: PertinentHistory
Version: 1.0.0
Url: PertinentHistory
Status: draft
Type:

system: http://terminology.hl7.org/CodeSystem/library-type

code: logic-library

Date: 2025-02-28 23:16:14+0000
Publisher: MITRE
Related Artifacts:

Dependencies

Parameters:
NameTypeMinMaxIn/Out
encounterstring01In
ObservationLookbackPeriodQuantity01In
PatientPatient01Out
UsualPatientNameHumanName0*Out
OfficialPatientNameHumanName0*Out
PatientNamestring01Out
BirthSexTextstring01Out
GenderTextstring01Out
CommunicationLanguageCodestring01Out
CommunicationLanguageTextstring01Out
LanguageCodestring01Out
LanguageTextAbbreviatedstring01Out
LanguageTextstring01Out
RaceExtensionValuesResource0*Out
RaceTextstring01Out
EthnicityExtensionValuesResource0*Out
EthnicityTextstring01Out
PertinentConditionsCondition0*Out
PertinentConditionsSummaryResource0*Out
PertinentFamilyMemberHistoryResource0*Out
PertinentFamilyMemberHistorySummaryResource0*Out
PertinentObservationsObservation0*Out
PertinentObservationsSummaryResource0*Out
PertinentProceduresProcedure0*Out
PertinentProceduresSummaryResource0*Out
BreastImagingDiagnosticReportsSummaryResource0*Out
BiopsyDiagnosticReportsSummaryResource0*Out
IncompleteBiopsyDiagnosticReportsSummaryResource0*Out
PertinentDiagnosticReportsSummaryResource0*Out
PertinentEncountersEncounter0*Out
PertinentEncountersSummaryResource0*Out
PertinentEpisodesOfCareEpisodeOfCare0*Out
PertinentEpisodesOfCareSummaryResource0*Out
DueOrOverdueScreeningSummaryResource0*Out
IncompleteScreeningOrdersSummaryResource0*Out
GeneticCounselingReferralOrdersSummaryResource0*Out
PertinentRequestsSummaryResource0*Out
PertinentHistoryResource01Out
Data Requirements:
TypeProfileMSCode Filter
Patient http://hl7.org/fhir/StructureDefinition/Patient ; code filter:
path: use

code: usual

Patient http://hl7.org/fhir/StructureDefinition/Patient ; code filter:
path: use

code: official

Patient http://hl7.org/fhir/StructureDefinition/Patient ;;;;;;;
Condition http://hl7.org/fhir/StructureDefinition/Condition ;;;;;;;;;; code filter:
path: code
value set: http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113883.3.526.3.378
Condition http://hl7.org/fhir/StructureDefinition/Condition ;;;;;;;;;; code filter:
path: code
value set: http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113762.1.4.1032.80
Condition http://hl7.org/fhir/StructureDefinition/Condition ;;;;;;;;;; code filter:
path: code
value set: http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113762.1.4.1032.367
Condition http://hl7.org/fhir/StructureDefinition/Condition ;;;;;;;;;; code filter:
path: code
value set: http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113883.3.464.1003.198.12.1068
Condition http://hl7.org/fhir/StructureDefinition/Condition ;;;;;;;;;; code filter:
path: code
value set: http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113883.3.464.1003.198.12.1069
Condition http://hl7.org/fhir/StructureDefinition/Condition ;;;;;;;;;; code filter:
path: code
value set: http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113883.3.464.1003.198.12.1070
Condition http://hl7.org/fhir/StructureDefinition/Condition ;;;;;;;;;; code filter:
path: code
value set: http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113883.3.464.1003.198.12.1071
Condition http://hl7.org/fhir/StructureDefinition/Condition ;;;;;;;;;; code filter:
path: code
value set: http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113762.1.4.1032.374
Condition http://hl7.org/fhir/StructureDefinition/Condition ;;;;;;;;;; code filter:
path: code
value set: http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113762.1.4.1032.379
Condition http://hl7.org/fhir/StructureDefinition/Condition ;;;;;;;;;; code filter:
path: code
value set: http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113762.1.4.1032.380
Condition http://hl7.org/fhir/StructureDefinition/Condition ;;;;;;;;;; code filter:
path: code

system: http://snomed.info/sct

code: 428850001

display: Li-Fraumeni syndrome (disorder)

Condition http://hl7.org/fhir/StructureDefinition/Condition ;;;;;;;;;; code filter:
path: code

system: http://snomed.info/sct

code: 58037000

display: Cowden syndrome (disorder)

Condition http://hl7.org/fhir/StructureDefinition/Condition ;;;;;;;;;; code filter:
path: code

system: http://snomed.info/sct

code: 234138005

display: Bannayan syndrome (disorder)

Condition http://hl7.org/fhir/StructureDefinition/Condition ;;;;;;;;;; code filter:
path: code

system: http://snomed.info/sct

code: 77941000119104

display: History of radiation therapy to chest

Condition http://hl7.org/fhir/StructureDefinition/Condition ;;;;;;;;;; code filter:
path: code
value set: http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113762.1.4.1032.392
FamilyMemberHistory http://hl7.org/fhir/StructureDefinition/FamilyMemberHistory ;
Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;;;;;;;;;; code filter:
path: code

system: http://cancerscreeningcds.github.io/bcsm-cds/CodeSystem/screening-observation-code-system

code: GeneticMarkerOrSyndrome

display: Genetic marker or syndrome associated with breast cancer

Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;;;;;;;;;; code filter:
path: code

system: http://cancerscreeningcds.github.io/bcsm-cds/CodeSystem/screening-observation-code-system

code: chestxrt

display: History of chest radiation ages 10-30 y

Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;;;;;;;;;; code filter:
path: code

system: http://cancerscreeningcds.github.io/bcsm-cds/CodeSystem/plan-definition-action-code-system

code: lifetimeriskassessment

display: Lifetime risk assessment

Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;;;;;;;;;; code filter:
path: code

system: http://cancerscreeningcds.github.io/bcsm-cds/CodeSystem/plan-definition-action-code-system

code: fhxrisktool

display: Brief familial risk assessment tool

Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;;;;;;;;;; code filter:
path: code

system: http://cancerscreeningcds.github.io/bcsm-cds/CodeSystem/plan-definition-action-code-system

code: 5yriskassessment

display: 5 year risk assessment

Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;;;;;;;;;; code filter:
path: code

system: http://cancerscreeningcds.github.io/bcsm-cds/CodeSystem/screening-observation-code-system

code: firstdeggenetic

display: First degree relative with genetic marker or syndrome

Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;;;;;;;;;; code filter:
path: code

system: http://cancerscreeningcds.github.io/bcsm-cds/CodeSystem/screening-observation-code-system

code: currentlylactating

display: Currently lactating

Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;;;;;;;;;; code filter:
path: code

system: http://loinc.org

code: 82810-3

display: Pregnancy status

code filter:
path: value
Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;;;;;;;;;; code filter:
path: code

system: http://cancerscreeningcds.github.io/bcsm-cds/CodeSystem/screening-observation-code-system

code: BreastSymptoms

display: New or worsening breast symptoms

Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;;;;;;;;;; code filter:
path: code

system: http://cancerscreeningcds.github.io/bcsm-cds/CodeSystem/screening-observation-code-system

code: BreastFindings

display: New or worsening breast exam findings

Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;;;;;;;;;; code filter:
path: code

system: http://cancerscreeningcds.github.io/bcsm-cds/CodeSystem/screening-observation-code-system

code: lifeexp

display: Reduced life expectancy indicator

Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;;;;;;;;;; code filter:
path: code

system: http://cancerscreeningcds.github.io/bcsm-cds/CodeSystem/plan-definition-action-code-system

code: familialriskassessment

display: Familial risk assessment

Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;;;;;;;;;; code filter:
path: code

system: http://cancerscreeningcds.github.io/bcsm-cds/CodeSystem/screening-observation-code-system

code: fhxgenetic

display: Family history of inherited cancer susceptibility

Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;;;;;;;;;; code filter:
path: code

system: http://cancerscreeningcds.github.io/bcsm-cds/CodeSystem/screening-observation-code-system

code: KnownVariantStatus

display: Known breast cancer variant status

Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;;;;;;;;;; code filter:
path: code

system: http://cancerscreeningcds.github.io/bcsm-cds/CodeSystem/screening-observation-code-system

code: brcageneancestry

display: Ancestry associated with BRCA1/2 gene mutations

Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;;;;;;;;;; code filter:
path: code

system: http://cancerscreeningcds.github.io/bcsm-cds/CodeSystem/screening-observation-code-system

code: PersonalRiskFactors

display: Personal risk factors conferring increased risk of breast cancer

Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;;;;;;;;;; code filter:
path: code

system: http://loinc.org

code: 10193-1

display: Physical findings of Breasts Narrative

Observation http://hl7.org/fhir/StructureDefinition/Observation ;;;;;;;;;;;; code filter:
path: code

system: http://loinc.org

code: 66110-8

display: Breast Pathology biopsy report

Procedure http://hl7.org/fhir/StructureDefinition/Procedure ;;;; code filter:
path: code
value set: http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113883.3.464.1003.198.12.1005
Procedure http://hl7.org/fhir/StructureDefinition/Procedure ;;;; code filter:
path: code
value set: http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113883.3.464.1003.198.12.1133
Procedure http://hl7.org/fhir/StructureDefinition/Procedure ;;;; code filter:
path: code
value set: http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113883.3.464.1003.198.12.1134
DiagnosticReport http://hl7.org/fhir/StructureDefinition/DiagnosticReport ;;;;;; code filter:
path: code
value set: http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113762.1.4.1032.394
DiagnosticReport http://hl7.org/fhir/StructureDefinition/DiagnosticReport ;;;;;;
DiagnosticReport http://hl7.org/fhir/StructureDefinition/DiagnosticReport ;;;;;; code filter:
path: code

system: http://loinc.org

code: 66110-8

display: Breast Pathology biopsy report

Encounter http://hl7.org/fhir/StructureDefinition/Encounter ;;;;;;;;
EpisodeOfCare http://hl7.org/fhir/StructureDefinition/EpisodeOfCare ;;;;;;;
CarePlan http://hl7.org/fhir/StructureDefinition/CarePlan ;;;;;;;;;;;;;; code filter:
path: status

code: active

ServiceRequest http://hl7.org/fhir/StructureDefinition/ServiceRequest ;;;;; code filter:
path: intent

code: plan

ServiceRequest http://hl7.org/fhir/StructureDefinition/ServiceRequest ;;;;; code filter:
path: code
value set: http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113762.1.4.1032.394
ServiceRequest http://hl7.org/fhir/StructureDefinition/ServiceRequest ;;;;; code filter:
path: code
value set: http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113762.1.4.1032.387
ServiceRequest http://hl7.org/fhir/StructureDefinition/ServiceRequest ;;;;;;
Content: text/cql
/*  
  Author: CMS Alliance to Modernize Healthcare, operated by THE MITRE Corporation.

  (C) 2024 The MITRE Corporation. All Rights Reserved. 
  Approved for Public Release: 24-2711. 
  Distribution Unlimited.

  Unless otherwise noted, this work is available under an Apache 2.0 license. 
  It was produced by the MITRE Corporation for the Division of Cancer Prevention 
  and Control, Centers for Disease Control and Prevention in accordance with the 
  Statement of Work, contract number 75FCMC18D0047, task order number 75D30123F17931.
*/

library PertinentHistory version '1.0.0'

using FHIR version '4.0.1'

include CDSConnectCommonsforFHIRv401 version '1.0.0' called C3F
include CSMCommonFunctions version '1.0.0' called Common
include BCSMCommonFunctions version '1.0.0' called BCSMCommon
include FHIRHelpers version '4.0.1' called FHIRHelpers
include DataElements version '1.0.0' called DataElements
include ScreeningDue version '1.0.0'
include ScreeningTestIncomplete version '1.0.0'

codesystem "SNOMED-CT": 'http://snomed.info/sct'

code "Category0": '397138000' from "SNOMED-CT" display 'Mammography assessment (Category 0) - Need additional imaging evaluation (finding)'
code "Category1": '397140005' from "SNOMED-CT" display 'Mammography assessment (Category 1) - Negative (finding)'
code "Category2": '397141009' from "SNOMED-CT" display 'Mammography assessment (Category 2) - Benign finding (finding)'
code "Category3": '397143007' from "SNOMED-CT" display 'Mammography assessment (Category 3) - Probably benign finding, short interval follow-up (finding)'
code "Category4": '397144001' from "SNOMED-CT" display 'Mammography assessment (Category 4) - Suspicious abnormality, biopsy should be considered (finding)'
code "Category4A": '6121000179106' from "SNOMED-CT" display 'Mammography assessment (Category 4A) - Suspicious abnormality, biopsy should be considered, low suspicion of malignancy (finding)'
code "Category4B": '6131000179108' from "SNOMED-CT" display 'Mammography assessment (Category 4B) - Suspicious abnormality, biopsy should be considered, moderate suspicion of malignancy (finding)'
code "Category4C": '6141000179100' from "SNOMED-CT" display 'Mammography assessment (Category 4C) - Suspicious abnormality, biopsy should be considered, high suspicion of malignancy (finding)'
code "Category5": '397145000' from "SNOMED-CT" display 'Mammography assessment (Category 5) - Highly suggestive of malignancy (finding)'
code "Category6": '6111000179101' from "SNOMED-CT" display 'Mammography assessment (Category 6) - known biopsy, proven malignancy (finding)'

context Patient

define PertinentHistory:
{
  patientInfo: {
    name: PatientName,
    id: Patient.identifier,
    isPregnant: DataElements.Pregnant,
    dateOfBirth: Patient.birthDate,
    sexAtBirth: BirthSexText,
    age: AgeInYears(),
    gender: GenderText,
    primaryLanguage: LanguageText,
    race: RaceText + '; ' + EthnicityText
  },
  patientHistory: {
    conditions: PertinentConditionsSummary,
    familyMemberHistory: PertinentFamilyMemberHistorySummary,
    observations: PertinentObservationsSummary,
    procedures: PertinentProceduresSummary,
    diagnosticReports: PertinentDiagnosticReportsSummary,
    encounters: PertinentEncountersSummary,
    episodeOfCares: PertinentEpisodesOfCareSummary,
    requests: PertinentRequestsSummary
  }
}

//------------------------------------------------------------------------------
// PERTINENT DEMOGRAPHICS
//------------------------------------------------------------------------------

define PatientName:
  if Exists(UsualPatientName) then
    FormatPatientName(First(UsualPatientName))
  else if Exists(OfficialPatientName) then
    FormatPatientName(First(OfficialPatientName))
  else
    FormatPatientName(First(Patient.name))

define UsualPatientName:
  Patient.name N
    where N.use = 'usual'
    return N

define OfficialPatientName:
  Patient.name N
    where N.use = 'official'
    return N

define function FormatPatientName(name FHIR.HumanName):
  First(name.given) + ' ' + name.family

define BirthSexText:
  if DataElements.BirthSexCode = 'F' then 'Female'
  else if DataElements.BirthSexCode = 'M' then 'Male'
  else 'Unknown'  

define GenderText:
  if Exists(DataElements.GenderIdentityExtension) 
  then DataElements.GenderIdentityExtension[0].value.coding[0].code.value
  else Patient.gender.value  

define LanguageText:
  case
    when LanguageTextAbbreviated = 'en' then 'English'
    when LanguageTextAbbreviated = 'es' then 'Spanish'
    when LanguageTextAbbreviated = 'zh' then 'Chinese'
    when LanguageTextAbbreviated = 'fr' then 'French'
    when LanguageTextAbbreviated = 'tl' then 'Tagalog'
    when LanguageTextAbbreviated = 'vi' then 'Vietnamese'
    else 'Unknown'
  end  

define LanguageTextAbbreviated:
  Coalesce( {CommunicationLanguageCode, CommunicationLanguageText, LanguageCode} )

define CommunicationLanguageCode:
  Patient.communication.language.coding.code.value[0]

define CommunicationLanguageText:
  Patient.communication.language.text.value[0]

define LanguageCode:
  Patient.language.value

define RaceText:
  Coalesce(Coalesce(RaceExtensionValues))

define RaceExtensionValues:
  (Patient.extension) E 
    where E.url = 'http://hl7.org/fhir/us/core/StructureDefinition/us-core-race'
    return (E.extension) X where X.url = 'text' return FHIRHelpers.ToString(X.value as FHIR.string)

define EthnicityText:
  Coalesce(Coalesce(EthnicityExtensionValues))

define EthnicityExtensionValues:
  (Patient.extension) E
    where E.url = 'http://hl7.org/fhir/us/core/StructureDefinition/us-core-ethnicity'
    return (E.extension) X where X.url = 'text' return FHIRHelpers.ToString(X.value as FHIR.string)  

//------------------------------------------------------------------------------
// PERTINENT CONDITIONS
//------------------------------------------------------------------------------

define PertinentConditions:
  DataElements.BreastCancerDiagnoses union
  DataElements.BilateralMastectomyConditions union
  DataElements.LeftUnilateralMastectomyConditions union
  DataElements.RightUnilateralMastectomyConditions union
  DataElements.UnilateralMastectomyConditions union
  DataElements.BreastSymptomConditions union
  DataElements.PregnancyDiagnoses union
  DataElements."BRCA-1MutationConditions" union
  DataElements."BRCA-2MutationConditions" union
  DataElements."Li-FraumeniSyndromeConditions" union
  DataElements.CowdenSyndromeConditions union
  DataElements."Bannayan-Riley-RuvalcabaSyndromeConditions" union
	DataElements."de-chestxrt" union
	DataElements.AtypicalBiopsyConditions

define PertinentConditionsSummary:
  (PertinentConditions) C
  return {
    name: Common.ConceptText(C.code),
    value: C.clinicalStatus.coding[0].code.value,
    date: Common.DateTimeText(
      Coalesce(
        C.onset as FHIR.dateTime,
        C.recordedDate
      )
    ),
    reference: 'Condition/' + C.id,
    edited: HasBeenLocallyEdited(C)
  }
  sort by (date as String) desc

//------------------------------------------------------------------------------
// PERTINENT FAMILY MEMBER HISTORY
//------------------------------------------------------------------------------  

define PertinentFamilyMemberHistory:
  DataElements.FamilyMemberBreastOvarianTubalPeritonealCancerCondition union
  DataElements.FamilyMemberGeneticMarkerOrSyndromeConditions

define PertinentFamilyMemberHistorySummary:
  Flatten((PertinentFamilyMemberHistory) FMH
    return 
      FMH.conditions C return {
        name: Common.ConceptText(FMH.familyMemberHistory.relationship),
        value: Common.ConceptText(C.code),
        onset: Onset(C),
        reference: 'FamilyMemberHistory/' + FMH.familyMemberHistory.id,
        edited: HasBeenLocallyEdited(FMH.familyMemberHistory)
  })
  
//------------------------------------------------------------------------------
// PERTINENT OBSERVATIONS
//------------------------------------------------------------------------------

define PertinentObservations:
  ({DataElements.MostRecentLactatingObservation} union
  DataElements.PregnancyObservations union
  DataElements.NewOrWorseningBreastSymptomsObservations union
  DataElements.NewOrWorseningBreastFindingsObservations union
  {DataElements.MostRecentReducedLifeExpectancyObservation} union
  {DataElements.MostRecentGeneticMarkerOrSyndromeObservation} union
  {DataElements.HistoryOfChestRadiationAges10To30Observation} union
  {DataElements.MostRecentLifetimeRiskAssessment} union
  {DataElements.MostRecentFamilialRiskAssessmentObservation} union
  {DataElements.MostRecentFamilialRiskAssessmentResult} union
  {DataElements.FamilyGeneticHistoryAssessmentObservation} union
  {DataElements.KnownGeneticMarkerOrSyndromeStatusObservation} union
  {DataElements.MostRecentBRCAGeneAncestryObservation} union
  {DataElements.MostRecent5YrRiskAssessment} union
  {DataElements.MostRecentPersonalRiskFactorAssessment} union
  {DataElements.MostRecentFirstDegRelativeGeneticObservation})
  except {null}

define PertinentObservationsSummary:
  (PertinentObservations) Obs
  return {
    name: Common.ConceptText(Obs.code),
    value: Common.ConceptText(Obs.value),
    date: Common.ObservationDateString(Obs),
    reference: 'Observation/' + Obs.id,
    edited: HasBeenLocallyEdited(Obs)
  }
  sort by (date as String) desc

//------------------------------------------------------------------------------
// PERTINENT PROCEDURES
//------------------------------------------------------------------------------

define PertinentProcedures:
  DataElements.BilateralMastectomyProcedures union
  DataElements.LeftUnilateralMastectomyProcedures union
  DataElements.RightUnilateralMastectomyProcedures

define PertinentProceduresSummary:
  (PertinentProcedures) P
  return {
    name: Common.ConceptText(P.code),
    value: P.status.value,
    date: Common.ProcedureDateString(P),
    reference: 'Procedure/' + P.id,
    edited: HasBeenLocallyEdited(P)
  } sort by date desc  

//------------------------------------------------------------------------------
// PERTINENT DIAGNOSTIC REPORTS
//------------------------------------------------------------------------------

define PertinentDiagnosticReportsSummary:
  (
    BreastImagingDiagnosticReportsSummary
    union BiopsyDiagnosticReportsSummary
    union IncompleteBiopsyDiagnosticReportsSummary
  ) D
  sort by date desc

define BreastImagingDiagnosticReportsSummary:
  FormatReports(DataElements.BreastImagingDiagnosticReports, DataElements.BreastImagingObservations, 'Breast Imaging')

define BiopsyDiagnosticReportsSummary:
  FormatReports(DataElements.BiopsyDiagnosticReports, DataElements.BiopsyObservations, 'Breast Biopsy')  

define IncompleteBiopsyDiagnosticReportsSummary:
  FormatReports(ScreeningTestIncomplete.IncompleteScreeningPathology, DataElements.BiopsyObservations, 'Breast Biopsy')    

//------------------------------------------------------------------------------
// PERTINENT ENCOUNTERS
//------------------------------------------------------------------------------

define PertinentEncounters:
  DataElements.AllBreastSymptomEncounters union
  DataElements.GeneticCounselingEncounters

define PertinentEncountersSummary:
  (PertinentEncounters) E
  return {
    name: Common.ConceptText(E.reasonCode[0]),
    value: E.status,
    date: Common.DateTimeText((E.period as FHIR.Period)."start") +
      ' to ' +
      Common.DateTimeText((E.period as FHIR.Period)."end"),
    reference: 'Encounter/' + E.id
  }

//------------------------------------------------------------------------------
// PERTINENT EPISODES OF CARE
//------------------------------------------------------------------------------
  define PertinentEpisodesOfCare:
    DataElements.PregnancyEpisodesOfCare

define PertinentEpisodesOfCareSummary:
  (PertinentEpisodesOfCare) E
  return {
    name: Common.ConceptText(E.type[0]),
    value: E.status.value,
    date:
      displayStartDate(Common.PeriodObject(E.period)."Start") +
      ' to ' +
      displayEndDate(Common.PeriodObject(E.period)."End"),
    reference: 'EpisodeOfCare/' + E.id,
    edited: HasBeenLocallyEdited(E)
  }
  sort by (date as String) desc


//------------------------------------------------------------------------------
// PERTINENT REQUESTS
//------------------------------------------------------------------------------  

define DueOrOverdueScreeningSummary:
  (ScreeningDue.DueOrOverdue) R
    return
  {
    name: Common.ConceptText(R.code),
    intent: R.intent, 
    occurrence: R.nextDue,
    reference: null as String,
    flag: R.flag.display,
    plannedRepeat: R.plannedRepeat
  }  

define IncompleteScreeningOrdersSummary:
  (ScreeningTestIncomplete.IncompleteScreeningOrders) R
    return
  {
    name: Common.ConceptText(R.code),
    intent: FHIRHelpers.ToString(R.intent), 
    occurrence: FHIRHelpers.ToDateTime(R.occurrence as FHIR.dateTime),
    reference: 'ServiceRequest/' + R.id,
    flag: null as String,
    plannedRepeat: null as String
  } 

define GeneticCounselingReferralOrdersSummary:
  (DataElements.GeneticCounselingReferralOrders) R
    return
  {
    name: Common.ConceptText(R.code),
    intent: FHIRHelpers.ToString(R.intent), 
    occurrence: FHIRHelpers.ToDateTime(R.occurrence as FHIR.dateTime),
    reference: 'ServiceRequest/' + R.id,
    flag: null as String,
    plannedRepeat: null as String
  }  

define PertinentRequestsSummary:
  DueOrOverdueScreeningSummary union
  IncompleteScreeningOrdersSummary union
  GeneticCounselingReferralOrdersSummary

//------------------------------------------------------------------------------
// FUNCTIONS
//------------------------------------------------------------------------------

define function HasBeenLocallyEdited(R FHIR.DomainResource):
  R.extension ext
    where
      ext.url = 'http://cancerscreeningcds.github.io/bcsm-cds/StructureDefinition/bcsm-edited'
      and ext.value as FHIR.boolean = true

define function Onset(c FHIR.FamilyMemberHistory.Condition):
  Coalesce( List{
    Common.QuantityText(c.onset as FHIR.Age),
    Common.PeriodObject(c.onset as FHIR.Period),
    Common.RangeObject(c.onset as FHIR.Range),
    (c.onset as FHIR.string).value
  })      

define function FormatReports(DrList List<DiagnosticReport>, ObsList List<Observation>, ReportType String):
  (DrList) D
  let CollatedCodes: BCSMCommon.CollateConclusionCodes(D,ObsList),
  HighestRanked: HighestRankedResult(CollatedCodes)
  return {
    name: ReportType,
    longName: Common.ConceptText(D.code),
    value: HighestRanked.display,
    longValue: flatten(
      (CollatedCodes) cC
        return (cC.coding) cCC
          return cCC.display
    ),
    date: Common.DiagnosticReportDateString(D),
    reference: 'DiagnosticReport/' + D.id,
    edited: HasBeenLocallyEdited(D)
  }      

//return biopsy result annotation or BI-RADS result annotation if exists, otherwise return first coding
define function HighestRankedResult(CollatedCodes List<FHIR.CodeableConcept>):
  Coalesce(
    First(CollatedCodes cC    
    return First(cC.coding C 
      where C.system = 'http://cancerscreeningcds.github.io/bcsm-cds/CodeSystem/biopsy-result-observation-code-system'
    )),
    First(CollatedCodes cC    
    return First(cC.coding coding
      where FHIRHelpers.ToCode(coding) in {Category0, Category1, Category2, Category3, Category4, Category4A, Category4B, Category4C, Category5, Category6}
    )),
    First(CollatedCodes cC return First(cC.coding C))
  )
  
define function displayStartDate(startDate String):
  if startDate is not null then
    startDate
  else
    'no start date available'

define function displayEndDate(endDate String):
  if endDate is not null then
    endDate
  else
    'no end date available'
Content: application/elm+xml
Encoded data (416636 characters)
Content: application/elm+json
Encoded data (778928 characters)