Data Management with FHIR
0.1.0 - ci-build France flag

Data Management with FHIR - Local Development build (v0.1.0) built by the FHIR (HL7® FHIR® Standard) Build Tools. See the Directory of published versions

StructureMap: Transforms QuestionnaireResponse based on Questionnaire Usage Variables socles into FHIR resources conforming to DM profiles

Official URL: https://interop.aphp.fr/ig/fhir/dm/StructureMap/Q2FSL Version: 0.1.0
Draft as of 2025-10-23 Computable Name: Q2FSL

Transforms QuestionnaireResponse based on Questionnaire Usage Variables socles into FHIR resources conforming to DM profiles

map "https://interop.aphp.fr/ig/fhir/dm/StructureMap/Q2FSL" = "Q2FSL"

// Transforms QuestionnaireResponse based on Questionnaire Usage Variables socles into FHIR resources conforming to DM profiles

uses "http://hl7.org/fhir/StructureDefinition/QuestionnaireResponse" alias QuestionnaireResponse as source
uses "http://hl7.org/fhir/StructureDefinition/Bundle" alias Bundle as target

// Main entry point: Transform QuestionnaireResponse to Bundle
group QuestionnaireResponseToBundle(source src : QuestionnaireResponse, target bundle : Bundle) {
  // Initialize Bundle
  src -> bundle.id = uuid() "bundleid";
  src -> bundle.type = 'collection' "bundletype";
  src -> bundle.timestamp = (now()) "setTimestamp";
  // Create Patient resource
  src -> bundle.entry as patientEntry then {
    src -> patientEntry.resource = create('Patient') as patient then {
      src then CreatePatient(src, patient) "createpatient";
      src ->  patient.id as patientId,  patientEntry.fullUrl = append('urn:uuid:', patientId) "setfullUrl";
      // Create Locations from geocoding data
      // src then CreateLocations(src, patient, bundle) "create-locations";
      // Create Encounters from PMSI data
      src then CreateEncounters(src, patient, bundle) "createencounters";
      // Create Laboratory Observations
      src then CreateLaboratoryObservations(src, patient, bundle) "createlabobservations";
      // Create Medication Requests
      src then CreateMedicationRequests(src, patient, bundle) "createmedicationrequests";
      // Create Medication Administrations
      src then CreateMedicationAdministrations(src, patient, bundle) "createmedicationadministrations";
      // Create Vital Sign Observations
      src then CreateVitalSignObservations(src, patient, bundle) "createvitalsigns";
    } "createpatientresource";
  } "patiententry";
}

