# All files / blue-button/lib/parser/cda/sections medications.js

80.58% Statements 137/170
100% Branches 1/1
100% Functions 1/1
80.58% Lines 137/170

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
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    1711x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x

    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    1x
    1x
    1x
     "use strict";

    var shared = require("../shared");
    var component = require("@amida-tech/blue-button-xml").component;
    var cleanup = require("../cleanup");
    var processor = require("@amida-tech/blue-button-xml").processor;
    var bbm = require("@amida-tech/blue-button-meta");
    var _ = require("lodash");

    var exportMedicationsSection = function (version) {
      var sectionIDs = bbm.CCDA["sections" + version];
      var clinicalStatementsIDs = bbm.CCDA["statements" + version];

      var MedicationInterval = component.define("MedicationInterval")
        .fields([
          ["phase", "0..1", "./h:phase", shared.EffectiveTime],
          ["period", "0..1", "./h:period", shared.PhysicalQuantity],
          ["alignment", "0..1", "./@alignment"],
          ["frequency", "0..1", "./@institutionSpecified", processor.asBoolean],
          ["event", "0..1", "./h:event/@code", shared.SimpleCode("2.16.840.1.113883.5.139")],
          ["event_offset", "0..1", "./h:offset", shared.EventOffset]
        ]);

      var MedicationAdministration = component.define("MedicationAdministration")
        .fields([
          ["route", "0..1", "h:routeCode", shared.ConceptDescriptor],
          ["site", "0..1", "h:approachSiteCode", shared.ConceptDescriptor],
          ["form", "0..1", "h:administrationUnitCode", shared.ConceptDescriptor],
          ["dose", "0..1", "h:doseQuantity", shared.PhysicalQuantity],
          ["rate", "0..1", "h:rateQuantity", shared.PhysicalQuantity],
          ["dose_restriction", "0..1", "h:maxDoseQuantity", shared.PhysicalQuantity],
          ["interval", "0..1", "h:effectiveTime[@operator='A']", MedicationInterval],
        ]);

      var MedicationIndication = component.define("MedicationIndication")
        .fields([
          ["identifiers", "0..*", "h:id", shared.Identifier],
          ["code", "0..1", "h:code", shared.ConceptDescriptor],
          ["date_time", "0..1", "h:effectiveTime", shared.EffectiveTime],
          ["value", "0..1", "h:value", shared.ConceptDescriptor]
        ]);

      var MedicationPrecondition = component.define("MedicationPrecondition")
        .fields([
          ["code", "0..1", "h:code", shared.ConceptDescriptor],
          ["text", "0..1", "h:text"],
          ["value", "0..1", "h:value", shared.ConceptDescriptor]
        ]);

      var author = component.define("author")
        .fields([
          ["date_time", "0..1", "h:time", shared.EffectiveTime],
          ["identifiers", "0..*", "h:assignedAuthor/h:id", shared.Identifier],
          ["organization", "0..1", "h:assignedAuthor/h:representedOrganization", shared.Organization]
        ]);

      var MedicationInformation = component.define("MedicationInformation")
        .templateRoot("2.16.840.1.113883.10.20.22.4.23")
        .fields([
          ["identifiers", "0..*", "h:id", shared.Identifier],
          ["unencoded_name", "0..1", "h:manufacturedMaterial/h:code/h:originalText", shared.TextWithReference],
          ["product", "1..1", "h:manufacturedMaterial/h:code", shared.ConceptDescriptor],
          ["manufacturer", "0..1", "h:manufacturerOrganization/h:name"]
        ]);

      var MedicationSupplyOrder = component.define("MedicationSupplyOrder")
        .fields([
          ["identifiers", "0..*", "h:assignedEntity/h:id", shared.Identifier],
          ["date_time", "0..1", "h:effectiveTime", shared.EffectiveTime],
          ["repeatNumber", "0..1", "h:repeatNumber/@value"],
          ["quantity", "0..1", "h:quantity/@value"],
          ["status", "0..1", "h:status/@code"],
          ["author", "0..1", "h:author", author] //, instructions use references, which are not supported (also samples don't have good data for it)
          //["instructions", "0..1", "h:entryRelationship[@typeCode='SUBJ']/h:act", MedicationInstructions]
        ]);

      var MedicationPerformer = component.define("MedicationPerformer")
        .fields([
          ["identifiers", "0..*", "h:assignedEntity/h:id", shared.Identifier],
          ["address", "0..*", "h:assignedEntity/h:addr", shared.Address],
          ["phone", "0..1", "h:assignedEntity/" + shared.phone.xpath(), shared.phone],
          ["organization", "0..*", "h:assignedEntity/h:representedOrganization", shared.Organization]
        ]);
      /*
            var MedicationDrugVehicle = component.define("MedicationDrugVehicle")
                .templateRoot("2.16.840.1.113883.10.20.22.4.24")
                .fields([
                    ["playingEntity", "0..1", "h:playingEntity/h:code", shared.ConceptDescriptor]
                ]).cleanupStep(cleanup.extractAllFields(["drug_vehicle"]));
    */
      var MedicationInstructions = component.define("MedicationInstructions")
        .fields([
          ["code", "0..1", "../h:code", shared.ConceptDescriptor],
          ["freeText", "0..1", "../h:text", shared.TextWithReference]
        ]);

      var MedicationDispense = component.define("MedicationDispense")
        .templateRoot("2.16.840.1.113883.10.20.22.4.18")
        .fields([
          ["identifiers", "0..*", "h:id", shared.Identifier],
          ["performer", "0..1", "h:performer", MedicationPerformer],
          ["supply", "0..1", "h:entryRelationship[@typeCode='REFR']/h:supply", MedicationSupplyOrder]
        ]);

      var MedicationActivity = component.define("MedicationActivity")
        .templateRoot("2.16.840.1.113883.10.20.1.34")
        .fields([
          ["date_time", "0..1", "h:effectiveTime[not (@operator='A')]", shared.EffectiveTime],
          ["identifiers", "0..*", "h:id", shared.Identifier],
          ["status", "1..1", "./../h:supply/@moodCode"],
          //["sig", "0..1", "h:text", shared.TextWithReference],
          ["product", "1..1", "h:product/h:manufacturedProduct", MedicationInformation],
          ["supply", "0..1", "./../h:supply", MedicationSupplyOrder],
          //["administration", "0..1", "../h:substanceAdministration", MedicationAdministration],
          //["performer", "0..1", "h:performer", MedicationPerformer],
          //["drug_vehicle", "0..1", "h:participant[@typeCode='CSM']/h:participantRole/h:playingEntity[@classCode='MMAT']/h:code", shared.ConceptDescriptor],
          //["precondition", "1..1", "h:precondition/h:criterion", MedicationPrecondition],
          //["indication", "0..1", "h:entryRelationship[@typeCode='RSON']/h:observation", MedicationIndication],
          //["instructions", "0..1", "h:entryRelationship[@typeCode='REFR']/h:supply/*/*/h:templateId[@root='2.16.840.1.113883.10.20.22.4.20']", MedicationInstructions],
          //["dispense", "0..1", MedicationDispense.xpath(), MedicationDispense]
        ])
        //.cleanupStep(Cleanup.extractAllFields(["medicationName"]))
        .cleanupStep(function () {

          //Cleanup Status.

          if (_.get(this, "js.status") === "EVN") {
            this.js.status = "Completed";
          }
          if (_.get(this, "js.status") === "INT") {
            this.js.status = "Prescribed";
          }

          // separate out two effectiveTimes

          /*
              // 1.  startDate --- endDate
              var range = this.js.times.filter(function(t){
                return -1 === ['PIVL_TS', 'EIVL_TS'].indexOf(t.js.xsitype);
              });

              // 2.  dosing interval
              var period= this.js.times.filter(function(t){
                return -1 !== ['PIVL_TS', 'EIVL_TS'].indexOf(t.js.xsitype);
              });

              delete this.js.times;

              if (range.length > 0) {
                this.js.dateRange = range[0];
              }

              if (period.length > 0) {
                this.js.dosePeriod = period[0].js.period;
              }*/

        });

      var medicationsSection = component.define("medicationsSection");
      medicationsSection.templateRoot(["2.16.840.1.113883.10.20.1.8"]);
      medicationsSection.fields([
        ["medications", "0..*", MedicationActivity.xpath(), MedicationActivity]
      ]);
      medicationsSection.cleanupStep(cleanup.replaceWithField('medications'));
      return [medicationsSection, MedicationActivity];

    };

    exports.medicationsSection = exportMedicationsSection;
    exports.medicationsEntry = exportMedicationsSection;