Generated Narrative: StructureMap ExtractMedicationRequest #xD; /// url = 'https://fhir.slade360.co.ke/fhir/StructureMap/ExtractMedicationRequest'#xD; /// name = 'ExtractMedicationRequest'#xD; /// status = 'draft'#xD; #xD; uses "http://hl7.org/fhir/StructureDefinition/QuestionnaireResponse" alias QR as source#xD; uses "http://hl7.org/fhir/StructureDefinition/Bundle" alias Bundle as target#xD; uses "http://hl7.org/fhir/StructureDefinition/MedicationRequest" alias MedicationRequest as target#xD; uses "http://hl7.org/fhir/StructureDefinition/Medication" alias Medication as target#xD; #xD; group ExtractMedicationRequest(source qr : QR, target bundle : Bundle) {#xD; qr -> bundle.type = 'transaction' "setBundleType";#xD; qr.item as prescriptionGroup where (linkId = 'prescription-form') then {#xD; prescriptionGroup -> bundle.entry as entry, entry.resource = create('MedicationRequest') as medReq then {#xD; qr -> medReq, entry then BuildMedicationRequest(qr, prescriptionGroup, medReq, entry) "buildMedReq";#xD; } "createMedReq";#xD; } "findPrescriptionGroup";#xD; }#xD; #xD; group BuildMedicationRequest(source qr : QR, source prescriptionGroup, target medReq : MedicationRequest, target entry) {#xD; qr -> medReq.status = 'draft' "setStatus"; // status will always be draft until medication has been signed off#xD; qr -> medReq.intent = 'order' "setIntent";#xD; qr.subject as s -> medReq.subject = s "setSubject"; // Subject from QuestionnaireResponse#xD; qr.encounter as enc -> medReq.encounter = enc "setEncounter"; // Encounter from QuestionnaireResponse#xD; qr.authored as authored -> medReq.authoredOn = authored "setAuthoredOn"; // AuthoredOn from QuestionnaireResponse.authored#xD; qr.source as src -> medReq.requester = src "setRequester"; // Requester from QuestionnaireResponse.source (the Organization)#xD; qr.identifier first as qrIdent -> medReq.identifier = qrIdent "copyIdentifier"; // Copy identifier from QuestionnaireResponse to MedicationRequest#xD; qr.extension first as ext then {#xD; ext.valueCodeableConcept as medCC -> medReq.medication as med then {#xD; medCC -> med.concept = medCC "setMedConcept";#xD; } "accessMedication";#xD; } "findMedicationExt"; // Medication as CodeableReference from QR extension (using concept)#xD; prescriptionGroup.item as priorityItem where (linkId = 'priority') then {#xD; priorityItem.answer first as priorityAns then {#xD; priorityAns.valueCoding as priorityCoding then {#xD; priorityCoding.code as pCode -> medReq.priority = pCode "setPriorityCode";#xD; } "extractPriorityCode";#xD; } "extractPriority";#xD; } "findPriority"; // Priority#xD; qr -> medReq.dosageInstruction = create('Dosage') as dosage then {#xD; prescriptionGroup -> dosage then BuildDosage(prescriptionGroup, dosage) "buildDosage";#xD; } "createDosage"; // Dosage Instruction#xD; prescriptionGroup.item as substItem where (linkId = 'substitutable') then {#xD; substItem.answer first as substAns then {#xD; substAns.valueBoolean as substBool -> medReq.substitution as subst then {#xD; substBool -> subst.allowedBoolean = substBool "setSubstValue";#xD; } "createSubst";#xD; } "extractSubst";#xD; } "findSubst"; // Substitution#xD; qr -> medReq.dispenseRequest as dispReq then {#xD; prescriptionGroup -> dispReq then BuildDispenseRequest(qr, prescriptionGroup, dispReq) "buildDispReq";#xD; } "createDispReq"; // Dispense Request#xD; qr -> medReq.id = uuid() then SetMedicationRequestFullUrl(medReq, entry) "setMedReqIdAndFullUrl"; // Set ID and FullUrl#xD; qr -> entry.request as request then {#xD; qr -> request.method = 'POST' "reqMethod";#xD; qr -> request.url = 'MedicationRequest' "reqUrl";#xD; } "entryRequest"; // Entry request for transaction bundle#xD; }#xD; #xD; group BuildDosage(source prescriptionGroup, target dosage : Dosage) {#xD; prescriptionGroup -> dosage.timing as timing then {#xD; prescriptionGroup -> timing.repeat as repeat then {#xD; prescriptionGroup.item as freqItem where (linkId = 'frequency') then {#xD; freqItem.answer first as freqAns then {#xD; freqAns.valueCoding as freqCoding where (code = 'OD') -> repeat.frequency = '1' "setFreqOD";#xD; freqAns.valueCoding as freqCoding where (code = 'BD') -> repeat.frequency = '2' "setFreqBD";#xD; freqAns.valueCoding as freqCoding where (code = 'TID') -> repeat.frequency = '3' "setFreqTID";#xD; freqAns.valueCoding as freqCoding where (code = 'QID') -> repeat.frequency = '4' "setFreqQID";#xD; } "extractFreq";#xD; } "findFreq"; // Frequency (OD, BD, TID, QID)#xD; prescriptionGroup -> repeat.period = '1' "setPeriod"; // Period (1 day)#xD; prescriptionGroup -> repeat.periodUnit = 'd' "setPeriodUnit";#xD; prescriptionGroup.item as durItem where (linkId = 'duration') then {#xD; durItem.answer first as durAns then {#xD; durAns.valueInteger as durValue -> repeat.duration = durValue "setDuration";#xD; } "extractDuration";#xD; } "findDuration"; // Duration#xD; prescriptionGroup.item as durUomItem where (linkId = 'duration-uom') then {#xD; durUomItem.answer first as durUomAns then {#xD; durUomAns.valueCoding as durUomCoding then {#xD; durUomCoding.code as durUomCode -> repeat.durationUnit = durUomCode "setDurationUnit";#xD; } "extractDurUomCode";#xD; } "extractDurUom";#xD; } "findDurUom"; // Duration UOM - directly use the code from unitsofmeasure.org (h, d, wk, mo, a)#xD; prescriptionGroup.item as whenItem where (linkId = 'when-to-take') then {#xD; whenItem.answer first as whenAns then {#xD; whenAns.valueCoding as whenCoding then {#xD; whenCoding.code as whenCode -> repeat.when = whenCode "setWhenCode";#xD; } "extractWhenCode";#xD; } "extractWhen";#xD; } "findWhen"; // When to take - directly use the code (AC, PC, ACM, ACD, ACV, PCM, PCD, PCV, etc.)#xD; prescriptionGroup.item as startItem where (linkId = 'start-date') then {#xD; startItem.answer first as startAns then {#xD; startAns.valueDate as startDate -> repeat.boundsPeriod as boundsPeriod then {#xD; startDate -> boundsPeriod.start = startDate "setBoundsStart";#xD; prescriptionGroup.item as endItem where (linkId = 'end-date') then {#xD; endItem.answer first as endAns then {#xD; endAns.valueDate as endDate -> boundsPeriod.end = endDate "setBoundsEnd";#xD; } "extractEndDate";#xD; } "findEndDate";#xD; } "createBoundsPeriod";#xD; } "extractStartDate";#xD; } "findStartDate"; // Bounds Period from start-date and end-date#xD; } "accessRepeat";#xD; } "accessTiming";#xD; prescriptionGroup.item as routeItem where (linkId = 'route') then {#xD; routeItem.answer first as routeAns then {#xD; routeAns.valueCoding as routeCoding -> dosage.route = create('CodeableConcept') as routeCC then {#xD; routeCoding -> routeCC.coding = routeCoding "setRouteCoding";#xD; routeCoding.display as disp -> routeCC.text = disp "setRouteText";#xD; } "setRouteCC";#xD; } "extractRoute";#xD; } "findRoute"; // Route#xD; prescriptionGroup.item as doseItem where (linkId = 'dose') then {#xD; doseItem.answer first as doseAns then {#xD; doseAns.valueInteger as doseValue -> dosage.doseAndRate as doseRate, doseRate.doseQuantity as doseQty then {#xD; doseValue -> doseQty.value = doseValue "setDoseValue";#xD; prescriptionGroup.item as doseUnitItem where (linkId = 'dosage-unit') then {#xD; doseUnitItem.answer first as doseUnitAns then {#xD; doseUnitAns.valueCoding as unitCoding then {#xD; unitCoding.display as unitDisp -> doseQty.unit = unitDisp "setDoseUnit";#xD; unitCoding.code as unitCode -> doseQty.code = unitCode "setDoseCode";#xD; unitCoding.system as unitSys -> doseQty.system = unitSys "setDoseSystem";#xD; } "extractUnitCoding";#xD; } "extractDoseUnitAns";#xD; } "findDoseUnit";#xD; } "createDoseAndRate";#xD; } "extractDoseAns";#xD; } "findDose"; // Dose and Rate#xD; prescriptionGroup.item as instrItem where (linkId = 'dosage-instructions') then {#xD; instrItem.answer first as instrAns then {#xD; instrAns.valueString as instrText -> dosage.patientInstruction = instrText "setPatientInstruction";#xD; } "extractInstr";#xD; } "findDosageInstr"; // Dosage Instructions (patient instruction)#xD; prescriptionGroup.item as addInstrItem where (linkId = 'additional-instructions') then {#xD; addInstrItem.answer first as addInstrAns then {#xD; addInstrAns.valueString as addInstrText -> dosage.text = addInstrText "setDosageText";#xD; } "extractAddInstr";#xD; } "findAddInstr"; // Additional Instructions (text)#xD; }#xD; #xD; group BuildDispenseRequest(source qr : QR, source prescriptionGroup, target dispReq) {#xD; prescriptionGroup.item as refillsItem where (linkId = 'refills') then {#xD; refillsItem.answer first as refillsAns then {#xD; refillsAns.valueInteger as refillsValue -> dispReq.numberOfRepeatsAllowed = refillsValue "setRefills";#xD; } "extractRefills";#xD; } "findRefills"; // Refills#xD; prescriptionGroup.item as startItem where (linkId = 'start-date') then {#xD; startItem.answer first as startAns then {#xD; startAns.valueDate as startDate -> dispReq.validityPeriod as validPeriod then {#xD; startDate -> validPeriod.start = startDate "setValidityStart";#xD; prescriptionGroup.item as endItem where (linkId = 'end-date') then {#xD; endItem.answer first as endAns then {#xD; endAns.valueDate as endDate -> validPeriod.end = endDate "setValidityEnd";#xD; } "extractValidityEnd";#xD; } "findValidityEnd";#xD; } "createValidityPeriod";#xD; } "extractValidityStart";#xD; } "findValidityStart"; // Validity Period from start-date and end-date#xD; prescriptionGroup.item as durItem where (linkId = 'duration') then {#xD; durItem.answer first as durAns then {#xD; durAns.valueInteger as durValue -> dispReq.expectedSupplyDuration as supplyDur then {#xD; durValue -> supplyDur.value = durValue "setSupplyDurValue";#xD; prescriptionGroup.item as durUomItem where (linkId = 'duration-uom') then {#xD; durUomItem.answer first as durUomAns then {#xD; durUomAns.valueCoding as durUomCoding then {#xD; durUomCoding.code as uomCode -> supplyDur.code = uomCode "setSupplyDurCode";#xD; durUomCoding.display as uomDisplay -> supplyDur.unit = uomDisplay "setSupplyDurUnit";#xD; durUomCoding.system as uomSystem -> supplyDur.system = uomSystem "setSupplyDurSystem";#xD; } "extractSupplyDurUom";#xD; } "extractSupplyDurUomAns";#xD; } "findSupplyDurUom";#xD; } "createSupplyDuration";#xD; } "extractSupplyDuration";#xD; } "findSupplyDuration"; // Expected Supply Duration#xD; qr.source as src -> dispReq.dispenser = src "setDispenser"; // Dispenser - from QuestionnaireResponse.source (Organization)#xD; }#xD; #xD; group SetMedicationRequestFullUrl(source medReq : MedicationRequest, target entry) {#xD; medReq.id as id -> entry.fullUrl = append('https://fhir.slade360.co.ke/fhir/MedicationRequest/', id) "assignFullUrl";#xD; }#xD; #xD;
status will always be draft until medication has been signed off
AuthoredOn from QuestionnaireResponse.authored
Requester from QuestionnaireResponse.source (the Organization)
Copy identifier from QuestionnaireResponse to MedicationRequest
Medication as CodeableReference from QR extension (using concept)
Duration UOM - directly use the code from unitsofmeasure.org (h, d, wk, mo, a)
When to take - directly use the code (AC, PC, ACM, ACD, ACV, PCM, PCD, PCV, etc.)
Bounds Period from start-date and end-date
Dosage Instructions (patient instruction)
Validity Period from start-date and end-date
Dispenser - from QuestionnaireResponse.source (Organization)