Repository navigation
changes for the benificiary phase sync #206
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
Changes from all commits
ec3ec6e
9e63d93
0b96122
43a16f6
aa7bfa1
eb0da96
084d0ca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| package com.iemr.mmu.controller.nikshay; | ||
|
|
||
| import com.google.gson.Gson; | ||
| import com.iemr.mmu.data.nikshay.NikshayMissingBeneficiaryDTO; | ||
| import com.iemr.mmu.data.nikshay.NikshaySyncStatusUpdateDTO; | ||
| import com.iemr.mmu.service.nikshay.NikshaySyncStatusService; | ||
| import com.iemr.mmu.utils.NikshayConstants; | ||
| import com.iemr.mmu.utils.exception.IEMRException; | ||
| import com.iemr.mmu.utils.response.OutputResponse; | ||
| import com.iemr.mmu.utils.validator.Validator; | ||
| import io.swagger.v3.oas.annotations.Operation; | ||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.data.domain.Page; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
| import org.springframework.web.bind.annotation.RequestHeader; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RequestParam; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.Map; | ||
|
|
||
| @RestController | ||
| @RequestMapping(NikshayConstants.NIKSHAY_PATH) | ||
| public class NikshaySyncStatusController { | ||
|
|
||
| private static final int MAX_PAGE_SIZE = 100; | ||
|
|
||
| private final Logger logger = LoggerFactory.getLogger(NikshaySyncStatusController.class); | ||
|
|
||
| private final Gson gson = new Gson(); | ||
|
|
||
| @Autowired | ||
|
Check warning on line 41 in src/main/java/com/iemr/mmu/controller/nikshay/NikshaySyncStatusController.java
|
||
| private NikshaySyncStatusService nikshaySyncStatusService; | ||
|
|
||
| @Autowired | ||
|
Check warning on line 44 in src/main/java/com/iemr/mmu/controller/nikshay/NikshaySyncStatusController.java
|
||
| private Validator validator; | ||
|
|
||
| @PostMapping("/syncStatus/syncFailed") | ||
| @Operation(summary = "Copy all FAILED rows from nikshay_beneficiary_phase_status into nikshay_beneficiary_sync_status") | ||
| public String syncFailedPhaseStatuses(@RequestHeader(value = "Authorization") String authorization, | ||
| @RequestHeader(value = "ServerAuthorization") String serverAuthorization) { | ||
| OutputResponse response = new OutputResponse(); | ||
| try { | ||
| Map<String, Object> result = nikshaySyncStatusService.syncFailedPhaseStatuses(serverAuthorization); | ||
| response.setResponse(gson.toJson(result)); | ||
| } catch (Exception e) { | ||
| logger.error("Error in syncFailedPhaseStatuses: " + e); | ||
|
Check warning on line 56 in src/main/java/com/iemr/mmu/controller/nikshay/NikshaySyncStatusController.java
|
||
| response.setError(5000, "Error syncing failed Nikshay phase statuses: " + e.getMessage()); | ||
| } | ||
| return response.toString(); | ||
| } | ||
|
|
||
| @GetMapping("/syncStatus/missingBeneficiaries") | ||
| @Operation(summary = "Get beneficiaries whose Ni-kshay sync failed (paged); all of them with no filter, " | ||
| + "else only those matching providerServiceMapID and/or villageID") | ||
| public ResponseEntity<Map<String, Object>> getMissingBeneficiaries( | ||
| @RequestParam(value = "providerServiceMapID", required = false) Integer providerServiceMapID, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should missingBeneficiaries and update (line 137) also check ServerAuthorization, like failedBeneficiaries does (line 121)? They return beneficiary names/villages and change data. |
||
| @RequestParam(value = "villageID", required = false) Integer villageID, | ||
| @RequestParam(value = "pageNo", defaultValue = "0") Integer pageNo, | ||
| @RequestParam(value = "pageSize", defaultValue = "30") Integer pageSize) { | ||
| logger.info("getMissingBeneficiaries request: providerServiceMapID={}, villageID={}, pageNo={}, pageSize={}", | ||
| providerServiceMapID, villageID, pageNo, pageSize); | ||
| try { | ||
| if (pageNo < 0) { | ||
| return errorResponse(HttpStatus.BAD_REQUEST, "pageNo must be 0 or greater"); | ||
| } | ||
| if (pageSize < 1 || pageSize > MAX_PAGE_SIZE) { | ||
| return errorResponse(HttpStatus.BAD_REQUEST, "pageSize must be between 1 and " + MAX_PAGE_SIZE); | ||
| } | ||
|
|
||
| Page<NikshayMissingBeneficiaryDTO> page = nikshaySyncStatusService.getMissingBeneficiaries( | ||
| providerServiceMapID, villageID, pageNo, pageSize); | ||
|
|
||
| Map<String, Object> pagination = new LinkedHashMap<>(); | ||
| pagination.put("page", page.getNumber()); | ||
| pagination.put("size", page.getSize()); | ||
| pagination.put("totalElements", page.getTotalElements()); | ||
| pagination.put("totalPages", page.getTotalPages()); | ||
| pagination.put("hasNext", page.hasNext()); | ||
|
|
||
| Map<String, Object> body = new LinkedHashMap<>(); | ||
| body.put("status", "SUCCESS"); | ||
|
Check failure on line 91 in src/main/java/com/iemr/mmu/controller/nikshay/NikshaySyncStatusController.java
|
||
| body.put("message", page.isEmpty() | ||
|
Check failure on line 92 in src/main/java/com/iemr/mmu/controller/nikshay/NikshaySyncStatusController.java
|
||
| ? "No missing beneficiary data found" | ||
| : "Missing beneficiary data fetched successfully"); | ||
| body.put("data", page.getContent()); | ||
| body.put("pagination", pagination); | ||
| return ResponseEntity.ok(body); | ||
| } catch (Exception e) { | ||
| logger.error("Error in getMissingBeneficiaries for providerServiceMapID={}, villageID={}", | ||
| providerServiceMapID, villageID, e); | ||
| return errorResponse(HttpStatus.INTERNAL_SERVER_ERROR, | ||
| "Error fetching missing beneficiary data: " + e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| @GetMapping(NikshayConstants.FAILED_BENEFICIARIES_PATH) | ||
| @Operation(summary = "Get every beneficiary with a FAILED phase in nikshay_beneficiary_phase_status, with all its phases (not paged)") | ||
| public ResponseEntity<Map<String, Object>> getFailedBeneficiaries( | ||
| @RequestHeader(value = "ServerAuthorization") String serverAuthorization, HttpServletRequest request) { | ||
| // The JWT filter lets java/ and okhttp clients through without a token, and HttpInterceptor skips | ||
| // an empty Authorization header, so the header's presence proves nothing. ServerAuthorization must | ||
| // be a live login session key (the one /syncFailed sends after the data-sync server login). | ||
| if (serverAuthorization == null || serverAuthorization.trim().isEmpty()) { | ||
| return errorResponse(HttpStatus.UNAUTHORIZED, "ServerAuthorization is required"); | ||
| } | ||
| String remoteAddress = request.getHeader("X-FORWARDED-FOR"); | ||
| if (remoteAddress == null || remoteAddress.trim().isEmpty()) { | ||
| remoteAddress = request.getRemoteAddr(); | ||
| } | ||
| try { | ||
| validator.checkKeyExists(serverAuthorization, remoteAddress); | ||
| } catch (IEMRException e) { | ||
| return errorResponse(HttpStatus.UNAUTHORIZED, "Invalid or expired ServerAuthorization"); | ||
| } | ||
|
|
||
| try { | ||
| return ResponseEntity.ok(nikshaySyncStatusService.getFailedBeneficiaries()); | ||
| } catch (Exception e) { | ||
| logger.error("Error in getFailedBeneficiaries", e); | ||
| return errorResponse(HttpStatus.INTERNAL_SERVER_ERROR, | ||
| "Error fetching failed Nikshay beneficiaries: " + e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| @PostMapping("/syncStatus/update") | ||
| @Operation(summary = "Set isUpdated on all nikshay_beneficiary_sync_status rows of a beneficiary") | ||
| public ResponseEntity<Map<String, Object>> updateSyncStatus(@RequestBody NikshaySyncStatusUpdateDTO request) { | ||
| logger.info("updateSyncStatus request: beneficiaryId={}, isUpdated={}", | ||
| request != null ? request.getBeneficiaryId() : null, | ||
| request != null ? request.getIsUpdated() : null); | ||
| try { | ||
| if (request == null || request.getBeneficiaryId() == null) { | ||
| return errorResponse(HttpStatus.BAD_REQUEST, "beneficiaryId is required"); | ||
| } | ||
| if (request.getIsUpdated() == null) { | ||
| return errorResponse(HttpStatus.BAD_REQUEST, "isUpdated is required"); | ||
| } | ||
|
|
||
| int rows = nikshaySyncStatusService.updateIsUpdatedByBeneficiaryId( | ||
| request.getBeneficiaryId(), request.getIsUpdated()); | ||
| if (rows == 0) { | ||
| return errorResponse(HttpStatus.NOT_FOUND, | ||
| "No sync status record found for beneficiaryId " + request.getBeneficiaryId()); | ||
| } | ||
|
|
||
| Map<String, Object> data = new LinkedHashMap<>(); | ||
| data.put("beneficiaryId", request.getBeneficiaryId()); | ||
| data.put("isUpdated", request.getIsUpdated()); | ||
| data.put("recordsUpdated", rows); | ||
|
|
||
| Map<String, Object> body = new LinkedHashMap<>(); | ||
| body.put("status", "SUCCESS"); | ||
| body.put("message", "Update done for beneficiaryId " + request.getBeneficiaryId()); | ||
| body.put("data", data); | ||
| return ResponseEntity.ok(body); | ||
| } catch (Exception e) { | ||
| logger.error("Error in updateSyncStatus for beneficiaryId={}", | ||
| request != null ? request.getBeneficiaryId() : null, e); | ||
| return errorResponse(HttpStatus.INTERNAL_SERVER_ERROR, | ||
| "Error updating Nikshay sync status: " + e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| private ResponseEntity<Map<String, Object>> errorResponse(HttpStatus httpStatus, String message) { | ||
| if (httpStatus.is4xxClientError()) { | ||
| logger.warn("Nikshay sync status request rejected ({}): {}", httpStatus.value(), message); | ||
| } | ||
| Map<String, Object> body = new LinkedHashMap<>(); | ||
| body.put("status", "FAILED"); | ||
| body.put("message", message); | ||
| body.put("data", new ArrayList<>()); | ||
| return ResponseEntity.status(httpStatus).body(body); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package com.iemr.mmu.data.nikshay; | ||
|
|
||
| // One row of NikshayBeneficiarySyncStatusRepo.findBeneficiaryNames; column aliases match the getters | ||
| public interface BeneficiaryNameRow { | ||
|
|
||
| Number getBenRegID(); | ||
|
|
||
| String getFirstName(); | ||
|
|
||
| String getLastName(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| package com.iemr.mmu.data.nikshay; | ||
|
|
||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.GeneratedValue; | ||
| import jakarta.persistence.GenerationType; | ||
| import jakarta.persistence.Id; | ||
| import jakarta.persistence.Table; | ||
| import lombok.Data; | ||
| import org.hibernate.annotations.CreationTimestamp; | ||
| import org.hibernate.annotations.UpdateTimestamp; | ||
|
|
||
| import java.sql.Timestamp; | ||
|
|
||
| @Entity | ||
| @Table(name = "nikshay_beneficiary_phase_status", schema = "db_iemr") | ||
| @Data | ||
| public class NikshayBeneficiaryPhaseStatus { | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| private Long id; | ||
|
|
||
| @Column(name = "ben_reg_id", nullable = false) | ||
| private Long benRegID; | ||
|
|
||
| // BeneficiaryID from db_identity.m_beneficiaryregidmapping; null if not found | ||
| @Column(name = "beneficiary_id") | ||
| private Long beneficiaryID; | ||
|
|
||
| // INDEX | CONTACT | ALL_NEGATIVE | ||
| @Column(name = "beneficiary_type", length = 20, nullable = false) | ||
| private String beneficiaryType; | ||
|
|
||
| // For a CONTACT: BenRegId of their index patient | ||
| @Column(name = "index_ben_reg_id") | ||
| private Long indexBenRegID; | ||
|
|
||
| // ENROLMENT | CONTACTS | XRAY_UPLOAD | NAAT_UPLOAD | TRANSFER_OUT | ||
| @Column(name = "phase", length = 20, nullable = false) | ||
| private String phase; | ||
|
|
||
| // PENDING | IN_PROGRESS | DONE | FAILED | NOT_APPLICABLE | ||
| @Column(name = "status", length = 20, nullable = false) | ||
| private String status; | ||
|
|
||
| @Column(name = "failure_reason", length = 50) | ||
| private String failureReason; | ||
|
|
||
| @Column(name = "error_detail", length = 2000) | ||
| private String errorDetail; | ||
|
|
||
| @Column(name = "attempts", nullable = false) | ||
| private Integer attempts = 0; | ||
|
|
||
| @Column(name = "nikshay_id", length = 50) | ||
| private String nikshayId; | ||
|
|
||
| @Column(name = "episode_id", length = 50) | ||
| private String episodeId; | ||
|
|
||
| @Column(name = "last_attempt_at") | ||
| private Timestamp lastAttemptAt; | ||
|
|
||
| @Column(name = "completed_at") | ||
| private Timestamp completedAt; | ||
|
|
||
| @CreationTimestamp | ||
| @Column(name = "created_at", updatable = false) | ||
| private Timestamp createdAt; | ||
|
|
||
| @UpdateTimestamp | ||
| @Column(name = "updated_at") | ||
| private Timestamp updatedAt; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| package com.iemr.mmu.data.nikshay; | ||
|
|
||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.GeneratedValue; | ||
| import jakarta.persistence.GenerationType; | ||
| import jakarta.persistence.Id; | ||
| import jakarta.persistence.Table; | ||
| import lombok.Data; | ||
| import org.hibernate.annotations.CreationTimestamp; | ||
| import org.hibernate.annotations.UpdateTimestamp; | ||
|
|
||
| import java.sql.Timestamp; | ||
|
|
||
| @Entity | ||
| @Table(name = "nikshay_beneficiary_sync_status", schema = "db_iemr") | ||
|
PankajUnthin marked this conversation as resolved.
Resolved
|
||
| @Data | ||
| public class NikshayBeneficiarySyncStatus { | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| private Long id; | ||
|
|
||
| @Column(name = "ben_reg_id", nullable = false) | ||
| private Long benRegID; | ||
|
|
||
| // BeneficiaryID from db_identity.m_beneficiaryregidmapping; null if not found | ||
| @Column(name = "beneficiary_id") | ||
| private Long beneficiaryID; | ||
|
|
||
| // INDEX | CONTACT | ALL_NEGATIVE | ||
| @Column(name = "beneficiary_type", length = 20, nullable = false) | ||
| private String beneficiaryType; | ||
|
|
||
| // For a CONTACT: BenRegId of their index patient | ||
| @Column(name = "index_ben_reg_id") | ||
| private Long indexBenRegID; | ||
|
|
||
| // ENROLMENT | CONTACTS | XRAY_UPLOAD | NAAT_UPLOAD | TRANSFER_OUT | ||
| @Column(name = "phase", length = 20, nullable = false) | ||
| private String phase; | ||
|
|
||
| // PENDING | IN_PROGRESS | DONE | FAILED | NOT_APPLICABLE | ||
| @Column(name = "status", length = 20, nullable = false) | ||
| private String status; | ||
|
|
||
| @Column(name = "failure_reason", length = 50) | ||
| private String failureReason; | ||
|
|
||
| @Column(name = "error_detail", length = 2000) | ||
| private String errorDetail; | ||
|
|
||
| @Column(name = "nikshay_id", length = 50) | ||
| private String nikshayId; | ||
|
|
||
| @Column(name = "episode_id", length = 50) | ||
| private String episodeId; | ||
|
|
||
| @Column(name = "last_attempt_at") | ||
| private Timestamp lastAttemptAt; | ||
|
|
||
| @Column(name = "is_updated", nullable = false) | ||
| private Boolean isUpdated = false; | ||
|
|
||
| @CreationTimestamp | ||
| @Column(name = "created_at", updatable = false) | ||
| private Timestamp createdAt; | ||
|
|
||
| @UpdateTimestamp | ||
| @Column(name = "updated_at") | ||
| private Timestamp updatedAt; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do not put exact ip value it is just blank or Enter failed URL in <>