// Group: Create Patient from QuestionnaireResponse
group CreatePatient(source src : QuestionnaireResponse, target patient : Patient) {
  src -> patient.id = uuid() "patientid";
  src -> patient.meta = create('Meta') as meta then {
    src -> meta.profile = 'https://interop.aphp.fr/ig/fhir/dm/StructureDefinition/DMPatient' "patientprofile";
  } "patientmeta";
  // Extract patient identity data from linkId 4647259356106 > 2958000860428
  src.item as socioDemo where (linkId = '4647259356106') then {
    socioDemo.item as identity where (linkId = '2958000860428') then {
      // Patient name (linkId 8605698058770 = family, 6214879623503 = given)
      identity -> patient.name = create('HumanName') as name then {
        identity.item as familyItem where (linkId = '8605698058770') then {
          familyItem.answer as ans -> name.family = (ans.valueString) "setfamily";
        } "extractfamily";
        identity.item as givenItem where (linkId = '6214879623503') then {
          givenItem.answer as ans -> name.given = (ans.valueString) "setgiven";
        } "extractgiven";
      } "setname";
      // NIR identifier (linkId 5711960356160)
      identity.item as nirItem where (linkId = '5711960356160') then {
        nirItem.answer as ans -> patient.identifier = create('Identifier') as nir then {
          ans -> nir.system = 'urn:oid:1.2.250.1.213.1.4.8' "nirsystem";
          ans -> nir.value = (ans.valueString) "nirvalue";
          ans -> nir.type = cc('http://interopsante.org/fhir/CodeSystem/fr-v2-0203', 'NIR') "nirtype";
        } "setnir";
      } "extractnir";
      // INS identifier (linkId 3764723550987)
      identity.item as insItem where (linkId = '3764723550987') then {
        insItem.answer as ans -> patient.identifier = create('Identifier') as ins then {
          ans -> ins.system = 'urn:oid:1.2.250.1.213.1.4.10' "inssystem";
          ans -> ins.value = (ans.valueString) "insvalue";
          ans -> ins.type = cc('http://interopsante.org/fhir/CodeSystem/fr-v2-0203', 'INS-C') "instype";
        } "setins";
      } "extractins";
      // Birth date (linkId 5036133558154)
      identity.item as birthItem where (linkId = '5036133558154') then {
        birthItem.answer as ans -> patient.birthDate = (ans.valueDate) "setbirthdate";
      } "extractbirthdate";
      // Death date (linkId 5633552097315)
      identity.item as deathItem where (linkId = '5633552097315') then {
        deathItem.answer as ans -> patient.deceased = (ans.valueDate) "setdeceaseddate";
        // Death source (linkId 9098810065693)
        deathItem.item as deathSource where (linkId = '9098810065693') then {
          deathSource.answer as sourceAns -> patient.extension = create('Extension') as ext then {
            sourceAns -> ext.url = 'https://interop.aphp.fr/ig/fhir/dm/StructureDefinition/death-source' "deathsourceurl";
            sourceAns -> ext.value = (sourceAns.valueCoding) "deathsourcevalue";
          } "setdeathsourceext";
        } "extractdeathsource";
      } "extractdeath";
      // Multiple birth rank (linkId 6931296968515)
      identity.item as multipleItem where (linkId = '6931296968515') then {
        multipleItem.answer as ans then {
          ans.valueInteger as valueInteger -> patient.multipleBirth = valueInteger "setmultiplebirth";
        } "navigatevalue";
      } "extractmultiplebirth";
    } "processidentity";
    // Gender from PMSI data (linkId 3894630481120 within 2825244231605)
    src.item as pmsiGroup where (linkId = '2825244231605') then {
      pmsiGroup.item as sexItem where (linkId = '3894630481120') then {
        sexItem.answer as ans then {
          ans.valueCoding as valueCoding -> patient.gender = translate(valueCoding, 'https://interop.aphp.fr/ig/fhir/dm/ConceptMap/dpi-gender-2-hl7-gender', 'code') "setgender";
        } "valueCoding";
      } "extractgender";
      // Address from code géographique (linkId 2446369196222)
      pmsiGroup.item as codeGeoItem where (linkId = '2446369196222') then {
        codeGeoItem.answer as ans -> patient.address = create('Address') as addr then {
          ans -> addr.extension = create('Extension') as ext then {
            ans -> ext.url = 'https://interop.aphp.fr/ig/fhir/dm/StructureDefinition/pmsi-code-geo' "codegeourl";
            ans -> ext.value = (ans.valueCoding) "codegeovalue";
          } "setcodegeoext";
        } "setaddress";
      } "extractcodegeo";
    } "extractpmsidemographics";
  } "processsociodemographics";
}

// Group: Create Locations from geocoding and IRIS data
group CreateLocations(source src : QuestionnaireResponse, target patient : Patient, target bundle : Bundle) {
  // Extract geocoding data from linkId 4647259356106 > 5491974639955 > 3816475533472
  src.item as socioDemo where (linkId = '4647259356106') then {
    socioDemo.item as environment where (linkId = '5491974639955') then {
      // Geocoding locations (repeating group 3816475533472)
      environment.item as geocodingItem where (linkId = '3816475533472') -> bundle.entry as locationEntry then {
        geocodingItem -> locationEntry.resource = create('Location') as location then {
          geocodingItem -> location.id = uuid() "locationid";
          geocodingItem -> location.meta = create('Meta') as meta then {
            geocodingItem -> meta.profile = 'https://interop.aphp.fr/ig/fhir/dm/StructureDefinition/DMLocation' "locationprofile";
          } "locationmeta";
          // Position with latitude and longitude
          geocodingItem -> location.position = create('LocationPosition') as pos then {
            // Latitude (linkId 3709843054556)
            geocodingItem.item as latItem where (linkId = '3709843054556') then {
              latItem.answer as ans -> pos.latitude = (ans.valueDecimal) "setlatitude";
            } "extractlatitude";
            // Longitude (linkId 7651448032665)
            geocodingItem.item as longItem where (linkId = '7651448032665') then {
              longItem.answer as ans -> pos.longitude = (ans.valueDecimal) "setlongitude";
            } "extractlongitude";
          } "setposition";
          // Extension for collection date (linkId 1185653257776)
          // geocodingItem.item as dateItem where (linkId = '1185653257776') then {             dateItem.answer as ans -> location.extension = create('Extension') as ext then {               ans -> ext.url = 'http://hl7.org/fhir/StructureDefinition/data-collection-date' "date-url";               ans -> ext.value = (ans.valueDate) "date-value";             } "set-collection-date";           } "extract-collection-date";
          location -> locationEntry.request as request then {
            location -> request.method = 'POST' "setmethod";
            location -> request.url = 'Location' "seturl";
          } "setrequest";
          location.id as locId -> locationEntry.fullUrl = append('urn:uuid:', locId) "setfullUrl";
        } "createlocation";
      } "geocodinglocationentry";
      // IRIS locations (repeating item 7621032273792)
      environment.item as irisItem where (linkId = '7621032273792') then {
        irisItem.answer as irisAnswer -> bundle.entry as locationEntry then {
          irisAnswer -> locationEntry.resource = create('Location') as location then {
            irisAnswer -> location.id = uuid() "locationid";
            irisAnswer -> location.meta = create('Meta') as meta then {
              irisAnswer -> meta.profile = 'https://interop.aphp.fr/ig/fhir/dm/StructureDefinition/DMLocation' "locationprofile";
            } "locationmeta";
            // IRIS code as identifier
            irisAnswer -> location.identifier = create('Identifier') as identifier then {
              irisAnswer -> identifier.system = 'urn:oid:2.16.840.1.113883.2.8.1.5.5' "irissystem";
              irisAnswer -> identifier.value = (irisAnswer.valueCoding.code) "irisvalue";
            } "setirisidentifier";
            // Collection date from nested item (linkId 4999580038872)
            // irisAnswer.item as dateItem where (linkId = '4999580038872') then {               dateItem.answer as ans -> location.extension = create('Extension') as ext then {                 ans -> ext.url = 'http://hl7.org/fhir/StructureDefinition/data-collection-date' "date-url";                 ans -> ext.value = (ans.valueDate) "date-value";               } "set-collection-date";             } "extract-iris-date";
            location -> locationEntry.request as request then {
              location -> request.method = 'POST' "setmethod";
              location -> request.url = 'Location' "seturl";
            } "setrequest";
            location.id as locId -> locationEntry.fullUrl = append('urn:uuid:', locId) "setfullUrl";
          } "createirislocation";
        } "irislocationentry";
      } "processiris";
    } "processenvironment";
  } "processsociodemographicslocation";
}

