Gravitate Health FHIR Implementation Guide
0.1.0 - CI Build

Gravitate Health FHIR Implementation Guide, published by Gravitate Health Project. This guide is not an authorized publication; it is the continuous build for version 0.1.0 built by the FHIR (HL7® FHIR® Standard) CI Build. This version is based on the current content of https://github.com/hl7-eu/gravitate-health/ and changes regularly. See the Directory of published versions

Library: pregnancy-lens (Experimental)

Official URL: http://hl7.eu/fhir/ig/gravitate-health/Library/mock-lib Version: 0.1.0
Draft as of 2024-06-12 Computable Name: pregnancy-lens
Other Identifiers: http://gravitate-health.lst.tfo.upm.es#pregnancy-lens

Copyright/Legal: © 2024 Gravitate Health

Lens that highlight or collapses pregnancy related information

Collapse or highlight pregnancy related information on a preprocessed ePI.

Generated Narrative: Library pregnancy-lens

Parameters

inCodeableConceptparameter if it exists

Contents

Lens for pregnancy: (application/javascript)

let pvData = pv;
let htmlData = html;

let epiData = epi;
let ipsData = ips;

let getSpecification = () => {
    return "1.0.0";
};

let annotationProcess = (listOfCategories, enhanceTag, document, response) => {
    listOfCategories.forEach((check) => {
        if (response.includes(check)) {
            let elements = document.getElementsByClassName(check);
            for (let i = 0; i < elements.length; i++) {
                elements[i].classList.add(enhanceTag);
            }
            if (document.getElementsByTagName("head").length > 0) {
                document.getElementsByTagName("head")[0].remove();
            }
            if (document.getElementsByTagName("body").length > 0) {
                response = document.getElementsByTagName("body")[0].innerHTML;
                console.log("Response: " + response);
            } else {
                console.log("Response: " + document.documentElement.innerHTML);
                response = document.documentElement.innerHTML;
            }
        }
    });

    if (response == null || response == "") {
        throw new Error(
            "Annotation proccess failed: Returned empty or null response"
        );
        //return htmlData
    } else {
        console.log("Response: " + response);
        return response;
    }
}

let annotateHTMLsection = async (listOfCategories, enhanceTag) => {
    let response = htmlData;
    let document;

    if (typeof window === "undefined") {
        let jsdom = await import("jsdom");
        let { JSDOM } = jsdom;
        let dom = new JSDOM(htmlData);
        document = dom.window.document;
        return annotationProcess(listOfCategories, enhanceTag, document, response);
    } else {
        document = window.document;
        return annotationProcess(listOfCategories, enhanceTag, document, response);
    }
};

let getIPSAge = (birthDate) => {
    let today = new Date();
    let birthDateParsed = new Date(birthDate);

    let ageMiliseconds = today - birthDateParsed;
    let age = Math.floor(ageMiliseconds / 31536000000);

    return age;
}

let enhance = async () => {
    //                  pregnancyCategory    breastfeedingCategory
    //                             SNOMED    SNOMED
    let listOfCategoriesToSearch = ["W78", "77386006", "69840006"]; //"contra-indication-pregancy"]

    // Get IPS gender and check if is female
    let gender;

    let enhanceTag;

    if (ips == "" || ips == null) {
        throw new Error("Failed to load IPS: the LEE is getting a empty IPS");
    }
    ips.entry.forEach((element) => {
        if (element.resource.resourceType == "Patient") {
            gender = element.resource.gender;
            if (gender != "female" || getIPSAge(element.resource.birthDate) >= 75) {
                enhanceTag = "collapsed";
            } else {
                enhanceTag = "highlight";
            }
        }
    });

    // ePI traslation from terminology codes to their human redable translations in the sections
    let compositions = 0;
    let categories = [];
    epi.entry.forEach((entry) => {
        if (entry.resource.resourceType == "Composition") {
            compositions++;
            //Iterated through the Condition element searching for conditions
            entry.resource.extension.forEach((element) => {
                
                // Check if the position of the extension[1] is correct
                if (element.extension[1].url == "concept") {
                    // Search through the different terminologies that may be avaible to check in the condition
                    if (element.extension[1].valueCodeableReference.concept != undefined) {
                        element.extension[1].valueCodeableReference.concept.coding.forEach(
                            (coding) => {
                                console.log("Extension: " + element.extension[0].valueString + ":" + coding.code)
                                // Check if the code is in the list of categories to search
                                if (listOfCategoriesToSearch.includes(coding.code)) {
                                    // Check if the category is already in the list of categories
                                    categories.push(element.extension[0].valueString);
                                }
                            }
                        );
                    }
                }
            });
        }
    });

    if (compositions == 0) {
        throw new Error('Bad ePI: no category "Composition" found');
    }

    if (categories.length == 0) {
        // throw new Error("No categories found", categories);
        return htmlData;
    }
    //Focus (adds highlight class) the html applying every category found
    return await annotateHTMLsection(categories, enhanceTag);
};

return {
    enhance: enhance,
    getSpecification: getSpecification,
};