Retrieve MedicationRequest which doesnt have any MedicationDispense
LKShankarKarthik opened this issue · 2 comments
LKShankarKarthik commented
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.
mannawar commented
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 commented
Closing this issue as this is a question for FHIR implementers (refer to chat.fhir.org)