// Group: Create Encounters from PMSI data
group CreateEncounters(source src : QuestionnaireResponse, target patient : Patient, target bundle : Bundle) {
  // PMSI data group (linkId 2825244231605) - Note: This can repeat in the structure
  src.item as pmsiItem where (linkId = '2825244231605') -> bundle.entry as encounterEntry then {
    pmsiItem -> encounterEntry.resource = create('Encounter') as encounter then {
      pmsiItem -> encounter.id = uuid() "encounterid";
      pmsiItem -> encounter.meta = create('Meta') as meta then {
        pmsiItem -> meta.profile = 'https://interop.aphp.fr/ig/fhir/dm/StructureDefinition/DMEncounter' "encounterprofile";
      } "encountermeta";
      // Link to patient
      src ->  encounter.subject = create('Reference') as ref,  patient.id as patId,  ref.reference = append('Patient/', patId) "setsubject";
      // Encounter period (start: 5991443718282, end: 6114780320846)
      pmsiItem -> encounter.period = create('Period') as period then {
        pmsiItem.item as startItem where (linkId = '5991443718282') then {
          startItem.answer as ans -> period.start = (ans.valueDate) "setstart";
        } "extractstart";
        pmsiItem.item as endItem where (linkId = '6114780320846') then {
          endItem.answer as ans -> period.end = (ans.valueDate) "setend";
        } "extractend";
      } "setperiod";
      // Mode d'entrée (linkId 6172398101212)
      pmsiItem.item as modeInItem where (linkId = '6172398101212') "extractmodein";
      // Mode de sortie (linkId 3354867075704)
      pmsiItem.item as modeOutItem where (linkId = '3354867075704') "extractmodeout";
      src ->  encounter.id as encId,  encounterEntry.fullUrl = append('urn:uuid:', encId) "setfullUrl";
      // Create nested Conditions and Procedures
      pmsiItem then CreateConditions(pmsiItem, patient, encounter, bundle) "createconditions";
      pmsiItem then CreateProcedures(pmsiItem, patient, encounter, bundle) "createprocedures";
    } "createencounter";
  } "encounterentry";
}

