Smile CDR v2024.05.PRE
On this page:

30.2.1Custom Matching Script
Experimental

 

To make the $member-match operation more adaptive to different health providers, the System-to-System Data Exchange Module supports custom matching scripts that can be defined by clients in the form of JavaScript.

The function signature must match the following template, where the first parameter contains the memberPatient and coverageToMatch parameters to the $member-match operation, and the second parameter contains details about the request that is about to be processed.

function matchPatient(theMemberMatchRequest, theRequestDetails) {
		// your implementation goes here
}

The function may return Patient if a matching patient is found, or null otherwise. Note that if null is returned, then the system will perform the matching again using default matching.

30.2.1.1Example

Below is an example of the custom matching script, which fetches Coverage resources that have the same health plan Identifier as the coverageToMatch parameter to $member-match, and performs some validation on the referenced patient:

function matchPatient(theMemberMatchRequest, theRequestDetails){
	 // get the memberPatient and the coverageToMatch from the first parameter
    let memberPatient = theMemberMatchRequest.getPatient();
    let coverageToMatch = theMemberMatchRequest.getCoverage();
    
    // search for coverages that have the same identifier as coverageToMatch on
    // an accepted list of health plans
    var coverageList = ["http://oldhealthplan.example.com",
                        "http://oldhealthplan.example.com2",
                        "http://oldhealthplan.example.com3"];
    var coverageSystem = null;
    var coverageValue = null;
    for (let i = 0; i < coverageToMatch.identifier.length; i++) {
        if (coverageList.includes(coverageToMatch.identifier[i].system)) {
            coverageSystem = coverageToMatch.identifier[i].system;
            coverageValue = coverageToMatch.identifier[i].value;
            break;
        }
    }

    // return null if coverageToMatch does not fit to any of the health plans
    if (coverageSystem == null) {
        return null;
    }

    // perform actual search
    var coverageSearchResult = Fhir
    .search()
    .forResource('Coverage')
    .whereToken('identifier', coverageSystem, coverageValue)
    .asList();

    // get the patient ids from the resulting coverages
    patientIds = '';
    for (let i = 0; i < coverageSearchResult.length; i++) {
        Log.info(coverageSearchResult[i].beneficiary.reference);
        patientIds = patientIds
        .concat(coverageSearchResult[i].beneficiary.reference)
        .concat(',');
    }
    
    // return null if no patient reference was found
    if (patientIds == '') {
        return null;
    }

    // search for the patient ids
    var patientSearchResult = Fhir
    .search()
    .forResource('Patient')
    .where('_id', patientIds)
    .asList();

    // perform validation on the patients
    let resultPatient = null;
    for (let i = 0; i < patientSearchResult.length; i++) {
        if (validatePatient(patientSearchResult[i], memberPatient)){
            resultPatient = patientSearchResult[i];
            break;
        }
    }

    return resultPatient;
}

// validate the patient by comparing gender with the input member patient,
// and return the first one that matches
function validatePatient(thePatient, theMemberPatient) {
		if (thePatient && theMemberPatient) {
			if (thePatient.gender && theMemberPatient.gender) {
				return thePatient.gender.toString().toLowerCase == theMemberPatient.gender.toString().toLowerCase;
			} else if (!thePatient.gender && !theMemberPatient.gender) {
				return true; // neither have a gender - we'll say they're the same
			}
		}
		return false;
}