.. _tut_editmappings:

Edit mappings
=============

.. note::
    A whole tutorial referenced here is available in the following Postman collection: https://documenter.getpostman.com/view/1515623/2sAYdimoej
.. note::
    If running against a public sandbox.open-fhir.com, don't forget to include Authorization header of type Basic.


In this step, we'll be adding mapping of a new data point to the body weight model mapper.

First, we need to get an ID of the body weight model mapper we've persisted in the state preparation step.

.. http:get:: http://localhost:8080/fc

   .. sourcecode:: http

      GET http://localhost:8080/fc HTTP/1.1

This will provide you with all available mappings. There should be 4 based on our state preparation. Find the one that handles mapping of the weight Observation (i.e. fhirConfig.condition.criteria=weight).

Adding an Observation.note.text to the mapping
----------------------------------------------

Now that you have a specific model mapper, add this entry under mappings

::

  - name: "comment"
    with:
      fhir: "$resource.note.text"
      openehr: "$archetype/data[at0002]/events[at0003]/data[at0001]/items[at0024]"
      type: "STRING"

This will map from/to ``Observation.node.text`` <> ``body_weight.any_event[n].comment``

Final model mapper after editing should look like this:

::

    grammar: FHIRConnect/v0.0.1
    type: model
    metadata:
      name: "openEHR-EHR-OBSERVATION.body_weight.v2"
      version: 1.0.0
    spec:
      system: FHIR
      version: R4
      openEhrConfig:
        archetype: "openEHR-EHR-OBSERVATION.body_weight.v2"
      fhirConfig:
        structureDefinition: http://hl7.org/fhir/StructureDefinition/Observation
        condition:
          - targetRoot: "$resource"
            targetAttribute: "category.coding.code"
            operator: "one of"
            criteria: "[weight]"



    mappings:
      - name: "weight"
        with:
          fhir: "$resource.value"
          openehr: "$archetype/data[at0002]/events[at0003]/data[at0001]/items[at0004]"
          type: "QUANTITY"
        fhirCondition:
          targetRoot: "$resource"
          targetAttribute: "code.coding.code"
          operator: "one of"
          criteria: "[$loinc.29463-7, $snomed.27113001]"

      - name: "time"
        with:
          fhir: "$resource.effective"
          openehr: "$archetype/data[at0002]/events[at0003]/time"
          type: "DATETIME"
      - name: "comment"
        with:
          fhir: "$resource.note.text"
          openehr: "$archetype/data[at0002]/events[at0003]/data[at0001]/items[at0024]"
          type: "STRING"

Trigger a PUT transaction with the payload above. This will edit the state of the engine.

.. http:put:: http://localhost:8080/fc/model/(id obtained from step above)

   .. sourcecode:: http

      PUT http://localhost:8080/fc/model/(id) HTTP/1.1
      Content-Type: plain/text
      Body: <full yaml text above>

Testing a new mapping
---------------------

With the PUT above, we've edited a state of the engine. You can use the same body payload as in the previous step :ref:`tut_runmappings` to test them out (run /tofhir and /toopenehr).

Apart from what's been mapped before, you should now also see that ``Observation.note.text`` is populated when mapping to FHIR and
``"growth_chart/body_weight/any_event:2/comment": "body_weightLorem ipsum0"`` is populated when mapping Observations to openEHR.

Note: this should only be the case for Observations that are coded as body weight, as we've only edited that specific mapping.