// Group: Create Conditions from nested diagnostics
group CreateConditions(source pmsiItem, target patient : Patient, target encounter : Encounter, target bundle : Bundle) {
  // Diagnostics group (linkId 9391816419630) - can repeat
  pmsiItem.item as diagGroup where (linkId = '9391816419630') -> bundle.entry as conditionEntry then {
    diagGroup -> conditionEntry.resource = create('Condition') as condition then {
      diagGroup -> condition.id = uuid() "conditionid";
      diagGroup -> condition.meta = create('Meta') as meta then {
        diagGroup -> meta.profile = 'https://interop.aphp.fr/ig/fhir/dm/StructureDefinition/DMCondition' "conditionprofile";
      } "conditionmeta";
      // Link to patient
      diagGroup ->  condition.subject = create('Reference') as ref,  patient.id as patId,  ref.reference = append('Patient/', patId) "setsubject";
      // Link to encounter
      diagGroup ->  encounter.id as encId,  condition.encounter = create('Reference') as ref,  ref.reference = append('Encounter/', encId) "setencounter";
      // Diagnostic code (linkId 5505101189372) - CIM-10
      diagGroup.item as diagCodeItem where (linkId = '5505101189372') then {
        diagCodeItem.answer as ans -> condition.code = create('CodeableConcept') as code then {
          ans -> code.coding = (ans.valueCoding) "setcoding";
        } "setcode";
      } "extractdiagcode";
      // Type de diagnostic (linkId 6427586743735) - DP/DAS/DR
      diagGroup.item as diagTypeItem where (linkId = '6427586743735') then {
        diagTypeItem.answer as ans -> condition.category = create('CodeableConcept') as category then {
          ans -> category.coding = create('Coding') as coding then {
            ans -> coding.system = 'https://interop.aphp.fr/ig/fhir/dm/CodeSystem/pmsi-mco-diag-type' "diagtypesystem";
            ans -> coding.code = (ans.valueCoding.code) "diagtypecode";
            ans -> coding.display = (ans.valueCoding.display) "diagtypedisplay";
          } "setcategorycoding";
        } "setcategory";
      } "extractdiagtype";
      // Recorded date (linkId 7114466839467)
      diagGroup.item as dateItem where (linkId = '7114466839467') then {
        dateItem.answer as ans -> condition.recordedDate = (ans.valueDate) "setrecordeddate";
      } "extractrecordeddate";
    } "createcondition";
  } "conditionentry";
}

// Group: Create Procedures from nested actes
group CreateProcedures(source pmsiItem, target patient : Patient, target encounter : Encounter, target bundle : Bundle) {
  // Actes group (linkId 591926901726) - can repeat
  pmsiItem.item as acteGroup where (linkId = '591926901726') -> bundle.entry as procedureEntry then {
    acteGroup -> procedureEntry.resource = create('Procedure') as procedure then {
      acteGroup -> procedure.id = uuid() "procedureid";
      acteGroup -> procedure.meta = create('Meta') as meta then {
        acteGroup -> meta.profile = 'https://interop.aphp.fr/ig/fhir/dm/StructureDefinition/DMProcedure' "procedureprofile";
      } "proceduremeta";
      // Link to patient
      acteGroup ->  procedure.subject = create('Reference') as ref,  patient.id as patId,  ref.reference = append('Patient/', patId) "setsubject";
      // Link to encounter
      acteGroup ->  encounter.id as encId,  procedure.encounter = create('Reference') as ref,  ref.reference = append('Encounter/', encId) "setencounter";
      // Acte code (linkId 7758110033600) - CCAM
      acteGroup.item as acteCodeItem where (linkId = '7758110033600') then {
        acteCodeItem.answer as ans -> procedure.code = create('CodeableConcept') as code then {
          ans -> code.coding = (ans.valueCoding) "setcoding";
        } "setcode";
      } "extractactecode";
      // Date de l'acte (linkId 5066866286682)
      acteGroup.item as dateItem where (linkId = '5066866286682') then {
        dateItem.answer as ans -> procedure.performed = (ans.valueDateTime) "setperformed";
      } "extractperformeddate";
    } "createprocedure";
  } "procedureentry";
}

// Group: Create Medication Requests
group CreateMedicationRequests(source src : QuestionnaireResponse, target patient : Patient, target bundle : Bundle) {
  // Exposition médicamenteuse (linkId 817801935685)
  src.item as medExpoGroup where (linkId = '817801935685') then {
    // Médicament prescrit (linkId 156631794800) - repeating
    medExpoGroup.item as prescribedGroup where (linkId = '156631794800') -> bundle.entry as medReqEntry then {
      prescribedGroup -> medReqEntry.resource = create('MedicationRequest') as medReq then {
        prescribedGroup -> medReq.id = uuid() "medreqid";
        prescribedGroup -> medReq.meta = create('Meta') as meta then {
          prescribedGroup -> meta.profile = 'https://interop.aphp.fr/ig/fhir/dm/StructureDefinition/DMMedicationRequest' "medreqprofile";
        } "medreqmeta";
        // Link to patient
        prescribedGroup ->  medReq.subject = create('Reference') as ref,  patient.id as patId,  ref.reference = append('Patient/', patId) "setsubject";
        // Status and intent
        prescribedGroup -> medReq.status = 'active' "setstatus";
        prescribedGroup -> medReq.intent = 'order' "setintent";
        // Create Medication resource and reference
        prescribedGroup -> bundle.entry as medEntry then {
          prescribedGroup -> medEntry.resource = create('Medication') as medication then {
            prescribedGroup -> medication.id = uuid() "medid";
            // ATC code from nested items
            prescribedGroup.item as atcItem where (linkId.contains('ATC')) then {
              atcItem.answer as ans -> medication.code = create('CodeableConcept') as code then {
                ans -> code.coding = (ans.valueCoding) "setatccoding";
              } "setmedcode";
            } "extractatc";
            prescribedGroup ->  medication.id as medId,  medEntry.fullUrl = append('urn:uuid:', medId) "setfullUrl";
            // Link MedicationRequest to Medication
            prescribedGroup ->  medication.id as medId,  medReq.medication = create('Reference') as ref,  ref.reference = append('Medication/', medId) "setmedicationreference";
          } "createmedication";
        } "medicationentry";
        // Dosage instructions
        prescribedGroup -> medReq.dosageInstruction = create('Dosage') as dosage then {
          // Route
          prescribedGroup.item as routeItem where (linkId.contains('voie')) then {
            routeItem.answer as ans -> dosage.route = create('CodeableConcept') as route then {
              ans -> route.coding = (ans.valueCoding) "setroutecoding";
            } "setroute";
          } "extractroute";
          // Timing
          prescribedGroup.item as timingItem where (linkId.contains('timing') or linkId.contains('fréquence')) then {
            timingItem.answer as ans -> dosage.timing = create('Timing') as timing then {
              ans -> timing.repeat = create('TimingRepeat') as repeat "settiming";
            } "settimingstructure";
          } "extracttiming";
        } "setdosage";
      } "createmedreq";
    } "medreqentry";
  } "processmedexpo";
}

