|
19 | 19 | import org.hl7.davinci.endpoint.rules.CoverageRequirementRuleResult; |
20 | 20 | import org.hl7.davinci.r4.CardTypes; |
21 | 21 | import org.hl7.davinci.r4.CoverageGuidance; |
| 22 | +import org.hl7.davinci.r4.crdhook.ConfigurationOption; |
22 | 23 | import org.hl7.davinci.r4.crdhook.DiscoveryExtension; |
23 | 24 | import org.hl7.davinci.r4.crdhook.orderselect.OrderSelectRequest; |
24 | 25 | import org.hl7.fhir.instance.model.api.IBaseResource; |
@@ -195,78 +196,91 @@ public CdsResponse handleRequest(@Valid @RequestBody requestTypeT request, URL a |
195 | 196 | // no error cards on empty when order-select request |
196 | 197 |
|
197 | 198 | boolean foundApplicableRule = false; |
| 199 | + int availableCardsLeft = hookConfiguration.getMaxCards(); |
| 200 | + |
198 | 201 | for (CoverageRequirementRuleResult lookupResult : lookupResults) { |
199 | 202 | requestLog.addTopic(requestService, lookupResult.getTopic()); |
200 | 203 | CqlResultsForCard results = executeCqlAndGetRelevantResults(lookupResult.getContext(), lookupResult.getTopic()); |
201 | 204 | CoverageRequirements coverageRequirements = results.getCoverageRequirements(); |
202 | 205 | cardBuilder.setDeidentifiedResourcesContainsPhi(lookupResult.getDeidentifiedResourceContainsPhi()); |
203 | 206 |
|
204 | | - if (results.ruleApplies()) { |
205 | | - foundApplicableRule = true; |
206 | | - |
207 | | - if (results.getCoverageRequirements().getApplies()) { |
208 | | - |
209 | | - // if prior auth already approved |
210 | | - if (coverageRequirements.isPriorAuthApproved()) { |
211 | | - response.addCard(cardBuilder.priorAuthCard(results, results.getRequest(), fhirComponents, coverageRequirements.getPriorAuthId(), |
212 | | - request.getContext().getPatientId(), lookupResult.getCriteria().getPayorId(), request.getContext().getUserId(), |
213 | | - applicationBaseUrl.toString() + "/fhir/" + fhirComponents.getFhirVersion().toString(), |
214 | | - fhirResourceRepository)); |
215 | | - |
216 | | - } else if (coverageRequirements.isDocumentationRequired() || coverageRequirements.isPriorAuthRequired()) { |
217 | | - if (StringUtils.isNotEmpty(coverageRequirements.getQuestionnaireOrderUri()) |
218 | | - || StringUtils.isNotEmpty(coverageRequirements.getQuestionnaireFaceToFaceUri()) |
219 | | - || StringUtils.isNotEmpty(coverageRequirements.getQuestionnaireLabUri()) |
220 | | - || StringUtils.isNotEmpty(coverageRequirements.getQuestionnaireProgressNoteUri()) |
221 | | - || StringUtils.isNotEmpty(coverageRequirements.getQuestionnairePARequestUri()) |
222 | | - || StringUtils.isNotEmpty(coverageRequirements.getQuestionnairePlanOfCareUri()) |
223 | | - || StringUtils.isNotEmpty(coverageRequirements.getQuestionnaireDispenseUri()) |
224 | | - || StringUtils.isNotEmpty(coverageRequirements.getQuestionnaireAdditionalUri())) { |
225 | | - List<Link> smartAppLinks = createQuestionnaireLinks(request, applicationBaseUrl, lookupResult, results); |
226 | | - |
227 | | - if (coverageRequirements.isPriorAuthRequired()) { |
228 | | - Card card = cardBuilder.transform(CardTypes.PRIOR_AUTH, results, smartAppLinks); |
229 | | - card.addSuggestionsItem(cardBuilder.createSuggestionWithNote(card, results.getRequest(), fhirComponents, |
230 | | - "Save Update To EHR", "Update original " + results.getRequest().fhirType() + " to add note", |
231 | | - true, CoverageGuidance.ADMIN)); |
232 | | - response.addCard(card); |
233 | | - } else if (coverageRequirements.isDocumentationRequired()) { |
234 | | - Card card = cardBuilder.transform(CardTypes.DTR_CLIN, results, smartAppLinks); |
235 | | - card.addSuggestionsItem(cardBuilder.createSuggestionWithNote(card, results.getRequest(), fhirComponents, |
236 | | - "Save Update To EHR", "Update original " + results.getRequest().fhirType() + " to add note", |
237 | | - true, CoverageGuidance.CLINICAL)); |
238 | | - response.addCard(card); |
239 | | - } |
240 | | - |
241 | | - // add a card for an alternative therapy if there is one |
242 | | - if (results.getAlternativeTherapy().getApplies() && hookConfiguration.getAlternativeTherapy()) { |
243 | | - try { |
244 | | - response.addCard(cardBuilder.alternativeTherapyCard(results.getAlternativeTherapy(), |
245 | | - results.getRequest(), fhirComponents)); |
246 | | - } catch (RuntimeException e) { |
247 | | - logger.warn("Failed to process alternative therapy: " + e.getMessage()); |
248 | | - } |
249 | | - } |
250 | | - } else { |
251 | | - logger.warn("Unspecified Questionnaire URI; summary card sent to client"); |
| 207 | + if (!results.ruleApplies()) |
| 208 | + continue; |
| 209 | + |
| 210 | + logger.info(String.valueOf(availableCardsLeft)); |
| 211 | + |
| 212 | + if (availableCardsLeft <= 0) |
| 213 | + break; |
| 214 | + |
| 215 | + foundApplicableRule = true; |
| 216 | + |
| 217 | + if (results.getCoverageRequirements().getApplies()) { |
| 218 | + // if prior auth already approved |
| 219 | + if (coverageRequirements.isPriorAuthApproved()) { |
| 220 | + response.addCard(cardBuilder.priorAuthCard(results, results.getRequest(), fhirComponents, coverageRequirements.getPriorAuthId(), |
| 221 | + request.getContext().getPatientId(), lookupResult.getCriteria().getPayorId(), request.getContext().getUserId(), |
| 222 | + applicationBaseUrl.toString() + "/fhir/" + fhirComponents.getFhirVersion().toString(), |
| 223 | + fhirResourceRepository)); |
| 224 | + break; |
| 225 | + } |
| 226 | + |
| 227 | + if (coverageRequirements.isDocumentationRequired() || coverageRequirements.isPriorAuthRequired()) { |
| 228 | + if (!coverageRequirements.hasQuestionnaireUri()) { |
| 229 | + logger.warn("Unspecified Questionnaire URI; summary card sent to client"); |
| 230 | + if (hookConfiguration.getCoverage()) { |
252 | 231 | response.addCard(cardBuilder.transform(CardTypes.COVERAGE, results)); |
253 | 232 | } |
254 | | - } else { |
255 | | - // no prior auth or documentation required |
256 | | - logger.info("Add the no doc or prior auth required card"); |
257 | | - Card card = cardBuilder.transform(CardTypes.COVERAGE, results); |
| 233 | + break; |
| 234 | + } |
| 235 | + |
| 236 | + List<Link> smartAppLinks = createQuestionnaireLinks(request, applicationBaseUrl, lookupResult, results); |
| 237 | + |
| 238 | + if (coverageRequirements.isPriorAuthRequired() && hookConfiguration.getPriorAuth()) { |
| 239 | + Card card = cardBuilder.transform(CardTypes.PRIOR_AUTH, results, smartAppLinks); |
258 | 240 | card.addSuggestionsItem(cardBuilder.createSuggestionWithNote(card, results.getRequest(), fhirComponents, |
259 | 241 | "Save Update To EHR", "Update original " + results.getRequest().fhirType() + " to add note", |
260 | | - true, CoverageGuidance.COVERED)); |
261 | | - card.setSelectionBehavior(Card.SelectionBehaviorEnum.ANY); |
| 242 | + true, CoverageGuidance.ADMIN)); |
| 243 | + response.addCard(card); |
| 244 | + availableCardsLeft--; |
| 245 | + } else if (coverageRequirements.isDocumentationRequired() && hookConfiguration.getDTRClin()) { |
| 246 | + Card card = cardBuilder.transform(CardTypes.DTR_CLIN, results, smartAppLinks); |
| 247 | + card.addSuggestionsItem(cardBuilder.createSuggestionWithNote(card, results.getRequest(), fhirComponents, |
| 248 | + "Save Update To EHR", "Update original " + results.getRequest().fhirType() + " to add note", |
| 249 | + true, CoverageGuidance.CLINICAL)); |
262 | 250 | response.addCard(card); |
| 251 | + availableCardsLeft--; |
263 | 252 | } |
| 253 | + |
| 254 | + // add a card for an alternative therapy if there is one |
| 255 | + if (availableCardsLeft != 0 && results.getAlternativeTherapy().getApplies() && hookConfiguration.getAlternativeTherapy()) { |
| 256 | + try { |
| 257 | + response.addCard(cardBuilder.alternativeTherapyCard(results.getAlternativeTherapy(), |
| 258 | + results.getRequest(), fhirComponents)); |
| 259 | + } catch (RuntimeException e) { |
| 260 | + logger.warn("Failed to process alternative therapy: " + e.getMessage()); |
| 261 | + } |
| 262 | + } |
| 263 | + break; |
264 | 264 | } |
265 | 265 |
|
266 | | - // apply the DrugInteractions |
267 | | - if (results.getDrugInteraction().getApplies()) { |
268 | | - response.addCard(cardBuilder.drugInteractionCard(results.getDrugInteraction(), results.getRequest())); |
| 266 | + // no prior auth or documentation required |
| 267 | + logger.info("Add the no doc or prior auth required card"); |
| 268 | + if (availableCardsLeft != 0 && hookConfiguration.getCoverage()) { |
| 269 | + Card card = cardBuilder.transform(CardTypes.COVERAGE, results); |
| 270 | + card.addSuggestionsItem(cardBuilder.createSuggestionWithNote(card, results.getRequest(), fhirComponents, |
| 271 | + "Save Update To EHR", "Update original " + results.getRequest().fhirType() + " to add note", |
| 272 | + true, CoverageGuidance.COVERED)); |
| 273 | + card.setSelectionBehavior(Card.SelectionBehaviorEnum.ANY); |
| 274 | + response.addCard(card); |
269 | 275 | } |
| 276 | + |
| 277 | + logger.info(String.valueOf(availableCardsLeft)); |
| 278 | + } |
| 279 | + |
| 280 | + // apply the DrugInteractions |
| 281 | + if (availableCardsLeft != 0 && results.getDrugInteraction().getApplies()) { |
| 282 | + response.addCard(cardBuilder.drugInteractionCard(results.getDrugInteraction(), results.getRequest())); |
| 283 | + availableCardsLeft--; |
270 | 284 | } |
271 | 285 | } |
272 | 286 |
|
|
0 commit comments