Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retrieve MedicationRequest which doesnt have any MedicationDispense #4034

Closed
LKShankarKarthik opened this issue Aug 16, 2024 · 2 comments
Closed
Labels
Question Issue is a question?

Comments

@LKShankarKarthik
Copy link

Question

In our case, we have a scenario where we want to retrieve all FHIR MedicationRequest resources which does not have any associated FHIR MedicationDispense events (/resources).

Is there a way in FHIR Query to use modifiers on '_revinclude' or '_include' to achieve above requirement?

Appreciate any guidance on this please.

@LKShankarKarthik LKShankarKarthik added the Question Issue is a question? label Aug 16, 2024
@mannawar
Copy link

First of all implementers would be the right person to answer this. But as a fallback mechanism you can use some thing like this as below. I just referenced firely as server.

` static async Task Main(string[] args)
{
CallWithoutReference();
}

private static async Task CallWithoutReference()
{
    var fhirServerUrl = "https://server.fire.ly/";
    var client = new FhirClient(fhirServerUrl);
    
    //1. Fetch MedicationRequests with their associated MedicationDispense resources
    var bundleWithDispenses = await client.SearchAsync<MedicationRequest>(new string[] {"_revinclude=MedicationDispense:medicationrequest" });
    var medicationRequestWithDispenses = bundleWithDispenses?.Entry.Where(e => e.Resource is MedicationRequest).Select(e => e.Resource as MedicationRequest).ToList();
    
    
    //2. Fetch all medication request
    var allMedicationRequest = new List<MedicationRequest>();
    Bundle bundle;

    var nextPageUrl = "MedicationRequest";

    do
    {
        bundle = await client.SearchAsync<MedicationRequest>(new string[]{$"_page=1&url={nextPageUrl}"});
        
        allMedicationRequest.AddRange(bundle.Entry.Where(e => e.Resource is MedicationRequest).Select(e => e.Resource as MedicationRequest));
        
        nextPageUrl = bundle.NextLink?.ToString();
    }while(nextPageUrl != null);
    
    //3. Filter now from those who have dispenses
    var dispenseIds = medicationRequestWithDispenses?.Select(req => req?.Id).ToHashSet();
    
    var medicationRequestWithoutDispenses = allMedicationRequest.Where(req => !dispenseIds.Contains(req.Id)).ToList();

    foreach (var req in medicationRequestWithoutDispenses)
    {
        Console.WriteLine($"MedicationRequest without Dispense: {req.Id}"); 
    }
}`

@EXPEkesheth
Copy link
Collaborator

Closing this issue as this is a question for FHIR implementers (refer to chat.fhir.org)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question Issue is a question?
Projects
None yet
Development

No branches or pull requests

3 participants