// Group: Create Medication Administrations
group CreateMedicationAdministrations(source src : QuestionnaireResponse, target patient : Patient, target bundle : Bundle) {
  // Exposition médicamenteuse (linkId 817801935685)
  src.item as medExpoGroup where (linkId = '817801935685') then {
    // Médicament administré (linkId 266852453304) - repeating
    medExpoGroup.item as adminGroup where (linkId = '266852453304') -> bundle.entry as medAdminEntry then {
      adminGroup -> medAdminEntry.resource = create('MedicationAdministration') as medAdmin then {
        adminGroup -> medAdmin.id = uuid() "medadminid";
        adminGroup -> medAdmin.meta = create('Meta') as meta then {
          adminGroup -> meta.profile = 'https://interop.aphp.fr/ig/fhir/dm/StructureDefinition/DMMedicationAdministration' "medadminprofile";
        } "medadminmeta";
        // Link to patient
        adminGroup ->  medAdmin.subject = create('Reference') as ref,  patient.id as patId,  ref.reference = append('Patient/', patId) "setsubject";
        // Status
        adminGroup -> medAdmin.status = 'completed' "setstatus";
        // Create Medication resource and reference
        adminGroup -> bundle.entry as medEntry then {
          adminGroup -> medEntry.resource = create('Medication') as medication then {
            adminGroup -> medication.id = uuid() "medid";
            // ATC code from nested items
            adminGroup.item as atcItem where (linkId.contains('ATC')) then {
              atcItem.answer as ans -> medication.code = create('CodeableConcept') as code then {
                ans -> code.coding = (ans.valueCoding) "setatccoding";
              } "setmedcode";
            } "extractatc";
            adminGroup ->  medication.id as medId,  medEntry.fullUrl = append('Medication/', medId) "setfullUrl";
            // Link MedicationAdministration to Medication
            adminGroup ->  medication.id as medId,  medAdmin.medication = create('Reference') as ref,  ref.reference = append('urn:uuid:', medId) "setmedicationreference";
          } "createmedication";
        } "medicationentry";
        // Effective period
        adminGroup -> medAdmin.effective = create('Period') as period then {
          adminGroup.item as startItem where (linkId.contains('début')) then {
            startItem.answer as ans -> period.start = (ans.valueDateTime) "setstart";
          } "extractstart";
          adminGroup.item as endItem where (linkId.contains('fin')) then {
            endItem.answer as ans -> period.end = (ans.valueDateTime) "setend";
          } "extractend";
        } "seteffective";
        // Dosage
        adminGroup -> medAdmin.dosage = create('Dosage') as dosage then {
          // Route
          adminGroup.item as routeItem where (linkId.contains('voie')) then {
            routeItem.answer as ans -> dosage.route = create('CodeableConcept') as route then {
              ans -> route.coding = (ans.valueCoding) "setroutecoding";
            } "setroute";
          } "extractroute";
          // Dose
          adminGroup.item as doseItem where (linkId.contains('dose')) then {
            doseItem.answer as ans -> dosage.dose = (ans.valueQuantity) "setdose";
          } "extractdose";
        } "setdosage";
      } "createmedadmin";
    } "medadminentry";
  } "processmedexpoadmin";
}

