All files / src cql-patient.coffee

92.31% Statements 48/52
66.67% Branches 16/24
88.46% Functions 23/26
92.31% Lines 48/52

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 1391x 1x 1x   1x         16x           4x       2x   2x 2x       2x       3x         8x 8x 8x 8x 8x 16x   16x             1x             1x       1x 116x   116x 198x 198x     1x     1x     1x 18x           1x         1x 9x               1x 23x     1x   12x               1x 40x     1x     1x   9x 9x         1426x 1426x           1443x 1443x       1x 1x  
DT = require './datatypes/datatypes'
FHIR = require './fhir/models'
{ typeIsArray } = require './util/util'
 
toDate = (str) ->
  if typeof str is 'string' then new Date(str)
  else null
 
class Record
  constructor: (@json) ->
 
  get: (field) ->
    @json[field]
 
  getDate: (field) ->
    val = @get field
    if val? then DT.DateTime.parse(val) else null
 
  getInterval: (field) ->
    val = @get field
    if val? and typeof val is 'object'
      start = if val.start? then DT.DateTime.parse val.start else null
      end = if val.end? then DT.DateTime.parse val.end else null
      new DT.Interval(start, end)
 
  getDateOrInterval: (field) ->
    val = @get field
    if val? and typeof val is 'object' then @getInterval field else @getDate field
 
  getCode: (field) ->
    val = @get field
    if val? and typeof val is 'object' then new DT.Code(val.code, val.system, val.version)
 
class Patient
  constructor: (json) ->
    @identifier = json.identifier
    @name = json.name
    @gender = json.gender
    @birthDate = if json.birthDate? then DT.DateTime.parse json.birthDate
    @records = {}
    for r in json.records ? []
      @records[r.profile] ?= []
      @records[r.profile].push new Record(r)
 
  findRecords: (profile) ->
    if profile is 'patient-qicore-qicore-patient' then [@] else @records[profile] ? []
 
 
 
FHIR.Patient::records = ->
  @_records = {}
  for r in @json.records ? []
    @_records[r.profile] ?= []
    @_records[r.profile].push new Record(r)
  @_records
 
FHIR.Patient::findRecords = (profile) ->
  if profile is 'patient-qicore-qicore-patient' then [@] else @_bundle?.findRecords(profile) ? []
 
 
FHIR.Bundle::findRecords = (profile) ->
  filtered = @entry().filter (e)->
    e.resource()?.meta()?.profile()?.indexOf(profile) > -1
  for e in filtered
    r = e.resource()
    r._bundle = this
    r
 
FHIR.Bundle::findRecord = (profile) ->
  @findRecords(profile)[0]
 
FHIR.Base::get = (field) ->
  @[field]?.call(@)
 
FHIR.Base::getDate = (field) ->
  val = @get field
  if val instanceof DT.DateTime
    val
  else if typeof val is "string"
    DT.DateTime.parse(val)
 
FHIR.Base::getInterval= (field) ->
  val = @get field
  if val instannceOf FHIR.Period
    @periodToInterval val
 
FHIR.Base::getDateOrInterval = (field) ->
  val = @get field
  if val instanceof FHIR.Period
    @periodToInterval(val)
  else if typeof val is "string"
    DT.DateTime.parse(val)
  else if val instanceof  DT.DateTime
    val
 
FHIR.Base::getCode = (field) ->
  val = @get field
  @toCode(val)
 
FHIR.Base::toCode = (val) ->
  if typeIsArray(val)
    for c in val
      @toCode(c)
  else if val instanceof FHIR.CodeableConcept
    @codableConceptToCodes  val
  else if val instanceof FHIR.Coding
    @codingToCode val
 
 
FHIR.Base::codableConceptToCodes =(cc) ->
  for c in cc.coding()
    @codingToCode c
 
FHIR.Base::codingToCode = (coding) ->
  new DT.Code(coding.code(), coding.system(), coding.version())
 
FHIR.Base::periodToInterval =(val) ->
  if val instanceof FHIR.Period
    start =  val.getDate("start")
    end =  val.getDate("end")
    new DT.Interval(start, end)
 
 
class PatientSource
  constructor: (@patients) ->
    @nextPatient()
 
  currentPatient: ->
    @current_patient
 
  nextPatient: ->
    @current = @patients.shift()
    @current_bundle = if @current then new FHIR.Bundle(@current)
    @current_patient = @current_bundle?.findRecord("patient-qicore-qicore-patient")
 
 
module.exports.Patient = Patient
module.exports.PatientSource = PatientSource