Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ public String retryOrder(@RequestParam Long beneficiaryId, @RequestParam String

@PostMapping("/order/result")
@Operation(summary = "Get the diagnostic result for beneficiaryId+orderType, at whatever stage it's currently in "
+ "(targets the latest order unless visitCode is given)")
+ "(targets the latest order unless visitCode is given). results[] has one entry per test: the order's "
+ "own (XRAY_CHEST / MTB / MTB_PLUS), then MDR_RIF on an MTB order once its RIF test exists. status INVALID: the MTB/RIF test came back "
+ "invalid (e.g. Error-2) and the machine is retesting the same order — it is still polled and "
+ "moves to COMPLETED when the retest result arrives.")
public String getOrderResult(@RequestParam Long beneficiaryId, @RequestParam String orderType,
@RequestParam(required = false) Long visitCode) {
OutputResponse response = new OutputResponse();
Expand All @@ -114,7 +117,8 @@ public String getOrderResult(@RequestParam Long beneficiaryId, @RequestParam Str

@PostMapping("/order/poll")
@Operation(summary = "Trigger an immediate poll for one beneficiary+orderType's diagnostic order and return the result (ops use). "
+ "Targets the latest order unless visitCode is given")
+ "Targets the latest order unless visitCode is given. An invalid MTB/RIF result gives status INVALID "
+ "(still polled, no retest order); an invalid X-ray closes the order and pushes a retest.")
Comment thread
sehjotsinghunthinkable marked this conversation as resolved.
Resolved
public String pollOrder(@RequestParam Long beneficiaryId, @RequestParam String orderType,
@RequestParam(required = false) Long visitCode) {
OutputResponse response = new OutputResponse();
Expand All @@ -129,11 +133,19 @@ public String pollOrder(@RequestParam Long beneficiaryId, @RequestParam String o
}

@PostMapping("/order/manualResult")
@Operation(summary = "Exactly one of resultSummary/reasonToClose is required. resultSummary: manually submit "
+ "a diagnostic result for a beneficiary's latest order of the given type, for use when no vendor "
+ "device is integrated. Stored as-is; tbPresence, tbConfidence, and drugResistancePresence are left "
+ "null since nothing derives them. Rejected if that order is already COMPLETED. reasonToClose: close "
+ "the order instead (same behavior as reasonToClose on /order/push), sourcing patient/orderEvent "
@Operation(summary = "Send either a result (resultSummary and/or mdrRifResult) or one reasonToClose. resultSummary: "
+ "manually submit a diagnostic result for a beneficiary's latest order of the given type, for use when "
+ "no vendor device is integrated. Stored like a vendor result, one row per test in results[]. "
+ "MTB/MTB_PLUS orders carry two results: resultSummary is the MTB result; mdrRifResult is the MDR RIF "
+ "result, stored as the order's MDR_RIF result (and tb_suspected's MDR RIF result), only allowed on a positive MTB. "
+ "A positive MTB without mdrRifResult leaves the order open until RIF is in: PENDING if it was pushed "
+ "to a vendor (polling continues, and the vendor's result replaces a differing manual one), "
+ "MANUAL_ENTRY if there is no vendor (send mdrRifResult or reasonToCloseRif). Otherwise the order "
+ "is COMPLETED; a vendor's differing result still replaces it when its documents are fetched. "
+ "An invalid result (resultSummary or mdrRifResult \"Invalid/Error\") closes the order. "
+ "Rejected if that order is already COMPLETED or CLOSED (push a new order to test again). Closing (same as on /order/push): reasonToCloseXray for "
+ "XRAY_CHEST; reasonToCloseMTB when the MTB test wasn't done; reasonToCloseRif when MTB was positive but "
+ "MDR RIF wasn't done — closes that same order, keeping its MTB result. Patient/orderEvent are sourced "
+ "from the beneficiary's existing order for this type.")
public String submitManualResult(@RequestBody @Valid ManualDiagnosticResultRequestDto request,
@RequestHeader("JwtToken") String jwtToken) {
Expand All @@ -150,7 +162,8 @@ public String submitManualResult(@RequestBody @Valid ManualDiagnosticResultReque

@GetMapping("/order/getBeneficiariesByStatus")
@Operation(summary = "Get beneficiary IDs bucketed by diagnostic order status for the given order type, "
+ "optionally filtered by village and/or provider service map")
+ "optionally filtered by village and/or provider service map. invalid: MTB/RIF invalid, waiting for "
+ "the machine's own retest")
public ResponseEntity<ApiResponse> getBeneficiariesByStatus(
@RequestParam DiagnosticOrderType orderType,
@RequestParam(required = false) Integer villageId,
Expand Down
21 changes: 19 additions & 2 deletions src/main/java/com/iemr/flw/domain/iemr/DiagnosticOrder.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,17 @@ public class DiagnosticOrder {
@Column(name = "error_message", columnDefinition = "TEXT")
private String errorMessage;

@Column(name = "reason_to_close", columnDefinition = "TEXT")
private String reasonToClose;
// Why the order was closed, one column per test (see DiagnosticCloseStage): xray, mtb, or mdrrif
// for an MTB/MTB_PLUS order whose MTB was positive but whose MDR RIF test wasn't done. Set through
// setReasonToClose(stage, reason). The old reason_to_close column is no longer mapped.
@Column(name = "reason_to_close_xray", columnDefinition = "TEXT")
private String reasonToCloseXray;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

@Column(name = "reason_to_close_mtb", columnDefinition = "TEXT")
private String reasonToCloseMTB;

@Column(name = "reason_to_close_mdrrif", columnDefinition = "TEXT")
private String reasonToCloseMDRRIF;

@Column(name = "push_response_json", columnDefinition = "LONGTEXT")
private String pushResponseJson;
Expand Down Expand Up @@ -115,4 +124,12 @@ public class DiagnosticOrder {
@org.hibernate.annotations.CreationTimestamp
@Column(name = "synced_date")
private Timestamp syncedDate;

public void setReasonToClose(com.iemr.flw.masterEnum.DiagnosticCloseStage stage, String reason) {
switch (stage) {
case XRAY -> reasonToCloseXray = reason;
case MTB -> reasonToCloseMTB = reason;
case RIF -> reasonToCloseMDRRIF = reason;
}
}
}
6 changes: 5 additions & 1 deletion src/main/java/com/iemr/flw/domain/iemr/DiagnosticResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

@Entity
@Table(name = "tb_diagnostic_result", schema = "db_iemr",
uniqueConstraints = @UniqueConstraint(columnNames = {"external_order_id"}))
uniqueConstraints = @UniqueConstraint(columnNames = {"external_order_id", "result_type"}))
@Data
public class DiagnosticResult {

Expand All @@ -23,6 +23,10 @@ public class DiagnosticResult {
@Column(name = "external_order_id", length = 100)
private String externalOrderId;

// Which test this row holds (DiagnosticResultType): the order's own test, or MDR_RIF.
@Column(name = "result_type", length = 20)
private String resultType;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

@Column(name = "beneficiary_id")
private Long beneficiaryId;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@

private String orderEvent;

private String reasonToClose;
// Close instead of pushing/entering a result — send at most one, for the test that wasn't done:
// reasonToCloseXray (XRAY_CHEST); reasonToCloseMTB (MTB/MTB_PLUS, MTB test not done);

Check warning on line 23 in src/main/java/com/iemr/flw/dto/DiagnosticOrderRequestDto.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

This block of commented-out lines of code should be removed.

See more on https://sonarcloud.io/project/issues?id=PSMRI_FLW-API&issues=AaHTQ8HrxgIGZ2MAP3xG&open=AaHTQ8HrxgIGZ2MAP3xG&pullRequest=414
// reasonToCloseRif (MTB/MTB_PLUS, MTB was positive but the MDR RIF test wasn't done).
private String reasonToCloseXray;
private String reasonToCloseMTB;
private String reasonToCloseRif;

@NotNull
@Valid
Expand Down
16 changes: 10 additions & 6 deletions src/main/java/com/iemr/flw/dto/DiagnosticOrderResultDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

@Data
@NoArgsConstructor
public class DiagnosticOrderResultDto {

private Long beneficiaryId;
private Long visitCode;
private String externalOrderId;
private String orderType;
private String status;
private String providerStatus;
private String resultSummary;
private String errorMessage;
private String reasonToClose;
private Boolean tbPresence;
private Double tbConfidence;
private Boolean drugResistancePresence;
private String reasonToCloseXray;
private String reasonToCloseMTB;
private String reasonToCloseMDRRIF;
// One entry per tb_diagnostic_result row: the order's own test first, then MDR_RIF when present.
private List<DiagnosticResultItemDto> results = new ArrayList<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ public class DiagnosticOrderStatusSummaryDto {
private List<Long> failed;
private List<Long> closed;
private List<Long> awaitingManualEntry;
// MTB/RIF invalid, waiting for the machine's own retest (still polled).
private List<Long> invalid;
}
19 changes: 19 additions & 0 deletions src/main/java/com/iemr/flw/dto/DiagnosticResultItemDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.iemr.flw.dto;

import lombok.Data;
import lombok.NoArgsConstructor;

/** One test's result on an order: the order's own test, or MDR_RIF on an MTB/MTB_PLUS order. */
@Data
@NoArgsConstructor
public class DiagnosticResultItemDto {

// XRAY_CHEST, MTB, MTB_PLUS or MDR_RIF
private String resultType;
private String providerStatus;
private String resultSummary;
private Boolean tbPresence;
private Double tbConfidence;
// MDR_RIF only: true = DR TB, false = Non DR TB, null = not done / indeterminate
private Boolean drugResistancePresence;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,18 @@
@NotNull
private String orderType;

// MTB / MTB_PLUS: the MTB test's result (e.g. "TB Positive", "TB Negative").
private String resultSummary;

private String reasonToClose;
// MTB / MTB_PLUS only: the MDR RIF result run on the same order after a positive MTB
// (e.g. "DR TB", "Non DR TB"). Send it with resultSummary, or on its own in a later call
// once the MTB result is stored.
private String mdrRifResult;

// Close instead of pushing/entering a result — send at most one, for the test that wasn't done:
// reasonToCloseXray (XRAY_CHEST); reasonToCloseMTB (MTB/MTB_PLUS, MTB test not done);

Check warning on line 26 in src/main/java/com/iemr/flw/dto/ManualDiagnosticResultRequestDto.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

This block of commented-out lines of code should be removed.

See more on https://sonarcloud.io/project/issues?id=PSMRI_FLW-API&issues=AaHTQ8F2xgIGZ2MAP3xF&open=AaHTQ8F2xgIGZ2MAP3xF&pullRequest=414
// reasonToCloseRif (MTB/MTB_PLUS, MTB was positive but the MDR RIF test wasn't done).
private String reasonToCloseXray;
private String reasonToCloseMTB;
private String reasonToCloseRif;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ public class DiagnosticDocumentAsset {
private String contentType;
private String fileName;
private String base64Content;
private String component;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,30 @@
private Boolean tbPresence;
private Double tbConfidence;
private Boolean drugResistancePresence;
// The MDR RIF test's own summary (e.g. "DR TB", "Non DR TB", "Indeterminate"), run on the same
// order when MTB is positive. Null when no RIF test ran or it isn't finished.
private String drugResistanceSummary;
// The MDR RIF test's own status from the vendor (PENDING / COMPLETED / FAILED). Null when there is
// no RIF test.
private String drugResistanceStatus;
// The order's own test's status (MTB / MTB_PLUS component) from the vendor, kept apart from the
// order status above: an MTB test is COMPLETED while the order stays PENDING for its RIF test.
// Null when the vendor reported none (X-ray, manual entry) — the result row then works it out.
private String mainStatus;

public DiagnosticPollResult(DiagnosticOrderStatus status, String providerOrderId, String resultSummary,

Check warning on line 34 in src/main/java/com/iemr/flw/integration/provider/DiagnosticPollResult.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Constructor has 10 parameters, which is greater than 7 authorized.

See more on https://sonarcloud.io/project/issues?id=PSMRI_FLW-API&issues=AaHTQ8hxxgIGZ2MAP3xc&open=AaHTQ8hxxgIGZ2MAP3xc&pullRequest=414
String rawResponseJson, List<DiagnosticDocumentAsset> assets, String errorMessage,
Boolean tbPresence, Double tbConfidence, Boolean drugResistancePresence,
String drugResistanceSummary) {
this(status, providerOrderId, resultSummary, rawResponseJson, assets, errorMessage, tbPresence,
tbConfidence, drugResistancePresence, drugResistanceSummary, null, null);
}

public DiagnosticPollResult(DiagnosticOrderStatus status, String providerOrderId, String resultSummary,

Check warning on line 42 in src/main/java/com/iemr/flw/integration/provider/DiagnosticPollResult.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Constructor has 11 parameters, which is greater than 7 authorized.

See more on https://sonarcloud.io/project/issues?id=PSMRI_FLW-API&issues=AaHT0HyspwGOAvZzLLpW&open=AaHT0HyspwGOAvZzLLpW&pullRequest=414
String rawResponseJson, List<DiagnosticDocumentAsset> assets, String errorMessage,
Boolean tbPresence, Double tbConfidence, Boolean drugResistancePresence,
String drugResistanceSummary, String drugResistanceStatus) {
this(status, providerOrderId, resultSummary, rawResponseJson, assets, errorMessage, tbPresence,
tbConfidence, drugResistancePresence, drugResistanceSummary, drugResistanceStatus, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public DiagnosticProvider getProvider(String providerCode) {
return provider;
}

// XRAY_CHEST is routed to its own vendor; the TrueNat family (MTB/MTB_PLUS/MDR_RIF) shares
// XRAY_CHEST is routed to its own vendor; the TrueNat family (MTB/MTB_PLUS) shares
// another, each configurable independently so a new vendor can be swapped in for either group
// without a code change.
public String getProviderCodeForOrderType(DiagnosticOrderType orderType) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.iemr.flw.integration.provider;

import com.iemr.flw.masterEnum.DiagnosticOrderType;

/**
* How a TrueNat (MTB / MTB_PLUS) order's two results are read, shared by the vendor poll and
* manual entry so both store the same thing. One order carries both tests: the MTB result itself,
* and an MDR RIF result that is only run when MTB comes back positive.
*/
public final class TrueNatResultRules {

public static final String SUMMARY_TB_POSITIVE = "TB Positive";
public static final String SUMMARY_TB_NEGATIVE = "TB Negative";
public static final String SUMMARY_DR_TB = "DR TB";
public static final String SUMMARY_NON_DR_TB = "Non DR TB";

private TrueNatResultRules() {
}

public static boolean isTrueNat(String orderType) {
return DiagnosticOrderType.MTB.name().equals(orderType) || DiagnosticOrderType.MTB_PLUS.name().equals(orderType);
}

/** A positive MTB result is followed by an MDR RIF test on the same order. */
public static boolean needsMdrRif(String mtbSummary) {
return mtbSummary != null && SUMMARY_TB_POSITIVE.equalsIgnoreCase(mtbSummary.trim());
}

/** TB presence from the MTB result; null for non-TrueNat orders or an unknown/error summary. */
public static Boolean classifyTbPresence(String orderType, String summary) {
if (!isTrueNat(orderType) || summary == null) {
return null;

Check failure on line 32 in src/main/java/com/iemr/flw/integration/provider/TrueNatResultRules.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Null is returned but a "Boolean" is expected.

See more on https://sonarcloud.io/project/issues?id=PSMRI_FLW-API&issues=AaHTQ8hvxgIGZ2MAP3xY&open=AaHTQ8hvxgIGZ2MAP3xY&pullRequest=414
}
if (SUMMARY_TB_POSITIVE.equalsIgnoreCase(summary.trim())) {
return true;
}
if (SUMMARY_TB_NEGATIVE.equalsIgnoreCase(summary.trim())) {
return false;
}
return null;

Check failure on line 40 in src/main/java/com/iemr/flw/integration/provider/TrueNatResultRules.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Null is returned but a "Boolean" is expected.

See more on https://sonarcloud.io/project/issues?id=PSMRI_FLW-API&issues=AaHTQ8hvxgIGZ2MAP3xZ&open=AaHTQ8hvxgIGZ2MAP3xZ&pullRequest=414
}

/** Rifampicin resistance from the MDR RIF result; null when there is none or it is indeterminate. */
public static Boolean classifyDrugResistance(String summary) {
if (summary == null) {
return null;

Check failure on line 46 in src/main/java/com/iemr/flw/integration/provider/TrueNatResultRules.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Null is returned but a "Boolean" is expected.

See more on https://sonarcloud.io/project/issues?id=PSMRI_FLW-API&issues=AaHTQ8hvxgIGZ2MAP3xa&open=AaHTQ8hvxgIGZ2MAP3xa&pullRequest=414
}
if (SUMMARY_DR_TB.equalsIgnoreCase(summary.trim())) {
return true;
}
if (SUMMARY_NON_DR_TB.equalsIgnoreCase(summary.trim())) {
return false;
}
return null;

Check failure on line 54 in src/main/java/com/iemr/flw/integration/provider/TrueNatResultRules.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Null is returned but a "Boolean" is expected.

See more on https://sonarcloud.io/project/issues?id=PSMRI_FLW-API&issues=AaHTQ8hvxgIGZ2MAP3xb&open=AaHTQ8hvxgIGZ2MAP3xb&pullRequest=414
}
}
Loading
Loading