// Group: Create Vital Sign Observations
group CreateVitalSignObservations(source src : QuestionnaireResponse, target patient : Patient, target bundle : Bundle) {
  // Examen clinique (linkId 214880328197)
  src.item as examGroup where (linkId = '214880328197') then {
    // Dossier de soins (linkId 305831246173) - repeating
    examGroup.item as dossierGroup where (linkId = '305831246173') then {
      // Height (linkId 4846902346416)
      dossierGroup.item as heightItem where (linkId = '4846902346416') then {
        heightItem.answer as ans -> bundle.entry as obsEntry then {
          ans -> obsEntry.resource = create('Observation') as obs then {
            ans -> obs.id = uuid() "obsid";
            ans ->  obs.id as obsId,  obsEntry.fullUrl = append('urn:uuid:', obsId) "setfullUrl";
            ans -> obs.meta = create('Meta') as meta then {
              ans -> meta.profile = 'https://interop.aphp.fr/ig/fhir/dm/StructureDefinition/DMObservationBodyHeight' "obsprofile";
            } "obsmeta";
            ans -> obs.status = 'final' "obsstatus";
            ans -> obs.category = cc('http://terminology.hl7.org/CodeSystem/observation-category', 'vital-signs') "obscategory";
            ans -> obs.code = cc('http://loinc.org', '8302-2', 'Body height') "obscode";
            // Link to patient
            ans ->  obs.subject = create('Reference') as ref,  patient.id as patId,  ref.reference = append('Patient/', patId) "setsubject";
            // Value
            ans -> obs.value = (ans.valueQuantity) "setvalue";
            // Effective date from nested item
            heightItem.item as dateItem where (linkId.contains('Date')) then {
              dateItem.answer as dateAns -> obs.effective = (dateAns.valueDate) "seteffective";
            } "extracteffective";
          } "createheightobs";
        } "heightobsentry";
      } "extractheight";
      // Weight (linkId 451513217936)
      dossierGroup.item as weightItem where (linkId = '451513217936') then {
        weightItem.answer as ans -> bundle.entry as obsEntry then {
          ans -> obsEntry.resource = create('Observation') as obs then {
            ans -> obs.id = uuid() "obsid";
            ans ->  obs.id as obsId,  obsEntry.fullUrl = append('urn:uuid:', obsId) "setfullUrl";
            ans -> obs.meta = create('Meta') as meta then {
              ans -> meta.profile = 'https://interop.aphp.fr/ig/fhir/dm/StructureDefinition/DMObservationBodyWeight' "obsprofile";
            } "obsmeta";
            ans -> obs.status = 'final' "obsstatus";
            ans -> obs.category = cc('http://terminology.hl7.org/CodeSystem/observation-category', 'vital-signs') "obscategory";
            ans -> obs.code = cc('http://loinc.org', '29463-7', 'Body weight') "obscode";
            // Link to patient
            ans ->  obs.subject = create('Reference') as ref,  patient.id as patId,  ref.reference = append('Patient/', patId) "setsubject";
            // Value
            ans -> obs.value = (ans.valueQuantity) "setvalue";
            // Effective date from nested item
            weightItem.item as dateItem where (linkId.contains('Date')) then {
              dateItem.answer as dateAns -> obs.effective = (dateAns.valueDate) "seteffective";
            } "extracteffective";
          } "createweightobs";
        } "weightobsentry";
      } "extractweight";
      // Blood Pressure - need both systolic and diastolic
      dossierGroup.item as sysItem where (linkId = '4160905247955') then {
        sysItem.answer as sysAns then {
          dossierGroup.item as diaItem where (linkId = '848797127998') then {
            diaItem.answer as diaAns -> bundle.entry as obsEntry then {
              sysAns -> obsEntry.resource = create('Observation') as obs then {
                sysAns -> obs.id = uuid() "obsid";
                sysAns ->  obs.id as obsId,  obsEntry.fullUrl = append('urn:uuid:', obsId) "setfullUrl";
                sysAns -> obs.meta = create('Meta') as meta then {
                  sysAns -> meta.profile = 'https://interop.aphp.fr/ig/fhir/dm/StructureDefinition/DMObservationBloodPressure' "obsprofile";
                } "obsmeta";
                sysAns -> obs.status = 'final' "obsstatus";
                sysAns -> obs.category = cc('http://terminology.hl7.org/CodeSystem/observation-category', 'vital-signs') "obscategory";
                sysAns -> obs.code = cc('http://loinc.org', '85354-9', 'Blood pressure panel') "obscode";
                // Link to patient
                sysAns ->  obs.subject = create('Reference') as ref,  patient.id as patId,  ref.reference = append('Patient/', patId) "setsubject";
                // Systolic component
                sysAns -> obs.component = create('Observation') as sysComp then {
                  sysAns -> sysComp.code = cc('http://loinc.org', '8480-6', 'Systolic blood pressure') "syscode";
                  sysAns -> sysComp.value = (sysAns.valueQuantity) "sysvalue";
                } "setsystolic";
                // Diastolic component
                diaAns -> obs.component = create('Observation') as diaComp then {
                  diaAns -> diaComp.code = cc('http://loinc.org', '8462-4', 'Diastolic blood pressure') "diacode";
                  diaAns -> diaComp.value = (diaAns.valueQuantity) "diavalue";
                } "setdiastolic";
                // Effective date from nested item
                sysItem.item as dateItem where (linkId.contains('Date')) then {
                  dateItem.answer as dateAns -> obs.effective = (dateAns.valueDate) "seteffective";
                } "extracteffective";
              } "createbpobs";
            } "bpobsentr";
          } "extractdiastolic";
        } "processsystolic";
      } "extractbloodpressure";
    } "processdossier";
  } "processexam";
}

// Group: Create Laboratory Observations
group CreateLaboratoryObservations(source src : QuestionnaireResponse, target patient : Patient, target bundle : Bundle) {
  // Biologie group (linkId 7702944131447)
  src.item as bioGroup where (linkId = '7702944131447') then {
    // Fonction rénale (linkId 5241323453538)
    bioGroup.item as renalGroup where (linkId = '5241323453538') then {
      // Urée (linkId 7169026818760)
      renalGroup.item as ureaItem where (linkId = '7169026818760') then {
        ureaItem.answer as ans -> bundle then CreateLabObservation(ans, ureaItem, patient, bundle) "createureaobs";
      } "extracturea";
      // Créatininémie (linkId 500408205043)
      renalGroup.item as creatItem where (linkId = '500408205043') then {
        creatItem.answer as ans -> bundle then CreateLabObservation(ans, creatItem, patient, bundle) "createcreatobs";
      } "extractcreat";
      // DFG (linkId 786621340679)
      renalGroup.item as dfgItem where (linkId = '786621340679') then {
        dfgItem.answer as ans -> bundle then CreateLabObservation(ans, dfgItem, patient, bundle) "createdfgobs";
      } "extractdfg";
    } "processrenal";
    // Hémogramme (linkId 419282985970)
    bioGroup.item as hemoGroup where (linkId = '419282985970') then {
      // Leucocytes (linkId 210077225604)
      hemoGroup.item as leukoItem where (linkId = '210077225604') then {
        leukoItem.answer as ans -> bundle then CreateLabObservation(ans, leukoItem, patient, bundle) "createleukoobs";
      } "extractleuko";
      // Hémoglobine (linkId 304159088493)
      hemoGroup.item as hemoItem where (linkId = '304159088493') then {
        hemoItem.answer as ans -> bundle then CreateLabObservation(ans, hemoItem, patient, bundle) "createhemoobs";
      } "extracthemo";
      // Hématocrite (linkId 5687959006070)
      hemoGroup.item as hematItem where (linkId = '5687959006070') then {
        hematItem.answer as ans -> bundle then CreateLabObservation(ans, hematItem, patient, bundle) "createhematobs";
      } "extracthemat";
      // Erythrocytes (linkId 8697447896867)
      hemoGroup.item as eryItem where (linkId = '8697447896867') then {
        eryItem.answer as ans -> bundle then CreateLabObservation(ans, eryItem, patient, bundle) "createeryobs";
      } "extractery";
      // VGM (linkId 5584978899134)
      hemoGroup.item as vgmItem where (linkId = '5584978899134') then {
        vgmItem.answer as ans -> bundle then CreateLabObservation(ans, vgmItem, patient, bundle) "createvgmobs";
      } "extractvgm";
      // Plaquettes (linkId 2881119993430)
      hemoGroup.item as platItem where (linkId = '2881119993430') then {
        platItem.answer as ans -> bundle then CreateLabObservation(ans, platItem, patient, bundle) "createplatobs";
      } "extractplat";
      // Neutrophiles (linkId 7408951746270)
      hemoGroup.item as neutItem where (linkId = '7408951746270') then {
        neutItem.answer as ans -> bundle then CreateLabObservation(ans, neutItem, patient, bundle) "createneutobs";
      } "extractneut";
      // Lymphocytes (linkId 809808816195)
      hemoGroup.item as lymphItem where (linkId = '809808816195') then {
        lymphItem.answer as ans -> bundle then CreateLabObservation(ans, lymphItem, patient, bundle) "createlymphobs";
      } "extractlymph";
      // Eosinophiles (linkId 6451887990893)
      hemoGroup.item as eosiItem where (linkId = '6451887990893') then {
        eosiItem.answer as ans -> bundle then CreateLabObservation(ans, eosiItem, patient, bundle) "createeosiobs";
      } "extracteosi";
      // Monocytes (linkId 7935935816936)
      hemoGroup.item as monoItem where (linkId = '7935935816936') then {
        monoItem.answer as ans -> bundle then CreateLabObservation(ans, monoItem, patient, bundle) "createmonoobs";
      } "extractmono";
      // TP (linkId 2055949655770)
      hemoGroup.item as tpItem where (linkId = '2055949655770') then {
        tpItem.answer as ans -> bundle then CreateLabObservation(ans, tpItem, patient, bundle) "createtpobs";
      } "extracttp";
      // TCA (linkId 2527095476817)
      hemoGroup.item as tcaItem where (linkId = '2527095476817') then {
        tcaItem.answer as ans -> bundle then CreateLabObservation(ans, tcaItem, patient, bundle) "createtcaobs";
      } "extracttca";
    } "processhemo";
    // Bilan hépatique (linkId 3893612773040)
    bioGroup.item as liverGroup where (linkId = '3893612773040') then {
      // ASAT (linkId 3028330976100)
      liverGroup.item as asatItem where (linkId = '3028330976100') then {
        asatItem.answer as ans -> bundle then CreateLabObservation(ans, asatItem, patient, bundle) "createasatobs";
      } "extractasat";
      // ALAT (linkId 8486813893880)
      liverGroup.item as alatItem where (linkId = '8486813893880') then {
        alatItem.answer as ans -> bundle then CreateLabObservation(ans, alatItem, patient, bundle) "createalatobs";
      } "extractalat";
      // GGT (linkId 3663296044037)
      liverGroup.item as ggtItem where (linkId = '3663296044037') then {
        ggtItem.answer as ans -> bundle then CreateLabObservation(ans, ggtItem, patient, bundle) "createggtobs";
      } "extractggt";
      // PAL (linkId 2690026606933)
      liverGroup.item as palItem where (linkId = '2690026606933') then {
        palItem.answer as ans -> bundle then CreateLabObservation(ans, palItem, patient, bundle) "createpalobs";
      } "extractpal";
      // Bilirubine totale (linkId 9068831071023)
      liverGroup.item as bilTotItem where (linkId = '9068831071023') then {
        bilTotItem.answer as ans -> bundle then CreateLabObservation(ans, bilTotItem, patient, bundle) "createbiltotobs";
      } "extractbiltot";
      // Bilirubine conjuguée (linkId 8893717988787)
      liverGroup.item as bilConjItem where (linkId = '8893717988787') then {
        bilConjItem.answer as ans -> bundle then CreateLabObservation(ans, bilConjItem, patient, bundle) "createbilconjobs";
      } "extractbilconj";
    } "processliver";
    // Métabolisme glucidique (linkId 8929068894076)
    bioGroup.item as glucoseGroup where (linkId = '8929068894076') then {
      // Glycémie à jeun (linkId 8193929027997)
      glucoseGroup.item as glycItem where (linkId = '8193929027997') then {
        glycItem.answer as ans -> bundle then CreateLabObservation(ans, glycItem, patient, bundle) "createglycobs";
      } "extractglyc";
      // HbA1c (linkId 7063892830923)
      glucoseGroup.item as hba1cItem where (linkId = '7063892830923') then {
        hba1cItem.answer as ans -> bundle then CreateLabObservation(ans, hba1cItem, patient, bundle) "createhba1cobs";
      } "extracthba1c";
    } "processglucose";
  } "processbiology";
}

// Helper: Create a single Laboratory Observation
group CreateLabObservation(source ans, source parentItem, target patient : Patient, target bundle : Bundle) {
  ans -> bundle.entry as obsEntry then {
    ans -> obsEntry.resource = create('Observation') as obs then {
      ans -> obs.id = uuid() "obsid";
      ans ->  obs.id as obsId,  obsEntry.fullUrl = append('urn:uuid:', obsId) "setfullUrl";
      ans -> obs.meta = create('Meta') as meta then {
        ans -> meta.profile = 'https://interop.aphp.fr/ig/fhir/dm/StructureDefinition/dm-observation-laboratory-generic' "obsprofile";
      } "obsmeta";
      // Status
      ans -> obs.status = 'final' "obsstatus";
      // Category
      ans -> obs.category = create('CodeableConcept') as cat then {
        ans -> cat.coding = create('Coding') as coding then {
          ans -> coding.system = 'http://terminology.hl7.org/CodeSystem/observation-category' "catsystem";
          ans -> coding.code = 'laboratory' "catcode";
        } "setcategorycoding";
      } "setcategory";
      // Link to patient
      ans ->  obs.subject = create('Reference') as ref,  patient.id as patId,  ref.reference = append('Patient/', patId) "setsubject";
      // Value as Quantity
      ans -> obs.value = (ans.valueQuantity) "setvalue";
      // Extract nested items - LOINC code, effective date, status, reference ranges
      parentItem.item as loincItem where (linkId.contains('code loinc')) then {
        loincItem.answer as loincAns -> obs.code = create('CodeableConcept') as code then {
          loincAns -> code.coding = (loincAns.valueCoding) "setloinccoding";
        } "setcode";
      } "extractloinc";
      parentItem.item as dateItem where (linkId.contains('Date et heure du prélèvement')) then {
        dateItem.answer as dateAns -> obs.effective = (dateAns.valueDateTime) "seteffective";
      } "extracteffective";
    } "createobs";
  } "obsentry";
}