diff --git a/.gitignore b/.gitignore index 769653c4..a4786a90 100644 --- a/.gitignore +++ b/.gitignore @@ -31,3 +31,4 @@ mvnw mvnw.cmd .mvn /logs/ +src/main/environment/common_local.properties diff --git a/pom.xml b/pom.xml index 512a160b..b9c36ae8 100644 --- a/pom.xml +++ b/pom.xml @@ -337,6 +337,32 @@ + + io.github.git-commit-id + git-commit-id-maven-plugin + 9.0.2 + + + get-the-git-infos + + revision + + initialize + + + + true + ${project.build.outputDirectory}/git.properties + + ^git.branch$ + ^git.commit.id.abbrev$ + ^git.build.version$ + ^git.build.time$ + + false + false + + org.apache.maven.plugins maven-antrun-plugin @@ -420,4 +446,4 @@ - \ No newline at end of file + diff --git a/src/main/environment/common_ci.properties b/src/main/environment/common_ci.properties index 347e06a3..50ae2030 100644 --- a/src/main/environment/common_ci.properties +++ b/src/main/environment/common_ci.properties @@ -1,6 +1,7 @@ fhir-url = @env.FHIR_API@ tm-url = @env.TM_API@ +common-api-base-url=@env.COMMON_API@ ##--------------------------------------------## Primary db------------------------------------------------------------------- @@ -16,8 +17,7 @@ secondary.datasource.password=@env.DATABASE_IDENTITY_PASSWORD@ secondary.datasource.url=@env.DATABASE_IDENTITY_URL@ secondary.datasource.driver-class-name=com.mysql.jdbc.Driver -springdoc.api-docs.enabled=@env.SWAGGER_UI_ENABLED@ -springdoc.swagger-ui.enabled=@env.SWAGGER_UI_ENABLED@ + #ELK logging file name logging.path=logs/ @@ -37,4 +37,17 @@ sms-username=@env.SMS_USERNAME@ sms-password=@env.SMS_PASSWORD@ send-message-url=@env.SMS_MESSAGE_URL@ -crash.logs.base.path=@env.CRASH_LOGS_PATH@ +#crash.logs.base.path=@env.CRASH_LOGS_PATH@ + +# ABHA Token Config +abha.client.id=@env.ABHA_CLIENT_ID@ +abha.client.secret=@env.ABHA_CLIENT_SECRET@ +abha.token.url=@env.ABHA_TOKEN_URL@ +abha.xcmid=@env.ABHA_XCM_ID@ + +# GovThealth API Config +govthealth.user.details.url=@env.GOVTHEALTH_USER_DETAILS_URL@ +govthealth.user.id=@env.GOVTHEALTH_USER_ID@ +govthealth.password=@env.GOVTHEALTH_PASSWORD@ + + diff --git a/src/main/environment/common_docker.properties b/src/main/environment/common_docker.properties index 958a9a19..8a31d5d6 100644 --- a/src/main/environment/common_docker.properties +++ b/src/main/environment/common_docker.properties @@ -1,5 +1,8 @@ -fhir-url = ${FHIR_URL} -tm-url = ${TM_URL} +fhir-url = ${FHIR_API} +tm-url = ${TM_API} +common-api-base-url=${COMMON_API} + + ##--------------------------------------------## Primary db------------------------------------------------------------------- @@ -15,8 +18,8 @@ secondary.datasource.password=${DATABASE_IDENTITY_PASSWORD} secondary.datasource.url=${DATABASE_IDENTITY_URL} secondary.datasource.driver-class-name=com.mysql.jdbc.Driver -springdoc.api-docs.enabled=true -springdoc.swagger-ui.enabled=true + + #ELK logging file name logging.path=logs/ @@ -35,4 +38,18 @@ sms-password=${SMS_PASSWORD} send-message-url=${SMS_MESSAGE_URL} -crash.logs.base.path=${CRASH_LOGS_PATH} +#crash.logs.base.path=${CRASH_LOGS_PATH} + +# ABHA Token Config +abha.client.id=${ABHA_CLIENT_ID} +abha.client.secret=${ABHA_CLIENT_SECRET} +abha.token.url=${ABHA_TOKEN_URL} +abha.xcmid=${ABHA_XCM_ID} + +# GovThealth API Config +govthealth.user.details.url=${GOVTHEALTH_USER_DETAILS_URL} +govthealth.user.id=${GOVTHEALTH_USER_ID} +govthealth.password=${GOVTHEALTH_PASSWORD} + + + diff --git a/src/main/environment/common_example.properties b/src/main/environment/common_example.properties index 51873ab6..3c679d09 100644 --- a/src/main/environment/common_example.properties +++ b/src/main/environment/common_example.properties @@ -38,6 +38,12 @@ send-message-url= crash.logs.base.path= +# ABHA Token Config +abha.client.id= +abha.client.secret= +abha.token.url= +abha.xcmid= + diff --git a/src/main/environment/common_local.properties b/src/main/environment/common_local.properties deleted file mode 100644 index 6e5bdd00..00000000 --- a/src/main/environment/common_local.properties +++ /dev/null @@ -1,45 +0,0 @@ -spring.main.banner-mode=off -spring.data.jpa.repositories.enabled=true -#spring.jpa.hibernate.ddl-auto=none - -# Naming strategies -spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl -spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl - -# The SQL dialect makes Hibernate generate better SQL for the chosen database -spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQLDialect - -#Below lines are added for security reasons - -spring.session.store-type=redis -spring.redis.password= -spring.redis.port=6379 - -## Below values are needed for extending the expiry time and extend expiry time. -iemr.extend.expiry.time=true -iemr.session.expiry.time=7200 -enableIPValidation=false - -#logging.pattern.level=DEBUG -logging.level.root=INFO -logging.level.org.springframework.web=INFO -logging.level.org.hibernate=INFO -logging.level.com.iemr=DEBUG -logging.level.org.springframework=INFO - -spring.datasource.tomcat.initial-size=5 -spring.datasource.tomcat.max-idle=15 -spring.datasource.tomcat.max-active=30 -spring.datasource.tomcat.min-idle=5 -spring.datasource.tomcat.min-evictable-idle-time-millis=15000 -spring.datasource.tomcat.remove-abandoned=true -spring.datasource.tomcat.logAbandoned=true -spring.datasource.continue-on-error=true -spring.datasource.tomcat.remove-abandoned-timeout=600 -door-to-door-page-size=10 - -#Get-HRP-Status -get-HRP-Status=ANC/getHRPStatus - -#Get Beneficiary ABHA -getHealthID=healthID/getBenhealthID diff --git a/src/main/java/com/iemr/flw/controller/AbhaBeneficiaryController.java b/src/main/java/com/iemr/flw/controller/AbhaBeneficiaryController.java new file mode 100644 index 00000000..8025de55 --- /dev/null +++ b/src/main/java/com/iemr/flw/controller/AbhaBeneficiaryController.java @@ -0,0 +1,54 @@ +package com.iemr.flw.controller; + +import com.iemr.flw.domain.iemr.AdolescentHealth; +import com.iemr.flw.dto.abhaBeneficiary.AbhaBeneficiaryDTO; +import com.iemr.flw.dto.identity.GetBenRequestHandler; +import com.iemr.flw.dto.iemr.AbhaRequestDTO; +import com.iemr.flw.service.AbhaBeneficiaryService; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@RestController +@RequestMapping("/UserRegistration") +public class AbhaBeneficiaryController { + @Autowired + private AbhaBeneficiaryService abhaBeneficiaryService; + + private final org.slf4j.Logger logger = LoggerFactory.getLogger(AbhaBeneficiaryController.class); + + + + @RequestMapping(value = "/GetUserDetailsByAyushmanCardNo",method = RequestMethod.POST) + public ResponseEntity> getAllAdolescentHealth(@RequestBody AbhaRequestDTO request ) { + Map response = new HashMap<>(); + try { + if(request.getCardNo()!=null){ + Object abhaBeneficiaryDTOList = abhaBeneficiaryService.getBeneficiaryByAbha(request); + + if (abhaBeneficiaryDTOList != null ) { + response.put("statusCode",200); + response.put("data", abhaBeneficiaryDTOList); + } else { + response.put("statusCode", 5000); + response.put("error", "Invalid/NULL request obj"); + } + } + + } catch (Exception e) { + logger.error("Error in get data : " + e); + response.put("statusCode",5000); + response.put("error","Error in get data : " + e); + + } + return ResponseEntity.ok(response); + } +} diff --git a/src/main/java/com/iemr/flw/controller/AbhaTokenController.java b/src/main/java/com/iemr/flw/controller/AbhaTokenController.java new file mode 100644 index 00000000..5197ddf2 --- /dev/null +++ b/src/main/java/com/iemr/flw/controller/AbhaTokenController.java @@ -0,0 +1,40 @@ +package com.iemr.flw.controller; + +import com.iemr.flw.service.AbhaTokenService; +import com.iemr.flw.utils.ApiResponse; +import io.swagger.v3.oas.annotations.Operation; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.HashMap; +import java.util.Map; + +@RestController +@RequestMapping(value = "/abha", headers = "Authorization") +public class AbhaTokenController { + + private final Logger logger = LoggerFactory.getLogger(AbhaTokenController.class); + + @Autowired + private AbhaTokenService abhaTokenService; + + @Operation(summary = "Get ABHA access token") + @PostMapping("/token") + public ResponseEntity getAbhaToken( + @RequestHeader(value = "Authorization") String authorization) { + try { + Map tokenResponse = abhaTokenService.getAbhaToken(); + return new ResponseEntity<>( + new ApiResponse(true, null, tokenResponse), HttpStatus.OK); + } catch (Exception e) { + logger.error("Error fetching ABHA token: " + e.getMessage()); + return new ResponseEntity<>( + new ApiResponse(false, "Error fetching ABHA token: " + e.getMessage(), null), + HttpStatus.INTERNAL_SERVER_ERROR); + } + } +} diff --git a/src/main/java/com/iemr/flw/controller/AshaProfileController.java b/src/main/java/com/iemr/flw/controller/AshaProfileController.java index 1bda4499..2d02ce23 100644 --- a/src/main/java/com/iemr/flw/controller/AshaProfileController.java +++ b/src/main/java/com/iemr/flw/controller/AshaProfileController.java @@ -1,35 +1,21 @@ package com.iemr.flw.controller; import com.iemr.flw.domain.iemr.AshaWorker; -import com.iemr.flw.domain.iemr.M_User; -import com.iemr.flw.dto.iemr.UserServiceRoleDTO; -import com.iemr.flw.repo.iemr.UserServiceRoleRepo; import com.iemr.flw.service.AshaProfileService; import com.iemr.flw.service.EmployeeMasterInter; -import com.iemr.flw.service.UserService; import io.lettuce.core.dynamic.annotation.Param; import com.iemr.flw.utils.JwtUtil; -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.http.HttpStatus; -import com.iemr.flw.utils.JwtAuthenticationUtil; -import com.iemr.flw.utils.JwtUtil; -import com.iemr.flw.utils.exception.IEMRException; -import io.jsonwebtoken.Claims; -import io.swagger.v3.oas.annotations.Operation; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.util.HashMap; import java.util.Map; -import java.util.Objects; @RestController @RequestMapping(value = "/asha", produces = "application/json") @@ -76,18 +62,18 @@ public ResponseEntity> getProfile(HttpServletRequest request try { String jwtFromHeader = request.getHeader("JwtToken"); - // Validate JWT header presence -// if (jwtFromHeader == null || jwtFromHeader.trim().isEmpty()) { -// response.put("statusCode", 401); -// response.put("status", "Unauthorized"); -// response.put("errorMessage", "JWT token is missing"); -// return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(response); -// } +// Validate JWT header presence + if (jwtFromHeader == null || jwtFromHeader.trim().isEmpty()) { + response.put("statusCode", 401); + response.put("status", "Unauthorized"); + response.put("errorMessage", "JWT token is missing"); + return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(response); + } // Extract and validate user ID from JWT - // int userId = jwtUtil.extractUserId(jwtFromHeader); // Make sure this returns 0 or throws for invalid token + int userId = jwtUtil.extractUserId(jwtFromHeader); // Make sure this returns 0 or throws for invalid token - if (employeeId == 0) { + if (userId == 0) { response.put("statusCode", 401); response.put("status", "Unauthorized"); response.put("errorMessage", "Invalid JWT token"); @@ -95,7 +81,7 @@ public ResponseEntity> getProfile(HttpServletRequest request } // Business logic - AshaWorker ashaWorker = ashaProfileService.getProfileData(employeeId); + AshaWorker ashaWorker = ashaProfileService.getProfileData(userId); logger.info("Asha Profile"+ashaWorker); response.put("statusCode", 200); diff --git a/src/main/java/com/iemr/flw/controller/AshaSupervisorIncentiveApproval.java b/src/main/java/com/iemr/flw/controller/AshaSupervisorIncentiveApproval.java new file mode 100644 index 00000000..3ba457a6 --- /dev/null +++ b/src/main/java/com/iemr/flw/controller/AshaSupervisorIncentiveApproval.java @@ -0,0 +1,125 @@ +package com.iemr.flw.controller; + +import com.iemr.flw.dto.iemr.AshaByFacilityRequestDTO; +import com.iemr.flw.dto.iemr.UpdateApprovalRequestDTO; +import com.iemr.flw.service.SupervisorDashboardService; +import com.iemr.flw.utils.JwtUtil; +import com.iemr.flw.utils.response.OutputResponse; +import io.swagger.v3.oas.annotations.Operation; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + + +@RestController +@RequestMapping("/ashaSupervisor") +public class AshaSupervisorIncentiveApproval { + + @Autowired + private SupervisorDashboardService supervisorDashboardService; + + @Autowired + private JwtUtil jwtUtil; + + private final Logger logger = LoggerFactory.getLogger(this.getClass().getName()); + + + @PostMapping("/getAshaListByFacility") + public ResponseEntity getAshaListByFacility( + @RequestBody AshaByFacilityRequestDTO request, + @RequestHeader(value = "JwtToken") String token) { + + try { + logger.info("getAshaListByFacility request{}",request.getFacilityId()); + + if (token == null) { + return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("UNAUTHORIZED"); + } + + Map result = supervisorDashboardService.getAshasAtFacility( + jwtUtil.extractUserId(token), + request.getFacilityId(), + request.getMonth(), + request.getYear(), + request.getApprovalStatus() + ); + + logger.info("result {}", result); + + return ResponseEntity.status(HttpStatus.OK).body(result); + + } catch (Exception e) { + + logger.error("Error in getAshaListByFacility", e); + + return ResponseEntity + .status(HttpStatus.INTERNAL_SERVER_ERROR) + .body("Server error: " + e.getMessage()); + } + } + + + + + @Operation(summary = "Get comprehensive ASHA Supervisor dashboard with facilities, ASHAs, villages and incentive data") + @PostMapping("/dashboard") + public String getSupervisorDashboard(@RequestBody AshaByFacilityRequestDTO request,@RequestHeader(value = "JwtToken") String token ) { + OutputResponse response = new OutputResponse(); + logger.info("dashboard request{}",request.getFacilityId()); + + try { + if(token!=null){ + String result = supervisorDashboardService.getSupervisorDashboard(jwtUtil.extractUserId(token),request.getMonth(),request.getYear()); + response.setResponse(result); + } + + } catch (Exception e) { + logger.error("getSupervisorDashboard failed: " + e.getMessage(), e); + response.setError(e); + } + return response.toString(); + } + + @PostMapping("/updateApprovalStatus") + public ResponseEntity updateApprovalStatus( + @RequestBody UpdateApprovalRequestDTO request, + @RequestHeader(value = "JwtToken") String token) { + + try { + if (token == null) { + return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("UNAUTHORIZED"); + } + + int updatedRows = supervisorDashboardService.updateApprovalStatus( + request.getAshaId(), + request.getMonth(), + request.getYear(), + request.getApprovalStatus(), + request.getIncentiveIds(), + request.getReason(), + request.getOtherReason(), + token + ); + + Map response = new HashMap<>(); + response.put("statusCode", 200); + response.put("updatedRecords", updatedRows); + + return ResponseEntity.ok(response); + + } catch (Exception e) { + return ResponseEntity + .status(HttpStatus.INTERNAL_SERVER_ERROR) + .body("Server error: " + e.getMessage()); + } + } + + +} diff --git a/src/main/java/com/iemr/flw/controller/BeneficiaryController.java b/src/main/java/com/iemr/flw/controller/BeneficiaryController.java index dd083840..d439fbf5 100644 --- a/src/main/java/com/iemr/flw/controller/BeneficiaryController.java +++ b/src/main/java/com/iemr/flw/controller/BeneficiaryController.java @@ -40,10 +40,13 @@ public class BeneficiaryController { @Autowired private MdaFormSubmissionService mdaFormSubmissionService; + @Autowired + private JwtUtil jwtUtil; + @RequestMapping(value = "/getBeneficiaryData", method = RequestMethod.POST) @Operation(summary = "get beneficiary data for given user ") public String getBeneficiaryDataByAsha(@RequestBody GetBenRequestHandler requestDTO, - @RequestHeader(value = "Authorization") String authorization) { + @RequestHeader(value = "Authorization") String authorization) { OutputResponse response = new OutputResponse(); try { @@ -65,8 +68,9 @@ public String getBeneficiaryDataByAsha(@RequestBody GetBenRequestHandler request return response.toString(); } + @RequestMapping(value = {"/eye_surgery/saveAll"}, method = RequestMethod.POST) - public ResponseEntity saveEyeSurgery(@RequestBody List eyeCheckupRequestDTOS) { + public ResponseEntity saveEyeSurgery(@RequestBody List eyeCheckupRequestDTOS, @RequestHeader(value = "JwtToken") String token) { Map response = new LinkedHashMap<>(); logger.info("Eye Checkup Save Request: {}", eyeCheckupRequestDTOS); @@ -77,7 +81,7 @@ public ResponseEntity saveEyeSurgery(@RequestBody List return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response); } - String responseObject = beneficiaryService.saveEyeCheckupVsit(eyeCheckupRequestDTOS); + String responseObject = beneficiaryService.saveEyeCheckupVsit(eyeCheckupRequestDTOS, token); if (responseObject != null) { response.put("statusCode", HttpStatus.OK.value()); @@ -91,47 +95,102 @@ public ResponseEntity saveEyeSurgery(@RequestBody List } catch (Exception e) { logger.error("Error saving eye checkup visit:", e); - response.put("statusCode", HttpStatus.INTERNAL_SERVER_ERROR.value()); + response.put("statusCode", 5000); response.put("errorMessage", e.getMessage()); - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); + return ResponseEntity.ok(response); } } - @RequestMapping(value = {"/eye_surgery/getAll"}, method = RequestMethod.POST) - public ResponseEntity getAllEyeSurgery(@RequestBody GetBenRequestHandler request) { + public ResponseEntity getAllEyeSurgery(@RequestBody GetBenRequestHandler request, @RequestHeader(value = "JwtToken") String token) { Map response = new LinkedHashMap<>(); logger.info("Eye Checkup Get Request: {}", request); try { - List responseObject = beneficiaryService.getEyeCheckUpVisit(request); + List responseObject = beneficiaryService.getEyeCheckUpVisit(request, token); if (responseObject != null && !responseObject.isEmpty()) { response.put("statusCode", HttpStatus.OK.value()); response.put("data", responseObject); return ResponseEntity.ok(response); } else { - response.put("statusCode", HttpStatus.NOT_FOUND.value()); + response.put("statusCode", 5000); response.put("message", "No records found"); - return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response); + return ResponseEntity.ok(response); } } catch (Exception e) { logger.error("Error fetching eye checkup visit:", e); - response.put("statusCode", HttpStatus.INTERNAL_SERVER_ERROR.value()); + response.put("statusCode", 5000); response.put("errorMessage", e.getMessage()); - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); + return ResponseEntity.ok(response); } } - @RequestMapping(value = "/ifa/saveAll",method = RequestMethod.POST) - public ResponseEntity saveFormData(@RequestBody List requests) { - String message = ifaFormSubmissionService.saveFormData(requests); - return ResponseEntity.ok(Map.of("success", true, "message", message)); + @RequestMapping(value = "/ifa/saveAll", method = RequestMethod.POST) + public ResponseEntity saveIfaFormData( + @RequestBody List requests, + @RequestHeader(value = "JwtToken") String token) { + + Map response = new LinkedHashMap<>(); + + logger.info("IFA Save Request: {}", requests); + + try { + + if (requests == null || requests.isEmpty()) { + + response.put("statusCode", HttpStatus.BAD_REQUEST.value()); + response.put("message", "Request body cannot be empty"); + + return ResponseEntity + .status(HttpStatus.BAD_REQUEST) + .body(response); + } + if (token != null) { + String message = + ifaFormSubmissionService.saveFormData(requests, jwtUtil.extractUserId(token)); + + if (message != null) { + + response.put("statusCode", HttpStatus.OK.value()); + response.put("message", message); + + return ResponseEntity.ok(response); + + } else { + + response.put("statusCode", HttpStatus.BAD_REQUEST.value()); + response.put("message", "Failed to save records"); + + return ResponseEntity + .status(HttpStatus.BAD_REQUEST) + .body(response); + } + } else { + response.put("statusCode", HttpStatus.UNAUTHORIZED.value()); + response.put("message", "UNAUTHORIZED"); + + return ResponseEntity + .status(HttpStatus.UNAUTHORIZED) + .body(response); + } + + + } catch (Exception e) { + + logger.error("Error saving IFA records:", e); + + response.put("statusCode", 5000); + response.put("errorMessage", e.getMessage()); + + return ResponseEntity.ok(response); + + } } - @RequestMapping(value = "ifa/getAll",method = RequestMethod.POST) + @RequestMapping(value = "ifa/getAll", method = RequestMethod.POST) public ResponseEntity getFormData(@RequestBody GetBenRequestHandler getBenRequestHandler) { var data = ifaFormSubmissionService.getFormData(getBenRequestHandler); return ResponseEntity.ok(Map.of("success", true, "message", "Data fetched successfully", "data", data)); @@ -139,15 +198,15 @@ public ResponseEntity getFormData(@RequestBody GetBenRequestHandler getBenReq @RequestMapping(value = "/mda/saveAll", method = RequestMethod.POST) public ResponseEntity saveFormData(@RequestBody List requests, - @RequestHeader(value = "Authorization") String authorization) { + @RequestHeader(value = "Authorization") String authorization) { String message = mdaFormSubmissionService.saveFormData(requests); return ResponseEntity.ok(Map.of("success", true, "message", message)); } @RequestMapping(value = "/mda/getAll", method = RequestMethod.POST) public ResponseEntity getFormDataByCreatedBy(@RequestBody Map request, - @RequestHeader(value = "Authorization") String authorization) { - String userName = request.get("userName") ; + @RequestHeader(value = "Authorization") String authorization) { + String userName = request.get("userName"); var data = mdaFormSubmissionService.getFormDataByUserName(userName); return ResponseEntity.ok(Map.of("success", true, "message", "Data fetched successfully", "data", data)); } diff --git a/src/main/java/com/iemr/flw/controller/CampaignController.java b/src/main/java/com/iemr/flw/controller/CampaignController.java new file mode 100644 index 00000000..f5f13d69 --- /dev/null +++ b/src/main/java/com/iemr/flw/controller/CampaignController.java @@ -0,0 +1,360 @@ +package com.iemr.flw.controller; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import com.iemr.flw.domain.iemr.CampaignOrs; +import com.iemr.flw.domain.iemr.FilariasisCampaign; +import com.iemr.flw.domain.iemr.PulsePolioCampaign; +import com.iemr.flw.dto.iemr.*; +import com.iemr.flw.service.CampaignService; +import com.iemr.flw.utils.exception.IEMRException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; + +import java.sql.Timestamp; +import java.util.*; + +@RestController +@RequestMapping(value = "/campaign") +public class CampaignController { + private final Logger logger = LoggerFactory.getLogger(CampaignController.class); + + @Autowired + private CampaignService campaignService; + + @PostMapping("/ors/distribution/saveAll") + public ResponseEntity> saveOrsDistribution( + @RequestPart("formDataJson") String fields, + @RequestHeader("jwtToken") String token) { + + Map response = new LinkedHashMap<>(); + + try { + + // Validate input + if (fields == null || fields.trim().isEmpty()) { + response.put("statusCode", HttpStatus.BAD_REQUEST.value()); + response.put("message", "Form data is required"); + return ResponseEntity.badRequest().body(response); + } + + // Parse JSON to DTO + ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.registerModule(new JavaTimeModule()); + objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + + OrsCampaignDTO requestDTO = objectMapper.readValue(fields, OrsCampaignDTO.class); + + // Validate fields + if (requestDTO.getFields() == null) { + response.put("statusCode", HttpStatus.BAD_REQUEST.value()); + response.put("message", "Campaign fields are required"); + return ResponseEntity.badRequest().body(response); + } + + logger.info("Parsed DTO - Start Date: {}, End Date: {}, Families: {}, Photos: {}", + requestDTO.getFields().getStartDate(), + requestDTO.getFields().getEndDate(), + requestDTO.getFields().getNumberOfFamilies(), + requestDTO.getFields().getCampaignPhotos() + ); + + // Create DTO + OrsCampaignDTO campaignDTO = new OrsCampaignDTO(); + campaignDTO.setFields(requestDTO.getFields()); + + List orsCampaignDTOList = Collections.singletonList(campaignDTO); + + // Save to database + List result = campaignService.saveOrsCampaign(orsCampaignDTOList, token); + + if (result != null && !result.isEmpty()) { + response.put("statusCode", HttpStatus.OK.value()); + response.put("message", "Campaign saved successfully"); + response.put("data", result); + return ResponseEntity.status(HttpStatus.OK).body(response); + } else { + response.put("statusCode", HttpStatus.BAD_REQUEST.value()); + response.put("message", "Failed to save campaign"); + return ResponseEntity.badRequest().body(response); + } + + } catch (JsonProcessingException e) { + logger.error("JSON parsing error: {}", e.getMessage(), e); + response.put("statusCode", HttpStatus.BAD_REQUEST.value()); + response.put("message", "Invalid JSON format"); + response.put("errorMessage", e.getMessage()); + return ResponseEntity.badRequest().body(response); + + } catch (IEMRException e) { + logger.error("Business logic error: {}", e.getMessage(), e); + response.put("statusCode", HttpStatus.BAD_REQUEST.value()); + response.put("message", e.getMessage()); + return ResponseEntity.badRequest().body(response); + + } catch (Exception e) { + logger.error("Error saving ORS distribution: {}", e.getMessage(), e); + response.put("statusCode", HttpStatus.INTERNAL_SERVER_ERROR.value()); + response.put("message", "Internal server error occurred"); + response.put("errorMessage", e.getMessage()); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); + } + } + + + @RequestMapping(value = "ors/distribution/getAll", method = RequestMethod.POST) + public ResponseEntity> getAllOrsDistribution(@RequestHeader(value = "jwtToken") String token) { + + Map response = new LinkedHashMap<>(); + + try { + List result = campaignService.getOrsCampaign(token); + + + if (result != null) { + response.put("statusCode", HttpStatus.OK.value()); + response.put("data", result); + return ResponseEntity.ok(response); + } else { + response.put("statusCode", HttpStatus.INTERNAL_SERVER_ERROR.value()); + response.put("message", "No records found"); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); + } + + } catch (Exception e) { + logger.error("Error save ors distribution :", e.getMessage()); + response.put("statusCode", HttpStatus.INTERNAL_SERVER_ERROR.value()); + response.put("errorMessage", e.getMessage()); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); + } + } + + + @PostMapping("/polio/campaign/saveAll") + public ResponseEntity> savePolioCampaign( + @RequestPart("formDataJson") String fields, + @RequestHeader("jwtToken") String token) { + + Map response = new LinkedHashMap<>(); + + try { + logger.info("Received polio campaign data"); + + // Validate input + if (fields == null || fields.trim().isEmpty()) { + response.put("statusCode", HttpStatus.BAD_REQUEST.value()); + response.put("message", "Form data is required"); + return ResponseEntity.badRequest().body(response); + } + + // Parse JSON to DTO + ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.registerModule(new JavaTimeModule()); + objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + + PolioCampaignDTO requestDTO = objectMapper.readValue(fields, PolioCampaignDTO.class); + + // Validate fields + if (requestDTO.getFields() == null) { + response.put("statusCode", HttpStatus.BAD_REQUEST.value()); + response.put("message", "Campaign fields are required"); + return ResponseEntity.badRequest().body(response); + } + + logger.info("Parsed DTO - Start Date: {}, End Date: {}, Families: {}, Individuals: {}, Photos: {}", + requestDTO.getFields().getStartDate(), + requestDTO.getFields().getEndDate(), + requestDTO.getFields().getNumberOfChildren(), + + requestDTO.getFields().getCampaignPhotos() + ); + + // Create DTO + PolioCampaignDTO campaignDTO = new PolioCampaignDTO(); + campaignDTO.setFields(requestDTO.getFields()); + + List polioCampaignDTOList = Collections.singletonList(campaignDTO); + + // Save to database + List result = campaignService.savePolioCampaign(polioCampaignDTOList, token); + + if (result != null && !result.isEmpty()) { + response.put("statusCode", HttpStatus.OK.value()); + response.put("message", "polio campaign saved successfully"); + response.put("data", result); + return ResponseEntity.status(HttpStatus.OK).body(response); + } else { + response.put("statusCode", HttpStatus.BAD_REQUEST.value()); + response.put("message", "Failed to save Polio campaign"); + return ResponseEntity.badRequest().body(response); + } + + } catch (JsonProcessingException e) { + logger.error("JSON parsing error: {}", e.getMessage(), e); + response.put("statusCode", HttpStatus.BAD_REQUEST.value()); + response.put("message", "Invalid JSON format"); + response.put("errorMessage", e.getMessage()); + return ResponseEntity.badRequest().body(response); + + } catch (IEMRException e) { + logger.error("Business logic error: {}", e.getMessage(), e); + response.put("statusCode", HttpStatus.BAD_REQUEST.value()); + response.put("message", e.getMessage()); + return ResponseEntity.badRequest().body(response); + + } catch (Exception e) { + logger.error("Error saving filariasis campaign: {}", e.getMessage(), e); + response.put("statusCode", HttpStatus.INTERNAL_SERVER_ERROR.value()); + response.put("message", "Internal server error occurred"); + response.put("errorMessage", e.getMessage()); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); + } + } + + + @RequestMapping(value = "polio/campaign/getAll", method = RequestMethod.POST) + public ResponseEntity> getAllPolioCampaign(@RequestHeader(value = "jwtToken") String token) { + + Map response = new LinkedHashMap<>(); + + try { + List result = campaignService.getPolioCampaign(token); + + + if (result != null) { + response.put("statusCode", HttpStatus.OK.value()); + response.put("data", result); + return ResponseEntity.ok(response); + } else { + response.put("statusCode", HttpStatus.INTERNAL_SERVER_ERROR.value()); + response.put("message", "No records found"); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); + } + + } catch (Exception e) { + logger.error("Error save ors distribution :", e.getMessage()); + response.put("statusCode", HttpStatus.INTERNAL_SERVER_ERROR.value()); + response.put("errorMessage", e.getMessage()); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); + } + } + + + @PostMapping("/filariasis/campaign/saveAll") + public ResponseEntity> saveFilariasisCampaign( + @RequestPart("formDataJson") String fields, + @RequestHeader("jwtToken") String token) { + + Map response = new LinkedHashMap<>(); + + try { + logger.info("Received polio campaign data"); + + // Validate input + if (fields == null || fields.trim().isEmpty()) { + response.put("statusCode", HttpStatus.BAD_REQUEST.value()); + response.put("message", "Form data is required"); + return ResponseEntity.badRequest().body(response); + } + + // Parse JSON to DTO + ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.registerModule(new JavaTimeModule()); + objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + + FilariasisCampaignDTO requestDTO = objectMapper.readValue(fields, FilariasisCampaignDTO.class); + + // Validate fields + if (requestDTO.getFields() == null) { + response.put("statusCode", HttpStatus.BAD_REQUEST.value()); + response.put("message", "Campaign fields are required"); + return ResponseEntity.badRequest().body(response); + } + + logger.info("Parsed DTO - Start Date: {}, End Date: {}, Families: {}, Individuals: {}, Photos: {}", + requestDTO.getFields().getStartDate(), + requestDTO.getFields().getEndDate(), + requestDTO.getFields().getNumberOfIndividuals(), + requestDTO.getFields().getNumberOfIndividuals(), + + requestDTO.getFields().getMdaPhotos() + ); + + // Create DTO + FilariasisCampaignDTO campaignDTO = new FilariasisCampaignDTO(); + campaignDTO.setFields(requestDTO.getFields()); + + List filariasisCampaignDTOS = Collections.singletonList(campaignDTO); + + + // Save to database + List result = campaignService.saveFilariasisCampaign(filariasisCampaignDTOS, token); + + if (result != null && !result.isEmpty()) { + response.put("statusCode", HttpStatus.OK.value()); + response.put("message", "filariasis campaign saved successfully"); + response.put("data", result); + return ResponseEntity.status(HttpStatus.OK).body(response); + } else { + response.put("statusCode", HttpStatus.BAD_REQUEST.value()); + response.put("message", "Failed to save filariasis campaign"); + return ResponseEntity.badRequest().body(response); + } + + } catch (JsonProcessingException e) { + logger.error("JSON parsing error: {}", e.getMessage(), e); + response.put("statusCode", HttpStatus.BAD_REQUEST.value()); + response.put("message", "Invalid JSON format"); + response.put("errorMessage", e.getMessage()); + return ResponseEntity.badRequest().body(response); + + } catch (IEMRException e) { + logger.error("Business logic error: {}", e.getMessage(), e); + response.put("statusCode", HttpStatus.BAD_REQUEST.value()); + response.put("message", e.getMessage()); + return ResponseEntity.badRequest().body(response); + + } catch (Exception e) { + logger.error("Error saving filariasis campaign: {}", e.getMessage(), e); + response.put("statusCode", HttpStatus.INTERNAL_SERVER_ERROR.value()); + response.put("message", "Internal server error occurred"); + response.put("errorMessage", e.getMessage()); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); + } + } + + + @RequestMapping(value = "filariasis/campaign/getAll", method = RequestMethod.POST) + public ResponseEntity> getAllFilariasisCampaign(@RequestHeader(value = "jwtToken") String token) { + + Map response = new LinkedHashMap<>(); + + try { + List result = campaignService.getAllFilariasisCampaign(token); + + + if (result != null) { + response.put("statusCode", HttpStatus.OK.value()); + response.put("data", result); + return ResponseEntity.ok(response); + } else { + response.put("statusCode", HttpStatus.INTERNAL_SERVER_ERROR.value()); + response.put("message", "No records found"); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); + } + + } catch (Exception e) { + logger.error("Error save ors distribution :", e.getMessage()); + response.put("statusCode", HttpStatus.INTERNAL_SERVER_ERROR.value()); + response.put("errorMessage", e.getMessage()); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); + } + } +} diff --git a/src/main/java/com/iemr/flw/controller/ChildCareController.java b/src/main/java/com/iemr/flw/controller/ChildCareController.java index d5e4335f..bbc9cba2 100644 --- a/src/main/java/com/iemr/flw/controller/ChildCareController.java +++ b/src/main/java/com/iemr/flw/controller/ChildCareController.java @@ -7,14 +7,19 @@ import com.iemr.flw.domain.iemr.HbncVisit; import com.iemr.flw.domain.iemr.IfaDistribution; import com.iemr.flw.domain.iemr.SamVisitResponseDTO; +import com.iemr.flw.domain.iemr.UserServiceRole; import com.iemr.flw.dto.identity.GetBenRequestHandler; import com.iemr.flw.dto.iemr.*; +import com.iemr.flw.repo.iemr.UserServiceRoleRepo; import com.iemr.flw.service.ChildCareService; +import com.iemr.flw.service.UserService; +import com.iemr.flw.utils.JwtUtil; import com.iemr.flw.utils.response.OutputResponse; import io.swagger.v3.oas.annotations.Operation; import jakarta.mail.Multipart; +import org.checkerframework.checker.units.qual.A; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -37,10 +42,18 @@ public class ChildCareController { @Autowired private ChildCareService childCareService; + @Autowired + private UserService userService; + + @Autowired + private JwtUtil jwtUtil; + + @Autowired + private UserServiceRoleRepo userServiceRoleRepo; @Operation(summary = "save HBYC details") @RequestMapping(value = {"/hbycVisit/saveAll"}, method = {RequestMethod.POST}) public String saveHbycRecords(@RequestBody List hbycDTOs, - @RequestHeader(value = "Authorization") String Authorization) { + @RequestHeader(value = "JwtToken") String token) { ObjectMapper mapper = new ObjectMapper(); mapper.enable(SerializationFeature.INDENT_OUTPUT); // Pretty print @@ -92,25 +105,27 @@ public String getHbycRecords(@RequestBody GetBenRequestHandler requestDTO) { @PostMapping("/hbncVisit/saveAll") public String saveHBNCVisit(@RequestBody List hbncRequestDTOs, - @RequestHeader(value = "Authorization") String Authorization) { + @RequestHeader(value = "jwtToken") String jwtToken) { OutputResponse response = new OutputResponse(); try { if (!hbncRequestDTOs.isEmpty()) { logger.info("Saving HBNC details at: " + new Timestamp(System.currentTimeMillis())); + if(jwtToken!=null){ + String result = childCareService.saveHBNCDetails(hbncRequestDTOs,jwtUtil.extractUserId(jwtToken)); // <-- actual save - String result = childCareService.saveHBNCDetails(hbncRequestDTOs); // <-- actual save + if (result != null) + response.setResponse(result); + else + response.setError(500, "Failed to save HBNC visit data."); + } - if (result != null) - response.setResponse(result); - else - response.setError(500, "Failed to save HBNC visit data."); } else { response.setError(400, "Empty request list."); } } catch (Exception e) { logger.error("Error saving HBNC visit: ", e); - response.setError(500, "Server error: " + e.getMessage()); + response.setError(5000, "Server error: " + e.getMessage()); } return response.toString(); } @@ -146,7 +161,7 @@ public ResponseEntity>> getHBNCVisit } catch (Exception e) { logger.error("Exception in fetching HBNC visits", e); - response.setStatusCode(500); + response.setStatusCode(5000); response.setStatus("Failed"); response.setErrorMessage("Internal Server Error: " + e.getMessage()); response.setData(null); @@ -228,7 +243,7 @@ public String getChildVaccinationDetails(@RequestParam(value = "category") Strin return response.toString(); } @RequestMapping(value = {"/sam/saveAll"}, method = RequestMethod.POST) - public ResponseEntity saveSevereAcuteMalnutrition(@RequestBody List samRequest) { + public ResponseEntity saveSevereAcuteMalnutrition(@RequestBody List samRequest,@RequestHeader(value = "JwtToken") String token) { Map response = new LinkedHashMap<>(); logger.info("SAM Request: {}", samRequest); @@ -240,21 +255,30 @@ public ResponseEntity saveSevereAcuteMalnutrition(@RequestBody List s } - String responseObject = childCareService.saveSamDetails(samRequest); - - if (responseObject != null) { - response.put("statusCode", HttpStatus.OK.value()); - response.put("message", responseObject); - return ResponseEntity.ok(response); - } else { - response.put("statusCode", HttpStatus.BAD_REQUEST.value()); - response.put("message", "Failed to save SAM details"); - return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response); + if(token!=null){ + Integer userId = jwtUtil.extractUserId(token); + String userName = userService.getUserDetail(userId).getUserName(); + String responseObject = childCareService.saveSamDetails(samRequest,userId,userName); + + if (responseObject != null) { + response.put("statusCode", HttpStatus.OK.value()); + response.put("message", responseObject); + return ResponseEntity.ok(response); + } else { + response.put("statusCode", HttpStatus.BAD_REQUEST.value()); + response.put("message", "Failed to save SAM details"); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response); + } + }else { + response.put("statusCode", HttpStatus.UNAUTHORIZED.value()); + response.put("message", "Invalid token"); + return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(response); } + } catch (Exception e) { logger.error("Error saving SAM details:", e); - response.put("statusCode", HttpStatus.INTERNAL_SERVER_ERROR.value()); + response.put("statusCode",5000); response.put("errorMessage", e.getMessage()); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); } @@ -272,14 +296,14 @@ public ResponseEntity getAllSevereAcuteMalnutrition(@RequestBody GetBenReques response.put("data", responseObject); return ResponseEntity.ok(response); } else { - response.put("statusCode", HttpStatus.NOT_FOUND.value()); + response.put("statusCode", HttpStatus.OK.value()); response.put("message", "No SAM records found"); - return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response); + return ResponseEntity.status(HttpStatus.NO_CONTENT).body(response); } } catch (Exception e) { logger.error("Error fetching SAM records:", e); - response.put("statusCode", HttpStatus.INTERNAL_SERVER_ERROR.value()); + response.put("statusCode", 5000); response.put("errorMessage", e.getMessage()); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); } @@ -287,7 +311,7 @@ public ResponseEntity getAllSevereAcuteMalnutrition(@RequestBody GetBenReques @RequestMapping(value = {"/ors/saveAll"}, method = RequestMethod.POST) - public ResponseEntity saveOrsDistribution(@RequestBody List orsDistributionDTOS) { + public ResponseEntity saveOrsDistribution(@RequestBody List orsDistributionDTOS,@RequestHeader(value = "JwtToken") String token) { Map response = new LinkedHashMap<>(); logger.info("ORS Request: {}", orsDistributionDTOS); @@ -297,22 +321,29 @@ public ResponseEntity saveOrsDistribution(@RequestBody List getAllOrDistribution(@RequestBody GetBenRequestHandler response.put("data", responseObject); return ResponseEntity.ok(response); } else { - response.put("statusCode", HttpStatus.NOT_FOUND.value()); + response.put("statusCode", 5000); response.put("message", "No ORS records found"); - return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); } } catch (Exception e) { logger.error("Error fetching ORS records:", e); - response.put("statusCode", HttpStatus.INTERNAL_SERVER_ERROR.value()); + response.put("statusCode", 5000); response.put("errorMessage", e.getMessage()); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); } @@ -346,7 +377,7 @@ public ResponseEntity getAllOrDistribution(@RequestBody GetBenRequestHandler @RequestMapping(value = {"/ifa/saveAll"}, method = RequestMethod.POST) - public ResponseEntity saveIfDistribution(@RequestBody List ifaDistributionDTOS) { + public ResponseEntity saveIfDistribution(@RequestBody List ifaDistributionDTOS,@RequestHeader(value = "JwtToken") String token) { Map response = new LinkedHashMap<>(); logger.info("IFA Request: {}", ifaDistributionDTOS); @@ -356,22 +387,29 @@ public ResponseEntity saveIfDistribution(@RequestBody List responseObject = childCareService.saveAllIfa(ifaDistributionDTOS,userId); + + if (responseObject != null && !responseObject.isEmpty()) { + response.put("statusCode", HttpStatus.OK.value()); + response.put("message", "IFA saved successfully"); + return ResponseEntity.ok(response); + } else { + response.put("statusCode", HttpStatus.BAD_REQUEST.value()); + response.put("message", "Failed to save IFA details"); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response); + } + }else { + response.put("statusCode", HttpStatus.UNAUTHORIZED.value()); + response.put("message", "Invalid token"); + return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(response); + } - List responseObject = childCareService.saveAllIfa(ifaDistributionDTOS); - - if (responseObject != null && !responseObject.isEmpty()) { - response.put("statusCode", HttpStatus.OK.value()); - response.put("message", "IFA saved successfully"); - return ResponseEntity.ok(response); - } else { - response.put("statusCode", HttpStatus.BAD_REQUEST.value()); - response.put("message", "Failed to save IFA details"); - return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response); - } } catch (Exception e) { logger.error("Error saving IFA:", e); - response.put("statusCode", HttpStatus.INTERNAL_SERVER_ERROR.value()); + response.put("statusCode", 5000); response.put("errorMessage", e.getMessage()); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); } @@ -397,7 +435,7 @@ public ResponseEntity getIfaDistribution(@RequestBody GetBenRequestHandler re } catch (Exception e) { logger.error("Error fetching IFA records:", e); - response.put("statusCode", HttpStatus.INTERNAL_SERVER_ERROR.value()); + response.put("statusCode", 5000); response.put("errorMessage", e.getMessage()); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); } diff --git a/src/main/java/com/iemr/flw/controller/DeathReportsController.java b/src/main/java/com/iemr/flw/controller/DeathReportsController.java index f5317a8b..e8b50de9 100644 --- a/src/main/java/com/iemr/flw/controller/DeathReportsController.java +++ b/src/main/java/com/iemr/flw/controller/DeathReportsController.java @@ -84,8 +84,8 @@ public String getCdrRecords(@RequestBody GetBenRequestHandler requestDTO, logger.info("fetching All CDR Details for user: " + requestDTO.getAshaId()); if (requestDTO != null) { List result = deathReportsService.getCdrRecords(requestDTO); - Gson gson = new GsonBuilder() - .setDateFormat("MMM dd, yyyy h:mm:ss a") // Set the desired date format + Gson gson = new GsonBuilder()// Set the desired date format + .setDateFormat("MMM dd, yyyy h:mm:ss a") .create(); String s = gson.toJson(result); if (s != null) @@ -111,8 +111,8 @@ public String getMdsrRecords(@RequestBody GetBenRequestHandler requestDTO, logger.info("fetching All MDSR Details for user: " + requestDTO.getAshaId()); if (requestDTO != null) { List result = deathReportsService.getMdsrRecords(requestDTO); - Gson gson = new GsonBuilder() - .setDateFormat("MMM dd, yyyy h:mm:ss a") // Set the desired date format + Gson gson = new GsonBuilder()// Set the desired date format + .setDateFormat("MMM dd, yyyy h:mm:ss a") .create(); String s = gson.toJson(result); if (s != null) diff --git a/src/main/java/com/iemr/flw/controller/DiseaseControlController.java b/src/main/java/com/iemr/flw/controller/DiseaseControlController.java index 2f5d4264..764ebeaf 100644 --- a/src/main/java/com/iemr/flw/controller/DiseaseControlController.java +++ b/src/main/java/com/iemr/flw/controller/DiseaseControlController.java @@ -34,11 +34,7 @@ import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatusCode; import org.springframework.http.ResponseEntity; -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.RequestMethod; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import java.util.HashMap; import java.util.LinkedHashMap; @@ -67,12 +63,12 @@ public ResponseEntity> saveMalaria(@RequestBody MalariaDTO m response.put("data", diseaseControlService.saveMalaria(malariaDTO)); }else { response.put("message", "Invalid request"); - response.put("statusCode", 400); + response.put("statusCode", 5000); } } catch (Exception e) { response.put("status", "Error" + e.getMessage()); - response.put("statusCode", 500); + response.put("statusCode", 5000); } return ResponseEntity.ok(response); @@ -90,12 +86,12 @@ public ResponseEntity> saveKalaAzar(@RequestBody KalaAzarDTO response.put("data", diseaseControlService.saveKalaAzar(kalaAzarDTO)); }else { response.put("message", "Invalid request"); - response.put("statusCode", 400); + response.put("statusCode", 5000); } } catch (Exception e) { response.put("status", "Error" + e.getMessage()); - response.put("statusCode", 500); + response.put("statusCode", 5000); } return ResponseEntity.ok(response); @@ -112,12 +108,12 @@ public ResponseEntity> saveAESJE(@RequestBody AesJeDTO aesJe response.put("data", diseaseControlService.saveAES(aesJeDTO)); }else { response.put("message", "Invalid request"); - response.put("statusCode", 400); + response.put("statusCode", 5000); } } catch (Exception e) { response.put("status", "Error" + e.getMessage()); - response.put("statusCode", 500); + response.put("statusCode", 5000); } return ResponseEntity.ok(response); @@ -134,12 +130,12 @@ public ResponseEntity> saveFilaria(@RequestBody FilariaDTO f response.put("data", diseaseControlService.saveFilaria(filariaDTO)); }else { response.put("message", "Invalid request"); - response.put("statusCode", 400); + response.put("statusCode", 5000); } } catch (Exception e) { response.put("status", "Error" + e.getMessage()); - response.put("statusCode", 500); + response.put("statusCode", 5000); } return ResponseEntity.ok(response); @@ -155,12 +151,12 @@ public ResponseEntity> saveLeprosy(@RequestBody LeprosyDTO l response.put("data", diseaseControlService.saveLeprosy(leprosyDTO)); }else { response.put("message", "Invalid request"); - response.put("statusCode", 400); + response.put("statusCode", 5000); } } catch (Exception e) { response.put("status", "Error" + e.getMessage()); - response.put("statusCode", 500); + response.put("statusCode", 5000); } return ResponseEntity.ok(response); @@ -179,11 +175,11 @@ public ResponseEntity> saveLeprosyFollowUP(@RequestBody List response.put("message", "All follow-ups saved successfully"); } else { response.put("message", "Invalid request - no follow-up data"); - response.put("statusCode", 400); + response.put("statusCode", 5000); } } catch (Exception e) { response.put("status", "Error: " + e.getMessage()); - response.put("statusCode", 500); + response.put("statusCode", 5000); } return ResponseEntity.ok(response); @@ -207,15 +203,17 @@ public ResponseEntity> getAllLeprosy( response.put("data", result); // ← Put list directly, no gson.toJson() } else { response.put("message", "No record found"); - response.put("statusCode", 404); + response.put("statusCode", 5000); } } else { response.put("message", "Invalid request - userName required"); - response.put("statusCode", 400); + response.put("statusCode", 5000); } } catch (Exception e) { + logger.info("Fail leprosy: "+e.getMessage()); + logger.info("Fail leprosy full error: "+e); response.put("status", "Error: " + e.getMessage()); - response.put("statusCode", 500); + response.put("statusCode", 5000); } return ResponseEntity.ok(response); // ← Spring serializes the whole map } @@ -237,15 +235,17 @@ public ResponseEntity> getAllLeprosyFollowUp( response.put("data", result); // ← Put list directly, no gson.toJson() } else { response.put("message", "No record found"); - response.put("statusCode", 404); + response.put("statusCode", 5000); } } else { response.put("message", "Invalid request - userName required"); - response.put("statusCode", 400); + response.put("statusCode", 5000); } } catch (Exception e) { + logger.info("Fail leprosy followUp: "+e.getMessage()); + logger.info("Fail leprosy full error: "+e); response.put("status", "Error: " + e.getMessage()); - response.put("statusCode", 500); + response.put("statusCode", 5000); } return ResponseEntity.ok(response); // ← Spring serializes the whole map } @@ -260,8 +260,10 @@ public ResponseEntity> getAllData( response.put("data", diseaseControlService.getAllScreeningData(getDiseaseRequestHandler)); } catch (Exception e) { + logger.info("getAllDisease "+e.getMessage()); + logger.info("Fail getAllDisease full error: "+e); response.put("status", "Error" + e.getMessage()); - response.put("statusCode", 500); + response.put("statusCode", 5000); } return ResponseEntity.ok(response); } @@ -314,5 +316,63 @@ public ResponseEntity> getAllMobilizationMosquitoNet(@Reques return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); } } + @RequestMapping(value = "cdtfVisit/saveAll", method = RequestMethod.POST) + public ResponseEntity> saveVisit( + @RequestBody List requestList,@RequestHeader(value = "JwtToken") String token) { + + Map response = new LinkedHashMap<>(); + logger.info("Chronic Disease Visit Save Request: {}", requestList); + + try { + List savedList = + diseaseControlService.saveChronicDiseaseVisit(requestList,token); + + if (savedList != null && !savedList.isEmpty()) { + response.put("statusCode", HttpStatus.OK.value()); + response.put("message", "Data saved successfully"); + response.put("data", savedList); + return ResponseEntity.ok(response); + } else { + response.put("statusCode", HttpStatus.BAD_REQUEST.value()); + response.put("message", "No data saved"); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response); + } + + } catch (Exception e) { + logger.error("Error saving Chronic Disease Visit :", e); + response.put("statusCode", HttpStatus.INTERNAL_SERVER_ERROR.value()); + response.put("errorMessage", e.getMessage()); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); + } + } + + @RequestMapping(value = "cdtfVisit/getAll", method = RequestMethod.POST) + public ResponseEntity> getVisitDetails( + @RequestBody GetBenRequestHandler getBenRequestHandler) { + + Map response = new LinkedHashMap<>(); + + try { + List result = + diseaseControlService.getCdtfVisits(getBenRequestHandler); + + if (result != null && !result.isEmpty()) { + response.put("statusCode", HttpStatus.OK.value()); + response.put("data", result); + return ResponseEntity.ok(response); + } else { + response.put("statusCode", HttpStatus.NOT_FOUND.value()); + response.put("message", "No records found"); + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response); + } + + } catch (Exception e) { + logger.error("Error fetching Chronic Disease Visit :", e); + response.put("statusCode", HttpStatus.INTERNAL_SERVER_ERROR.value()); + response.put("errorMessage", e.getMessage()); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); + } + } + } \ No newline at end of file diff --git a/src/main/java/com/iemr/flw/controller/GeneralOPDController.java b/src/main/java/com/iemr/flw/controller/GeneralOPDController.java index 5e7823df..c683a9be 100644 --- a/src/main/java/com/iemr/flw/controller/GeneralOPDController.java +++ b/src/main/java/com/iemr/flw/controller/GeneralOPDController.java @@ -77,7 +77,7 @@ public ResponseEntity> getBeneficiaryDataByAsha(@RequestBody } catch (Exception e) { logger.error("Error in get data : " + e); - response.put("status", 500); + response.put("statusCode", 5000); response.put("message", "Error in get data : " + e); } return ResponseEntity.ok(response); diff --git a/src/main/java/com/iemr/flw/controller/IRSRoundController.java b/src/main/java/com/iemr/flw/controller/IRSRoundController.java index 4425cdeb..77047f89 100644 --- a/src/main/java/com/iemr/flw/controller/IRSRoundController.java +++ b/src/main/java/com/iemr/flw/controller/IRSRoundController.java @@ -3,7 +3,10 @@ import com.iemr.flw.domain.iemr.IRSRound; import com.iemr.flw.dto.iemr.IRSRoundDTO; import com.iemr.flw.dto.iemr.IRSRoundListDTO; +import com.iemr.flw.repo.iemr.UserServiceRoleRepo; import com.iemr.flw.service.IRSRoundService; +import com.iemr.flw.service.UserService; +import com.iemr.flw.utils.JwtUtil; import com.iemr.flw.utils.response.OutputResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; @@ -24,12 +27,23 @@ public class IRSRoundController { @Autowired private IRSRoundService irsRoundService; + @Autowired + private JwtUtil jwtUtil; + + @Autowired + private UserService userService; + @PostMapping(value = "/add") - public ResponseEntity> addRound(@RequestBody IRSRoundListDTO dto) { + public ResponseEntity> addRound(@RequestBody IRSRoundListDTO dto,@RequestHeader("jwtToken") String token) { Map response = new LinkedHashMap<>(); try { if (dto.getRounds().size() != 0) { - List s = irsRoundService.addRounds(dto.getRounds()); + if(token==null){ + response.put("statusCode", 401); + return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(response) ; + } + Integer userId = jwtUtil.extractUserId(token); + List s = irsRoundService.addRounds(dto.getRounds(), userId, userService.getUserDetail(userId).getUserName()); if (s.size() != 0) { Map data = new HashMap<>(); data.put("entries", s); diff --git a/src/main/java/com/iemr/flw/controller/IncentiveController.java b/src/main/java/com/iemr/flw/controller/IncentiveController.java index 670d611b..5feead21 100644 --- a/src/main/java/com/iemr/flw/controller/IncentiveController.java +++ b/src/main/java/com/iemr/flw/controller/IncentiveController.java @@ -1,8 +1,7 @@ package com.iemr.flw.controller; import com.iemr.flw.dto.identity.GetBenRequestHandler; -import com.iemr.flw.dto.iemr.IncentiveActivityDTO; -import com.iemr.flw.dto.iemr.IncentiveRequestDTO; +import com.iemr.flw.dto.iemr.*; import com.iemr.flw.service.IncentiveService; import com.iemr.flw.utils.JwtUtil; import com.iemr.flw.utils.response.OutputResponse; @@ -13,10 +12,14 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.sql.Timestamp; +import java.util.HashMap; import java.util.List; +import java.util.Map; @RestController @RequestMapping(value = "/incentive", consumes = "application/json", produces = "application/json") @@ -28,9 +31,12 @@ public class IncentiveController { @Autowired IncentiveService incentiveService; + @Autowired + private JwtUtil jwtUtil; + @Operation(summary = "save incentive master") - @RequestMapping(value = { "/masterData/saveAll" }, method = { RequestMethod.POST }) - public String saveIncentiveMasterData(@RequestBody List activityDTOS,@RequestHeader(value = "Authorization") String authorization, HttpServletRequest request) { + @RequestMapping(value = {"/masterData/saveAll"}, method = {RequestMethod.POST}) + public String saveIncentiveMasterData(@RequestBody List activityDTOS, @RequestHeader(value = "Authorization") String authorization, HttpServletRequest request) { OutputResponse response = new OutputResponse(); try { logger.info("Saving All incentives"); @@ -50,9 +56,9 @@ public String saveIncentiveMasterData(@RequestBody List ac } @Operation(summary = "get incentive master") - @RequestMapping(value = { "/masterData/getAll" }, method = { RequestMethod.POST }) + @RequestMapping(value = {"/masterData/getAll"}, method = {RequestMethod.POST}) public String saveIncentiveMasterData(@RequestBody IncentiveRequestDTO incentiveRequestDTO, - @RequestHeader(value = "Authorization") String Authorization) { + @RequestHeader(value = "Authorization") String Authorization) { OutputResponse response = new OutputResponse(); try { @@ -61,7 +67,7 @@ public String saveIncentiveMasterData(@RequestBody IncentiveRequestDTO incentive // add logic for different state or district if (incentiveRequestDTO != null) { String s = incentiveService.getIncentiveMaster(incentiveRequestDTO); - logger.info("All incentives"+s); + // logger.info("All incentives" + s); if (s != null) response.setResponse(s); @@ -77,21 +83,45 @@ public String saveIncentiveMasterData(@RequestBody IncentiveRequestDTO incentive } + @Operation(summary = "get high risk assessment data of all beneficiaries registered with given user id") + @RequestMapping(value = {"/fetchUserData"}, method = {RequestMethod.POST}) + public String getAllIncentivesByUserId(@RequestBody GetBenRequestHandler requestDTO, + @RequestHeader(value = "Authorization") String Authorization) { + OutputResponse response = new OutputResponse(); + try { + if (requestDTO != null) { + logger.info("request object with timestamp : " + new Timestamp(System.currentTimeMillis()) + " " + + requestDTO); + String s = incentiveService.getAllIncentivesByUserId(requestDTO); + //logger.info("User Incentive:" + s); + if (s != null) + response.setResponse(s); + else + response.setError(5000, "No record found"); + } else + response.setError(5000, "Invalid/NULL request obj"); + } catch (Exception e) { + logger.error("Error in high risk assessment data : " + e); + response.setError(5000, "Error in high risk assessment data : " + e); + } + return response.toString(); + } @Operation(summary = "get high risk assessment data of all beneficiaries registered with given user id") - @RequestMapping(value = { "/fetchUserData" }, method = { RequestMethod.POST }) - public String getAllIncentivesByUserId(@RequestBody GetBenRequestHandler requestDTO, - @RequestHeader(value = "Authorization") String Authorization) { + @RequestMapping(value = {"/claimedIncentiveByUser"}, method = {RequestMethod.POST}) + public String getAllIncentivesByUserIdGroupByCount(@RequestBody GetBenRequestHandler requestDTO, + @RequestHeader(value = "jwtToken") String jwtToken) { OutputResponse response = new OutputResponse(); try { if (requestDTO != null) { + Integer userId= jwtUtil.extractUserId(jwtToken); logger.info("request object with timestamp : " + new Timestamp(System.currentTimeMillis()) + " " + requestDTO); - String s = incentiveService.getAllIncentivesByUserId(requestDTO); - logger.info("User Incentive:"+s); + String s = incentiveService.getAllIncentivesGroupedSummary(requestDTO,userId); + logger.info("User Incentive:" + s); if (s != null) response.setResponse(s); else @@ -105,4 +135,86 @@ public String getAllIncentivesByUserId(@RequestBody GetBenRequestHandler request return response.toString(); } + @Operation(summary = "get high risk assessment data of all beneficiaries registered with given user id") + @RequestMapping(value = {"/AllIncentiveByActivityId"}, method = {RequestMethod.POST}) + public String getAllIncentivesByActivityIdAndUser(@RequestBody GetBenRequestHandler requestDTO, + @RequestHeader(value = "Authorization") String Authorization) { + OutputResponse response = new OutputResponse(); + try { + + if (requestDTO != null) { + logger.info("request object with timestamp : " + new Timestamp(System.currentTimeMillis()) + " " + + requestDTO); + String s = incentiveService.getAllIncentivesGroupedActivity(requestDTO); + logger.info("User Incentive:" + s); + if (s != null) + response.setResponse(s); + else + response.setError(5000, "No record found"); + } else + response.setError(5000, "Invalid/NULL request obj"); + } catch (Exception e) { + logger.error("Error in high risk assessment data : " + e); + response.setError(5000, "Error in high risk assessment data : " + e); + } + return response.toString(); + } + + @RequestMapping(value = {"/update"}, method = RequestMethod.POST, consumes = {"multipart/form-data"}) + public String updateIncentive(@ModelAttribute PendingActivityDTO requestDTO) { + OutputResponse response = new OutputResponse(); + try { + + if (requestDTO != null) { + logger.info("request object with timestamp : " + new Timestamp(System.currentTimeMillis()) + " " + + requestDTO); + String s = incentiveService.updateIncentive(requestDTO); + logger.info("User Incentive:" + s); + if (s != null) + response.setResponse(s); + else + response.setError(500, "No record found"); + } else + response.setError(500, "Invalid/NULL request obj"); + } catch (Exception e) { + logger.error("Error in high risk assessment data : " + e); + response.setError(500, "Error in high risk assessment data : " + e); + } + return response.toString(); + + + } + + @PostMapping("/updateClaim") + public ResponseEntity updateApprovalStatus( + @RequestBody UpdateClaimedStatusRequestDTO request, + @RequestHeader(value = "JwtToken") String token) { + + try { + if (token == null) { + return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("UNAUTHORIZED"); + } + + String result = incentiveService.updateClaimStatus( + jwtUtil.extractUserId(token), + request.getMonth(), + request.getYear(), + request.isClaimed(), + token + ); + + Map response = new HashMap<>(); + response.put("statusCode", 200); + response.put("message", result); + + return ResponseEntity.ok(response); + + } catch (Exception e) { + return ResponseEntity + .status(HttpStatus.INTERNAL_SERVER_ERROR) + .body("Server error: " + e.getMessage()); + } + } + + } diff --git a/src/main/java/com/iemr/flw/controller/MaaMeetingController.java b/src/main/java/com/iemr/flw/controller/MaaMeetingController.java index d9bcdb1b..012438c5 100644 --- a/src/main/java/com/iemr/flw/controller/MaaMeetingController.java +++ b/src/main/java/com/iemr/flw/controller/MaaMeetingController.java @@ -35,22 +35,102 @@ public MaaMeetingController(MaaMeetingService service) { @PostMapping(value = "/saveAll", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public ResponseEntity saveMeeting( + + @RequestPart(value = "villageName", required = false) String villageName, + @RequestPart(value = "noOfPragnentWoment", required = false) String noOfPragnentWomen, + @RequestPart(value = "noOfLactingMother", required = false) String noOfLactingMother, + @RequestPart(value = "mitaninActivityCheckList", required = false) String mitaninActivityCheckList, + @RequestPart(value = "meetingDate") String meetingDate, + @RequestPart(value = "place", required = false) String place, + @RequestPart(value = "participants") String participants, + @RequestPart(value = "ashaId", required = false) String ashaId, + @RequestPart(value = "createdBy", required = false) String createdBy, + @RequestPart(value = "meetingImages", required = false) List meetingImages + ) { + try { + MaaMeetingRequestDTO dto = new MaaMeetingRequestDTO(); + + if (meetingDate != null && !meetingDate.isEmpty()) { + dto.setMeetingDate(LocalDate.parse(meetingDate)); + } + + dto.setPlace(place); + dto.setVillageName(villageName); + dto.setMitaninActivityCheckList(mitaninActivityCheckList); + dto.setCreatedBY(createdBy); + + if (participants != null && !participants.isEmpty()) { + dto.setParticipants(Integer.parseInt(participants)); + } + + if (ashaId != null && !ashaId.isEmpty()) { + dto.setAshaId(Integer.parseInt(ashaId)); + } + + if (noOfLactingMother != null && !noOfLactingMother.isEmpty()) { + dto.setNoOfLactingMother(Integer.parseInt(noOfLactingMother)); + } + + if (noOfPragnentWomen != null && !noOfPragnentWomen.isEmpty()) { + dto.setNoOfPragnentWomen(Integer.parseInt(noOfPragnentWomen)); + } + + if (meetingImages != null && !meetingImages.isEmpty()) { + dto.setMeetingImages(meetingImages.toArray(new MultipartFile[0])); + } + + service.saveMeeting(dto); + + return ResponseEntity.ok("Saved Successfully"); + + } catch (Exception e) { + e.printStackTrace(); + return ResponseEntity.badRequest().body(e.getMessage()); + } + } + + @PostMapping(value = "/update", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) + public ResponseEntity updateMeeting( @RequestPart("meetingDate") String meetingDate, @RequestPart("place") String place, @RequestPart("participants") String participants, + @RequestPart("id") Long meetingId, @RequestPart("ashaId") String ashaId, @RequestPart("createdBy") String createdBy, @RequestPart(value = "meetingImages", required = false) List meetingImages) { try { MaaMeetingRequestDTO dto = new MaaMeetingRequestDTO(); - dto.setMeetingDate(LocalDate.parse(meetingDate)); - dto.setPlace(place); - dto.setParticipants(Integer.parseInt(participants)); - dto.setAshaId(Integer.parseInt(ashaId)); - dto.setCreatedBY(createdBy); - dto.setMeetingImages(meetingImages != null ? meetingImages.toArray(new MultipartFile[0]) : null); + if (meetingDate != null) { + dto.setMeetingDate(LocalDate.parse(meetingDate)); - service.saveMeeting(dto); + } + if (place != null) { + dto.setPlace(place); + + } + if (participants != null) { + dto.setParticipants(Integer.parseInt(participants)); + + } + if (ashaId != null) { + dto.setAshaId(Integer.parseInt(ashaId)); + + } + if (createdBy != null) { + dto.setCreatedBY(createdBy); + + } + if (meetingImages != null) { + dto.setMeetingImages(meetingImages != null ? meetingImages.toArray(new MultipartFile[0]) : null); + + } + if(meetingId!=null){ + dto.setId(meetingId); + } + if (dto != null) { + service.updateMeeting(dto); + + } return ResponseEntity.ok("Saved Successfully"); } catch (Exception e) { return ResponseEntity.badRequest().body(e.getMessage()); diff --git a/src/main/java/com/iemr/flw/controller/MalariaFollowUpController.java b/src/main/java/com/iemr/flw/controller/MalariaFollowUpController.java index 4e3ae194..29204ab6 100644 --- a/src/main/java/com/iemr/flw/controller/MalariaFollowUpController.java +++ b/src/main/java/com/iemr/flw/controller/MalariaFollowUpController.java @@ -28,6 +28,9 @@ import com.iemr.flw.dto.iemr.MalariaFollowListUpDTO; import com.iemr.flw.dto.iemr.MalariaFollowUpDTO; import com.iemr.flw.service.MalariaFollowUpService; +import com.iemr.flw.utils.JwtUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -39,46 +42,59 @@ @RestController @RequestMapping(value = "/follow-up", headers = "Authorization") public class MalariaFollowUpController { + private final Logger logger = LoggerFactory.getLogger(MalariaFollowUpController.class); + + @Autowired + private JwtUtil jwtUtil; @Autowired private MalariaFollowUpService followUpService; @RequestMapping(value = "save", method = RequestMethod.POST) - public ResponseEntity> save(@RequestBody MalariaFollowUpDTO dto) { + public ResponseEntity> save(@RequestBody MalariaFollowUpDTO dto,@RequestHeader(value = "JwtToken") String token) { Map response = new HashMap<>(); try { - Boolean result = followUpService.saveFollowUp(dto); - if (result) { - response.put("status", "Success"); - response.put("statusCode", 200); - response.put("message", "Follow-up saved successfully"); - } else { - response.put("status", "Failed"); - response.put("statusCode", 400); + if(token!=null){ + + Boolean result = followUpService.saveFollowUp(dto,token); + if (result) { + response.put("status", "Success"); + response.put("statusCode", 200); + response.put("message", "Follow-up saved successfully"); + } else { + response.put("status", "Failed"); + response.put("statusCode", 5000); + } } + } catch (Exception e) { response.put("status", "Error: " + e.getMessage()); - response.put("statusCode", 500); + response.put("statusCode", 5000); } return ResponseEntity.ok(response); } @RequestMapping(value = "get", method = RequestMethod.POST) - public ResponseEntity> getFollowUpsByUserId(@RequestBody GetDiseaseRequestHandler getDiseaseRequestHandler) { + public ResponseEntity> getFollowUpsByUserId(@RequestBody GetDiseaseRequestHandler getDiseaseRequestHandler,@RequestHeader(value = "JwtToken") String token) { Map response = new HashMap<>(); try { - List data = followUpService.getByUserId(getDiseaseRequestHandler.getUserId()); - response.put("status", "Success"); - response.put("statusCode", 200); - response.put("data", data); - if (data.isEmpty()) { - response.put("message", "No records found"); + if(token!=null){ + List data = followUpService.getByUserId(jwtUtil.extractUserId(token)); + response.put("status", "Success"); + response.put("statusCode", 200); + response.put("data", data); + if (data.isEmpty()) { + response.put("message", "No records found"); + } } + } catch (Exception e) { + logger.info("Fail Malaria followUp: "+e.getMessage()); + logger.info("Fail Malaria full error: "+e); response.put("status", "Error: " + e.getMessage()); - response.put("statusCode", 500); + response.put("statusCode", 5000); } return ResponseEntity.ok(response); diff --git a/src/main/java/com/iemr/flw/controller/MaternalHealthController.java b/src/main/java/com/iemr/flw/controller/MaternalHealthController.java index e23cc8ed..9c8459da 100644 --- a/src/main/java/com/iemr/flw/controller/MaternalHealthController.java +++ b/src/main/java/com/iemr/flw/controller/MaternalHealthController.java @@ -15,17 +15,18 @@ import io.swagger.v3.oas.annotations.Operation; import jakarta.servlet.http.HttpServletRequest; -import org.checkerframework.checker.units.qual.A; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.sql.Timestamp; import java.util.List; @RestController -@RequestMapping(value = "/maternalCare", headers = "Authorization", consumes = "application/json", produces = "application/json") +@RequestMapping(value = "/maternalCare", consumes = "application/json", produces = "application/json") public class MaternalHealthController { private final Logger logger = LoggerFactory.getLogger(CoupleController.class); @@ -91,21 +92,26 @@ public String getPregnantWomanList(@RequestBody GetBenRequestHandler requestDTO, response.setError(5000, "Error in pregnant woman get data : " + e); } return response.toString(); + } @Operation(summary = "save anc visit details") @RequestMapping(value = { "/ancVisit/saveAll" }, method = { RequestMethod.POST }) public String saveANCVisit(@RequestBody List ancVisitDTOs, - @RequestHeader(value = "Authorization") String Authorization) { + @RequestHeader(value = "JwtToken") String token) { OutputResponse response = new OutputResponse(); try { if (ancVisitDTOs.size() != 0) { logger.info("Saving ANC visits with timestamp : " + new Timestamp(System.currentTimeMillis())); - String s = maternalHealthService.saveANCVisit(ancVisitDTOs); - if (s != null) - response.setResponse(s); - else - response.setError(5000, "Saving anc data to db failed"); + if(token!=null){ + String s = maternalHealthService.saveANCVisit(ancVisitDTOs,jwtUtil.extractUserId(token)); + + if (s != null) + response.setResponse(s); + else + response.setError(5000, "Saving anc data to db failed"); + } + } else response.setError(5000, "Invalid/NULL request obj"); } catch (Exception e) { @@ -141,6 +147,67 @@ public String getANCVisitDetails(@RequestBody GetBenRequestHandler requestDTO, return response.toString(); } + @Operation(summary = "save anc visit question") + @RequestMapping(value = { "/ancVisit/counselling/saveAll" }, method = { RequestMethod.POST }) + public String saveANCVisitQuestion(@RequestBody List ancVisitQuestionsDTOS, + @RequestHeader(value = "JwtToken") String Authorization) { + OutputResponse response = new OutputResponse(); + try { + if (ancVisitQuestionsDTOS.size() != 0) { + + logger.info("Saving ANC visits with timestamp : " + new Timestamp(System.currentTimeMillis())); + String s = maternalHealthService.saveANCVisitQuestions(ancVisitQuestionsDTOS,Authorization); + if (s != null) + response.setResponse(s); + else + response.setError(500, "Saving anc data to db failed"); + } else + response.setError(500, "Invalid/NULL request obj"); + } catch (Exception e) { + logger.error("Error in save ANC visit details : ",e); + + response.setError(500, "Error in save ANC visit details : " + e); + } + return response.toString(); + } + + @Operation(summary = "get anc visit questions") + @RequestMapping(value = { "/ancVisit/counselling/getAll" }, method = { RequestMethod.POST }) + public ResponseEntity>> getANCVisitQuestion(@RequestBody GetBenRequestHandler requestDTO, + @RequestHeader(value = "JwtToken") String Authorization) { + StandardResponse> response = new StandardResponse<>(); + + try { + if (requestDTO != null) { + logger.info("Request: " + requestDTO); + + List result = maternalHealthService.getANCCounselling(requestDTO); + + response.setStatusCode(200); + response.setStatus("Success"); + response.setErrorMessage("Success"); + response.setData(result); + + return ResponseEntity.ok(response); + + } else { + response.setStatusCode(400); + response.setStatus("Failed"); + response.setErrorMessage("Invalid request object"); + response.setData(null); + return ResponseEntity.badRequest().body(response); + } + } catch (Exception e) { + logger.error("Exception in fetching HBNC visits", e); + + response.setStatusCode(500); + response.setStatus("Failed"); + response.setErrorMessage("Internal Server Error: " + e.getMessage()); + response.setData(null); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); + } + } + @Operation(summary = "save Delivery Outcome details") @RequestMapping(value = { "/deliveryOutcome/saveAll" }, method = { RequestMethod.POST }) public String saveDeliveryOutcome(@RequestBody List deliveryOutcomeDTOS, @@ -163,6 +230,7 @@ public String saveDeliveryOutcome(@RequestBody List delivery response.setError(5000, "Error in save delivery outcome details : " + e); } return response.toString(); + } @Operation(summary = "get Delivery Outcome details") @@ -177,9 +245,9 @@ public String getDeliveryOutcome(@RequestBody GetBenRequestHandler requestDTO, List result = deliveryOutcomeService.getDeliveryOutcome(requestDTO); Gson gson = new GsonBuilder().setDateFormat("MMM dd, yyyy h:mm:ss a").create(); String s = gson.toJson(result); - if (s != null) - response.setResponse(s); - else + if (result != null && !result.isEmpty()) { + response.setResponse(gson.toJson(result)); + }else response.setError(5000, "No record found"); } else response.setError(5000, "Invalid/NULL request obj"); diff --git a/src/main/java/com/iemr/flw/controller/NotificationController.java b/src/main/java/com/iemr/flw/controller/NotificationController.java new file mode 100644 index 00000000..54d0443e --- /dev/null +++ b/src/main/java/com/iemr/flw/controller/NotificationController.java @@ -0,0 +1,160 @@ +package com.iemr.flw.controller; + +import com.iemr.flw.dto.iemr.NotificationListDTO; +import com.iemr.flw.dto.iemr.SendNotificationRequestDTO; +import com.iemr.flw.service.NotificationService; +import com.iemr.flw.utils.JwtUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.util.HashMap; +import java.util.Map; + +@RestController +@RequestMapping("/notification") +public class NotificationController { + + private final Logger logger = LoggerFactory.getLogger(this.getClass().getName()); + @Autowired + private JwtUtil jwtUtil; + + @Autowired + private NotificationService notificationService; + + /** GET /notifications/{receiverUserId}?page=0&size=20 */ + @PostMapping("/list") + public Map listNotifications(@RequestHeader(value = "JwtToken") String token) { + Map response = new HashMap<>(); + try { + Integer receiverUserId = jwtUtil.extractUserId(token); + if (receiverUserId != null) { + NotificationListDTO notificationListDTO = + notificationService.getNotifications(receiverUserId); + + if (notificationListDTO != null) { + response.put("statusCode", 200); + response.put("data", notificationListDTO); + } else { + response.put("statusCode", 5000); + response.put("error", "Invalid/NULL request obj"); + } + } else { + response.put("statusCode", 5000); + response.put("error", "Invalid/NULL request obj"); + } + } catch (Exception e) { + logger.error("Error in get data : " + e); + response.put("statusCode", 5000); + response.put("error", "Error in get data : " + e); + } + return response; + } + + /** GET /notifications/{receiverUserId}/unread-count */ + @GetMapping("/{receiverUserId}/unread-count") + public Map unreadCount(@PathVariable Integer receiverUserId) { + Map response = new HashMap<>(); + try { + + if (receiverUserId != null) { + Long unreadCount = notificationService.getUnreadCount(receiverUserId); + + if (unreadCount != null) { + response.put("statusCode", 200); + response.put("data", unreadCount); + } else { + response.put("statusCode", 5000); + response.put("error", "Invalid/NULL request obj"); + } + } else { + response.put("statusCode", 5000); + response.put("error", "Invalid/NULL request obj"); + } + } catch (Exception e) { + logger.error("Error in get data : " + e); + response.put("statusCode", 5000); + response.put("error", "Error in get data : " + e); + } + return response; + } + + /** PUT /notifications/{notificationId}/read */ + @PutMapping("/{notificationId}/read") + public Map markAsRead(@PathVariable Long notificationId) { + Map response = new HashMap<>(); + try { + if (notificationId != null) { + boolean success = notificationService.markAsRead(notificationId); + + response.put("statusCode", 200); + response.put("data", success); + } else { + response.put("statusCode", 5000); + response.put("error", "Invalid/NULL request obj"); + } + } catch (Exception e) { + logger.error("Error in update data : " + e); + response.put("statusCode", 5000); + response.put("error", "Error in update data : " + e); + } + return response; + } + + /** POST /notifications/send */ + @PostMapping("/send") + public Map sendNotification(@RequestBody SendNotificationRequestDTO request) { + Map response = new HashMap<>(); + try { + if (request != null) { + String result = notificationService.sendNotification( + request.getAppType(), + request.getTopic(), + request.getTitle(), + request.getBody(), + request.getRedirect(), + request.getNotificationType(), + request.getReceiverId()); + + if (result != null) { + response.put("statusCode", 200); + response.put("data", result); + } else { + response.put("statusCode", 5000); + response.put("error", "Invalid/NULL request obj"); + } + } else { + response.put("statusCode", 5000); + response.put("error", "Invalid/NULL request obj"); + } + } catch (Exception e) { + logger.error("Error in send notification : " + e); + response.put("statusCode", 5000); + response.put("error", "Error in send notification : " + e); + } + return response; + } + + /** PUT /notifications/{receiverUserId}/read-all */ + @PutMapping("/{receiverUserId}/read-all") + public Map markAllAsRead(@PathVariable Integer receiverUserId) { + Map response = new HashMap<>(); + try { + if (receiverUserId != null) { + int updatedCount = notificationService.markAllAsRead(receiverUserId); + + response.put("statusCode", 200); + response.put("data", updatedCount); + } else { + response.put("statusCode", 5000); + response.put("error", "Invalid/NULL request obj"); + } + } catch (Exception e) { + logger.error("Error in update data : " + e); + response.put("statusCode", 5000); + response.put("error", "Error in update data : " + e); + } + return response; + } +} \ No newline at end of file diff --git a/src/main/java/com/iemr/flw/controller/TBController.java b/src/main/java/com/iemr/flw/controller/TBController.java index 6beb9008..e95c3ab4 100644 --- a/src/main/java/com/iemr/flw/controller/TBController.java +++ b/src/main/java/com/iemr/flw/controller/TBController.java @@ -1,14 +1,18 @@ package com.iemr.flw.controller; +import com.iemr.flw.domain.iemr.TBConfirmedCaseDTO; import com.iemr.flw.dto.identity.GetBenRequestHandler; +import com.iemr.flw.dto.iemr.TBConfirmedRequestDTO; import com.iemr.flw.dto.iemr.TBScreeningRequestDTO; import com.iemr.flw.dto.iemr.TBSuspectedRequestDTO; +import com.iemr.flw.service.TBConfirmedCaseService; import com.iemr.flw.service.TBScreeningService; import com.iemr.flw.service.TBSuspectedService; import com.iemr.flw.utils.response.OutputResponse; import io.swagger.v3.oas.annotations.Operation; +import org.checkerframework.checker.units.qual.A; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -28,6 +32,9 @@ public class TBController { @Autowired private TBSuspectedService tbSuspectedService; + @Autowired + private TBConfirmedCaseService tbConfirmedCaseService; + @Operation(summary = "get tb screening data of all beneficiaries registered with given user id") @RequestMapping(value = { "/screening/getAll" }, method = { RequestMethod.POST }) public String getAllScreeningByUserId(@RequestBody GetBenRequestHandler requestDTO, @@ -42,9 +49,9 @@ public String getAllScreeningByUserId(@RequestBody GetBenRequestHandler requestD if (s != null) response.setResponse(s); else - response.setError(5000, "No record found"); + response.setError(500, "No record found"); } else - response.setError(5000, "Invalid/NULL request obj"); + response.setError(500, "Invalid/NULL request obj"); } catch (Exception e) { logger.error("Error in tb screening get data : " + e); response.setError(5000, "Error in tb screening get data : " + e); @@ -66,9 +73,9 @@ public String saveAllScreeningByUserId(@RequestBody TBScreeningRequestDTO reques if (s != null) response.setResponse(s); else - response.setError(5000, "No record found"); + response.setError(500, "No record found"); } else - response.setError(5000, "Invalid/NULL request obj"); + response.setError(500, "Invalid/NULL request obj"); } catch (Exception e) { logger.error("Error in save tb screening details : " + e); response.setError(5000, "Error in save tb suspected details : " + e); @@ -91,12 +98,12 @@ public String getAllSuspectedByUserId(@RequestBody GetBenRequestHandler requestD if (s != null) response.setResponse(s); else - response.setError(5000, "No record found"); + response.setError(500, "No record found"); } else - response.setError(5000, "Invalid/NULL request obj"); + response.setError(500, "Invalid/NULL request obj"); } catch (Exception e) { logger.error("Error in get data : " + e); - response.setError(5000, "Error in get data : " + e); + response.setError(500, "Error in get data : " + e); } return response.toString(); } @@ -115,13 +122,73 @@ public String saveAllSuspectedByUserId(@RequestBody TBSuspectedRequestDTO reques if (s != null) response.setResponse(s); else - response.setError(5000, "No record found"); + response.setError(500, "No record found"); } else - response.setError(5000, "Invalid/NULL request obj"); + response.setError(500, "Invalid/NULL request obj"); } catch (Exception e) { logger.error("Error in save tb suspected details : " + e); response.setError(5000, "Error in save tb suspected details : " + e); } return response.toString(); } + + @Operation(summary = "save tb confirmed case data of beneficiary") + @RequestMapping(value = { "/confirmed/save" }, method = { RequestMethod.POST }) + public String saveConfirmedCase( + @RequestBody TBConfirmedRequestDTO requestDTO, + @RequestHeader(value = "jwtToken") String token) { + + OutputResponse response = new OutputResponse(); + + try { + if (requestDTO != null) { + + logger.info("request object with timestamp : " + + new Timestamp(System.currentTimeMillis()) + " " + + requestDTO); + + String result = tbConfirmedCaseService.save(requestDTO.getTbConfirmedList(), token); + + if (result != null) + response.setResponse(result); + else + response.setError(500, "No record saved"); + + } else { + response.setError(500, "Invalid/NULL request obj"); + } + } catch (Exception e) { + logger.error("Error in save tb confirmed case details : ", e); + response.setError(500, "Error in save tb confirmed case details : " + e.getMessage()); + } + + return response.toString(); + } + + @Operation(summary = "get tb confirmed case by beneficiary id") + @RequestMapping(value = { "/confirmed/getAll" }, method = { RequestMethod.GET }) + public String getConfirmedByBenId( + @RequestHeader(value = "jwtToken") String token) { + + OutputResponse response = new OutputResponse(); + + try { + String result = + tbConfirmedCaseService.getByUserId(token); + + if (result != null) + response.setResponse(result); + else + response.setError(404, "No record found"); + + } catch (Exception e) { + logger.error("Error in get tb confirmed case details : ", e); + response.setError(500, "Error in get tb confirmed case details : " + e.getMessage()); + } + + return response.toString(); + } + + + } diff --git a/src/main/java/com/iemr/flw/controller/UwinSessionController.java b/src/main/java/com/iemr/flw/controller/UwinSessionController.java index 6fd31b16..6a273d9a 100644 --- a/src/main/java/com/iemr/flw/controller/UwinSessionController.java +++ b/src/main/java/com/iemr/flw/controller/UwinSessionController.java @@ -31,14 +31,14 @@ public class UwinSessionController { @Autowired private JwtUtil jwtUtil; - @RequestMapping(value = "saveAll", method = RequestMethod.POST, headers = "Authorization", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) + @RequestMapping(value = "saveAll", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public ResponseEntity saveSession( @RequestPart("meetingDate") String meetingDate, @RequestPart("place") String place, @RequestPart("participants") String participants, @RequestPart("ashaId") String ashaId, @RequestPart("createdBy") String createdBy, - @RequestPart(value = "meetingImages", required = false) List images) throws Exception { + @RequestPart(value = "meetingImages", required = false) List images,@RequestHeader(value = "jwtToken") String jwtToken) throws Exception { Map response = new LinkedHashMap<>(); UwinSessionRequestDTO dto = new UwinSessionRequestDTO(); diff --git a/src/main/java/com/iemr/flw/controller/VillageLevelFormController.java b/src/main/java/com/iemr/flw/controller/VillageLevelFormController.java index ddbb7a76..884f794d 100644 --- a/src/main/java/com/iemr/flw/controller/VillageLevelFormController.java +++ b/src/main/java/com/iemr/flw/controller/VillageLevelFormController.java @@ -36,13 +36,13 @@ import java.util.Map; @RestController -@RequestMapping(value = "/forms/villageLevel", headers = "Authorization") +@RequestMapping(value = "/forms/villageLevel") public class VillageLevelFormController { @Autowired private VillageLevelFormService villageLevelFormService; - @RequestMapping(value = "vhnd/saveAll", method = RequestMethod.POST, headers = "Authorization") + @RequestMapping(value = "vhnd/saveAll",method = RequestMethod.POST) public ResponseEntity> saveVhndForm(@RequestBody VhndDto dto) { Map response = new HashMap<>(); if (!dto.getEntries().isEmpty()) { @@ -64,7 +64,7 @@ public ResponseEntity> saveVhndForm(@RequestBody VhndDto dto } - @RequestMapping(value = "vhnc/saveAll", method = RequestMethod.POST, headers = "Authorization") + @RequestMapping(value = "vhnc/saveAll", method = RequestMethod.POST) public ResponseEntity> saveVhncForm(@RequestBody VhncDto dto) { Map response = new HashMap<>(); @@ -87,7 +87,7 @@ public ResponseEntity> saveVhncForm(@RequestBody VhncDto dto } - @RequestMapping(value = "phc/saveAll", method = RequestMethod.POST, headers = "Authorization") + @RequestMapping(value = "phc/saveAll", method = RequestMethod.POST) public ResponseEntity> savePhcForm(@RequestBody PhcReviewMeetingDTO dto) { Map response = new HashMap<>(); @@ -109,7 +109,7 @@ public ResponseEntity> savePhcForm(@RequestBody PhcReviewMee } - @RequestMapping(value = "ahd/saveAll", method = RequestMethod.POST, headers = "Authorization") + @RequestMapping(value = "ahd/saveAll", method = RequestMethod.POST) public ResponseEntity> saveAhdForm(@RequestBody AhdMeetingDto dto) { Map response = new HashMap<>(); @@ -129,7 +129,7 @@ public ResponseEntity> saveAhdForm(@RequestBody AhdMeetingDt } - @RequestMapping(value = "deworming/saveAll", method = RequestMethod.POST, headers = "Authorization") + @RequestMapping(value = "deworming/saveAll", method = RequestMethod.POST) public ResponseEntity> saveDewormingForm(@RequestBody DewormingDto dto) { Map response = new HashMap<>(); @@ -150,7 +150,7 @@ public ResponseEntity> saveDewormingForm(@RequestBody Deworm } - @RequestMapping(value = "getAll", method = RequestMethod.POST, headers = "Authorization") + @RequestMapping(value = "getAll", method = RequestMethod.POST) public ResponseEntity> getVillageLevelFormData(@RequestBody GetVillageLevelRequestHandler getVillageLevelRequestHandler) { Map response = new LinkedHashMap<>(); diff --git a/src/main/java/com/iemr/flw/controller/health/HealthController.java b/src/main/java/com/iemr/flw/controller/health/HealthController.java new file mode 100644 index 00000000..93308554 --- /dev/null +++ b/src/main/java/com/iemr/flw/controller/health/HealthController.java @@ -0,0 +1,83 @@ +/* +* AMRIT – Accessible Medical Records via Integrated Technology +* Integrated EHR (Electronic Health Records) Solution +* +* Copyright (C) "Piramal Swasthya Management and Research Institute" +* +* This file is part of AMRIT. +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see https://www.gnu.org/licenses/. +*/ + +package com.iemr.flw.controller.health; + +import java.time.Instant; +import java.util.Map; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import com.iemr.flw.service.health.HealthService; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.responses.ApiResponses; +import io.swagger.v3.oas.annotations.tags.Tag; + +@RestController +@RequestMapping("/health") +@Tag(name = "Health Check", description = "APIs for checking infrastructure health status") +public class HealthController { + + private static final Logger logger = LoggerFactory.getLogger(HealthController.class); + + private final HealthService healthService; + + public HealthController(HealthService healthService) { + this.healthService = healthService; + } + + @GetMapping + @Operation(summary = "Check infrastructure health", + description = "Returns the health status of MySQL, Redis, and other configured services") + @ApiResponses({ + @ApiResponse(responseCode = "200", description = "Services are UP or DEGRADED (operational with warnings)"), + @ApiResponse(responseCode = "503", description = "One or more critical services are DOWN") + }) + public ResponseEntity> checkHealth() { + logger.debug("Health check endpoint called"); + + try { + Map healthStatus = healthService.checkHealth(); + String overallStatus = (String) healthStatus.get("status"); + + HttpStatus httpStatus = "DOWN".equals(overallStatus) ? HttpStatus.SERVICE_UNAVAILABLE : HttpStatus.OK; + + logger.debug("Health check completed with status: {}", overallStatus); + return new ResponseEntity<>(healthStatus, httpStatus); + + } catch (Exception e) { + logger.error("Unexpected error during health check", e); + + Map errorResponse = Map.of( + "status", "DOWN", + "timestamp", Instant.now().toString() + ); + + return new ResponseEntity<>(errorResponse, HttpStatus.SERVICE_UNAVAILABLE); + } + } +} diff --git a/src/main/java/com/iemr/flw/controller/version/VersionController.java b/src/main/java/com/iemr/flw/controller/version/VersionController.java new file mode 100644 index 00000000..f2f2bf66 --- /dev/null +++ b/src/main/java/com/iemr/flw/controller/version/VersionController.java @@ -0,0 +1,80 @@ +/* +* AMRIT – Accessible Medical Records via Integrated Technology +* Integrated EHR (Electronic Health Records) Solution +* +* Copyright (C) "Piramal Swasthya Management and Research Institute" +* +* This file is part of AMRIT. +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see https://www.gnu.org/licenses/. +*/ + +package com.iemr.flw.controller.version; + +import java.io.IOException; +import java.io.InputStream; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Properties; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import io.swagger.v3.oas.annotations.Operation; + +@RestController +public class VersionController { + + private final Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName()); + + private static final String UNKNOWN_VALUE = "unknown"; + + @Operation(summary = "Get version information") + @GetMapping(value = "/version", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity> versionInformation() { + Map response = new LinkedHashMap<>(); + try { + logger.info("version Controller Start"); + Properties gitProperties = loadGitProperties(); + response.put("buildTimestamp", gitProperties.getProperty("git.build.time", UNKNOWN_VALUE)); + response.put("version", gitProperties.getProperty("git.build.version", UNKNOWN_VALUE)); + response.put("branch", gitProperties.getProperty("git.branch", UNKNOWN_VALUE)); + response.put("commitHash", gitProperties.getProperty("git.commit.id.abbrev", UNKNOWN_VALUE)); + } catch (Exception e) { + logger.error("Failed to load version information", e); + response.put("buildTimestamp", UNKNOWN_VALUE); + response.put("version", UNKNOWN_VALUE); + response.put("branch", UNKNOWN_VALUE); + response.put("commitHash", UNKNOWN_VALUE); + } + logger.info("version Controller End"); + return ResponseEntity.ok(response); + } + + private Properties loadGitProperties() throws IOException { + Properties properties = new Properties(); + try (InputStream input = getClass().getClassLoader() + .getResourceAsStream("git.properties")) { + if (input != null) { + properties.load(input); + } + } + return properties; + } +} diff --git a/src/main/java/com/iemr/flw/domain/identity/RMNCHBeneficiaryDetailsRmnch.java b/src/main/java/com/iemr/flw/domain/identity/RMNCHBeneficiaryDetailsRmnch.java index ac336da1..297694b7 100644 --- a/src/main/java/com/iemr/flw/domain/identity/RMNCHBeneficiaryDetailsRmnch.java +++ b/src/main/java/com/iemr/flw/domain/identity/RMNCHBeneficiaryDetailsRmnch.java @@ -386,6 +386,9 @@ public class RMNCHBeneficiaryDetailsRmnch { @Expose private Integer noofAlivechildren; + @Expose + private Boolean isDeactivate; + @Expose @Transient private Integer servicePointID; diff --git a/src/main/java/com/iemr/flw/domain/identity/RMNCHHouseHoldDetails.java b/src/main/java/com/iemr/flw/domain/identity/RMNCHHouseHoldDetails.java index 9ab4f822..3a3ccec8 100644 --- a/src/main/java/com/iemr/flw/domain/identity/RMNCHHouseHoldDetails.java +++ b/src/main/java/com/iemr/flw/domain/identity/RMNCHHouseHoldDetails.java @@ -325,4 +325,8 @@ public class RMNCHHouseHoldDetails { @Column(name = "mohallaName") private String mohallaName; + @Expose + @Column(name = "isDeactivate") + private Boolean isDeactivate; + } diff --git a/src/main/java/com/iemr/flw/domain/identity/RMNCHMBeneficiarydetail.java b/src/main/java/com/iemr/flw/domain/identity/RMNCHMBeneficiarydetail.java index 72e994f5..779a09da 100644 --- a/src/main/java/com/iemr/flw/domain/identity/RMNCHMBeneficiarydetail.java +++ b/src/main/java/com/iemr/flw/domain/identity/RMNCHMBeneficiarydetail.java @@ -186,4 +186,8 @@ public class RMNCHMBeneficiarydetail { @Transient private Integer ProviderServiceMapID; + @Expose + @Column(name = "familyid") + private String familyId; + } diff --git a/src/main/java/com/iemr/flw/domain/iemr/ANCVisit.java b/src/main/java/com/iemr/flw/domain/iemr/ANCVisit.java index c5240d1c..4128f082 100644 --- a/src/main/java/com/iemr/flw/domain/iemr/ANCVisit.java +++ b/src/main/java/com/iemr/flw/domain/iemr/ANCVisit.java @@ -167,6 +167,12 @@ public class ANCVisit { @Column(name = "date_of_sterilisation") private Timestamp dateSterilisation; + @Column (name = "place_of_anc") + private String placeOfAnc; + + @Column(name = "place_of_ancId") + private Integer placeOfAncId; + diff --git a/src/main/java/com/iemr/flw/domain/iemr/AbhaApiResponse.java b/src/main/java/com/iemr/flw/domain/iemr/AbhaApiResponse.java new file mode 100644 index 00000000..db38d11a --- /dev/null +++ b/src/main/java/com/iemr/flw/domain/iemr/AbhaApiResponse.java @@ -0,0 +1,21 @@ +package com.iemr.flw.domain.iemr; + +import com.fasterxml.jackson.annotation.JsonAlias; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.iemr.flw.dto.abhaBeneficiary.AbhaBeneficiaryDTO; +import lombok.Data; + +import java.util.List; + +@Data +public class AbhaApiResponse { + + @JsonProperty("status_code") + private String statusCode; + + private String message; + + @JsonProperty("data") + @JsonAlias("object_data") + private List data; +} \ No newline at end of file diff --git a/src/main/java/com/iemr/flw/domain/iemr/AncCounsellingCare.java b/src/main/java/com/iemr/flw/domain/iemr/AncCounsellingCare.java new file mode 100644 index 00000000..46737f12 --- /dev/null +++ b/src/main/java/com/iemr/flw/domain/iemr/AncCounsellingCare.java @@ -0,0 +1,128 @@ +package com.iemr.flw.domain.iemr; + +import jakarta.persistence.*; +import lombok.Data; + +import java.time.LocalDate; +import java.time.LocalDateTime; + +@Entity +@Table(name = "anc_counselling_care", schema = "db_iemr") +@Data +public class AncCounsellingCare { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(name = "beneficiary_id", nullable = false) + private Long beneficiaryId; + + @Column(name = "visit_date") + private LocalDate visitDate; + + @Column(name = "home_visit_date", nullable = false) + private LocalDate homeVisitDate; + + @Column(name = "anc_visit_id", nullable = false) + private Long ancVisitId; + + @Column(name = "user_id") + private Integer userId; + + @Column(name = "created_by") + private String createdBy; + + @Column(name = "updated_by") + private String updatedBy; + + @Column(name = "created_at", updatable = false) + private LocalDateTime createdAt; + + @Column(name = "updated_at") + private LocalDateTime updatedAt; + + /* ---------- BOOLEAN FLAGS ---------- */ + + @Column(name = "select_all") + private Boolean selectAll = false; + + @Column(name = "swelling") + private Boolean swelling = false; + + @Column(name = "high_bp") + private Boolean highBp = false; + + @Column(name = "convulsions") + private Boolean convulsions = false; + + @Column(name = "anemia") + private Boolean anemia = false; + + @Column(name = "reduced_fetal_movement") + private Boolean reducedFetalMovement = false; + + @Column(name = "age_risk") + private Boolean ageRisk = false; + + @Column(name = "child_gap") + private Boolean childGap = false; + + @Column(name = "short_height") + private Boolean shortHeight = false; + + @Column(name = "pre_preg_weight") + private Boolean prePregWeight = false; + + @Column(name = "bleeding") + private Boolean bleeding = false; + + @Column(name = "miscarriage_history") + private Boolean miscarriageHistory = false; + + @Column(name = "four_plus_delivery") + private Boolean fourPlusDelivery = false; + + @Column(name = "first_delivery") + private Boolean firstDelivery = false; + + @Column(name = "twin_pregnancy") + private Boolean twinPregnancy = false; + + @Column(name = "c_section_history") + private Boolean cSectionHistory = false; + + @Column(name = "pre_existing_disease") + private Boolean preExistingDisease = false; + + @Column(name = "fever_malaria") + private Boolean feverMalaria = false; + + @Column(name = "jaundice") + private Boolean jaundice = false; + + @Column(name = "sickle_cell") + private Boolean sickleCell = false; + + @Column(name = "prolonged_labor") + private Boolean prolongedLabor = false; + + @Column(name = "malpresentation") + private Boolean malpresentation = false; + + /* ---------- Lifecycle Hooks ---------- */ + + @PrePersist + protected void onCreate() { + this.createdAt = LocalDateTime.now(); + this.updatedAt = LocalDateTime.now(); + } + + @PreUpdate + protected void onUpdate() { + this.updatedAt = LocalDateTime.now(); + } + + /* ---------- Getters & Setters ---------- */ + // Lombok use kar raha ho toh @Getter @Setter laga sakta hai +} diff --git a/src/main/java/com/iemr/flw/domain/iemr/AshaSupervisorMapping.java b/src/main/java/com/iemr/flw/domain/iemr/AshaSupervisorMapping.java new file mode 100644 index 00000000..ee641db8 --- /dev/null +++ b/src/main/java/com/iemr/flw/domain/iemr/AshaSupervisorMapping.java @@ -0,0 +1,34 @@ +package com.iemr.flw.domain.iemr; + +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 lombok.NoArgsConstructor; + +@Entity +@Table(name = "asha_supervisor_mapping") +@Data +@NoArgsConstructor +public class AshaSupervisorMapping { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id") + private Long id; + + @Column(name = "supervisorUserID") + private Integer supervisorUserID; + + @Column(name = "ashaUserID") + private Integer ashaUserID; + + @Column(name = "facilityID") + private Integer facilityID; + + @Column(name = "deleted", insertable = false, updatable = true) + private Boolean deleted; +} \ No newline at end of file diff --git a/src/main/java/com/iemr/flw/domain/iemr/AshaWorker.java b/src/main/java/com/iemr/flw/domain/iemr/AshaWorker.java index 36fba04e..2df2bf43 100644 --- a/src/main/java/com/iemr/flw/domain/iemr/AshaWorker.java +++ b/src/main/java/com/iemr/flw/domain/iemr/AshaWorker.java @@ -27,7 +27,7 @@ public class AshaWorker { private Integer employeeId; // Changed from Integer to String @Column(name = "dob") - private LocalDate dob; // Changed from String to LocalDate + private String dob; // Changed from String to LocalDate @Column(name = "mobile_number") @@ -40,7 +40,7 @@ public class AshaWorker { private String fatherOrSpouseName; @Column(name = "date_of_joining") - private LocalDate dateOfJoining; // Changed from String to LocalDate + private String dateOfJoining; // Changed from String to LocalDate @Column(name = "bank_account") @@ -98,8 +98,9 @@ public class AshaWorker { @Column(name = "ProviderServiceMapID") private Integer ProviderServiceMapID; + @Lob @Column(name = "profileImage") - private String profileImage; + private byte[] profileImage; @Column(name = "isFatherOrSpouse") private Boolean isFatherOrSpouse; diff --git a/src/main/java/com/iemr/flw/domain/iemr/CampaignOrs.java b/src/main/java/com/iemr/flw/domain/iemr/CampaignOrs.java new file mode 100644 index 00000000..e33e1543 --- /dev/null +++ b/src/main/java/com/iemr/flw/domain/iemr/CampaignOrs.java @@ -0,0 +1,59 @@ +package com.iemr.flw.domain.iemr; + +import jakarta.persistence.*; +import lombok.Data; + +import java.time.LocalDate; +import java.time.LocalDateTime; + +@Entity +@Table(name = "campaign_ors",schema = "db_iemr") +@Data +public class CampaignOrs { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(name = "start_date", nullable = false) + private LocalDate startDate; + + @Column(name = "end_date", nullable = false) + private LocalDate endDate; + + @Column(name = "user_id", nullable = false) + private Integer userId; + + @Column(name = "number_of_families", nullable = false) + private Integer numberOfFamilies = 0; + + @Column(name = "campaign_photos",columnDefinition = "LONGTEXT") + private String campaignPhotos; + + @Column(name = "created_by", length = 200) + private String createdBy; + + @Column(name = "updated_by", length = 200) + private String updatedBy; + + @Column(name = "created_date", updatable = false) + private LocalDateTime createdDate; + + @Column(name = "updated_date") + private LocalDateTime updatedDate; + + /* ---------- Auto timestamps ---------- */ + + @PrePersist + protected void onCreate() { + this.createdDate = LocalDateTime.now(); + this.updatedDate = LocalDateTime.now(); + } + + @PreUpdate + protected void onUpdate() { + this.updatedDate = LocalDateTime.now(); + } + + +} diff --git a/src/main/java/com/iemr/flw/domain/iemr/CbacDetailsImer.java b/src/main/java/com/iemr/flw/domain/iemr/CbacDetailsImer.java new file mode 100644 index 00000000..989ed1d1 --- /dev/null +++ b/src/main/java/com/iemr/flw/domain/iemr/CbacDetailsImer.java @@ -0,0 +1,330 @@ +/* +* AMRIT – Accessible Medical Records via Integrated Technology +* Integrated EHR (Electronic Health Records) Solution +* +* Copyright (C) "Piramal Swasthya Management and Research Institute" +* +* This file is part of AMRIT. +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see https://www.gnu.org/licenses/. +*/ +package com.iemr.flw.domain.iemr; + +import java.sql.Timestamp; + +import jakarta.persistence.*; + +import com.google.gson.annotations.Expose; +import lombok.Data; + +@Entity +@Table(name = "t_cbacdetails") +@Data +public class CbacDetailsImer { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Expose + @Column(name = "Id", insertable = false) + private Long id; + @Expose + @Column(name = "BeneficiaryRegId") + private Long beneficiaryRegId; + + @Expose + @Column(name = "Visitcode") + private Long visitCode; + @Expose + @Column(name = "Cbac_Age") + private String cbacAge; + @Expose + @Column(name = "Cbac_Age_Score") + private Integer cbacAgeScore; + @Expose + @Column(name = "Cbac_ConusmeGutka") + private String cbacConsumeGutka; + @Expose + @Column(name = "Cbac_ConusmeGutka_Score") + private Integer cbacConsumeGutkaScore; + @Expose + @Column(name = "cbac_alcohol") + private String cbacAlcohol; + @Expose + @Column(name = "cbac_alcohol_Score") + private Integer cbacAlcoholScore; + @Expose + @Column(name = "Cbac_waist_Male") + private String cbacWaistMale; + @Expose + @Column(name = "Cbac_waist_Male_Score") + private Integer cbacWaistMaleScore; + @Expose + @Column(name = "Cbac_Waist_Female") + private String cbacWaistFemale; + @Expose + @Column(name = "Cbac_Waist_Female_Score") + private Integer cbacWaistFemaleScore; + @Expose + @Column(name = "Cbac_PhysicalActivity") + private String cbacPhysicalActivity; + @Expose + @Column(name = "Cbac_PhysicalActivity_Score") + private Integer cbacPhysicalActivityScore; + @Expose + @Column(name = "Cbac_FamilyHistory_bpdiabetes") + private String cbacFamilyHistoryBpdiabetes; + @Expose + @Column(name = "Cbac_FamilyHistory_bpdiabetes_Score") + private Integer cbacFamilyHistoryBpdiabetesScore; + @Expose + @Column(name = "Cbac_ShortnessBreath") + private String cbacShortnessBreath; + @Expose + @Column(name = "Cbac_Cough2weeks") + private String cbacCough2weeks; + @Expose + @Column(name = "Cbac_Bloodsputum") + private String cbacBloodsputum; + @Expose + @Column(name = "Cbac_fever2weeks") + private String cbacFever2weeks; + @Expose + @Column(name = "Cbac_WeightLoss") + private String cbacWeightLoss; + @Expose + @Column(name = "Cbac_NightSweats") + private String cbacNightSweats; + @Expose + @Column(name = "Cbac_AntiTBDrugs") + private String cbacAntiTBDrugs; + @Expose + @Column(name = "Cabc_TB") + private String cbacTb; + @Expose + @Column(name = "Cbac_TBHistory") + private String cbacTBHistory; + @Expose + @Column(name = "Cbac_Ulceration") + private String cbacUlceration; + @Expose + @Column(name = "Cbac_RecurrentTingling") + private String cbacRecurrentTingling; + @Expose + @Column(name = "Cbac_FitsHistory") + private String cbacFitsHistory; + @Expose + @Column(name = "Cbac_MouthopeningDifficulty") + private String cbacMouthopeningDifficulty; + @Expose + @Column(name = "Cbac_MouthUlcers") + private String cbacMouthUlcers; + @Expose + @Column(name = "Cbac_MouthUlcersGrowth") + private String cbacMouthUlcersGrowth; + @Expose + @Column(name = "Cbac_Mouthredpatch") + private String cbacMouthredpatch; + @Expose + @Column(name = "Cbac_Painchewing") + private String cbacPainchewing; + @Expose + @Column(name = "Cbac_Tonechange") + private String cbacTonechange; + @Expose + @Column(name = "Cbac_Hypopigmentedpatches") + private String cbacHypopigmentedpatches; + @Expose + @Column(name = "Cbac_Thickenedskin") + private String cbacThickenedskin; + @Expose + @Column(name = "Cbac_Nodulesonskin") + private String cbacNodulesonskin; + @Expose + @Column(name = "Cbac_RecurrentNumbness") + private String cbacRecurrentNumbness; + @Expose + @Column(name = "Cbac_BlurredVision") + private String cbacBlurredVision; + @Expose + @Column(name = "Cbac_Difficultyreading") + private String cbacDifficultyreading; + @Expose + @Column(name = "Cbac_Painineyes") + private String cbacPainineyes; + @Expose + @Column(name = "Cbac_RednessPain") + private String cbacRednessPain; + @Expose + @Column(name = "Cbac_DifficultyHearing") + private String cbacDifficultyHearing; + @Expose + @Column(name = "Cbac_Clawingfingers") + private String cbacClawingfingers; + @Expose + @Column(name = "Cbac_HandTingling") + private String cbacHandTingling; + @Expose + @Column(name = "Cbac_InabilityCloseeyelid") + private String cbacInabilityCloseeyelid; + @Expose + @Column(name = "Cbac_DifficultHoldingObjects") + private String cbacDifficultHoldingObjects; + @Expose + @Column(name = "Cbac_Feetweakness") + private String cbacFeetweakness; + @Expose + @Column(name = "Cbac_LumpBreast") + private String cbacLumpBreast; + @Expose + @Column(name = "Cbac_BloodnippleDischarge") + private String cbacBloodnippleDischarge; + @Expose + @Column(name = "Cbac_Breastsizechange") + private String cbacBreastsizechange; + @Expose + @Column(name = "Cbac_BleedingPeriods") + private String cbacBleedingPeriods; + @Expose + @Column(name = "Cbac_BleedingMenopause") + private String cbacBleedingMenopause; + @Expose + @Column(name = "Cbac_BleedingIntercourse") + private String cbacBleedingIntercourse; + @Expose + @Column(name = "Cbac_VaginalDischarge") + private String cbacVaginalDischarge; + @Expose + @Column(name = "Cbac_FeelingUnsteady") + private String cbacFeelingUnsteady; + @Expose + @Column(name = "Cbac_PhysicalDisabilitySuffering") + private String cbacPhysicalDisabilitySuffering; + @Expose + @Column(name = "Cbac_NeedhelpEverydayActivities") + private String cbacNeedhelpEverydayActivities; + @Expose + @Column(name = "Cbac_Forgetnearones") + private String cbacForgetnearones; + @Expose + @Column(name = "ProviderServiceMapID") + private Integer providerServiceMapId; + + @Expose + @Column(name = "Total_Score") + private Integer totalScore; + @Expose + @Column(name = "Deleted", insertable = false, updatable = true) + private Boolean deleted; + + @Expose + @Column(name = "Processed", insertable = false, updatable = true) + private Character processed; + + @Expose + @Column(name = "CreatedBy") + private String createdBy; + + @Expose + @Column(name = "CreatedDate", insertable = false, updatable = false) + private Timestamp createdDate; + + @Expose + @Column(name = "ModifiedBy") + private String modifiedBy; + + @Expose + @Column(name = "LastModDate", insertable = false, updatable = false) + private Timestamp lastModDate; + + @Expose + @Column(name = "VanSerialNo") + private Integer vanSerialNo; + @Expose + @Column(name = "VanID") + private Integer vanId; + @Expose + @Column(name = "VehicalNo") + private String vehicalNo; + @Expose + @Column(name = "ParkingPlaceID") + private Integer parkingPlaceId; + @Expose + @Column(name = "SyncedBy") + private String syncedBy; + @Expose + @Column(name = "SyncedDate") + private Timestamp syncedDate; + + @Transient + @Expose + private Long beneficiaryId; + + @Expose + @Column(name = "Cbac_OccupationalExposure") + private String CbacOccupationalExposure; + + @Expose + @Column(name = "Cbac_BotheredProblem_last2weeks") + private String CbacBotheredProblemLast2weeks; + + @Expose + @Column(name = "Cbac_LittleInterest_Pleasure") + private String CbacLittleInterestPleasure; + + @Expose + @Column(name = "Cbac_Depressed_hopeless") + private String CbacDepressedhopeless; + + @Expose + @Column(name = "Cbac_DiscolorationSkin") + private String CbacDiscolorationSkin; + + @Expose + @Column(name = "Cbac_Cooking_Oil") + private String CbacCookingOil; + + @Expose + @Column(name = "Cbac_OccupationalExposure_score") + private String CbacOccupationalExposureScore; + + @Expose + @Column(name = "Cbac_BotheredProblem_last2weeks_score") + private String CbacBotheredProblemLast2weeksScore; + + @Expose + @Column(name = "Cbac_LittleInterest_Pleasure_score") + private String CbacLittleInterestPleasureScore; + + @Expose + @Column(name = "Cbac_Depressed_hopeless_score") + private String CbacDepressedhopelessScore; + + @Expose + @Column(name = "Cbac_Cooking_Oil_score") + private String CbacCookingOilScore; + + + @Expose + @Column(name = "Cbac_Feeling_Down_score") + private String CbacFeelingDownScore; + + @Expose + @Column(name = "is_refer") + private Boolean isRefer; + + + + + + +} diff --git a/src/main/java/com/iemr/flw/domain/iemr/ChildVaccination.java b/src/main/java/com/iemr/flw/domain/iemr/ChildVaccination.java index 45d5562e..4050b77c 100644 --- a/src/main/java/com/iemr/flw/domain/iemr/ChildVaccination.java +++ b/src/main/java/com/iemr/flw/domain/iemr/ChildVaccination.java @@ -10,6 +10,7 @@ import jakarta.persistence.Id; import jakarta.persistence.Table; import jakarta.persistence.Transient; +import org.checkerframework.checker.units.qual.C; @Entity @Table(name = "t_childvaccinedetail1", schema = "db_iemr", catalog = "") @@ -17,6 +18,8 @@ public class ChildVaccination { private long id; private Long beneficiaryRegId; private Long benVisitId; + + private Long ProviderServiceMapID; private String defaultReceivingAge; private Integer vaccineId; private String vaccineName; @@ -314,4 +317,14 @@ public String getReservedForChange() { public void setReservedForChange(String reservedForChange) { this.reservedForChange = reservedForChange; } + + @Basic + @Column(name = "ProviderServiceMapID") + public Long getProviderServiceMapID() { + return ProviderServiceMapID; + } + + public void setProviderServiceMapID(Long providerServiceMapID) { + ProviderServiceMapID = providerServiceMapID; + } } diff --git a/src/main/java/com/iemr/flw/domain/iemr/ChronicDiseaseVisitEntity.java b/src/main/java/com/iemr/flw/domain/iemr/ChronicDiseaseVisitEntity.java new file mode 100644 index 00000000..10689cff --- /dev/null +++ b/src/main/java/com/iemr/flw/domain/iemr/ChronicDiseaseVisitEntity.java @@ -0,0 +1,75 @@ +package com.iemr.flw.domain.iemr; + +import jakarta.persistence.*; +import lombok.Data; +import java.time.LocalDate; +import java.time.LocalDateTime; + +@Entity +@Table(name = "cdtf_visit_details") +@Data +public class ChronicDiseaseVisitEntity { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(name = "beneficiary_id") + private Long benId; + + @Column(name = "household_id") + private Long hhId; + + @Column(name = "form_id") + private String formId; + + @Column(name = "version") + private Integer version; + + @Column(name = "visit_no") + private Integer visitNo; + + @Column(name = "follow_up_no") + private Integer followUpNo; + + @Column(name = "diagnosis_codes", columnDefinition = "TEXT") + private String diagnosisCodes; + + @Column(name = "treatment_start_date") + private LocalDate treatmentStartDate; + + @Column(name = "follow_up_date") + private LocalDate followUpDate; + + @Column(name = "form_data_json", columnDefinition = "JSON") + private String formDataJson; + + @Column(name = "user_id") + private Integer userID; + + @Column(name = "created_by") + private String createdBy; + + @Column(name = "created_date") + private LocalDateTime createdDate; + + @Column(name = "updated_by") + private Integer updatedBy; + + @Column(name = "updated_date") + private LocalDateTime updatedDate; + + // 🔹 Auto timestamps + @PrePersist + protected void onCreate() { + this.createdDate = LocalDateTime.now(); + this.updatedDate = LocalDateTime.now(); + } + + @PreUpdate + protected void onUpdate() { + this.updatedDate = LocalDateTime.now(); + } + + +} diff --git a/src/main/java/com/iemr/flw/domain/iemr/DeliveryOutcome.java b/src/main/java/com/iemr/flw/domain/iemr/DeliveryOutcome.java index 6050fab5..d3a1b56c 100644 --- a/src/main/java/com/iemr/flw/domain/iemr/DeliveryOutcome.java +++ b/src/main/java/com/iemr/flw/domain/iemr/DeliveryOutcome.java @@ -17,7 +17,6 @@ public class DeliveryOutcome { @Column(name = "ben_id") private Long benId; - @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "MMM dd, yyyy, h:mm:ss a") @Column(name = "delivery_date") private Timestamp dateOfDelivery; @@ -54,7 +53,6 @@ public class DeliveryOutcome { @Column(name = "still_birth") private Integer stillBirth; - @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "MMM dd, yyyy, h:mm:ss a") @Column(name = "discharge_date") private Timestamp dateOfDischarge; diff --git a/src/main/java/com/iemr/flw/domain/iemr/FilariasisCampaign.java b/src/main/java/com/iemr/flw/domain/iemr/FilariasisCampaign.java new file mode 100644 index 00000000..2f0c12ad --- /dev/null +++ b/src/main/java/com/iemr/flw/domain/iemr/FilariasisCampaign.java @@ -0,0 +1,66 @@ +package com.iemr.flw.domain.iemr; + +import jakarta.persistence.*; +import lombok.Data; + +import java.time.LocalDate; +import java.time.LocalDateTime; + +@Entity +@Table(name = "campaign_filariasis_mda",schema = "db_iemr") +@Data +public class FilariasisCampaign { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(name = "start_date", nullable = false) + private LocalDate startDate; + + @Column(name = "end_date", nullable = false) + private LocalDate endDate; + + @Column(name = "user_id", nullable = false) + private Integer userId; + + @Column(name = "number_of_families", nullable = false) + private Integer numberOfFamilies = 0; + + @Column(name = "number_of_individuals", nullable = false) + private Integer numberOfIndividuals = 0; + + /** + * Store JSON array like ["img1.jpg","img2.jpg"] + * MySQL JSON column + */ + @Column(name = "mda_photos", columnDefinition = "json") + private String campaignPhotos; + + @Column(name = "created_by", length = 200) + private String createdBy; + + @Column(name = "updated_by", length = 200) + private String updatedBy; + + @Column(name = "created_date", updatable = false) + private LocalDateTime createdDate; + + @Column(name = "updated_date") + private LocalDateTime updatedDate; + + /* ---------- Auto timestamps ---------- */ + + @PrePersist + protected void onCreate() { + this.createdDate = LocalDateTime.now(); + this.updatedDate = LocalDateTime.now(); + } + + @PreUpdate + protected void onUpdate() { + this.updatedDate = LocalDateTime.now(); + } + + +} diff --git a/src/main/java/com/iemr/flw/domain/iemr/HbycChildVisit.java b/src/main/java/com/iemr/flw/domain/iemr/HbycChildVisit.java index bcaeabc4..205716cc 100644 --- a/src/main/java/com/iemr/flw/domain/iemr/HbycChildVisit.java +++ b/src/main/java/com/iemr/flw/domain/iemr/HbycChildVisit.java @@ -53,7 +53,7 @@ public class HbycChildVisit { private String other_place_of_death; @Column(name = "baby_weight") - private BigDecimal baby_weight; // 0.5 - 7.0 + private Integer baby_weight; // 0.5 - 7.0 @Column(name = "is_child_sick") private Boolean is_child_sick; diff --git a/src/main/java/com/iemr/flw/domain/iemr/IRSRound.java b/src/main/java/com/iemr/flw/domain/iemr/IRSRound.java index 0f078f5f..6f3143e0 100644 --- a/src/main/java/com/iemr/flw/domain/iemr/IRSRound.java +++ b/src/main/java/com/iemr/flw/domain/iemr/IRSRound.java @@ -28,4 +28,10 @@ public class IRSRound { @Column(name = "householdId") private Long householdId; + @Column(name = "created_by") + private String createdBy; + + @Column(name = "user_id") + private Integer userId; + } \ No newline at end of file diff --git a/src/main/java/com/iemr/flw/domain/iemr/IncentiveActivity.java b/src/main/java/com/iemr/flw/domain/iemr/IncentiveActivity.java index b5c039f5..a62d73b9 100644 --- a/src/main/java/com/iemr/flw/domain/iemr/IncentiveActivity.java +++ b/src/main/java/com/iemr/flw/domain/iemr/IncentiveActivity.java @@ -36,7 +36,6 @@ public class IncentiveActivity { @Column(name = "group_name") private String group; - @Column(name = "fmr_code") private String fmrCode; @@ -58,5 +57,12 @@ public class IncentiveActivity { @Column(name = "is_deleted") private Boolean isDeleted; + @Column(name = "state_activity_code") + private Integer stateActivityCode; + + @Column(name = "is_default_activity") + private Boolean isDefaultActivity; + + } diff --git a/src/main/java/com/iemr/flw/domain/iemr/IncentiveActivityRecord.java b/src/main/java/com/iemr/flw/domain/iemr/IncentiveActivityRecord.java index 5b921bb4..b6d193e6 100644 --- a/src/main/java/com/iemr/flw/domain/iemr/IncentiveActivityRecord.java +++ b/src/main/java/com/iemr/flw/domain/iemr/IncentiveActivityRecord.java @@ -3,6 +3,8 @@ import lombok.Data; import jakarta.persistence.*; + +import javax.management.MXBean; import java.sql.Timestamp; @Entity @@ -46,4 +48,43 @@ public class IncentiveActivityRecord { @Column(name = "updated_by") private String updatedBy; + + @Column(name = "is_eligible",columnDefinition = "tinyint(1) default 1") + private Boolean isEligible=true; + + @Column(name = "is_default_activity",columnDefinition = "tinyint(1) default 0") + private Boolean isDefaultActivity = false; + + @Column(name = "approval_status",columnDefinition = "int default 102") + Integer approvalStatus = 102; + + @Column(name = "verifiedBy_userId") + private Integer verifiedByUserId; + + @Column(name = "verifiedBy_userName") + private String verifiedByUserName; + + @Column(name = "reason") + private String reason; + + @Column(name = "other_reason") + private String otherReason; + + @Column(name = "is_claimed",columnDefinition = "tinyint(1) default 0") + private Boolean isClaimed = false; + + @Column(name = "approval_date") + private Timestamp approvalDate; + + @Column(name = "calimed_date") + private Timestamp calimedDate; + + @Transient + private String supervisorRole; + + @Transient + private String activityDec; + + @Transient + private String groupName; } diff --git a/src/main/java/com/iemr/flw/domain/iemr/IncentivePendingActivity.java b/src/main/java/com/iemr/flw/domain/iemr/IncentivePendingActivity.java new file mode 100644 index 00000000..3f35dc59 --- /dev/null +++ b/src/main/java/com/iemr/flw/domain/iemr/IncentivePendingActivity.java @@ -0,0 +1,39 @@ +package com.iemr.flw.domain.iemr; + +import java.util.Date; + +import jakarta.persistence.*; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Entity +@Table(name = "incentive_pending_activity", schema = "db_iemr") +@Data +@NoArgsConstructor +@AllArgsConstructor +public class IncentivePendingActivity { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id") + private Long id; + + @Column(name = "activity_id", nullable = false) + private Long activityId; + + @Column(name = "record_id", nullable = false) + private Long recordId; + + @Column(name = "m_incentive_id") + private Long mincentiveId; + + @Column(name = "user_id", nullable = false) + private Integer userId; + + @Column(name = "created_date") + private Date createdDate; + + @Column(name = "updated_date") + private Date updatedDate; +} diff --git a/src/main/java/com/iemr/flw/domain/iemr/MaaMeeting.java b/src/main/java/com/iemr/flw/domain/iemr/MaaMeeting.java index 2bfb2970..0e843714 100644 --- a/src/main/java/com/iemr/flw/domain/iemr/MaaMeeting.java +++ b/src/main/java/com/iemr/flw/domain/iemr/MaaMeeting.java @@ -5,6 +5,7 @@ import java.sql.Timestamp; import java.time.LocalDate; +import java.util.List; @Entity @Data @@ -39,6 +40,18 @@ public class MaaMeeting { @Column(name = "meeting_images", columnDefinition = "LONGTEXT") private String meetingImagesJson; + @Column(name = "village_name") + private String villageName; + + @Column(name = "no_of_pragnent_women") + private Integer noOfPragnentWomen; + + @Column(name = "no_of_lacting_mother") + private Integer noOfLactingMother; + + @Column(name = "mitanin_activity_checkList") + private String mitaninActivityCheckList; + @Column(name = "created_by") private String createdBy; } diff --git a/src/main/java/com/iemr/flw/domain/iemr/MosquitoNetEntity.java b/src/main/java/com/iemr/flw/domain/iemr/MosquitoNetEntity.java index 9444d1af..1f57d23a 100644 --- a/src/main/java/com/iemr/flw/domain/iemr/MosquitoNetEntity.java +++ b/src/main/java/com/iemr/flw/domain/iemr/MosquitoNetEntity.java @@ -9,21 +9,22 @@ @Data @Table(name = "i_mobilization_mosquito_net",schema = "db_iemr") public class MosquitoNetEntity { - @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; + @Column(name = "beneficiary_id") private Long beneficiaryId; + @Column(name = "household_id") private Long houseHoldId; + @Column(name = "visit_date") private LocalDate visitDate; @Column(name = "user_name") private String userName; - @Column(name = "user_id") private Integer userId; diff --git a/src/main/java/com/iemr/flw/domain/iemr/Notification.java b/src/main/java/com/iemr/flw/domain/iemr/Notification.java new file mode 100644 index 00000000..b2f4ba71 --- /dev/null +++ b/src/main/java/com/iemr/flw/domain/iemr/Notification.java @@ -0,0 +1,67 @@ +package com.iemr.flw.domain.iemr; + +import jakarta.persistence.*; +import lombok.Data; + +import java.util.Date; + +/** + * Persisted record of a notification that was sent (or attempted) to a user. + * Backs the "notification list with unread count" feature. + */ +@Data +@Entity +@Table(name = "notification") +public class Notification { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id") + private Long id; + + @Column(name = "app_type") + private String appType; + + @Column(name = "sender_user_id") + private Integer senderUserId; + + @Column(name = "receiver_user_id", nullable = false) + private Integer receiverUserId; + + @Column(name = "title") + private String title; + + @Column(name = "body") + private String body; + + @Column(name = "notification_type") + private String notificationType; + + @Column(name = "nav_id") + private String navId; + + @Column(name = "redirect") + private String redirect; + + @Column(name = "priority") + private String priority; + + @Column(name = "is_read", nullable = false) + private boolean isRead = false; + + @Temporal(TemporalType.TIMESTAMP) + @Column(name = "created_date", nullable = false, updatable = false) + private Date createdDate; + + @Temporal(TemporalType.TIMESTAMP) + @Column(name = "read_date") + private Date readDate; + + @PrePersist + protected void onCreate() { + this.createdDate = new Date(); + if (!isRead) { + this.readDate = null; + } + } +} \ No newline at end of file diff --git a/src/main/java/com/iemr/flw/domain/iemr/PHCReviewForm.java b/src/main/java/com/iemr/flw/domain/iemr/PHCReviewForm.java index 50ec3375..4dea0ce7 100644 --- a/src/main/java/com/iemr/flw/domain/iemr/PHCReviewForm.java +++ b/src/main/java/com/iemr/flw/domain/iemr/PHCReviewForm.java @@ -42,6 +42,19 @@ public class PHCReviewForm { @Column(name = "created_date", updatable = false) private Timestamp createdDate; + @Column(name = "village_name ") + private String villageName; + + @Column(name = "mitanin_history") + private String mitaninHistory; + + @Column(name = "mitanin_activity_checkList") + private String mitaninActivityCheckList; + + @Column(name = "place_Id") + private Integer placeId; + @Column(name = "form_type") private String formType; + } \ No newline at end of file diff --git a/src/main/java/com/iemr/flw/domain/iemr/PulsePolioCampaign.java b/src/main/java/com/iemr/flw/domain/iemr/PulsePolioCampaign.java new file mode 100644 index 00000000..ddf7b8b6 --- /dev/null +++ b/src/main/java/com/iemr/flw/domain/iemr/PulsePolioCampaign.java @@ -0,0 +1,60 @@ +package com.iemr.flw.domain.iemr; + +import jakarta.persistence.*; +import lombok.Data; + +import java.time.LocalDate; +import java.time.LocalDateTime; + +@Entity +@Table(name = "campaign_pulse_polio",schema = "db_iemr") +@Data +public class PulsePolioCampaign { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(name = "start_date", nullable = false) + private LocalDate startDate; + + @Column(name = "end_date", nullable = false) + private LocalDate endDate; + + @Column(name = "user_id", nullable = false) + private Integer userId; + + @Column(name = "number_of_children", nullable = false) + private Integer numberOfChildren = 0; + + + @Column(name = "campaign_photos",columnDefinition = "LONGTEXT") + private String campaignPhotos; + + @Column(name = "created_by", length = 200) + private String createdBy; + + @Column(name = "updated_by", length = 200) + private String updatedBy; + + @Column(name = "created_date", updatable = false) + private LocalDateTime createdDate; + + @Column(name = "updated_date") + private LocalDateTime updatedDate; + + /* ---------- Auto timestamps ---------- */ + + @PrePersist + protected void onCreate() { + this.createdDate = LocalDateTime.now(); + this.updatedDate = LocalDateTime.now(); + } + + @PreUpdate + protected void onUpdate() { + this.updatedDate = LocalDateTime.now(); + } + + +} diff --git a/src/main/java/com/iemr/flw/domain/iemr/ScreeningKalaAzar.java b/src/main/java/com/iemr/flw/domain/iemr/ScreeningKalaAzar.java index eb8b0107..d6bcd7b6 100644 --- a/src/main/java/com/iemr/flw/domain/iemr/ScreeningKalaAzar.java +++ b/src/main/java/com/iemr/flw/domain/iemr/ScreeningKalaAzar.java @@ -45,7 +45,7 @@ public class ScreeningKalaAzar { @Column(name = "house_hold_details_Id",nullable = false) private Long houseHoldDetailsId; - @Column(name = "userID") + @Column(name = "user_id") private Integer userId; @Temporal(TemporalType.DATE) @@ -97,7 +97,7 @@ public class ScreeningKalaAzar { @Column(name = "created_by") private String createdBy; - @Column(name = "diseaseTypeID") + @Column(name = "disease_type_id") private Integer diseaseTypeId; @Column(name = "refer_to_name") diff --git a/src/main/java/com/iemr/flw/domain/iemr/ScreeningLeprosy.java b/src/main/java/com/iemr/flw/domain/iemr/ScreeningLeprosy.java index 225a1dfc..6a485eef 100644 --- a/src/main/java/com/iemr/flw/domain/iemr/ScreeningLeprosy.java +++ b/src/main/java/com/iemr/flw/domain/iemr/ScreeningLeprosy.java @@ -26,7 +26,6 @@ import jakarta.persistence.*; import lombok.Data; - import java.sql.Timestamp; import java.util.Date; @@ -84,25 +83,25 @@ public class ScreeningLeprosy { @Column(name = "beneficiary_statusId") private Integer beneficiaryStatusId; - @Column(name = "beneficiary_status", length = 50) + @Column(name = "beneficiary_status") private String beneficiaryStatus; @Column(name = "date_of_death") private Date dateOfDeath; - @Column(name = "place_of_death", length = 50) + @Column(name = "place_of_death") private String placeOfDeath; @Column(name = "other_place_of_death", columnDefinition = "TEXT") private String otherPlaceOfDeath; - @Column(name = "reason_for_death", length = 50) + @Column(name = "reason_for_death") private String reasonForDeath; @Column(name = "other_reason_for_death", columnDefinition = "TEXT") private String otherReasonForDeath; - @Column(name = "leprosy_symptoms", length = 255) + @Column(name = "leprosy_symptoms") private String leprosySymptoms; @Column(name = "leprosy_symptoms_position") @@ -114,7 +113,7 @@ public class ScreeningLeprosy { @Column(name = "current_visit_number") private Integer currentVisitNumber; - @Column(name = "visit_label", length = 50) + @Column(name = "visit_label") private String visitLabel; @Column(name = "visit_number") @@ -123,7 +122,7 @@ public class ScreeningLeprosy { @Column(name = "is_confirmed") private Boolean isConfirmed; - @Column(name = "leprosy_state", length = 50) + @Column(name = "leprosy_state", length = 255) private String leprosyState; @Temporal(TemporalType.DATE) @@ -143,6 +142,78 @@ public class ScreeningLeprosy { @Column(name = "treatment_status", length = 100) private String treatmentStatus; + @Column(name = "recurrent_ulceration_id") + private Integer recurrentUlcerationId; + + @Column(name = "recurrent_tingling_id") + private Integer recurrentTinglingId; + + @Column(name = "hypopigmented_patch_id") + private Integer hypopigmentedPatchId; + + @Column(name = "thickened_skin_id") + private Integer thickenedSkinId; + + @Column(name = "skin_nodules_id") + private Integer skinNodulesId; + + @Column(name = "skin_patch_discoloration_id") + private Integer skinPatchDiscolorationId; + + @Column(name = "recurrent_numbness_id") + private Integer recurrentNumbnessId; + + @Column(name = "clawing_fingers_id") + private Integer clawingFingersId; + + @Column(name = "tingling_numbness_extremities_id") + private Integer tinglingNumbnessExtremitiesId; + + @Column(name = "inability_close_eyelid_id") + private Integer inabilityCloseEyelidId; + + @Column(name = "difficulty_holding_objects_id") + private Integer difficultyHoldingObjectsId; + + @Column(name = "weakness_feet_id") + private Integer weaknessFeetId; + + @Column(name = "recurrent_ulceration") + private String recurrentUlceration; + + @Column(name = "recurrent_tingling") + private String recurrentTingling; + + @Column(name = "hypopigmented_patch") + private String hypopigmentedPatch; + + @Column(name = "thickened_skin") + private String thickenedSkin; + + @Column(name = "skin_nodules") + private String skinNodules; + + @Column(name = "skin_patch_discoloration") + private String skinPatchDiscoloration; + + @Column(name = "recurrent_numbness") + private String recurrentNumbness; + + @Column(name = "clawing_fingers") + private String clawingFingers; + + @Column(name = "tingling_numbness_extremities") + private String tinglingNumbnessExtremities; + + @Column(name = "inability_close_eyelid") + private String inabilityCloseEyelid; + + @Column(name = "difficulty_holding_objects") + private String difficultyHoldingObjects; + + @Column(name = "weakness_feet") + private String weaknessFeet; + @Column(name = "CreatedBy", length = 100) private String createdBy; diff --git a/src/main/java/com/iemr/flw/domain/iemr/TBConfirmedCase.java b/src/main/java/com/iemr/flw/domain/iemr/TBConfirmedCase.java new file mode 100644 index 00000000..8310eb65 --- /dev/null +++ b/src/main/java/com/iemr/flw/domain/iemr/TBConfirmedCase.java @@ -0,0 +1,74 @@ +package com.iemr.flw.domain.iemr; + +import jakarta.persistence.*; +import lombok.Data; + +import java.sql.Timestamp; +import java.time.LocalDate; + +@Entity +@Data +@Table(name = "tb_confirmed_cases",schema = "db_iemr") +public class TBConfirmedCase { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Integer id; + + @Column(name = "ben_id", nullable = false) + private Long benId; + + @Column(name = "user_id",nullable = false) + private Integer userId; + + + @Column(name = "regimen_type") + private String regimenType; + + @Column(name = "treatment_start_date") + private LocalDate treatmentStartDate; + + @Column(name = "expected_treatment_completion_date") + private LocalDate expectedTreatmentCompletionDate; + + @Column(name = "follow_up_date") + private LocalDate followUpDate; + + @Column(name = "monthly_follow_up_done") + private String monthlyFollowUpDone; + + @Column(name = "adherence_to_medicines") + private String adherenceToMedicines; + + @Column(name = "any_discomfort") + private Boolean anyDiscomfort; + + @Column(name = "treatment_completed") + private Boolean treatmentCompleted; + + @Column(name = "actual_treatment_completion_date") + private LocalDate actualTreatmentCompletionDate; + + @Column(name = "treatment_outcome") + private String treatmentOutcome; + + @Column(name = "date_of_death") + private LocalDate dateOfDeath; + + @Column(name = "place_of_death") + private String placeOfDeath; + + @Column(name = "reason_for_death") + private String reasonForDeath = "Tuberculosis"; + + @Column(name = "reason_for_not_completing") + private String reasonForNotCompleting; + + @Column(name = "created_at") + private LocalDate createdAt = LocalDate.now(); + + @Column(name = "updated_at") + private LocalDate updatedAt; + + +} diff --git a/src/main/java/com/iemr/flw/domain/iemr/TBConfirmedCaseDTO.java b/src/main/java/com/iemr/flw/domain/iemr/TBConfirmedCaseDTO.java new file mode 100644 index 00000000..180c3bbe --- /dev/null +++ b/src/main/java/com/iemr/flw/domain/iemr/TBConfirmedCaseDTO.java @@ -0,0 +1,29 @@ +package com.iemr.flw.domain.iemr; + +import jakarta.persistence.*; +import lombok.Data; + +import java.time.LocalDate; + +@Data +public class TBConfirmedCaseDTO { + + private Long benId; + private Integer userId; + private Integer suspectedTbId; + private String regimenType; + private LocalDate treatmentStartDate; + private LocalDate expectedTreatmentCompletionDate; + private LocalDate followUpDate; + private String monthlyFollowUpDone; + private String adherenceToMedicines; + private Boolean anyDiscomfort; + private Boolean treatmentCompleted; + private LocalDate actualTreatmentCompletionDate; + private String treatmentOutcome; + private LocalDate dateOfDeath; + private String placeOfDeath; + private String reasonForDeath ; + private String reasonForNotCompleting; + +} diff --git a/src/main/java/com/iemr/flw/domain/iemr/TBScreening.java b/src/main/java/com/iemr/flw/domain/iemr/TBScreening.java index 9bf7ea8d..6789f9bf 100644 --- a/src/main/java/com/iemr/flw/domain/iemr/TBScreening.java +++ b/src/main/java/com/iemr/flw/domain/iemr/TBScreening.java @@ -46,4 +46,37 @@ public class TBScreening { @Column(name = "family_check") private Boolean familySufferingFromTB; + + @Column(name = "rise_of_fever") + private Boolean riseOfFever; + + @Column(name = "loss_of_appetite") + private Boolean lossOfAppetite; + + @Column(name = "age") + private Boolean age; + + @Column(name = "diabetic") + private Boolean diabetic; + + @Column(name = "tobacco_user") + private Boolean tobaccoUser; + + @Column(name = "bmi") + private Boolean bmi; + + @Column(name = "contact_with_tb_patient") + private Boolean contactWithTBPatient; + + @Column(name = "history_of_tb_in_last_five_yrs") + private Boolean historyOfTBInLastFiveYrs; + + @Column(name = "sympotomatic") + private String sympotomatic; + + @Column(name = "asymptomatic") + private String asymptomatic; + + @Column(name = "recommandate_test") + private String recommandateTest; } diff --git a/src/main/java/com/iemr/flw/domain/iemr/TBSuspected.java b/src/main/java/com/iemr/flw/domain/iemr/TBSuspected.java index 7cb945a8..2de2baf8 100644 --- a/src/main/java/com/iemr/flw/domain/iemr/TBSuspected.java +++ b/src/main/java/com/iemr/flw/domain/iemr/TBSuspected.java @@ -40,4 +40,40 @@ public class TBSuspected { @Column(name = "followups") private String followUps; + + // Visit Information + @Column(name = "visit_label") + private String visitLabel; + + @Column(name = "type_of_tb_case") + private String typeOfTBCase; + + @Column(name = "reason_for_suspicion", length = 500) + private String reasonForSuspicion; + + + // Chest X-Ray + @Column(name = "is_chest_xray_done") + private Boolean isChestXRayDone; + + @Column(name = "chest_xray_result", length = 100) + private String chestXRayResult; + + // Referral & Confirmation + @Column(name = "referral_facility", length = 200) + private String referralFacility; + + @Column(name = "is_tb_confirmed") + private Boolean isTBConfirmed; + + @Column(name = "is_drtb_confirmed") + private Boolean isDRTBConfirmed; + + @Column(name = "provider_service_map_id") + private Integer providerServiceMapId; + + @Column(name = "is_confirmed") + private Boolean isConfirmed; + + } diff --git a/src/main/java/com/iemr/flw/domain/iemr/M_User.java b/src/main/java/com/iemr/flw/domain/iemr/User.java similarity index 91% rename from src/main/java/com/iemr/flw/domain/iemr/M_User.java rename to src/main/java/com/iemr/flw/domain/iemr/User.java index 0187a613..d422f926 100644 --- a/src/main/java/com/iemr/flw/domain/iemr/M_User.java +++ b/src/main/java/com/iemr/flw/domain/iemr/User.java @@ -1,7 +1,6 @@ package com.iemr.flw.domain.iemr; import java.sql.Timestamp; -import java.time.LocalDate; import com.fasterxml.jackson.annotation.JsonProperty; import com.google.gson.annotations.Expose; @@ -14,18 +13,10 @@ import jakarta.persistence.Table; import lombok.Data; -import com.google.gson.annotations.Expose; -import jakarta.persistence.*; -import lombok.Data; -import java.sql.Date; -import java.sql.Timestamp; -import java.time.LocalDate; - @Entity -@Table(name = "m_User", schema = "db_iemr") +@Table(name = "m_user", schema = "db_iemr") @Data -public class M_User { - +public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Expose @@ -144,11 +135,11 @@ public class M_User { @Column(name = "failed_attempt", insertable = false) private Integer failedAttempt; - public M_User() { + public User() { // TODO Auto-generated constructor stub } - public M_User(Integer userID, String userName) { + public User(Integer userID, String userName) { // TODO Auto-generated constructor stub this.userID = userID; this.userName = userName; diff --git a/src/main/java/com/iemr/flw/domain/iemr/UserFcmTokenData.java b/src/main/java/com/iemr/flw/domain/iemr/UserFcmTokenData.java new file mode 100644 index 00000000..dd56c5b2 --- /dev/null +++ b/src/main/java/com/iemr/flw/domain/iemr/UserFcmTokenData.java @@ -0,0 +1,23 @@ +package com.iemr.flw.domain.iemr; + +import jakarta.persistence.*; +import lombok.Data; + +import java.sql.Timestamp; + +import static jakarta.persistence.GenerationType.IDENTITY; + +@Entity +@Table(name = "user_fcm_tokens", schema = "db_iemr") +@Data +public class UserFcmTokenData { + @Id + @GeneratedValue(strategy = IDENTITY) + private int id; + @Column(name = "user_id") + private Integer userId; + @Column(name = "token") + private String token; + @Column(name = "updated_at") + private Timestamp updatedAt; +} diff --git a/src/main/java/com/iemr/flw/domain/iemr/UwinSession.java b/src/main/java/com/iemr/flw/domain/iemr/UwinSession.java index f995a71d..9ab6d5cb 100644 --- a/src/main/java/com/iemr/flw/domain/iemr/UwinSession.java +++ b/src/main/java/com/iemr/flw/domain/iemr/UwinSession.java @@ -23,6 +23,9 @@ public class UwinSession { @Column(name = "date", nullable = false) private Timestamp date; + @Column(name = "session_date", nullable = false) + private Timestamp sessionDate; + @Column(name = "place", nullable = false) private String place; diff --git a/src/main/java/com/iemr/flw/domain/iemr/VHNDForm.java b/src/main/java/com/iemr/flw/domain/iemr/VHNDForm.java index 488b33c4..758cc449 100644 --- a/src/main/java/com/iemr/flw/domain/iemr/VHNDForm.java +++ b/src/main/java/com/iemr/flw/domain/iemr/VHNDForm.java @@ -37,6 +37,39 @@ public class VHNDForm { @Column(name = "form_type") private String formType; + @Column(name = "vhnd_place_id") + private Integer vhndPlaceId; + + @Column(name = "pregnant_women_anc") + private String pregnantWomenAnc; + + @Column(name = "lactating_mothers_pnc") + private String lactatingMothersPnc; + + @Column(name = "children_immunization") + private String childrenImmunization; + + @Column(name = "select_all_education") + private Boolean selectAllEducation; + + @Column(name = "knowledge_balanced_diet") + private String knowledgeBalancedDiet; + + @Column(name = "care_during_pregnancy") + private String careDuringPregnancy; + + @Column(name = "importance_breastfeeding") + private String importanceBreastfeeding; + + @Column(name = "complementary_feeding") + private String complementaryFeeding; + + @Column(name = "hygiene_sanitation") + private String hygieneSanitation; + + @Column(name = "family_planning_healthcare") + private String familyPlanningHealthcare; + @Column(name = "created_by") private String createdBy; diff --git a/src/main/java/com/iemr/flw/domain/iemr/VhncForm.java b/src/main/java/com/iemr/flw/domain/iemr/VhncForm.java index 5b53b27e..858554da 100644 --- a/src/main/java/com/iemr/flw/domain/iemr/VhncForm.java +++ b/src/main/java/com/iemr/flw/domain/iemr/VhncForm.java @@ -1,6 +1,7 @@ package com.iemr.flw.domain.iemr; import jakarta.persistence.*; +import jakarta.xml.bind.annotation.XmlAccessorOrder; import lombok.Data; import org.hibernate.annotations.CreationTimestamp; @@ -43,4 +44,26 @@ public class VhncForm { @Column(name = "form_type") private String formType; + + @Column(name = "village_name") + private String villageName; + + @Column(name = "anm") + private Integer anm; + + @Column(name = "aww") + private Integer aww; + + @Column(name = "no_of_pragnent_women") + private Integer noOfPragnentWoment; + + @Column(name = "no_of_lacting_mother") + private Integer noOfLactingMother; + + @Column(name = "no_of_committee") + private Integer noOfCommittee; + + @Column(name = "followup_previous") + private Boolean followupPrevius; + } \ No newline at end of file diff --git a/src/main/java/com/iemr/flw/dto/abhaBeneficiary/AbhaBeneficiaryDTO.java b/src/main/java/com/iemr/flw/dto/abhaBeneficiary/AbhaBeneficiaryDTO.java new file mode 100644 index 00000000..dab9dec3 --- /dev/null +++ b/src/main/java/com/iemr/flw/dto/abhaBeneficiary/AbhaBeneficiaryDTO.java @@ -0,0 +1,65 @@ +package com.iemr.flw.dto.abhaBeneficiary; + +import com.fasterxml.jackson.annotation.JsonAlias; +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Data; + +@Data +public class AbhaBeneficiaryDTO { + @JsonProperty("personName") + private String personName; + + private String firstName; + + private String lastName; + + @JsonProperty("age") + private String age; + + @JsonProperty("address") + private String address; + + @JsonProperty("district") + private String district; + + @JsonProperty("mobileNo") + private String mobileNo; + + @JsonProperty("block") + private String block; + + @JsonProperty("cardNo") + private String cardNo; + + @JsonProperty("villagename") + private String villagename; + + @JsonProperty("gender") + private String gender; + + @JsonProperty("district_Code") + private String districtCode; + + @JsonProperty("block_Code") + private String blockCode; + + @JsonProperty("village_Code") + private String villageCode; + + @JsonProperty("rural_Urban") + private String ruralUrban; + + @JsonProperty("abhaId") + private String abhaId; + + @JsonProperty("vvs") + private String vvs; + + @JsonProperty("familyid") + private String familyid; + + @JsonProperty("dob") + @JsonAlias("dob_secc") + private String dob; + +} diff --git a/src/main/java/com/iemr/flw/dto/identity/GetBenRequestHandler.java b/src/main/java/com/iemr/flw/dto/identity/GetBenRequestHandler.java index 00e452c5..d1a654ec 100644 --- a/src/main/java/com/iemr/flw/dto/identity/GetBenRequestHandler.java +++ b/src/main/java/com/iemr/flw/dto/identity/GetBenRequestHandler.java @@ -12,6 +12,36 @@ public class GetBenRequestHandler { private Integer ashaId; + private Long activityId; + + private Integer month; + + private Integer year; + + public Integer getMonth() { + return month; + } + + public void setMonth(Integer month) { + this.month = month; + } + + public Integer getYear() { + return year; + } + + public void setYear(Integer year) { + this.year = year; + } + + public Long getActivityId() { + return activityId; + } + + public void setActivityId(Long activityId) { + this.activityId = activityId; + } + public Integer getAshaId() { return ashaId; } diff --git a/src/main/java/com/iemr/flw/dto/iemr/ANCVisitDTO.java b/src/main/java/com/iemr/flw/dto/iemr/ANCVisitDTO.java index f028655f..b4cf0033 100644 --- a/src/main/java/com/iemr/flw/dto/iemr/ANCVisitDTO.java +++ b/src/main/java/com/iemr/flw/dto/iemr/ANCVisitDTO.java @@ -63,6 +63,8 @@ public class ANCVisitDTO { private Timestamp visitDate; private Timestamp dateSterilisation; private Boolean isYesOrNo; + private String placeOfAnc; + private Integer placeOfAncId; } diff --git a/src/main/java/com/iemr/flw/dto/iemr/AbhaRequestDTO.java b/src/main/java/com/iemr/flw/dto/iemr/AbhaRequestDTO.java new file mode 100644 index 00000000..9ec31c6f --- /dev/null +++ b/src/main/java/com/iemr/flw/dto/iemr/AbhaRequestDTO.java @@ -0,0 +1,10 @@ +package com.iemr.flw.dto.iemr; + +import lombok.Data; + +@Data +public class AbhaRequestDTO { + private String cardNo; + private Long houseHoldId; + +} diff --git a/src/main/java/com/iemr/flw/dto/iemr/AncCounsellingCareDTO.java b/src/main/java/com/iemr/flw/dto/iemr/AncCounsellingCareDTO.java new file mode 100644 index 00000000..76aff81d --- /dev/null +++ b/src/main/java/com/iemr/flw/dto/iemr/AncCounsellingCareDTO.java @@ -0,0 +1,15 @@ +package com.iemr.flw.dto.iemr; + +import lombok.Data; + +@Data +public class AncCounsellingCareDTO { + private String formId; + private Long beneficiaryId; + private String visitDate; + private AncCounsellingCareListDTO fields; + +} + + + diff --git a/src/main/java/com/iemr/flw/dto/iemr/AncCounsellingCareListDTO.java b/src/main/java/com/iemr/flw/dto/iemr/AncCounsellingCareListDTO.java new file mode 100644 index 00000000..981e34e2 --- /dev/null +++ b/src/main/java/com/iemr/flw/dto/iemr/AncCounsellingCareListDTO.java @@ -0,0 +1,79 @@ +package com.iemr.flw.dto.iemr; + +import com.google.gson.annotations.SerializedName; +import lombok.Data; + +@Data +public class AncCounsellingCareListDTO { + + @SerializedName("home_visit_date") + private String homeVisitDate; + + @SerializedName("select_all") + private String selectAll; + + @SerializedName("swelling") + private String swelling; + + @SerializedName("high_bp") + private String highBp; + + @SerializedName("convulsions") + private String convulsions; + + @SerializedName("anemia") + private String anemia; + + @SerializedName("reduced_fetal_movement") + private String reducedFetalMovement; + + @SerializedName("age_risk") + private String ageRisk; + + @SerializedName("child_gap") + private String childGap; + + @SerializedName("short_height") + private String shortHeight; + + @SerializedName("pre_preg_weight") + private String prePregWeight; + + @SerializedName("bleeding") + private String bleeding; + + @SerializedName("miscarriage_history") + private String miscarriageHistory; + + @SerializedName("four_plus_delivery") + private String fourPlusDelivery; + + @SerializedName("first_delivery") + private String firstDelivery; + + @SerializedName("twin_pregnancy") + private String twinPregnancy; + + @SerializedName("c_section_history") + private String cSectionHistory; + + @SerializedName("pre_existing_disease") + private String preExistingDisease; + + @SerializedName("fever_malaria") + private String feverMalaria; + + @SerializedName("jaundice") + private String jaundice; + + @SerializedName("sickle_cell") + private String sickleCell; + + @SerializedName("prolonged_labor") + private String prolongedLabor; + + @SerializedName("malpresentation") + private String malpresentation; + + +} diff --git a/src/main/java/com/iemr/flw/dto/iemr/AncCounsellingCareResponseDTO.java b/src/main/java/com/iemr/flw/dto/iemr/AncCounsellingCareResponseDTO.java new file mode 100644 index 00000000..2e019558 --- /dev/null +++ b/src/main/java/com/iemr/flw/dto/iemr/AncCounsellingCareResponseDTO.java @@ -0,0 +1,17 @@ +package com.iemr.flw.dto.iemr; + +import lombok.Data; + +import java.util.Map; + +@Data +public class AncCounsellingCareResponseDTO { + private String formId; + private Long beneficiaryId; + private String visitDate; + private Map fields; // for dynamic form fields + +} + + + diff --git a/src/main/java/com/iemr/flw/dto/iemr/AshaByFacilityRequestDTO.java b/src/main/java/com/iemr/flw/dto/iemr/AshaByFacilityRequestDTO.java new file mode 100644 index 00000000..9ae46ad4 --- /dev/null +++ b/src/main/java/com/iemr/flw/dto/iemr/AshaByFacilityRequestDTO.java @@ -0,0 +1,11 @@ +package com.iemr.flw.dto.iemr; + +import lombok.Data; + +@Data +public class AshaByFacilityRequestDTO { + private Integer facilityId; + private Integer month; + private Integer year; + private Integer approvalStatus; +} diff --git a/src/main/java/com/iemr/flw/dto/iemr/CdrDTO.java b/src/main/java/com/iemr/flw/dto/iemr/CdrDTO.java index 122f652d..e39f0641 100644 --- a/src/main/java/com/iemr/flw/dto/iemr/CdrDTO.java +++ b/src/main/java/com/iemr/flw/dto/iemr/CdrDTO.java @@ -54,6 +54,8 @@ public class CdrDTO { private String deathCertImage1; private String deathCertImage2; + private String cdrImage; + private Timestamp updatedDate; diff --git a/src/main/java/com/iemr/flw/dto/iemr/ChronicDiseaseVisitDTO.java b/src/main/java/com/iemr/flw/dto/iemr/ChronicDiseaseVisitDTO.java new file mode 100644 index 00000000..2a2790ad --- /dev/null +++ b/src/main/java/com/iemr/flw/dto/iemr/ChronicDiseaseVisitDTO.java @@ -0,0 +1,44 @@ +package com.iemr.flw.dto.iemr; + +import com.fasterxml.jackson.annotation.JsonProperty; +import jakarta.persistence.Column; +import lombok.Data; + +import java.time.LocalDate; + +@Data +public class ChronicDiseaseVisitDTO { + + @JsonProperty("id") + private Long id; + + @JsonProperty("benId") + private Long benId; + + @JsonProperty("hhId") + private Long hhId; + + @JsonProperty("formId") + private String formId; + + @JsonProperty("version") + private Integer version; + + @JsonProperty("visitNo") + private Integer visitNo; + + @JsonProperty("followUpNo") + private Integer followUpNo; + + @Column(name = "followUpDate") + private LocalDate followUpDate; + + @JsonProperty("diagnosisCodes") + private String diagnosisCodes; + + @JsonProperty("treatmentStartDate") + private String treatmentStartDate; // yyyy-MM-dd + + @JsonProperty("formDataJson") + private String formDataJson; +} diff --git a/src/main/java/com/iemr/flw/dto/iemr/DeliveryOutcomeDTO.java b/src/main/java/com/iemr/flw/dto/iemr/DeliveryOutcomeDTO.java index 941721ca..5e94697a 100644 --- a/src/main/java/com/iemr/flw/dto/iemr/DeliveryOutcomeDTO.java +++ b/src/main/java/com/iemr/flw/dto/iemr/DeliveryOutcomeDTO.java @@ -10,7 +10,6 @@ public class DeliveryOutcomeDTO { private Long id; private Long benId; - @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd") private Timestamp dateOfDelivery; private String timeOfDelivery; private String placeOfDelivery; diff --git a/src/main/java/com/iemr/flw/dto/iemr/DiseaseLeprosyDTO.java b/src/main/java/com/iemr/flw/dto/iemr/DiseaseLeprosyDTO.java index c0acbe71..47489c54 100644 --- a/src/main/java/com/iemr/flw/dto/iemr/DiseaseLeprosyDTO.java +++ b/src/main/java/com/iemr/flw/dto/iemr/DiseaseLeprosyDTO.java @@ -72,4 +72,28 @@ public class DiseaseLeprosyDTO { private Timestamp createdDate; private String modifiedBy; private Timestamp lastModDate; + private Integer recurrentUlcerationId; + private Integer recurrentTinglingId; + private Integer hypopigmentedPatchId; + private Integer thickenedSkinId; + private Integer skinNodulesId; + private Integer skinPatchDiscolorationId; + private Integer recurrentNumbnessId; + private Integer clawingFingersId; + private Integer tinglingNumbnessExtremitiesId; + private Integer inabilityCloseEyelidId; + private Integer difficultyHoldingObjectsId; + private Integer weaknessFeetId; + private String recurrentUlceration; + private String recurrentTingling; + private String hypopigmentedPatch; + private String thickenedSkin; + private String skinNodules; + private String skinPatchDiscoloration; + private String recurrentNumbness; + private String clawingFingers; + private String tinglingNumbnessExtremities; + private String inabilityCloseEyelid; + private String difficultyHoldingObjects; + private String weaknessFeet; } \ No newline at end of file diff --git a/src/main/java/com/iemr/flw/dto/iemr/EyeCheckupListDTO.java b/src/main/java/com/iemr/flw/dto/iemr/EyeCheckupListDTO.java index 3480aa0b..12cba394 100644 --- a/src/main/java/com/iemr/flw/dto/iemr/EyeCheckupListDTO.java +++ b/src/main/java/com/iemr/flw/dto/iemr/EyeCheckupListDTO.java @@ -3,13 +3,16 @@ import com.google.gson.annotations.SerializedName; import lombok.Data; +import java.util.List; +import java.util.stream.Collectors; + @Data public class EyeCheckupListDTO { @SerializedName("visit_date") private String visit_date; @SerializedName("symptoms_observed") - private String symptoms_observed; + private Object symptoms_observed; @SerializedName("eye_affected") private String eye_affected; @@ -26,4 +29,16 @@ public class EyeCheckupListDTO { @SerializedName("discharge_summary_upload") private String discharge_summary_upload; + public String getSymptomsAsString() { + if (symptoms_observed == null) return null; + + if (symptoms_observed instanceof List) { + return ((List) symptoms_observed) + .stream() + .map(Object::toString) + .collect(Collectors.joining(", ")); + } + return symptoms_observed.toString(); + } + } diff --git a/src/main/java/com/iemr/flw/dto/iemr/EyeCheckupRequestDTO.java b/src/main/java/com/iemr/flw/dto/iemr/EyeCheckupRequestDTO.java index f0c6d8bb..2bd9f2c8 100644 --- a/src/main/java/com/iemr/flw/dto/iemr/EyeCheckupRequestDTO.java +++ b/src/main/java/com/iemr/flw/dto/iemr/EyeCheckupRequestDTO.java @@ -9,6 +9,7 @@ public class EyeCheckupRequestDTO { private Long houseHoldId; private String visitDate; private String userName; + private String eyeSide; private EyeCheckupListDTO fields; } diff --git a/src/main/java/com/iemr/flw/dto/iemr/FilariasisCampaignDTO.java b/src/main/java/com/iemr/flw/dto/iemr/FilariasisCampaignDTO.java new file mode 100644 index 00000000..2e53ee29 --- /dev/null +++ b/src/main/java/com/iemr/flw/dto/iemr/FilariasisCampaignDTO.java @@ -0,0 +1,10 @@ +package com.iemr.flw.dto.iemr; + +import lombok.Data; + +@Data +public class FilariasisCampaignDTO { + private Long id; + private String visitDate; + private FilariasisCampaignListDTO fields; +} diff --git a/src/main/java/com/iemr/flw/dto/iemr/FilariasisCampaignListDTO.java b/src/main/java/com/iemr/flw/dto/iemr/FilariasisCampaignListDTO.java new file mode 100644 index 00000000..81438df9 --- /dev/null +++ b/src/main/java/com/iemr/flw/dto/iemr/FilariasisCampaignListDTO.java @@ -0,0 +1,30 @@ +package com.iemr.flw.dto.iemr; + +import com.fasterxml.jackson.annotation.JsonFormat; +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Data; + +import java.time.LocalDate; +import java.util.List; + +@Data +public class FilariasisCampaignListDTO { + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy") + @JsonProperty("start_date") + private LocalDate startDate; + + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy") + @JsonProperty("end_date") + private LocalDate endDate; + + @JsonProperty("no_of_families") + private String numberOfFamilies; + + @JsonProperty("no_of_individuals") + private String numberOfIndividuals; + + @JsonProperty("mda_photos") + private List mdaPhotos; + + +} diff --git a/src/main/java/com/iemr/flw/dto/iemr/FilariasisCampaignListResponseDTO.java b/src/main/java/com/iemr/flw/dto/iemr/FilariasisCampaignListResponseDTO.java new file mode 100644 index 00000000..700d4665 --- /dev/null +++ b/src/main/java/com/iemr/flw/dto/iemr/FilariasisCampaignListResponseDTO.java @@ -0,0 +1,31 @@ +package com.iemr.flw.dto.iemr; + +import com.fasterxml.jackson.annotation.JsonFormat; +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Data; + +import java.time.LocalDate; +import java.util.List; + +@Data +public class FilariasisCampaignListResponseDTO { + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy") + @JsonProperty("start_date") + private LocalDate startDate; + + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy") + @JsonProperty("end_date") + private LocalDate endDate; + + @JsonProperty("no_of_families") + private Double numberOfFamilies; + + @JsonProperty("no_of_individuals") + private Double numberOfIndividuals; + + @JsonProperty("mda_photos") + private List mdaPhotos; + + + +} diff --git a/src/main/java/com/iemr/flw/dto/iemr/FilariasisResponseDTO.java b/src/main/java/com/iemr/flw/dto/iemr/FilariasisResponseDTO.java new file mode 100644 index 00000000..7b6dc5aa --- /dev/null +++ b/src/main/java/com/iemr/flw/dto/iemr/FilariasisResponseDTO.java @@ -0,0 +1,10 @@ +package com.iemr.flw.dto.iemr; + +import lombok.Data; + +@Data +public class FilariasisResponseDTO { + private Long id; + private Long houseHoldId; + private FilariasisCampaignListResponseDTO fields; +} diff --git a/src/main/java/com/iemr/flw/dto/iemr/HbycDTO.java b/src/main/java/com/iemr/flw/dto/iemr/HbycDTO.java index acc54ca8..172132e0 100644 --- a/src/main/java/com/iemr/flw/dto/iemr/HbycDTO.java +++ b/src/main/java/com/iemr/flw/dto/iemr/HbycDTO.java @@ -47,7 +47,7 @@ public class HbycDTO { private String other_place_of_death; @SerializedName("baby_weight") - private BigDecimal baby_weight; + private Integer baby_weight; @SerializedName("is_child_sick") private String is_child_sick; diff --git a/src/main/java/com/iemr/flw/dto/iemr/IncentiveRecordDTO.java b/src/main/java/com/iemr/flw/dto/iemr/IncentiveRecordDTO.java index 58da9958..354e762d 100644 --- a/src/main/java/com/iemr/flw/dto/iemr/IncentiveRecordDTO.java +++ b/src/main/java/com/iemr/flw/dto/iemr/IncentiveRecordDTO.java @@ -1,5 +1,7 @@ package com.iemr.flw.dto.iemr; +import jakarta.persistence.Column; +import jakarta.persistence.Transient; import lombok.Data; import java.sql.Timestamp; @@ -30,5 +32,35 @@ public class IncentiveRecordDTO { private Timestamp updatedDate; private String updatedBy; + + private Boolean isEligible; + + private Boolean isDefaultActivity; + + private Integer approvalStatus; + + private Integer verifiedByUserId; + + private String verifiedByUserName; + + private String activityDec; + + private String groupName; + + private String reason; + + private String otherReason; + + private Boolean isClaimed; + + private Timestamp approvalDate; + + private Timestamp calimedDate; + + private String supervisorRole; + + + + } diff --git a/src/main/java/com/iemr/flw/dto/iemr/MaaMeetingRequestDTO.java b/src/main/java/com/iemr/flw/dto/iemr/MaaMeetingRequestDTO.java index c08c8d64..e4ca6af5 100644 --- a/src/main/java/com/iemr/flw/dto/iemr/MaaMeetingRequestDTO.java +++ b/src/main/java/com/iemr/flw/dto/iemr/MaaMeetingRequestDTO.java @@ -1,17 +1,24 @@ package com.iemr.flw.dto.iemr; import lombok.Data; +import org.springframework.web.bind.annotation.RequestPart; import org.springframework.web.multipart.MultipartFile; import java.sql.Timestamp; import java.time.LocalDate; +import java.util.List; @Data public class MaaMeetingRequestDTO { + private Long id; private LocalDate meetingDate; private String place; private Integer participants; private MultipartFile[] meetingImages; // up to 5 images private Integer ashaId; + private String villageName; + private Integer noOfPragnentWomen; + private Integer noOfLactingMother; + private String mitaninActivityCheckList; private String createdBY; } diff --git a/src/main/java/com/iemr/flw/dto/iemr/MaaMeetingResponseDTO.java b/src/main/java/com/iemr/flw/dto/iemr/MaaMeetingResponseDTO.java index 9ba81ad8..b56356c2 100644 --- a/src/main/java/com/iemr/flw/dto/iemr/MaaMeetingResponseDTO.java +++ b/src/main/java/com/iemr/flw/dto/iemr/MaaMeetingResponseDTO.java @@ -35,6 +35,11 @@ public class MaaMeetingResponseDTO { example = "[\"iVBORw0KGgoAAAANSUhEUgAA...\", \"iVBORw0KGgoAAAANSUhEUgBB...\"]") private List meetingImages; + private String villageName; + private String noOfPragnentWomen; + private String noOfLactingMother; + private String mitaninActivityCheckList; + @Column(name = "created_by") private String createdBy; diff --git a/src/main/java/com/iemr/flw/dto/iemr/MdsrDTO.java b/src/main/java/com/iemr/flw/dto/iemr/MdsrDTO.java index 5b07e40a..e59047ab 100644 --- a/src/main/java/com/iemr/flw/dto/iemr/MdsrDTO.java +++ b/src/main/java/com/iemr/flw/dto/iemr/MdsrDTO.java @@ -35,4 +35,10 @@ public class MdsrDTO { private Timestamp updatedDate; private String updatedBy; + + private String mdsr1File; + + private String mdsr2File; + + private String mdsrDeathCertFile; } diff --git a/src/main/java/com/iemr/flw/dto/iemr/NotificationDTO.java b/src/main/java/com/iemr/flw/dto/iemr/NotificationDTO.java new file mode 100644 index 00000000..b20dd715 --- /dev/null +++ b/src/main/java/com/iemr/flw/dto/iemr/NotificationDTO.java @@ -0,0 +1,13 @@ +package com.iemr.flw.dto.iemr; + +import lombok.Data; + +import java.util.Map; + +@Data +public class NotificationDTO { + private String title; + private String body; + private String token; + private Map data; +} diff --git a/src/main/java/com/iemr/flw/dto/iemr/NotificationListDTO.java b/src/main/java/com/iemr/flw/dto/iemr/NotificationListDTO.java new file mode 100644 index 00000000..2e94be29 --- /dev/null +++ b/src/main/java/com/iemr/flw/dto/iemr/NotificationListDTO.java @@ -0,0 +1,24 @@ +package com.iemr.flw.dto.iemr; + +import com.iemr.flw.domain.iemr.Notification; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; + +/** + * Response payload for the "list notifications" endpoint. + * Bundles the page of notifications together with the receiver's + * total unread count so the client can update a badge in one call. + */ +@Data +@NoArgsConstructor +@AllArgsConstructor +public class NotificationListDTO { + private List notifications; + private long unreadCount; + private long totalCount; + private int page; + private int size; +} \ No newline at end of file diff --git a/src/main/java/com/iemr/flw/dto/iemr/OrsCampaignDTO.java b/src/main/java/com/iemr/flw/dto/iemr/OrsCampaignDTO.java new file mode 100644 index 00000000..dbd83c26 --- /dev/null +++ b/src/main/java/com/iemr/flw/dto/iemr/OrsCampaignDTO.java @@ -0,0 +1,10 @@ +package com.iemr.flw.dto.iemr; + +import lombok.Data; + +@Data +public class OrsCampaignDTO { + private Long id; + private String visitDate; + private OrsCampaignListDTO fields; +} diff --git a/src/main/java/com/iemr/flw/dto/iemr/OrsCampaignListDTO.java b/src/main/java/com/iemr/flw/dto/iemr/OrsCampaignListDTO.java new file mode 100644 index 00000000..552c4fc8 --- /dev/null +++ b/src/main/java/com/iemr/flw/dto/iemr/OrsCampaignListDTO.java @@ -0,0 +1,30 @@ +package com.iemr.flw.dto.iemr; + +import com.fasterxml.jackson.annotation.JsonFormat; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.google.gson.annotations.SerializedName; +import lombok.Data; +import org.springframework.web.multipart.MultipartFile; + +import java.sql.Timestamp; +import java.time.LocalDate; +import java.util.List; + +@Data +public class OrsCampaignListDTO { + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy") + @JsonProperty("start_date") + private LocalDate StartDate; + + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy") + @JsonProperty("end_date") + private LocalDate EndDate; + + @JsonProperty("number_of_families") + private String NumberOfFamilies; + + @JsonProperty("campaign_photos") + private List campaignPhotos; + + +} diff --git a/src/main/java/com/iemr/flw/dto/iemr/OrsCampaignListResponseDTO.java b/src/main/java/com/iemr/flw/dto/iemr/OrsCampaignListResponseDTO.java new file mode 100644 index 00000000..e72fb04c --- /dev/null +++ b/src/main/java/com/iemr/flw/dto/iemr/OrsCampaignListResponseDTO.java @@ -0,0 +1,25 @@ +package com.iemr.flw.dto.iemr; + +import com.fasterxml.jackson.annotation.JsonFormat; +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Data; + +import java.time.LocalDate; +import java.util.List; + +@Data +public class OrsCampaignListResponseDTO { + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy") + @JsonProperty("start_date") + private LocalDate StartDate; + + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy") + @JsonProperty("end_date") + private LocalDate EndDate; + + @JsonProperty("number_of_families") + private Double NumberOfFamilies; + + @JsonProperty("campaign_photos") + private List campaignPhotos; +} \ No newline at end of file diff --git a/src/main/java/com/iemr/flw/dto/iemr/OrsCampaignResponseDTO.java b/src/main/java/com/iemr/flw/dto/iemr/OrsCampaignResponseDTO.java new file mode 100644 index 00000000..5c8b954d --- /dev/null +++ b/src/main/java/com/iemr/flw/dto/iemr/OrsCampaignResponseDTO.java @@ -0,0 +1,9 @@ +package com.iemr.flw.dto.iemr; + +import lombok.Data; + +@Data +public class OrsCampaignResponseDTO { + private Long id; + private OrsCampaignListResponseDTO fields; +} diff --git a/src/main/java/com/iemr/flw/dto/iemr/OrsDistributionListDTO.java b/src/main/java/com/iemr/flw/dto/iemr/OrsDistributionListDTO.java index 25ab11b5..45ef5429 100644 --- a/src/main/java/com/iemr/flw/dto/iemr/OrsDistributionListDTO.java +++ b/src/main/java/com/iemr/flw/dto/iemr/OrsDistributionListDTO.java @@ -10,7 +10,7 @@ public class OrsDistributionListDTO { private String visit_date; @SerializedName("num_under5_children") - private Double num_under5_children; + private Double num_under5_children=1.0; @SerializedName("num_ors_packets") private Double num_ors_packets; diff --git a/src/main/java/com/iemr/flw/dto/iemr/PaymentItem.java b/src/main/java/com/iemr/flw/dto/iemr/PaymentItem.java new file mode 100644 index 00000000..2bb0ecea --- /dev/null +++ b/src/main/java/com/iemr/flw/dto/iemr/PaymentItem.java @@ -0,0 +1,18 @@ +package com.iemr.flw.dto.iemr; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Data; + +@Data +public class PaymentItem { + @JsonProperty("activity_code") + public String activityCode; + + @JsonProperty("count") + public String count; + + @JsonProperty("incentive_amount") + public String incentiveAmount; + + + } \ No newline at end of file diff --git a/src/main/java/com/iemr/flw/dto/iemr/PaymentRequest.java b/src/main/java/com/iemr/flw/dto/iemr/PaymentRequest.java new file mode 100644 index 00000000..88218b26 --- /dev/null +++ b/src/main/java/com/iemr/flw/dto/iemr/PaymentRequest.java @@ -0,0 +1,43 @@ +package com.iemr.flw.dto.iemr; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.util.List; + +public class PaymentRequest { + @JsonProperty("submission_id") + public String submissionId; + + @JsonProperty("partner_code") + public String partnerCode; + + @JsonProperty("period") + public Period period; + + @JsonProperty("asha_id") + public String ashaId; + + @JsonProperty("generated_at") + public String generatedAt; + + @JsonProperty("verified_by") + public VerifiedBy verifiedBy; + + @JsonProperty("items") + public List items; + + public PaymentRequest() {} + + public PaymentRequest(String submissionId, String partnerCode, Period period, + String ashaId, String generatedAt, + VerifiedBy verifiedBy, List items) { + this.submissionId = submissionId; + this.partnerCode = partnerCode; + this.period = period; + this.ashaId = ashaId; + this.generatedAt = generatedAt; + this.verifiedBy = verifiedBy; + this.items = items; + } + } + diff --git a/src/main/java/com/iemr/flw/dto/iemr/PendingActivityDTO.java b/src/main/java/com/iemr/flw/dto/iemr/PendingActivityDTO.java new file mode 100644 index 00000000..5fcdd537 --- /dev/null +++ b/src/main/java/com/iemr/flw/dto/iemr/PendingActivityDTO.java @@ -0,0 +1,15 @@ +package com.iemr.flw.dto.iemr; + +import lombok.Data; +import org.springframework.web.multipart.MultipartFile; + +import java.util.List; + +@Data +public class PendingActivityDTO { + private Long id; + private Integer userId; + private List images; + private String moduleName; + private String activityName; +} diff --git a/src/main/java/com/iemr/flw/dto/iemr/Period.java b/src/main/java/com/iemr/flw/dto/iemr/Period.java new file mode 100644 index 00000000..049e35be --- /dev/null +++ b/src/main/java/com/iemr/flw/dto/iemr/Period.java @@ -0,0 +1,19 @@ +package com.iemr.flw.dto.iemr; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Data; + +@Data +public class Period { + @JsonProperty("start") + public String start; + + @JsonProperty("end") + public String end; + + public Period() {} + + + + } + diff --git a/src/main/java/com/iemr/flw/dto/iemr/PhcReviewMeetingFormDTO.java b/src/main/java/com/iemr/flw/dto/iemr/PhcReviewMeetingFormDTO.java index 00830988..b08386c6 100644 --- a/src/main/java/com/iemr/flw/dto/iemr/PhcReviewMeetingFormDTO.java +++ b/src/main/java/com/iemr/flw/dto/iemr/PhcReviewMeetingFormDTO.java @@ -1,5 +1,6 @@ package com.iemr.flw.dto.iemr; +import jakarta.persistence.Column; import lombok.Data; @Data @@ -8,8 +9,13 @@ public class PhcReviewMeetingFormDTO { private String phcReviewDate; private String place; private Integer noOfBeneficiariesAttended; + private String villageName; + private String mitaninHistory; + private String mitaninActivityCheckList; + private Integer placeId; private String image1; private String image2; + } \ No newline at end of file diff --git a/src/main/java/com/iemr/flw/dto/iemr/PolioCampaignDTO.java b/src/main/java/com/iemr/flw/dto/iemr/PolioCampaignDTO.java new file mode 100644 index 00000000..7f6cfc87 --- /dev/null +++ b/src/main/java/com/iemr/flw/dto/iemr/PolioCampaignDTO.java @@ -0,0 +1,10 @@ +package com.iemr.flw.dto.iemr; + +import lombok.Data; + +@Data +public class PolioCampaignDTO { + private Long id; + private String visitDate; + private PolioCampaignListDTO fields; +} diff --git a/src/main/java/com/iemr/flw/dto/iemr/PolioCampaignListDTO.java b/src/main/java/com/iemr/flw/dto/iemr/PolioCampaignListDTO.java new file mode 100644 index 00000000..a42ef3c6 --- /dev/null +++ b/src/main/java/com/iemr/flw/dto/iemr/PolioCampaignListDTO.java @@ -0,0 +1,28 @@ +package com.iemr.flw.dto.iemr; + +import com.fasterxml.jackson.annotation.JsonFormat; +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Data; +import org.springframework.web.multipart.MultipartFile; + +import java.time.LocalDate; +import java.util.List; + +@Data +public class PolioCampaignListDTO { + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy") + @JsonProperty("start_date") + private LocalDate startDate; + + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy") + @JsonProperty("end_date") + private LocalDate endDate; + + @JsonProperty("children_vaccinated") + private String numberOfChildren; + + @JsonProperty("campaign_photos") + private List campaignPhotos; + + +} diff --git a/src/main/java/com/iemr/flw/dto/iemr/PolioCampaignListResponseDTO.java b/src/main/java/com/iemr/flw/dto/iemr/PolioCampaignListResponseDTO.java new file mode 100644 index 00000000..f27468f3 --- /dev/null +++ b/src/main/java/com/iemr/flw/dto/iemr/PolioCampaignListResponseDTO.java @@ -0,0 +1,25 @@ +package com.iemr.flw.dto.iemr; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Data; +import org.springframework.web.multipart.MultipartFile; + +import java.time.LocalDate; +import java.util.List; + +@Data +public class PolioCampaignListResponseDTO { + @JsonProperty("start_date") + private LocalDate startDate; + + @JsonProperty("end_date") + private LocalDate endDate; + + @JsonProperty("children_vaccinated") + private Double numberOfChildren; + + @JsonProperty("campaign_photos") + private List campaignPhotos; + + +} diff --git a/src/main/java/com/iemr/flw/dto/iemr/PolioCampaignResponseDTO.java b/src/main/java/com/iemr/flw/dto/iemr/PolioCampaignResponseDTO.java new file mode 100644 index 00000000..a6504236 --- /dev/null +++ b/src/main/java/com/iemr/flw/dto/iemr/PolioCampaignResponseDTO.java @@ -0,0 +1,9 @@ +package com.iemr.flw.dto.iemr; + +import lombok.Data; + +@Data +public class PolioCampaignResponseDTO { + private Long id; + private PolioCampaignListResponseDTO fields; +} diff --git a/src/main/java/com/iemr/flw/dto/iemr/SendNotificationRequestDTO.java b/src/main/java/com/iemr/flw/dto/iemr/SendNotificationRequestDTO.java new file mode 100644 index 00000000..b1551ab3 --- /dev/null +++ b/src/main/java/com/iemr/flw/dto/iemr/SendNotificationRequestDTO.java @@ -0,0 +1,18 @@ +package com.iemr.flw.dto.iemr; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class SendNotificationRequestDTO { + private String appType; + private String topic; + private String title; + private String body; + private String redirect; + private String notificationType; + private Integer receiverId; +} diff --git a/src/main/java/com/iemr/flw/dto/iemr/TBConfirmedCasesResponseDTO.java b/src/main/java/com/iemr/flw/dto/iemr/TBConfirmedCasesResponseDTO.java new file mode 100644 index 00000000..42504dcd --- /dev/null +++ b/src/main/java/com/iemr/flw/dto/iemr/TBConfirmedCasesResponseDTO.java @@ -0,0 +1,11 @@ +package com.iemr.flw.dto.iemr; + +import com.iemr.flw.domain.iemr.TBConfirmedCase; +import lombok.Data; + +import java.util.List; +@Data +public class TBConfirmedCasesResponseDTO { + Integer userId ; + List tbConfirmedCases; +} diff --git a/src/main/java/com/iemr/flw/dto/iemr/TBConfirmedRequestDTO.java b/src/main/java/com/iemr/flw/dto/iemr/TBConfirmedRequestDTO.java new file mode 100644 index 00000000..d3e76a4a --- /dev/null +++ b/src/main/java/com/iemr/flw/dto/iemr/TBConfirmedRequestDTO.java @@ -0,0 +1,13 @@ +package com.iemr.flw.dto.iemr; + +import com.iemr.flw.domain.iemr.TBConfirmedCaseDTO; +import lombok.Data; +import java.util.List; + +@Data +public class TBConfirmedRequestDTO { + + private Long userId; + + private List tbConfirmedList; +} diff --git a/src/main/java/com/iemr/flw/dto/iemr/TBScreeningDTO.java b/src/main/java/com/iemr/flw/dto/iemr/TBScreeningDTO.java index 270526bd..36f48dfc 100644 --- a/src/main/java/com/iemr/flw/dto/iemr/TBScreeningDTO.java +++ b/src/main/java/com/iemr/flw/dto/iemr/TBScreeningDTO.java @@ -29,91 +29,15 @@ public class TBScreeningDTO { private Boolean familySufferingFromTB; - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public Long getBenId() { - return benId; - } - - public void setBenId(Long benId) { - this.benId = benId; - } - - public Timestamp getVisitDate() { - return visitDate; - } - - public void setVisitDate(Timestamp visitDate) { - this.visitDate = visitDate; - } - - public Boolean getCoughMoreThan2Weeks() { - return coughMoreThan2Weeks; - } - - public void setCoughMoreThan2Weeks(Boolean coughMoreThan2Weeks) { - this.coughMoreThan2Weeks = coughMoreThan2Weeks; - } - - public Boolean getBloodInSputum() { - return bloodInSputum; - } - - public void setBloodInSputum(Boolean bloodInSputum) { - this.bloodInSputum = bloodInSputum; - } - - public Boolean getFeverMoreThan2Weeks() { - return feverMoreThan2Weeks; - } - - public void setFeverMoreThan2Weeks(Boolean feverMoreThan2Weeks) { - this.feverMoreThan2Weeks = feverMoreThan2Weeks; - } - - public Boolean getLossOfWeight() { - return lossOfWeight; - } - - public void setLossOfWeight(Boolean lossOfWeight) { - this.lossOfWeight = lossOfWeight; - } - - public Boolean getNightSweats() { - return nightSweats; - } - - public void setNightSweats(Boolean nightSweats) { - this.nightSweats = nightSweats; - } - - public Boolean getHistoryOfTb() { - return historyOfTb; - } - - public void setHistoryOfTb(Boolean historyOfTb) { - this.historyOfTb = historyOfTb; - } - - public Boolean getTakingAntiTBDrugs() { - return takingAntiTBDrugs; - } - - public void setTakingAntiTBDrugs(Boolean takingAntiTBDrugs) { - this.takingAntiTBDrugs = takingAntiTBDrugs; - } - - public Boolean getFamilySufferingFromTB() { - return familySufferingFromTB; - } - - public void setFamilySufferingFromTB(Boolean familySufferingFromTB) { - this.familySufferingFromTB = familySufferingFromTB; - } + private Boolean riseOfFever; + private Boolean lossOfAppetite; + private Boolean age; + private Boolean diabetic; + private Boolean tobaccoUser; + private Boolean bmi; + private Boolean contactWithTBPatient; + private Boolean historyOfTBInLastFiveYrs; + private String sympotomatic; + private String asymptomatic; + private String recommandateTest; } diff --git a/src/main/java/com/iemr/flw/dto/iemr/TBSuspectedDTO.java b/src/main/java/com/iemr/flw/dto/iemr/TBSuspectedDTO.java index 8d224a9c..29fb25f6 100644 --- a/src/main/java/com/iemr/flw/dto/iemr/TBSuspectedDTO.java +++ b/src/main/java/com/iemr/flw/dto/iemr/TBSuspectedDTO.java @@ -10,18 +10,20 @@ public class TBSuspectedDTO { private Long id; private Long benId; - + private String visitLabel; private Timestamp visitDate; - + private String typeOfTBCase; + private String reasonForSuspicion; private Boolean isSputumCollected; - private String sputumSubmittedAt; - private String nikshayId; - private String sputumTestResult; - + private Boolean isChestXRayDone; + private String chestXRayResult; + private String referralFacility; + private Boolean isTBConfirmed; + private Boolean isConfirmed; + private Boolean isDRTBConfirmed; private Boolean referred; - private String followUps; } diff --git a/src/main/java/com/iemr/flw/dto/iemr/UpdateApprovalRequestDTO.java b/src/main/java/com/iemr/flw/dto/iemr/UpdateApprovalRequestDTO.java new file mode 100644 index 00000000..ff786730 --- /dev/null +++ b/src/main/java/com/iemr/flw/dto/iemr/UpdateApprovalRequestDTO.java @@ -0,0 +1,14 @@ +package com.iemr.flw.dto.iemr; + +import lombok.Data; + +@Data +public class UpdateApprovalRequestDTO { + private Integer ashaId; + private Integer month; + private Integer year; + private Integer approvalStatus; + private String incentiveIds; + private String reason; + private String otherReason; +} diff --git a/src/main/java/com/iemr/flw/dto/iemr/UpdateClaimedStatusRequestDTO.java b/src/main/java/com/iemr/flw/dto/iemr/UpdateClaimedStatusRequestDTO.java new file mode 100644 index 00000000..052b7d12 --- /dev/null +++ b/src/main/java/com/iemr/flw/dto/iemr/UpdateClaimedStatusRequestDTO.java @@ -0,0 +1,11 @@ +package com.iemr.flw.dto.iemr; + +import lombok.Data; + +@Data +public class UpdateClaimedStatusRequestDTO { + private Integer ashaId; + private Integer month; + private Integer year; + private boolean isClaimed; +} diff --git a/src/main/java/com/iemr/flw/dto/iemr/UserServiceRoleDTO.java b/src/main/java/com/iemr/flw/dto/iemr/UserServiceRoleDTO.java index 81d74903..ead3ddc0 100644 --- a/src/main/java/com/iemr/flw/dto/iemr/UserServiceRoleDTO.java +++ b/src/main/java/com/iemr/flw/dto/iemr/UserServiceRoleDTO.java @@ -4,6 +4,8 @@ import lombok.Getter; import lombok.Setter; +import java.util.Map; + @Data @Getter @Setter @@ -23,6 +25,7 @@ public class UserServiceRoleDTO { private String blockName; private String villageId; private String villageName; + private Map facilityData; public UserServiceRoleDTO(Integer userId, String name, String userName, Integer stateId, String stateName, Integer workingDistrictId, String workingDistrictName, Short serviceProviderId, Integer roleId, String roleName, Integer providerServiceMapId, Integer blockId, String blockName, String villageId, String villageName) { this.userId = userId; diff --git a/src/main/java/com/iemr/flw/dto/iemr/VHNDFormDTO.java b/src/main/java/com/iemr/flw/dto/iemr/VHNDFormDTO.java index 0dec7976..bee7f247 100644 --- a/src/main/java/com/iemr/flw/dto/iemr/VHNDFormDTO.java +++ b/src/main/java/com/iemr/flw/dto/iemr/VHNDFormDTO.java @@ -24,6 +24,7 @@ */ package com.iemr.flw.dto.iemr; +import jakarta.persistence.Column; import lombok.Data; @Data @@ -33,6 +34,17 @@ public class VHNDFormDTO { private Integer noOfBeneficiariesAttended; private String image1; private String image2; + private Integer vhndPlaceId; + private String pregnantWomenAnc; + private String lactatingMothersPnc; + private String childrenImmunization; + private Boolean selectAllEducation; + private String knowledgeBalancedDiet; + private String careDuringPregnancy; + private String importanceBreastfeeding; + private String complementaryFeeding; + private String hygieneSanitation; + private String familyPlanningHealthcare; } diff --git a/src/main/java/com/iemr/flw/dto/iemr/VerifiedBy.java b/src/main/java/com/iemr/flw/dto/iemr/VerifiedBy.java new file mode 100644 index 00000000..e64deb39 --- /dev/null +++ b/src/main/java/com/iemr/flw/dto/iemr/VerifiedBy.java @@ -0,0 +1,17 @@ +package com.iemr.flw.dto.iemr; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Data; + +@Data +public class VerifiedBy { + @JsonProperty("employee_id") + public String employeeId; + + @JsonProperty("name") + public String name; + + public VerifiedBy() {} + + + } diff --git a/src/main/java/com/iemr/flw/dto/iemr/VhncFormDTO.java b/src/main/java/com/iemr/flw/dto/iemr/VhncFormDTO.java index 8a12be96..a2515b42 100644 --- a/src/main/java/com/iemr/flw/dto/iemr/VhncFormDTO.java +++ b/src/main/java/com/iemr/flw/dto/iemr/VhncFormDTO.java @@ -34,4 +34,12 @@ public class VhncFormDTO { private Integer noOfBeneficiariesAttended; private String image1; private String image2; + private String villageName; + private Integer anm; + private Integer aww; + private Integer noOfPragnentWoment; + private Integer noOfLactingMother; + private Integer noOfCommittee; + private Boolean followupPrevius; + } \ No newline at end of file diff --git a/src/main/java/com/iemr/flw/masterEnum/GroupName.java b/src/main/java/com/iemr/flw/masterEnum/GroupName.java index 8d6736eb..e53733b0 100644 --- a/src/main/java/com/iemr/flw/masterEnum/GroupName.java +++ b/src/main/java/com/iemr/flw/masterEnum/GroupName.java @@ -33,3 +33,6 @@ public String getDisplayName() { } + + + diff --git a/src/main/java/com/iemr/flw/masterEnum/IncentiveApprovalStatus.java b/src/main/java/com/iemr/flw/masterEnum/IncentiveApprovalStatus.java new file mode 100644 index 00000000..4149cab0 --- /dev/null +++ b/src/main/java/com/iemr/flw/masterEnum/IncentiveApprovalStatus.java @@ -0,0 +1,36 @@ +package com.iemr.flw.masterEnum; + +public enum IncentiveApprovalStatus { + + VERIFIED(101, "Verified"), + PENDING(102, "Pending"), + REJECTED(103, "Rejected"), + OVERDUE(104, "Overdue"), + APPROVED(105, "Approved"); + + private final int code; + private final String label; + + IncentiveApprovalStatus(int code, String label) { + this.code = code; + this.label = label; + } + + public int getCode() { + return code; + } + + public String getLabel() { + return label; + } + + // 🔥 Reverse lookup (code → enum) + public static IncentiveApprovalStatus fromCode(int code) { + for (IncentiveApprovalStatus status : values()) { + if (status.code == code) { + return status; + } + } + throw new IllegalArgumentException("Invalid approval status code: " + code); + } +} \ No newline at end of file diff --git a/src/main/java/com/iemr/flw/masterEnum/IncentiveName.java b/src/main/java/com/iemr/flw/masterEnum/IncentiveName.java new file mode 100644 index 00000000..052ab011 --- /dev/null +++ b/src/main/java/com/iemr/flw/masterEnum/IncentiveName.java @@ -0,0 +1,139 @@ +package com.iemr.flw.masterEnum; + +public enum IncentiveName { + + // ---------------- CHILD HEALTH ---------------- + HBNC_0_42_DAYS(GroupName.CHILD_HEALTH), + HBYC_QUARTERLY_VISITS(GroupName.CHILD_HEALTH), + SNCU_LBW_FOLLOWUP(GroupName.CHILD_HEALTH), + SAM_REFERRAL_NRC(GroupName.CHILD_HEALTH), + CHILD_DEATH_REPORTING(GroupName.CHILD_HEALTH), + E_HBNC_CACHAR(GroupName.CHILD_HEALTH), + MAA_QUARTERLY_MEETING(GroupName.CHILD_HEALTH), + NATIONAL_DEWORMING_DAY(GroupName.CHILD_HEALTH), + ORS_DISTRIBUTION(GroupName.CHILD_HEALTH), + NATIONAL_IRON_PLUS(GroupName.CHILD_HEALTH), + NIPI_CHILDREN(GroupName.CHILD_HEALTH), + + // ---------------- IMMUNIZATION ---------------- + FULL_IMMUNIZATION_0_1(GroupName.IMMUNIZATION), + COMPLETE_IMMUNIZATION_1_2(GroupName.IMMUNIZATION), + DPT_IMMUNIZATION_5_YEARS(GroupName.IMMUNIZATION), + CHILD_MOBILIZATION_SESSIONS(GroupName.IMMUNIZATION), + + // ---------------- MATERNAL HEALTH ---------------- + ANC_REGISTRATION_1ST_TRIM(GroupName.MATERNAL_HEALTH), + FULL_ANC(GroupName.MATERNAL_HEALTH), + COMPREHENSIVE_ABORTION_CARE(GroupName.MATERNAL_HEALTH), + INST_DELIVERY_HRP(GroupName.MATERNAL_HEALTH), + EPMSMA_INST_DELIVERY(GroupName.MATERNAL_HEALTH), + EPMSMA_HRP_IDENTIFIED(GroupName.MATERNAL_HEALTH), + MATERNAL_DEATH_REPORT(GroupName.MATERNAL_HEALTH), + MH_MISOPROSTOL(GroupName.MATERNAL_HEALTH), + MH_EARLY_REG_BANK(GroupName.MATERNAL_HEALTH), + MH_ANC_3RD_TRIM(GroupName.MATERNAL_HEALTH), + MH_MOTIVATE_INST_DEL(GroupName.MATERNAL_HEALTH), + MH_HR_POSTNATAL(GroupName.MATERNAL_HEALTH), + + // ---------------- JSY ---------------- + JSY_1ST_DEL_ANC_RURAL(GroupName.JSY), + JSY_1ST_DEL_INST_RURAL(GroupName.JSY), + JSY_2ND_DEL_ANC_RURAL(GroupName.JSY), + JSY_2ND_DEL_INST_RURAL(GroupName.JSY), + JSY_3RD_DEL_ANC_RURAL(GroupName.JSY), + JSY_3RD_DEL_INST_RURAL(GroupName.JSY), + JSY_4TH_DEL_ANC_RURAL(GroupName.JSY), + JSY_4TH_DEL_INST_RURAL(GroupName.JSY), + JSY_ANC_URBAN(GroupName.JSY), + JSY_INST_URBAN(GroupName.JSY), + + // ---------------- FAMILY PLANNING ---------------- + FP_ANC_MPA1(GroupName.FAMILY_PLANNING), + FP_ANC_MPA2(GroupName.FAMILY_PLANNING), + FP_ANC_MPA3(GroupName.FAMILY_PLANNING), + FP_ANC_MPA4(GroupName.FAMILY_PLANNING), + FP_ANC_MPA5(GroupName.FAMILY_PLANNING), + FIRST_SECOND_CHILD_GAP(GroupName.FAMILY_PLANNING), + FP_DELAY_2Y(GroupName.FAMILY_PLANNING), + FP_LIMIT_2CHILD(GroupName.FAMILY_PLANNING), + FP_PPIUCD(GroupName.FAMILY_PLANNING), + FP_PAIUCD(GroupName.FAMILY_PLANNING), + FP_CONDOM(GroupName.FAMILY_PLANNING), + FP_ORALPILLS(GroupName.FAMILY_PLANNING), + FP_EC(GroupName.FAMILY_PLANNING), + FP_FEMALE_STER(GroupName.FAMILY_PLANNING), + FP_PPS(GroupName.FAMILY_PLANNING), + FP_MINILAP(GroupName.FAMILY_PLANNING), + FP_MALE_STER(GroupName.FAMILY_PLANNING), + FP_EC_SURVEY(GroupName.FAMILY_PLANNING), + FP_SAAS_BAHU(GroupName.FAMILY_PLANNING), + FP_NP_KIT(GroupName.FAMILY_PLANNING), + + // ---------------- ADOLESCENT HEALTH ---------------- + AH_SANITARY(GroupName.ADOLESCENT_HEALTH), + AH_PEER_ED(GroupName.ADOLESCENT_HEALTH), + AH_MOBILIZE(GroupName.ADOLESCENT_HEALTH), + + // ---------------- ASHA ROUTINE ---------------- + ASHA_MONTHLY_ROUTINE(GroupName.ASHA_MONTHLY_ROUTINE), + + // ---------------- UMBRELLA PROGRAMMES ---------------- + NPCB_GOVT_CATARACT(GroupName.UMBRELLA_PROGRAMMES), + NPCB_PRIVATE_CATARACT(GroupName.UMBRELLA_PROGRAMMES), + DSTB_TREATMENT(GroupName.UMBRELLA_PROGRAMMES), + DRTB_TREATMENT(GroupName.UMBRELLA_PROGRAMMES), + INFORMANT_INCENTIVE(GroupName.UMBRELLA_PROGRAMMES), + TPT_PROVIDING(GroupName.UMBRELLA_PROGRAMMES), + BANK_ACCOUNT_NPY(GroupName.UMBRELLA_PROGRAMMES), + HOUSE_TO_HOUSE_SURVEY(GroupName.UMBRELLA_PROGRAMMES), + NLEP_TRAINING(GroupName.UMBRELLA_PROGRAMMES), + NLEP_CASE_DETECTION(GroupName.UMBRELLA_PROGRAMMES), + NLEP_PB_TREATMENT(GroupName.UMBRELLA_PROGRAMMES), + NLEP_MB_TREATMENT(GroupName.UMBRELLA_PROGRAMMES), + LEPROSY_PARTIAL_ASHA(GroupName.UMBRELLA_PROGRAMMES), + NVBDCP_SLIDE_COLLECTION(GroupName.UMBRELLA_PROGRAMMES), + NVBDCP_MALARIA_TREATMENT(GroupName.UMBRELLA_PROGRAMMES), + AES_JE_REFERRAL(GroupName.UMBRELLA_PROGRAMMES), + DENGUE_CHIKUNGUNYA(GroupName.UMBRELLA_PROGRAMMES), + NIDDCP_SALT_TEST(GroupName.UMBRELLA_PROGRAMMES), + + // ---------------- NCD ---------------- + NCD_POP_ENUMERATION(GroupName.NCD), + NCD_FOLLOWUP_TREATMENT(GroupName.NCD), + NCD_NEW_PATIENT_MEDICATION_SUPPORT(GroupName.NCD), + + // ---------------- ADDITIONAL INCENTIVE ---------------- + ADDITIONAL_ASHA_INCENTIVE(GroupName.ADDITIONAL_INCENTIVE), + + // ---------------- OTHER INCENTIVES ---------------- + ABHA_ID_CREATION(GroupName.OTHER_INCENTIVES), + MOBILE_BILL_REIMB(GroupName.OTHER_INCENTIVES), + + // ---------------- ACTIVITY ---------------- + ANC_FOUR_CHECKUPS_SUPPORT(GroupName.ACTIVITY), + INST_DELIVERY_ESCORT(GroupName.ACTIVITY), + FILARIASIS_MEDICINE_DISTRIBUTION(GroupName.ACTIVITY), + VHND_PARTICIPATION(GroupName.ACTIVITY), + CLUSTER_MEETING(GroupName.ACTIVITY), + VHSNC_MEETING(GroupName.ACTIVITY), + MITANIN_REGISTER(GroupName.ACTIVITY), + HWC_REFERRAL_10_CASES(GroupName.ACTIVITY), + LACTATING_MOTHERS_HOME_VISIT(GroupName.ACTIVITY), + MITANIN_REGISTER_5_INFO_FILL(GroupName.ACTIVITY), + HIGH_RISK_POSTPARTUM_HEALTH_CHECK(GroupName.ACTIVITY), + MOSQUITO_NET_DISTRIBUTION_MOBILIZATION(GroupName.ACTIVITY), + HIGH_RISK_POSTPARTUM_CARE(GroupName.ACTIVITY), + MONTHLY_HONORARIUM(GroupName.ACTIVITY), + FULL_ANC_45(GroupName.ACTIVITY), + MH_MOTIVATE_INST_DEL_46(GroupName.ACTIVITY); + + private final GroupName groupName; + + IncentiveName(GroupName groupName) { + this.groupName = groupName; + } + + public GroupName getGroupName() { + return groupName; + } +} diff --git a/src/main/java/com/iemr/flw/repo/identity/BeneficiaryRepo.java b/src/main/java/com/iemr/flw/repo/identity/BeneficiaryRepo.java index 96301fb3..e4ce22f0 100644 --- a/src/main/java/com/iemr/flw/repo/identity/BeneficiaryRepo.java +++ b/src/main/java/com/iemr/flw/repo/identity/BeneficiaryRepo.java @@ -12,7 +12,10 @@ import java.math.BigInteger; import java.sql.Timestamp; import java.util.ArrayList; +import java.util.Collection; +import java.util.List; import java.util.Optional; +import java.util.stream.Stream; @Repository public interface BeneficiaryRepo extends JpaRepository { @@ -22,22 +25,30 @@ public interface BeneficiaryRepo extends JpaRepository findById(Long benID); + List findByHouseoldId(Long houseHoldId); + List findByBenficieryid(Long benID); + @Query(value = "SELECT beneficiaryRegID FROM db_identity.i_beneficiarydetails_rmnch WHERE BeneficiaryId = :benId", nativeQuery = true) Long getBenRegIdFromBenId(@Param("benId") Long benId); @Query(nativeQuery = true, value = " SELECT userid FROM db_iemr.m_user WHERE UserName = :userName ") Integer getUserIDByUserName(@Param("userName") String userName); - @Query(value = " SELECT t FROM RMNCHMBeneficiaryaddress t WHERE DATE(t.createdDate) BETWEEN DATE(:fromDate) " - + " AND DATE(:toDate) AND t.createdBy = :userName ") - Page getBenDataWithinDates(@Param("userName") String userName, - @Param("fromDate") Timestamp fromDate, @Param("toDate") Timestamp toDate, Pageable pageable); + + @Query("SELECT t FROM RMNCHMBeneficiaryaddress t " + + "WHERE t.createdDate BETWEEN :fromDate AND :toDate " + + "AND t.createdBy = :userName") + Page getBenDataWithinDates( + @Param("userName") String userName, + @Param("fromDate") Timestamp fromDate, + @Param("toDate") Timestamp toDate, + Pageable pageable); @Query(value = " SELECT t FROM RMNCHMBeneficiaryaddress t WHERE t.createdBy = :userName ") Page getBenDataByUser(@Param("userName") String userName, Pageable pageable); @Query(value = " SELECT t FROM RMNCHMBeneficiarymapping t WHERE t.benAddressId = :addressID") - RMNCHMBeneficiarymapping getByAddressID(@Param("addressID") BigInteger addressID); + List getByAddressID(@Param("addressID") BigInteger addressID); @Query(value = " SELECT t FROM RMNCHMBeneficiarymapping t WHERE t.benRegId = :BenRegId") RMNCHMBeneficiarymapping getById(@Param("BenRegId") BigInteger BenRegId); @@ -61,7 +72,7 @@ Page getBenDataWithinDates(@Param("userName") String u BigInteger getBenIdFromRegID(@Param("benRegID") Long benRegID); @Query(value = " SELECT t FROM RMNCHBeneficiaryDetailsRmnch t WHERE t.BenRegId =:benRegID ") - RMNCHBeneficiaryDetailsRmnch getDetailsByRegID(@Param("benRegID") Long benRegID); + List getDetailsByRegID(@Param("benRegID") Long benRegID); @Query(value = " SELECT t FROM RMNCHBornBirthDetails t WHERE t.BenRegId =:benRegID ") RMNCHBornBirthDetails getBornBirthByRegID(@Param("benRegID") Long benRegID); @@ -72,6 +83,9 @@ Page getBenDataWithinDates(@Param("userName") String u @Query(nativeQuery = true, value = " SELECT HealthIdNumber,HealthID FROM db_iemr.m_benhealthidmapping WHERE BeneficiaryRegID = :benRegId ") Object[] getBenHealthIdNumber(@Param("benRegId") BigInteger benRegId); + + @Query(nativeQuery = true, value = " SELECT HealthIdNumber,HealthID FROM db_iemr.m_benhealthidmapping WHERE HealthIdNumber = :HealthIdNumber ") + Object[] getHealthIdNumber(@Param("HealthIdNumber") String HealthIdNumber); @Query(nativeQuery = true, value = " SELECT HealthID,HealthIdNumber,isNewAbha FROM db_iemr.t_healthid WHERE HealthIdNumber = :healthIdNumber ") ArrayList getBenHealthDetails(@Param("healthIdNumber") String healthIdNumber); @@ -83,6 +97,24 @@ Page getBenDataWithinDates(@Param("userName") String u RMNCHMBeneficiarydetail findByBeneficiaryDetailsId(@Param("beneficiaryDetailsId") BigInteger beneficiaryDetailsId); - - + // BeneficiaryRepo — replaces 3 separate queries per beneficiary + @Query(value = """ + SELECT + ibd.BeneficiaryId AS benId, + bd.firstName AS firstName, + bd.lastName AS lastName + FROM db_identity.i_beneficiarydetails_rmnch ibd + JOIN db_identity.i_beneficiarymapping bm + ON bm.BenRegId = ibd.BeneficiaryRegID + JOIN db_identity.i_beneficiarydetails bd + ON bd.beneficiaryDetailsId = bm.BenDetailsId + WHERE ibd.BeneficiaryId IN :benIds + """, nativeQuery = true) + List findBenNamesByBenIds(@Param("benIds") List benIds); + +// BeneficiaryRepo — replaces the per-row getBenIdFromRegID call +@Query("SELECT b.BenRegId, b.benficieryid " + + "FROM RMNCHBeneficiaryDetailsRmnch b " + + "WHERE b.BenRegId IN :regIds AND b.benficieryid IS NOT NULL") +List getBenIdsFromRegIDs(@Param("regIds") List regIds); } diff --git a/src/main/java/com/iemr/flw/repo/identity/HouseHoldRepo.java b/src/main/java/com/iemr/flw/repo/identity/HouseHoldRepo.java index 514eeb6b..a5ee8f25 100644 --- a/src/main/java/com/iemr/flw/repo/identity/HouseHoldRepo.java +++ b/src/main/java/com/iemr/flw/repo/identity/HouseHoldRepo.java @@ -5,8 +5,10 @@ import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; +import java.util.List; + public interface HouseHoldRepo extends JpaRepository { @Query(" SELECT t FROM RMNCHHouseHoldDetails t WHERE t.houseoldId =:houseoldId ") - RMNCHHouseHoldDetails getByHouseHoldID(@Param("houseoldId") long houseoldId); + List getByHouseHoldID(@Param("houseoldId") long houseoldId); } diff --git a/src/main/java/com/iemr/flw/repo/iemr/ANCVisitRepo.java b/src/main/java/com/iemr/flw/repo/iemr/ANCVisitRepo.java index 067e6acc..67e3e7b9 100644 --- a/src/main/java/com/iemr/flw/repo/iemr/ANCVisitRepo.java +++ b/src/main/java/com/iemr/flw/repo/iemr/ANCVisitRepo.java @@ -17,8 +17,13 @@ public interface ANCVisitRepo extends JpaRepository { List getANCForPW(@Param("userId") String userId, @Param("fromDate") Timestamp fromDate, @Param("toDate") Timestamp toDate); + @Query(value = "SELECT anc FROM ANCVisit anc WHERE anc.createdBy = :userId and anc.isActive = true") + List getANCForPW(@Param("userId") String userId); + @Query - ANCVisit findANCVisitByBenIdAndAncVisitAndIsActive(Long benId, Integer ancVisit, boolean b); + List findANCVisitByBenIdAndAncVisitAndIsActive(Long benId, Integer ancVisit, boolean b); + + List findByBenId(Long benId); } diff --git a/src/main/java/com/iemr/flw/repo/iemr/AncCounsellingCareRepo.java b/src/main/java/com/iemr/flw/repo/iemr/AncCounsellingCareRepo.java new file mode 100644 index 00000000..714dae59 --- /dev/null +++ b/src/main/java/com/iemr/flw/repo/iemr/AncCounsellingCareRepo.java @@ -0,0 +1,13 @@ +package com.iemr.flw.repo.iemr; + +import com.iemr.flw.domain.iemr.AncCare; +import com.iemr.flw.domain.iemr.AncCounsellingCare; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public interface AncCounsellingCareRepo extends JpaRepository { + List findAllByUserId(Integer userId); +} diff --git a/src/main/java/com/iemr/flw/repo/iemr/AshaSupervisorLoginRepo.java b/src/main/java/com/iemr/flw/repo/iemr/AshaSupervisorLoginRepo.java new file mode 100644 index 00000000..3b52abe2 --- /dev/null +++ b/src/main/java/com/iemr/flw/repo/iemr/AshaSupervisorLoginRepo.java @@ -0,0 +1,149 @@ +package com.iemr.flw.repo.iemr; + +import java.sql.Timestamp; +import java.util.ArrayList; +import java.util.List; + +import com.iemr.flw.domain.iemr.AshaSupervisorMapping; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.CrudRepository; +import org.springframework.data.repository.query.Param; +import org.springframework.stereotype.Repository; + +@Repository +public interface AshaSupervisorLoginRepo extends CrudRepository { + + ArrayList findBySupervisorUserIDAndDeletedFalse(Integer supervisorUserID); + + // API 1: All ASHAs mapped to a supervisor with facility info + @Query(value = "SELECT DISTINCT asm.ashaUserID, u.FirstName, u.LastName, " + + "f.FacilityID, f.FacilityName, " + + "COALESCE(ft.FacilityTypeName,'') AS facilityTypeName " + + "FROM asha_supervisor_mapping asm " + + "JOIN m_User u ON u.UserID = asm.ashaUserID AND u.Deleted = false " + + "JOIN m_facility f ON f.FacilityID = asm.facilityID AND f.Deleted = false " + + "LEFT JOIN m_facilitytype ft ON ft.FacilityTypeID = f.FacilityTypeID " + + "WHERE asm.supervisorUserID = :supervisorUserID " + + "AND asm.deleted = false", nativeQuery = true) + List getAllMappedAshas(@Param("supervisorUserID") Integer supervisorUserID); + + // API 2: ASHAs at a specific facility for a supervisor with user details + @Query(value = "SELECT DISTINCT asm.ashaUserID, u.FirstName, u.LastName, " + + "COALESCE(u.AgentID,'') AS agentID, " + + "COALESCE(u.EmergencyContactNo,'') AS mobile, " + + "COALESCE(g.GenderName,'') AS gender " + + "FROM asha_supervisor_mapping asm " + + "JOIN m_User u ON u.UserID = asm.ashaUserID " + + "AND u.Deleted = false " + + "LEFT JOIN m_gender g ON g.GenderID = u.GenderID " + + "JOIN incentive_activity_record iar " + + "ON iar.asha_id = asm.ashaUserID " + + "WHERE asm.supervisorUserID = :supervisorUserID " + + "AND asm.facilityID = :facilityID " + + "AND asm.deleted = false " + + "AND iar.is_claimed = true " + + "AND iar.start_date >= :startDate " + + "AND iar.end_date < :endDate " + + "AND (:approvalStatus = 0 " + + "OR iar.approval_status = :approvalStatus)", + nativeQuery = true) + List getAshasAtFacility( + @Param("supervisorUserID") Integer supervisorUserID, + @Param("facilityID") Integer facilityID, + @Param("approvalStatus") Integer approvalStatus, + @Param("startDate") Timestamp startDate, + @Param("endDate") Timestamp endDate); + + + @Query(value = "SELECT DISTINCT asm.ashaUserID, u.FirstName, u.LastName, " + + "COALESCE(u.AgentID,'') AS agentID, " + + "COALESCE(u.EmergencyContactNo,'') AS mobile, " + + "COALESCE(g.GenderName,'') AS gender " + + "FROM asha_supervisor_mapping asm " + + "JOIN m_User u ON u.UserID = asm.ashaUserID " + + "AND u.Deleted = false " + + "LEFT JOIN m_gender g ON g.GenderID = u.GenderID " + + "JOIN incentive_activity_record iar " + + "ON iar.asha_id = asm.ashaUserID " + + "WHERE asm.supervisorUserID = :supervisorUserID " + + "AND asm.facilityID = :facilityID " + + "AND asm.deleted = false " + + "AND iar.is_claimed = true " + + "AND iar.start_date >= :startDate " + + "AND iar.end_date < :endDate " + + "AND (" + + " :approvalStatus = 0 " + + " OR (:approvalStatus = 105 AND iar.approval_status IN (101,105)) " + + " OR (:approvalStatus = 106 AND iar.approval_status = 102) " + + " OR (:approvalStatus NOT IN (105,106) AND iar.approval_status = :approvalStatus)" + + ")", + nativeQuery = true) + List getAshasAtFacilityCg( + @Param("supervisorUserID") Integer supervisorUserID, + @Param("facilityID") Integer facilityID, + @Param("approvalStatus") Integer approvalStatus, + @Param("startDate") Timestamp startDate, + @Param("endDate") Timestamp endDate); + + + @Query(value = "SELECT DISTINCT asm.ashaUserID, u.FirstName, u.LastName, " + + "COALESCE(u.AgentID,'') AS agentID, " + + "COALESCE(u.EmergencyContactNo,'') AS mobile, " + + "COALESCE(g.GenderName,'') AS gender " + + "FROM asha_supervisor_mapping asm " + + "JOIN m_User u ON u.UserID = asm.ashaUserID " + + "AND u.Deleted = false " + + "LEFT JOIN m_gender g ON g.GenderID = u.GenderID " + + "JOIN incentive_activity_record iar " + + "ON iar.asha_id = asm.ashaUserID " + + "WHERE asm.supervisorUserID = :supervisorUserID " + + "AND asm.deleted = false " + + "AND (" + + " (:approvalStatus = 106 AND (iar.is_claimed = false OR iar.is_claimed IS NULL)) " + + " OR (:approvalStatus <> 106 AND iar.is_claimed = true)" + + ") " + + "AND iar.start_date >= :startDate " + + "AND iar.end_date < :endDate " + + "AND (" + + " :approvalStatus = 0 " + + " OR (:approvalStatus = 105 AND iar.approval_status IN (101,105)) " + + " OR (:approvalStatus = 106 AND iar.approval_status = 102) " + + " OR (:approvalStatus NOT IN (105,106) AND iar.approval_status = :approvalStatus)" + + ")", + nativeQuery = true) + List getAshasAtFacilityCg( + @Param("supervisorUserID") Integer supervisorUserID, + @Param("approvalStatus") Integer approvalStatus, + @Param("startDate") Timestamp startDate, + @Param("endDate") Timestamp endDate); + + + + @Query(value = "SELECT DISTINCT asm.ashaUserID, " + + "u.FirstName, " + + "u.LastName, " + + "COALESCE(u.AgentID,'') AS agentID, " + + "COALESCE(u.EmergencyContactNo,'') AS mobile, " + + "COALESCE(g.GenderName,'') AS gender " + + "FROM asha_supervisor_mapping asm " + + "JOIN m_User u " + + "ON u.UserID = asm.ashaUserID " + + "AND u.Deleted = false " + + "LEFT JOIN m_gender g " + + "ON g.GenderID = u.GenderID " + + "JOIN incentive_activity_record iar " + + "ON iar.asha_id = asm.ashaUserID " + + "WHERE asm.supervisorUserID = :supervisorUserID " + + "AND asm.deleted = false " + + "AND iar.is_claimed = true " + + "AND iar.start_date >= :startDate " + + "AND iar.end_date < :endDate " + + "AND ( :approvalStatus = 0 " + + "OR iar.approval_status = :approvalStatus )", + nativeQuery = true) + List getAshasAtFacility( + @Param("supervisorUserID") Integer supervisorUserID, + @Param("approvalStatus") Integer approvalStatus, + @Param("startDate") Timestamp startDate, + @Param("endDate") Timestamp endDate); +} \ No newline at end of file diff --git a/src/main/java/com/iemr/flw/repo/iemr/BenReferDetailsRepo.java b/src/main/java/com/iemr/flw/repo/iemr/BenReferDetailsRepo.java index b5277c19..9cb35662 100644 --- a/src/main/java/com/iemr/flw/repo/iemr/BenReferDetailsRepo.java +++ b/src/main/java/com/iemr/flw/repo/iemr/BenReferDetailsRepo.java @@ -24,11 +24,15 @@ import com.iemr.flw.domain.iemr.BenReferDetails; +import io.swagger.v3.oas.annotations.info.License; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; +import java.util.List; +import java.util.Optional; + @Repository public interface BenReferDetailsRepo extends JpaRepository { @@ -40,4 +44,19 @@ public interface BenReferDetailsRepo extends JpaRepository findByCreatedBy(String userName); + + @Query(value = """ + SELECT COUNT(*) + FROM db_iemr.t_benreferdetails + WHERE CreatedBy = :userName + AND MONTH(CreatedDate) = :month + AND YEAR(CreatedDate) = :year + """, nativeQuery = true) + Long countMonthlyReferrals( + @Param("userName") String userName, + @Param("month") Integer month, + @Param("year") Integer year); + } diff --git a/src/main/java/com/iemr/flw/repo/iemr/CbacIemrDetailsRepo.java b/src/main/java/com/iemr/flw/repo/iemr/CbacIemrDetailsRepo.java new file mode 100644 index 00000000..5e93dfad --- /dev/null +++ b/src/main/java/com/iemr/flw/repo/iemr/CbacIemrDetailsRepo.java @@ -0,0 +1,44 @@ +/* +* AMRIT – Accessible Medical Records via Integrated Technology +* Integrated EHR (Electronic Health Records) Solution +* +* Copyright (C) "Piramal Swasthya Management and Research Institute" +* +* This file is part of AMRIT. +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see https://www.gnu.org/licenses/. +*/ +package com.iemr.flw.repo.iemr; + +import com.iemr.flw.domain.iemr.CbacDetailsImer; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.CrudRepository; +import org.springframework.data.repository.query.Param; +import org.springframework.stereotype.Repository; + + +import java.util.List; + +@Repository +public interface CbacIemrDetailsRepo extends CrudRepository { + + public CbacDetailsImer findByBeneficiaryRegIdAndVisitCode(Long beneficiaryRegId, Long visitCode); + + List findByCreatedBy(String userName); + + @Query(value = "SELECT BeneficiaryID FROM db_identity.m_beneficiaryregidmapping " + + "WHERE BenRegId = :benRegId AND Deleted = 0", nativeQuery = true) + Long getBeneficiaryId(@Param("benRegId") Long benRegId); + +} diff --git a/src/main/java/com/iemr/flw/repo/iemr/CdrRepo.java b/src/main/java/com/iemr/flw/repo/iemr/CdrRepo.java index d5e0e711..069362e8 100644 --- a/src/main/java/com/iemr/flw/repo/iemr/CdrRepo.java +++ b/src/main/java/com/iemr/flw/repo/iemr/CdrRepo.java @@ -14,7 +14,5 @@ public interface CdrRepo extends JpaRepository { CDR findCDRByBenId(Long benId); - @Query(" SELECT c FROM CDR c WHERE c.createdBy = :userId and c.createdDate >= :fromDate and c.createdDate <= :toDate") - List getAllCdrByBenId(@Param("userId") String userId, - @Param("fromDate") Timestamp fromDate, @Param("toDate") Timestamp toDate); + ListfindByCreatedBy(String userName); } diff --git a/src/main/java/com/iemr/flw/repo/iemr/ChildRegisterRepo.java b/src/main/java/com/iemr/flw/repo/iemr/ChildRegisterRepo.java index 7d10f595..f456428b 100644 --- a/src/main/java/com/iemr/flw/repo/iemr/ChildRegisterRepo.java +++ b/src/main/java/com/iemr/flw/repo/iemr/ChildRegisterRepo.java @@ -18,4 +18,8 @@ List getChildDetailsForUser(@Param("userId") String userId, // @Query(value = "Select * from db_identity.i_cbacdetails where beneficiaryRegId = :benRegId and createdDate = :createdDate limit 1", nativeQuery = true) ChildRegister findChildRegisterByBenIdAndCreatedDate(Long benRegId, Timestamp createdDate); + + Long countByBenId(Long benId); + + List findByBenId(Long benId); } diff --git a/src/main/java/com/iemr/flw/repo/iemr/ChildVaccinationRepo.java b/src/main/java/com/iemr/flw/repo/iemr/ChildVaccinationRepo.java index 9ed0943c..99487cc2 100644 --- a/src/main/java/com/iemr/flw/repo/iemr/ChildVaccinationRepo.java +++ b/src/main/java/com/iemr/flw/repo/iemr/ChildVaccinationRepo.java @@ -18,11 +18,34 @@ List getChildVaccinationDetails(@Param("userId") String userId ChildVaccination findChildVaccinationByBeneficiaryRegIdAndCreatedDateAndVaccineName(Long benRegId, Timestamp createdDate, String vaccine); - @Query(value = "select count(*) from db_iemr.t_childvaccinedetail1 cv left outer join db_iemr.m_immunizationservicevaccination v on " + - "cv.VaccineName = v.VaccineName where cv.beneficiaryRegId = :benRegId and v.Currentimmunizationserviceid in (1,2,3,4,5) and " + - "v.category = 'CHILD'", nativeQuery = true) + @Query(value = """ + SELECT COUNT(DISTINCT cv.VaccineName) + FROM db_iemr.t_childvaccinedetail1 cv + JOIN db_iemr.m_immunizationservicevaccination v + ON cv.VaccineName = v.VaccineName + WHERE cv.BeneficiaryRegID = :benRegId + AND v.Currentimmunizationserviceid IN (1,2,3,4,5) + AND v.category = 'CHILD' + """, nativeQuery = true) Integer getFirstYearVaccineCountForBenId(@Param("benRegId") Long benRegId); + @Query(value = """ + SELECT COUNT(DISTINCT cv.VaccineName) + FROM db_iemr.t_childvaccinedetail1 cv + WHERE cv.BeneficiaryRegID = :benRegId + AND cv.VaccineName IN ( + 'BCG Vaccine', + 'OPV-1', + 'OPV-2', + 'OPV-3', + 'Pentavalent-1', + 'Pentavalent-2', + 'Pentavalent-3', + 'Measles & Rubella (MR)-1' + ) + """, nativeQuery = true) + Integer getEligibleFirstYearVaccines(@Param("benRegId") Long benRegId); + @Query(value = "select count(*) from db_iemr.m_immunizationservicevaccination v where v.Currentimmunizationserviceid in (1,2,3,4,5) " + @@ -42,4 +65,16 @@ List getChildVaccinationDetails(@Param("userId") String userId "cv.VaccineName = v.VaccineName where cv.beneficiaryRegId = :benRegId and v.Currentimmunizationserviceid = 8 and v.VaccineName = " + "'DPT Booster-2' and v.category = 'CHILD'), 1, 0);", nativeQuery = true) Integer checkDptVaccinatedUser(@Param("benRegId") Long benRegId); + + @Query(value = """ + SELECT COUNT(DISTINCT cv.VaccineName) + FROM db_iemr.t_childvaccinedetail1 cv + WHERE cv.BeneficiaryRegID = :benRegId + AND cv.VaccineName IN ( + 'DPT Booster-1', + 'Measles & Rubella (MR)-2', + 'OPV Booster' + ) + """, nativeQuery = true) + Integer getEligibleSecondYearVaccines(@Param("benRegId") Long benRegId); } diff --git a/src/main/java/com/iemr/flw/repo/iemr/ChronicDiseaseVisitRepository.java b/src/main/java/com/iemr/flw/repo/iemr/ChronicDiseaseVisitRepository.java new file mode 100644 index 00000000..e63835a9 --- /dev/null +++ b/src/main/java/com/iemr/flw/repo/iemr/ChronicDiseaseVisitRepository.java @@ -0,0 +1,14 @@ +package com.iemr.flw.repo.iemr; + +import com.iemr.flw.domain.iemr.ChronicDiseaseVisitEntity; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public interface ChronicDiseaseVisitRepository + extends JpaRepository { + + List findByUserID(Integer ashaId); +} diff --git a/src/main/java/com/iemr/flw/repo/iemr/DeliveryOutcomeRepo.java b/src/main/java/com/iemr/flw/repo/iemr/DeliveryOutcomeRepo.java index 643d7c9a..cf09f625 100644 --- a/src/main/java/com/iemr/flw/repo/iemr/DeliveryOutcomeRepo.java +++ b/src/main/java/com/iemr/flw/repo/iemr/DeliveryOutcomeRepo.java @@ -16,5 +16,7 @@ public interface DeliveryOutcomeRepo extends JpaRepository getDeliveryOutcomeByAshaId(@Param("userId") String userId, @Param("fromDate") Timestamp fromDate, @Param("toDate") Timestamp toDate); + List findByCreatedBy(String userName); + DeliveryOutcome findDeliveryOutcomeByBenIdAndIsActive(Long benId, Boolean isActive); } diff --git a/src/main/java/com/iemr/flw/repo/iemr/DiseaseLeprosyRepository.java b/src/main/java/com/iemr/flw/repo/iemr/DiseaseLeprosyRepository.java index 567b62a5..36873319 100644 --- a/src/main/java/com/iemr/flw/repo/iemr/DiseaseLeprosyRepository.java +++ b/src/main/java/com/iemr/flw/repo/iemr/DiseaseLeprosyRepository.java @@ -16,4 +16,6 @@ public interface DiseaseLeprosyRepository extends JpaRepository getByCreatedBy(@Param("createdBy") String createdBy); + + } \ No newline at end of file diff --git a/src/main/java/com/iemr/flw/repo/iemr/EligibleCoupleRegisterRepo.java b/src/main/java/com/iemr/flw/repo/iemr/EligibleCoupleRegisterRepo.java index 2eae0b05..218c5b2b 100644 --- a/src/main/java/com/iemr/flw/repo/iemr/EligibleCoupleRegisterRepo.java +++ b/src/main/java/com/iemr/flw/repo/iemr/EligibleCoupleRegisterRepo.java @@ -14,7 +14,12 @@ public interface EligibleCoupleRegisterRepo extends JpaRepository findByCreatedBy(String userName); + @Query(" SELECT ecr FROM EligibleCoupleRegister ecr WHERE ecr.createdBy = :userId and ecr.createdDate >= :fromDate and ecr.createdDate <= :toDate") List getECRegRecords(@Param("userId") String userId, @Param("fromDate") Timestamp fromDate, @Param("toDate") Timestamp toDate); + + @Query(" SELECT ecr FROM EligibleCoupleRegister ecr WHERE ecr.createdBy = :userId") + List getECRegRecords(@Param("userId") String userId); } diff --git a/src/main/java/com/iemr/flw/repo/iemr/EmployeeMasterRepo.java b/src/main/java/com/iemr/flw/repo/iemr/EmployeeMasterRepo.java index d3ac8edf..943b43b1 100644 --- a/src/main/java/com/iemr/flw/repo/iemr/EmployeeMasterRepo.java +++ b/src/main/java/com/iemr/flw/repo/iemr/EmployeeMasterRepo.java @@ -1,12 +1,14 @@ package com.iemr.flw.repo.iemr; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.CrudRepository; +import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; -import com.iemr.flw.domain.iemr.M_User; +import com.iemr.flw.domain.iemr.User; @Repository -public interface EmployeeMasterRepo extends JpaRepository { - M_User findByUserID(Integer userID); - - M_User getUserByUserID(Integer parseLong); +public interface EmployeeMasterRepo extends JpaRepository { + @Query("SELECT u FROM User u WHERE u.userID = :userID and u.deleted=false and u.statusID in (1, 2)") + User findUserByUserID(@Param("userID") Integer userID); } diff --git a/src/main/java/com/iemr/flw/repo/iemr/EyeCheckUpVisitRepo.java b/src/main/java/com/iemr/flw/repo/iemr/EyeCheckUpVisitRepo.java index c0e3df73..fd713c44 100644 --- a/src/main/java/com/iemr/flw/repo/iemr/EyeCheckUpVisitRepo.java +++ b/src/main/java/com/iemr/flw/repo/iemr/EyeCheckUpVisitRepo.java @@ -10,4 +10,5 @@ public interface EyeCheckUpVisitRepo extends JpaRepository { List findByUserId(Integer userId); + List findByCreatedBy(String userName); } diff --git a/src/main/java/com/iemr/flw/repo/iemr/FacilityLoginRepo.java b/src/main/java/com/iemr/flw/repo/iemr/FacilityLoginRepo.java new file mode 100644 index 00000000..2f956b3b --- /dev/null +++ b/src/main/java/com/iemr/flw/repo/iemr/FacilityLoginRepo.java @@ -0,0 +1,98 @@ +package com.iemr.flw.repo.iemr; + +import java.util.List; + +import com.iemr.flw.domain.iemr.AshaSupervisorMapping; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.CrudRepository; +import org.springframework.data.repository.query.Param; +import org.springframework.stereotype.Repository; + + +@Repository +public interface FacilityLoginRepo extends CrudRepository { + + // Get user's facility IDs from m_UserServiceRoleMapping (for ANY user) + @Query(value = "SELECT DISTINCT usrm.FacilityID " + + "FROM m_UserServiceRoleMapping usrm " + + "WHERE usrm.UserID = :userID AND usrm.Deleted = false " + + "AND usrm.FacilityID IS NOT NULL", nativeQuery = true) + List getUserFacilityIDs(@Param("userID") Integer userID); + + // Get facility details with geo names and facilityType + @Query(value = "SELECT DISTINCT f.FacilityID, f.FacilityName, " + + "f.StateID, COALESCE(s.StateName,'') AS stateName, " + + "f.DistrictID, COALESCE(d.DistrictName,'') AS districtName, " + + "f.BlockID, COALESCE(b.BlockName,'') AS blockName, " + + "COALESCE(f.RuralUrban,'') AS ruralUrban, " + + "COALESCE(ft.FacilityTypeName,'') AS facilityTypeName " + + "FROM m_facility f " + + "LEFT JOIN m_state s ON s.StateID = f.StateID " + + "LEFT JOIN m_district d ON d.DistrictID = f.DistrictID " + + "LEFT JOIN m_districtblock b ON b.BlockID = f.BlockID " + + "LEFT JOIN m_facilitytype ft ON ft.FacilityTypeID = f.FacilityTypeID " + + "WHERE f.FacilityID IN :facilityIDs AND f.Deleted = false", nativeQuery = true) + List getFacilityDetails(@Param("facilityIDs") List facilityIDs); + + // ASHA login: get supervisor details + @Query(value = "SELECT asm.supervisorUserID, u.FirstName, u.LastName, u.ContactNo, " + + "COALESCE(u.EmployeeID,'') AS employeeID " + + "FROM asha_supervisor_mapping asm " + + "JOIN m_User u ON u.UserID = asm.supervisorUserID " + + "WHERE asm.ashaUserID = :ashaUserID AND asm.deleted = false " + + "AND u.Deleted = false LIMIT 1", nativeQuery = true) + List getSupervisorForAsha(@Param("ashaUserID") Integer ashaUserID); + + @Query(value = "SELECT GenderName FROM m_gender WHERE GenderID = :genderID", nativeQuery = true) + String getGenderName(@Param("genderID") Integer genderID); + + // Villages mapped to facilities + @Query(value = "SELECT fvm.FacilityID, fvm.DistrictBranchID, dbm.VillageName " + + "FROM facility_village_mapping fvm " + + "JOIN m_DistrictBranchMapping dbm ON dbm.DistrictBranchID = fvm.DistrictBranchID " + + "WHERE fvm.FacilityID IN :facilityIDs AND fvm.Deleted = false", nativeQuery = true) + List getVillagesForFacilities(@Param("facilityIDs") List facilityIDs); + + // ASHA login: get peers at same facility (ANM, CHO, etc.) + @Query(value = "SELECT DISTINCT usrm.UserID, u.FirstName, u.LastName, r.RoleName, " + + "COALESCE(u.EmployeeID,'') AS employeeID, " + + "COALESCE(u.ContactNo,'') AS mobile " + + "FROM m_UserServiceRoleMapping usrm " + + "JOIN m_User u ON u.UserID = usrm.UserID " + + "JOIN m_Role r ON r.RoleID = usrm.RoleID " + + "WHERE usrm.FacilityID IN :facilityIDs " + + "AND usrm.UserID != :currentUserID " + + "AND usrm.Deleted = false AND u.Deleted = false", nativeQuery = true) + List getPeersAtFacility(@Param("facilityIDs") List facilityIDs, + @Param("currentUserID") Integer currentUserID); + + // Supervisor: get mapped ASHAs with facility details + @Query(value = "SELECT DISTINCT u.UserID, u.FirstName, u.LastName, " + + "COALESCE(u.EmployeeID,'') AS employeeID, " + + "f.FacilityID, f.FacilityName, " + + "COALESCE(ft.FacilityTypeName,'') AS facilityTypeName, " + + "COALESCE(u.ContactNo,'') AS mobile " + + "FROM asha_supervisor_mapping asm " + + "JOIN m_User u ON u.UserID = asm.ashaUserID " + + "JOIN m_facility f ON f.FacilityID = asm.facilityID " + + "LEFT JOIN m_facilitytype ft ON ft.FacilityTypeID = f.FacilityTypeID " + + "WHERE asm.supervisorUserID = :supervisorUserID " + + "AND asm.deleted = false AND u.Deleted = false AND f.Deleted = false", nativeQuery = true) + List getMappedAshasBySupervisor(@Param("supervisorUserID") Integer supervisorUserID); + + // CHO/ANM: get ASHAs at same facilities + @Query(value = "SELECT DISTINCT u.UserID, u.FirstName, u.LastName, " + + "COALESCE(u.EmployeeID,'') AS employeeID, " + + "f.FacilityID, f.FacilityName, " + + "COALESCE(ft.FacilityTypeName,'') AS facilityTypeName, " + + "COALESCE(u.ContactNo,'') AS mobile " + + "FROM m_UserServiceRoleMapping usrm " + + "JOIN m_User u ON u.UserID = usrm.UserID " + + "JOIN m_Role r ON r.RoleID = usrm.RoleID " + + "JOIN m_facility f ON f.FacilityID = usrm.FacilityID " + + "LEFT JOIN m_facilitytype ft ON ft.FacilityTypeID = f.FacilityTypeID " + + "WHERE usrm.FacilityID IN :facilityIDs " + + "AND r.RoleName = 'ASHA' " + + "AND usrm.Deleted = false AND u.Deleted = false AND f.Deleted = false", nativeQuery = true) + List getAshaListByFacilities(@Param("facilityIDs") List facilityIDs); +} \ No newline at end of file diff --git a/src/main/java/com/iemr/flw/repo/iemr/FilariasisCampaignRepo.java b/src/main/java/com/iemr/flw/repo/iemr/FilariasisCampaignRepo.java new file mode 100644 index 00000000..1fe0d3e2 --- /dev/null +++ b/src/main/java/com/iemr/flw/repo/iemr/FilariasisCampaignRepo.java @@ -0,0 +1,13 @@ +package com.iemr.flw.repo.iemr; + +import com.iemr.flw.domain.iemr.FilariasisCampaign; +import com.iemr.flw.domain.iemr.PulsePolioCampaign; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface FilariasisCampaignRepo extends JpaRepository { + Page findByUserId(Integer userId, Pageable pageable); +} diff --git a/src/main/java/com/iemr/flw/repo/iemr/HbncVisitRepo.java b/src/main/java/com/iemr/flw/repo/iemr/HbncVisitRepo.java index 655c0547..cbc7f568 100644 --- a/src/main/java/com/iemr/flw/repo/iemr/HbncVisitRepo.java +++ b/src/main/java/com/iemr/flw/repo/iemr/HbncVisitRepo.java @@ -1,6 +1,8 @@ package com.iemr.flw.repo.iemr; import com.iemr.flw.domain.iemr.HbncVisit; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; @@ -20,6 +22,5 @@ public interface HbncVisitRepo extends JpaRepository { HbncVisit findByBeneficiaryIdAndVisit_day(@Param("beneficiaryId") Long beneficiaryId, @Param("visitDay") String visitDay); - List findByAshaId(Integer ashaId); - + Page findByAshaId(Integer ashaId, Pageable pageable); } diff --git a/src/main/java/com/iemr/flw/repo/iemr/IFAFormSubmissionRepository.java b/src/main/java/com/iemr/flw/repo/iemr/IFAFormSubmissionRepository.java index 40cbd60b..0b1b5ff7 100644 --- a/src/main/java/com/iemr/flw/repo/iemr/IFAFormSubmissionRepository.java +++ b/src/main/java/com/iemr/flw/repo/iemr/IFAFormSubmissionRepository.java @@ -2,6 +2,8 @@ import com.iemr.flw.domain.iemr.IFAFormSubmissionData; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; import java.util.List; @@ -9,4 +11,10 @@ public interface IFAFormSubmissionRepository extends JpaRepository { List findByUserId(Integer userName); + @Query("SELECT i FROM IFAFormSubmissionData i " + + "WHERE i.userId = :userId " + + "AND i.visitDate = :visitDate") + List findTodayData( + @Param("userId") Integer userId, + @Param("visitDate") String visitDate); } diff --git a/src/main/java/com/iemr/flw/repo/iemr/IncentiveActivityLangMappingRepo.java b/src/main/java/com/iemr/flw/repo/iemr/IncentiveActivityLangMappingRepo.java index f9807010..4ba40dc6 100644 --- a/src/main/java/com/iemr/flw/repo/iemr/IncentiveActivityLangMappingRepo.java +++ b/src/main/java/com/iemr/flw/repo/iemr/IncentiveActivityLangMappingRepo.java @@ -3,9 +3,12 @@ import com.iemr.flw.domain.iemr.IncentiveActivityLangMapping; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.Collection; import java.util.List; import java.util.Optional; public interface IncentiveActivityLangMappingRepo extends JpaRepository { IncentiveActivityLangMapping findByIdAndName(Long activityId,String activityName); + List findAllByIdIn(List ids); + } diff --git a/src/main/java/com/iemr/flw/repo/iemr/IncentivePendingActivityRepository.java b/src/main/java/com/iemr/flw/repo/iemr/IncentivePendingActivityRepository.java new file mode 100644 index 00000000..295d447b --- /dev/null +++ b/src/main/java/com/iemr/flw/repo/iemr/IncentivePendingActivityRepository.java @@ -0,0 +1,13 @@ +package com.iemr.flw.repo.iemr; + +import java.util.Optional; +import com.iemr.flw.domain.iemr.IncentivePendingActivity; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + + +@Repository +public interface IncentivePendingActivityRepository extends JpaRepository { + + Optional findByMincentiveIdAndActivityId(Long mIncentiveId,Long activityId); +} diff --git a/src/main/java/com/iemr/flw/repo/iemr/IncentiveRecordRepo.java b/src/main/java/com/iemr/flw/repo/iemr/IncentiveRecordRepo.java index 6b8db13e..fdda9847 100644 --- a/src/main/java/com/iemr/flw/repo/iemr/IncentiveRecordRepo.java +++ b/src/main/java/com/iemr/flw/repo/iemr/IncentiveRecordRepo.java @@ -2,43 +2,389 @@ import com.iemr.flw.domain.iemr.IncentiveActivityRecord; +import jakarta.persistence.Column; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; import java.sql.Timestamp; import java.util.List; +import java.util.Optional; @Repository public interface IncentiveRecordRepo extends JpaRepository { - @Query("select record from IncentiveActivityRecord record where record.activityId = :id and record.createdDate = :createdDate and record.benId = :benId") - IncentiveActivityRecord findRecordByActivityIdCreatedDateBenId(@Param("id") Long id, @Param("createdDate") Timestamp createdDate, @Param("benId") Long benId); + @Query(value = """ + SELECT * + FROM db_iemr.incentive_activity_record r + WHERE r.activity_id = :id + AND r.ben_id = :benId + AND r.asha_id = :ashaId + AND MONTH(r.start_date) = MONTH(:createdDate) + AND YEAR(r.start_date) = YEAR(:createdDate) + LIMIT 1 + """, nativeQuery = true) + IncentiveActivityRecord findRecordByActivityIdCreatedDateBenId( + @Param("id") Long id, + @Param("createdDate") Timestamp createdDate, + @Param("benId") Long benId, + @Param("ashaId") Integer ashaId); - @Query("select record from IncentiveActivityRecord record where record.activityId = :id and record.createdDate = :createdDate and record.benId = :benId and record.ashaId = :ashaId") - IncentiveActivityRecord findRecordByActivityIdCreatedDateBenId(@Param("id") Long id, @Param("createdDate") Timestamp createdDate, @Param("benId") Long benId,@Param("ashaId") Integer ashaId); + Optional findById(Long id); - @Query("SELECT record FROM IncentiveActivityRecord record " + - "WHERE record.activityId = :id " + - "AND record.createdDate BETWEEN :startDate AND :endDate " + - "AND record.benId = :benId " + // ← space added here - "AND record.ashaId = :ashaId") + @Query(value = """ + SELECT * + FROM incentive_activity_record + WHERE activity_id = :activityId + AND ben_id = :benId + AND asha_id = :ashaId + AND created_date >= :startOfMonth + AND created_date < :startOfNextMonth + ORDER BY created_date DESC + LIMIT 1 + """, nativeQuery = true) IncentiveActivityRecord findRecordByActivityIdCreatedDateBenId( - @Param("id") Long id, - @Param("startDate") Timestamp startDate, - @Param("endDate") Timestamp endDate, + @Param("activityId") Long activityId, @Param("benId") Long benId, - @Param("ashaId") Integer ashaId + @Param("ashaId") Integer ashaId, + @Param("startOfMonth") Timestamp startOfMonth, + @Param("startOfNextMonth") Timestamp startOfNextMonth ); @Query("select record from IncentiveActivityRecord record where record.ashaId = :ashaId and record.startDate >= :fromDate and record.startDate <= :toDate and record.endDate >= :fromDate and record.endDate <= :toDate ") - List findRecordsByAsha(@Param("ashaId") Integer ashaId, @Param("fromDate") Timestamp fromDate,@Param("toDate") Timestamp toDate); + List findRecordsByAsha(@Param("ashaId") Integer ashaId, @Param("fromDate") Timestamp fromDate, @Param("toDate") Timestamp toDate); + @Query("SELECT DISTINCT record FROM IncentiveActivityRecord record WHERE record.ashaId = :ashaId") + Page findRecordsByAsha(@Param("ashaId") Integer ashaId, Pageable pageable); - @Query("select record from IncentiveActivityRecord record where record.ashaId = :ashaId") + @Query("SELECT DISTINCT record FROM IncentiveActivityRecord record WHERE record.ashaId = :ashaId") List findRecordsByAsha(@Param("ashaId") Integer ashaId); + + @Query("SELECT " + + "SUM(CASE WHEN record.approvalStatus = 101 THEN 1 ELSE 0 END), " + + "SUM(CASE WHEN record.approvalStatus = 102 THEN 1 ELSE 0 END), " + + "SUM(CASE WHEN record.approvalStatus = 103 THEN 1 ELSE 0 END), " + + "SUM(CASE WHEN record.approvalStatus = 105 THEN 1 ELSE 0 END) " + + "FROM IncentiveActivityRecord record " + + "WHERE record.ashaId = :ashaId " + + "AND record.isClaimed = true " + + "AND record.createdDate >= :startDate " + + "AND record.createdDate < :endDate") + List getStatusCountByAshaId( + @Param("ashaId") Integer ashaId, + @Param("startDate") Timestamp startDate, + @Param("endDate") Timestamp endDate); + + + @Query("SELECT " + + "SUM(CASE WHEN record.approvalStatus = 101 THEN 1 ELSE 0 END), " + + "SUM(CASE WHEN record.approvalStatus = 102 THEN 1 ELSE 0 END), " + + "SUM(CASE WHEN record.approvalStatus = 103 THEN 1 ELSE 0 END), " + + "SUM(CASE WHEN record.approvalStatus IN (102,105) THEN 1 ELSE 0 END) " + + "FROM IncentiveActivityRecord record " + + "WHERE record.ashaId = :ashaId " + + "AND record.isClaimed = true " + + "AND record.createdDate >= :startDate " + + "AND record.createdDate < :endDate") + List getStatusCountByAshaIdANM( + @Param("ashaId") Integer ashaId, + @Param("startDate") Timestamp startDate, + @Param("endDate") Timestamp endDate); + + @Query(""" +SELECT + SUM(CASE WHEN record.approvalStatus = 101 THEN 1 ELSE 0 END), + SUM(CASE WHEN record.approvalStatus = 102 THEN 1 ELSE 0 END), + SUM(CASE WHEN record.approvalStatus = 103 THEN 1 ELSE 0 END), + SUM(CASE WHEN record.approvalStatus IN (101,105) THEN 1 ELSE 0 END) +FROM IncentiveActivityRecord record +WHERE record.ashaId = :ashaId +AND record.isClaimed = true +AND record.isDefaultActivity = true +AND record.createdDate >= :startDate +AND record.createdDate < :endDate +""") + List getStatusCountByAshaIdOfDefaultActivity( + @Param("ashaId") Integer ashaId, + @Param("startDate") Timestamp startDate, + @Param("endDate") Timestamp endDate); + + + @Query(""" +SELECT + SUM(CASE WHEN record.approvalStatus = 101 THEN 1 ELSE 0 END), + SUM(CASE WHEN record.approvalStatus = 102 THEN 1 ELSE 0 END), + SUM(CASE WHEN record.approvalStatus = 103 THEN 1 ELSE 0 END), + SUM(CASE WHEN record.approvalStatus IN (101,105) THEN 1 ELSE 0 END) +FROM IncentiveActivityRecord record +WHERE record.ashaId = :ashaId +AND record.isClaimed = false +AND record.createdDate >= :startDate +AND record.createdDate < :endDate +""") + List getStatusUnclaimedCountByAshaId( + @Param("ashaId") Integer ashaId, + @Param("startDate") Timestamp startDate, + @Param("endDate") Timestamp endDate); + + + + @Query("SELECT COALESCE(SUM(record.amount), 0) " + + "FROM IncentiveActivityRecord record " + + "JOIN IncentiveActivity master ON record.activityId = master.id " + + "WHERE record.ashaId = :ashaId " + + "AND master.state = :stateCode " + + "AND record.isClaimed = true " + + "AND record.startDate >= :fromDate " + + "AND record.endDate <= :toDate " + + "AND (:approvalStatus = 0 OR record.approvalStatus = :approvalStatus)") + Long getTotalAmountByAsha( + @Param("ashaId") Integer ashaId, + @Param("fromDate") Timestamp fromDate, + @Param("toDate") Timestamp toDate, + @Param("approvalStatus") Integer approvalStatus, + @Param("stateCode") Integer stateCode); + + + @Query(""" + SELECT COALESCE(SUM(record.amount), 0) + FROM IncentiveActivityRecord record + JOIN IncentiveActivity master ON record.activityId = master.id + WHERE record.ashaId = :ashaId + AND master.state = :stateCode + AND record.isClaimed = true + AND record.startDate >= :fromDate + AND record.endDate <= :toDate + AND ( + :approvalStatus = 0 + OR ( + :approvalStatus = 102 + AND record.approvalStatus IN (102, 105) + ) + OR ( + :approvalStatus <> 102 + AND record.approvalStatus = :approvalStatus + ) + ) +""") + Long getTotalAmountByAshaANM( + @Param("ashaId") Integer ashaId, + @Param("fromDate") Timestamp fromDate, + @Param("toDate") Timestamp toDate, + @Param("approvalStatus") Integer approvalStatus, + @Param("stateCode") Integer stateCode + ); + + @Query(""" + SELECT COALESCE(SUM(record.amount), 0) + FROM IncentiveActivityRecord record + JOIN IncentiveActivity master ON record.activityId = master.id + WHERE record.ashaId = :ashaId + AND master.state = :stateCode + AND record.isClaimed = true + AND record.isDefaultActivity = false + AND record.startDate >= :fromDate + AND record.endDate <= :toDate + AND ( + :approvalStatus = 0 + OR ( + :approvalStatus = 102 + AND record.approvalStatus IN (102, 105) + ) + OR ( + :approvalStatus <> 102 + AND record.approvalStatus = :approvalStatus + ) + ) +""") + Long getTotalAmountByAshaANMbeforeClaimedHour( + @Param("ashaId") Integer ashaId, + @Param("fromDate") Timestamp fromDate, + @Param("toDate") Timestamp toDate, + @Param("approvalStatus") Integer approvalStatus, + @Param("stateCode") Integer stateCode + ); + + @Query("SELECT COALESCE(SUM(record.amount), 0) " + + "FROM IncentiveActivityRecord record " + + "JOIN IncentiveActivity master ON record.activityId = master.id " + + "WHERE record.ashaId = :ashaId " + + "AND master.state = :stateCode " + + "AND record.isClaimed = true " + + "AND record.isDefaultActivity = true " + + "AND record.startDate >= :fromDate " + + "AND record.endDate <= :toDate " + + "AND (" + + " :approvalStatus = 0 " + + " OR (:approvalStatus = 101 AND record.approvalStatus IN (101,105)) " + + " OR (:approvalStatus <> 101 AND record.approvalStatus = :approvalStatus)" + + ")") + Long getDefaultActivityTotalAmountByAsha( + @Param("ashaId") Integer ashaId, + @Param("fromDate") Timestamp fromDate, + @Param("toDate") Timestamp toDate, + @Param("approvalStatus") Integer approvalStatus, + @Param("stateCode") Integer stateCode); + + + @Query("SELECT record " + + "FROM IncentiveActivityRecord record " + + "WHERE record.ashaId = :ashaId " + + "AND record.isClaimed = true " + + "AND record.startDate <= :toDate " + + "AND record.isDefaultActivity = true " + + "AND record.endDate >= :fromDate") + List getDefalutRecordsByAsha( + @Param("ashaId") Integer ashaId, + @Param("fromDate") Timestamp fromDate, + @Param("toDate") Timestamp toDate); + + @Query("SELECT record " + + "FROM IncentiveActivityRecord record " + + "WHERE record.ashaId = :ashaId " + + "AND record.isClaimed = true " + + "AND record.startDate <= :toDate " + + "AND record.endDate >= :fromDate") + List getRecordsByAsha( + @Param("ashaId") Integer ashaId, + @Param("fromDate") Timestamp fromDate, + @Param("toDate") Timestamp toDate); + + @Modifying + @Transactional + @Query("UPDATE IncentiveActivityRecord iar " + + "SET iar.approvalStatus = :approvalStatus, " + + "iar.verifiedByUserId = :ashaSupervisorUserId, " + + "iar.reason = NULL, " + + "iar.otherReason = NULL, " + + "iar.approvalDate = :approvalDate, " + + "iar.verifiedByUserName = :ashaSupervisorUserName " + + "WHERE iar.ashaId = :ashaId " + + "AND iar.isClaimed = true " + + "AND iar.createdDate >= :startDate " + + "AND iar.createdDate < :endDate") + int updateApprovalStatusByAshaAndDateRange( + @Param("ashaId") Integer ashaId, + @Param("approvalStatus") Integer approvalStatus, + @Param("startDate") Timestamp startDate, + @Param("endDate") Timestamp endDate, + @Param("approvalDate") Timestamp approvalDate, + @Param("ashaSupervisorUserId") Integer ashaSupervisorUserId, + @Param("ashaSupervisorUserName") String ashaSupervisorUserName); + + + @Modifying + @Transactional + @Query(""" +UPDATE IncentiveActivityRecord iar +SET + iar.approvalStatus = :approvalStatus, + iar.verifiedByUserId = :ashaSupervisorUserId, + iar.verifiedByUserName = :ashaSupervisorUserName, + iar.approvalDate = :approvalDate, + iar.reason = NULL, + iar.otherReason = NULL +WHERE iar.ashaId = :ashaId +AND iar.isClaimed = true +AND iar.createdDate >= :startDate +AND iar.createdDate < :endDate +""") + int updateApprovalStatusByAshaAndDateRangeForDefaultActivity( + @Param("ashaId") Integer ashaId, + @Param("approvalStatus") Integer approvalStatus, + @Param("startDate") Timestamp startDate, + @Param("endDate") Timestamp endDate, + @Param("approvalDate") Timestamp approvalDate, + @Param("ashaSupervisorUserId") Integer ashaSupervisorUserId, + @Param("ashaSupervisorUserName") String ashaSupervisorUserName + ); + + + @Modifying + @Transactional + @Query("UPDATE IncentiveActivityRecord iar " + + "SET iar.approvalStatus = :status, " + + "iar.verifiedByUserId = :ashaSupervisorUserId, " + + "iar.reason = :reason, " + + "iar.otherReason = :otherReason, " + + "iar.approvalDate = :approvalDate, " + + "iar.verifiedByUserName = :ashaSupervisorUserName " + + "WHERE iar.isClaimed = true " + + "AND iar.ashaId = :ashaId") + int updateApprovalStatusById( + @Param("status") Integer status, + @Param("ashaId") Integer ashaId, + @Param("ashaSupervisorUserId") Integer ashaSupervisorUserId, + @Param("ashaSupervisorUserName") String ashaSupervisorUserName, + @Param("reason") String reason, + @Param("approvalDate") Timestamp approvalDate, + @Param("otherReason") String otherReason + ); + + @Modifying + @Transactional + @Query("UPDATE IncentiveActivityRecord iar " + + "SET iar.isClaimed = :isClaimed, " + + "iar.calimedDate = :calimedDate, " + + "iar.approvalStatus = 102 " + + "WHERE iar.ashaId = :ashaId " + + "AND iar.createdDate >= :startDate " + + "AND iar.createdDate < :endDate") + int updateClaimStatusByAshaAndDateRange( + @Param("ashaId") Integer ashaId, + @Param("isClaimed") Boolean isClaimed, + @Param("calimedDate") Timestamp calimedDate, + @Param("startDate") Timestamp startDate, + @Param("endDate") Timestamp endDate + ); + + @Query("SELECT DISTINCT iar.ashaId FROM IncentiveActivityRecord iar " + + "WHERE iar.calimedDate >= :startDate " + + "AND iar.calimedDate < :endDate " + + "AND iar.isClaimed = true " + + "AND iar.approvalStatus = 101") + List findDistinctAshaIdsByDateRange( + @Param("startDate") Timestamp startDate, + @Param("endDate") Timestamp endDate + ); + + @Query("SELECT iar FROM IncentiveActivityRecord iar " + + "WHERE iar.ashaId = :ashaId " + + "AND iar.calimedDate >= :startDate " + + "AND iar.calimedDate < :endDate " + + "AND iar.isClaimed = true " + + "AND iar.approvalStatus = 101") + List findClaimedApprovedRecordsByAshaAndDateRange( + @Param("ashaId") Integer ashaId, + @Param("startDate") Timestamp startDate, + @Param("endDate") Timestamp endDate + ); + + // RecordRepo — existing records batch mein fetch karo + @Query("SELECT r FROM IncentiveActivityRecord r " + + "WHERE r.activityId = :activityId " + + "AND r.benId IN :benIds " + + "AND r.ashaId = :ashaId") + List findExistingRecords( + @Param("activityId") Long activityId, + @Param("benIds") List benIds, + @Param("ashaId") Integer ashaId + ); + + @Query("SELECT r FROM IncentiveActivityRecord r WHERE r.ashaId = :ashaId " + + "AND r.calimedDate >= :startDate AND r.calimedDate < :endDate " + + "AND (:approvalStatus = 0 OR r.approvalStatus = :approvalStatus)") + List getRecordsBySuperVisor( + @Param("ashaId") Integer ashaId, + @Param("startDate") Timestamp startDate, + @Param("endDate") Timestamp endDate, + @Param("approvalStatus") Integer approvalStatus); } diff --git a/src/main/java/com/iemr/flw/repo/iemr/IncentivesRepo.java b/src/main/java/com/iemr/flw/repo/iemr/IncentivesRepo.java index 112f74ef..d1cbc7f8 100644 --- a/src/main/java/com/iemr/flw/repo/iemr/IncentivesRepo.java +++ b/src/main/java/com/iemr/flw/repo/iemr/IncentivesRepo.java @@ -9,7 +9,9 @@ import org.springframework.stereotype.Repository; import java.sql.Timestamp; +import java.util.Collection; import java.util.List; +import java.util.Set; @Repository public interface IncentivesRepo extends JpaRepository { @@ -28,4 +30,28 @@ IncentiveActivity findIncentiveMasterById( @Query("select record from IncentiveActivityRecord record where record.activityId = :id and record.createdDate = :createdDate and record.benId = :benId") IncentiveActivityRecord findRecordByActivityIdCreatedDateBenId(@Param("id") Long id, @Param("createdDate") Timestamp createdDate, @Param("benId") Long benId); -} + + @Query(value = "SELECT " + + "SUM(CASE WHEN i.approval_status = 101 THEN 1 ELSE 0 END) as pendingCount, " + + "SUM(CASE WHEN i.approval_status = 102 THEN 1 ELSE 0 END) as approvalCount, " + + "SUM(CASE WHEN i.approval_status = 103 THEN 1 ELSE 0 END) as rejectCount " + + "FROM m_incentive_activity i " + + "WHERE i.asha_id = :ashaId " + + "AND i.is_deleted = false", + nativeQuery = true) + Object[] getStatusCountByAshaId(@Param("ashaId") Integer ashaId); + + List findByGroupAndIsDeleted(String group, Boolean isDeleted); + + List findByGroupNotAndIsDeleted(String group, Boolean isDeleted); + + // IncentivesRepo — replaces N calls to findIncentiveMasterById() + @Query("SELECT i.id FROM IncentiveActivity i WHERE i.id IN :ids AND i.isDeleted = false AND " + + "(:isCG = true AND i.group = 'ACTIVITY' OR :isCG = false AND i.group != 'ACTIVITY')") + Set findValidActivityIds(@Param("ids") List ids, @Param("isCG") boolean isCG); + + @Query("SELECT i FROM IncentiveActivity i WHERE i.name IN :names AND i.group = :groupName") + List findIncentiveMasterByNameAndGroup( + @Param("names") List names, + @Param("groupName") String groupName + );} diff --git a/src/main/java/com/iemr/flw/repo/iemr/LeprosyFollowUpRepository.java b/src/main/java/com/iemr/flw/repo/iemr/LeprosyFollowUpRepository.java index 4227fb3a..a2b38f55 100644 --- a/src/main/java/com/iemr/flw/repo/iemr/LeprosyFollowUpRepository.java +++ b/src/main/java/com/iemr/flw/repo/iemr/LeprosyFollowUpRepository.java @@ -12,7 +12,7 @@ @Repository public interface LeprosyFollowUpRepository extends JpaRepository { - Optional findByBenId(Long benId); + List findByBenId(Long benId); // Custom queries can be added here if needed @Query("SELECT s FROM LeprosyFollowUp s WHERE s.createdBy = :createdBy") diff --git a/src/main/java/com/iemr/flw/repo/iemr/MdsrRepo.java b/src/main/java/com/iemr/flw/repo/iemr/MdsrRepo.java index 532a62c9..127954c6 100644 --- a/src/main/java/com/iemr/flw/repo/iemr/MdsrRepo.java +++ b/src/main/java/com/iemr/flw/repo/iemr/MdsrRepo.java @@ -14,7 +14,6 @@ public interface MdsrRepo extends JpaRepository { MDSR findMDSRByBenId(Long benId); - @Query(" SELECT m FROM MDSR m WHERE m.createdBy = :userId and m.createdDate >= :fromDate and m.createdDate <= :toDate") - List getAllMdsrByAshaId(@Param("userId") String userId, - @Param("fromDate") Timestamp fromDate, @Param("toDate") Timestamp toDate); + List findByCreatedBy(String userName); + } diff --git a/src/main/java/com/iemr/flw/repo/iemr/NotificationRepository.java b/src/main/java/com/iemr/flw/repo/iemr/NotificationRepository.java new file mode 100644 index 00000000..c269c97c --- /dev/null +++ b/src/main/java/com/iemr/flw/repo/iemr/NotificationRepository.java @@ -0,0 +1,35 @@ +package com.iemr.flw.repo.iemr; + +import com.iemr.flw.domain.iemr.Notification; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; +import org.springframework.transaction.annotation.Transactional; + +import java.util.Date; + +public interface NotificationRepository extends JpaRepository { + + /** Paged list for a receiver, newest first. */ + Page findByReceiverUserIdOrderByCreatedDateDesc(Integer receiverUserId, Pageable pageable); + + /** Cheap count for badge display. */ + long countByReceiverUserIdAndIsReadFalse(Integer receiverUserId); + + /** Mark a single notification as read. */ + @Modifying + @Transactional + @Query("UPDATE Notification n SET n.isRead = true, n.readDate = :readDate " + + "WHERE n.id = :id") + int markAsRead(@Param("id") Long id, @Param("readDate") Date readDate); + + /** Mark every unread notification for a user as read. */ + @Modifying + @Transactional + @Query("UPDATE Notification n SET n.isRead = true, n.readDate = :readDate " + + "WHERE n.receiverUserId = :receiverUserId AND n.isRead = false") + int markAllAsRead(@Param("receiverUserId") Integer receiverUserId, @Param("readDate") Date readDate); +} \ No newline at end of file diff --git a/src/main/java/com/iemr/flw/repo/iemr/OrsCampaignRepo.java b/src/main/java/com/iemr/flw/repo/iemr/OrsCampaignRepo.java new file mode 100644 index 00000000..fbbe380b --- /dev/null +++ b/src/main/java/com/iemr/flw/repo/iemr/OrsCampaignRepo.java @@ -0,0 +1,17 @@ +package com.iemr.flw.repo.iemr; + +import com.iemr.flw.domain.iemr.CampaignOrs; +import com.iemr.flw.dto.iemr.OrsCampaignDTO; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; +import org.springframework.web.bind.annotation.RequestBody; + +import java.util.List; +import java.util.Optional; + +@Repository +public interface OrsCampaignRepo extends JpaRepository { + Page findByUserId(Integer userId, Pageable pageable); +} diff --git a/src/main/java/com/iemr/flw/repo/iemr/PNCVisitRepo.java b/src/main/java/com/iemr/flw/repo/iemr/PNCVisitRepo.java index 063a0795..39da97da 100644 --- a/src/main/java/com/iemr/flw/repo/iemr/PNCVisitRepo.java +++ b/src/main/java/com/iemr/flw/repo/iemr/PNCVisitRepo.java @@ -12,9 +12,10 @@ @Repository public interface PNCVisitRepo extends JpaRepository { - @Query(value = "SELECT pnc FROM PNCVisit pnc WHERE pnc.createdBy = :userId and pnc.isActive = true and pnc.createdDate >= :fromDate and pnc.createdDate <= :toDate") - List getPNCForPW(@Param("userId") String userId, - @Param("fromDate") Timestamp fromDate, @Param("toDate") Timestamp toDate); + @Query(value = "SELECT pnc FROM PNCVisit pnc WHERE pnc.createdBy = :userId and pnc.isActive = true") + List getPNCForPW(@Param("userId") String userId); PNCVisit findPNCVisitByBenIdAndPncPeriodAndIsActive(Long benId, Integer pncVisit, Boolean isActive); + + List findByBenId(Long benId); } diff --git a/src/main/java/com/iemr/flw/repo/iemr/PulsePolioCampaignRepo.java b/src/main/java/com/iemr/flw/repo/iemr/PulsePolioCampaignRepo.java new file mode 100644 index 00000000..c2cded97 --- /dev/null +++ b/src/main/java/com/iemr/flw/repo/iemr/PulsePolioCampaignRepo.java @@ -0,0 +1,13 @@ +package com.iemr.flw.repo.iemr; + +import com.iemr.flw.domain.iemr.CampaignOrs; +import com.iemr.flw.domain.iemr.PulsePolioCampaign; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface PulsePolioCampaignRepo extends JpaRepository { + Page findByUserId(Integer userId, Pageable pageable); +} diff --git a/src/main/java/com/iemr/flw/repo/iemr/SupervisorDashboardRepo.java b/src/main/java/com/iemr/flw/repo/iemr/SupervisorDashboardRepo.java new file mode 100644 index 00000000..071aeab0 --- /dev/null +++ b/src/main/java/com/iemr/flw/repo/iemr/SupervisorDashboardRepo.java @@ -0,0 +1,193 @@ +package com.iemr.flw.repo.iemr; + +import java.sql.Timestamp; +import java.util.List; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; +import org.springframework.stereotype.Repository; + +import com.iemr.flw.domain.iemr.IncentiveActivityRecord; + +@Repository +public interface SupervisorDashboardRepo extends JpaRepository { + + // Get all ASHA userIDs mapped to a supervisor + @Query(value = "SELECT DISTINCT asm.ashaUserID " + + "FROM asha_supervisor_mapping asm " + + "WHERE asm.supervisorUserID = :supervisorUserID " + + "AND asm.deleted = false", nativeQuery = true) + List getAshaIdsBySupervisor(@Param("supervisorUserID") Integer supervisorUserID); + + @Query(value = "SELECT DISTINCT usrm.FacilityID " + + "FROM m_UserServiceRoleMapping usrm " + + "WHERE usrm.UserID = :userID AND usrm.Deleted = false " + + "AND usrm.FacilityID IS NOT NULL", nativeQuery = true) + List getUserFacilityIDs(@Param("userID") Integer userID); + + // Get ASHAs with facility info for a supervisor + @Query(value = "SELECT DISTINCT u.UserID, u.FirstName, u.LastName, " + + "COALESCE(u.EmployeeID,'') AS employeeID, " + + "f.FacilityID, f.FacilityName, " + + "COALESCE(ft.FacilityTypeName,'') AS facilityTypeName, " + + "COALESCE(u.ContactNo,'') AS mobile " + + "FROM asha_supervisor_mapping asm " + + "JOIN m_User u ON u.UserID = asm.ashaUserID " + + "JOIN m_facility f ON f.FacilityID = asm.facilityID " + + "LEFT JOIN m_facilitytype ft ON ft.FacilityTypeID = f.FacilityTypeID " + + "WHERE asm.supervisorUserID = :supervisorUserID " + + "AND asm.deleted = false AND u.Deleted = false AND f.Deleted = false", nativeQuery = true) + List getAshasWithFacilityInfo(@Param("supervisorUserID") Integer supervisorUserID); + + + @Query(value = "SELECT DISTINCT u.UserID, u.FirstName, u.LastName, " + + "COALESCE(u.EmployeeID,'') AS employeeID, " + + "f.FacilityID, f.FacilityName, " + + "COALESCE(ft.FacilityTypeName,'') AS facilityTypeName, " + + "COALESCE(u.ContactNo,'') AS mobile " + + "FROM m_UserServiceRoleMapping usrm " + + "JOIN m_User u ON u.UserID = usrm.UserID " + + "JOIN m_Role r ON r.RoleID = usrm.RoleID " + + "JOIN m_facility f ON f.FacilityID = usrm.FacilityID " + + "LEFT JOIN m_facilitytype ft ON ft.FacilityTypeID = f.FacilityTypeID " + + "WHERE usrm.FacilityID IN :facilityIDs " + + "AND r.RoleName = 'ASHA' " + + "AND usrm.Deleted = false AND u.Deleted = false AND f.Deleted = false", nativeQuery = true) + List getAshaListByFacilities(@Param("facilityIDs") List facilityIDs); + + @Query(value = """ + SELECT asm.supervisorUserID + FROM asha_supervisor_mapping asm + WHERE asm.ashaUserID = :ashaUserID + AND asm.deleted = false + LIMIT 1 + """, nativeQuery = true) + Integer getSupervisorUserIdByAshaId(@Param("ashaUserID") Integer ashaUserID); + + // Unclaimed incentive count per ASHA + @Query(value = "SELECT iar.asha_id, " + + "COUNT(*) AS unclaimedCount " + + "FROM incentive_activity_record iar " + + "WHERE iar.asha_id IN (:ashaIds) " + + "AND iar.created_date >= :startDate " + + "AND iar.created_date < :endDate " + + "AND (iar.is_claimed = false OR iar.is_claimed IS NULL) " + + "GROUP BY iar.asha_id", + nativeQuery = true) + List getUnclaimedCountByAshaIds( + @Param("ashaIds") List ashaIds, + @Param("startDate") Timestamp startDate, + @Param("endDate") Timestamp endDate); + + + @Query(value = "SELECT COUNT(*) " + + "FROM incentive_activity_record iar " + + "WHERE iar.asha_id = :ashaId " + + "AND iar.created_date >= :startDate " + + "AND iar.created_date < :endDate " + + "AND (iar.is_claimed = false OR iar.is_claimed IS NULL)", + nativeQuery = true) + Long getUnclaimedCountByAshaId( + @Param("ashaId") Integer ashaId, + @Param("startDate") Timestamp startDate, + @Param("endDate") Timestamp endDate); + + // Get facility details with geo names + @Query(value = "SELECT DISTINCT f.FacilityID, f.FacilityName, " + + "COALESCE(s.StateName,'') AS stateName, " + + "COALESCE(d.DistrictName,'') AS districtName, " + + "COALESCE(b.BlockName,'') AS blockName, " + + "COALESCE(f.RuralUrban,'') AS ruralUrban, " + + "COALESCE(ft.FacilityTypeName,'') AS facilityTypeName " + + "FROM m_facility f " + + "LEFT JOIN m_state s ON s.StateID = f.StateID " + + "LEFT JOIN m_district d ON d.DistrictID = f.DistrictID " + + "LEFT JOIN m_districtblock b ON b.BlockID = f.BlockID " + + "LEFT JOIN m_facilitytype ft ON ft.FacilityTypeID = f.FacilityTypeID " + + "WHERE f.FacilityID IN :facilityIDs AND f.Deleted = false", nativeQuery = true) + List getFacilityDetails(@Param("facilityIDs") List facilityIDs); + + // Villages mapped to facilities + @Query(value = "SELECT fvm.FacilityID, fvm.DistrictBranchID, dbm.VillageName " + + "FROM facility_village_mapping fvm " + + "JOIN m_DistrictBranchMapping dbm ON dbm.DistrictBranchID = fvm.DistrictBranchID " + + "WHERE fvm.FacilityID IN :facilityIDs AND fvm.Deleted = false", nativeQuery = true) + List getVillagesForFacilities(@Param("facilityIDs") List facilityIDs); + + // Incentive status counts per ASHA: verified, rejected, pending, total amount + @Query(value = "SELECT iar.asha_id, " + + "COUNT(*) AS totalRecords, " + + "SUM(CASE WHEN iar.approval_status = 101 THEN 1 ELSE 0 END) AS verified, " + + "SUM(CASE WHEN iar.approval_status = 103 THEN 1 ELSE 0 END) AS rejected, " + + "SUM(CASE WHEN iar.approval_status = 102 THEN 1 ELSE 0 END) AS pending, " + + "SUM(CASE WHEN iar.approval_status IN (101, 105) THEN 1 ELSE 0 END) AS approved " + + "FROM incentive_activity_record iar " + + "WHERE iar.asha_id IN (:ashaIds) " + + "AND iar.created_date >= :startDate " + + "AND iar.is_claimed = true " + + "AND iar.created_date < :endDate " + + "GROUP BY iar.asha_id", + nativeQuery = true) + List getIncentiveStatusByAshaIds( + @Param("ashaIds") List ashaIds, + @Param("startDate") Timestamp startDate, + @Param("endDate") Timestamp endDate); + + + + @Query(value = "SELECT iar.asha_id, " + + "COUNT(*) AS totalRecords, " + + "SUM(CASE WHEN iar.approval_status = 101 THEN 1 ELSE 0 END) AS verified, " + + "SUM(CASE WHEN iar.approval_status = 103 THEN 1 ELSE 0 END) AS rejected, " + + "SUM(CASE WHEN iar.approval_status = 102 THEN 1 ELSE 0 END) AS pending, " + + "SUM(CASE WHEN iar.approval_status IN (102,105) THEN 1 ELSE 0 END) AS approved " + + "FROM incentive_activity_record iar " + + "WHERE iar.asha_id IN (:ashaIds) " + + "AND iar.created_date >= :startDate " + + "AND iar.is_claimed = true " + + "AND iar.created_date < :endDate " + + "GROUP BY iar.asha_id", + nativeQuery = true) + List getIncentiveStatusByAshaIdsForAnm( + @Param("ashaIds") List ashaIds, + @Param("startDate") Timestamp startDate, + @Param("endDate") Timestamp endDate); + + @Query(value = "SELECT iar.asha_id, " + + "COUNT(*) AS totalRecords, " + + "SUM(CASE WHEN iar.approval_status = 101 THEN 1 ELSE 0 END) AS verified, " + + "SUM(CASE WHEN iar.approval_status = 103 THEN 1 ELSE 0 END) AS rejected, " + + "SUM(CASE WHEN iar.approval_status = 102 THEN 1 ELSE 0 END) AS pending, " + + "SUM(CASE WHEN iar.approval_status IN (101, 105) THEN 1 ELSE 0 END) AS approved " + + "FROM incentive_activity_record iar " + + "WHERE iar.asha_id IN (:ashaIds) " + + "AND iar.created_date >= :startDate " + + "AND iar.is_claimed = true " + + "AND iar.is_default_activity = true " + + "AND iar.created_date < :endDate " + + "GROUP BY iar.asha_id", + nativeQuery = true) + List getDefaultIncentiveStatusByAshaIds( + @Param("ashaIds") List ashaIds, + @Param("startDate") Timestamp startDate, + @Param("endDate") Timestamp endDate); + + // Incentive activity history per ASHA (recent records) + @Query(value = "SELECT iar.asha_id, iar.name AS activityName, " + + "iar.amount, iar.is_eligible, iar.created_date " + + "FROM incentive_activity_record iar " + + "WHERE iar.asha_id IN :ashaIds " + + "ORDER BY iar.created_date DESC", nativeQuery = true) + List getIncentiveHistoryByAshaIds(@Param("ashaIds") List ashaIds); + + // Supervisor user details + @Query(value = "SELECT u.UserID, u.FirstName, u.LastName, " + + "COALESCE(u.AgentID,'') AS agentID, " + + "COALESCE(u.EmergencyContactNo,'') AS mobile, " + + "COALESCE(g.GenderName,'') AS gender " + + "FROM m_User u " + + "LEFT JOIN m_gender g ON g.GenderID = u.GenderID " + + "WHERE u.UserID = :userId AND u.Deleted = false", nativeQuery = true) + List getSupervisorUserDetails(@Param("userId") Integer userId); +} \ No newline at end of file diff --git a/src/main/java/com/iemr/flw/repo/iemr/TBConfirmedTreatmentRepository.java b/src/main/java/com/iemr/flw/repo/iemr/TBConfirmedTreatmentRepository.java new file mode 100644 index 00000000..31345dd4 --- /dev/null +++ b/src/main/java/com/iemr/flw/repo/iemr/TBConfirmedTreatmentRepository.java @@ -0,0 +1,17 @@ +package com.iemr.flw.repo.iemr; + +import com.iemr.flw.domain.iemr.TBConfirmedCase; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.List; +import java.util.Optional; + +@Repository +public interface TBConfirmedTreatmentRepository + extends JpaRepository { + + List findByBenId(Long benId); + List findByUserId(Integer benId); + +} diff --git a/src/main/java/com/iemr/flw/repo/iemr/TBScreeningRepo.java b/src/main/java/com/iemr/flw/repo/iemr/TBScreeningRepo.java index e2f7fed0..61be2a57 100644 --- a/src/main/java/com/iemr/flw/repo/iemr/TBScreeningRepo.java +++ b/src/main/java/com/iemr/flw/repo/iemr/TBScreeningRepo.java @@ -15,6 +15,6 @@ public interface TBScreeningRepo extends JpaRepository { @Query(value = "SELECT tbs FROM TBScreening tbs WHERE tbs.benId = :benId and tbs.userId = :userId") TBScreening getByUserIdAndBenId(@Param("benId") Long benId, @Param("userId") Integer userId); - @Query(value = "SELECT tbs FROM TBScreening tbs WHERE tbs.userId = :userId and tbs.visitDate >= :fromDate and tbs.visitDate <= :toDate") - List getByUserId(@Param("userId") Integer userId, @Param("fromDate") Timestamp fromDate, @Param("toDate") Timestamp toDate); + @Query(value = "SELECT tbs FROM TBScreening tbs WHERE tbs.userId = :userId") + List getByUserId(@Param("userId") Integer userId); } diff --git a/src/main/java/com/iemr/flw/repo/iemr/UserFcmTokenRepo.java b/src/main/java/com/iemr/flw/repo/iemr/UserFcmTokenRepo.java new file mode 100644 index 00000000..3426f5a2 --- /dev/null +++ b/src/main/java/com/iemr/flw/repo/iemr/UserFcmTokenRepo.java @@ -0,0 +1,34 @@ +/* +* AMRIT – Accessible Medical Records via Integrated Technology +* Integrated EHR (Electronic Health Records) Solution +* +* Copyright (C) "Piramal Swasthya Management and Research Institute" +* +* This file is part of AMRIT. +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see https://www.gnu.org/licenses/. +* +/* +* AMRIT – Accessible Medical Records via Integrated Technology +*/ +package com.iemr.flw.repo.iemr; + +import com.iemr.flw.domain.iemr.UserFcmTokenData; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface UserFcmTokenRepo extends JpaRepository { + UserFcmTokenData findByUserId(Integer userId); +} diff --git a/src/main/java/com/iemr/flw/repo/iemr/UserServiceRoleRepo.java b/src/main/java/com/iemr/flw/repo/iemr/UserServiceRoleRepo.java index 899a7ffd..2b78b68f 100644 --- a/src/main/java/com/iemr/flw/repo/iemr/UserServiceRoleRepo.java +++ b/src/main/java/com/iemr/flw/repo/iemr/UserServiceRoleRepo.java @@ -18,10 +18,22 @@ public interface UserServiceRoleRepo extends JpaRepository getUserRole(@Param("userId") Integer userId); - @Query("select u.userId from UserServiceRole u where u.userName = :userName and u.userServciceRoleDeleted = false") + @Query(value = """ + SELECT UserID + FROM db_iemr.v_userservicerolemapping + WHERE UserName = :userName + AND UserServciceRoleDeleted = 0 + LIMIT 1 + """, nativeQuery = true) Integer getUserIdByName(@Param("userName") String userName); - @Query("select u.userName from UserServiceRole u where u.userId = :userId and u.userServciceRoleDeleted = false") - String getUserNamedByUserId(@Param("userId") Integer userId); + @Query(value = """ + SELECT UserName + FROM db_iemr.v_userservicerolemapping + WHERE UserID = :userId + AND UserServciceRoleDeleted = 0 + LIMIT 1 + """, nativeQuery = true) + String getUserNamedByUserId(@Param("userId") Integer userId); } diff --git a/src/main/java/com/iemr/flw/service/AbhaBeneficiaryService.java b/src/main/java/com/iemr/flw/service/AbhaBeneficiaryService.java new file mode 100644 index 00000000..02a1281b --- /dev/null +++ b/src/main/java/com/iemr/flw/service/AbhaBeneficiaryService.java @@ -0,0 +1,12 @@ +package com.iemr.flw.service; + +import com.iemr.flw.dto.abhaBeneficiary.AbhaBeneficiaryDTO; +import com.iemr.flw.dto.iemr.AbhaRequestDTO; +import org.springframework.stereotype.Service; + +import java.util.List; + +public interface AbhaBeneficiaryService { + + Object getBeneficiaryByAbha(AbhaRequestDTO request); +} diff --git a/src/main/java/com/iemr/flw/service/AbhaTokenService.java b/src/main/java/com/iemr/flw/service/AbhaTokenService.java new file mode 100644 index 00000000..dc0affae --- /dev/null +++ b/src/main/java/com/iemr/flw/service/AbhaTokenService.java @@ -0,0 +1,7 @@ +package com.iemr.flw.service; + +import java.util.Map; + +public interface AbhaTokenService { + Map getAbhaToken() throws Exception; +} diff --git a/src/main/java/com/iemr/flw/service/BeneficiaryService.java b/src/main/java/com/iemr/flw/service/BeneficiaryService.java index 1db3026b..e69bc887 100644 --- a/src/main/java/com/iemr/flw/service/BeneficiaryService.java +++ b/src/main/java/com/iemr/flw/service/BeneficiaryService.java @@ -10,7 +10,7 @@ public interface BeneficiaryService { String getBenData(GetBenRequestHandler requestDTO, String authorisation) throws Exception; - String saveEyeCheckupVsit(List eyeCheckupRequestDTOS); + String saveEyeCheckupVsit(List eyeCheckupRequestDTOS,String token); - List getEyeCheckUpVisit(GetBenRequestHandler request); + List getEyeCheckUpVisit(GetBenRequestHandler request,String token); } diff --git a/src/main/java/com/iemr/flw/service/CampaignService.java b/src/main/java/com/iemr/flw/service/CampaignService.java new file mode 100644 index 00000000..26c67a6a --- /dev/null +++ b/src/main/java/com/iemr/flw/service/CampaignService.java @@ -0,0 +1,24 @@ +package com.iemr.flw.service; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.iemr.flw.domain.iemr.CampaignOrs; +import com.iemr.flw.domain.iemr.FilariasisCampaign; +import com.iemr.flw.domain.iemr.PulsePolioCampaign; +import com.iemr.flw.dto.iemr.*; +import com.iemr.flw.utils.exception.IEMRException; + +import java.util.List; + +public interface CampaignService { + List saveOrsCampaign(List orsCampaignDTO, String token) throws IEMRException, JsonProcessingException; + + List savePolioCampaign(List polioCampaignDTOS, String token) throws IEMRException, JsonProcessingException; + + List getOrsCampaign(String token) throws IEMRException; + + List getPolioCampaign(String token) throws IEMRException; + + List saveFilariasisCampaign(List filariasisCampaignDTOS, String token) throws IEMRException, JsonProcessingException; + + List getAllFilariasisCampaign(String token) throws IEMRException; +} diff --git a/src/main/java/com/iemr/flw/service/ChildCareService.java b/src/main/java/com/iemr/flw/service/ChildCareService.java index bbd5c96f..c78de3fd 100644 --- a/src/main/java/com/iemr/flw/service/ChildCareService.java +++ b/src/main/java/com/iemr/flw/service/ChildCareService.java @@ -15,7 +15,7 @@ public interface ChildCareService { List getHBNCDetails(GetBenRequestHandler dto); - String saveHBNCDetails(List hbncRequestDTOs); + String saveHBNCDetails(List hbncRequestDTOs,Integer userId); List getChildVaccinationDetails(GetBenRequestHandler dto); @@ -23,7 +23,7 @@ public interface ChildCareService { List getAllChildVaccines(String category); - String saveSamDetails(List samRequest); + String saveSamDetails(List samRequest,Integer userId,String userName); List getSamVisitsByBeneficiary(GetBenRequestHandler dto); @@ -31,7 +31,7 @@ public interface ChildCareService { List getOrdDistrubtion(GetBenRequestHandler request); - List saveAllIfa(List dtoList); + List saveAllIfa(List dtoList,Integer userId); List getByBeneficiaryId(GetBenRequestHandler request); diff --git a/src/main/java/com/iemr/flw/service/DiseaseControlService.java b/src/main/java/com/iemr/flw/service/DiseaseControlService.java index 92accf4a..65e315dc 100644 --- a/src/main/java/com/iemr/flw/service/DiseaseControlService.java +++ b/src/main/java/com/iemr/flw/service/DiseaseControlService.java @@ -24,7 +24,9 @@ */ package com.iemr.flw.service; +import com.iemr.flw.dto.identity.GetBenRequestHandler; import com.iemr.flw.dto.iemr.*; +import com.iemr.flw.utils.exception.IEMRException; import java.util.List; @@ -44,4 +46,7 @@ public interface DiseaseControlService { public String saveLeprosyFollowUp(LeprosyFollowUpDTO leprosyDTO); List getAllLeprosyFollowUpData(String createdBy); + List saveChronicDiseaseVisit(List requestList,String token) throws IEMRException; + + List getCdtfVisits(GetBenRequestHandler getBenRequestHandler); } \ No newline at end of file diff --git a/src/main/java/com/iemr/flw/service/EmployeeMasterInter.java b/src/main/java/com/iemr/flw/service/EmployeeMasterInter.java index 1c4369da..4e81445c 100644 --- a/src/main/java/com/iemr/flw/service/EmployeeMasterInter.java +++ b/src/main/java/com/iemr/flw/service/EmployeeMasterInter.java @@ -1,13 +1,13 @@ package com.iemr.flw.service; -import com.iemr.flw.domain.iemr.M_User; +import com.iemr.flw.domain.iemr.User; import org.springframework.stereotype.Service; import java.util.List; @Service public interface EmployeeMasterInter { - public M_User getUserDetails(Integer userID); + public User getUserDetails(Integer userID); - List getAllUsers(); + List getAllUsers(); } diff --git a/src/main/java/com/iemr/flw/service/IFAFormSubmissionService.java b/src/main/java/com/iemr/flw/service/IFAFormSubmissionService.java index 1ecd338d..d94fbd68 100644 --- a/src/main/java/com/iemr/flw/service/IFAFormSubmissionService.java +++ b/src/main/java/com/iemr/flw/service/IFAFormSubmissionService.java @@ -9,6 +9,6 @@ import java.util.List; @Service public interface IFAFormSubmissionService { - String saveFormData(List requests); + String saveFormData(List requests,Integer userId); List getFormData(GetBenRequestHandler getBenRequestHandler); } diff --git a/src/main/java/com/iemr/flw/service/IRSRoundService.java b/src/main/java/com/iemr/flw/service/IRSRoundService.java index d45cd621..438702d5 100644 --- a/src/main/java/com/iemr/flw/service/IRSRoundService.java +++ b/src/main/java/com/iemr/flw/service/IRSRoundService.java @@ -8,7 +8,7 @@ @Service public interface IRSRoundService { - List addRounds(List dtos); + List addRounds(List dtos,Integer userId,String userName); List getRounds(Long householdId); } diff --git a/src/main/java/com/iemr/flw/service/IncentiveLogicService.java b/src/main/java/com/iemr/flw/service/IncentiveLogicService.java new file mode 100644 index 00000000..8becce19 --- /dev/null +++ b/src/main/java/com/iemr/flw/service/IncentiveLogicService.java @@ -0,0 +1,35 @@ +package com.iemr.flw.service; + +import com.iemr.flw.domain.iemr.IncentiveActivityRecord; +import org.springframework.stereotype.Service; + +import java.sql.Timestamp; +import java.util.Date; + +public interface IncentiveLogicService { + public IncentiveActivityRecord incentiveForLeprosyPaucibacillaryConfirmed(Long benId, Date treatmentStartDate, Date treatmentEndDate, Integer userId); + public IncentiveActivityRecord incentiveForIdentificationLeprosy(Long benId, Date treatmentStartDate, Date treatmentEndDate, Integer userId); + public IncentiveActivityRecord incentiveForLeprosyMultibacillaryConfirmed(Long benId, Date treatmentStartDate, Date treatmentEndDate, Integer userId); + + public IncentiveActivityRecord incentiveForVhndMeeting(Long benId, Date treatmentStartDate, Date treatmentEndDate, Integer userId); + + public IncentiveActivityRecord incentiveForClusterMeeting(Long benId, Date treatmentStartDate, Date treatmentEndDate, Integer userId); + public IncentiveActivityRecord incentiveForAttendingVhsnc(Long benId, Date treatmentStartDate, Date treatmentEndDate, Integer userId); + + IncentiveActivityRecord incentiveForChildBirthGap(Long benId, Date treatmentStartDate, Date treatmentEndDate, Integer userId); + + IncentiveActivityRecord incentiveForSecondChildGap(Long benId, Timestamp secondChildDob, Timestamp secondChildDob1, Integer userId); + + IncentiveActivityRecord incentiveForEyeSurgeyReferGovtHospital(Long benId, Timestamp treatmentStartDate, Timestamp treatmentEndDate, Integer userId); + IncentiveActivityRecord incentiveForEyeSurgeyReferPrivateHospital(Long benId, Timestamp treatmentStartDate, Timestamp treatmentEndDate, Integer userId); + + IncentiveActivityRecord incentiveForMalariaFollowUp(Long benId, Timestamp treatmentStartDate, Timestamp treatmentEndDate, Integer userId); + + IncentiveActivityRecord incentiveForTbFollowUp(Long benId, Timestamp treatmentStartDate, Timestamp treatmentEndDate, Integer userId); + + IncentiveActivityRecord incentiveForTbFollowUpIsDrTb(Long benId, Timestamp treatmentStartDate, Timestamp treatmentEndDate, Integer userId); + + IncentiveActivityRecord incentiveForGiveingIFA(Long benId, Timestamp treatmentStartDate, Timestamp treatmentEndDate, Integer userId); + + IncentiveActivityRecord incentiveForTbSuspected(Long benId, Timestamp treatmentStartDate, Timestamp treatmentEndDate, Integer userId); +} diff --git a/src/main/java/com/iemr/flw/service/IncentiveService.java b/src/main/java/com/iemr/flw/service/IncentiveService.java index ab49a0cd..01da2431 100644 --- a/src/main/java/com/iemr/flw/service/IncentiveService.java +++ b/src/main/java/com/iemr/flw/service/IncentiveService.java @@ -3,6 +3,7 @@ import com.iemr.flw.dto.identity.GetBenRequestHandler; import com.iemr.flw.dto.iemr.IncentiveActivityDTO; import com.iemr.flw.dto.iemr.IncentiveRequestDTO; +import com.iemr.flw.dto.iemr.PendingActivityDTO; import java.util.List; @@ -13,4 +14,12 @@ public interface IncentiveService { String getIncentiveMaster(IncentiveRequestDTO incentiveRequestDTO); String getAllIncentivesByUserId(GetBenRequestHandler requestDTO); -} + String getAllIncentivesGroupedSummary(GetBenRequestHandler requestDTO,Integer userId); + String getAllIncentivesGroupedActivity(GetBenRequestHandler requestDTO); + + String updateIncentive(PendingActivityDTO pendingActivityDTO); + public String updateClaimStatus(Integer ashaId, + Integer month, + Integer year, + Boolean isClaimed, + String token);} diff --git a/src/main/java/com/iemr/flw/service/MaaMeetingService.java b/src/main/java/com/iemr/flw/service/MaaMeetingService.java index d5f4c5bd..cfe6aa20 100644 --- a/src/main/java/com/iemr/flw/service/MaaMeetingService.java +++ b/src/main/java/com/iemr/flw/service/MaaMeetingService.java @@ -3,28 +3,20 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; -import com.iemr.flw.domain.iemr.IncentiveActivity; -import com.iemr.flw.domain.iemr.IncentiveActivityRecord; -import com.iemr.flw.domain.iemr.MaaMeeting; -import com.iemr.flw.domain.iemr.UwinSession; +import com.iemr.flw.domain.iemr.*; import com.iemr.flw.dto.identity.GetBenRequestHandler; import com.iemr.flw.dto.iemr.MaaMeetingRequestDTO; import com.iemr.flw.dto.iemr.MaaMeetingResponseDTO; import com.iemr.flw.masterEnum.GroupName; -import com.iemr.flw.repo.iemr.IncentiveRecordRepo; -import com.iemr.flw.repo.iemr.IncentivesRepo; -import com.iemr.flw.repo.iemr.MaaMeetingRepository; -import com.iemr.flw.repo.iemr.UserServiceRoleRepo; +import com.iemr.flw.repo.iemr.*; +import jakarta.persistence.EntityNotFoundException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; import java.sql.Timestamp; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Base64; -import java.util.List; +import java.util.*; import java.util.stream.Collectors; @Service @@ -40,6 +32,9 @@ public class MaaMeetingService { private final MaaMeetingRepository repository; private final ObjectMapper objectMapper; + @Autowired + private IncentivePendingActivityRepository incentivePendingActivityRepository; + public MaaMeetingService(MaaMeetingRepository repository, ObjectMapper objectMapper) { this.repository = repository; this.objectMapper = objectMapper; @@ -51,6 +46,11 @@ public MaaMeeting saveMeeting(MaaMeetingRequestDTO req) throws Exception { meeting.setPlace(req.getPlace()); meeting.setParticipants(req.getParticipants()); meeting.setAshaId(req.getAshaId()); + meeting.setNoOfLactingMother(req.getNoOfLactingMother()); + meeting.setNoOfPragnentWomen(req.getNoOfPragnentWomen()); + meeting.setVillageName(req.getVillageName()); + meeting.setMitaninActivityCheckList(req.getMitaninActivityCheckList()); + meeting.setCreatedBy(req.getCreatedBY()); // Convert meeting images to Base64 JSON @@ -69,12 +69,93 @@ public MaaMeeting saveMeeting(MaaMeetingRequestDTO req) throws Exception { String imagesJson = objectMapper.writeValueAsString(base64Images); meeting.setMeetingImagesJson(imagesJson); } + checkAndAddIncentive(meeting); return repository.save(meeting); } + public MaaMeeting updateMeeting(MaaMeetingRequestDTO req) throws JsonProcessingException { + MaaMeeting existingMeeting = repository.findById(req.getId()) + .orElseThrow(() -> new EntityNotFoundException("Meeting not found: " + req.getId())); + + // ✅ NULL CHECK + if (req.getMeetingDate() != null) { + existingMeeting.setMeetingDate(req.getMeetingDate()); + } + if (req.getPlace() != null) { + existingMeeting.setPlace(req.getPlace()); + } + if (req.getParticipants() != null) { + existingMeeting.setParticipants(req.getParticipants()); + } + if (req.getAshaId() != null) { + existingMeeting.setAshaId(req.getAshaId()); + } + if (req.getCreatedBY() != null) { // ✅ Typo fixed: CreatedBY → CreatedBy + existingMeeting.setCreatedBy(req.getCreatedBY()); + } + + // Images - only if provided + if (req.getMeetingImages() != null && req.getMeetingImages().length > 0) { + List base64Images = Arrays.stream(req.getMeetingImages()) + .filter(file -> file != null && !file.isEmpty()) + .map(this::convertToBase64) + .collect(Collectors.toList()); + existingMeeting.setMeetingImagesJson(objectMapper.writeValueAsString(base64Images)); + } + + checkAndAddIncentive(existingMeeting); + + return repository.save(existingMeeting); + } + + public MaaMeeting updateMeetingFromFileUpload(MaaMeetingRequestDTO req, Long incentiveRecordId) throws JsonProcessingException { + MaaMeeting existingMeeting = repository.findById(req.getId()) + .orElseThrow(() -> new EntityNotFoundException("Meeting not found: " + req.getId())); + + // ✅ NULL CHECK + if (req.getMeetingDate() != null) { + existingMeeting.setMeetingDate(req.getMeetingDate()); + } + if (req.getPlace() != null) { + existingMeeting.setPlace(req.getPlace()); + } + if (req.getParticipants() != null) { + existingMeeting.setParticipants(req.getParticipants()); + } + if (req.getAshaId() != null) { + existingMeeting.setAshaId(req.getAshaId()); + } + if (req.getCreatedBY() != null) { // ✅ Typo fixed: CreatedBY → CreatedBy + existingMeeting.setCreatedBy(req.getCreatedBY()); + } + + // Images - only if provided + if (req.getMeetingImages() != null && req.getMeetingImages().length > 0) { + List base64Images = Arrays.stream(req.getMeetingImages()) + .filter(file -> file != null && !file.isEmpty()) + .map(this::convertToBase64) + .collect(Collectors.toList()); + existingMeeting.setMeetingImagesJson(objectMapper.writeValueAsString(base64Images)); + } + + if (existingMeeting.getMeetingImagesJson() != null) { + checkAndUpdateIncentive(incentiveRecordId); + + } + return repository.save(existingMeeting); + } + + + private String convertToBase64(MultipartFile file) { + try { + return Base64.getEncoder().encodeToString(file.getBytes()); + } catch (IOException e) { + throw new RuntimeException("Failed to convert image to Base64: " + file.getOriginalFilename(), e); + } + } public List getAllMeetings(GetBenRequestHandler getBenRequestHandler) throws Exception { @@ -87,13 +168,18 @@ public List getAllMeetings(GetBenRequestHandler getBenReq dto.setPlace(meeting.getPlace()); dto.setParticipants(meeting.getParticipants()); dto.setAshaId(meeting.getAshaId()); + dto.setVillageName(meeting.getVillageName()); + dto.setNoOfLactingMother(String.valueOf(meeting.getNoOfLactingMother())); + dto.setNoOfPragnentWomen(String.valueOf(meeting.getNoOfPragnentWomen())); + dto.setMitaninActivityCheckList(meeting.getMitaninActivityCheckList()); dto.setCreatedBy(meeting.getCreatedBy()); try { if (meeting.getMeetingImagesJson() != null) { List base64Images = objectMapper.readValue( meeting.getMeetingImagesJson(), - new TypeReference>() {} + new TypeReference>() { + } ); dto.setMeetingImages(base64Images); @@ -107,22 +193,39 @@ public List getAllMeetings(GetBenRequestHandler getBenReq return dto; }).collect(Collectors.toList()); } + + private void updatePendingActivity(Integer userId, Long recordId, Long activityId, Long mIncentiveId) { + IncentivePendingActivity incentivePendingActivity = new IncentivePendingActivity(); + incentivePendingActivity.setActivityId(activityId); + incentivePendingActivity.setRecordId(recordId); + incentivePendingActivity.setUserId(userId); + incentivePendingActivity.setMincentiveId(mIncentiveId); + if (incentivePendingActivity != null) { + incentivePendingActivityRepository.save(incentivePendingActivity); + } + + } + + private void checkAndUpdateIncentive(Long incentiveId) { + updateIncentive(incentiveId); + } + private void checkAndAddIncentive(MaaMeeting meeting) { IncentiveActivity incentiveActivityAM = incentivesRepo.findIncentiveMasterByNameAndGroup("MAA_QUARTERLY_MEETING", GroupName.CHILD_HEALTH.getDisplayName()); IncentiveActivity incentiveActivityCH = incentivesRepo.findIncentiveMasterByNameAndGroup("MAA_QUARTERLY_MEETING", GroupName.ACTIVITY.getDisplayName()); - if(incentiveActivityAM!=null){ - addIncentive(incentiveActivityAM,meeting); - } - if(incentiveActivityCH!=null){ - addIncentive(incentiveActivityCH,meeting); + if (incentiveActivityAM != null) { + addIncentive(incentiveActivityAM, meeting); + } + if (incentiveActivityCH != null) { + addIncentive(incentiveActivityCH, meeting); - } + } } - private void addIncentive(IncentiveActivity incentiveActivity,MaaMeeting meeting){ + private void addIncentive(IncentiveActivity incentiveActivity, MaaMeeting meeting) { IncentiveActivityRecord record = recordRepo - .findRecordByActivityIdCreatedDateBenId(incentiveActivity.getId(), Timestamp.valueOf(meeting.getMeetingDate().atStartOfDay()), 0L,meeting.getAshaId()); + .findRecordByActivityIdCreatedDateBenId(incentiveActivity.getId(), Timestamp.valueOf(meeting.getMeetingDate().atStartOfDay()), 0L, meeting.getAshaId()); if (record == null) { record = new IncentiveActivityRecord(); @@ -136,9 +239,37 @@ record = new IncentiveActivityRecord(); record.setBenId(0L); record.setAshaId(meeting.getAshaId()); record.setAmount(Long.valueOf(incentiveActivity.getRate())); + record.setIsEligible(true); recordRepo.save(record); + + +// if (meeting.getMeetingImagesJson() != null) { +// record.setIsEligible(true); +// recordRepo.save(record); +// +// } else { +// record.setIsEligible(false); +// IncentiveActivityRecord activityRecord = recordRepo.save(record); +// if (activityRecord != null) { +// updatePendingActivity(meeting.getAshaId(), meeting.getId(), activityRecord.getId(), incentiveActivity.getId()); +// +// } +// +// } } } + private void updateIncentive(Long id) { + + Optional optionalRecord = recordRepo.findById(id); + + if (optionalRecord.isPresent()) { + IncentiveActivityRecord record = optionalRecord.get(); + record.setIsEligible(true); + recordRepo.save(record); + } + } + + } diff --git a/src/main/java/com/iemr/flw/service/MalariaFollowUpService.java b/src/main/java/com/iemr/flw/service/MalariaFollowUpService.java index eb979318..324c6cd6 100644 --- a/src/main/java/com/iemr/flw/service/MalariaFollowUpService.java +++ b/src/main/java/com/iemr/flw/service/MalariaFollowUpService.java @@ -9,7 +9,7 @@ public interface MalariaFollowUpService { - public Boolean saveFollowUp(MalariaFollowUpDTO dto) ; + public Boolean saveFollowUp(MalariaFollowUpDTO dto,String token) ; List getByUserId(Integer userId); diff --git a/src/main/java/com/iemr/flw/service/MaternalHealthService.java b/src/main/java/com/iemr/flw/service/MaternalHealthService.java index c4af2768..5e4e9b61 100644 --- a/src/main/java/com/iemr/flw/service/MaternalHealthService.java +++ b/src/main/java/com/iemr/flw/service/MaternalHealthService.java @@ -2,6 +2,7 @@ import com.iemr.flw.dto.identity.GetBenRequestHandler; import com.iemr.flw.dto.iemr.*; +import com.iemr.flw.utils.exception.IEMRException; import org.springframework.stereotype.Component; import java.util.List; @@ -15,7 +16,7 @@ public interface MaternalHealthService { List getANCVisits(GetBenRequestHandler dto); - String saveANCVisit(List ancVisitDTOs); + String saveANCVisit(List ancVisitDTOs,Integer useId ); List getPmsmaRecords(GetBenRequestHandler dto); @@ -25,4 +26,7 @@ public interface MaternalHealthService { String savePNCVisit(List pncVisitDTOs); + String saveANCVisitQuestions(List ancVisitQuestionsDTOS, String authorization) throws IEMRException; + + List getANCCounselling(GetBenRequestHandler requestDTO); } diff --git a/src/main/java/com/iemr/flw/service/NotificationService.java b/src/main/java/com/iemr/flw/service/NotificationService.java new file mode 100644 index 00000000..4d561b51 --- /dev/null +++ b/src/main/java/com/iemr/flw/service/NotificationService.java @@ -0,0 +1,14 @@ +package com.iemr.flw.service; + +import com.iemr.flw.dto.iemr.NotificationListDTO; + +public interface NotificationService { + public String sendNotification(String appType, String topic, String title, String body, String redirect,String notificationType,Integer reciverID); + NotificationListDTO getNotifications(Integer receiverUserId); + + Long getUnreadCount(Integer receiverUserId); + + boolean markAsRead(Long notificationId); + + int markAllAsRead(Integer receiverUserId); +} diff --git a/src/main/java/com/iemr/flw/service/SupervisorDashboardService.java b/src/main/java/com/iemr/flw/service/SupervisorDashboardService.java new file mode 100644 index 00000000..bc580a26 --- /dev/null +++ b/src/main/java/com/iemr/flw/service/SupervisorDashboardService.java @@ -0,0 +1,20 @@ +package com.iemr.flw.service; + +import java.util.List; +import java.util.Map; + +public interface SupervisorDashboardService { + + String getSupervisorDashboard(Integer supervisorUserID, Integer month, Integer year); + + Map getAshasAtFacility(Integer supervisorId, Integer facilityId, Integer month, Integer year, Integer approvalStatusID); + + public int updateApprovalStatus(Integer ashaId, + Integer month, + Integer year, + Integer approvalStatus, + String incentiveIds, + String reason, + String otherReason, + String token); +} diff --git a/src/main/java/com/iemr/flw/service/TBConfirmedCaseService.java b/src/main/java/com/iemr/flw/service/TBConfirmedCaseService.java new file mode 100644 index 00000000..64505b79 --- /dev/null +++ b/src/main/java/com/iemr/flw/service/TBConfirmedCaseService.java @@ -0,0 +1,16 @@ +package com.iemr.flw.service; + +import com.iemr.flw.domain.iemr.TBConfirmedCaseDTO; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public interface TBConfirmedCaseService { + + String save(List tbConfirmedCaseDTO, String token) throws Exception; + + String getByBenId(Long benId, String authorisation) throws Exception; + + String getByUserId(String authorisation) throws Exception; +} diff --git a/src/main/java/com/iemr/flw/service/UwinSessionService.java b/src/main/java/com/iemr/flw/service/UwinSessionService.java index 3f6ddf0a..ce4185c3 100644 --- a/src/main/java/com/iemr/flw/service/UwinSessionService.java +++ b/src/main/java/com/iemr/flw/service/UwinSessionService.java @@ -1,5 +1,6 @@ package com.iemr.flw.service; +import com.iemr.flw.domain.iemr.UwinSession; import com.iemr.flw.dto.iemr.UwinSessionRequestDTO; import com.iemr.flw.dto.iemr.UwinSessionResponseDTO; @@ -7,5 +8,6 @@ public interface UwinSessionService { UwinSessionResponseDTO saveSession(UwinSessionRequestDTO req) throws Exception; + UwinSession updateSession(UwinSessionRequestDTO req, Long recordId,Long activityId) throws Exception; List getSessionsByAsha(Integer ashaId) throws Exception; } diff --git a/src/main/java/com/iemr/flw/service/health/HealthService.java b/src/main/java/com/iemr/flw/service/health/HealthService.java new file mode 100644 index 00000000..084ffe5e --- /dev/null +++ b/src/main/java/com/iemr/flw/service/health/HealthService.java @@ -0,0 +1,517 @@ +/* +* AMRIT – Accessible Medical Records via Integrated Technology +* Integrated EHR (Electronic Health Records) Solution +* +* Copyright (C) "Piramal Swasthya Management and Research Institute" +* +* This file is part of AMRIT. +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see https://www.gnu.org/licenses/. +*/ + +package com.iemr.flw.service.health; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.time.Instant; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import java.util.concurrent.ExecutionException; +import java.util.function.Supplier; +import jakarta.annotation.PreDestroy; +import javax.sql.DataSource; +import com.zaxxer.hikari.HikariDataSource; +import com.zaxxer.hikari.HikariPoolMXBean; +import java.lang.management.ManagementFactory; +import javax.management.MBeanServer; +import javax.management.ObjectName; +import java.util.concurrent.locks.ReentrantReadWriteLock; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Service; + +@Service +public class HealthService { + + private static final Logger logger = LoggerFactory.getLogger(HealthService.class); + + private static final String STATUS_KEY = "status"; + private static final String STATUS_UP = "UP"; + private static final String STATUS_DOWN = "DOWN"; + private static final String STATUS_DEGRADED = "DEGRADED"; + private static final String SEVERITY_KEY = "severity"; + private static final String SEVERITY_OK = "OK"; + private static final String SEVERITY_WARNING = "WARNING"; + private static final String SEVERITY_CRITICAL = "CRITICAL"; + private static final String ERROR_KEY = "error"; + private static final String MESSAGE_KEY = "message"; + private static final String RESPONSE_TIME_KEY = "responseTimeMs"; + private static final long MYSQL_TIMEOUT_SECONDS = 3; + private static final long REDIS_TIMEOUT_SECONDS = 3; + + private static final long ADVANCED_CHECKS_THROTTLE_SECONDS = 30; + private static final long RESPONSE_TIME_THRESHOLD_MS = 2000; + + private static final String DIAGNOSTIC_LOCK_WAIT = "MYSQL_LOCK_WAIT"; + private static final String DIAGNOSTIC_SLOW_QUERIES = "MYSQL_SLOW_QUERIES"; + private static final String DIAGNOSTIC_POOL_EXHAUSTED = "MYSQL_POOL_EXHAUSTED"; + private static final String DIAGNOSTIC_LOG_TEMPLATE = "Diagnostic: {}"; + + private final DataSource dataSource; + private final RedisTemplate redisTemplate; + private final ExecutorService executorService; + + private volatile long lastAdvancedCheckTime = 0; + private volatile AdvancedCheckResult cachedAdvancedCheckResult = null; + private final ReentrantReadWriteLock advancedCheckLock = new ReentrantReadWriteLock(); + private final AtomicBoolean advancedCheckInProgress = new AtomicBoolean(false); + + private static final boolean ADVANCED_HEALTH_CHECKS_ENABLED = true; + + public HealthService(DataSource dataSource, + @Autowired(required = false) RedisTemplate redisTemplate) { + this.dataSource = dataSource; + this.redisTemplate = redisTemplate; + this.executorService = Executors.newFixedThreadPool(6); + } + + @PreDestroy + public void shutdown() { + if (executorService != null && !executorService.isShutdown()) { + try { + executorService.shutdown(); + if (!executorService.awaitTermination(5, TimeUnit.SECONDS)) { + executorService.shutdownNow(); + logger.warn("ExecutorService did not terminate gracefully"); + } + } catch (InterruptedException e) { + executorService.shutdownNow(); + Thread.currentThread().interrupt(); + logger.warn("ExecutorService shutdown interrupted", e); + } + } + } + + public Map checkHealth() { + Map response = new LinkedHashMap<>(); + response.put("timestamp", Instant.now().toString()); + + Map mysqlStatus = new ConcurrentHashMap<>(); + Map redisStatus = new ConcurrentHashMap<>(); + + if (!executorService.isShutdown()) { + performHealthChecks(mysqlStatus, redisStatus); + } + + ensurePopulated(mysqlStatus, "MySQL"); + ensurePopulated(redisStatus, "Redis"); + + Map> components = new LinkedHashMap<>(); + components.put("mysql", mysqlStatus); + components.put("redis", redisStatus); + + response.put("components", components); + response.put(STATUS_KEY, computeOverallStatus(components)); + + return response; + } + + private void performHealthChecks(Map mysqlStatus, Map redisStatus) { + Future mysqlFuture = null; + Future redisFuture = null; + try { + mysqlFuture = executorService.submit( + () -> performHealthCheck("MySQL", mysqlStatus, this::checkMySQLHealthSync)); + redisFuture = executorService.submit( + () -> performHealthCheck("Redis", redisStatus, this::checkRedisHealthSync)); + + awaitHealthChecks(mysqlFuture, redisFuture); + } catch (TimeoutException e) { + logger.warn("Health check aggregate timeout after {} seconds", getMaxTimeout()); + cancelFutures(mysqlFuture, redisFuture); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + logger.warn("Health check was interrupted"); + cancelFutures(mysqlFuture, redisFuture); + } catch (Exception e) { + logger.warn("Health check execution error: {}", e.getMessage()); + cancelFutures(mysqlFuture, redisFuture); + } + } + + private void awaitHealthChecks(Future mysqlFuture, Future redisFuture) throws TimeoutException, InterruptedException, ExecutionException { + long maxTimeout = getMaxTimeout(); + long deadlineNs = System.nanoTime() + TimeUnit.SECONDS.toNanos(maxTimeout); + + mysqlFuture.get(maxTimeout, TimeUnit.SECONDS); + long remainingNs = deadlineNs - System.nanoTime(); + + if (remainingNs > 0) { + redisFuture.get(remainingNs, TimeUnit.NANOSECONDS); + } else { + redisFuture.cancel(true); + } + } + + private long getMaxTimeout() { + return Math.max(MYSQL_TIMEOUT_SECONDS, REDIS_TIMEOUT_SECONDS) + 1; + } + + private void cancelFutures(Future mysqlFuture, Future redisFuture) { + if (mysqlFuture != null) mysqlFuture.cancel(true); + if (redisFuture != null) redisFuture.cancel(true); + } + + private void ensurePopulated(Map status, String componentName) { + if (!status.containsKey(STATUS_KEY)) { + status.put(STATUS_KEY, STATUS_DOWN); + status.put(SEVERITY_KEY, SEVERITY_CRITICAL); + status.put(ERROR_KEY, componentName + " health check did not complete in time"); + } + } + + private HealthCheckResult checkMySQLHealthSync() { + try (Connection connection = dataSource.getConnection(); + PreparedStatement stmt = connection.prepareStatement("SELECT 1 as health_check")) { + + stmt.setQueryTimeout((int) MYSQL_TIMEOUT_SECONDS); + + try (ResultSet rs = stmt.executeQuery()) { + if (!rs.next()) { + return new HealthCheckResult(false, "No result from health check query", false); + } + } + } catch (Exception e) { + logger.warn("MySQL health check failed: {}", e.getMessage(), e); + return new HealthCheckResult(false, "MySQL connection failed", false); + } + boolean isDegraded = performAdvancedMySQLChecksWithThrottle(); + return new HealthCheckResult(true, null, isDegraded); + } + + private HealthCheckResult checkRedisHealthSync() { + if (redisTemplate == null) { + return new HealthCheckResult(true, "Redis not configured — skipped", false); + } + + try { + String pong = redisTemplate.execute((org.springframework.data.redis.core.RedisCallback) (connection) -> connection.ping()); + + if ("PONG".equals(pong)) { + return new HealthCheckResult(true, null, false); + } + + return new HealthCheckResult(false, "Redis PING failed", false); + + } catch (Exception e) { + logger.warn("Redis health check failed: {}", e.getMessage(), e); + return new HealthCheckResult(false, "Redis connection failed", false); + } + } + + private Map performHealthCheck(String componentName, + Map status, + Supplier checker) { + long startTime = System.currentTimeMillis(); + + try { + HealthCheckResult result = checker.get(); + long responseTime = System.currentTimeMillis() - startTime; + + String componentStatus; + if (!result.isHealthy) { + componentStatus = STATUS_DOWN; + } else if (result.isDegraded) { + componentStatus = STATUS_DEGRADED; + } else { + componentStatus = STATUS_UP; + } + status.put(STATUS_KEY, componentStatus); + + status.put(RESPONSE_TIME_KEY, responseTime); + + String severity = determineSeverity(result.isHealthy, responseTime, result.isDegraded); + status.put(SEVERITY_KEY, severity); + + if (result.error != null) { + String fieldKey = result.isHealthy ? MESSAGE_KEY : ERROR_KEY; + status.put(fieldKey, result.error); + } + + return status; + + } catch (Exception e) { + long responseTime = System.currentTimeMillis() - startTime; + logger.error("{} health check failed with exception: {}", componentName, e.getMessage(), e); + + status.put(STATUS_KEY, STATUS_DOWN); + status.put(RESPONSE_TIME_KEY, responseTime); + status.put(SEVERITY_KEY, SEVERITY_CRITICAL); + status.put(ERROR_KEY, "Health check failed with an unexpected error"); + + return status; + } + } + + private String determineSeverity(boolean isHealthy, long responseTimeMs, boolean isDegraded) { + if (!isHealthy) { + return SEVERITY_CRITICAL; + } + + if (isDegraded) { + return SEVERITY_WARNING; + } + + if (responseTimeMs > RESPONSE_TIME_THRESHOLD_MS) { + return SEVERITY_WARNING; + } + + return SEVERITY_OK; + } + + private String computeOverallStatus(Map> components) { + boolean hasCritical = false; + boolean hasDegraded = false; + + for (Map componentStatus : components.values()) { + String status = (String) componentStatus.get(STATUS_KEY); + String severity = (String) componentStatus.get(SEVERITY_KEY); + + if (STATUS_DOWN.equals(status) || SEVERITY_CRITICAL.equals(severity)) { + hasCritical = true; + } + + if (STATUS_DEGRADED.equals(status)) { + hasDegraded = true; + } + + if (SEVERITY_WARNING.equals(severity)) { + hasDegraded = true; + } + } + + if (hasCritical) { + return STATUS_DOWN; + } + + if (hasDegraded) { + return STATUS_DEGRADED; + } + + return STATUS_UP; + } + + private boolean performAdvancedMySQLChecksWithThrottle() { + if (!ADVANCED_HEALTH_CHECKS_ENABLED) { + return false; + } + + long currentTime = System.currentTimeMillis(); + + advancedCheckLock.readLock().lock(); + try { + if (cachedAdvancedCheckResult != null && + (currentTime - lastAdvancedCheckTime) < ADVANCED_CHECKS_THROTTLE_SECONDS * 1000) { + return cachedAdvancedCheckResult.isDegraded; + } + } finally { + advancedCheckLock.readLock().unlock(); + } + + // Only one thread may submit; others fall back to the (stale) cache + if (!advancedCheckInProgress.compareAndSet(false, true)) { + advancedCheckLock.readLock().lock(); + try { + return cachedAdvancedCheckResult != null && cachedAdvancedCheckResult.isDegraded; + } finally { + advancedCheckLock.readLock().unlock(); + } + } + + try { + // Perform DB I/O outside the write lock to avoid lock contention + AdvancedCheckResult result; + try (Connection connection = dataSource.getConnection()) { + result = performAdvancedMySQLChecks(connection); + } catch (Exception e) { + if (e.getCause() instanceof InterruptedException) { + Thread.currentThread().interrupt(); + } + logger.debug("Failed to get connection for advanced checks: {}", e.getMessage()); + result = new AdvancedCheckResult(false); + } + + // Re-acquire write lock only to update the cache atomically + advancedCheckLock.writeLock().lock(); + try { + lastAdvancedCheckTime = currentTime; + cachedAdvancedCheckResult = result; + return result.isDegraded; + } finally { + advancedCheckLock.writeLock().unlock(); + } + } finally { + advancedCheckInProgress.set(false); + } + } + + private AdvancedCheckResult performAdvancedMySQLChecks(Connection connection) { + try { + boolean hasIssues = false; + + if (hasLockWaits(connection)) { + logger.warn(DIAGNOSTIC_LOG_TEMPLATE, DIAGNOSTIC_LOCK_WAIT); + hasIssues = true; + } + + if (hasSlowQueries(connection)) { + logger.warn(DIAGNOSTIC_LOG_TEMPLATE, DIAGNOSTIC_SLOW_QUERIES); + hasIssues = true; + } + + if (hasConnectionPoolExhaustion()) { + logger.warn(DIAGNOSTIC_LOG_TEMPLATE, DIAGNOSTIC_POOL_EXHAUSTED); + hasIssues = true; + } + + return new AdvancedCheckResult(hasIssues); + } catch (Exception e) { + logger.debug("Advanced MySQL checks encountered exception, marking degraded"); + return new AdvancedCheckResult(true); + } + } + + private boolean hasLockWaits(Connection connection) { + try (PreparedStatement stmt = connection.prepareStatement( + "SELECT COUNT(*) FROM INFORMATION_SCHEMA.PROCESSLIST " + + "WHERE (state = 'Waiting for table metadata lock' " + + " OR state = 'Waiting for row lock' " + + " OR state = 'Waiting for lock') " + + "AND user = SUBSTRING_INDEX(USER(), '@', 1)")) { + stmt.setQueryTimeout(2); + try (ResultSet rs = stmt.executeQuery()) { + if (rs.next()) { + int lockCount = rs.getInt(1); + return lockCount > 0; + } + } + } catch (Exception e) { + logger.debug("Could not check for lock waits"); + } + return false; + } + + + private boolean hasSlowQueries(Connection connection) { + try (PreparedStatement stmt = connection.prepareStatement( + "SELECT COUNT(*) FROM INFORMATION_SCHEMA.PROCESSLIST " + + "WHERE command != 'Sleep' AND time > ? AND user = SUBSTRING_INDEX(USER(), '@', 1)")) { + stmt.setQueryTimeout(2); + stmt.setInt(1, 10); + try (ResultSet rs = stmt.executeQuery()) { + if (rs.next()) { + int slowQueryCount = rs.getInt(1); + return slowQueryCount > 3; + } + } + } catch (Exception e) { + logger.debug("Could not check for slow queries"); + } + return false; + } + + private boolean hasConnectionPoolExhaustion() { + if (dataSource instanceof HikariDataSource hikariDataSource) { + try { + HikariPoolMXBean poolMXBean = hikariDataSource.getHikariPoolMXBean(); + + if (poolMXBean != null) { + int activeConnections = poolMXBean.getActiveConnections(); + int maxPoolSize = hikariDataSource.getMaximumPoolSize(); + + int threshold = (int) (maxPoolSize * 0.8); + return activeConnections > threshold; + } + } catch (Exception e) { + logger.debug("Could not retrieve HikariCP pool metrics"); + } + } + + return checkPoolMetricsViaJMX(); + } + + private boolean checkPoolMetricsViaJMX() { + try { + MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); + ObjectName objectName = new ObjectName("com.zaxxer.hikari:type=Pool (*)"); + var mBeans = mBeanServer.queryMBeans(objectName, null); + + for (var mBean : mBeans) { + if (evaluatePoolMetrics(mBeanServer, mBean.getObjectName())) { + return true; + } + } + } catch (Exception e) { + logger.debug("Could not access HikariCP pool metrics via JMX"); + } + + logger.debug("Pool exhaustion check disabled: HikariCP metrics unavailable"); + return false; + } + + private boolean evaluatePoolMetrics(MBeanServer mBeanServer, ObjectName objectName) { + try { + Integer activeConnections = (Integer) mBeanServer.getAttribute(objectName, "ActiveConnections"); + Integer maximumPoolSize = (Integer) mBeanServer.getAttribute(objectName, "MaximumPoolSize"); + + if (activeConnections != null && maximumPoolSize != null) { + int threshold = (int) (maximumPoolSize * 0.8); + return activeConnections > threshold; + } + } catch (Exception e) { + // Continue to next MBean + } + return false; + } + + private static class AdvancedCheckResult { + final boolean isDegraded; + + AdvancedCheckResult(boolean isDegraded) { + this.isDegraded = isDegraded; + } + } + + private static class HealthCheckResult { + final boolean isHealthy; + final String error; + final boolean isDegraded; + + HealthCheckResult(boolean isHealthy, String error, boolean isDegraded) { + this.isHealthy = isHealthy; + this.error = error; + this.isDegraded = isDegraded; + } + } +} diff --git a/src/main/java/com/iemr/flw/service/impl/AbhaBeneficiaryServiceImpl.java b/src/main/java/com/iemr/flw/service/impl/AbhaBeneficiaryServiceImpl.java new file mode 100644 index 00000000..15ca0e7c --- /dev/null +++ b/src/main/java/com/iemr/flw/service/impl/AbhaBeneficiaryServiceImpl.java @@ -0,0 +1,198 @@ +package com.iemr.flw.service.impl; + +import com.google.gson.Gson; +import com.iemr.flw.controller.AbhaBeneficiaryController; +import com.iemr.flw.domain.iemr.AbhaApiResponse; +import com.iemr.flw.dto.abhaBeneficiary.AbhaBeneficiaryDTO; +import com.iemr.flw.dto.iemr.AbhaRequestDTO; +import com.iemr.flw.repo.identity.BeneficiaryRepo; +import com.iemr.flw.repo.identity.HouseHoldRepo; +import com.iemr.flw.service.AbhaBeneficiaryService; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.http.*; +import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; + +import java.math.BigInteger; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@Service +public class AbhaBeneficiaryServiceImpl implements AbhaBeneficiaryService { + + private final org.slf4j.Logger logger = LoggerFactory.getLogger(AbhaBeneficiaryService.class); + + + @Autowired + private BeneficiaryRepo beneficiaryRepo; + + + + @Value("${govthealth.user.details.url}") + private String getUserDetailsUrl; + + @Value("${govthealth.user.id}") + private String govthealthUserId; + + @Value("${govthealth.password}") + private String govthealthPassword; + + @Autowired + private HouseHoldRepo houseHoldRepo; + + @Override + public Object getBeneficiaryByAbha(AbhaRequestDTO request) { + + try { + Long benRedId = null; + BigInteger benDetailsdId = null; + String familyId =null; + if(request.getHouseHoldId()!=null){ + benRedId = beneficiaryRepo.findByHouseoldId(request.getHouseHoldId()).get(0).getBenRegId(); + if(benRedId!=null){ + benDetailsdId = beneficiaryRepo.findByBenRegIdFromMapping(BigInteger.valueOf(benRedId)).getBenDetailsId(); + + } + if(benDetailsdId!=null){ + familyId = beneficiaryRepo.findByBeneficiaryDetailsId(benDetailsdId).getFamilyId(); + + } + } + + Object[] benHealthIdNumber = beneficiaryRepo.getHealthIdNumber(request.getCardNo()); + if (benHealthIdNumber != null && benHealthIdNumber.length > 0) { + Object[] healthData = (Object[]) benHealthIdNumber[0]; + String healthIdNumber = healthData[0] != null ? healthData[0].toString() : null; + String healthId = healthData[1] != null ? healthData[1].toString() : null; + logger.info("healthIdNumber:"+healthIdNumber); + logger.info("healthId:"+healthId); + if (request.getCardNo().equals(healthIdNumber)) { + + Map response = new HashMap<>(); + response.put("statusCode", 5000); + response.put("message", + "This ABHA No already exists"); + + return response; + } + } + + AbhaApiResponse abhaApiResponse = + getAbhaResponse(request.getCardNo()).getBody(); + + if (abhaApiResponse == null || abhaApiResponse.getData() == null) { + + Map response = new HashMap<>(); + response.put("statusCode", 5001); + response.put("message", "No data found"); + + return response; + } + + + for (AbhaBeneficiaryDTO dto : abhaApiResponse.getData()) { + + // Check if ABHA already exists in system + benHealthIdNumber = beneficiaryRepo.getHealthIdNumber(dto.getAbhaId()); + if (benHealthIdNumber != null && benHealthIdNumber.length > 0) { + Object[] healthData = (Object[]) benHealthIdNumber[0]; + String healthIdNumber = healthData[0] != null ? healthData[0].toString() : null; + String healthId = healthData[1] != null ? healthData[1].toString() : null; + logger.info("healthIdNumber:"+healthIdNumber); + logger.info("healthId:"+healthId); + if (dto.getAbhaId().equals(healthIdNumber)) { + + Map response = new HashMap<>(); + response.put("statusCode", 5000); + response.put("message", + "Beneficiary is already exists"); + + return response; + } + } + if(familyId!=null){ + if(!dto.getFamilyid().toString().equals(familyId)){ + Map response = new HashMap<>(); + response.put("statusCode", 5000); + response.put("message", + "Beneficiary is not associated with this family."); + + return response; + } + } + + + + + // Split name into firstName and lastName + String personName = dto.getPersonName(); + + if (personName != null + && !personName.trim().isEmpty()) { + + String[] names = + personName.trim().split("\\s+", 2); + + dto.setFirstName(names[0]); + + if (names.length > 1) { + dto.setLastName(names[1]); + } else { + dto.setLastName(""); + } + } + } + + + logger.info("ABHA Response Status : {}", + abhaApiResponse.getStatusCode()); + + logger.info("ABHA Response : {}", + new Gson().toJson(abhaApiResponse)); + + return abhaApiResponse; + + } catch (Exception e) { + + logger.error("Error while fetching beneficiary by ABHA", e); + + Map response = new HashMap<>(); + response.put("statusCode", 5000); + response.put("message", "Internal Server Error"); + + return response; + } + } + + public ResponseEntity getAbhaResponse(String requestId) { + RestTemplate restTemplate = new RestTemplate(); + + Map body = new HashMap<>(); + body.put("userId",govthealthUserId); + body.put("password", govthealthPassword); + body.put("cardNo", requestId); + + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + + HttpEntity request = new HttpEntity<>(body, headers); + + ResponseEntity response = restTemplate.exchange( + getUserDetailsUrl, + HttpMethod.POST, + request, + AbhaApiResponse.class + ); + + System.out.println("Status = " + response.getStatusCode()); + System.out.println("Body = " + response.getBody()); + + return response; + } + +} diff --git a/src/main/java/com/iemr/flw/service/impl/AbhaTokenServiceImpl.java b/src/main/java/com/iemr/flw/service/impl/AbhaTokenServiceImpl.java new file mode 100644 index 00000000..eef104e8 --- /dev/null +++ b/src/main/java/com/iemr/flw/service/impl/AbhaTokenServiceImpl.java @@ -0,0 +1,103 @@ +package com.iemr.flw.service.impl; + +import com.google.gson.Gson; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import com.iemr.flw.service.AbhaTokenService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpMethod; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Service; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; +import org.springframework.web.client.RestTemplate; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.*; + +@Service +public class AbhaTokenServiceImpl implements AbhaTokenService { + + private final Logger logger = LoggerFactory.getLogger(this.getClass().getName()); + + private static Map ABHA_TOKEN_RESPONSE; + private static Long ABHA_TOKEN_EXP; + + @Value("${abha.client.id}") + private String clientId; + + @Value("${abha.client.secret}") + private String clientSecret; + + @Value("${abha.token.url}") + private String abhaTokenUrl; + + @Value("${abha.xcmid:sbx}") + private String xCmId; + + @Override + public synchronized Map getAbhaToken() throws Exception { + try { + if (ABHA_TOKEN_RESPONSE == null || ABHA_TOKEN_EXP == null + || ABHA_TOKEN_EXP < System.currentTimeMillis()) { + generateAbhaToken(); + logger.info("ABHA token generated successfully"); + } + } catch (Exception e) { + logger.error("Error generating ABHA token: " + e.getMessage()); + throw new Exception("Failed to generate ABHA token: " + e.getMessage()); + } + return ABHA_TOKEN_RESPONSE; + } + + private void generateAbhaToken() throws Exception { + RestTemplate restTemplate = new RestTemplate(); + + Map requestBody = new HashMap<>(); + requestBody.put("clientId", clientId); + requestBody.put("clientSecret", clientSecret); + requestBody.put("grantType", "client_credentials"); + + String requestJson = new Gson().toJson(requestBody); + + MultiValueMap headers = new LinkedMultiValueMap<>(); + headers.add("Content-Type", MediaType.APPLICATION_JSON_VALUE + ";charset=utf-8"); + headers.add("REQUEST-ID", UUID.randomUUID().toString()); + + TimeZone tz = TimeZone.getTimeZone("UTC"); + DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); + df.setTimeZone(tz); + headers.add("TIMESTAMP", df.format(new Date())); + headers.add("X-CM-ID", xCmId); + + HttpEntity httpEntity = new HttpEntity<>(requestJson, headers); + ResponseEntity responseEntity = restTemplate.exchange( + abhaTokenUrl, HttpMethod.POST, httpEntity, String.class); + + String responseBody = responseEntity.getBody(); + if (responseBody != null) { + JsonObject jsonResponse = JsonParser.parseString(responseBody).getAsJsonObject(); + + Map tokenResponse = new HashMap<>(); + tokenResponse.put("accessToken", jsonResponse.get("accessToken").getAsString()); + tokenResponse.put("expiresIn", jsonResponse.get("expiresIn").getAsInt()); + tokenResponse.put("refreshExpiresIn", jsonResponse.get("refreshExpiresIn").getAsInt()); + tokenResponse.put("refreshToken", jsonResponse.get("refreshToken").getAsString()); + tokenResponse.put("tokenType", jsonResponse.get("tokenType").getAsString()); + + Integer expiresIn = jsonResponse.get("expiresIn").getAsInt(); + Calendar calendar = Calendar.getInstance(); + calendar.add(Calendar.SECOND, expiresIn); + ABHA_TOKEN_EXP = calendar.getTimeInMillis(); + + ABHA_TOKEN_RESPONSE = tokenResponse; + } else { + throw new Exception("Empty response from ABDM token API"); + } + } +} diff --git a/src/main/java/com/iemr/flw/service/impl/AdolescentHealthServiceImpl.java b/src/main/java/com/iemr/flw/service/impl/AdolescentHealthServiceImpl.java index 43986a8c..8be992af 100644 --- a/src/main/java/com/iemr/flw/service/impl/AdolescentHealthServiceImpl.java +++ b/src/main/java/com/iemr/flw/service/impl/AdolescentHealthServiceImpl.java @@ -126,7 +126,7 @@ private void checkAndAddIncentives(AdolescentHealth adolescentHealth) { if (sellingSanitaryActivity != null) { IncentiveActivityRecord record = recordRepo - .findRecordByActivityIdCreatedDateBenId(sellingSanitaryActivity.getId(), adolescentHealth.getCreatedDate(), adolescentHealth.getBenId().longValue()); + .findRecordByActivityIdCreatedDateBenId(sellingSanitaryActivity.getId(), adolescentHealth.getCreatedDate(), adolescentHealth.getBenId().longValue(),adolescentHealth.getUserId()); if (record == null) { record = new IncentiveActivityRecord(); record.setActivityId(sellingSanitaryActivity.getId()); @@ -151,7 +151,7 @@ record = new IncentiveActivityRecord(); if (mobilizingADHActivity != null) { IncentiveActivityRecord record = recordRepo - .findRecordByActivityIdCreatedDateBenId(mobilizingADHActivity.getId(), adolescentHealth.getCreatedDate(), adolescentHealth.getBenId().longValue()); + .findRecordByActivityIdCreatedDateBenId(mobilizingADHActivity.getId(), adolescentHealth.getCreatedDate(), adolescentHealth.getBenId().longValue(),adolescentHealth.getUserId()); if (record == null) { record = new IncentiveActivityRecord(); record.setActivityId(mobilizingADHActivity.getId()); diff --git a/src/main/java/com/iemr/flw/service/impl/AshaProfileImpl.java b/src/main/java/com/iemr/flw/service/impl/AshaProfileImpl.java index 29ca30da..fe7b04db 100644 --- a/src/main/java/com/iemr/flw/service/impl/AshaProfileImpl.java +++ b/src/main/java/com/iemr/flw/service/impl/AshaProfileImpl.java @@ -1,19 +1,17 @@ package com.iemr.flw.service.impl; import com.iemr.flw.domain.iemr.AshaWorker; -import com.iemr.flw.domain.iemr.M_User; +import com.iemr.flw.domain.iemr.User; +import com.iemr.flw.dto.iemr.UserServiceRoleDTO; import com.iemr.flw.repo.iemr.AshaProfileRepo; import com.iemr.flw.repo.iemr.UserServiceRoleRepo; import com.iemr.flw.service.AshaProfileService; import com.iemr.flw.service.EmployeeMasterInter; import com.iemr.flw.repo.iemr.EmployeeMasterRepo; -import com.iemr.flw.service.AshaProfileService; -import com.iemr.flw.service.EmployeeMasterInter; +import com.iemr.flw.service.UserService; import com.iemr.flw.utils.JwtAuthenticationUtil; import com.iemr.flw.utils.JwtUtil; -import com.iemr.flw.utils.exception.IEMRException; -import io.jsonwebtoken.Claims; import jakarta.transaction.Transactional; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -22,12 +20,9 @@ import java.time.LocalDate; import java.util.Objects; +import java.util.Optional; import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.stereotype.Service; - -import java.util.Objects; -import java.util.concurrent.TimeUnit; @Service public class AshaProfileImpl implements AshaProfileService { @@ -45,6 +40,9 @@ public class AshaProfileImpl implements AshaProfileService { JwtAuthenticationUtil jwtAuthenticationUtil; @Autowired private UserServiceRoleRepo userServiceRoleRepo; + + @Autowired + private UserService userService; private final Logger logger = LoggerFactory.getLogger(AshaProfileImpl.class); @Transactional @@ -53,16 +51,17 @@ public AshaWorker saveEditData(AshaWorker request) { try { Objects.requireNonNull(request, "ASHA worker request must not be null"); - Long id = request.getId(); + Optional ashaWorker = ashaProfileRepo.findByEmployeeId(request.getEmployeeId()); // ---------- CREATE Case ---------- - if (id == null || id == 0) { - // treat id=0 as null (frontend mistake) - if (id != null && id == 0) { - request.setId(null); - } + if (!ashaWorker.isPresent()) { + request.setId(null); - AshaWorker saved = ashaProfileRepo.save(request); + UserServiceRoleDTO userServiceRoleDTO= userService.getUserDetail(request.getEmployeeId()); + if(userServiceRoleDTO!=null){ + request.setName(userServiceRoleDTO.getName()); + } + AshaWorker saved = saveProfile(request); logger.info("Created ASHA Worker: {}", saved.getId()); return saved; } @@ -96,22 +95,15 @@ public AshaWorker getProfileData(Integer userId) { private AshaWorker getDetails(Integer userID) { try { - M_User m_user = Objects.requireNonNull(employeeMasterInter.getUserDetails(userID), "User details not found for ID: " + userID); + User m_user = Objects.requireNonNull(employeeMasterInter.getUserDetails(userID), "User details not found for ID: " + userID); AshaWorker ashaWorker = new AshaWorker(); ashaWorker.setEmployeeId(m_user.getUserID()); - // Convert DOB (Timestamp) to LocalDate - java.sql.Timestamp dobTimestamp = m_user.getDOB(); - LocalDate dob = dobTimestamp != null ? dobTimestamp.toLocalDateTime().toLocalDate() : null; - ashaWorker.setDob(dob); - // Convert DOJ (Timestamp) to LocalDate - java.sql.Timestamp dojTimestamp = m_user.getDOJ(); - LocalDate doj = dojTimestamp != null ? dojTimestamp.toLocalDateTime().toLocalDate() : LocalDate.now(); - ashaWorker.setDateOfJoining(doj); + ashaWorker.setName(String.format("%s %s", Objects.toString(m_user.getFirstName(), ""), Objects.toString(m_user.getLastName(), "")).trim()); ashaWorker.setMobileNumber(m_user.getContactNo()); ashaWorker.setAlternateMobileNumber(m_user.getEmergencyContactNo()); ashaWorker.setProviderServiceMapID(m_user.getServiceProviderID()); - ashaWorker.setProfileImage(""); + ashaWorker.setProfileImage(null); ashaWorker.setSupervisorName(""); ashaWorker.setAwwName(""); ashaWorker.setVillage(""); @@ -140,8 +132,104 @@ private AshaWorker getDetails(Integer userID) { throw new RuntimeException("Failed to create ASHA worker profile from user details", e); } } + @Transactional + public AshaWorker saveProfile(AshaWorker request) { + logger.info("Saving ASHA Profile for EmployeeId: {}", request.getEmployeeId()); + + logger.info("Received DOB: {}", request.getDob()); + logger.info("Received Date Of Joining: {}", request.getDateOfJoining()); + + AshaWorker existing = ashaProfileRepo + .findByEmployeeId(request.getEmployeeId()) + .orElse(new AshaWorker()); + + if (isValid(request.getName())) + existing.setName(request.getName()); + + if (isValid(request.getVillage())) + existing.setVillage(request.getVillage()); + + if (request.getEmployeeId() != null) + existing.setEmployeeId(request.getEmployeeId()); + + // Save even if null + existing.setDob(request.getDob()); + + if (isValid(request.getMobileNumber())) + existing.setMobileNumber(request.getMobileNumber()); + + if (isValid(request.getAlternateMobileNumber())) + existing.setAlternateMobileNumber(request.getAlternateMobileNumber()); + + if (isValid(request.getFatherOrSpouseName())) + existing.setFatherOrSpouseName(request.getFatherOrSpouseName()); + + // Save even if null + existing.setDateOfJoining(request.getDateOfJoining()); + + if (isValid(request.getBankAccount())) + existing.setBankAccount(request.getBankAccount()); + + if (isValid(request.getIfsc())) + existing.setIfsc(request.getIfsc()); + + if (request.getPopulationCovered() != null) + existing.setPopulationCovered(request.getPopulationCovered()); + + if (isValid(request.getChoName())) + existing.setChoName(request.getChoName()); + + if (isValid(request.getChoMobile())) + existing.setChoMobile(request.getChoMobile()); + + if (isValid(request.getAwwName())) + existing.setAwwName(request.getAwwName()); + + if (isValid(request.getAwwMobile())) + existing.setAwwMobile(request.getAwwMobile()); + + if (isValid(request.getAnm1Name())) + existing.setAnm1Name(request.getAnm1Name()); + + if (isValid(request.getAnm1Mobile())) + existing.setAnm1Mobile(request.getAnm1Mobile()); + + if (isValid(request.getAnm2Name())) + existing.setAnm2Name(request.getAnm2Name()); + + if (isValid(request.getAnm2Mobile())) + existing.setAnm2Mobile(request.getAnm2Mobile()); + + if (isValid(request.getAbhaNumber())) + existing.setAbhaNumber(request.getAbhaNumber()); + + if (isValid(request.getAshaHouseholdRegistration())) + existing.setAshaHouseholdRegistration(request.getAshaHouseholdRegistration()); + + if (isValid(request.getAshaFamilyMember())) + existing.setAshaFamilyMember(request.getAshaFamilyMember()); + + if (request.getProviderServiceMapID() != null) + existing.setProviderServiceMapID(request.getProviderServiceMapID()); + + if (request.getProfileImage() != null && request.getProfileImage().length > 0) + existing.setProfileImage(request.getProfileImage()); + + if (request.getIsFatherOrSpouse() != null) + existing.setIsFatherOrSpouse(request.getIsFatherOrSpouse()); + + if (isValid(request.getSupervisorName())) + existing.setSupervisorName(request.getSupervisorName()); + + if (isValid(request.getSupervisorMobile())) + existing.setSupervisorMobile(request.getSupervisorMobile()); + + return ashaProfileRepo.save(existing); + } + - public AshaWorker updateProfile(AshaWorker request) { + + public AshaWorker updateProfile(AshaWorker request) { AshaWorker existing = ashaProfileRepo.findByEmployeeId(request.getEmployeeId()).orElseThrow(() -> new RuntimeException("ASHA worker not found")); if (isValid(request.getAbhaNumber())) existing.setAbhaNumber(request.getAbhaNumber()); if (request.getEmployeeId() != null) existing.setEmployeeId(request.getEmployeeId()); @@ -169,16 +257,20 @@ public AshaWorker updateProfile(AshaWorker request) { if (isValid(request.getAwwMobile())) existing.setAwwMobile(request.getAwwMobile()); if (request.getProviderServiceMapID() != null) existing.setProviderServiceMapID(request.getProviderServiceMapID()); - if (isValid(request.getProfileImage())) existing.setProfileImage(request.getProfileImage()); + if (isValidImage(request.getProfileImage())) existing.setProfileImage(request.getProfileImage()); if (request.getIsFatherOrSpouse() != null) existing.setIsFatherOrSpouse(request.getIsFatherOrSpouse()); if (isValid(request.getSupervisorName())) existing.setSupervisorName(request.getSupervisorName()); if (isValid(request.getSupervisorMobile())) existing.setSupervisorMobile(request.getSupervisorMobile()); - return existing; + return ashaProfileRepo.save(existing); } private boolean isValid(String value) { return value != null && !value.trim().isEmpty() && !"null".equalsIgnoreCase(value.trim()); } + private boolean isValidImage(byte[] value) { + return value != null && value.length > 0; + } + } diff --git a/src/main/java/com/iemr/flw/service/impl/BeneficiaryServiceImpl.java b/src/main/java/com/iemr/flw/service/impl/BeneficiaryServiceImpl.java index 510d4b53..23c4bd60 100644 --- a/src/main/java/com/iemr/flw/service/impl/BeneficiaryServiceImpl.java +++ b/src/main/java/com/iemr/flw/service/impl/BeneficiaryServiceImpl.java @@ -2,16 +2,19 @@ import java.math.BigInteger; import java.sql.Date; +import java.sql.Timestamp; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.Period; import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; import java.util.ArrayList; import java.util.Calendar; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; +import java.util.stream.Stream; import com.iemr.flw.domain.iemr.EyeCheckupVisit; import com.iemr.flw.domain.iemr.IncentiveActivity; @@ -19,6 +22,8 @@ import com.iemr.flw.dto.iemr.EyeCheckupRequestDTO; import com.iemr.flw.masterEnum.GroupName; import com.iemr.flw.repo.iemr.*; +import com.iemr.flw.service.IncentiveLogicService; +import com.iemr.flw.service.UserService; import com.iemr.flw.utils.JwtUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -54,6 +59,7 @@ import com.iemr.flw.service.BeneficiaryService; import com.iemr.flw.utils.config.ConfigProperties; import com.iemr.flw.utils.http.HttpUtils; +import org.springframework.transaction.annotation.Transactional; @Service @Qualifier("rmnchServiceImpl") @@ -90,56 +96,69 @@ public class BeneficiaryServiceImpl implements BeneficiaryService { private JwtUtil jwtUtil; @Autowired - private UserServiceRoleRepo userRepo; + private UserService userService; - private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("dd-MM-yyyy"); + @Autowired + private IncentiveLogicService incentiveLogicService; + private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("dd-MM-yyyy"); @Override public String getBenData(GetBenRequestHandler request, String authorisation) throws Exception { - String outputResponse = null; - int totalPage = 0; - try { - if (request != null && request.getAshaId() != null) { - List resultSet; - Integer pageSize = Integer.valueOf(door_to_door_page_size); - if (request.getPageNo() != null) { - String userName = beneficiaryRepo.getUserName(request.getAshaId()); - if (userName == null || userName.isEmpty()) - throw new Exception("Asha details not found, please contact administrator"); - - request.setUserName(userName); - - PageRequest pr = PageRequest.of(request.getPageNo(), pageSize); - if (request.getFromDate() != null && request.getToDate() != null) { - Page p = beneficiaryRepo.getBenDataWithinDates( - request.getUserName(), request.getFromDate(), request.getToDate(), pr); - resultSet = p.getContent(); - totalPage = p.getTotalPages(); - } else { - Page p = beneficiaryRepo.getBenDataByUser(request.getUserName(), - pr); - resultSet = p.getContent(); - totalPage = p.getTotalPages(); - } - if (resultSet != null && resultSet.size() > 0) { - outputResponse = getMappingsForAddressIDs(resultSet, totalPage, authorisation); - } - } else { - // page no not invalid - throw new Exception("Invalid page no"); - } - } else - throw new Exception("Invalid/missing village details"); - } catch (Exception e) { - throw new Exception(e.getMessage()); + if (request == null || request.getAshaId() == null) { + throw new Exception("Invalid/missing asha details"); + } + + if (request.getPageNo() == null || request.getPageNo() < 0) { + throw new Exception("Invalid page number"); + } + + String userName = beneficiaryRepo.getUserName(request.getAshaId()); + + if (userName == null || userName.isEmpty()) { + throw new Exception("Asha details not found, please contact administrator"); } - return outputResponse; + request.setUserName(userName); + + int pageSize = Integer.parseInt(door_to_door_page_size); + PageRequest pageRequest = PageRequest.of(request.getPageNo(), pageSize); + + Page pageResult; + + // ✅ Date Filter Handling + if (request.getFromDate() != null && request.getToDate() != null) { + + if (request.getFromDate().after(request.getToDate())) { + throw new Exception("Invalid date range"); + } + + pageResult = beneficiaryRepo.getBenDataWithinDates( + userName, + request.getFromDate(), + request.getToDate(), + pageRequest + ); + + } else { + + pageResult = beneficiaryRepo.getBenDataByUser(userName, pageRequest); + } + + if (!pageResult.hasContent()) { + return null; + } + + return getMappingsForAddressIDs( + pageResult.getContent(), + pageResult.getTotalPages(), + authorisation + ); } + private String getMappingsForAddressIDs(List addressList, int totalPage, String authorisation) { RMNCHHouseHoldDetails benHouseHoldRMNCH_ROBJ; @@ -158,276 +177,291 @@ private String getMappingsForAddressIDs(List addressLi for (RMNCHMBeneficiaryaddress a : addressList) { // exception by-passing try { - RMNCHMBeneficiarymapping m = beneficiaryRepo.getByAddressID(a.getId()); - if (m != null) { - benHouseHoldRMNCH_ROBJ = new RMNCHHouseHoldDetails(); - benDetailsRMNCH_OBJ = new RMNCHBeneficiaryDetailsRmnch(); - benBotnBirthRMNCH_ROBJ = new RMNCHBornBirthDetails(); - - benDetailsOBJ = new RMNCHMBeneficiarydetail(); - benAccountOBJ = new RMNCHMBeneficiaryAccount(); - benImageOBJ = new RMNCHMBeneficiaryImage(); - benAddressOBJ = new RMNCHMBeneficiaryaddress(); - benContactOBJ = new RMNCHMBeneficiarycontact(); - Map healthDetails = getBenHealthDetails(m.getBenRegId()); - if (m.getBenDetailsId() != null) { - benDetailsOBJ = beneficiaryRepo.getDetailsById(m.getBenDetailsId()); - } - if (m.getBenAccountID() != null) { - benAccountOBJ = beneficiaryRepo.getAccountById(m.getBenAccountID()); - } - if (m.getBenImageId() != null) { - benImageOBJ = beneficiaryRepo.getImageById(m.getBenImageId().longValue()); - } - if (m.getBenAddressId() != null) { - benAddressOBJ = beneficiaryRepo.getAddressById(m.getBenAddressId()); - } - if (m.getBenContactsId() != null) { - benContactOBJ = beneficiaryRepo.getContactById(m.getBenContactsId()); - } + if(!beneficiaryRepo.getByAddressID(a.getId()).isEmpty()){ + RMNCHMBeneficiarymapping m = beneficiaryRepo.getByAddressID(a.getId()).get(0); + if (m != null) { + benHouseHoldRMNCH_ROBJ = new RMNCHHouseHoldDetails(); + benDetailsRMNCH_OBJ = new RMNCHBeneficiaryDetailsRmnch(); + benBotnBirthRMNCH_ROBJ = new RMNCHBornBirthDetails(); + + benDetailsOBJ = new RMNCHMBeneficiarydetail(); + benAccountOBJ = new RMNCHMBeneficiaryAccount(); + benImageOBJ = new RMNCHMBeneficiaryImage(); + benAddressOBJ = new RMNCHMBeneficiaryaddress(); + benContactOBJ = new RMNCHMBeneficiarycontact(); + Map healthDetails = getBenHealthDetails(m.getBenRegId()); + if (m.getBenDetailsId() != null) { + benDetailsOBJ = beneficiaryRepo.getDetailsById(m.getBenDetailsId()); + } + if (m.getBenAccountID() != null) { + benAccountOBJ = beneficiaryRepo.getAccountById(m.getBenAccountID()); + } + if (m.getBenImageId() != null) { + benImageOBJ = beneficiaryRepo.getImageById(m.getBenImageId().longValue()); + } + if (m.getBenAddressId() != null) { + benAddressOBJ = beneficiaryRepo.getAddressById(m.getBenAddressId()); + } + if (m.getBenContactsId() != null) { + benContactOBJ = beneficiaryRepo.getContactById(m.getBenContactsId()); + } - BigInteger benID = null; - if (m.getBenRegId() != null) - benID = beneficiaryRepo.getBenIdFromRegID(m.getBenRegId().longValue()); + BigInteger benID = null; + if (m.getBenRegId() != null) + benID = beneficiaryRepo.getBenIdFromRegID(m.getBenRegId().longValue()); - if (m.getBenRegId() != null) { - benDetailsRMNCH_OBJ = beneficiaryRepo - .getDetailsByRegID((m.getBenRegId()).longValue()); - benBotnBirthRMNCH_ROBJ = beneficiaryRepo.getBornBirthByRegID((m.getBenRegId()).longValue()); + if (m.getBenRegId() != null) { + if(!beneficiaryRepo + .getDetailsByRegID((m.getBenRegId()).longValue()).isEmpty()){ + benDetailsRMNCH_OBJ = beneficiaryRepo + .getDetailsByRegID((m.getBenRegId()).longValue()).get(0); - if (benDetailsRMNCH_OBJ != null && benDetailsRMNCH_OBJ.getHouseoldId() != null) - benHouseHoldRMNCH_ROBJ = houseHoldRepo - .getByHouseHoldID(benDetailsRMNCH_OBJ.getHouseoldId()); + } + benBotnBirthRMNCH_ROBJ = beneficiaryRepo.getBornBirthByRegID((m.getBenRegId()).longValue()); - } - if (benDetailsRMNCH_OBJ == null) - benDetailsRMNCH_OBJ = new RMNCHBeneficiaryDetailsRmnch(); - // new mapping 30-06-2021 - if (benDetailsOBJ.getMotherName() != null) - benDetailsRMNCH_OBJ.setMotherName(benDetailsOBJ.getMotherName()); - if (benDetailsOBJ.getLiteracyStatus() != null) - benDetailsRMNCH_OBJ.setLiteracyStatus(benDetailsOBJ.getLiteracyStatus()); - - // bank - if (benAccountOBJ.getNameOfBank() != null) - benDetailsRMNCH_OBJ.setNameOfBank(benAccountOBJ.getNameOfBank()); - if (benAccountOBJ.getBranchName() != null) - benDetailsRMNCH_OBJ.setBranchName(benAccountOBJ.getBranchName()); - if (benAccountOBJ.getIfscCode() != null) - benDetailsRMNCH_OBJ.setIfscCode(benAccountOBJ.getIfscCode()); - if (benAccountOBJ.getBankAccount() != null) - benDetailsRMNCH_OBJ.setBankAccount(benAccountOBJ.getBankAccount()); - - // location - if (benAddressOBJ.getCountyid() != null) - benDetailsRMNCH_OBJ.setCountryId(benAddressOBJ.getCountyid()); - if (benAddressOBJ.getPermCountry() != null) - benDetailsRMNCH_OBJ.setCountryName(benAddressOBJ.getPermCountry()); - - if (benAddressOBJ.getStatePerm() != null) - benDetailsRMNCH_OBJ.setStateId(benAddressOBJ.getStatePerm()); - if (benAddressOBJ.getPermState() != null) - benDetailsRMNCH_OBJ.setStateName(benAddressOBJ.getPermState()); - - if (benAddressOBJ.getDistrictidPerm() != null) { - benDetailsRMNCH_OBJ.setDistrictid(benAddressOBJ.getDistrictidPerm()); + if (benDetailsRMNCH_OBJ != null && benDetailsRMNCH_OBJ.getHouseoldId() != null) + if(!houseHoldRepo + .getByHouseHoldID(benDetailsRMNCH_OBJ.getHouseoldId()).isEmpty()){ + benHouseHoldRMNCH_ROBJ = houseHoldRepo + .getByHouseHoldID(benDetailsRMNCH_OBJ.getHouseoldId()).get(0); + } - } - if (benAddressOBJ.getDistrictnamePerm() != null) { - benDetailsRMNCH_OBJ.setDistrictname(benAddressOBJ.getDistrictnamePerm()); - } + } + if (benDetailsRMNCH_OBJ == null) + benDetailsRMNCH_OBJ = new RMNCHBeneficiaryDetailsRmnch(); + + // new mapping 30-06-2021 + if (benDetailsOBJ.getMotherName() != null) + benDetailsRMNCH_OBJ.setMotherName(benDetailsOBJ.getMotherName()); + if (benDetailsOBJ.getLiteracyStatus() != null) + benDetailsRMNCH_OBJ.setLiteracyStatus(benDetailsOBJ.getLiteracyStatus()); + + // bank + if (benAccountOBJ.getNameOfBank() != null) + benDetailsRMNCH_OBJ.setNameOfBank(benAccountOBJ.getNameOfBank()); + if (benAccountOBJ.getBranchName() != null) + benDetailsRMNCH_OBJ.setBranchName(benAccountOBJ.getBranchName()); + if (benAccountOBJ.getIfscCode() != null) + benDetailsRMNCH_OBJ.setIfscCode(benAccountOBJ.getIfscCode()); + if (benAccountOBJ.getBankAccount() != null) + benDetailsRMNCH_OBJ.setBankAccount(benAccountOBJ.getBankAccount()); + + // location + if (benAddressOBJ.getCountyid() != null) + benDetailsRMNCH_OBJ.setCountryId(benAddressOBJ.getCountyid()); + if (benAddressOBJ.getPermCountry() != null) + benDetailsRMNCH_OBJ.setCountryName(benAddressOBJ.getPermCountry()); + + if (benAddressOBJ.getStatePerm() != null) + benDetailsRMNCH_OBJ.setStateId(benAddressOBJ.getStatePerm()); + if (benAddressOBJ.getPermState() != null) + benDetailsRMNCH_OBJ.setStateName(benAddressOBJ.getPermState()); + + if (benAddressOBJ.getDistrictidPerm() != null) { + benDetailsRMNCH_OBJ.setDistrictid(benAddressOBJ.getDistrictidPerm()); - if (benAddressOBJ.getPermSubDistrictId() != null) - benDetailsRMNCH_OBJ.setBlockId(benAddressOBJ.getPermSubDistrictId()); - if (benAddressOBJ.getPermSubDistrict() != null) - benDetailsRMNCH_OBJ.setBlockName(benAddressOBJ.getPermSubDistrict()); - - if (benAddressOBJ.getVillageidPerm() != null) - benDetailsRMNCH_OBJ.setVillageId(benAddressOBJ.getVillageidPerm()); - if (benAddressOBJ.getVillagenamePerm() != null) - benDetailsRMNCH_OBJ.setVillageName(benAddressOBJ.getVillagenamePerm()); - - if (benAddressOBJ.getPermServicePointId() != null) - benDetailsRMNCH_OBJ.setServicePointID(benAddressOBJ.getPermServicePointId()); - if (benAddressOBJ.getPermServicePoint() != null) - benDetailsRMNCH_OBJ.setServicePointName(benAddressOBJ.getPermServicePoint()); - - if (benAddressOBJ.getPermZoneID() != null) - benDetailsRMNCH_OBJ.setZoneID(benAddressOBJ.getPermZoneID()); - if (benAddressOBJ.getPermZone() != null) - benDetailsRMNCH_OBJ.setZoneName(benAddressOBJ.getPermZone()); - - if (benAddressOBJ.getPermAddrLine1() != null) - benDetailsRMNCH_OBJ.setAddressLine1(benAddressOBJ.getPermAddrLine1()); - if (benAddressOBJ.getPermAddrLine2() != null) - benDetailsRMNCH_OBJ.setAddressLine2(benAddressOBJ.getPermAddrLine2()); - if (benAddressOBJ.getPermAddrLine3() != null) - benDetailsRMNCH_OBJ.setAddressLine3(benAddressOBJ.getPermAddrLine3()); - - // ----------------------------------------------------------------------------- - - // related benids - if (benDetailsRMNCH_OBJ.getRelatedBeneficiaryIdsDB() != null) { - - String[] relatedBenIDsString = benDetailsRMNCH_OBJ.getRelatedBeneficiaryIdsDB().split(","); - Long[] relatedBenIDs = new Long[relatedBenIDsString.length]; - int pointer = 0; - for (String s : relatedBenIDsString) { - relatedBenIDs[pointer] = Long.valueOf(s); - pointer++; } + if (benAddressOBJ.getDistrictnamePerm() != null) { + benDetailsRMNCH_OBJ.setDistrictname(benAddressOBJ.getDistrictnamePerm()); - benDetailsRMNCH_OBJ.setRelatedBeneficiaryIds(relatedBenIDs); - } - // ------------------------------------------------------------------------------ - - if (benDetailsOBJ.getCommunity() != null) - benDetailsRMNCH_OBJ.setCommunity(benDetailsOBJ.getCommunity()); - if (benDetailsOBJ.getCommunityId() != null) - benDetailsRMNCH_OBJ.setCommunityId(benDetailsOBJ.getCommunityId()); - if (benContactOBJ.getPreferredPhoneNum() != null) - benDetailsRMNCH_OBJ.setContact_number(benContactOBJ.getPreferredPhoneNum()); - - if (benDetailsOBJ.getDob() != null) - benDetailsRMNCH_OBJ.setDob(benDetailsOBJ.getDob()); - if (benDetailsOBJ.getFatherName() != null) - benDetailsRMNCH_OBJ.setFatherName(benDetailsOBJ.getFatherName()); - if (benDetailsOBJ.getFirstName() != null) - benDetailsRMNCH_OBJ.setFirstName(benDetailsOBJ.getFirstName()); - if (benDetailsOBJ.getGender() != null) - benDetailsRMNCH_OBJ.setGender(benDetailsOBJ.getGender()); - if (benDetailsOBJ.getGenderId() != null) - benDetailsRMNCH_OBJ.setGenderId(benDetailsOBJ.getGenderId()); - - if (benDetailsOBJ.getMaritalstatus() != null) - benDetailsRMNCH_OBJ.setMaritalstatus(benDetailsOBJ.getMaritalstatus()); - if (benDetailsOBJ.getMaritalstatusId() != null) - benDetailsRMNCH_OBJ.setMaritalstatusId(benDetailsOBJ.getMaritalstatusId()); - if (benDetailsOBJ.getMarriageDate() != null) - benDetailsRMNCH_OBJ.setMarriageDate(benDetailsOBJ.getMarriageDate()); - - if (benDetailsOBJ.getReligion() != null) - benDetailsRMNCH_OBJ.setReligion(benDetailsOBJ.getReligion()); - if (benDetailsOBJ.getReligionID() != null) - benDetailsRMNCH_OBJ.setReligionID(benDetailsOBJ.getReligionID()); - if (benDetailsOBJ.getSpousename() != null) - benDetailsRMNCH_OBJ.setSpousename(benDetailsOBJ.getSpousename()); - - if (benImageOBJ != null && benImageOBJ.getUser_image() != null) - benDetailsRMNCH_OBJ.setUser_image(benImageOBJ.getUser_image()); - - // new fields + } + + if (benAddressOBJ.getPermSubDistrictId() != null) + benDetailsRMNCH_OBJ.setBlockId(benAddressOBJ.getPermSubDistrictId()); + if (benAddressOBJ.getPermSubDistrict() != null) + benDetailsRMNCH_OBJ.setBlockName(benAddressOBJ.getPermSubDistrict()); + + if (benAddressOBJ.getVillageidPerm() != null) + benDetailsRMNCH_OBJ.setVillageId(benAddressOBJ.getVillageidPerm()); + if (benAddressOBJ.getVillagenamePerm() != null) + benDetailsRMNCH_OBJ.setVillageName(benAddressOBJ.getVillagenamePerm()); + + if (benAddressOBJ.getPermServicePointId() != null) + benDetailsRMNCH_OBJ.setServicePointID(benAddressOBJ.getPermServicePointId()); + if (benAddressOBJ.getPermServicePoint() != null) + benDetailsRMNCH_OBJ.setServicePointName(benAddressOBJ.getPermServicePoint()); + + if (benAddressOBJ.getPermZoneID() != null) + benDetailsRMNCH_OBJ.setZoneID(benAddressOBJ.getPermZoneID()); + if (benAddressOBJ.getPermZone() != null) + benDetailsRMNCH_OBJ.setZoneName(benAddressOBJ.getPermZone()); + + if (benAddressOBJ.getPermAddrLine1() != null) + benDetailsRMNCH_OBJ.setAddressLine1(benAddressOBJ.getPermAddrLine1()); + if (benAddressOBJ.getPermAddrLine2() != null) + benDetailsRMNCH_OBJ.setAddressLine2(benAddressOBJ.getPermAddrLine2()); + if (benAddressOBJ.getPermAddrLine3() != null) + benDetailsRMNCH_OBJ.setAddressLine3(benAddressOBJ.getPermAddrLine3()); + + // ----------------------------------------------------------------------------- + + // related benids + if (benDetailsRMNCH_OBJ.getRelatedBeneficiaryIdsDB() != null) { + + String[] relatedBenIDsString = benDetailsRMNCH_OBJ.getRelatedBeneficiaryIdsDB().split(","); + Long[] relatedBenIDs = new Long[relatedBenIDsString.length]; + int pointer = 0; + for (String s : relatedBenIDsString) { + relatedBenIDs[pointer] = Long.valueOf(s); + pointer++; + } + + benDetailsRMNCH_OBJ.setRelatedBeneficiaryIds(relatedBenIDs); + } + // ------------------------------------------------------------------------------ + + if (benDetailsOBJ.getCommunity() != null) + benDetailsRMNCH_OBJ.setCommunity(benDetailsOBJ.getCommunity()); + if (benDetailsOBJ.getCommunityId() != null) + benDetailsRMNCH_OBJ.setCommunityId(benDetailsOBJ.getCommunityId()); + if (benContactOBJ.getPreferredPhoneNum() != null) + benDetailsRMNCH_OBJ.setContact_number(benContactOBJ.getPreferredPhoneNum()); + + if (benDetailsOBJ.getDob() != null) + benDetailsRMNCH_OBJ.setDob(benDetailsOBJ.getDob()); + if (benDetailsOBJ.getFatherName() != null) + benDetailsRMNCH_OBJ.setFatherName(benDetailsOBJ.getFatherName()); + if (benDetailsOBJ.getFirstName() != null) + benDetailsRMNCH_OBJ.setFirstName(benDetailsOBJ.getFirstName()); + if (benDetailsOBJ.getGender() != null) + benDetailsRMNCH_OBJ.setGender(benDetailsOBJ.getGender()); + if (benDetailsOBJ.getGenderId() != null) + benDetailsRMNCH_OBJ.setGenderId(benDetailsOBJ.getGenderId()); + + if (benDetailsOBJ.getMaritalstatus() != null) + benDetailsRMNCH_OBJ.setMaritalstatus(benDetailsOBJ.getMaritalstatus()); + if (benDetailsOBJ.getMaritalstatusId() != null) + benDetailsRMNCH_OBJ.setMaritalstatusId(benDetailsOBJ.getMaritalstatusId()); + if (benDetailsOBJ.getMarriageDate() != null) + benDetailsRMNCH_OBJ.setMarriageDate(benDetailsOBJ.getMarriageDate()); + + if (benDetailsOBJ.getReligion() != null) + benDetailsRMNCH_OBJ.setReligion(benDetailsOBJ.getReligion()); + if (benDetailsOBJ.getReligionID() != null) + benDetailsRMNCH_OBJ.setReligionID(benDetailsOBJ.getReligionID()); + if (benDetailsOBJ.getSpousename() != null) + benDetailsRMNCH_OBJ.setSpousename(benDetailsOBJ.getSpousename()); + + if (benImageOBJ != null && benImageOBJ.getUser_image() != null) + benDetailsRMNCH_OBJ.setUser_image(benImageOBJ.getUser_image()); + + // new fields // benDetailsRMNCH_OBJ.setRegistrationDate(benDetailsOBJ.getCreatedDate()); - if (benID != null) - benDetailsRMNCH_OBJ.setBenficieryid(benID.longValue()); - - if (benDetailsOBJ.getLastName() != null) - benDetailsRMNCH_OBJ.setLastName(benDetailsOBJ.getLastName()); - - if (benDetailsRMNCH_OBJ.getCreatedBy() == null) - if (benDetailsOBJ.getCreatedBy() != null) - benDetailsRMNCH_OBJ.setCreatedBy(benDetailsOBJ.getCreatedBy()); - - // age calculation - String ageDetails = ""; - int age_val = 0; - String ageUnit = null; - if (benDetailsOBJ.getDob() != null) { - - Date date = new Date(benDetailsOBJ.getDob().getTime()); - Calendar cal = Calendar.getInstance(); - - cal.setTime(date); - - int year = cal.get(Calendar.YEAR); - int month = cal.get(Calendar.MONTH) + 1; - int day = cal.get(Calendar.DAY_OF_MONTH); - - java.time.LocalDate todayDate = java.time.LocalDate.now(); - java.time.LocalDate birthdate = java.time.LocalDate.of(year, month, day); - Period p = Period.between(birthdate, todayDate); - - int d = p.getDays(); - int mo = p.getMonths(); - int y = p.getYears(); - - if (y > 0) { - ageDetails = y + " years - " + mo + " months"; - age_val = y; - ageUnit = (age_val > 1) ? "Years" : "Year"; - } else { - if (mo > 0) { - ageDetails = mo + " months - " + d + " days"; - age_val = mo; - ageUnit = (age_val > 1) ? "Months" : "Month"; + if (benID != null) + benDetailsRMNCH_OBJ.setBenficieryid(benID.longValue()); + + if (benDetailsOBJ.getLastName() != null) + benDetailsRMNCH_OBJ.setLastName(benDetailsOBJ.getLastName()); + + if (benDetailsRMNCH_OBJ.getCreatedBy() == null) + if (benDetailsOBJ.getCreatedBy() != null) + benDetailsRMNCH_OBJ.setCreatedBy(benDetailsOBJ.getCreatedBy()); + + // age calculation + String ageDetails = ""; + int age_val = 0; + String ageUnit = null; + if (benDetailsOBJ.getDob() != null) { + + Date date = new Date(benDetailsOBJ.getDob().getTime()); + Calendar cal = Calendar.getInstance(); + + cal.setTime(date); + + int year = cal.get(Calendar.YEAR); + int month = cal.get(Calendar.MONTH) + 1; + int day = cal.get(Calendar.DAY_OF_MONTH); + + java.time.LocalDate todayDate = java.time.LocalDate.now(); + java.time.LocalDate birthdate = java.time.LocalDate.of(year, month, day); + Period p = Period.between(birthdate, todayDate); + + int d = p.getDays(); + int mo = p.getMonths(); + int y = p.getYears(); + + if (y > 0) { + ageDetails = y + " years - " + mo + " months"; + age_val = y; + ageUnit = (age_val > 1) ? "Years" : "Year"; } else { - ageDetails = d + " days"; - age_val = d; - ageUnit = (age_val > 1) ? "Days" : "Day"; + if (mo > 0) { + ageDetails = mo + " months - " + d + " days"; + age_val = mo; + ageUnit = (age_val > 1) ? "Months" : "Month"; + } else { + ageDetails = d + " days"; + age_val = d; + ageUnit = (age_val > 1) ? "Days" : "Day"; + } } - } - } + } - benDetailsRMNCH_OBJ.setAgeFull(ageDetails); - benDetailsRMNCH_OBJ.setAge(age_val); - if (ageUnit != null) - benDetailsRMNCH_OBJ.setAge_unit(ageUnit); - - resultMap = new HashMap<>(); - if (benHouseHoldRMNCH_ROBJ != null) - resultMap.put("householdDetails", benHouseHoldRMNCH_ROBJ); - else - resultMap.put("householdDetails", new HashMap()); - - if (benBotnBirthRMNCH_ROBJ != null) - resultMap.put("bornbirthDeatils", benBotnBirthRMNCH_ROBJ); - else - resultMap.put("bornbirthDeatils", new HashMap()); - - resultMap.put("beneficiaryDetails", benDetailsRMNCH_OBJ); - resultMap.put("abhaHealthDetails", healthDetails); - resultMap.put("houseoldId", benDetailsRMNCH_OBJ.getHouseoldId()); - resultMap.put("benficieryid", benDetailsRMNCH_OBJ.getBenficieryid()); - resultMap.put("isDeath", benDetailsRMNCH_OBJ.getIsDeath()); - resultMap.put("isDeathValue", benDetailsRMNCH_OBJ.getIsDeathValue()); - resultMap.put("dateOfDeath",benDetailsRMNCH_OBJ.getDateOfDeath()); - resultMap.put("timeOfDeath", benDetailsRMNCH_OBJ.getTimeOfDeath()); - resultMap.put("reasonOfDeath", benDetailsRMNCH_OBJ.getReasonOfDeath()); - resultMap.put("reasonOfDeathId", benDetailsRMNCH_OBJ.getReasonOfDeathId()); - resultMap.put("placeOfDeath", benDetailsRMNCH_OBJ.getPlaceOfDeath()); - resultMap.put("placeOfDeathId", benDetailsRMNCH_OBJ.getPlaceOfDeathId()); - resultMap.put("isSpouseAdded", benDetailsRMNCH_OBJ.getIsSpouseAdded()); - resultMap.put("isChildrenAdded", benDetailsRMNCH_OBJ.getIsChildrenAdded()); - resultMap.put("noOfchildren", benDetailsRMNCH_OBJ.getNoOfchildren()); - resultMap.put("isMarried", benDetailsRMNCH_OBJ.getIsMarried()); - resultMap.put("doYouHavechildren", benDetailsRMNCH_OBJ.getDoYouHavechildren()); - resultMap.put("noofAlivechildren ",benDetailsRMNCH_OBJ.getNoofAlivechildren()); - - - - resultMap.put("BenRegId", m.getBenRegId()); - - // adding asha id / created by - user id - if (benAddressOBJ.getCreatedBy() != null) { - Integer userID = beneficiaryRepo.getUserIDByUserName(benAddressOBJ.getCreatedBy()); - if (userID != null && userID > 0) - resultMap.put("ashaId", userID); - } - // get HealthID of ben - if (m.getBenRegId() != null) { - fetchHealthIdByBenRegID(m.getBenRegId().longValue(), authorisation, resultMap); - } + benDetailsRMNCH_OBJ.setAgeFull(ageDetails); + benDetailsRMNCH_OBJ.setAge(age_val); + if (ageUnit != null) + benDetailsRMNCH_OBJ.setAge_unit(ageUnit); + + resultMap = new HashMap<>(); + if (benHouseHoldRMNCH_ROBJ != null) + resultMap.put("householdDetails", benHouseHoldRMNCH_ROBJ); + else + resultMap.put("householdDetails", new HashMap()); + + if (benBotnBirthRMNCH_ROBJ != null) + resultMap.put("bornbirthDeatils", benBotnBirthRMNCH_ROBJ); + else + resultMap.put("bornbirthDeatils", new HashMap()); + + resultMap.put("beneficiaryDetails", benDetailsRMNCH_OBJ); + resultMap.put("abhaHealthDetails", healthDetails); + resultMap.put("houseoldId", benDetailsRMNCH_OBJ.getHouseoldId()); + resultMap.put("benficieryid", benDetailsRMNCH_OBJ.getBenficieryid()); + resultMap.put("isDeath", benDetailsRMNCH_OBJ.getIsDeath()); + resultMap.put("isDeathValue", benDetailsRMNCH_OBJ.getIsDeathValue()); + resultMap.put("dateOfDeath",benDetailsRMNCH_OBJ.getDateOfDeath()); + resultMap.put("timeOfDeath", benDetailsRMNCH_OBJ.getTimeOfDeath()); + resultMap.put("reasonOfDeath", benDetailsRMNCH_OBJ.getReasonOfDeath()); + resultMap.put("reasonOfDeathId", benDetailsRMNCH_OBJ.getReasonOfDeathId()); + resultMap.put("placeOfDeath", benDetailsRMNCH_OBJ.getPlaceOfDeath()); + resultMap.put("placeOfDeathId", benDetailsRMNCH_OBJ.getPlaceOfDeathId()); + resultMap.put("isSpouseAdded", benDetailsRMNCH_OBJ.getIsSpouseAdded()); + resultMap.put("isChildrenAdded", benDetailsRMNCH_OBJ.getIsChildrenAdded()); + resultMap.put("noOfchildren", benDetailsRMNCH_OBJ.getNoOfchildren()); + resultMap.put("isMarried", benDetailsRMNCH_OBJ.getIsMarried()); + resultMap.put("doYouHavechildren", benDetailsRMNCH_OBJ.getDoYouHavechildren()); + resultMap.put("noofAlivechildren",benDetailsRMNCH_OBJ.getNoofAlivechildren()); + resultMap.put("isDeactivate", benDetailsRMNCH_OBJ.getIsDeactivate() != null + ? benDetailsRMNCH_OBJ.getIsDeactivate() + : false + ); + resultMap.put("BenRegId", m.getBenRegId()); + + // adding asha id / created by - user id + if (benAddressOBJ.getCreatedBy() != null) { + Integer userID = beneficiaryRepo.getUserIDByUserName(benAddressOBJ.getCreatedBy()); + if (userID != null && userID > 0) + resultMap.put("ashaId", userID); + } + // get HealthID of ben + if (m.getBenRegId() != null) { + fetchHealthIdByBenRegID(m.getBenRegId().longValue(), authorisation, resultMap); + } - resultList.add(resultMap); + resultList.add(resultMap); - } else { - // mapping not available + } else { + // mapping not available + } } + } catch (Exception e) { - logger.error("error for addressID :"+e.getMessage() + a.getId() + " and vanID : " + a.getVanID()); + logger.info("Error for ben :"+e.getMessage()); + logger.info("Error for ben :"+e); + logger.error("error for addressID :" + e.getMessage() + a.getId() + " and vanID : " + a.getVanID()); } } @@ -440,33 +474,33 @@ private String getMappingsForAddressIDs(List addressLi } - private Map getBenHealthDetails(BigInteger benRegId) { - Map healthDetails = new HashMap<>(); - if (null != benRegId) { - Object[] benHealthIdNumber = beneficiaryRepo.getBenHealthIdNumber(benRegId); - if (benHealthIdNumber != null && benHealthIdNumber.length > 0) { - Object[] healthData = (Object[]) benHealthIdNumber[0]; - String healthIdNumber = healthData[0] != null ? healthData[0].toString() : null; - String healthId = healthData[1] != null ? healthData[1].toString() : null; - - if (null != healthIdNumber) { - List health = beneficiaryRepo.getBenHealthDetails(healthIdNumber); - if (health != null && !health.isEmpty()) { - for (Object[] objects : health) { - healthDetails.put("HealthID", objects[0]); - healthDetails.put("HealthIdNumber", objects[1]); - healthDetails.put("isNewAbha", objects[2]); - } - } else { - healthDetails.put("HealthIdNumber", healthIdNumber); - healthDetails.put("HealthID", healthId); - healthDetails.put("isNewAbha", null); - } - } - } - } - return healthDetails; - } + private Map getBenHealthDetails(BigInteger benRegId) { + Map healthDetails = new HashMap<>(); + if (null != benRegId) { + Object[] benHealthIdNumber = beneficiaryRepo.getBenHealthIdNumber(benRegId); + if (benHealthIdNumber != null && benHealthIdNumber.length > 0) { + Object[] healthData = (Object[]) benHealthIdNumber[0]; + String healthIdNumber = healthData[0] != null ? healthData[0].toString() : null; + String healthId = healthData[1] != null ? healthData[1].toString() : null; + + if (null != healthIdNumber) { + List health = beneficiaryRepo.getBenHealthDetails(healthIdNumber); + if (health != null && !health.isEmpty()) { + for (Object[] objects : health) { + healthDetails.put("HealthID", objects[0]); + healthDetails.put("HealthIdNumber", objects[1]); + healthDetails.put("isNewAbha", objects[2]); + } + } else { + healthDetails.put("HealthIdNumber", healthIdNumber); + healthDetails.put("HealthID", healthId); + healthDetails.put("isNewAbha", null); + } + } + } + } + return healthDetails; + } private Map getBenBenVisitDetails(BigInteger benRegId) { Map healthDetails = new HashMap<>(); @@ -484,7 +518,7 @@ private Map getBenBenVisitDetails(BigInteger benRegId) { return healthDetails; } - public void fetchHealthIdByBenRegID(Long benRegID, String authorization, Map resultMap) { + public void fetchHealthIdByBenRegID(Long benRegID, String authorization, Map resultMap) { Map requestMap = new HashMap(); requestMap.put("beneficiaryRegID", benRegID); requestMap.put("beneficiaryID", null); @@ -523,7 +557,7 @@ public void fetchHealthIdByBenRegID(Long benRegID, String authorization, Map eyeCheckupRequestDTOS) { + public String saveEyeCheckupVsit(List eyeCheckupRequestDTOS, String token) { try { for (EyeCheckupRequestDTO dto : eyeCheckupRequestDTOS) { @@ -531,41 +565,72 @@ public String saveEyeCheckupVsit(List eyeCheckupRequestDTO visit.setBeneficiaryId(dto.getBeneficiaryId()); visit.setHouseholdId(dto.getHouseHoldId()); - visit.setUserId(userRepo.getUserIdByName(jwtUtil.getUserNameFromStorage())); // cache se lena hai + visit.setUserId(jwtUtil.extractUserId(token)); visit.setCreatedBy(dto.getUserName()); - StringBuilder sb = new StringBuilder(); - - // fields mapping EyeCheckupListDTO f = dto.getFields(); - sb.append(f.getDischarge_summary_upload()); - String longText = sb.toString(); + + visit.setSymptomsObserved(f.getSymptomsAsString()); + + String upload = f.getDischarge_summary_upload(); + visit.setDischargeSummaryUpload( + (upload != null && !upload.equalsIgnoreCase("null")) ? upload : null + ); + visit.setVisitDate(LocalDate.parse(f.getVisit_date(), FORMATTER)); - visit.setSymptomsObserved(f.getSymptoms_observed()); + + visit.setDateOfSurgery(f.getDate_of_surgery()); + + visit.setEyeAffected(f.getEye_affected()); visit.setReferredTo(f.getReferred_to()); - visit.setDischargeSummaryUpload(longText); visit.setFollowUpStatus(f.getFollow_up_status()); - visit.setDateOfSurgery(f.getDate_of_surgery()); - // save/update eyeCheckUpVisitRepo.save(visit); + if (visit.getReferredTo() != null) { + if (visit.getReferredTo().equals("Govt Public Facility")) { + LocalDate localDate = visit.getVisitDate(); + + Timestamp visitDate = Timestamp.valueOf(localDate.atStartOfDay()); + incentiveLogicService.incentiveForEyeSurgeyReferGovtHospital(visit.getBeneficiaryId(), visitDate, visitDate, visit.getUserId()); + } + + if (visit.getReferredTo().equals("Private Facility")) { + LocalDate localDate = visit.getVisitDate(); + + Timestamp visitDate = Timestamp.valueOf(localDate.atStartOfDay()); + incentiveLogicService.incentiveForEyeSurgeyReferPrivateHospital(visit.getBeneficiaryId(), visitDate, visitDate, visit.getUserId()); + } + } + } + return "Eye checkup data saved successfully."; - } catch (Exception e) { + + } catch (DateTimeParseException e) { e.printStackTrace(); + throw new RuntimeException("Invalid date format. Expected dd-MM-yyyy. " + e.getMessage()); + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException("Failed to save eye checkup data: " + e.getMessage()); } - return null ; } @Override - public List getEyeCheckUpVisit(GetBenRequestHandler request) { - List visits = eyeCheckUpVisitRepo.findByUserId(request.getAshaId()); + public List getEyeCheckUpVisit(GetBenRequestHandler request,String token) { + String createdBy = null; + try { + createdBy = userService.getUserDetail(jwtUtil.extractUserId(token)).getUserName(); + } catch (Exception e) { + logger.error("Error extracting userId from token: " + e.getMessage()); + } + List visits = eyeCheckUpVisitRepo.findByCreatedBy(createdBy); return visits.stream().map(v -> { EyeCheckupRequestDTO dto = new EyeCheckupRequestDTO(); dto.setId(v.getId()); + dto.setEyeSide(v.getEyeAffected()); dto.setBeneficiaryId(v.getBeneficiaryId()); dto.setHouseHoldId(v.getHouseholdId()); dto.setUserName(v.getCreatedBy()); @@ -582,6 +647,22 @@ public List getEyeCheckUpVisit(GetBenRequestHandler reques dto.setFields(fields); + if (v.getReferredTo() != null) { + if (v.getReferredTo().equals("Govt Public Facility")) { + LocalDate localDate = v.getVisitDate(); + + Timestamp visitDate = Timestamp.valueOf(localDate.atStartOfDay()); + incentiveLogicService.incentiveForEyeSurgeyReferGovtHospital(v.getBeneficiaryId(), visitDate, visitDate, v.getUserId()); + } + + if (v.getReferredTo().equals("Private Facility")) { + LocalDate localDate = v.getVisitDate(); + + Timestamp visitDate = Timestamp.valueOf(localDate.atStartOfDay()); + incentiveLogicService.incentiveForEyeSurgeyReferPrivateHospital(v.getBeneficiaryId(), visitDate, visitDate, v.getUserId()); + } + } + return dto; }).collect(Collectors.toList()); } diff --git a/src/main/java/com/iemr/flw/service/impl/CampaignServiceImpl.java b/src/main/java/com/iemr/flw/service/impl/CampaignServiceImpl.java new file mode 100644 index 00000000..f9c07632 --- /dev/null +++ b/src/main/java/com/iemr/flw/service/impl/CampaignServiceImpl.java @@ -0,0 +1,507 @@ +package com.iemr.flw.service.impl; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.iemr.flw.domain.iemr.*; +import com.iemr.flw.dto.iemr.*; +import com.iemr.flw.masterEnum.GroupName; +import com.iemr.flw.repo.iemr.*; +import com.iemr.flw.service.CampaignService; +import com.iemr.flw.utils.JwtUtil; +import com.iemr.flw.utils.exception.IEMRException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.multipart.MultipartFile; + +import java.io.IOException; +import java.sql.Timestamp; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.Base64; +import java.util.Collections; +import java.util.List; + +@Service +public class CampaignServiceImpl implements CampaignService { + + @Autowired + private OrsCampaignRepo orsCampaignRepo; + + @Autowired + private PulsePolioCampaignRepo pulsePolioCampaignRepo; + + @Autowired + private FilariasisCampaignRepo filariasisCampaignRepo; + + @Autowired + private JwtUtil jwtUtil; + + @Autowired + IncentivesRepo incentivesRepo; + + @Autowired + IncentiveRecordRepo recordRepo; + + @Autowired + private ObjectMapper objectMapper = new ObjectMapper(); + + @Autowired + private UserServiceRoleRepo userServiceRoleRepo; + + @Override + @Transactional + public List saveOrsCampaign(List orsCampaignDTO, String token) throws IEMRException, JsonProcessingException { + if (orsCampaignDTO == null || orsCampaignDTO.isEmpty()) { + return Collections.emptyList(); // + } + + List campaignOrsRequest = new ArrayList<>(); + Integer userId = jwtUtil.extractUserId(token); + String userName = userServiceRoleRepo.getUserNamedByUserId(userId); + + for (OrsCampaignDTO campaignDTO : orsCampaignDTO) { + if (campaignDTO.getFields() == null) { + continue; + } + + CampaignOrs campaignOrsEntity = new CampaignOrs(); + campaignOrsEntity.setUserId(userId); + campaignOrsEntity.setCreatedBy(userName); + campaignOrsEntity.setUpdatedBy(userName); + campaignOrsEntity.setStartDate(campaignDTO.getFields().getStartDate()); + campaignOrsEntity.setEndDate(campaignDTO.getFields().getEndDate()); + + try { + String familiesStr = campaignDTO.getFields().getNumberOfFamilies(); + if (familiesStr != null && !familiesStr.trim().isEmpty()) { + double familiesDouble = Double.parseDouble(familiesStr); + campaignOrsEntity.setNumberOfFamilies((int) familiesDouble); + } else { + campaignOrsEntity.setNumberOfFamilies(0); // default 0 + } + } catch (NumberFormatException e) { + throw new IEMRException("Invalid number format for families: " + campaignDTO.getFields().getNumberOfFamilies()); + } + + OrsCampaignListDTO campaignListDTO = campaignDTO.getFields(); + + if(campaignListDTO!=null){ + String photosJson = objectMapper.writeValueAsString(campaignListDTO.getCampaignPhotos()); + campaignOrsEntity.setCampaignPhotos(photosJson); + } + + campaignOrsRequest.add(campaignOrsEntity); + } + + if (!campaignOrsRequest.isEmpty()) { + List savedCampaigns = orsCampaignRepo.saveAll(campaignOrsRequest); + savedCampaigns.forEach(ors->{ + checkMonthlyPulseOrsDistribution(ors.getUserId(),ors.getStartDate(),ors.getEndDate()); + }); + return savedCampaigns; + } + + return Collections.emptyList(); + } + + @Override + public List getOrsCampaign(String token) throws IEMRException { + Integer userId = jwtUtil.extractUserId(token); + List orsCampaignDTOSResponse = new ArrayList<>(); + int page = 0; + int pageSize = 10; + Page campaignOrsPage; + do { + Pageable pageable = PageRequest.of(page, pageSize); + campaignOrsPage = orsCampaignRepo.findByUserId(userId, pageable); + for (CampaignOrs campaignOrs : campaignOrsPage.getContent()) { + OrsCampaignResponseDTO dto = convertOrsToDTO(campaignOrs); + orsCampaignDTOSResponse.add(dto); + } + page++; + } while (campaignOrsPage.hasNext()); + return orsCampaignDTOSResponse; + } + + + @Override + @Transactional + public List savePolioCampaign(List polioCampaignDTOs, String token) + throws IEMRException, JsonProcessingException { + + if (polioCampaignDTOs == null || polioCampaignDTOs.isEmpty()) { + throw new IEMRException("Campaign data is required"); + } + + List campaignPolioRequest = new ArrayList<>(); + Integer userId = jwtUtil.extractUserId(token); + String userName = userServiceRoleRepo.getUserNamedByUserId(userId); + + for (PolioCampaignDTO campaignDTO : polioCampaignDTOs) { + if (campaignDTO.getFields() == null) { + continue; + } + + PulsePolioCampaign campaignPolioEntity = new PulsePolioCampaign(); + campaignPolioEntity.setUserId(userId); + campaignPolioEntity.setCreatedBy(userName); + campaignPolioEntity.setUpdatedBy(userName); + + // Set start and end dates + campaignPolioEntity.setStartDate(campaignDTO.getFields().getStartDate()); + campaignPolioEntity.setEndDate(campaignDTO.getFields().getEndDate()); + + // Parse number of children + try { + String childrenStr = campaignDTO.getFields().getNumberOfChildren(); + if (childrenStr != null && !childrenStr.trim().isEmpty()) { + try { + // parse as double first, then cast to int + double childrenDouble = Double.parseDouble(childrenStr); + campaignPolioEntity.setNumberOfChildren((int) childrenDouble); + } catch (NumberFormatException e) { + campaignPolioEntity.setNumberOfChildren(0); // default 0 if invalid + } + } else { + campaignPolioEntity.setNumberOfChildren(0); + } + } catch (NumberFormatException e) { + throw new IEMRException("Invalid number format for children: " + e.getMessage()); + } + + // Convert photos to base64 JSON array + + PolioCampaignListDTO polioCampaignListDTO = campaignDTO.getFields(); + if(polioCampaignListDTO!=null){ + String photosJson = objectMapper.writeValueAsString(polioCampaignListDTO.getCampaignPhotos()); + campaignPolioEntity.setCampaignPhotos(photosJson); + + + } + + campaignPolioRequest.add(campaignPolioEntity); + } + + if (!campaignPolioRequest.isEmpty()) { + List savedCampaigns = pulsePolioCampaignRepo.saveAll(campaignPolioRequest); + savedCampaigns.forEach(this::checkIncentiveForPulsePolio); + + return savedCampaigns; + } + + throw new IEMRException("No valid campaign data to save"); + } + + private void checkIncentiveForPulsePolio(PulsePolioCampaign pulsePolioCampaign){ + checkMonthlyPulsePolioIncentive(pulsePolioCampaign.getUserId(),pulsePolioCampaign.getStartDate(),pulsePolioCampaign.getEndDate()); + + } + + @Override + @Transactional + public List saveFilariasisCampaign(List filariasisCampaignDTOS, String token) throws IEMRException, JsonProcessingException { + + if (filariasisCampaignDTOS == null || filariasisCampaignDTOS.isEmpty()) { + throw new IEMRException("Campaign data is required"); + } + + List campaignPolioRequest = new ArrayList<>(); + Integer userId = jwtUtil.extractUserId(token); + String userName = userServiceRoleRepo.getUserNamedByUserId(userId); + + for (FilariasisCampaignDTO campaignDTO : filariasisCampaignDTOS) { + if (campaignDTO.getFields() == null) { + continue; + } + + FilariasisCampaign filariasisCampaign = new FilariasisCampaign(); + filariasisCampaign.setUserId(userId); + filariasisCampaign.setCreatedBy(userName); + filariasisCampaign.setUpdatedBy(userName); + + // Set start and end dates + filariasisCampaign.setStartDate(campaignDTO.getFields().getStartDate()); + filariasisCampaign.setEndDate(campaignDTO.getFields().getEndDate()); + + // Parse number of children + try { + String numberOfFamilies = (campaignDTO != null && campaignDTO.getFields() != null) + ? campaignDTO.getFields().getNumberOfFamilies() + : null; + + String numberOfIndividuals = (campaignDTO != null && campaignDTO.getFields() != null) + ? campaignDTO.getFields().getNumberOfIndividuals() + : null; + if (numberOfFamilies != null && !numberOfFamilies.trim().isEmpty()) { + try { + // parse as double first, then cast to int + double noDouble = Double.parseDouble(numberOfFamilies); + filariasisCampaign.setNumberOfFamilies((int) noDouble); + } catch (NumberFormatException e) { + filariasisCampaign.setNumberOfFamilies(0); // default 0 if invalid + } + } else { + filariasisCampaign.setNumberOfFamilies(0); + } + + if (numberOfIndividuals != null && !numberOfIndividuals.trim().isEmpty()) { + try { + // parse as double first, then cast to int + double noDouble = Double.parseDouble(numberOfIndividuals); + filariasisCampaign.setNumberOfIndividuals((int) noDouble); + } catch (NumberFormatException e) { + filariasisCampaign.setNumberOfIndividuals(0); // default 0 if invalid + } + } else { + filariasisCampaign.setNumberOfIndividuals(0); + } + } catch (NumberFormatException e) { + throw new IEMRException("Invalid number format for children: " + e.getMessage()); + } + + // Convert photos to base64 JSON array + FilariasisCampaignListDTO filariasisCampaignListDTO = campaignDTO.getFields(); + if(filariasisCampaignListDTO!=null){ + String photosJson = objectMapper.writeValueAsString(filariasisCampaignListDTO.getMdaPhotos()); + filariasisCampaign.setCampaignPhotos(photosJson); + + + } + + campaignPolioRequest.add(filariasisCampaign); + } + + if (!campaignPolioRequest.isEmpty()) { + List savedCampaigns = filariasisCampaignRepo.saveAll(campaignPolioRequest); + + savedCampaigns.forEach(filariasisCampaign -> { + checkMonthlyFilariasisIncentive(filariasisCampaign.getUserId(),filariasisCampaign.getStartDate(),filariasisCampaign.getEndDate()); + + });{ + + } + return savedCampaigns; + } + + throw new IEMRException("No valid campaign data to save"); + } + + + + @Override + public List getPolioCampaign(String token) throws IEMRException { + Integer userId = jwtUtil.extractUserId(token); + List polioCampaignDTOSResponse = new ArrayList<>(); + int page = 0; + int pageSize = 10; + Page campaignPolioPage; + do { + Pageable pageable = PageRequest.of(page, pageSize); + campaignPolioPage = pulsePolioCampaignRepo.findByUserId(userId, pageable); + for (PulsePolioCampaign campaignOrs : campaignPolioPage.getContent()) { + PolioCampaignResponseDTO dto = convertPolioToDTO(campaignOrs); + polioCampaignDTOSResponse.add(dto); + + } + page++; + } while (campaignPolioPage.hasNext()); + return polioCampaignDTOSResponse; + } + + @Override + public List getAllFilariasisCampaign(String token) throws IEMRException { + Integer userId = jwtUtil.extractUserId(token); + List filariasisResponseDTOS = new ArrayList<>(); + int page = 0; + int pageSize = 10; + Page campaignPolioPage; + do { + Pageable pageable = PageRequest.of(page, pageSize); + campaignPolioPage = filariasisCampaignRepo.findByUserId(userId, pageable); + for (FilariasisCampaign campaignOrs : campaignPolioPage.getContent()) { + FilariasisResponseDTO dto = convertFilariasisDTO(campaignOrs); + filariasisResponseDTOS.add(dto); + } + page++; + } while (campaignPolioPage.hasNext()); + return filariasisResponseDTOS; + } + + + private OrsCampaignResponseDTO convertOrsToDTO(CampaignOrs campaign) { + OrsCampaignResponseDTO dto = new OrsCampaignResponseDTO(); + OrsCampaignListResponseDTO orsCampaignListDTO = new OrsCampaignListResponseDTO(); + if (campaign.getCampaignPhotos() != null) { + ObjectMapper objectMapper = new ObjectMapper(); + List photosList = new ArrayList<>(); + + String photoStr = campaign.getCampaignPhotos(); + + if (photoStr != null && !photoStr.trim().isEmpty()) { + try { + // try JSON list + photosList = objectMapper.readValue( + photoStr, + new TypeReference>() {} + ); + } catch (Exception e) { + // fallback: single image + photosList.add(photoStr.trim()); + } + } + orsCampaignListDTO.setCampaignPhotos(photosList); + } + orsCampaignListDTO.setEndDate(campaign.getEndDate()); + orsCampaignListDTO.setStartDate(campaign.getStartDate()); + orsCampaignListDTO.setNumberOfFamilies(Double.valueOf(campaign.getNumberOfFamilies())); + dto.setId(campaign.getId()); + dto.setFields(orsCampaignListDTO); + + return dto; + } + + private PolioCampaignResponseDTO convertPolioToDTO(PulsePolioCampaign campaign) { + PolioCampaignResponseDTO dto = new PolioCampaignResponseDTO(); + PolioCampaignListResponseDTO polioCampaignListDTO = new PolioCampaignListResponseDTO(); + if (campaign.getCampaignPhotos() != null) { + List photosList = new ArrayList<>(); + + String photoStr = campaign.getCampaignPhotos(); + + if (photoStr != null && !photoStr.trim().isEmpty()) { + try { + // try JSON list + photosList = objectMapper.readValue( + photoStr, + new TypeReference>() {} + ); + } catch (Exception e) { + // fallback: single image + photosList.add(photoStr.trim()); + } + } + + polioCampaignListDTO.setCampaignPhotos(photosList); + } + polioCampaignListDTO.setEndDate(campaign.getEndDate()); + polioCampaignListDTO.setStartDate(campaign.getStartDate()); + polioCampaignListDTO.setNumberOfChildren(Double.valueOf(campaign.getNumberOfChildren())); + dto.setId(campaign.getId()); + dto.setFields(polioCampaignListDTO); + return dto; + } + + private FilariasisResponseDTO convertFilariasisDTO(FilariasisCampaign campaign) { + FilariasisResponseDTO dto = new FilariasisResponseDTO(); + FilariasisCampaignListResponseDTO filariasisCampaignListDTO = new FilariasisCampaignListResponseDTO(); + if (campaign.getCampaignPhotos() != null) { + List photosList = new ArrayList<>(); + String photoStr = campaign.getCampaignPhotos(); + + if (photoStr != null && !photoStr.trim().isEmpty()) { + try { + // try JSON list + photosList = objectMapper.readValue( + photoStr, + new TypeReference>() {} + ); + } catch (Exception e) { + // fallback: single image + photosList.add(photoStr.trim()); + } + } + + filariasisCampaignListDTO.setMdaPhotos(photosList); + } + filariasisCampaignListDTO.setEndDate(campaign.getEndDate()); + filariasisCampaignListDTO.setStartDate(campaign.getStartDate()); + Double numberOfIndividuals = null; + Double numberOfFamilies = null; + + if (campaign.getNumberOfIndividuals() != null) { + numberOfIndividuals = Double.valueOf(campaign.getNumberOfIndividuals()); + } + + if (campaign.getNumberOfFamilies() != null) { + numberOfFamilies = Double.valueOf(campaign.getNumberOfFamilies()); + } + + filariasisCampaignListDTO.setNumberOfIndividuals(numberOfIndividuals); + filariasisCampaignListDTO.setNumberOfFamilies(numberOfFamilies); + + dto.setFields(filariasisCampaignListDTO); + return dto; + } + private List parseBase64Json(String jsonString) { + try { + ObjectMapper objectMapper = new ObjectMapper(); + return objectMapper.readValue(jsonString, new TypeReference>() {}); + } catch (JsonProcessingException e) { + return Collections.emptyList(); + } + } + + private void checkMonthlyPulsePolioIncentive(Integer ashaId,LocalDate startDate,LocalDate endDate) { + + IncentiveActivity CHILD_MOBILIZATION_SESSIONS = incentivesRepo.findIncentiveMasterByNameAndGroup("PULSE_POLIO_SUPPORT", GroupName.ACTIVITY.getDisplayName()); + if (CHILD_MOBILIZATION_SESSIONS != null) { + addAshaIncentiveRecord(CHILD_MOBILIZATION_SESSIONS, ashaId,startDate,endDate); + } + + } + + private void checkMonthlyFilariasisIncentive(Integer ashaId,LocalDate startDate,LocalDate endDate) { + IncentiveActivity FILARIASIS_MEDICINE_DISTRIBUTION = incentivesRepo.findIncentiveMasterByNameAndGroup("FILARIASIS_MEDICINE_DISTRIBUTION", GroupName.ACTIVITY.getDisplayName()); + if (FILARIASIS_MEDICINE_DISTRIBUTION != null) { + addAshaIncentiveRecord(FILARIASIS_MEDICINE_DISTRIBUTION, ashaId,startDate,endDate); + } + + } + + private void checkMonthlyPulseOrsDistribution(Integer ashaId,LocalDate startDate,LocalDate endDate) { + IncentiveActivity ORS_DISTRIBUTION = incentivesRepo.findIncentiveMasterByNameAndGroup("ORS_DISTRIBUTION", GroupName.ACTIVITY.getDisplayName()); + if (ORS_DISTRIBUTION != null) { + addAshaIncentiveRecord(ORS_DISTRIBUTION, ashaId,startDate,endDate); + } + + } + + private void addAshaIncentiveRecord(IncentiveActivity incentiveActivity, Integer ashaId,LocalDate startDate,LocalDate endDate) { + Timestamp timestamp = Timestamp.valueOf(LocalDateTime.now()); + + Timestamp startOfMonth = Timestamp.valueOf(startDate.atStartOfDay()); + Timestamp endOfMonth = Timestamp.valueOf(endDate.atStartOfDay()); + + IncentiveActivityRecord record = recordRepo.findRecordByActivityIdCreatedDateBenId( + incentiveActivity.getId(), + 0L, + ashaId, + startOfMonth, + endOfMonth + ); + + + if (record == null) { + record = new IncentiveActivityRecord(); + record.setActivityId(incentiveActivity.getId()); + record.setCreatedDate(timestamp); + record.setCreatedBy(userServiceRoleRepo.getUserNamedByUserId(ashaId)); + record.setStartDate(timestamp); + record.setEndDate(timestamp); + record.setUpdatedDate(timestamp); + record.setUpdatedBy(userServiceRoleRepo.getUserNamedByUserId(ashaId)); + record.setBenId(0L); + record.setAshaId(ashaId); + record.setAmount(Long.valueOf(incentiveActivity.getRate())); + recordRepo.save(record); + } + } + +} + diff --git a/src/main/java/com/iemr/flw/service/impl/ChildCareServiceImpl.java b/src/main/java/com/iemr/flw/service/impl/ChildCareServiceImpl.java index 43a9eb1f..8a840f05 100644 --- a/src/main/java/com/iemr/flw/service/impl/ChildCareServiceImpl.java +++ b/src/main/java/com/iemr/flw/service/impl/ChildCareServiceImpl.java @@ -3,19 +3,28 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.Gson; import com.iemr.flw.domain.iemr.*; import com.iemr.flw.dto.identity.GetBenRequestHandler; import com.iemr.flw.dto.iemr.*; import com.iemr.flw.masterEnum.GroupName; +import com.iemr.flw.masterEnum.StateCode; import com.iemr.flw.repo.identity.BeneficiaryRepo; import com.iemr.flw.repo.iemr.*; import com.iemr.flw.service.ChildCareService; +import com.iemr.flw.service.IncentiveLogicService; +import com.iemr.flw.service.UserService; import com.iemr.flw.utils.JwtUtil; +import jakarta.persistence.EntityNotFoundException; import org.aspectj.weaver.ast.Or; import org.modelmapper.ModelMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; @@ -75,6 +84,19 @@ public class ChildCareServiceImpl implements ChildCareService { @Autowired private OrsDistributionRepo orsDistributionRepo; + @Autowired + private IncentivePendingActivityRepository incentivePendingActivityRepository; + + @Autowired + private UserService userService; + + @Autowired + private IncentiveLogicService incentiveLogicService; + + @Autowired + private ANCVisitRepo ancVisitRepo; + @Autowired + private PNCVisitRepo pncVisitRepo; @Override @@ -82,6 +104,7 @@ public String registerHBYC(List hbycDTOs) { try { List hbycList = new ArrayList<>(); + hbycDTOs.forEach(it -> { HbycDTO hbycDTO = it.getFields(); hbycDTO.setVisit_date(it.getVisitDate()); @@ -90,14 +113,11 @@ public String registerHBYC(List hbycDTOs) { if (hbyc != null) { Long id = hbyc.getId(); - modelMapper.map(it, hbycDTO); hbyc.setId(id); hbyc.setUserId(userRepo.getUserIdByName(it.getUserName())); hbyc.setCreated_by(it.getUserName()); } else { hbyc = new HbycChildVisit(); - modelMapper.map(it, hbycDTO); - hbyc.setId(null); hbyc.setUserId(userRepo.getUserIdByName(it.getUserName())); hbyc.setCreated_by(it.getUserName()); hbyc.setBeneficiaryId(it.getBeneficiaryId()); @@ -134,14 +154,17 @@ public String registerHBYC(List hbycDTOs) { hbyc.setMcp_card_images(hbycDTO.getMcp_card_images()); } + hbycList.add(hbyc); }); hbycRepo.saveAll(hbycList); + logger.info("Total records to save : {}", hbycList.size()); checkAndAddHbyncIncentives(hbycList); return "no of hbyc details saved: " + hbycDTOs.size(); } catch (Exception e) { - logger.info("error while saving hbyc details: " + e.getMessage()); + logger.error("error while saving hbyc details", e); + } return null; } @@ -210,46 +233,62 @@ public List getHbycRecords(GetBenRequestHandler dto) { @Override public List getHBNCDetails(GetBenRequestHandler dto) { List result = new ArrayList<>(); + int page = 0; + int size = 20; + + Page visitPage; try { - List hbncVisits = hbncVisitRepo.findByAshaId(dto.getAshaId()); + do { + Pageable pageable = PageRequest.of(page, size, Sort.by("id").descending()); - for (HbncVisit visit : hbncVisits) { - HbncVisitResponseDTO responseDTO = new HbncVisitResponseDTO(); - responseDTO.setId(visit.getId()); - responseDTO.setBeneficiaryId(visit.getBeneficiaryId()); // Update with actual value - responseDTO.setHouseHoldId(visit.getHouseHoldId()); // Update with actual value - responseDTO.setVisitDate(visit.getVisit_date().split(" ")[0]); // Format visit.getVisitDate() + visitPage = hbncVisitRepo.findByAshaId(dto.getAshaId(), pageable); - // Convert all fields to Map - Map fields = new HashMap<>(); - addIfValid(fields, "visit_day", visit.getVisit_day()); - addIfValid(fields, "due_date", visit.getDue_date()); - addIfValid(fields, "is_baby_alive", convert(visit.getIs_baby_alive())); - addIfValid(fields, "date_of_death", visit.getDate_of_death()); - addIfValid(fields, "reason_for_death", visit.getReasonForDeath()); - addIfValid(fields, "place_of_death", visit.getPlace_of_death()); - addIfValid(fields, "other_place_of_death", visit.getOther_place_of_death()); - addIfValid(fields, "baby_weight", visit.getBaby_weight()); - addIfValid(fields, "urine_passed", convert(visit.getUrine_passed())); - addIfValid(fields, "stool_passed", convert(visit.getStool_passed())); - addIfValid(fields, "diarrhoea", convert(visit.getDiarrhoea())); - addIfValid(fields, "vomiting", convert(visit.getVomiting())); - addIfValid(fields, "convulsions", convert(visit.getConvulsions())); - addIfValid(fields, "activity", visit.getActivity()); - addIfValid(fields, "sucking", visit.getSucking()); - addIfValid(fields, "breathing", visit.getBreathing()); - addIfValid(fields, "chest_indrawing", visit.getChest_indrawing()); - addIfValid(fields, "temperature", visit.getTemperature()); - addIfValid(fields, "jaundice", convert(visit.getJaundice())); - addIfValid(fields, "umbilical_stump", visit.getUmbilical_stump()); - addIfValid(fields, "discharged_from_sncu", convert(visit.getDischarged_from_sncu())); - addIfValid(fields, "discharge_summary_upload", visit.getDischarge_summary_upload()); - - // Add more fields as required - - responseDTO.setFields(fields); - result.add(responseDTO); - } + List hbncVisits = visitPage.getContent(); + + for (HbncVisit visit : hbncVisits) { + HbncVisitResponseDTO responseDTO = new HbncVisitResponseDTO(); + responseDTO.setId(visit.getId()); + responseDTO.setBeneficiaryId(visit.getBeneficiaryId()); // Update with actual value + responseDTO.setHouseHoldId(visit.getHouseHoldId()); // Update with actual value + responseDTO.setVisitDate(visit.getVisit_date().split(" ")[0]); // Format visit.getVisitDate() + + // Convert all fields to Map + Map fields = new HashMap<>(); + if(visit.getVisit_day()!=null){ + addIfValid(fields, "visit_day", visit.getVisit_day()); + + } + addIfValid(fields, "due_date", visit.getDue_date()); + addIfValid(fields, "is_baby_alive", convert(visit.getIs_baby_alive())); + addIfValid(fields, "date_of_death", visit.getDate_of_death()); + addIfValid(fields, "reason_for_death", visit.getReasonForDeath()); + addIfValid(fields, "place_of_death", visit.getPlace_of_death()); + addIfValid(fields, "other_place_of_death", visit.getOther_place_of_death()); + addIfValid(fields, "baby_weight", visit.getBaby_weight()); + addIfValid(fields, "urine_passed", convert(visit.getUrine_passed())); + addIfValid(fields, "stool_passed", convert(visit.getStool_passed())); + addIfValid(fields, "diarrhoea", convert(visit.getDiarrhoea())); + addIfValid(fields, "vomiting", convert(visit.getVomiting())); + addIfValid(fields, "convulsions", convert(visit.getConvulsions())); + addIfValid(fields, "activity", visit.getActivity()); + addIfValid(fields, "sucking", visit.getSucking()); + addIfValid(fields, "breathing", visit.getBreathing()); + addIfValid(fields, "chest_indrawing", visit.getChest_indrawing()); + addIfValid(fields, "temperature", visit.getTemperature()); + addIfValid(fields, "jaundice", convert(visit.getJaundice())); + addIfValid(fields, "umbilical_stump", visit.getUmbilical_stump()); + addIfValid(fields, "discharged_from_sncu", convert(visit.getDischarged_from_sncu())); + addIfValid(fields, "discharge_summary_upload", visit.getDischarge_summary_upload()); + + // Add more fields as required + + responseDTO.setFields(fields); + result.add(responseDTO); + + } + page++; + + }while (visitPage.hasNext()); } catch (Exception e) { logger.error("Error in getHBNCDetails: ", e); @@ -271,8 +310,8 @@ private String convert(Boolean value) { } private Boolean convertBollen(String value) { - if (value.equals("Yes")) { - return true; + if (value != null && !value.isEmpty()) { + return value.equalsIgnoreCase("Yes"); } else { return false; } @@ -291,7 +330,7 @@ private String convert(String value) { @Override - public String saveHBNCDetails(List hbncRequestDTOs) { + public String saveHBNCDetails(List hbncRequestDTOs, Integer userId) { try { List hbncList = new ArrayList<>(); @@ -321,7 +360,7 @@ public String saveHBNCDetails(List hbncRequestDTOs) { hbncVisitRepo.saveAll(hbncList); - checkAndAddHbncIncentives(hbncList); + checkAndAddHbncIncentives(hbncList, userId); logger.info("HBNC details saved"); @@ -341,11 +380,18 @@ public List getChildVaccinationDetails(GetBenRequestHandler List result = new ArrayList<>(); vaccinationDetails.forEach(childVaccination -> { ChildVaccinationDTO vaccinationDTO = mapper.convertValue(childVaccination, ChildVaccinationDTO.class); - BigInteger benId = beneficiaryRepo.getBenIdFromRegID(childVaccination.getBeneficiaryRegId()); - vaccinationDTO.setBeneficiaryId(benId.longValue()); + if(childVaccination.getBeneficiaryRegId()!=null){ + BigInteger benId = beneficiaryRepo.getBenIdFromRegID(childVaccination.getBeneficiaryRegId()); + vaccinationDTO.setBeneficiaryId(benId.longValue()); + + } + if(!vaccinationDetails.isEmpty()){ + checkAndAddIncentives(vaccinationDetails); + + } + result.add(vaccinationDTO); - checkAndAddIncentives(vaccinationDetails); }); return result; } catch (Exception e) { @@ -377,7 +423,10 @@ public String saveChildVaccinationDetails(List childVaccina vaccinationList.add(vaccination); }); childVaccinationRepo.saveAll(vaccinationList); - checkAndAddIncentives(vaccinationList); + if(!vaccinationList.isEmpty()){ + checkAndAddIncentives(vaccinationList); + + } logger.info("Child Vaccination details saved"); return "No of child vaccination details saved: " + vaccinationList.size(); } catch (Exception e) { @@ -436,7 +485,7 @@ public List getAllChildVaccines(String category) { } @Override - public String saveSamDetails(List samRequest) { + public String saveSamDetails(List samRequest, Integer userId, String userName) { try { List vaccinationList = new ArrayList<>(); @@ -457,11 +506,9 @@ public String saveSamDetails(List samRequest) { samVisit.setVisitDate(LocalDate.parse(samDTO.getVisitDate())); // ✅ Common user details - samVisit.setUserId(userRepo.getUserIdByName(jwtUtil.getUserNameFromStorage())); - samVisit.setCreatedBy(jwtUtil.getUserNameFromStorage()); - if (samVisit.getCreatedBy() == null) { - samVisit.setCreatedBy(jwtUtil.getUserNameFromStorage()); - } + samVisit.setUserId(userId); + samVisit.setCreatedBy(userName); + // ✅ Field mapping samVisit.setMuac(samDTO.getFields().getMuac()); @@ -491,7 +538,7 @@ public String saveSamDetails(List samRequest) { samVisitRepository.saveAll(vaccinationList); // ✅ Handle incentive logic - checkAndAddSamVisitNRCReferalIncentive(vaccinationList); + checkAndAddSamVisitNRCReferalIncentive(vaccinationList, userId); return "Saved/Updated " + samRequest.size() + " SAM visit records successfully"; } catch (Exception e) { @@ -531,8 +578,10 @@ public List getSamVisitsByBeneficiary(GetBenRequestHandler reque // ✅ Final Visit Label dto.setVisitLabel("Visit-" + visitNo); - - dto.setMuac(Double.parseDouble(entity.getMuac())); + String muacVal = entity.getMuac(); + dto.setMuac((muacVal != null && !muacVal.trim().isEmpty()) + ? Double.parseDouble(muacVal.trim()) + : 0.0); dto.setWeightForHeightStatus(entity.getWeightForHeightStatus()); dto.setIsChildReferredNrc(entity.getIsChildReferredNrc()); dto.setIsChildAdmittedNrc(entity.getIsChildAdmittedNrc()); @@ -544,7 +593,8 @@ public List getSamVisitsByBeneficiary(GetBenRequestHandler reque try { List followUpDates = mapper.readValue( entity.getFollowUpVisitDate(), - new TypeReference>() {}); + new TypeReference>() { + }); dto.setFollowUpVisitDate(followUpDates); } catch (JsonProcessingException e) { throw new RuntimeException(e); @@ -559,13 +609,13 @@ public List getSamVisitsByBeneficiary(GetBenRequestHandler reque } - return samResponseListDTO; } @Override public String saveOrsDistributionDetails(List orsDistributionDTOS) { try { + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy"); List orsDistributionList = new ArrayList<>(); orsDistributionDTOS.forEach(orsDistributionDTO -> { @@ -573,28 +623,31 @@ public String saveOrsDistributionDetails(List orsDistributio orsDistribution.setId(orsDistributionDTO.getId()); orsDistribution.setBeneficiaryId(orsDistributionDTO.getBeneficiaryId()); orsDistribution.setNumOrsPackets(orsDistributionDTO.getFields().getNum_ors_packets().toString()); - orsDistribution.setChildCount(orsDistributionDTO.getFields().getNum_under5_children().toString()); + if (orsDistributionDTO.getFields().getNum_under5_children() != null) { + orsDistribution.setChildCount(orsDistributionDTO.getFields().getNum_under5_children().toString()); + + } orsDistribution.setHouseholdId(orsDistributionDTO.getHouseHoldId()); orsDistribution.setUserId(userRepo.getUserIdByName(orsDistributionDTO.getUserName())); - orsDistribution.setVisitDate(LocalDate.parse(orsDistributionDTO.getFields().getVisit_date(),formatter)); + orsDistribution.setVisitDate(LocalDate.parse(orsDistributionDTO.getFields().getVisit_date(), formatter)); orsDistributionList.add(orsDistribution); }); - logger.info("orsList"+orsDistributionList.size()); - if(!orsDistributionList.isEmpty()){ + logger.info("orsList" + orsDistributionList.size()); + if (!orsDistributionList.isEmpty()) { orsDistributionRepo.saveAll(orsDistributionList); checkAndAddOrdDistributionIncentive(orsDistributionList); return "Saved " + orsDistributionList.size() + " ORS visit records successfully"; } - }catch (Exception e){ - logger.error("ORS Error"+e); + } catch (Exception e) { + logger.error("ORS Error" + e); } - return null ; + return null; } @@ -603,23 +656,28 @@ public List getOrdDistrubtion(GetBenRequestHandler r List entities = orsDistributionRepo.findByUserId(request.getAshaId()); List orsDistributionResponseDTOSList = new ArrayList<>(); - for(OrsDistribution orsDistribution: entities){ + for (OrsDistribution orsDistribution : entities) { OrsDistributionResponseDTO orsDistributionResponseDTO = new OrsDistributionResponseDTO(); OrsDistributionResponseListDTO orsDistributionResponseListDTO = new OrsDistributionResponseListDTO(); orsDistributionResponseDTO.setId(orsDistribution.getId()); orsDistributionResponseDTO.setBeneficiaryId(orsDistribution.getBeneficiaryId()); orsDistributionResponseDTO.setHouseHoldId(orsDistribution.getHouseholdId()); - orsDistributionResponseListDTO.setNum_ors_packets(orsDistribution.getNumOrsPackets().toString()); - orsDistributionResponseListDTO.setNum_under5_children(orsDistribution.getChildCount().toString()); + if (orsDistribution.getNumOrsPackets() != null) { + orsDistributionResponseListDTO.setNum_ors_packets(orsDistribution.getNumOrsPackets().toString()); + + } + if (orsDistribution.getChildCount() != null) { + orsDistributionResponseListDTO.setNum_under5_children(orsDistribution.getChildCount().toString()); + + } orsDistributionResponseDTO.setFields(orsDistributionResponseListDTO); orsDistributionResponseDTO.setVisitDate(parseDate(orsDistribution.getVisitDate().toString()).toString()); orsDistributionResponseDTOSList.add(orsDistributionResponseDTO); - } - return orsDistributionResponseDTOSList; + return orsDistributionResponseDTOSList; } @@ -633,30 +691,76 @@ private LocalDate parseDate(String dateStr) { for (DateTimeFormatter f : formatters) { try { return LocalDate.parse(dateStr, f); - } catch (Exception ignored) {} + } catch (Exception ignored) { + } } throw new DateTimeParseException("Invalid date format: " + dateStr, dateStr, 0); } + @Override - public List saveAllIfa(List dtoList) { - return dtoList.stream() - .map(this::mapToEntity) + public List saveAllIfa(List dtoList, Integer userId) { + List savedList = dtoList.stream() + .map(dto -> mapToEntity(dto, userId)) .map(ifaDistributionRepository::save) .toList(); - } + // incentive generate + savedList.forEach(data -> { + Timestamp visitTimestamp = + Timestamp.valueOf(data.getIfaProvisionDate().atStartOfDay()); + + if(data!=null){ + incentiveLogicService.incentiveForGiveingIFA( + data.getBeneficiaryId(), + visitTimestamp, + visitTimestamp, + data.getUserId() + ); + } + + }); + + return savedList; + } @Override public List getByBeneficiaryId(GetBenRequestHandler requestHandler) { - return ifaDistributionRepository.findByUserId(requestHandler.getAshaId()).stream() - .map(this::mapToDTO) + + return ifaDistributionRepository.findByUserId(requestHandler.getAshaId()) + .stream() + .map(data -> { + + try { + if ("10.0".equals(data.getIfaBottleCount())) { + + Timestamp visitTimestamp = + Timestamp.valueOf(data.getIfaProvisionDate().atStartOfDay()); + + incentiveLogicService.incentiveForGiveingIFA( + data.getBeneficiaryId(), + visitTimestamp, + visitTimestamp, + data.getUserId() + ); + } + } catch (Exception e) { + logger.error( + "Error while processing IFA incentive for beneficiaryId: {}", + data.getBeneficiaryId(), + e + ); + } + + return mapToDTO(data); + }) .toList(); } + // 🔁 Entity → DTO (date formatted as dd-MM-yyyy) private IfaDistributionDTO mapToDTO(IfaDistribution entity) { - final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("dd-MM-yyyy"); + final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("dd-MM-yyyy"); IfaDistributionDTO dto = new IfaDistributionDTO(); @@ -671,14 +775,16 @@ private IfaDistributionDTO mapToDTO(IfaDistribution entity) { fields.setIfa_provision_date(entity.getIfaProvisionDate().format(FORMATTER)); } fields.setMcp_card_upload(entity.getMcpCardUpload()); - fields.setIfa_bottle_count(Double.valueOf(entity.getIfaBottleCount())); + if(entity.getIfaBottleCount()!=null){ + fields.setIfa_bottle_count(Double.valueOf(entity.getIfaBottleCount())); + } dto.setFields(fields); return dto; } // 🔄 Helper method to convert DTO → Entity - private IfaDistribution mapToEntity(IfaDistributionDTO dto) { + private IfaDistribution mapToEntity(IfaDistributionDTO dto, Integer userId) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy"); IfaDistribution entity = new IfaDistribution(); @@ -687,7 +793,7 @@ private IfaDistribution mapToEntity(IfaDistributionDTO dto) { entity.setHouseHoldId(dto.getHouseHoldId()); entity.setFormId(dto.getFormId()); entity.setVisitDate(dto.getVisitDate()); - entity.setUserId(userRepo.getUserIdByName(jwtUtil.getUserNameFromStorage())); + entity.setUserId(userId); if (dto.getFields() != null) { if (dto.getFields().getIfa_provision_date() != null) { @@ -700,27 +806,35 @@ private IfaDistribution mapToEntity(IfaDistributionDTO dto) { entity.setIfaProvisionDate(null); } } - entity.setIfaBottleCount(dto.getFields().getIfa_bottle_count().toString()); + if(dto.getFields().getIfa_bottle_count()!=null){ + entity.setIfaBottleCount(dto.getFields().getIfa_bottle_count().toString()); + + } entity.setMcpCardUpload(dto.getFields().getMcp_card_upload()); } return entity; } - private void checkAndAddSamVisitNRCReferalIncentive(List samVisits){ + private void checkAndAddSamVisitNRCReferalIncentive(List samVisits, Integer userId) { samVisits.forEach(samVisit -> { IncentiveActivity samreferralnrcActivityAm = incentivesRepo.findIncentiveMasterByNameAndGroup("SAM_REFERRAL_NRC", GroupName.CHILD_HEALTH.getDisplayName()); - IncentiveActivity samreferralnrcActivityCH = incentivesRepo.findIncentiveMasterByNameAndGroup("SAM_REFERRAL_NRC", GroupName.ACTIVITY.getDisplayName()); - if(samreferralnrcActivityAm!=null){ - if(samVisit.getIsChildReferredNrc().equals("Yes")){ - createIncentiveRecordforSamReferalToNrc(samVisit,samVisit.getBeneficiaryId(),samreferralnrcActivityAm,jwtUtil.getUserNameFromStorage()); + IncentiveActivity samreferralnrcActivityCH = incentivesRepo.findIncentiveMasterByNameAndGroup("SAM_REFERRAL_NRC", GroupName.ACTIVITY.getDisplayName()); + if (userService.getUserDetail(userId).getStateId().equals(StateCode.AM.getStateCode())) { + + if (samreferralnrcActivityAm != null) { + if (samVisit.getIsChildReferredNrc().equals("Yes")) { + createIncentiveRecordforSamReferalToNrc(samVisit, samVisit.getBeneficiaryId(), samreferralnrcActivityAm, samVisit.getCreatedBy()); + } } } - if(samreferralnrcActivityCH!=null){ - if(samVisit.getIsChildReferredNrc().equals("Yes")){ - createIncentiveRecordforSamReferalToNrc(samVisit,samVisit.getBeneficiaryId(),samreferralnrcActivityCH,jwtUtil.getUserNameFromStorage()); + if (userService.getUserDetail(userId).getStateId().equals(StateCode.CG.getStateCode())) { + if (samreferralnrcActivityCH != null) { + if (samVisit.getIsChildReferredNrc().equals("Yes")) { + createIncentiveRecordforSamReferalToNrc(samVisit, samVisit.getBeneficiaryId(), samreferralnrcActivityCH, samVisit.getCreatedBy()); + } } } @@ -728,20 +842,14 @@ private void checkAndAddSamVisitNRCReferalIncentive(List samVisits){ }); } - private void checkAndAddOrdDistributionIncentive(List orsDistributionList){ + + private void checkAndAddOrdDistributionIncentive(List orsDistributionList) { orsDistributionList.forEach(orsDistribution -> { IncentiveActivity orsPacketActivityAM = incentivesRepo.findIncentiveMasterByNameAndGroup("ORS_DISTRIBUTION", GroupName.CHILD_HEALTH.getDisplayName()); - IncentiveActivity orsPacketActivityCH = incentivesRepo.findIncentiveMasterByNameAndGroup("ORS_DISTRIBUTION", GroupName.ACTIVITY.getDisplayName()); - if(orsPacketActivityAM!=null){ - if(orsDistribution.getNumOrsPackets()!=null){ - createIncentiveRecordforOrsDistribution(orsDistribution,orsDistribution.getBeneficiaryId(),orsPacketActivityAM,userRepo.getUserNamedByUserId(orsDistribution.getUserId()),false); - } - } - - if(orsPacketActivityCH!=null){ - if(orsDistribution.getNumOrsPackets()!=null){ - createIncentiveRecordforOrsDistribution(orsDistribution,orsDistribution.getBeneficiaryId(),orsPacketActivityCH,userRepo.getUserNamedByUserId(orsDistribution.getUserId()),true); + if (orsPacketActivityAM != null) { + if (orsDistribution.getNumOrsPackets() != null) { + createIncentiveRecordforOrsDistribution(orsDistribution, orsDistribution.getBeneficiaryId(), orsPacketActivityAM, userRepo.getUserNamedByUserId(orsDistribution.getUserId()), false); } } @@ -751,74 +859,89 @@ private void checkAndAddOrdDistributionIncentive(List orsDistr } private void checkAndAddHbyncIncentives(List hbycList) { - IncentiveActivity hbync15MonethVisitActivityCH = - incentivesRepo.findIncentiveMasterByNameAndGroup("HBYC_QUARTERLY_VISITS", GroupName.ACTIVITY.getDisplayName()); - IncentiveActivity hbyncVisitActivity = - incentivesRepo.findIncentiveMasterByNameAndGroup("HBYC_QUARTERLY_VISITS", GroupName.CHILD_HEALTH.getDisplayName()); + if(!hbycList.isEmpty()){ + Integer userId = hbycList.get(0).getUserId(); + Integer stateId = userService.getUserDetail(userId).getStateId(); + IncentiveActivity hbync15MonethVisitActivityCH = + incentivesRepo.findIncentiveMasterByNameAndGroup("HBYC_QUARTERLY_VISITS", GroupName.ACTIVITY.getDisplayName()); + IncentiveActivity hbyncVisitActivity = + incentivesRepo.findIncentiveMasterByNameAndGroup("HBYC_QUARTERLY_VISITS", GroupName.CHILD_HEALTH.getDisplayName()); - IncentiveActivity hbyncOrsPacketActivityCH = - incentivesRepo.findIncentiveMasterByNameAndGroup("ORS_DISTRIBUTION", GroupName.ACTIVITY.getDisplayName()); - hbycList.forEach(hbyc -> { - Set eligibleHbycVisits = Set.of("3 Months", "6 Months", "9 Months", "12 Months", "15 Months"); + IncentiveActivity hbyncLactatingIronPacketActivityCH = + incentivesRepo.findIncentiveMasterByNameAndGroup("LACTATING_MOTHERS_HOME_VISIT", GroupName.ACTIVITY.getDisplayName()); - if (hbyncVisitActivity != null && eligibleHbycVisits.contains(hbyc.getVisit_day())) { - createIncentiveRecordforHbyncVisit(hbyc, hbyc.getBeneficiaryId(), hbyncVisitActivity, hbyc.getCreated_by()); - } - - if (hbync15MonethVisitActivityCH != null && eligibleHbycVisits.contains(hbyc.getVisit_day())) { - createIncentiveRecordforHbyncVisit(hbyc, hbyc.getBeneficiaryId(), hbync15MonethVisitActivityCH, hbyc.getCreated_by()); - } + hbycList.forEach(hbyc -> { + Set eligibleHbycVisits = Set.of("3 Months", "6 Months", "9 Months", "12 Months", "15 Months"); + if(stateId.equals(StateCode.AM.getStateCode())){ + if (hbyncVisitActivity != null && eligibleHbycVisits.contains(hbyc.getVisit_day())) { + createIncentiveRecordforHbyncVisit(hbyc, hbyc.getBeneficiaryId(), hbyncVisitActivity, hbyc.getCreated_by()); + } + } + if(stateId.equals(StateCode.CG.getStateCode())){ + if (hbync15MonethVisitActivityCH != null && eligibleHbycVisits.contains(hbyc.getVisit_day())) { + createIncentiveRecordforHbyncVisit(hbyc, hbyc.getBeneficiaryId(), hbync15MonethVisitActivityCH, hbyc.getCreated_by()); + } + if(hbyc.getIfa_given() && eligibleHbycVisits.contains(hbyc.getVisit_day())){ + if(hbyncLactatingIronPacketActivityCH!=null){ + createIncentiveRecordforHbyncVisit(hbyc, hbyc.getBeneficiaryId(), hbyncLactatingIronPacketActivityCH, hbyc.getCreated_by()); + } + } + } - if (hbyncOrsPacketActivityCH != null) { - if (hbyc.getOrs_given()) { - createIncentiveRecordforHbyncOrsDistribution(hbyc, hbyc.getBeneficiaryId(), hbyncOrsPacketActivityCH, hbyc.getCreated_by()); - } - } + }); + } - }); } - private void checkAndAddHbncIncentives(List hbncVisits) { + private void checkAndAddHbncIncentives(List hbncVisits, Integer userId) { + Integer stateCode = userService.getUserDetail(userId).getStateId(); hbncVisits.forEach(hbncVisit -> { - boolean isVisitDone = List.of("1st Day", "3rd Day", "7th Day", "42nd Day") - .stream() - .allMatch(hbncVisits::contains); - - GroupName.setIsCh(false); Long benId = hbncVisit.getBeneficiaryId(); - if (hbncVisit.getVisit_day().equals("42nd Day")) { - IncentiveActivity visitActivityAM = incentivesRepo.findIncentiveMasterByNameAndGroup("HBNC_0_42_DAYS", GroupName.CHILD_HEALTH.getDisplayName()); - IncentiveActivity visitActivityCH = incentivesRepo.findIncentiveMasterByNameAndGroup("HBNC_0_42_DAYS", GroupName.ACTIVITY.getDisplayName()); + if (stateCode.equals(StateCode.AM.getStateCode())) { + if (hbncVisit.getVisit_day().equals("42nd Day")) { + IncentiveActivity visitActivityAM = incentivesRepo.findIncentiveMasterByNameAndGroup("HBNC_0_42_DAYS", GroupName.CHILD_HEALTH.getDisplayName()); - createIncentiveRecordforHbncVisit(hbncVisit, benId, visitActivityAM, "HBNC_0_42_DAYS"); - createIncentiveRecordforHbncVisit(hbncVisit, benId, visitActivityCH, "HBNC_0_42_DAYS_CH"); + createIncentiveRecordforHbncVisit(hbncVisit, benId, visitActivityAM, "HBNC_0_42_DAYS"); + + } - } - logger.info("getDischarged_from_sncu" + hbncVisit.getDischarged_from_sncu()); - if (hbncVisit.getVisit_day().equals("42nd Day") && hbncVisit.getDischarged_from_sncu() && hbncVisit.getBaby_weight() <=2.5) { - IncentiveActivity babyDisChargeSNCUAActivity = - incentivesRepo.findIncentiveMasterByNameAndGroup("SNCU_LBW_FOLLOWUP", GroupName.CHILD_HEALTH.getDisplayName()); - createIncentiveRecordforHbncVisit(hbncVisit, benId, babyDisChargeSNCUAActivity, "SNCU_LBW_FOLLOWUP"); + logger.info("getDischarged_from_sncu" + hbncVisit.getDischarged_from_sncu()); + if (hbncVisit.getVisit_day().equals("42nd Day") && hbncVisit.getDischarged_from_sncu() && hbncVisit.getBaby_weight() <= 2.5) { + IncentiveActivity babyDisChargeSNCUAActivity = + incentivesRepo.findIncentiveMasterByNameAndGroup("SNCU_LBW_FOLLOWUP", GroupName.CHILD_HEALTH.getDisplayName()); + + createIncentiveRecordforHbncVisit(hbncVisit, benId, babyDisChargeSNCUAActivity, "SNCU_LBW_FOLLOWUP"); + + } + logger.info("getIs_baby_alive" + hbncVisit.getIs_baby_alive()); + if (!hbncVisit.getIs_baby_alive()) { + IncentiveActivity isChildDeathActivity = + incentivesRepo.findIncentiveMasterByNameAndGroup("CHILD_DEATH_REPORTING", GroupName.CHILD_HEALTH.getDisplayName()); + + createIncentiveRecordforHbncVisit(hbncVisit, benId, isChildDeathActivity, "CHILD_DEATH_REPORTING"); + } } - logger.info("getIs_baby_alive" + hbncVisit.getIs_baby_alive()); - if (!hbncVisit.getIs_baby_alive()) { - IncentiveActivity isChildDeathActivity = - incentivesRepo.findIncentiveMasterByNameAndGroup("CHILD_DEATH_REPORTING", GroupName.CHILD_HEALTH.getDisplayName()); - createIncentiveRecordforHbncVisit(hbncVisit, benId, isChildDeathActivity, "CHILD_DEATH_REPORTING"); + if (stateCode.equals(StateCode.CG.getStateCode())) { + if (hbncVisit.getVisit_day().equals("42nd Day")) { + IncentiveActivity visitActivityCH = incentivesRepo.findIncentiveMasterByNameAndGroup("HBNC_0_42_DAYS", GroupName.ACTIVITY.getDisplayName()); + createIncentiveRecordforHbncVisit(hbncVisit, benId, visitActivityCH, "HBNC_0_42_DAYS"); + + + } } @@ -829,59 +952,184 @@ private void checkAndAddHbncIncentives(List hbncVisits) { private void checkAndAddIncentives(List vaccinationList) { + String createdBy = vaccinationList.get(0).getCreatedBy(); - + Integer userId = userRepo.getUserIdByName(createdBy); + Integer stateId = userService.getUserDetail(userId).getStateId(); vaccinationList.forEach(vaccination -> { - Long benId = beneficiaryRepo.getBenIdFromRegID(vaccination.getBeneficiaryRegId()).longValue(); - Integer userId = userRepo.getUserIdByName(vaccination.getCreatedBy()); - Integer immunizationServiceId = getImmunizationServiceIdForVaccine(vaccination.getVaccineId().shortValue()); - if (immunizationServiceId < 6) { - IncentiveActivity immunizationActivityAM = - incentivesRepo.findIncentiveMasterByNameAndGroup("FULL_IMMUNIZATION_0_1", GroupName.IMMUNIZATION.getDisplayName()); - IncentiveActivity immunizationActivityCH = - incentivesRepo.findIncentiveMasterByNameAndGroup("FULL_IMMUNIZATION_0_1", GroupName.ACTIVITY.getDisplayName()); - - - if (immunizationActivityAM != null && childVaccinationRepo.getFirstYearVaccineCountForBenId(vaccination.getBeneficiaryRegId()) - .equals(childVaccinationRepo.getFirstYearVaccineCount())) { - createIncentiveRecord(vaccination, benId, userId, immunizationActivityAM); + Long benId= null; + if(vaccination.getBeneficiaryRegId()!=null){ + BigInteger benIdObj = beneficiaryRepo.getBenIdFromRegID(vaccination.getBeneficiaryRegId()); + if (benIdObj != null) { + benId = benIdObj.longValue(); } - if (immunizationActivityCH != null && childVaccinationRepo.getFirstYearVaccineCountForBenId(vaccination.getBeneficiaryRegId()) - .equals(childVaccinationRepo.getFirstYearVaccineCount())) { - createIncentiveRecord(vaccination, benId, userId, immunizationActivityCH); + Integer immunizationServiceId = getImmunizationServiceIdForVaccine(vaccination.getVaccineId().shortValue()); + if (immunizationServiceId < 6) { + + Integer completedVaccines = + childVaccinationRepo.getEligibleFirstYearVaccines( + vaccination.getBeneficiaryRegId()); + + + logger.info( + "FULL_IMMUNIZATION_0_1 :: BeneficiaryRegId={}, Vaccine={}, VaccineId={}, CompletedVaccines={}, StateId={}", + vaccination.getBeneficiaryRegId(), + vaccination.getVaccineName(), + vaccination.getVaccineId(), + completedVaccines, + stateId + ); + + IncentiveActivity immunizationActivityAM = + incentivesRepo.findIncentiveMasterByNameAndGroup( + "FULL_IMMUNIZATION_0_1", + GroupName.IMMUNIZATION.getDisplayName()); + + IncentiveActivity immunizationActivityCH = + incentivesRepo.findIncentiveMasterByNameAndGroup( + "FULL_IMMUNIZATION_0_1", + GroupName.ACTIVITY.getDisplayName()); + +// Required vaccines = 8 + if (completedVaccines != null && completedVaccines == 8) { + + logger.info( + "Beneficiary {} completed all required first year vaccines.", + vaccination.getBeneficiaryRegId()); + + if (stateId.equals(StateCode.AM.getStateCode()) + && immunizationActivityAM != null) { + + logger.info( + "Creating Assam FULL_IMMUNIZATION_0_1 incentive for BeneficiaryRegId={}", + vaccination.getBeneficiaryRegId()); + + createIncentiveRecord( + vaccination, + benId, + userId, + immunizationActivityAM); + } + + if (stateId.equals(StateCode.CG.getStateCode()) + && immunizationActivityCH != null) { + + logger.info( + "Creating Chhattisgarh FULL_IMMUNIZATION_0_1 incentive for BeneficiaryRegId={}", + vaccination.getBeneficiaryRegId()); + + createIncentiveRecord( + vaccination, + benId, + userId, + immunizationActivityCH); + } + + } else { + + logger.info( + "BeneficiaryRegId={} not eligible for FULL_IMMUNIZATION_0_1. CompletedVaccines={}/8", + vaccination.getBeneficiaryRegId(), + completedVaccines + ); + } + + } else if (immunizationServiceId == 7) { + + IncentiveActivity immunizationActivity2AM = + incentivesRepo.findIncentiveMasterByNameAndGroup( + "COMPLETE_IMMUNIZATION_1_2", + GroupName.IMMUNIZATION.getDisplayName()); + + IncentiveActivity immunizationActivity2CH = + incentivesRepo.findIncentiveMasterByNameAndGroup( + "COMPLETE_IMMUNIZATION_1_2", + GroupName.ACTIVITY.getDisplayName()); + + Integer completedVaccines = + childVaccinationRepo.getEligibleSecondYearVaccines( + vaccination.getBeneficiaryRegId()); + + logger.info( + "COMPLETE_IMMUNIZATION_1_2 :: BeneficiaryRegId={}, Vaccine={}, VaccineId={}, CompletedVaccines={}", + vaccination.getBeneficiaryRegId(), + vaccination.getVaccineName(), + vaccination.getVaccineId(), + completedVaccines + ); + + // Required vaccines count = 3 + if (completedVaccines != null && completedVaccines == 3) { + + logger.info( + "Beneficiary {} completed all required second year vaccines.", + vaccination.getBeneficiaryRegId()); + if(userService.getUserDetail(userId).getStateId().equals(StateCode.AM.getStateCode())) { + + if (immunizationActivity2AM != null) { + + logger.info( + "Creating IMMUNIZATION incentive for BeneficiaryRegId={}", + vaccination.getBeneficiaryRegId()); + + createIncentiveRecord( + vaccination, + benId, + userId, + immunizationActivity2AM); + } + } + if(userService.getUserDetail(userId).getStateId().equals(StateCode.CG.getStateCode())) { + + if (immunizationActivity2CH != null) { + + logger.info( + "Creating ACTIVITY incentive for BeneficiaryRegId={}", + vaccination.getBeneficiaryRegId()); + + createIncentiveRecord( + vaccination, + benId, + userId, + immunizationActivity2CH); + } + } + + } else { + + logger.info( + "BeneficiaryRegId={} not eligible. CompletedVaccines={}/3", + vaccination.getBeneficiaryRegId(), + completedVaccines); + } } - } else if (immunizationServiceId == 7) { - IncentiveActivity immunizationActivity2AM = - incentivesRepo.findIncentiveMasterByNameAndGroup("COMPLETE_IMMUNIZATION_1_2", GroupName.IMMUNIZATION.getDisplayName()); - IncentiveActivity immunizationActivity2CH = - incentivesRepo.findIncentiveMasterByNameAndGroup("COMPLETE_IMMUNIZATION_1_2", GroupName.ACTIVITY.getDisplayName()); - if (immunizationActivity2AM != null && childVaccinationRepo.getSecondYearVaccineCountForBenId(vaccination.getBeneficiaryRegId()) - .equals(childVaccinationRepo.getSecondYearVaccineCount())) { - createIncentiveRecord(vaccination, benId, userId, immunizationActivity2AM); + IncentiveActivity immunizationActivity5AM = + incentivesRepo.findIncentiveMasterByNameAndGroup("DPT_IMMUNIZATION_5_YEARS", GroupName.IMMUNIZATION.getDisplayName()); + if(userService.getUserDetail(userId).getStateId().equals(StateCode.AM.getStateCode())) { + + if (immunizationActivity5AM != null && childVaccinationRepo.checkDptVaccinatedUser(vaccination.getBeneficiaryRegId()) == 1) { + createIncentiveRecord(vaccination, benId, userId, immunizationActivity5AM); + } } - if (immunizationActivity2CH != null && childVaccinationRepo.getSecondYearVaccineCountForBenId(vaccination.getBeneficiaryRegId()) - .equals(childVaccinationRepo.getSecondYearVaccineCount())) { - createIncentiveRecord(vaccination, benId, userId, immunizationActivity2CH); + + IncentiveActivity immunizationActivity5CH = + incentivesRepo.findIncentiveMasterByNameAndGroup("DPT_IMMUNIZATION_5_YEARS", GroupName.ACTIVITY.getDisplayName()); + if(userService.getUserDetail(userId).getStateId().equals(StateCode.CG.getStateCode())) { + + if (immunizationActivity5CH != null && childVaccinationRepo.checkDptVaccinatedUser(vaccination.getBeneficiaryRegId()) == 1) { + createIncentiveRecord(vaccination, benId, userId, immunizationActivity5CH); + } } - } - IncentiveActivity immunizationActivity5AM = - incentivesRepo.findIncentiveMasterByNameAndGroup("DPT_IMMUNIZATION_5_YEARS", GroupName.IMMUNIZATION.getDisplayName()); - if (immunizationActivity5AM != null && childVaccinationRepo.checkDptVaccinatedUser(vaccination.getBeneficiaryRegId()) == 1) { - createIncentiveRecord(vaccination, benId, userId, immunizationActivity5AM); - } - IncentiveActivity immunizationActivity5CH = - incentivesRepo.findIncentiveMasterByNameAndGroup("DPT_IMMUNIZATION_5_YEARS", GroupName.ACTIVITY.getDisplayName()); - if (immunizationActivity5CH != null && childVaccinationRepo.checkDptVaccinatedUser(vaccination.getBeneficiaryRegId()) == 1) { - createIncentiveRecord(vaccination, benId, userId, immunizationActivity5CH); } + }); } private void createIncentiveRecord(ChildVaccination vaccination, Long benId, Integer userId, IncentiveActivity immunizationActivity) { IncentiveActivityRecord record = recordRepo - .findRecordByActivityIdCreatedDateBenId(immunizationActivity.getId(), vaccination.getCreatedDate(), benId); + .findRecordByActivityIdCreatedDateBenId(immunizationActivity.getId(), vaccination.getCreatedDate(), benId,userId); if (record == null) { record = new IncentiveActivityRecord(); @@ -903,7 +1151,7 @@ private void createIncentiveRecordforHbncVisit(HbncVisit hbncVisit, Long benId, logger.info("RecordIncentive" + activityName); IncentiveActivityRecord record = recordRepo - .findRecordByActivityIdCreatedDateBenId(immunizationActivity.getId(), hbncVisit.getCreatedDate(), benId); + .findRecordByActivityIdCreatedDateBenId(immunizationActivity.getId(), hbncVisit.getCreatedDate(), benId,hbncVisit.getAshaId()); if (record == null) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy"); @@ -928,37 +1176,60 @@ record = new IncentiveActivityRecord(); } } - private void createIncentiveRecordforHbyncVisit(HbycChildVisit data, Long benId, IncentiveActivity immunizationActivity, String createdBy) { - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy"); + private void updatePendingActivity(Integer userId, Long recordId, Long activityId, Long mIncentiveId) { + IncentivePendingActivity incentivePendingActivity = new IncentivePendingActivity(); + incentivePendingActivity.setActivityId(activityId); + incentivePendingActivity.setRecordId(recordId); + incentivePendingActivity.setUserId(userId); + incentivePendingActivity.setMincentiveId(mIncentiveId); + if (incentivePendingActivity != null) { + incentivePendingActivityRepository.save(incentivePendingActivity); + } - // Convert to LocalDate - LocalDate localDate = LocalDate.parse(data.getVisit_date(), formatter); + } - // Convert LocalDate to Timestamp (00:00:00 by default) - Timestamp visitDate = Timestamp.valueOf(localDate.atStartOfDay()); - IncentiveActivityRecord record = recordRepo - .findRecordByActivityIdCreatedDateBenId(immunizationActivity.getId(), visitDate, benId); + public HbncVisit updateHbncFromFileUpload(MultipartFile[] dischargeSncuImage, Long incentiveRecordId, Long id) throws JsonProcessingException { + HbncVisit existingHbncVisit = hbncVisitRepo.findById(id) + .orElseThrow(() -> new EntityNotFoundException("Meeting not found: " + id)); - if (record == null) { + // Images - only if provided + if (dischargeSncuImage != null && dischargeSncuImage.length > 0) { + List base64Images = Arrays.stream(dischargeSncuImage) + .filter(file -> file != null && !file.isEmpty()) + .map(this::convertToBase64) + .collect(Collectors.toList()); + existingHbncVisit.setDischarge_summary_upload(mapper.writeValueAsString(base64Images)); + } - record = new IncentiveActivityRecord(); - record.setActivityId(immunizationActivity.getId()); - record.setCreatedDate(visitDate); - record.setCreatedBy(createdBy); - record.setStartDate(visitDate); - record.setEndDate(visitDate); - record.setUpdatedDate(visitDate); - record.setUpdatedBy(createdBy); - record.setBenId(benId); - record.setAshaId(beneficiaryRepo.getUserIDByUserName(createdBy)); - record.setAmount(Long.valueOf(immunizationActivity.getRate())); + if (existingHbncVisit.getDischarge_summary_upload() != null) { + updateIncentive(incentiveRecordId); + + } + return hbncVisitRepo.save(existingHbncVisit); + } + + private void updateIncentive(Long id) { + + Optional optionalRecord = recordRepo.findById(id); + + if (optionalRecord.isPresent()) { + IncentiveActivityRecord record = optionalRecord.get(); + record.setIsEligible(true); recordRepo.save(record); } } - private void createIncentiveRecordforHbyncOrsDistribution(HbycChildVisit data, Long benId, IncentiveActivity immunizationActivity, String createdBy) { + private String convertToBase64(MultipartFile file) { + try { + return Base64.getEncoder().encodeToString(file.getBytes()); + } catch (IOException e) { + throw new RuntimeException("Failed to convert image to Base64: " + file.getOriginalFilename(), e); + } + } + + private void createIncentiveRecordforHbyncVisit(HbycChildVisit data, Long benId, IncentiveActivity immunizationActivity, String createdBy) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy"); // Convert to LocalDate @@ -967,7 +1238,7 @@ private void createIncentiveRecordforHbyncOrsDistribution(HbycChildVisit data, L // Convert LocalDate to Timestamp (00:00:00 by default) Timestamp visitDate = Timestamp.valueOf(localDate.atStartOfDay()); IncentiveActivityRecord record = recordRepo - .findRecordByActivityIdCreatedDateBenId(immunizationActivity.getId(), visitDate, benId); + .findRecordByActivityIdCreatedDateBenId(immunizationActivity.getId(), visitDate, benId,data.getUserId()); if (record == null) { @@ -981,46 +1252,47 @@ record = new IncentiveActivityRecord(); record.setUpdatedDate(visitDate); record.setUpdatedBy(createdBy); record.setBenId(benId); - record.setAshaId(beneficiaryRepo.getUserIDByUserName(createdBy)); + record.setAshaId(data.getUserId()); record.setAmount(Long.valueOf(immunizationActivity.getRate())); recordRepo.save(record); } } - private void createIncentiveRecordforOrsDistribution(OrsDistribution data, Long benId, IncentiveActivity immunizationActivity, String createdBy,boolean isCH) { - try { - // Convert to LocalDate - Timestamp visitDate = Timestamp.valueOf(data.getVisitDate().atStartOfDay()); - IncentiveActivityRecord record = recordRepo - .findRecordByActivityIdCreatedDateBenId(immunizationActivity.getId(), visitDate, benId); - double packets = Double.parseDouble(data.getNumOrsPackets()); - double rate = immunizationActivity.getRate(); - - if (record == null) { - - record = new IncentiveActivityRecord(); - record.setActivityId(immunizationActivity.getId()); - record.setCreatedDate(visitDate); - record.setCreatedBy(createdBy); - record.setStartDate(visitDate); - record.setEndDate(visitDate); - record.setUpdatedDate(visitDate); - record.setUpdatedBy(createdBy); - record.setBenId(benId); - record.setAshaId(beneficiaryRepo.getUserIDByUserName(createdBy)); - if(isCH){ - record.setAmount((long) rate); - - }else { - record.setAmount((long) (rate * packets)); - - } - recordRepo.save(record); - } - }catch (Exception e){ - logger.error("Exp"+e.getMessage()); - - } + + private void createIncentiveRecordforOrsDistribution(OrsDistribution data, Long benId, IncentiveActivity immunizationActivity, String createdBy, boolean isCH) { + try { + // Convert to LocalDate + Timestamp visitDate = Timestamp.valueOf(data.getVisitDate().atStartOfDay()); + IncentiveActivityRecord record = recordRepo + .findRecordByActivityIdCreatedDateBenId(immunizationActivity.getId(), visitDate, benId,data.getUserId()); + double packets = Double.parseDouble(data.getNumOrsPackets()); + double rate = immunizationActivity.getRate(); + + if (record == null) { + + record = new IncentiveActivityRecord(); + record.setActivityId(immunizationActivity.getId()); + record.setCreatedDate(visitDate); + record.setCreatedBy(createdBy); + record.setStartDate(visitDate); + record.setEndDate(visitDate); + record.setUpdatedDate(visitDate); + record.setUpdatedBy(createdBy); + record.setBenId(0L); + record.setAshaId(data.getUserId()); + if (isCH) { + record.setAmount((long) rate); + + } else { + record.setAmount((long) (rate * packets)); + + } + recordRepo.save(record); + } + } catch (Exception e) { + logger.error("Exp" + e.getMessage()); + + } } @@ -1029,7 +1301,7 @@ private void createIncentiveRecordforSamReferalToNrc(SamVisit data, Long benId, // Convert to LocalDate Timestamp visitDate = Timestamp.valueOf(data.getVisitDate().atStartOfDay()); IncentiveActivityRecord record = recordRepo - .findRecordByActivityIdCreatedDateBenId(incentiveActivity.getId(), visitDate, benId); + .findRecordByActivityIdCreatedDateBenId(incentiveActivity.getId(), visitDate, benId,data.getUserId()); if (record == null) { @@ -1042,12 +1314,14 @@ record = new IncentiveActivityRecord(); record.setUpdatedDate(visitDate); record.setUpdatedBy(createdBy); record.setBenId(benId); - record.setAshaId(beneficiaryRepo.getUserIDByUserName(createdBy)); + record.setAshaId(data.getUserId()); record.setAmount((long) incentiveActivity.getRate()); + record.setIsEligible(true); + record.setIsDefaultActivity(false); recordRepo.save(record); } - }catch (Exception e){ - logger.error("Exp"+e.getMessage()); + } catch (Exception e) { + logger.error("Exp" + e.getMessage()); } diff --git a/src/main/java/com/iemr/flw/service/impl/ChildServiceImpl.java b/src/main/java/com/iemr/flw/service/impl/ChildServiceImpl.java index 0ce841e8..32c6d76a 100644 --- a/src/main/java/com/iemr/flw/service/impl/ChildServiceImpl.java +++ b/src/main/java/com/iemr/flw/service/impl/ChildServiceImpl.java @@ -2,18 +2,24 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import com.iemr.flw.domain.identity.RMNCHBeneficiaryDetailsRmnch; import com.iemr.flw.domain.iemr.ChildRegister; +import com.iemr.flw.domain.iemr.IncentiveActivityRecord; +import com.iemr.flw.domain.iemr.UserServiceRole; import com.iemr.flw.dto.identity.GetBenRequestHandler; import com.iemr.flw.dto.iemr.ChildRegisterDTO; import com.iemr.flw.repo.identity.BeneficiaryRepo; import com.iemr.flw.repo.iemr.ChildRegisterRepo; +import com.iemr.flw.repo.iemr.UserServiceRoleRepo; import com.iemr.flw.service.ChildService; +import com.iemr.flw.service.IncentiveLogicService; import org.modelmapper.ModelMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.sql.Timestamp; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; @@ -28,6 +34,12 @@ public class ChildServiceImpl implements ChildService { @Autowired private BeneficiaryRepo beneficiaryRepo; + @Autowired + private IncentiveLogicService incentiveLogicService; + + @Autowired + private UserServiceRoleRepo userServiceRoleRepo; + public String getByUserId(GetBenRequestHandler dto) { try { String user = beneficiaryRepo.getUserName(dto.getAshaId()); @@ -38,6 +50,7 @@ public String getByUserId(GetBenRequestHandler dto) { // ChildRegisterDTO childDTO = modelMapper.map(childRegister, ChildRegisterDTO.class); // result.add(childDTO); // }); + List result = childRegisterList.stream() .map(childRegister -> modelMapper.map(childRegister, ChildRegisterDTO.class)) .collect(Collectors.toList()); @@ -68,6 +81,68 @@ public String save(List childRegisterDTOs) throws Exception { listToBeSaved.add(childRegister); }); childRepo.saveAll(listToBeSaved); + for (ChildRegister childRegister : listToBeSaved) { + processFirstChildIncentive(childRegister); + } + for (ChildRegister childRegister : listToBeSaved) { + processSecondChildGapIncentive(childRegister); + } return "no of child details saved: " + childRegisterDTOs.size(); } + + public void processFirstChildIncentive(ChildRegister childRegister) { + Long benId = childRegister.getBenId(); + logger.info("Child register {}"+childRegister.getBenId()); + + // First child validation + List childCount = childRepo.findByBenId(benId); + + + if(!childCount.isEmpty()){ + logger.info("Child register {}"+childCount.size()); + + if(childCount.size()==1){ + Integer userId = + userServiceRoleRepo.getUserIdByName(childRegister.getCreatedBy()); + + incentiveLogicService.incentiveForChildBirthGap( + benId, + childRegister.getCreatedDate(), + childRegister.getCreatedDate(), + userId + ); + } + } + + + + } + + private void processSecondChildGapIncentive(ChildRegister currentChild) { + + Long benId = currentChild.getBenId(); + + // Total children count + List childCount = childRepo.findByBenId(benId); + + // Applicable only for second child + + if(!childCount.isEmpty()){ + if(childCount.size()==2){ + Integer userId = + userServiceRoleRepo.getUserIdByName( + currentChild.getCreatedBy()); + + incentiveLogicService.incentiveForSecondChildGap( + benId, + currentChild.getCreatedDate(), + currentChild.getCreatedDate(), + userId + ); + } + } + + + + } } diff --git a/src/main/java/com/iemr/flw/service/impl/CoupleServiceImpl.java b/src/main/java/com/iemr/flw/service/impl/CoupleServiceImpl.java index 95b0557b..e22147af 100644 --- a/src/main/java/com/iemr/flw/service/impl/CoupleServiceImpl.java +++ b/src/main/java/com/iemr/flw/service/impl/CoupleServiceImpl.java @@ -11,14 +11,17 @@ import com.iemr.flw.dto.iemr.EligibleCoupleDTO; import com.iemr.flw.dto.iemr.EligibleCoupleTrackingDTO; import com.iemr.flw.masterEnum.GroupName; +import com.iemr.flw.masterEnum.StateCode; import com.iemr.flw.repo.identity.BeneficiaryRepo; import com.iemr.flw.repo.iemr.*; import com.iemr.flw.service.CoupleService; +import com.iemr.flw.service.UserService; import org.modelmapper.ModelMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; @@ -27,6 +30,7 @@ import java.util.Arrays; import java.util.Base64; import java.util.List; +import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; @Service @@ -46,6 +50,9 @@ public class CoupleServiceImpl implements CoupleService { @Autowired private UserServiceRoleRepo userRepo; + + @Autowired + private UserService userService; @Autowired private IncentiveRecordRepo recordRepo; @@ -54,82 +61,139 @@ public class CoupleServiceImpl implements CoupleService { private BeneficiaryRepo beneficiaryRepo; private final Logger logger = LoggerFactory.getLogger(CoupleServiceImpl.class); + private final ConcurrentHashMap lockMap = new ConcurrentHashMap<>(); + @Override - public String registerEligibleCouple(List eligibleCoupleDTOs, MultipartFile kitPhoto1, MultipartFile kitPhoto2) { + @Transactional + public String registerEligibleCouple(List eligibleCoupleDTOs, + MultipartFile kitPhoto1, + MultipartFile kitPhoto2) { try { + List ecrList = new ArrayList<>(); - List recordList = new ArrayList<>(); - eligibleCoupleDTOs.forEach(it -> { - EligibleCoupleRegister existingECR = -// eligibleCoupleRegisterRepo.findEligibleCoupleRegisterByBenIdAndCreatedDate(it.getBenId(), it.getCreatedDate()); - eligibleCoupleRegisterRepo.findEligibleCoupleRegisterByBenId(it.getBenId()); - if (kitPhoto1 != null) { - String kitPhoto1base64Image = null; - try { - kitPhoto1base64Image = Base64.getEncoder().encodeToString(kitPhoto1.getBytes()); - } catch (IOException e) { - throw new RuntimeException(e); + if(!eligibleCoupleDTOs.isEmpty()){ + for (EligibleCoupleDTO it : eligibleCoupleDTOs) { + Integer userId = userRepo.getUserIdByName(it.getCreatedBy()); + Integer stateId = userService.getUserDetail(userId).getStateId(); + EligibleCoupleRegister existingECR = + eligibleCoupleRegisterRepo.findEligibleCoupleRegisterByBenId(it.getBenId()); + + boolean isNew = false; + + if (existingECR == null) { + existingECR = new EligibleCoupleRegister(); + isNew = true; } - existingECR.setKitPhoto1(String.valueOf(kitPhoto1base64Image)); - } + Long id = existingECR.getId(); + modelMapper.map(it, existingECR); - if (kitPhoto2 != null) { - String kitPhoto2base64Image = null; - try { - kitPhoto2base64Image = Base64.getEncoder().encodeToString(kitPhoto2.getBytes()); - } catch (IOException e) { - throw new RuntimeException(e); - } - existingECR.setKitPhoto2(String.valueOf(kitPhoto2base64Image)); + existingECR.setId(id); - } + // Photo 1 + if (kitPhoto1 != null && !kitPhoto1.isEmpty()) { + existingECR.setKitPhoto1( + Base64.getEncoder().encodeToString(kitPhoto1.getBytes())); + } - if (existingECR != null && null != existingECR.getNumLiveChildren()) { - if (existingECR.getNumLiveChildren() == 0 && it.getNumLiveChildren() >= 1 && it.getMarriageFirstChildGap() != null && it.getMarriageFirstChildGap() >= 2) { - IncentiveActivity activity1 = - incentivesRepo.findIncentiveMasterByNameAndGroup("FP_DELAY_2Y", GroupName.FAMILY_PLANNING.getDisplayName()); - createIncentiveRecord(recordList, it, activity1); - } else if (existingECR.getNumLiveChildren() == 1 && it.getNumLiveChildren() >= 2 && it.getFirstAndSecondChildGap() != null && it.getFirstAndSecondChildGap() == 3) { - IncentiveActivity activity2 = - incentivesRepo.findIncentiveMasterByNameAndGroup("1st_2nd_CHILD_GAP", GroupName.FAMILY_PLANNING.getDisplayName()); - createIncentiveRecord(recordList, it, activity2); + // Photo 2 + if (kitPhoto2 != null && !kitPhoto2.isEmpty()) { + existingECR.setKitPhoto2( + Base64.getEncoder().encodeToString(kitPhoto2.getBytes())); } - Long id = existingECR.getId(); - modelMapper.map(it, existingECR); - existingECR.setId(id); - } else { - existingECR = new EligibleCoupleRegister(); - modelMapper.map(it, existingECR); - existingECR.setId(null); - } - if (existingECR.getIsKitHandedOver() && (!existingECR.getKitPhoto1().isEmpty() || !existingECR.getKitPhoto2().isEmpty())) { - IncentiveActivity handoverKitActivityAM = - incentivesRepo.findIncentiveMasterByNameAndGroup("FP_NP_KIT", GroupName.FAMILY_PLANNING.getDisplayName()); - if (handoverKitActivityAM != null) { - createIncentiveRecord(recordList, it, handoverKitActivityAM); + // Incentive only for new registration + if (isNew) { + + if (it.getMarriageFirstChildGap() != null + && it.getMarriageFirstChildGap() >= 2) { + + IncentiveActivity activity = null; + + if (stateId.equals(StateCode.AM.getStateCode())) { + activity = incentivesRepo.findIncentiveMasterByNameAndGroup( + "FP_DELAY_2Y", + GroupName.FAMILY_PLANNING.getDisplayName()); + + } else if (stateId.equals(StateCode.CG.getStateCode())) { + activity = incentivesRepo.findIncentiveMasterByNameAndGroup( + "FP_DELAY_2Y", + GroupName.ACTIVITY.getDisplayName()); + } + + if (activity != null) { + createIncentiveRecord(existingECR, activity); + } + } + + if (it.getFirstAndSecondChildGap() != null + && it.getFirstAndSecondChildGap() >= 3) { + + if(stateId.equals(StateCode.AM.getStateCode())){ + IncentiveActivity activity = + incentivesRepo.findIncentiveMasterByNameAndGroup( + "1st_2nd_CHILD_GAP", + GroupName.FAMILY_PLANNING.getDisplayName()); + + createIncentiveRecord(existingECR, activity); + + } + if(stateId.equals(StateCode.CG.getStateCode())){ + IncentiveActivity activity = + incentivesRepo.findIncentiveMasterByNameAndGroup( + "1st_2nd_CHILD_GAP", + GroupName.ACTIVITY.getDisplayName()); + + createIncentiveRecord(existingECR, activity); + } + } } + // Kit Incentive + if (Boolean.TRUE.equals(existingECR.getIsKitHandedOver()) + && ((existingECR.getKitPhoto1() != null && !existingECR.getKitPhoto1().isEmpty()) + || (existingECR.getKitPhoto2() != null && !existingECR.getKitPhoto2().isEmpty()))) { + if(stateId.equals(StateCode.AM.getStateCode())){ + IncentiveActivity activity = + incentivesRepo.findIncentiveMasterByNameAndGroup( + "FP_NP_KIT", + GroupName.FAMILY_PLANNING.getDisplayName()); + + if (activity != null) { + createIncentiveRecord(existingECR, activity); + } + } + if(stateId.equals(StateCode.CG.getStateCode())){ + IncentiveActivity activity = + incentivesRepo.findIncentiveMasterByNameAndGroup( + "FP_NP_KIT", + GroupName.ACTIVITY.getDisplayName()); + + if (activity != null) { + createIncentiveRecord(existingECR, activity); + } + } - IncentiveActivity handoverKitActivityCH = - incentivesRepo.findIncentiveMasterByNameAndGroup("FP_NP_KIT", GroupName.ACTIVITY.getDisplayName()); - if (handoverKitActivityCH != null) { - createIncentiveRecord(recordList, it, handoverKitActivityCH); } + + ecrList.add(existingECR); } - ecrList.add(existingECR); - }); - eligibleCoupleRegisterRepo.saveAll(ecrList); - recordRepo.saveAll(recordList); - return "no of ecr details saved: " + ecrList.size(); + + eligibleCoupleRegisterRepo.saveAll(ecrList); + + } + + + return "No of ECR details saved: " + ecrList.size(); + } catch (Exception e) { - return "error while saving ecr details: " + e.getMessage(); + logger.error("Error while saving Eligible Couple Registration", e); + return "Error while saving ECR details: " + e.getMessage(); } } @@ -137,46 +201,141 @@ public String registerEligibleCouple(List eligibleCoupleDTOs, public String registerEligibleCouple(List eligibleCoupleDTOs) { try { List ecrList = new ArrayList<>(); - List recordList = new ArrayList<>(); eligibleCoupleDTOs.forEach(it -> { EligibleCoupleRegister existingECR = -// eligibleCoupleRegisterRepo.findEligibleCoupleRegisterByBenIdAndCreatedDate(it.getBenId(), it.getCreatedDate()); eligibleCoupleRegisterRepo.findEligibleCoupleRegisterByBenId(it.getBenId()); - - if (existingECR != null && null != existingECR.getNumLiveChildren()) { - if (it.getNumLiveChildren() >= 1 && it.getMarriageFirstChildGap() != null && it.getMarriageFirstChildGap() >= 2) { - IncentiveActivity activity1 = - incentivesRepo.findIncentiveMasterByNameAndGroup("MARRIAGE_1st_CHILD_GAP", GroupName.FAMILY_PLANNING.getDisplayName()) - ; - - IncentiveActivity activityCH = - incentivesRepo.findIncentiveMasterByNameAndGroup("MARRIAGE_1st_CHILD_GAP", GroupName.ACTIVITY.getDisplayName()); - createIncentiveRecord(recordList, it, activity1); - createIncentiveRecord(recordList, it, activityCH); - } else if (it.getNumLiveChildren() >= 2 && it.getMarriageFirstChildGap() != null && it.getMarriageFirstChildGap() >= 3) { - IncentiveActivity activity2 = - incentivesRepo.findIncentiveMasterByNameAndGroup("1st_2nd_CHILD_GAP", GroupName.FAMILY_PLANNING.getDisplayName()); - - IncentiveActivity activityCH = - incentivesRepo.findIncentiveMasterByNameAndGroup("1st_2nd_CHILD_GAP", GroupName.ACTIVITY.getDisplayName()); - createIncentiveRecord(recordList, it, activity2); - createIncentiveRecord(recordList, it, activityCH); - } + if(existingECR!=null){ Long id = existingECR.getId(); modelMapper.map(it, existingECR); existingECR.setId(id); - } else { + }else { existingECR = new EligibleCoupleRegister(); modelMapper.map(it, existingECR); existingECR.setId(null); } + ecrList.add(existingECR); + + }); + + eligibleCoupleRegisterRepo.saveAll(ecrList); + if(!ecrList.isEmpty()){ + Integer userId = userRepo.getUserIdByName(ecrList.get(0).getCreatedBy()); + Integer stateId = userService.getUserDetail(userId).getStateId(); + checkIncentiveForChildGap(stateId,ecrList.get(0).getCreatedBy(),ecrList); + + } + return "no of ecr details saved: " + ecrList.size(); + } catch (Exception e) { + return "error while saving ecr details: " + e.getMessage(); + } + } + + + private void checkIncentiveForChildGap(Integer stateId, String userName,List eligibleCoupleRegisters) { + + logger.info("Checking Child Gap Incentive for user: {}, stateId: {}", userName, stateId); + + + logger.info("Eligible Couple Records Found: {}", eligibleCoupleRegisters.size()); + + if (!eligibleCoupleRegisters.isEmpty()) { + + eligibleCoupleRegisters.forEach(eligibleCoupleRegister -> { + + logger.info( + "Processing EligibleCoupleRegister -> BenId: {}, NumChildren: {}, MarriageFirstChildGap: {}, FirstAndSecondChildGap: {}", + eligibleCoupleRegister.getBenId(), + eligibleCoupleRegister.getNumChildren(), + eligibleCoupleRegister.getMarriageFirstChildGap(), + eligibleCoupleRegister.getFirstAndSecondChildGap()); + + // Marriage -> First Child Gap + if(eligibleCoupleRegister.getFirstAndSecondChildGap()!=null){ + if (eligibleCoupleRegister.getFirstAndSecondChildGap()>=2 ) { + + logger.info("Marriage -> First Child Gap condition matched."); + + if (stateId.equals(StateCode.AM.getStateCode())) { + + logger.info("Fetching incentive for Assam."); + + IncentiveActivity activity1 = + incentivesRepo.findIncentiveMasterByNameAndGroup( + "1st_2nd_CHILD_GAP", + GroupName.FAMILY_PLANNING.getDisplayName()); + + logger.info("Incentive Activity: {}", activity1); + + createIncentiveRecord(eligibleCoupleRegister, activity1); - if (existingECR.getIsKitHandedOver()) { + logger.info("Marriage -> First Child Gap incentive created."); + } + + if (stateId.equals(StateCode.CG.getStateCode())) { + + logger.info("Fetching incentive for Chhattisgarh."); + + IncentiveActivity activityCH = + incentivesRepo.findIncentiveMasterByNameAndGroup( + "1st_2nd_CHILD_GAP", + GroupName.ACTIVITY.getDisplayName()); + + logger.info("Incentive Activity: {}", activityCH); + + createIncentiveRecord(eligibleCoupleRegister, activityCH); + + logger.info("Marriage -> First Child Gap incentive created."); + } + } + } + + + // First -> Second Child Gap + if(eligibleCoupleRegister.getMarriageFirstChildGap()!=null){ + if (eligibleCoupleRegister.getMarriageFirstChildGap()>=3) { + + logger.info("1st -> 2nd Child Gap condition matched."); + + if (stateId.equals(StateCode.AM.getStateCode())) { + + logger.info("Fetching incentive for Assam."); + + IncentiveActivity activity2 = + incentivesRepo.findIncentiveMasterByNameAndGroup( + "FP_DELAY_2Y", + GroupName.FAMILY_PLANNING.getDisplayName()); + + logger.info("Incentive Activity: {}", activity2); + + createIncentiveRecord(eligibleCoupleRegister, activity2); + + logger.info("1st -> 2nd Child Gap incentive created."); + } + + if (stateId.equals(StateCode.CG.getStateCode())) { + + logger.info("Fetching incentive for Chhattisgarh."); + + IncentiveActivity activityCH = + incentivesRepo.findIncentiveMasterByNameAndGroup( + "FP_DELAY_2Y", + GroupName.ACTIVITY.getDisplayName()); + + logger.info("Incentive Activity: {}", activityCH); + + createIncentiveRecord(eligibleCoupleRegister, activityCH); + + logger.info("1st -> 2nd Child Gap incentive created."); + } + } + } + + if (eligibleCoupleRegister.getIsKitHandedOver()!=null && eligibleCoupleRegister.getIsKitHandedOver()) { IncentiveActivity handoverKitActivityAM = incentivesRepo.findIncentiveMasterByNameAndGroup("FP_NP_KIT", GroupName.FAMILY_PLANNING.getDisplayName()); if (handoverKitActivityAM != null) { - createIncentiveRecord(recordList, it, handoverKitActivityAM); + createIncentiveRecord(eligibleCoupleRegister, handoverKitActivityAM); } @@ -184,41 +343,54 @@ public String registerEligibleCouple(List eligibleCoupleDTOs) IncentiveActivity handoverKitActivityCH = incentivesRepo.findIncentiveMasterByNameAndGroup("FP_NP_KIT", GroupName.ACTIVITY.getDisplayName()); if (handoverKitActivityCH != null) { - createIncentiveRecord(recordList, it, handoverKitActivityCH); + createIncentiveRecord(eligibleCoupleRegister, handoverKitActivityCH); } } - ecrList.add(existingECR); + + + + }); - eligibleCoupleRegisterRepo.saveAll(ecrList); - recordRepo.saveAll(recordList); - return "no of ecr details saved: " + ecrList.size(); - } catch (Exception e) { - return "error while saving ecr details: " + e.getMessage(); + + } else { + logger.info("No Eligible Couple Register records found for user: {}", userName); } + + logger.info("Completed Child Gap Incentive check for user: {}", userName); } - private void createIncentiveRecord(List recordList, EligibleCoupleDTO eligibleCoupleDTO, IncentiveActivity activity) { - if (activity != null) { - IncentiveActivityRecord record = recordRepo - .findRecordByActivityIdCreatedDateBenId(activity.getId(), eligibleCoupleDTO.getCreatedDate(), eligibleCoupleDTO.getBenId()); - Integer userId = userRepo.getUserIdByName(eligibleCoupleDTO.getCreatedBy()); - if (record == null) { - record = new IncentiveActivityRecord(); - record.setActivityId(activity.getId()); - record.setCreatedDate(eligibleCoupleDTO.getCreatedDate()); - record.setCreatedBy(eligibleCoupleDTO.getCreatedBy()); - record.setStartDate(eligibleCoupleDTO.getCreatedDate()); - record.setEndDate(eligibleCoupleDTO.getCreatedDate()); - record.setUpdatedDate(eligibleCoupleDTO.getCreatedDate()); - record.setUpdatedBy(eligibleCoupleDTO.getCreatedBy()); - record.setBenId(eligibleCoupleDTO.getBenId()); - record.setAshaId(userId); - record.setAmount(Long.valueOf(activity.getRate())); - recordList.add(record); + private void createIncentiveRecord(EligibleCoupleRegister eligibleCoupleDTO, IncentiveActivity activity) { + String lockKey = activity.getId() + "_" + eligibleCoupleDTO.getBenId() + "_" + eligibleCoupleDTO.getCreatedDate(); + + Object lock = lockMap.computeIfAbsent(lockKey, k -> new Object()); + synchronized (lock){ + if (activity != null) { + Integer userId = userRepo.getUserIdByName(eligibleCoupleDTO.getCreatedBy()); + + IncentiveActivityRecord record = recordRepo + .findRecordByActivityIdCreatedDateBenId(activity.getId(), eligibleCoupleDTO.getCreatedDate(), eligibleCoupleDTO.getBenId(),userId); + if (record == null) { + record = new IncentiveActivityRecord(); + record.setActivityId(activity.getId()); + record.setCreatedDate(eligibleCoupleDTO.getCreatedDate()); + record.setCreatedBy(eligibleCoupleDTO.getCreatedBy()); + record.setStartDate(eligibleCoupleDTO.getCreatedDate()); + record.setEndDate(eligibleCoupleDTO.getCreatedDate()); + record.setUpdatedDate(eligibleCoupleDTO.getCreatedDate()); + record.setUpdatedBy(eligibleCoupleDTO.getCreatedBy()); + record.setBenId(eligibleCoupleDTO.getBenId()); + record.setAshaId(userId); + record.setAmount(Long.valueOf(activity.getRate())); + recordRepo.save(record); + } } } + lockMap.remove(lockKey, lock); + + + } @Override @@ -240,7 +412,7 @@ public String registerEligibleCoupleTracking(List eli ect.setId(null); } ectList.add(ect); - checkAndAddAntaraIncentive(recordList, ect); + checkAndAddAntaraIncentive(ect); }); eligibleCoupleTrackingRepo.saveAll(ectList); recordRepo.saveAll(recordList); @@ -250,135 +422,170 @@ public String registerEligibleCoupleTracking(List eli } } - private void checkAndAddAntaraIncentive(List recordList, EligibleCoupleTracking ect) { + private void checkAndAddAntaraIncentive(EligibleCoupleTracking ect) { Integer userId = userRepo.getUserIdByName(ect.getCreatedBy()); logger.info("Antra" + ect.getMethodOfContraception()); logger.info("Antra" + ect.getAntraDose()); + Integer stateId = userService.getUserDetail(userId).getStateId(); if (ect.getMethodOfContraception() != null && ect.getMethodOfContraception().contains("ANTRA Injection")) { - // for CG incentive - IncentiveActivity antaraActivityCH = - incentivesRepo.findIncentiveMasterByNameAndGroup("FP_ANC_MPA1", GroupName.ACTIVITY.getDisplayName()); - if (antaraActivityCH != null) { - String dose = ect.getAntraDose(); + if(stateId.equals(StateCode.CG.getStateCode())){ + // for CG incentive + IncentiveActivity antaraActivityCH = + incentivesRepo.findIncentiveMasterByNameAndGroup("FP_ANC_MPA1", GroupName.ACTIVITY.getDisplayName()); + if (antaraActivityCH != null) { + String dose = ect.getAntraDose(); - List validDoses = Arrays.asList("Dose-1", "Dose-2", "Dose-3", "Dose-4"); + List validDoses = Arrays.asList("Dose-1", "Dose-2", "Dose-3", "Dose-4"); - boolean isDose = validDoses.stream().anyMatch(dose::contains); + boolean isDose = validDoses.stream().anyMatch(dose::contains); - if (isDose) { - addIncenticeRecord(recordList, ect, userId, antaraActivityCH); + if (isDose) { + addIncenticeRecord(ect, userId, antaraActivityCH); + } } } - if (ect.getAntraDose().contains("Dose-1")) { - IncentiveActivity antaraActivity = - incentivesRepo.findIncentiveMasterByNameAndGroup("FP_ANC_MPA1", "FAMILY PLANNING"); - if (antaraActivity != null) { - addIncenticeRecord(recordList, ect, userId, antaraActivity); - } - } else if (ect.getAntraDose().contains("Dose-2")) { - IncentiveActivity antaraActivity2 = - incentivesRepo.findIncentiveMasterByNameAndGroup("FP_ANC_MPA2", "FAMILY PLANNING"); - if (antaraActivity2 != null) { - addIncenticeRecord(recordList, ect, userId, antaraActivity2); - } - } else if (ect.getAntraDose().contains("Dose-3")) { - IncentiveActivity antaraActivity3 = - incentivesRepo.findIncentiveMasterByNameAndGroup("FP_ANC_MPA3", "FAMILY PLANNING"); - if (antaraActivity3 != null) { - addIncenticeRecord(recordList, ect, userId, antaraActivity3); - } - } else if (ect.getAntraDose().contains("Dose-4")) { - IncentiveActivity antaraActivity4 = - incentivesRepo.findIncentiveMasterByNameAndGroup("FP_ANC_MPA4", "FAMILY PLANNING"); - if (antaraActivity4 != null) { - addIncenticeRecord(recordList, ect, userId, antaraActivity4); - } + if(stateId.equals(StateCode.AM.getStateCode())){ + if (ect.getAntraDose().contains("Dose-1")) { + IncentiveActivity antaraActivity = + incentivesRepo.findIncentiveMasterByNameAndGroup("FP_ANC_MPA1", "FAMILY PLANNING"); + if (antaraActivity != null) { + addIncenticeRecord(ect, userId, antaraActivity); + } + } else if (ect.getAntraDose().contains("Dose-2")) { + IncentiveActivity antaraActivity2 = + incentivesRepo.findIncentiveMasterByNameAndGroup("FP_ANC_MPA2", "FAMILY PLANNING"); + if (antaraActivity2 != null) { + addIncenticeRecord(ect, userId, antaraActivity2); + } + } else if (ect.getAntraDose().contains("Dose-3")) { + IncentiveActivity antaraActivity3 = + incentivesRepo.findIncentiveMasterByNameAndGroup("FP_ANC_MPA3", "FAMILY PLANNING"); + if (antaraActivity3 != null) { + addIncenticeRecord(ect, userId, antaraActivity3); + } + } else if (ect.getAntraDose().contains("Dose-4")) { + IncentiveActivity antaraActivity4 = + incentivesRepo.findIncentiveMasterByNameAndGroup("FP_ANC_MPA4", "FAMILY PLANNING"); + if (antaraActivity4 != null) { + addIncenticeRecord(ect, userId, antaraActivity4); + } - } else if (ect.getAntraDose().contains("Dose-5")) { - IncentiveActivity antaraActivity4 = - incentivesRepo.findIncentiveMasterByNameAndGroup("FP_ANC_MPA5", GroupName.FAMILY_PLANNING.getDisplayName()); + }else if (ect.getAntraDose().contains("Dose-5")) { + IncentiveActivity antaraActivity4 = + incentivesRepo.findIncentiveMasterByNameAndGroup("FP_ANC_MPA5", GroupName.FAMILY_PLANNING.getDisplayName()); + + if (antaraActivity4 != null) { + addIncenticeRecord(ect, userId, antaraActivity4); + } + } + } + if(stateId.equals(StateCode.CG.getStateCode())){ IncentiveActivity antaraActivity4CH = incentivesRepo.findIncentiveMasterByNameAndGroup("FP_ANC_MPA1", GroupName.ACTIVITY.getDisplayName()); if (antaraActivity4CH != null) { - addIncenticeRecord(recordList, ect, userId, antaraActivity4CH); - } - - if (antaraActivity4 != null) { - addIncenticeRecord(recordList, ect, userId, antaraActivity4); + addIncenticeRecord(ect, userId, antaraActivity4CH); } } + + } else if (ect.getMethodOfContraception() != null && ect.getMethodOfContraception().equals("MALE STERILIZATION")) { - - IncentiveActivity maleSterilizationActivityAM = - incentivesRepo.findIncentiveMasterByNameAndGroup("FP_MALE_STER", "FAMILY PLANNING"); - - IncentiveActivity maleSterilizationActivityCH = - incentivesRepo.findIncentiveMasterByNameAndGroup("FP_MALE_STER", GroupName.ACTIVITY.getDisplayName()); - if (maleSterilizationActivityAM != null) { - addIncenticeRecord(recordList, ect, userId, maleSterilizationActivityAM); + if(stateId.equals(StateCode.AM.getStateCode())){ + IncentiveActivity maleSterilizationActivityAM = + incentivesRepo.findIncentiveMasterByNameAndGroup("FP_MALE_STER", "FAMILY PLANNING"); + + if (maleSterilizationActivityAM != null) { + addIncenticeRecord(ect, userId, maleSterilizationActivityAM); + } + } - - if (maleSterilizationActivityCH != null) { - addIncenticeRecord(recordList, ect, userId, maleSterilizationActivityCH); + if(stateId.equals(StateCode.CG.getStateCode())){ + + IncentiveActivity maleSterilizationActivityCH = + incentivesRepo.findIncentiveMasterByNameAndGroup("FP_MALE_STER", GroupName.ACTIVITY.getDisplayName()); + + + if (maleSterilizationActivityCH != null) { + addIncenticeRecord(ect, userId, maleSterilizationActivityCH); + } } + } else if (ect.getMethodOfContraception() != null && ect.getMethodOfContraception().equals("FEMALE STERILIZATION")) { + if(stateId.equals(StateCode.CG.getStateCode())){ + IncentiveActivity femaleSterilizationActivityCH = + incentivesRepo.findIncentiveMasterByNameAndGroup("FP_FEMALE_STER", GroupName.ACTIVITY.getDisplayName()); - IncentiveActivity femaleSterilizationActivityAM = - incentivesRepo.findIncentiveMasterByNameAndGroup("FP_FEMALE_STER", GroupName.FAMILY_PLANNING.getDisplayName()); - - IncentiveActivity femaleSterilizationActivityCH = - incentivesRepo.findIncentiveMasterByNameAndGroup("FP_FEMALE_STER", GroupName.ACTIVITY.getDisplayName()); - if (femaleSterilizationActivityAM != null) { - addIncenticeRecord(recordList, ect, userId, femaleSterilizationActivityAM); + if (femaleSterilizationActivityCH != null) { + addIncenticeRecord(ect, userId, femaleSterilizationActivityCH); + } } + if(stateId.equals(StateCode.AM.getStateCode())){ + IncentiveActivity femaleSterilizationActivityAM = + incentivesRepo.findIncentiveMasterByNameAndGroup("FP_FEMALE_STER", GroupName.FAMILY_PLANNING.getDisplayName()); + if (femaleSterilizationActivityAM != null) { + addIncenticeRecord(ect, userId, femaleSterilizationActivityAM); + } - if (femaleSterilizationActivityCH != null) { - addIncenticeRecord(recordList, ect, userId, femaleSterilizationActivityCH); } - } else if (ect.getMethodOfContraception() != null && ect.getMethodOfContraception().equals("MiniLap")) { - IncentiveActivity miniLapActivity = - incentivesRepo.findIncentiveMasterByNameAndGroup("FP_MINILAP", "FAMILY PLANNING"); - if (miniLapActivity != null) { - addIncenticeRecord(recordList, ect, userId, miniLapActivity); + + + + } else if (ect.getMethodOfContraception() != null && ect.getMethodOfContraception().equals("MiniLap")) { + if(stateId.equals(StateCode.AM.getStateCode())){ + IncentiveActivity miniLapActivity = + incentivesRepo.findIncentiveMasterByNameAndGroup("FP_MINILAP", "FAMILY PLANNING"); + if (miniLapActivity != null) { + addIncenticeRecord(ect, userId, miniLapActivity); + } } + } else if (ect.getMethodOfContraception() != null && ect.getMethodOfContraception().equals("Condom")) { - - IncentiveActivity comdomActivity = - incentivesRepo.findIncentiveMasterByNameAndGroup("FP_CONDOM", "FAMILY PLANNING"); - if (comdomActivity != null) { - addIncenticeRecord(recordList, ect, userId, comdomActivity); + if(stateId.equals(StateCode.AM.getStateCode())){ + IncentiveActivity comdomActivity = + incentivesRepo.findIncentiveMasterByNameAndGroup("FP_CONDOM", "FAMILY PLANNING"); + if (comdomActivity != null) { + addIncenticeRecord(ect, userId, comdomActivity); + } } + } else if (ect.getMethodOfContraception() != null && ect.getMethodOfContraception().equals("Copper T (IUCD)")) { - - IncentiveActivity copperTActivity = - incentivesRepo.findIncentiveMasterByNameAndGroup("FP_CONDOM", "FAMILY PLANNING"); - if (copperTActivity != null) { - addIncenticeRecord(recordList, ect, userId, copperTActivity); + if(stateId.equals(StateCode.AM.getStateCode())){ + IncentiveActivity copperTActivity = + incentivesRepo.findIncentiveMasterByNameAndGroup("FP_CONDOM", "FAMILY PLANNING"); + if (copperTActivity != null) { + addIncenticeRecord(ect, userId, copperTActivity); + } } + } if (ect.getMethodOfContraception() != null && (ect.getMethodOfContraception().contains("POST PARTUM STERILIZATION (PPS WITHIN 7 DAYS OF DELIVERY)") || ect.getMethodOfContraception().contains("MiniLap") || ect.getMethodOfContraception().contains("MALE STERILIZATION") || ect.getMethodOfContraception().contains("FEMALE STERILIZATION"))) { - IncentiveActivity limitiing2ChildActivityAM = - incentivesRepo.findIncentiveMasterByNameAndGroup("FP_LIMIT_2CHILD", GroupName.FAMILY_PLANNING.getDisplayName()); - - IncentiveActivity limitiing2ChildActivityCH = - incentivesRepo.findIncentiveMasterByNameAndGroup("FP_LIMIT_2CHILD", GroupName.ACTIVITY.getDisplayName()); - if (limitiing2ChildActivityAM != null) { - addIncenticeRecord(recordList, ect, userId, limitiing2ChildActivityAM); + if(stateId.equals(StateCode.AM.getStateCode())){ + IncentiveActivity limitiing2ChildActivityAM = + incentivesRepo.findIncentiveMasterByNameAndGroup("FP_LIMIT_2CHILD", GroupName.FAMILY_PLANNING.getDisplayName()); + if (limitiing2ChildActivityAM != null) { + addIncenticeRecord(ect, userId, limitiing2ChildActivityAM); + } } + if(stateId.equals(StateCode.CG.getStateCode())){ + IncentiveActivity limitiing2ChildActivityCH = + incentivesRepo.findIncentiveMasterByNameAndGroup("FP_LIMIT_2CHILD", GroupName.ACTIVITY.getDisplayName()); + - if (limitiing2ChildActivityCH != null) { - addIncenticeRecord(recordList, ect, userId, limitiing2ChildActivityCH); + if (limitiing2ChildActivityCH != null) { + addIncenticeRecord(ect, userId, limitiing2ChildActivityCH); + } } + + } } - private void addIncenticeRecord(List recordList, EligibleCoupleTracking ect, Integer userId, IncentiveActivity antaraActivity) { + private void addIncenticeRecord(EligibleCoupleTracking ect, Integer userId, IncentiveActivity antaraActivity) { IncentiveActivityRecord record = recordRepo - .findRecordByActivityIdCreatedDateBenId(antaraActivity.getId(), ect.getCreatedDate(), ect.getBenId()); + .findRecordByActivityIdCreatedDateBenId(antaraActivity.getId(), ect.getVisitDate(), ect.getBenId(),userId); // get bene details if (record == null) { @@ -393,7 +600,7 @@ record = new IncentiveActivityRecord(); record.setBenId(ect.getBenId()); record.setAshaId(userId); record.setAmount(Long.valueOf(antaraActivity.getRate())); - recordList.add(record); + recordRepo.save(record); } } @@ -409,6 +616,7 @@ public String getEligibleCoupleRegRecords(GetBenRequestHandler dto) { Gson gson = new GsonBuilder() .serializeNulls() .setDateFormat("MMM dd, yyyy h:mm:ss a").create(); + return gson.toJson(list); } catch (Exception e) { logger.error(e.getMessage()); @@ -418,6 +626,7 @@ public String getEligibleCoupleRegRecords(GetBenRequestHandler dto) { @Override public List getEligibleCoupleTracking(GetBenRequestHandler dto) { + List recordList = new ArrayList<>(); try { String user = beneficiaryRepo.getUserName(dto.getAshaId()); @@ -425,9 +634,12 @@ public List getEligibleCoupleTracking(GetBenRequestHa List eligibleCoupleTrackingList = eligibleCoupleTrackingRepo.getECTrackRecords(user, dto.getFromDate(), dto.getToDate()); + recordRepo.saveAll(recordList); + return eligibleCoupleTrackingList.stream() .map(ect -> mapper.convertValue(ect, EligibleCoupleTrackingDTO.class)) .collect(Collectors.toList()); + } catch (Exception e) { logger.error(e.getMessage()); } diff --git a/src/main/java/com/iemr/flw/service/impl/CrashLogServiceImpl.java b/src/main/java/com/iemr/flw/service/impl/CrashLogServiceImpl.java index da2fbaa0..4c85ae66 100644 --- a/src/main/java/com/iemr/flw/service/impl/CrashLogServiceImpl.java +++ b/src/main/java/com/iemr/flw/service/impl/CrashLogServiceImpl.java @@ -24,7 +24,7 @@ public class CrashLogServiceImpl implements CrashLogService { private final Logger logger = LoggerFactory.getLogger(this.getClass().getName()); - @Value("${crash.logs.base.path}") + // @Value("${crash.logs.base.path}") private String crashLogsBasePath; @Override diff --git a/src/main/java/com/iemr/flw/service/impl/DeathReportsServiceImpl.java b/src/main/java/com/iemr/flw/service/impl/DeathReportsServiceImpl.java index 82cee5d0..cfc77214 100644 --- a/src/main/java/com/iemr/flw/service/impl/DeathReportsServiceImpl.java +++ b/src/main/java/com/iemr/flw/service/impl/DeathReportsServiceImpl.java @@ -7,15 +7,19 @@ import com.iemr.flw.dto.iemr.CdrDTO; import com.iemr.flw.dto.iemr.MdsrDTO; import com.iemr.flw.masterEnum.GroupName; +import com.iemr.flw.masterEnum.StateCode; import com.iemr.flw.repo.identity.BeneficiaryRepo; import com.iemr.flw.repo.iemr.*; import com.iemr.flw.service.DeathReportsService; import org.modelmapper.ModelMapper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; +import java.util.Optional; import java.util.stream.Collectors; @Service @@ -43,16 +47,20 @@ public class DeathReportsServiceImpl implements DeathReportsService { @Autowired private IncentiveRecordRepo recordRepo; + @Autowired + private UpdateIncentivePendindDocService pendindDocService; + + private final Logger logger = LoggerFactory.getLogger(DeathReportsServiceImpl.class); @Override public String registerCDR(List cdrDTOs) { try { List cdrList = new ArrayList<>(); - cdrDTOs.forEach(it ->{ + cdrDTOs.forEach(it -> { CDR existingCDR = cdrRepo.findCDRByBenId(it.getBenId()); - if(existingCDR != null) { + if (existingCDR != null) { Long id = existingCDR.getId(); modelMapper.map(it, existingCDR); existingCDR.setId(id); @@ -73,15 +81,16 @@ public String registerCDR(List cdrDTOs) { } } + @Override public String registerMDSR(List mdsrDTOs) { try { List mdsrList = new ArrayList<>(); - mdsrDTOs.forEach(it ->{ + mdsrDTOs.forEach(it -> { MDSR mdsr = mdsrRepo.findMDSRByBenId(it.getBenId()); - if(mdsr != null) { + if (mdsr != null) { Long id = mdsr.getId(); modelMapper.map(it, mdsr); mdsr.setId(id); @@ -101,16 +110,19 @@ public String registerMDSR(List mdsrDTOs) { } } + @Override public List getCdrRecords(GetBenRequestHandler dto) { - try{ - String user = beneficiaryRepo.getUserName(dto.getAshaId()); + try { + String user = userRepo.getUserNamedByUserId(dto.getAshaId()); List cdrlist = - cdrRepo.getAllCdrByBenId(user, dto.getFromDate(), dto.getToDate()); + cdrRepo.findByCreatedBy(user); return cdrlist.stream() .map(cdr -> mapper.convertValue(cdr, CdrDTO.class)) .collect(Collectors.toList()); } catch (Exception e) { + logger.error("MDSR Exception:" + e.getMessage()); + // log } return null; @@ -120,47 +132,74 @@ public List getCdrRecords(GetBenRequestHandler dto) { public List getMdsrRecords(GetBenRequestHandler dto) { try { - String user = beneficiaryRepo.getUserName(dto.getAshaId()); + String user = userRepo.getUserNamedByUserId(dto.getAshaId()); List mdsrList = - mdsrRepo.getAllMdsrByAshaId(user, dto.getFromDate(), dto.getToDate()); + mdsrRepo.findByCreatedBy(user); return mdsrList.stream() .map(mdsr -> mapper.convertValue(mdsr, MdsrDTO.class)) .collect(Collectors.toList()); } catch (Exception e) { + logger.error("MDSR Exception:" + e.getMessage()); + // log } return null; } private void checkAndAddIncentives(List cdrList) { + if(!cdrList.isEmpty()){ + Integer userId = userRepo.getUserIdByName(cdrList.get(0).getCreatedBy()); + cdrList.forEach(cdr -> { + IncentiveActivity immunizationActivity = + incentivesRepo.findIncentiveMasterByNameAndGroup("CHILD_DEATH_REPORTING", GroupName.CHILD_HEALTH.getDisplayName()); + createIncentiveRecord(cdr, cdr.getBenId(), userId, immunizationActivity); + if(immunizationActivity!=null){ + createIncentiveRecord(cdr,cdr.getBenId(),userId,immunizationActivity); + + } + }); + + } + - cdrList.forEach( cdr -> { - Integer userId = userRepo.getUserIdByName(cdr.getCreatedBy()); - IncentiveActivity immunizationActivity = - incentivesRepo.findIncentiveMasterByNameAndGroup("CHILD_DEATH_REPORTING", GroupName.CHILD_HEALTH.getDisplayName()); - createIncentiveRecord(cdr,cdr.getBenId(),userId,immunizationActivity); - }); } private void checkAndAddIncentivesMdsr(List mdsrList) { - - mdsrList.forEach( mdsr -> { + mdsrList.forEach(mdsr -> { Integer userId = userRepo.getUserIdByName(mdsr.getCreatedBy()); - IncentiveActivity immunizationActivity = - incentivesRepo.findIncentiveMasterByNameAndGroup("MATERNAL_DEATH_REPORT", GroupName.MATERNAL_HEALTH.getDisplayName()); + Integer stateId = userRepo.getUserRole(userId).get(0).getStateId(); + if(stateId.equals(StateCode.AM.getStateCode())){ + IncentiveActivity immunizationActivity = + incentivesRepo.findIncentiveMasterByNameAndGroup("MATERNAL_DEATH_REPORT", GroupName.MATERNAL_HEALTH.getDisplayName()); + + if(immunizationActivity!=null){ + createIncentiveRecord(mdsr,mdsr.getBenId(),userId,immunizationActivity); + + } + + + } + if(stateId.equals(StateCode.CG.getStateCode())){ + IncentiveActivity cgImmunizationActivity = + incentivesRepo.findIncentiveMasterByNameAndGroup("MATERNAL_DEATH_REPORT", GroupName.ACTIVITY.getDisplayName()); + + if(cgImmunizationActivity!=null){ + createIncentiveRecord(mdsr,mdsr.getBenId(),userId,cgImmunizationActivity); + + } + } + - createIncentiveRecord(mdsr,mdsr.getBenId(),userId,immunizationActivity); }); } - private void createIncentiveRecord(CDR cdr, Long benId, Integer userId, IncentiveActivity immunizationActivity) { IncentiveActivityRecord record = recordRepo - .findRecordByActivityIdCreatedDateBenId(immunizationActivity.getId(), cdr.getCreatedDate(), benId); + .findRecordByActivityIdCreatedDateBenId(immunizationActivity.getId(), cdr.getCreatedDate(), benId,userId); if (record == null) { record = new IncentiveActivityRecord(); record.setActivityId(immunizationActivity.getId()); @@ -173,13 +212,16 @@ record = new IncentiveActivityRecord(); record.setBenId(benId); record.setAshaId(userId); record.setAmount(Long.valueOf(immunizationActivity.getRate())); + record.setIsEligible(true); recordRepo.save(record); + + } } private void createIncentiveRecord(MDSR mdsr, Long benId, Integer userId, IncentiveActivity immunizationActivity) { IncentiveActivityRecord record = recordRepo - .findRecordByActivityIdCreatedDateBenId(immunizationActivity.getId(), mdsr.getCreatedDate(), benId); + .findRecordByActivityIdCreatedDateBenId(immunizationActivity.getId(), mdsr.getCreatedDate(), benId,userId); if (record == null) { record = new IncentiveActivityRecord(); record.setActivityId(immunizationActivity.getId()); @@ -192,14 +234,29 @@ record = new IncentiveActivityRecord(); record.setBenId(benId); record.setAshaId(userId); record.setAmount(Long.valueOf(immunizationActivity.getRate())); - recordRepo.save(record); - } - } + if (userRepo.getUserRole(userId).get(0).getStateId() == StateCode.AM.getStateCode()) { + if (mdsr.getMdsrDeathCertFile() != null && mdsr.getMdsr1File() != null && mdsr.getMdsr2File() != null) { + record.setIsEligible(true); + recordRepo.save(record); + + } else { + record.setIsEligible(false); + IncentiveActivityRecord incentiveActivityRecord = recordRepo.save(record); + if (incentiveActivityRecord == null) { + recordRepo.save(record); + pendindDocService.updatePendingActivity(userId, mdsr.getId(), record.getId(), immunizationActivity.getId()); + } + } + } else { + recordRepo.save(record); + } + } + } } diff --git a/src/main/java/com/iemr/flw/service/impl/DeliveryOutcomeServiceImpl.java b/src/main/java/com/iemr/flw/service/impl/DeliveryOutcomeServiceImpl.java index 0e329f4b..f6769fbd 100644 --- a/src/main/java/com/iemr/flw/service/impl/DeliveryOutcomeServiceImpl.java +++ b/src/main/java/com/iemr/flw/service/impl/DeliveryOutcomeServiceImpl.java @@ -11,6 +11,7 @@ import com.iemr.flw.dto.identity.GetBenRequestHandler; import com.iemr.flw.dto.iemr.DeliveryOutcomeDTO; import com.iemr.flw.masterEnum.GroupName; +import com.iemr.flw.masterEnum.StateCode; import com.iemr.flw.repo.identity.BeneficiaryRepo; import com.iemr.flw.repo.identity.HouseHoldRepo; import com.iemr.flw.repo.iemr.DeliveryOutcomeRepo; @@ -18,6 +19,7 @@ import com.iemr.flw.repo.iemr.IncentivesRepo; import com.iemr.flw.repo.iemr.UserServiceRoleRepo; import com.iemr.flw.service.DeliveryOutcomeService; +import com.iemr.flw.service.UserService; import jakarta.annotation.PostConstruct; import org.apache.commons.lang3.Validate; import org.modelmapper.ModelMapper; @@ -27,6 +29,10 @@ import org.springframework.stereotype.Service; import java.math.BigInteger; +import java.sql.Timestamp; +import java.time.LocalDate; +import java.time.ZoneId; +import java.time.temporal.ChronoUnit; import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -51,6 +57,9 @@ public class DeliveryOutcomeServiceImpl implements DeliveryOutcomeService { @Autowired private UserServiceRoleRepo userRepo; + @Autowired + private UserService userService; + @Autowired private IncentiveRecordRepo recordRepo; @@ -108,98 +117,185 @@ public String registerDeliveryOutcome(List deliveryOutcomeDT @Override public List getDeliveryOutcome(GetBenRequestHandler dto) { try { - String user = beneficiaryRepo.getUserName(dto.getAshaId()); - List deliveryOutcomeList = deliveryOutcomeRepo.getDeliveryOutcomeByAshaId(user, dto.getFromDate(), dto.getToDate()); + String user = userRepo.getUserNamedByUserId(dto.getAshaId()); + List deliveryOutcomeList = deliveryOutcomeRepo.getDeliveryOutcomeByAshaId(user,dto.getFromDate(),dto.getToDate()); + logger.info("DeliveryOutcome Response{}",deliveryOutcomeList); return deliveryOutcomeList.stream() .map(deliveryOutcome -> mapper.convertValue(deliveryOutcome, DeliveryOutcomeDTO.class)) .collect(Collectors.toList()); } catch (Exception e) { - logger.error(e.getMessage()); + logger.error("DeliveryOutcome Exception:"+e.getMessage()); } return null; } public void checkAndAddJsyIncentive(List delOutList) { + if(!delOutList.isEmpty()){ + Integer userID = userRepo.getUserIdByName(delOutList.get(0).getCreatedBy()); + Integer stateId = userService.getUserDetail(userID).getStateId(); + delOutList.forEach(deliveryOutcome -> { + + IncentiveActivity institutionalDeliveryActivityAM = incentivesRepo.findIncentiveMasterByNameAndGroup("MH_MOTIVATE_INST_DEL", GroupName.MATERNAL_HEALTH.getDisplayName()); + IncentiveActivity institutionalDeliveryActivityCH = incentivesRepo.findIncentiveMasterByNameAndGroup("INST_DELIVERY_ESCORT", GroupName.ACTIVITY.getDisplayName()); + if (deliveryOutcome.getPlaceOfDelivery() != null) { + String placeOfDelivery = deliveryOutcome.getPlaceOfDelivery(); + + if (placeOfDelivery != null && + (!placeOfDelivery.equalsIgnoreCase("home") || + !placeOfDelivery.equalsIgnoreCase("in transit") || + !placeOfDelivery.equalsIgnoreCase("other private hospital"))) { + + // Institutional delivery (eligible case) + if (institutionalDeliveryActivityAM != null) { + createIncentiveRecordforJsy(deliveryOutcome, deliveryOutcome.getBenId(), institutionalDeliveryActivityAM); + } - delOutList.forEach(deliveryOutcome -> { - - IncentiveActivity institutionalDeliveryActivityAM = incentivesRepo.findIncentiveMasterByNameAndGroup("MH_MOTIVATE_INST_DEL", GroupName.MATERNAL_HEALTH.getDisplayName()); - IncentiveActivity institutionalDeliveryActivityCH = incentivesRepo.findIncentiveMasterByNameAndGroup("INST_DELIVERY_ESCORT", GroupName.ACTIVITY.getDisplayName()); - if (deliveryOutcome.getPlaceOfDelivery() != null) { - String placeOfDelivery = deliveryOutcome.getPlaceOfDelivery(); - - if (placeOfDelivery != null && - (!placeOfDelivery.equalsIgnoreCase("home") || - !placeOfDelivery.equalsIgnoreCase("in transit") || - !placeOfDelivery.equalsIgnoreCase("other private hospital"))) { - - // Institutional delivery (eligible case) - if (institutionalDeliveryActivityAM != null) { - createIncentiveRecordforJsy(deliveryOutcome, deliveryOutcome.getBenId(), institutionalDeliveryActivityAM); + if (institutionalDeliveryActivityCH != null && placeOfDelivery.toLowerCase().equals("Government Hospital")) { + createIncentiveRecordforJsy(deliveryOutcome, deliveryOutcome.getBenId(), institutionalDeliveryActivityCH); + } } + } - if (institutionalDeliveryActivityCH != null) { - createIncentiveRecordforJsy(deliveryOutcome, deliveryOutcome.getBenId(), institutionalDeliveryActivityCH); + if (deliveryOutcome.getIsJSYBenificiary()) { + IncentiveActivity incentiveActivityInstJSY1 = incentivesRepo.findIncentiveMasterByNameAndGroup("JSY_1ST_DEL_INST_RURAL", GroupName.JSY.getDisplayName()); + IncentiveActivity incentiveActivityInstJSY2 = incentivesRepo.findIncentiveMasterByNameAndGroup("JSY_2ND_DEL_INST_RURAL", GroupName.JSY.getDisplayName()); + IncentiveActivity incentiveActivityInstJSY3 = incentivesRepo.findIncentiveMasterByNameAndGroup("JSY_3RD_DEL_INST_RURAL", GroupName.JSY.getDisplayName()); + IncentiveActivity incentiveActivityInstJSY4 = incentivesRepo.findIncentiveMasterByNameAndGroup("JSY_4TH_DEL_INST_RURAL", GroupName.JSY.getDisplayName()); + + + logger.info("delOutList" + gson.toJson(deliveryOutcome)); + IncentiveActivity incentiveActivityJSY1 = incentivesRepo.findIncentiveMasterByNameAndGroup("JSY_1ST_DEL_ANC_RURAL", GroupName.JSY.getDisplayName()); + if (incentiveActivityJSY1 != null) { + if (deliveryOutcome.getDeliveryOutcome() == 1) { + createIncentiveRecordforJsy(deliveryOutcome, deliveryOutcome.getBenId(), incentiveActivityJSY1); + if (deliveryOutcome.getPlaceOfDelivery() != null) { + createIncentiveRecordforJsy(deliveryOutcome, deliveryOutcome.getBenId(), incentiveActivityInstJSY1); + } + } } - } - } - if (deliveryOutcome.getIsJSYBenificiary()) { - IncentiveActivity incentiveActivityInstJSY1 = incentivesRepo.findIncentiveMasterByNameAndGroup("JSY_1ST_DEL_INST_RURAL", GroupName.JSY.getDisplayName()); - IncentiveActivity incentiveActivityInstJSY2 = incentivesRepo.findIncentiveMasterByNameAndGroup("JSY_2ND_DEL_INST_RURAL", GroupName.JSY.getDisplayName()); - IncentiveActivity incentiveActivityInstJSY3 = incentivesRepo.findIncentiveMasterByNameAndGroup("JSY_3RD_DEL_INST_RURAL", GroupName.JSY.getDisplayName()); - IncentiveActivity incentiveActivityInstJSY4 = incentivesRepo.findIncentiveMasterByNameAndGroup("JSY_4TH_DEL_INST_RURAL", GroupName.JSY.getDisplayName()); + IncentiveActivity incentiveActivityJSY2 = incentivesRepo.findIncentiveMasterByNameAndGroup("JSY_2ND_DEL_ANC_RURAL", GroupName.JSY.getDisplayName()); + if (incentiveActivityJSY2 != null) { + if (deliveryOutcome.getDeliveryOutcome() == 2) { + createIncentiveRecordforJsy(deliveryOutcome, deliveryOutcome.getBenId(), incentiveActivityJSY2); + if (deliveryOutcome.getPlaceOfDelivery() != null) { - logger.info("delOutList" + gson.toJson(deliveryOutcome)); - IncentiveActivity incentiveActivityJSY1 = incentivesRepo.findIncentiveMasterByNameAndGroup("JSY_1ST_DEL_ANC_RURAL", GroupName.JSY.getDisplayName()); - if (incentiveActivityJSY1 != null) { - if (deliveryOutcome.getDeliveryOutcome() == 1) { - createIncentiveRecordforJsy(deliveryOutcome, deliveryOutcome.getBenId(), incentiveActivityJSY1); - if (deliveryOutcome.getPlaceOfDelivery() != null) { - createIncentiveRecordforJsy(deliveryOutcome, deliveryOutcome.getBenId(), incentiveActivityInstJSY1); + createIncentiveRecordforJsy(deliveryOutcome, deliveryOutcome.getBenId(), incentiveActivityInstJSY2); + } } } - } + IncentiveActivity incentiveActivityJSY3 = incentivesRepo.findIncentiveMasterByNameAndGroup("JSY_3RD_DEL_ANC_RURAL", GroupName.JSY.getDisplayName()); + if (incentiveActivityJSY3 != null) { + if (deliveryOutcome.getDeliveryOutcome() == 3) { + createIncentiveRecordforJsy(deliveryOutcome, deliveryOutcome.getBenId(), incentiveActivityJSY3); + if (deliveryOutcome.getPlaceOfDelivery() != null) { + createIncentiveRecordforJsy(deliveryOutcome, deliveryOutcome.getBenId(), incentiveActivityInstJSY3); + } + } + } - IncentiveActivity incentiveActivityJSY2 = incentivesRepo.findIncentiveMasterByNameAndGroup("JSY_2ND_DEL_ANC_RURAL", GroupName.JSY.getDisplayName()); - if (incentiveActivityJSY2 != null) { - if (deliveryOutcome.getDeliveryOutcome() == 2) { - createIncentiveRecordforJsy(deliveryOutcome, deliveryOutcome.getBenId(), incentiveActivityJSY2); - if (deliveryOutcome.getPlaceOfDelivery() != null) { + IncentiveActivity incentiveActivityJSY4 = incentivesRepo.findIncentiveMasterByNameAndGroup("JSY_4TH_DEL_ANC_RURAL", GroupName.JSY.getDisplayName()); + if (incentiveActivityJSY4 != null) { + if (deliveryOutcome.getDeliveryOutcome() == 4) { + createIncentiveRecordforJsy(deliveryOutcome, deliveryOutcome.getBenId(), incentiveActivityJSY4); + if (deliveryOutcome.getPlaceOfDelivery() != null) { + createIncentiveRecordforJsy(deliveryOutcome, deliveryOutcome.getBenId(), incentiveActivityInstJSY4); - createIncentiveRecordforJsy(deliveryOutcome, deliveryOutcome.getBenId(), incentiveActivityInstJSY2); + } } } + } + if(deliveryOutcome.getDeliveryOutcome()!=null){ + if(!beneficiaryRepo.findByBenficieryid(deliveryOutcome.getBenId()).isEmpty()){ + RMNCHBeneficiaryDetailsRmnch rmnchBeneficiaryDetailsRmnch = beneficiaryRepo.findByBenficieryid(deliveryOutcome.getBenId()).get(0); + LocalDate marriageDate = rmnchBeneficiaryDetailsRmnch.getDateMarriage() + .toInstant() + .atZone(ZoneId.systemDefault()) + .toLocalDate(); + + LocalDate deliveryDate = deliveryOutcome.getDateOfDelivery() + .toInstant() + .atZone(ZoneId.systemDefault()) + .toLocalDate(); + + long years = ChronoUnit.YEARS.between(marriageDate, deliveryDate); + if (years >= 2 && !rmnchBeneficiaryDetailsRmnch.getDoYouHavechildren()) { + if(stateId.equals(StateCode.CG.getStateCode())){ + IncentiveActivity activity = + incentivesRepo.findIncentiveMasterByNameAndGroup( + "FP_DELAY_2Y", + GroupName.ACTIVITY.getDisplayName()); + + createIncentiveRecordforYearGap(deliveryOutcome, deliveryOutcome.getBenId(), activity); + } + if(stateId.equals(StateCode.AM.getStateCode())){ + IncentiveActivity activity = + incentivesRepo.findIncentiveMasterByNameAndGroup( + "FP_DELAY_2Y", + GroupName.FAMILY_PLANNING.getDisplayName()); + + createIncentiveRecordforYearGap(deliveryOutcome, deliveryOutcome.getBenId(), activity); + } + - IncentiveActivity incentiveActivityJSY3 = incentivesRepo.findIncentiveMasterByNameAndGroup("JSY_3RD_DEL_ANC_RURAL", GroupName.JSY.getDisplayName()); - if (incentiveActivityJSY3 != null) { - if (deliveryOutcome.getDeliveryOutcome() == 3) { - createIncentiveRecordforJsy(deliveryOutcome, deliveryOutcome.getBenId(), incentiveActivityJSY3); - if (deliveryOutcome.getPlaceOfDelivery() != null) { - createIncentiveRecordforJsy(deliveryOutcome, deliveryOutcome.getBenId(), incentiveActivityInstJSY3); } + } + + + } - IncentiveActivity incentiveActivityJSY4 = incentivesRepo.findIncentiveMasterByNameAndGroup("JSY_4TH_DEL_ANC_RURAL", GroupName.JSY.getDisplayName()); - if (incentiveActivityJSY4 != null) { - if (deliveryOutcome.getDeliveryOutcome() == 4) { - createIncentiveRecordforJsy(deliveryOutcome, deliveryOutcome.getBenId(), incentiveActivityJSY4); - if (deliveryOutcome.getPlaceOfDelivery() != null) { - createIncentiveRecordforJsy(deliveryOutcome, deliveryOutcome.getBenId(), incentiveActivityInstJSY4); + if(deliveryOutcome.getDeliveryOutcome()!=null){ + if(!beneficiaryRepo.findByBenficieryid(deliveryOutcome.getBenId()).isEmpty()){ + RMNCHBeneficiaryDetailsRmnch rmnchBeneficiaryDetailsRmnch = beneficiaryRepo.findByBenficieryid(deliveryOutcome.getBenId()).get(0); + LocalDate marriageDate = rmnchBeneficiaryDetailsRmnch.getDateMarriage() + .toInstant() + .atZone(ZoneId.systemDefault()) + .toLocalDate(); + + LocalDate deliveryDate = deliveryOutcome.getDateOfDelivery() + .toInstant() + .atZone(ZoneId.systemDefault()) + .toLocalDate(); + + long years = ChronoUnit.YEARS.between(marriageDate, deliveryDate); + + if (years >= 3 && rmnchBeneficiaryDetailsRmnch.getDoYouHavechildren()) { + if(stateId.equals(StateCode.AM.getStateCode())){ + IncentiveActivity activity = + incentivesRepo.findIncentiveMasterByNameAndGroup( + "1st_2nd_CHILD_GAP", + GroupName.FAMILY_PLANNING.getDisplayName()); + + createIncentiveRecordforYearGap(deliveryOutcome, deliveryOutcome.getBenId(), activity); + } + if(stateId.equals(StateCode.CG.getStateCode())){ + IncentiveActivity activity = + incentivesRepo.findIncentiveMasterByNameAndGroup( + "1st_2nd_CHILD_GAP", + GroupName.ACTIVITY.getDisplayName()); + + createIncentiveRecordforYearGap(deliveryOutcome, deliveryOutcome.getBenId(), activity); + } + } } + + + } - } + + }); + } - }); // JSY_ANC_URBAN // JSY_INST_URBAN @@ -212,7 +308,41 @@ private void createIncentiveRecordforJsy(DeliveryOutcome delOutList, Long benId, try { IncentiveActivityRecord record = recordRepo - .findRecordByActivityIdCreatedDateBenId(immunizationActivity.getId(), delOutList.getCreatedDate(), benId); + .findRecordByActivityIdCreatedDateBenId(immunizationActivity.getId(), delOutList.getDateOfDelivery(), benId,userRepo.getUserIdByName(delOutList.getUpdatedBy())); + + + if (record == null) { + logger.info("setStartDate" + delOutList.getDateOfDelivery()); + logger.info("setCreatedDate" + delOutList.getCreatedDate()); + record = new IncentiveActivityRecord(); + record.setActivityId(immunizationActivity.getId()); + record.setCreatedDate(delOutList.getDateOfDelivery()); + record.setCreatedBy(delOutList.getCreatedBy()); + record.setStartDate(delOutList.getDateOfDelivery()); + record.setEndDate(delOutList.getDateOfDelivery()); + record.setUpdatedDate(delOutList.getDateOfDelivery()); + record.setUpdatedBy(delOutList.getCreatedBy()); + record.setBenId(benId); + record.setAshaId(userRepo.getUserIdByName(delOutList.getUpdatedBy())); + record.setAmount(Long.valueOf(immunizationActivity.getRate())); + recordRepo.save(record); + } else { + logger.info("benId:" + record.getId()); + + } + } catch (Exception e) { + logger.error("JSY Incentive:", e); + } + + + } + + private void createIncentiveRecordforYearGap(DeliveryOutcome delOutList, Long benId, IncentiveActivity immunizationActivity) { + logger.info("benId" + benId); + + try { + IncentiveActivityRecord record = recordRepo + .findRecordByActivityIdCreatedDateBenId(immunizationActivity.getId(), delOutList.getDateOfDelivery(), benId,userRepo.getUserIdByName(delOutList.getUpdatedBy())); if (record == null) { @@ -224,7 +354,7 @@ record = new IncentiveActivityRecord(); record.setCreatedBy(delOutList.getCreatedBy()); record.setStartDate(delOutList.getDateOfDelivery()); record.setEndDate(delOutList.getDateOfDelivery()); - record.setUpdatedDate(delOutList.getCreatedDate()); + record.setUpdatedDate(delOutList.getDateOfDelivery()); record.setUpdatedBy(delOutList.getCreatedBy()); record.setBenId(benId); record.setAshaId(userRepo.getUserIdByName(delOutList.getUpdatedBy())); diff --git a/src/main/java/com/iemr/flw/service/impl/DiseaseControlServiceImpl.java b/src/main/java/com/iemr/flw/service/impl/DiseaseControlServiceImpl.java index bc780038..64bc90cf 100644 --- a/src/main/java/com/iemr/flw/service/impl/DiseaseControlServiceImpl.java +++ b/src/main/java/com/iemr/flw/service/impl/DiseaseControlServiceImpl.java @@ -25,12 +25,21 @@ package com.iemr.flw.service.impl; import com.fasterxml.jackson.databind.ObjectMapper; -import com.iemr.flw.controller.CoupleController; +import com.iemr.flw.domain.identity.RMNCHBeneficiaryDetailsRmnch; import com.iemr.flw.domain.iemr.*; +import com.iemr.flw.dto.identity.GetBenRequestHandler; import com.iemr.flw.dto.iemr.*; import com.iemr.flw.masterEnum.DiseaseType; +import com.iemr.flw.masterEnum.GroupName; +import com.iemr.flw.masterEnum.StateCode; +import com.iemr.flw.repo.identity.BeneficiaryRepo; import com.iemr.flw.repo.iemr.*; import com.iemr.flw.service.DiseaseControlService; +import com.iemr.flw.service.IncentiveLogicService; +import com.iemr.flw.service.UserService; +import com.iemr.flw.utils.JwtUtil; +import com.iemr.flw.utils.exception.IEMRException; +import jakarta.transaction.Transactional; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -42,6 +51,7 @@ import java.time.format.DateTimeFormatter; import java.util.*; import java.util.stream.Collectors; + import org.modelmapper.ModelMapper; @Service @@ -77,10 +87,23 @@ public class DiseaseControlServiceImpl implements DiseaseControlService { private MosquitoNetRepository mosquitoNetRepository; + @Autowired + private ChronicDiseaseVisitRepository chronicDiseaseVisitRepository; + + @Autowired + private JwtUtil jwtUtil; + + @Autowired + private IncentiveLogicService incentiveLogicService; + @Autowired + private UserService userService; + @Autowired + private BeneficiaryRepo beneficiaryRepo; - private final Logger logger = LoggerFactory.getLogger(CoupleController.class); + + private final Logger logger = LoggerFactory.getLogger(this.getClass().getName()); @Override public String saveMalaria(MalariaDTO diseaseControlDTO) { @@ -102,7 +125,7 @@ public String saveMalaria(MalariaDTO diseaseControlDTO) { @Override public String saveKalaAzar(KalaAzarDTO diseaseControlDTO) { - logger.info("Save request: "+diseaseControlDTO.toString()); + logger.info("Save request: " + diseaseControlDTO.toString()); for (DiseaseKalaAzarDTO diseaseControlData : diseaseControlDTO.getKalaAzarLists()) { if (diseaseKalaAzarRepository.findByBenId(diseaseControlData.getBenId()).isPresent()) { return updateKalaAzarDisease(diseaseControlData); @@ -268,6 +291,7 @@ private String updateFilaria(DiseaseFilariasisDTO diseaseControlData) { @Override + @Transactional public String saveLeprosy(LeprosyDTO diseaseControlDTO) { for (DiseaseLeprosyDTO diseaseControlData : diseaseControlDTO.getLeprosyLists()) { if (diseaseLeprosyRepository.findByBenId(diseaseControlData.getBenId()).isPresent()) { @@ -276,7 +300,21 @@ public String saveLeprosy(LeprosyDTO diseaseControlDTO) { if (diseaseControlDTO.getUserId() != null) { diseaseControlData.setUserId(diseaseControlDTO.getUserId()); } - diseaseLeprosyRepository.save(saveLeprosyData(diseaseControlData)); + ScreeningLeprosy screeningLeprosy = diseaseLeprosyRepository.save(saveLeprosyData(diseaseControlData)); + IncentiveActivityRecord incentiveActivityRecord = + incentiveLogicService.incentiveForIdentificationLeprosy( + screeningLeprosy.getBenId(), + screeningLeprosy.getHomeVisitDate(), + screeningLeprosy.getHomeVisitDate(), + diseaseControlDTO.getUserId()); + + if (incentiveActivityRecord != null) { + logger.info("Incentive processed for Screening Leprosy successfully. RecordId={}", + incentiveActivityRecord.getId()); + } else { + logger.info("Incentive not created"); + } + return "Data add successfully"; } @@ -313,46 +351,54 @@ private LeprosyFollowUp saveLeprosyFollowUpData(LeprosyFollowUpDTO data) { entity.setModifiedBy(data.getModifiedBy()); entity.setLastModDate( data.getLastModDate() != null ? data.getLastModDate() : new Timestamp(System.currentTimeMillis())); - + if(!leprosyFollowUpRepository.findByBenId(entity.getBenId()).isEmpty()){ + if(leprosyFollowUpRepository.findByBenId(entity.getBenId()).size()>=6 && leprosyFollowUpRepository.findByBenId(entity.getBenId()).size()<=12){ + Integer userId = userRepo.getUserIdByName(entity.getCreatedBy()); + IncentiveActivityRecord incentiveActivityRecord = + incentiveLogicService.incentiveForLeprosyPaucibacillaryConfirmed( + entity.getBenId(), + entity.getTreatmentEndDate(), + entity.getTreatmentEndDate(), + userId); + + if (incentiveActivityRecord != null) { + logger.info("Incentive processed for Screening Leprosy successfully. RecordId={}", + incentiveActivityRecord.getId()); + } else { + logger.info("Incentive not created"); + } + } + } + + if(!leprosyFollowUpRepository.findByBenId(entity.getBenId()).isEmpty()){ + if(leprosyFollowUpRepository.findByBenId(entity.getBenId()).size()>=12){ + Integer userId = userRepo.getUserIdByName(entity.getCreatedBy()); + IncentiveActivityRecord incentiveActivityRecord = + incentiveLogicService.incentiveForLeprosyMultibacillaryConfirmed( + entity.getBenId(), + entity.getTreatmentEndDate(), + entity.getTreatmentEndDate(), + userId); + + if (incentiveActivityRecord != null) { + logger.info("Incentive processed for Screening Leprosy successfully. RecordId={}", + incentiveActivityRecord.getId()); + } else { + logger.info("Incentive not created"); + } + } + } return entity; } - private String updateLeprosyFollowUpData(LeprosyFollowUpDTO data, LeprosyFollowUp entity) { - entity.setVisitNumber(data.getVisitNumber()); - entity.setFollowUpDate(data.getFollowUpDate()); - entity.setTreatmentStatus(data.getTreatmentStatus()); - entity.setMdtBlisterPackReceived(data.getMdtBlisterPackReceived()); - entity.setTreatmentCompleteDate(data.getTreatmentCompleteDate()); - entity.setRemarks(data.getRemarks()); - entity.setHomeVisitDate(data.getHomeVisitDate()); - entity.setLeprosySymptoms(data.getLeprosySymptoms()); - entity.setTypeOfLeprosy(data.getTypeOfLeprosy()); - entity.setLeprosySymptomsPosition(data.getLeprosySymptomsPosition()); - entity.setVisitLabel(data.getVisitLabel()); - entity.setLeprosyStatus(data.getLeprosyStatus()); - entity.setReferredTo(data.getReferredTo()); - entity.setReferToName(data.getReferToName()); - entity.setTreatmentEndDate(data.getTreatmentEndDate()); - entity.setMdtBlisterPackRecived(data.getMdtBlisterPackRecived()); - entity.setTreatmentStartDate(data.getTreatmentStartDate()); - - // Update audit info - entity.setModifiedBy(data.getModifiedBy()); - entity.setLastModDate( - data.getLastModDate() != null ? data.getLastModDate() : new Timestamp(System.currentTimeMillis())); - - leprosyFollowUpRepository.save(entity); - return "Follow-up data updated successfully"; - } - @Override public String saveLeprosyFollowUp(LeprosyFollowUpDTO dto) { if (dto == null) return "Invalid data"; - LeprosyFollowUp entity = saveLeprosyFollowUpData(dto); - leprosyFollowUpRepository.save(entity); - return "Follow-up data added successfully"; - + LeprosyFollowUp entity = saveLeprosyFollowUpData(dto); + leprosyFollowUpRepository.save(entity); + return "Follow-up data added successfully"; + } @Override @@ -369,7 +415,6 @@ public List getAllLeprosyData(String createdBy) { return dtos; } - @Override public List getAllLeprosyFollowUpData(String createdBy) { logger.info("Fetching leprosy data for createdBy: " + createdBy); @@ -390,47 +435,102 @@ public Object getAllMalaria(GetDiseaseRequestHandler getDiseaseRequestHandler) { // Fetch and filter malaria disease records List filteredList = diseaseMalariaRepository.findAll().stream() - .filter(disease -> Objects.equals(disease.getUserId(), getDiseaseRequestHandler.getUserId())) + .filter(disease -> Objects.equals(disease.getUserId(), getDiseaseRequestHandler.getAshaId())) .collect(Collectors.toList()); // Check if the list is empty if (filteredList.isEmpty()) { - return Collections.singletonMap("message", "No data found for Malaria."); + return filteredList; } // Map to DTOs List dtoList = filteredList.stream().map(disease -> { + DiseaseMalariaDTO dto = new DiseaseMalariaDTO(); - // Map fields from DiseaseMalaria to DTO - dto.setId(disease.getId()); - dto.setBenId(disease.getBenId()); - dto.setHouseHoldDetailsId(disease.getHouseHoldDetailsId()); - dto.setScreeningDate(disease.getScreeningDate()); - dto.setBeneficiaryStatus(disease.getBeneficiaryStatus()); - dto.setDateOfDeath(disease.getDateOfDeath()); - dto.setPlaceOfDeath(disease.getPlaceOfDeath()); - dto.setOtherPlaceOfDeath(disease.getOtherPlaceOfDeath()); - dto.setReasonForDeath(disease.getReasonForDeath()); - dto.setOtherReasonForDeath(disease.getOtherReasonForDeath()); - dto.setCaseStatus(disease.getCaseStatus()); - dto.setRapidDiagnosticTest(disease.getRapidDiagnosticTest()); - dto.setDateOfRdt(disease.getDateOfRdt()); - dto.setSlideTestPf(disease.getSlideTestPf()); - dto.setSlideTestPv(disease.getSlideTestPv()); - dto.setDateOfSlideTest(disease.getDateOfSlideTest()); - dto.setSlideNo(disease.getSlideNo()); - dto.setReferredTo(disease.getReferredTo()); - dto.setOtherReferredFacility(disease.getOtherReferredFacility()); - dto.setRemarks(disease.getRemarks()); - dto.setDateOfVisitBySupervisor(disease.getDateOfVisitBySupervisor()); - dto.setUserId(disease.getUserId()); - dto.setDiseaseTypeId(disease.getDiseaseTypeId()); - - // Parse symptoms (if present) + if (disease.getId() != null) + dto.setId(disease.getId()); + + if (disease.getBenId() != null) + dto.setBenId(disease.getBenId()); + + if (disease.getHouseHoldDetailsId() != null) + dto.setHouseHoldDetailsId(disease.getHouseHoldDetailsId()); + + if (disease.getScreeningDate() != null) + dto.setScreeningDate(disease.getScreeningDate()); + + if (disease.getBeneficiaryStatus() != null && !disease.getBeneficiaryStatus().trim().isEmpty()) + dto.setBeneficiaryStatus(disease.getBeneficiaryStatus()); + + if (disease.getDateOfDeath() != null) + dto.setDateOfDeath(disease.getDateOfDeath()); + + if (disease.getPlaceOfDeath() != null && !disease.getPlaceOfDeath().trim().isEmpty()) + dto.setPlaceOfDeath(disease.getPlaceOfDeath()); + + if (disease.getOtherPlaceOfDeath() != null && !disease.getOtherPlaceOfDeath().trim().isEmpty()) + dto.setOtherPlaceOfDeath(disease.getOtherPlaceOfDeath()); + + if (disease.getReasonForDeath() != null && !disease.getReasonForDeath().trim().isEmpty()) + dto.setReasonForDeath(disease.getReasonForDeath()); + + if (disease.getOtherReasonForDeath() != null && !disease.getOtherReasonForDeath().trim().isEmpty()) + dto.setOtherReasonForDeath(disease.getOtherReasonForDeath()); + + if (disease.getCaseStatus() != null && !disease.getCaseStatus().trim().isEmpty()) + dto.setCaseStatus(disease.getCaseStatus()); + + if (disease.getRapidDiagnosticTest() != null) + dto.setRapidDiagnosticTest(disease.getRapidDiagnosticTest()); + + if (disease.getDateOfRdt() != null) + dto.setDateOfRdt(disease.getDateOfRdt()); + + if (disease.getSlideTestPf() != null) + dto.setSlideTestPf(disease.getSlideTestPf()); + + if (disease.getSlideTestPv() != null) + dto.setSlideTestPv(disease.getSlideTestPv()); + + if (disease.getDateOfSlideTest() != null) + dto.setDateOfSlideTest(disease.getDateOfSlideTest()); + + if (disease.getSlideNo() != null && !disease.getSlideNo().trim().isEmpty()) + dto.setSlideNo(disease.getSlideNo()); + + if (disease.getReferredTo() != null) + dto.setReferredTo(disease.getReferredTo()); + + if (disease.getOtherReferredFacility() != null && !disease.getOtherReferredFacility().trim().isEmpty()) + dto.setOtherReferredFacility(disease.getOtherReferredFacility()); + + if (disease.getRemarks() != null && !disease.getRemarks().trim().isEmpty()) + dto.setRemarks(disease.getRemarks()); + + if (disease.getMalariaSlideTestType() != null && !disease.getMalariaSlideTestType().trim().isEmpty()) + dto.setMalariaSlideTestType(disease.getMalariaSlideTestType()); + + if (disease.getMalariaTestType() != null && !disease.getMalariaTestType().trim().isEmpty()) + dto.setMalariaTestType(disease.getMalariaTestType()); + + if (disease.getDateOfVisitBySupervisor() != null) + dto.setDateOfVisitBySupervisor(disease.getDateOfVisitBySupervisor()); + + if (disease.getUserId() != null) + dto.setUserId(disease.getUserId()); + + if (disease.getDiseaseTypeId() != null) + dto.setDiseaseTypeId(disease.getDiseaseTypeId()); + + // Symptoms JSON try { - if (disease.getSymptoms() != null && !disease.getSymptoms().isEmpty()) { - MalariaSymptomsDTO symptomsDTO = objectMapper.readValue(disease.getSymptoms(), MalariaSymptomsDTO.class); + if (disease.getSymptoms() != null && + !disease.getSymptoms().trim().isEmpty()) { + + MalariaSymptomsDTO symptomsDTO = + objectMapper.readValue(disease.getSymptoms(), MalariaSymptomsDTO.class); + dto.setFeverMoreThanTwoWeeks(symptomsDTO.isFeverMoreThanTwoWeeks()); dto.setFluLikeIllness(symptomsDTO.isFluLikeIllness()); dto.setShakingChills(symptomsDTO.isShakingChills()); @@ -442,7 +542,8 @@ public Object getAllMalaria(GetDiseaseRequestHandler getDiseaseRequestHandler) { dto.setDiarrhea(symptomsDTO.isDiarrhea()); } } catch (Exception e) { - throw new RuntimeException("Error parsing symptoms JSON for Malaria Disease ID: " + disease.getId(), e); + logger.error("Error parsing symptoms for diseaseId={}", + disease.getId(), e); } return dto; @@ -478,40 +579,87 @@ public Object getAllKalaAzar(GetDiseaseRequestHandler getDiseaseRequestHandler) // Fetch and filter Kala Azar disease records List filteredList = diseaseKalaAzarRepository.findAll().stream() - .filter(disease -> (Objects.equals(disease.getUserId(), getDiseaseRequestHandler.getUserId()))) + .filter(disease -> (Objects.equals(disease.getUserId(), getDiseaseRequestHandler.getAshaId()))) .collect(Collectors.toList()); // Check if the list is empty if (filteredList.isEmpty()) { - return Collections.singletonMap("message", "No data found for Kala Azar."); + return filteredList; } // Map to DTOs List dtoList = filteredList.stream().map(disease -> { + DiseaseKalaAzarDTO dto = new DiseaseKalaAzarDTO(); - dto.setId(disease.getId()); - dto.setBenId(disease.getBenId()); - dto.setHouseHoldDetailsId(disease.getHouseHoldDetailsId()); - dto.setVisitDate(disease.getVisitDate()); - dto.setBeneficiaryStatus(disease.getBeneficiaryStatus()); - dto.setDateOfDeath(disease.getDateOfDeath()); - dto.setPlaceOfDeath(disease.getPlaceOfDeath()); - dto.setOtherPlaceOfDeath(disease.getOtherPlaceOfDeath()); - dto.setReasonForDeath(disease.getReasonForDeath()); - dto.setOtherReasonForDeath(disease.getOtherReasonForDeath()); - dto.setKalaAzarCaseStatus(disease.getKalaAzarCaseStatus()); - dto.setKalaAzarCaseCount(disease.getKalaAzarCaseCount()); - dto.setRapidDiagnosticTest(disease.getRapidDiagnosticTest()); - dto.setDateOfRdt(disease.getDateOfRdt()); - dto.setFollowUpPoint(disease.getFollowUpPoint()); - dto.setReferredTo(disease.getReferredTo()); - dto.setOtherReferredFacility(disease.getOtherReferredFacility()); - dto.setCreatedDate(disease.getCreatedDate()); - dto.setCreatedBy(disease.getCreatedBy()); - dto.setBeneficiaryStatusId(disease.getBeneficiaryStatusId()); - dto.setReferToName(disease.getReferToName()); - dto.setUserId(disease.getUserId()); - dto.setDiseaseTypeId(disease.getDiseaseTypeId()); + + if (disease.getId() != null) + dto.setId(disease.getId()); + + if (disease.getBenId() != null) + dto.setBenId(disease.getBenId()); + + if (disease.getHouseHoldDetailsId() != null) + dto.setHouseHoldDetailsId(disease.getHouseHoldDetailsId()); + + if (disease.getVisitDate() != null) + dto.setVisitDate(disease.getVisitDate()); + + if (disease.getBeneficiaryStatus() != null && !disease.getBeneficiaryStatus().trim().isEmpty()) + dto.setBeneficiaryStatus(disease.getBeneficiaryStatus()); + + if (disease.getDateOfDeath() != null) + dto.setDateOfDeath(disease.getDateOfDeath()); + + if (disease.getPlaceOfDeath() != null && !disease.getPlaceOfDeath().trim().isEmpty()) + dto.setPlaceOfDeath(disease.getPlaceOfDeath()); + + if (disease.getOtherPlaceOfDeath() != null && !disease.getOtherPlaceOfDeath().trim().isEmpty()) + dto.setOtherPlaceOfDeath(disease.getOtherPlaceOfDeath()); + + if (disease.getReasonForDeath() != null && !disease.getReasonForDeath().trim().isEmpty()) + dto.setReasonForDeath(disease.getReasonForDeath()); + + if (disease.getOtherReasonForDeath() != null && !disease.getOtherReasonForDeath().trim().isEmpty()) + dto.setOtherReasonForDeath(disease.getOtherReasonForDeath()); + + if (disease.getKalaAzarCaseStatus() != null && !disease.getKalaAzarCaseStatus().trim().isEmpty()) + dto.setKalaAzarCaseStatus(disease.getKalaAzarCaseStatus()); + + if (disease.getKalaAzarCaseCount() != null) + dto.setKalaAzarCaseCount(disease.getKalaAzarCaseCount()); + + if (disease.getRapidDiagnosticTest() != null) + dto.setRapidDiagnosticTest(disease.getRapidDiagnosticTest()); + + if (disease.getDateOfRdt() != null) + dto.setDateOfRdt(disease.getDateOfRdt()); + + if (disease.getFollowUpPoint() != null) + dto.setFollowUpPoint(disease.getFollowUpPoint()); + + if (disease.getReferredTo() != null && !disease.getReferredTo().trim().isEmpty()) + dto.setReferredTo(disease.getReferredTo()); + + if (disease.getOtherReferredFacility() != null && !disease.getOtherReferredFacility().trim().isEmpty()) + dto.setOtherReferredFacility(disease.getOtherReferredFacility()); + + if (disease.getCreatedDate() != null) + dto.setCreatedDate(disease.getCreatedDate()); + + if (disease.getCreatedBy() != null && !disease.getCreatedBy().trim().isEmpty()) + dto.setCreatedBy(disease.getCreatedBy()); + + if (disease.getBeneficiaryStatusId() != null) + dto.setBeneficiaryStatusId(disease.getBeneficiaryStatusId()); + + if (disease.getReferToName() != null && !disease.getReferToName().trim().isEmpty()) + dto.setReferToName(disease.getReferToName()); + + if (disease.getUserId() != null) + dto.setUserId(disease.getUserId()); + + if (disease.getDiseaseTypeId() != null) + dto.setDiseaseTypeId(disease.getDiseaseTypeId()); return dto; }).collect(Collectors.toList()); @@ -522,17 +670,17 @@ public Object getAllKalaAzar(GetDiseaseRequestHandler getDiseaseRequestHandler) public Object getAllKalaAES(GetDiseaseRequestHandler getDiseaseRequestHandler) { if (diseaseAESJERepository.findAll().isEmpty()) { - return Collections.singletonMap("message", "No data found for AES."); + return diseaseAESJERepository.findAll(); } - return diseaseAESJERepository.findAll().stream().filter(diseaseAesje -> Objects.equals(diseaseAesje.getUserId(), getDiseaseRequestHandler.getUserId())).collect(Collectors.toList()); + return diseaseAESJERepository.findAll().stream().filter(diseaseAesje -> Objects.equals(diseaseAesje.getUserId(), getDiseaseRequestHandler.getAshaId())).collect(Collectors.toList()); } public Object getAllFilaria(GetDiseaseRequestHandler getDiseaseRequestHandler) { // Fetch and filter Filaria disease records - List filteredList = diseaseFilariasisRepository.findAll().stream().filter(screeningFilariasis -> Objects.equals(screeningFilariasis.getUserId(), getDiseaseRequestHandler.getUserId())).collect(Collectors.toList()); + List filteredList = diseaseFilariasisRepository.findAll().stream().filter(screeningFilariasis -> Objects.equals(screeningFilariasis.getUserId(), getDiseaseRequestHandler.getAshaId())).collect(Collectors.toList()); // Check if the list is empty if (filteredList.isEmpty()) { @@ -541,23 +689,58 @@ public Object getAllFilaria(GetDiseaseRequestHandler getDiseaseRequestHandler) { // Map to DTOs List dtoList = filteredList.stream().map(disease -> { + DiseaseFilariasisDTO dto = new DiseaseFilariasisDTO(); - dto.setId(disease.getId()); - dto.setBenId(disease.getBenId()); - dto.setHouseHoldDetailsId(disease.getHouseHoldDetailsId()); - dto.setSufferingFromFilariasis(disease.getSufferingFromFilariasis()); - dto.setAffectedBodyPart(disease.getAffectedBodyPart()); - dto.setMdaHomeVisitDate(disease.getMdaHomeVisitDate()); - dto.setDoseStatus(disease.getDoseStatus()); - dto.setFilariasisCaseCount(disease.getFilariasisCaseCount()); - dto.setOtherDoseStatusDetails(disease.getOtherDoseStatusDetails()); - dto.setMedicineSideEffect(disease.getMedicineSideEffect()); - dto.setOtherSideEffectDetails(disease.getOtherSideEffectDetails()); - dto.setCreatedDate(disease.getCreatedDate()); - dto.setCreatedBy(disease.getCreatedBy()); - dto.setUserId(disease.getUserId()); + + if (disease.getId() != null) + dto.setId(disease.getId()); + + if (disease.getBenId() != null) + dto.setBenId(disease.getBenId()); + + if (disease.getHouseHoldDetailsId() != null) + dto.setHouseHoldDetailsId(disease.getHouseHoldDetailsId()); + + if (disease.getSufferingFromFilariasis() != null) + dto.setSufferingFromFilariasis(disease.getSufferingFromFilariasis()); + + if (disease.getAffectedBodyPart() != null && + !disease.getAffectedBodyPart().trim().isEmpty()) + dto.setAffectedBodyPart(disease.getAffectedBodyPart()); + + if (disease.getMdaHomeVisitDate() != null) + dto.setMdaHomeVisitDate(disease.getMdaHomeVisitDate()); + + if (disease.getDoseStatus() != null && + !disease.getDoseStatus().trim().isEmpty()) + dto.setDoseStatus(disease.getDoseStatus()); + + if (disease.getFilariasisCaseCount() != null) + dto.setFilariasisCaseCount(disease.getFilariasisCaseCount()); + + if (disease.getOtherDoseStatusDetails() != null && + !disease.getOtherDoseStatusDetails().trim().isEmpty()) + dto.setOtherDoseStatusDetails(disease.getOtherDoseStatusDetails()); + + if (disease.getMedicineSideEffect() != null) + dto.setMedicineSideEffect(disease.getMedicineSideEffect()); + + if (disease.getOtherSideEffectDetails() != null && + !disease.getOtherSideEffectDetails().trim().isEmpty()) + dto.setOtherSideEffectDetails(disease.getOtherSideEffectDetails()); + + if (disease.getCreatedDate() != null) + dto.setCreatedDate(disease.getCreatedDate()); + + if (disease.getCreatedBy() != null && + !disease.getCreatedBy().trim().isEmpty()) + dto.setCreatedBy(disease.getCreatedBy()); + + if (disease.getUserId() != null) + dto.setUserId(disease.getUserId()); return dto; + }).collect(Collectors.toList()); return dtoList; @@ -568,30 +751,55 @@ public Object getAllLeprosy(GetDiseaseRequestHandler getDiseaseRequestHandler) { // Fetch and filter Leprosy disease records List filteredList = diseaseLeprosyRepository.findAll().stream() - .filter(disease -> Objects.equals(disease.getUserId(), getDiseaseRequestHandler.getUserId())) + .filter(disease -> Objects.equals(disease.getUserId(), getDiseaseRequestHandler.getAshaId())) .collect(Collectors.toList()); // Check if the list is empty if (filteredList.isEmpty()) { - return Collections.singletonMap("message", "No data found for Leprosy."); + return filteredList; } // Map to DTOs List dtoList = filteredList.stream().map(disease -> { DiseaseLeprosyDTO dto = new DiseaseLeprosyDTO(); - dto.setId(disease.getId()); - dto.setBenId(disease.getBenId()); - dto.setHouseHoldDetailsId(disease.getHouseHoldDetailsId()); - dto.setHomeVisitDate(disease.getHomeVisitDate()); - dto.setLeprosyStatus(disease.getLeprosyStatus()); - dto.setReferredTo(disease.getReferredTo()); - dto.setOtherReferredTo(disease.getOtherReferredTo()); - dto.setLeprosyStatusDate(disease.getLeprosyStatusDate()); - dto.setTypeOfLeprosy(disease.getTypeOfLeprosy()); - dto.setFollowUpDate(disease.getFollowUpDate()); - dto.setBeneficiaryStatus(disease.getLeprosyStatus()); - dto.setRemark(disease.getRemark()); - dto.setUserId(disease.getUserId()); + if (disease.getId() != null) + dto.setId(disease.getId()); + + if (disease.getBenId() != null) + dto.setBenId(disease.getBenId()); + + if (disease.getHouseHoldDetailsId() != null) + dto.setHouseHoldDetailsId(disease.getHouseHoldDetailsId()); + + if (disease.getHomeVisitDate() != null) + dto.setHomeVisitDate(disease.getHomeVisitDate()); + + if (disease.getLeprosyStatus() != null && !disease.getLeprosyStatus().trim().isEmpty()) + dto.setLeprosyStatus(disease.getLeprosyStatus()); + + if (disease.getReferredTo() != null && !disease.getReferredTo().trim().isEmpty()) + dto.setReferredTo(disease.getReferredTo()); + + if (disease.getOtherReferredTo() != null && !disease.getOtherReferredTo().trim().isEmpty()) + dto.setOtherReferredTo(disease.getOtherReferredTo()); + + if (disease.getLeprosyStatusDate() != null) + dto.setLeprosyStatusDate(disease.getLeprosyStatusDate()); + + if (disease.getTypeOfLeprosy() != null && !disease.getTypeOfLeprosy().trim().isEmpty()) + dto.setTypeOfLeprosy(disease.getTypeOfLeprosy()); + + if (disease.getFollowUpDate() != null) + dto.setFollowUpDate(disease.getFollowUpDate()); + + if (disease.getLeprosyStatus() != null && !disease.getLeprosyStatus().trim().isEmpty()) + dto.setBeneficiaryStatus(disease.getLeprosyStatus()); + + if (disease.getRemark() != null && !disease.getRemark().trim().isEmpty()) + dto.setRemark(disease.getRemark()); + + if (disease.getUserId() != null) + dto.setUserId(disease.getUserId()); return dto; @@ -602,7 +810,7 @@ public Object getAllLeprosy(GetDiseaseRequestHandler getDiseaseRequestHandler) { private ScreeningKalaAzar saveKalaAzarDisease(DiseaseKalaAzarDTO dto) { - logger.info("KalaAzarRequest: "+dto); + logger.info("KalaAzarRequest: " + dto); ScreeningKalaAzar entity = new ScreeningKalaAzar(); entity.setBenId(dto.getBenId()); @@ -687,7 +895,10 @@ private ScreeningLeprosy saveLeprosyData(DiseaseLeprosyDTO diseaseControlData) { diseaseLeprosy.setDiseaseTypeId(diseaseControlData.getDiseaseTypeId()); diseaseLeprosy.setOtherReferredTo(diseaseControlData.getOtherReferredTo()); diseaseLeprosy.setLeprosyStatusDate(diseaseControlData.getLeprosyStatusDate()); - diseaseLeprosy.setTypeOfLeprosy(diseaseControlData.getTypeOfLeprosy()); + if(diseaseControlData.getTypeOfLeprosy()!=null){ + diseaseLeprosy.setTypeOfLeprosy(diseaseControlData.getTypeOfLeprosy()); + + } diseaseLeprosy.setFollowUpDate(diseaseControlData.getFollowUpDate()); diseaseLeprosy.setBeneficiaryStatus(diseaseControlData.getBeneficiaryStatus()); diseaseLeprosy.setBeneficiaryStatusId(diseaseControlData.getBeneficiaryStatusId()); @@ -717,6 +928,33 @@ private ScreeningLeprosy saveLeprosyData(DiseaseLeprosyDTO diseaseControlData) { diseaseLeprosy.setModifiedBy(diseaseControlData.getModifiedBy()); diseaseLeprosy.setLastModDate(diseaseControlData.getLastModDate()); + diseaseLeprosy.setRecurrentUlcerationId(diseaseControlData.getRecurrentUlcerationId()); + diseaseLeprosy.setRecurrentTinglingId(diseaseControlData.getRecurrentTinglingId()); + diseaseLeprosy.setHypopigmentedPatchId(diseaseControlData.getHypopigmentedPatchId()); + diseaseLeprosy.setThickenedSkinId(diseaseControlData.getThickenedSkinId()); + diseaseLeprosy.setSkinNodulesId(diseaseControlData.getSkinNodulesId()); + diseaseLeprosy.setSkinPatchDiscolorationId(diseaseControlData.getSkinPatchDiscolorationId()); + diseaseLeprosy.setRecurrentNumbnessId(diseaseControlData.getRecurrentNumbnessId()); + diseaseLeprosy.setClawingFingersId(diseaseControlData.getClawingFingersId()); + diseaseLeprosy.setTinglingNumbnessExtremitiesId(diseaseControlData.getTinglingNumbnessExtremitiesId()); + diseaseLeprosy.setInabilityCloseEyelidId(diseaseControlData.getInabilityCloseEyelidId()); + diseaseLeprosy.setDifficultyHoldingObjectsId(diseaseControlData.getDifficultyHoldingObjectsId()); + diseaseLeprosy.setWeaknessFeetId(diseaseControlData.getWeaknessFeetId()); + + diseaseLeprosy.setRecurrentUlceration(diseaseControlData.getRecurrentUlceration()); + diseaseLeprosy.setRecurrentTingling(diseaseControlData.getRecurrentTingling()); + diseaseLeprosy.setHypopigmentedPatch(diseaseControlData.getHypopigmentedPatch()); + diseaseLeprosy.setThickenedSkin(diseaseControlData.getThickenedSkin()); + diseaseLeprosy.setSkinNodules(diseaseControlData.getSkinNodules()); + diseaseLeprosy.setSkinPatchDiscoloration(diseaseControlData.getSkinPatchDiscoloration()); + diseaseLeprosy.setRecurrentNumbness(diseaseControlData.getRecurrentNumbness()); + diseaseLeprosy.setClawingFingers(diseaseControlData.getClawingFingers()); + diseaseLeprosy.setTinglingNumbnessExtremities(diseaseControlData.getTinglingNumbnessExtremities()); + diseaseLeprosy.setInabilityCloseEyelid(diseaseControlData.getInabilityCloseEyelid()); + diseaseLeprosy.setDifficultyHoldingObjects(diseaseControlData.getDifficultyHoldingObjects()); + diseaseLeprosy.setWeaknessFeet(diseaseControlData.getWeaknessFeet()); + + return diseaseLeprosy; } @@ -761,6 +999,33 @@ private String updateLeprosyData(DiseaseLeprosyDTO diseaseControlData) { existingDiseaseLeprosy.setModifiedBy(diseaseControlData.getModifiedBy()); existingDiseaseLeprosy.setLastModDate(diseaseControlData.getLastModDate()); + existingDiseaseLeprosy.setRecurrentUlcerationId(diseaseControlData.getRecurrentUlcerationId()); + existingDiseaseLeprosy.setRecurrentTinglingId(diseaseControlData.getRecurrentTinglingId()); + existingDiseaseLeprosy.setHypopigmentedPatchId(diseaseControlData.getHypopigmentedPatchId()); + existingDiseaseLeprosy.setThickenedSkinId(diseaseControlData.getThickenedSkinId()); + existingDiseaseLeprosy.setSkinNodulesId(diseaseControlData.getSkinNodulesId()); + existingDiseaseLeprosy.setSkinPatchDiscolorationId(diseaseControlData.getSkinPatchDiscolorationId()); + existingDiseaseLeprosy.setRecurrentNumbnessId(diseaseControlData.getRecurrentNumbnessId()); + existingDiseaseLeprosy.setClawingFingersId(diseaseControlData.getClawingFingersId()); + existingDiseaseLeprosy.setTinglingNumbnessExtremitiesId(diseaseControlData.getTinglingNumbnessExtremitiesId()); + existingDiseaseLeprosy.setInabilityCloseEyelidId(diseaseControlData.getInabilityCloseEyelidId()); + existingDiseaseLeprosy.setDifficultyHoldingObjectsId(diseaseControlData.getDifficultyHoldingObjectsId()); + existingDiseaseLeprosy.setWeaknessFeetId(diseaseControlData.getWeaknessFeetId()); + + existingDiseaseLeprosy.setRecurrentUlceration(diseaseControlData.getRecurrentUlceration()); + existingDiseaseLeprosy.setRecurrentTingling(diseaseControlData.getRecurrentTingling()); + existingDiseaseLeprosy.setHypopigmentedPatch(diseaseControlData.getHypopigmentedPatch()); + existingDiseaseLeprosy.setThickenedSkin(diseaseControlData.getThickenedSkin()); + existingDiseaseLeprosy.setSkinNodules(diseaseControlData.getSkinNodules()); + existingDiseaseLeprosy.setSkinPatchDiscoloration(diseaseControlData.getSkinPatchDiscoloration()); + existingDiseaseLeprosy.setRecurrentNumbness(diseaseControlData.getRecurrentNumbness()); + existingDiseaseLeprosy.setClawingFingers(diseaseControlData.getClawingFingers()); + existingDiseaseLeprosy.setTinglingNumbnessExtremities(diseaseControlData.getTinglingNumbnessExtremities()); + existingDiseaseLeprosy.setInabilityCloseEyelid(diseaseControlData.getInabilityCloseEyelid()); + existingDiseaseLeprosy.setDifficultyHoldingObjects(diseaseControlData.getDifficultyHoldingObjects()); + existingDiseaseLeprosy.setWeaknessFeet(diseaseControlData.getWeaknessFeet()); + + diseaseLeprosyRepository.save(existingDiseaseLeprosy); // Return the updated entity return "Data update successfully"; @@ -789,6 +1054,8 @@ private ScreeningMalaria saveMalariaDisease(DiseaseMalariaDTO requestData) { diseaseScreening.setSlideTestPv(requestData.getSlideTestPv()); diseaseScreening.setDateOfSlideTest(requestData.getDateOfSlideTest()); diseaseScreening.setSlideNo(requestData.getSlideNo()); + diseaseScreening.setMalariaTestType(requestData.getMalariaTestType()); + diseaseScreening.setMalariaSlideTestType(requestData.getMalariaSlideTestType()); diseaseScreening.setReferredTo(requestData.getReferredTo()); diseaseScreening.setOtherReferredFacility(requestData.getOtherReferredFacility()); diseaseScreening.setRemarks(requestData.getRemarks()); @@ -823,6 +1090,8 @@ private String updateMalariaDisease(DiseaseMalariaDTO requestData) { diseaseScreening.setSlideNo(requestData.getSlideNo()); diseaseScreening.setReferredTo(requestData.getReferredTo()); diseaseScreening.setOtherReferredFacility(requestData.getOtherReferredFacility()); + diseaseScreening.setMalariaSlideTestType(requestData.getMalariaSlideTestType()); + diseaseScreening.setMalariaTestType(requestData.getMalariaTestType()); diseaseScreening.setRemarks(requestData.getRemarks()); diseaseScreening.setCreatedDate(Timestamp.valueOf(LocalDateTime.now())); diseaseScreening.setDateOfVisitBySupervisor(requestData.getDateOfVisitBySupervisor()); @@ -861,8 +1130,10 @@ public List saveMosquitoMobilizationNet(List mos List entityList = mosquitoNetDTOList.stream().map(dto -> { MosquitoNetEntity entity = new MosquitoNetEntity(); + if(!beneficiaryRepo.findByHouseoldId(dto.getHouseHoldId()).isEmpty()){ + entity.setBeneficiaryId(beneficiaryRepo.findByHouseoldId(dto.getHouseHoldId()).get(0).getBenficieryid()); - entity.setBeneficiaryId(dto.getBeneficiaryId()); + } entity.setHouseHoldId(dto.getHouseHoldId()); // ✅ String → LocalDate conversion @@ -887,12 +1158,10 @@ public List saveMosquitoMobilizationNet(List mos }).collect(Collectors.toList()); - // ✅ Save all List savedEntities = mosquitoNetRepository.saveAll(entityList); - // ✅ Entity → DTO return return savedEntities.stream().map(entity -> { @@ -913,7 +1182,7 @@ public List saveMosquitoMobilizationNet(List mos if (dto.getFields() != null) { dto.getFields().setIs_net_distributed(entity.getIsNetDistributed()); } - + checkAndAddIncentives(entity); return dto; }).collect(Collectors.toList()); @@ -940,62 +1209,337 @@ public List getAllMosquitoMobilizationNet(Integer userId) { mosquitoNetListDTO.setIs_net_distributed(entity.getIsNetDistributed()); mosquitoNetListDTO.setVisit_date(entity.getVisitDate().format(formatter)); - dto.setFields(mosquitoNetListDTO); + dto.setFields(mosquitoNetListDTO); + return dto; }).collect(Collectors.toList()); } + private void checkAndAddIncentives(MosquitoNetEntity mosquitoNetEntity) { + Integer stateId = userService.getUserDetail(mosquitoNetEntity.getUserId()).getStateId(); + IncentiveActivity activityMobilizingMosquitoNets = incentivesRepo.findIncentiveMasterByNameAndGroup("MOSQUITO_NET_DISTRIBUTION_MOBILIZATION", GroupName.ACTIVITY.getDisplayName()); + if(stateId.equals(StateCode.CG.getStateCode())){ + addIncentive(activityMobilizingMosquitoNets, mosquitoNetEntity); + } + } private void checkAndAddIncentives(ScreeningMalaria diseaseScreening) { - IncentiveActivity diseaseScreeningActivity; - if (Objects.equals(diseaseScreening.getCaseStatus(), "Confirmed Case")) { - diseaseScreeningActivity = incentivesRepo.findIncentiveMasterByNameAndGroup("MALARIA_1", "DISEASECONTROL"); + Integer stateId = userService.getUserDetail(diseaseScreening.getUserId()).getStateId(); + IncentiveActivity diseaseScreeningActivity = incentivesRepo.findIncentiveMasterByNameAndGroup("NVBDCP_MALARIA_TREATMENT", GroupName.UMBRELLA_PROGRAMMES.getDisplayName()); + IncentiveActivity diseaseScreeningActivityCG = incentivesRepo.findIncentiveMasterByNameAndGroup("NVBDCP_MALARIA_TREATMENT", GroupName.ACTIVITY.getDisplayName()); + IncentiveActivity incentiveActivityForCollectSlideAM = incentivesRepo.findIncentiveMasterByNameAndGroup("NVBDCP_SLIDE_COLLECTION", GroupName.UMBRELLA_PROGRAMMES.getDisplayName()); + IncentiveActivity incentiveActivityForCollectSlideCG = incentivesRepo.findIncentiveMasterByNameAndGroup("NVBDCP_SLIDE_COLLECTION", GroupName.ACTIVITY.getDisplayName()); + + if (diseaseScreeningActivity != null) { + if(stateId.equals(StateCode.AM.getStateCode())){ + if (Objects.equals(diseaseScreening.getCaseStatus(), "Confirmed")) { + addIncentive(diseaseScreeningActivity, diseaseScreening); - } else { - diseaseScreeningActivity = incentivesRepo.findIncentiveMasterByNameAndGroup("MALARIA_2", "DISEASECONTROL"); + } + } + if(stateId.equals(StateCode.CG.getStateCode())){ + if (Objects.equals(diseaseScreening.getCaseStatus(), "Confirmed")) { + addIncentive(diseaseScreeningActivityCG, diseaseScreening); + + } + } } + if (diseaseScreening.getCaseStatus().equals("Suspected")) { + if (!diseaseScreening.getMalariaTestType().isEmpty()) { + if (stateId.equals(StateCode.AM.getStateCode())) { + if (incentiveActivityForCollectSlideAM != null) { + addIncentive(incentiveActivityForCollectSlideAM, diseaseScreening); - if (diseaseScreeningActivity != null) { + } + } + + if (stateId.equals(StateCode.CG.getStateCode())) { + if (incentiveActivityForCollectSlideCG != null) { + addIncentive(incentiveActivityForCollectSlideCG, diseaseScreening); + + } + } + + + } + + } + if (diseaseScreening.getCaseStatus().equals("Suspected")) { + if (!diseaseScreening.getMalariaTestType().isEmpty()) { + if (stateId.equals(StateCode.AM.getStateCode())) { + if (incentiveActivityForCollectSlideAM != null) { + addIncentive(incentiveActivityForCollectSlideAM, diseaseScreening); + + } + } + + if (stateId.equals(StateCode.CG.getStateCode())) { + if (incentiveActivityForCollectSlideCG != null) { + addIncentive(incentiveActivityForCollectSlideCG, diseaseScreening); + + } + } + + + } + + } + } + private void addIncentive(IncentiveActivity diseaseScreeningActivity, MosquitoNetEntity diseaseScreening) { + + Timestamp visitTimestamp = + Timestamp.valueOf(diseaseScreening.getVisitDate().atStartOfDay()); + IncentiveActivityRecord record = recordRepo + .findRecordByActivityIdCreatedDateBenId( + diseaseScreeningActivity.getId(), + visitTimestamp, + diseaseScreening.getBeneficiaryId(),diseaseScreening.getUserId()); + + if (record == null) { + + record = new IncentiveActivityRecord(); + + record.setActivityId(diseaseScreeningActivity.getId()); + record.setCreatedDate(visitTimestamp); + record.setCreatedBy(userService.getUserDetail(diseaseScreening.getUserId()).getUserName()); + record.setStartDate(visitTimestamp); + record.setEndDate(visitTimestamp); + record.setUpdatedDate(visitTimestamp); + record.setUpdatedBy(userService.getUserDetail(diseaseScreening.getUserId()).getUserName()); + record.setBenId(diseaseScreening.getBeneficiaryId().longValue()); + record.setAshaId(diseaseScreening.getUserId()); + record.setAmount(Long.valueOf(diseaseScreeningActivity.getRate())); + record.setIsEligible(true); + + recordRepo.save(record); + } + + } + + + private void addIncentive(IncentiveActivity diseaseScreeningActivity, ScreeningMalaria diseaseScreening) { + try { + Timestamp screeningTimestamp = diseaseScreening.getScreeningDate() != null + ? new Timestamp(diseaseScreening.getScreeningDate().getTime()) + : null; IncentiveActivityRecord record = recordRepo - .findRecordByActivityIdCreatedDateBenId(diseaseScreeningActivity.getId(), Timestamp.valueOf(diseaseScreening.getCreatedDate().toString()), diseaseScreening.getBenId().longValue()); + .findRecordByActivityIdCreatedDateBenId(diseaseScreeningActivity.getId(), Timestamp.valueOf(diseaseScreening.getCreatedDate().toString()), diseaseScreening.getBenId().longValue(),diseaseScreening.getUserId()); if (record == null) { - if (Objects.equals(diseaseScreening.getCaseStatus(), "Confirmed Case")) { - record = new IncentiveActivityRecord(); - record.setActivityId(diseaseScreeningActivity.getId()); - record.setCreatedDate(Timestamp.valueOf(diseaseScreening.getCreatedDate().toString())); - record.setCreatedBy(diseaseScreening.getCreatedBy()); - record.setStartDate(Timestamp.valueOf(diseaseScreening.getCreatedDate().toString())); - record.setEndDate(Timestamp.valueOf(diseaseScreening.getCreatedDate().toString())); - record.setUpdatedDate(Timestamp.valueOf(diseaseScreening.getCreatedDate().toString())); - record.setUpdatedBy(diseaseScreening.getCreatedBy()); - record.setBenId(diseaseScreening.getBenId().longValue()); - record.setAshaId(diseaseScreening.getUserId()); - record.setName(diseaseScreeningActivity.getName()); - record.setAmount(Long.valueOf(diseaseScreeningActivity.getRate())); - recordRepo.save(record); - } else { - record = new IncentiveActivityRecord(); - record.setActivityId(diseaseScreeningActivity.getId()); - record.setCreatedDate(Timestamp.valueOf(diseaseScreening.getCreatedDate().toString())); - record.setCreatedBy(diseaseScreening.getCreatedBy()); - record.setStartDate(Timestamp.valueOf(diseaseScreening.getCreatedDate().toString())); - record.setEndDate(Timestamp.valueOf(diseaseScreening.getCreatedDate().toString())); - record.setUpdatedDate(Timestamp.valueOf(diseaseScreening.getCreatedDate().toString())); - record.setUpdatedBy(diseaseScreening.getCreatedBy()); - record.setBenId(diseaseScreening.getBenId().longValue()); - record.setName(diseaseScreeningActivity.getName()); - record.setAshaId(diseaseScreening.getUserId()); - record.setAmount(Long.valueOf(diseaseScreeningActivity.getRate())); - recordRepo.save(record); + record = new IncentiveActivityRecord(); + record.setActivityId(diseaseScreeningActivity.getId()); + record.setCreatedDate(screeningTimestamp); + record.setCreatedBy(userService.getUserDetail(diseaseScreening.getUserId()).getUserName()); + record.setStartDate(screeningTimestamp); + record.setEndDate(screeningTimestamp); + record.setUpdatedDate(screeningTimestamp); + record.setUpdatedBy(userService.getUserDetail(diseaseScreening.getUserId()).getUserName()); + record.setUpdatedBy(userService.getUserDetail(diseaseScreening.getUserId()).getUserName()); + record.setBenId(diseaseScreening.getBenId().longValue()); + record.setAshaId(diseaseScreening.getUserId()); + record.setAmount(Long.valueOf(diseaseScreeningActivity.getRate())); + record.setIsEligible(true); + recordRepo.save(record); + logger.info("Incentive for {}"+diseaseScreeningActivity.getDescription()); + + } + }catch (Exception e){ + logger.info("Fail to generate Incentive for {}"+diseaseScreeningActivity.getDescription()+ "Exception"+e.getMessage()); + + } + + } + + + @Override + public List saveChronicDiseaseVisit( + List requestList, String token) throws IEMRException { + Integer userId = jwtUtil.extractUserId(token); + String userName= userRepo.getUserNamedByUserId(userId); + + List responseList = new ArrayList<>(); + + for (ChronicDiseaseVisitDTO dto : requestList) { + + ChronicDiseaseVisitEntity entity = new ChronicDiseaseVisitEntity(); + + entity.setBenId(dto.getBenId()); + entity.setHhId(dto.getHhId()); + entity.setFormId(dto.getFormId()); + entity.setVersion(dto.getVersion()); + entity.setVisitNo(dto.getVisitNo()); + entity.setFollowUpNo(dto.getFollowUpNo()); + if (dto.getFollowUpDate() != null) { + entity.setFollowUpDate(dto.getFollowUpDate()); + + } + entity.setDiagnosisCodes(dto.getDiagnosisCodes()); + entity.setFormDataJson(dto.getFormDataJson()); + entity.setUserID(userId); + entity.setCreatedBy(userName); + entity.setUpdatedBy(userId); + + + if (dto.getTreatmentStartDate() != null) { + entity.setTreatmentStartDate( + LocalDate.parse(dto.getTreatmentStartDate()) + ); + } + + ChronicDiseaseVisitEntity savedEntity = + chronicDiseaseVisitRepository.save(entity); + + dto.setId(savedEntity.getId()); + responseList.add(dto); + checkIncentive(savedEntity, savedEntity.getUserID()); + + + } + + return responseList; + } + + private void checkIncentive(ChronicDiseaseVisitEntity chronicDiseaseVisitEntity, Integer ashaId) { + String userName = userRepo.getUserNamedByUserId(ashaId); + Integer stateId = userService.getUserDetail(ashaId).getStateId(); + IncentiveActivity incentiveActivity = incentivesRepo.findIncentiveMasterByNameAndGroup("NCD_FOLLOWUP_TREATMENT", GroupName.NCD.getDisplayName()); + IncentiveActivity incentiveActivityCG = incentivesRepo.findIncentiveMasterByNameAndGroup("NCD_FOLLOWUP_TREATMENT", GroupName.ACTIVITY.getDisplayName()); + IncentiveActivity incentiveActivityCGForNCDnewPatient = incentivesRepo.findIncentiveMasterByNameAndGroup("NCD_NEW_PATIENT_MEDICATION_SUPPORT", GroupName.ACTIVITY.getDisplayName()); + logger.info("incentiveActivity:" + incentiveActivity.getId()); + + if (chronicDiseaseVisitEntity.getFollowUpNo() != null + && chronicDiseaseVisitEntity.getCreatedDate() != null + && chronicDiseaseVisitEntity.getDiagnosisCodes() != null) { + + List targetDiseases = Arrays.asList( + "Hypertension (BP)", + "Diabetes (DM)", + "Cancer" + ); + + + List diagnosisList = Arrays.asList( + chronicDiseaseVisitEntity.getDiagnosisCodes().split(",") + ); + + boolean matchFound = diagnosisList.stream() + .map(String::trim) + .anyMatch(targetDiseases::contains); + if(matchFound & Integer.valueOf(0).equals(chronicDiseaseVisitEntity.getFollowUpNo()) ){ + LocalDateTime localDateTime = chronicDiseaseVisitEntity.getTreatmentStartDate().atStartOfDay(); + + Timestamp treatmentStartDate = Timestamp.valueOf(localDateTime); + addNCDFolloupIncentiveRecord( + incentiveActivityCGForNCDnewPatient, + ashaId, + chronicDiseaseVisitEntity.getBenId(), + treatmentStartDate, + userName + ); + } + if (matchFound && Integer.valueOf(6).equals(chronicDiseaseVisitEntity.getFollowUpNo())) { + LocalDateTime localDateTime = chronicDiseaseVisitEntity.getFollowUpDate().atStartOfDay(); + + Timestamp followUpTimestamp = Timestamp.valueOf(localDateTime); + if(stateId.equals(StateCode.AM.getStateCode())){ + addNCDFolloupIncentiveRecord( + incentiveActivity, + ashaId, + chronicDiseaseVisitEntity.getBenId(), + followUpTimestamp, + userName + ); + } + if(stateId.equals(StateCode.CG.getStateCode())){ + addNCDFolloupIncentiveRecord( + incentiveActivityCG, + ashaId, + chronicDiseaseVisitEntity.getBenId(), + followUpTimestamp, + userName + ); + } + + } + } + + + + } + + private void addNCDFolloupIncentiveRecord(IncentiveActivity incentiveActivity, Integer ashaId, + Long benId, Timestamp createdDate, String userName) { + try { + IncentiveActivityRecord record = recordRepo + .findRecordByActivityIdCreatedDateBenId(incentiveActivity.getId(), createdDate, benId,ashaId); + + Timestamp now = Timestamp.valueOf(LocalDateTime.now()); + if (record == null) { + record = new IncentiveActivityRecord(); + record.setActivityId(incentiveActivity.getId()); + record.setCreatedDate(createdDate); + record.setCreatedBy(userName); + record.setStartDate(createdDate); + record.setEndDate(createdDate); + record.setUpdatedDate(now); + record.setUpdatedBy(userName); + record.setBenId(benId); + record.setAshaId(ashaId); + record.setAmount(Long.valueOf(incentiveActivity.getRate())); + record.setIsEligible(true); + recordRepo.save(record); + } + } catch (Exception e) { + logger.error("Fail to save IncentiveActivityRecord " + e.getMessage()); + } + + + } + + @Override + public List getCdtfVisits(GetBenRequestHandler getBenRequestHandler) { + List dtoList = new ArrayList<>(); + + try { + + List entityList = chronicDiseaseVisitRepository.findByUserID(getBenRequestHandler.getAshaId()); + + + for (ChronicDiseaseVisitEntity entity : entityList) { + + ChronicDiseaseVisitDTO dto = new ChronicDiseaseVisitDTO(); + + dto.setId(entity.getId()); + dto.setBenId(entity.getBenId()); + dto.setHhId(entity.getHhId()); + dto.setFormId(entity.getFormId()); + dto.setVersion(entity.getVersion()); + dto.setVisitNo(entity.getVisitNo()); + dto.setFollowUpNo(entity.getFollowUpNo()); + if (entity.getFollowUpDate() != null) { + dto.setFollowUpDate(entity.getFollowUpDate()); + } + dto.setDiagnosisCodes(entity.getDiagnosisCodes()); + dto.setFormDataJson(entity.getFormDataJson()); + if (entity.getTreatmentStartDate() != null) { + dto.setTreatmentStartDate( + entity.getTreatmentStartDate().toString() + ); + } + checkIncentive(entity, entity.getUserID()); + dtoList.add(dto); } + } catch (Exception e) { + } + + return dtoList; } } \ No newline at end of file diff --git a/src/main/java/com/iemr/flw/service/impl/EmployeeMasterImpl.java b/src/main/java/com/iemr/flw/service/impl/EmployeeMasterImpl.java index 2f67fc2b..acf7836c 100644 --- a/src/main/java/com/iemr/flw/service/impl/EmployeeMasterImpl.java +++ b/src/main/java/com/iemr/flw/service/impl/EmployeeMasterImpl.java @@ -1,6 +1,6 @@ package com.iemr.flw.service.impl; -import com.iemr.flw.domain.iemr.M_User; +import com.iemr.flw.domain.iemr.User; import com.iemr.flw.repo.iemr.EmployeeMasterRepo; import com.iemr.flw.service.EmployeeMasterInter; @@ -19,13 +19,13 @@ public class EmployeeMasterImpl implements EmployeeMasterInter { @Override - public M_User getUserDetails(Integer userID) { + public User getUserDetails(Integer userID) { logger.debug("Fetching user details for userID: {}", userID); try { if (userID == null) { throw new IllegalArgumentException("UserID cannot be null"); } - return employeeMasterRepo.findByUserID(userID); + return employeeMasterRepo.findUserByUserID(userID); } catch (Exception e) { logger.error("Error fetching user details for userID: {}", userID, e); throw e; @@ -33,7 +33,7 @@ public M_User getUserDetails(Integer userID) { } @Override - public List getAllUsers() { + public List getAllUsers() { return employeeMasterRepo.findAll(); } } diff --git a/src/main/java/com/iemr/flw/service/impl/FacilityDataService.java b/src/main/java/com/iemr/flw/service/impl/FacilityDataService.java new file mode 100644 index 00000000..2e4f9556 --- /dev/null +++ b/src/main/java/com/iemr/flw/service/impl/FacilityDataService.java @@ -0,0 +1,186 @@ +package com.iemr.flw.service.impl; + +import com.iemr.flw.domain.iemr.AshaSupervisorMapping; +import com.iemr.flw.repo.iemr.AshaSupervisorLoginRepo; +import com.iemr.flw.repo.iemr.FacilityLoginRepo; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.*; + +@Service +public class FacilityDataService { + + private final Logger logger = LoggerFactory.getLogger(this.getClass().getName()); + + @Autowired + private AshaSupervisorLoginRepo ashaSupervisorLoginRepo; + + @Autowired + private FacilityLoginRepo facilityLoginRepo; + + public Map buildFacilityData(Integer userID, String roleName) { + Map result = new HashMap<>(); + result.put("location", emptyLocation()); + try { + if ("ASHA Supervisor".equalsIgnoreCase(roleName)) { + enrichAshaSupervisorData(result, userID); + } else if ("ASHA".equalsIgnoreCase(roleName)) { + enrichAshaData(result, userID); + } else { + enrichGeneralFacilityData(result, userID); + } + } catch (Exception e) { + logger.error("Error building facilityData for userID " + userID + ": " + e.getMessage(), e); + } + return result; + } + + private void enrichAshaSupervisorData(Map result, Integer supervisorUserID) { + ArrayList mappings = ashaSupervisorLoginRepo + .findBySupervisorUserIDAndDeletedFalse(supervisorUserID); + if (mappings == null || mappings.isEmpty()) return; + + Set facilityIDSet = new HashSet<>(); + for (AshaSupervisorMapping m : mappings) { + if (m.getFacilityID() != null) facilityIDSet.add(m.getFacilityID()); + } + if (facilityIDSet.isEmpty()) return; + List facilityIDs = new ArrayList<>(facilityIDSet); + + List facilityRows = facilityLoginRepo.getFacilityDetails(facilityIDs); + if (facilityRows == null || facilityRows.isEmpty()) return; + + result.put("location", buildLocation(facilityRows.get(0))); + + List> facilities = new ArrayList<>(); + for (Object[] row : facilityRows) { + Map facility = new HashMap<>(); + facility.put("facilityId", row[0]); + facility.put("facilityName", str(row[1])); + facility.put("facilityType", str(row[9])); + facilities.add(facility); + } + result.put("facilities", facilities); + + List ashaRows = facilityLoginRepo.getMappedAshasBySupervisor(supervisorUserID); + List> ashaList = buildAshaList(ashaRows); + result.put("ashaList", ashaList); + result.put("totalAshaCount", ashaList.size()); + } + + private void enrichAshaData(Map result, Integer ashaUserID) { + List facilityIDs = facilityLoginRepo.getUserFacilityIDs(ashaUserID); + if (facilityIDs == null || facilityIDs.isEmpty()) return; + + List facilityRows = facilityLoginRepo.getFacilityDetails(facilityIDs); + if (facilityRows == null || facilityRows.isEmpty()) return; + + result.put("location", buildLocation(facilityRows.get(0))); + + Object[] row = facilityRows.get(0); + Map facility = new HashMap<>(); + facility.put("facilityId", row[0]); + facility.put("facilityName", str(row[1])); + facility.put("facilityType", str(row[9])); + result.put("facility", facility); + + List supervisorRows = facilityLoginRepo.getSupervisorForAsha(ashaUserID); + if (supervisorRows != null && !supervisorRows.isEmpty()) { + Object[] sRow = supervisorRows.get(0); + Map supervisor = new HashMap<>(); + supervisor.put("userId", sRow[0]); + supervisor.put("fullName", fullName(sRow[1], sRow[2])); + supervisor.put("mobile", str(sRow[3])); + supervisor.put("employeeId", str(sRow[4]).isEmpty() ? null : str(sRow[4])); + result.put("supervisor", supervisor); + } else { + result.put("supervisor", null); + } + + List peerRows = facilityLoginRepo.getPeersAtFacility(facilityIDs, ashaUserID); + List> peers = new ArrayList<>(); + if (peerRows != null) { + for (Object[] pRow : peerRows) { + Map peer = new HashMap<>(); + peer.put("userId", pRow[0]); + peer.put("fullName", fullName(pRow[1], pRow[2])); + peer.put("role", str(pRow[3])); + peer.put("employeeId", str(pRow[4]).isEmpty() ? null : str(pRow[4])); + peer.put("mobile", str(pRow[5]).isEmpty() ? null : str(pRow[5])); + peers.add(peer); + } + } + result.put("peersAtFacility", peers); + } + + private void enrichGeneralFacilityData(Map result, Integer userID) { + List facilityIDs = facilityLoginRepo.getUserFacilityIDs(userID); + if (facilityIDs == null || facilityIDs.isEmpty()) return; + + List facilityRows = facilityLoginRepo.getFacilityDetails(facilityIDs); + if (facilityRows == null || facilityRows.isEmpty()) return; + + result.put("location", buildLocation(facilityRows.get(0))); + + List> facilities = new ArrayList<>(); + for (Object[] row : facilityRows) { + Map facility = new HashMap<>(); + facility.put("facilityId", row[0]); + facility.put("facilityName", str(row[1])); + facility.put("facilityType", str(row[9])); + facilities.add(facility); + } + result.put("facilities", facilities); + + List ashaRows = facilityLoginRepo.getAshaListByFacilities(facilityIDs); + List> ashaList = buildAshaList(ashaRows); + result.put("ashaList", ashaList); + result.put("totalAshaCount", ashaList.size()); + } + + private List> buildAshaList(List rows) { + List> list = new ArrayList<>(); + if (rows == null) return list; + for (Object[] row : rows) { + Map asha = new HashMap<>(); + asha.put("userId", row[0]); + asha.put("fullName", fullName(row[1], row[2])); + asha.put("employeeId", str(row[3]).isEmpty() ? null : str(row[3])); + asha.put("facilityId", row[4]); + asha.put("facilityName", str(row[5])); + asha.put("facilityType", str(row[6])); + asha.put("mobile", str(row[7]).isEmpty() ? null : str(row[7])); + list.add(asha); + } + return list; + } + + private Map buildLocation(Object[] facilityRow) { + Map location = new HashMap<>(); + location.put("state", str(facilityRow[3])); + location.put("district", str(facilityRow[5])); + location.put("blockOrUlb", str(facilityRow[7])); + location.put("locationType", str(facilityRow[8])); + return location; + } + + private Map emptyLocation() { + Map location = new HashMap<>(); + location.put("state", ""); + location.put("district", ""); + location.put("blockOrUlb", ""); + location.put("locationType", ""); + return location; + } + + private String str(Object val) { + return val != null ? val.toString() : ""; + } + + private String fullName(Object first, Object last) { + return (str(first) + " " + str(last)).trim(); + } +} diff --git a/src/main/java/com/iemr/flw/service/impl/IFAFormSubmissionServiceImpl.java b/src/main/java/com/iemr/flw/service/impl/IFAFormSubmissionServiceImpl.java index e4771229..362dc1ea 100644 --- a/src/main/java/com/iemr/flw/service/impl/IFAFormSubmissionServiceImpl.java +++ b/src/main/java/com/iemr/flw/service/impl/IFAFormSubmissionServiceImpl.java @@ -1,6 +1,8 @@ package com.iemr.flw.service.impl; import com.fasterxml.jackson.databind.ObjectMapper; +import com.iemr.flw.controller.DeathReportsController; +import com.iemr.flw.domain.iemr.EligibleCoupleRegister; import com.iemr.flw.domain.iemr.IFAFormSubmissionData; import com.iemr.flw.domain.iemr.IncentiveActivity; import com.iemr.flw.domain.iemr.IncentiveActivityRecord; @@ -9,13 +11,15 @@ import com.iemr.flw.dto.iemr.IFAFormSubmissionRequest; import com.iemr.flw.dto.iemr.IFAFormSubmissionResponse; import com.iemr.flw.masterEnum.GroupName; -import com.iemr.flw.repo.iemr.IFAFormSubmissionRepository; -import com.iemr.flw.repo.iemr.IncentiveRecordRepo; -import com.iemr.flw.repo.iemr.IncentivesRepo; -import com.iemr.flw.repo.iemr.UserServiceRoleRepo; +import com.iemr.flw.masterEnum.StateCode; +import com.iemr.flw.repo.iemr.*; import com.iemr.flw.service.IFAFormSubmissionService; +import com.iemr.flw.service.UserService; import com.iemr.flw.utils.JwtUtil; +import jakarta.persistence.criteria.CriteriaBuilder; import lombok.RequiredArgsConstructor; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -31,6 +35,9 @@ public class IFAFormSubmissionServiceImpl implements IFAFormSubmissionService { private final IFAFormSubmissionRepository repository; private final ObjectMapper mapper = new ObjectMapper(); + private final Logger logger = LoggerFactory.getLogger(IFAFormSubmissionServiceImpl.class); + + @Autowired private JwtUtil jwtUtil; @@ -42,8 +49,14 @@ public class IFAFormSubmissionServiceImpl implements IFAFormSubmissionService { @Autowired private IncentiveRecordRepo incentiveRecordRepo; + @Autowired + private UserService userService; + + @Autowired + private EligibleCoupleRegisterRepo eligibleCoupleRegisterRepo; + @Override - public String saveFormData(List requests) { + public String saveFormData(List requests, Integer userId) { try { List entities = new ArrayList<>(); for (IFAFormSubmissionRequest req : requests) { @@ -60,7 +73,7 @@ public String saveFormData(List requests) { entities.add(data); } repository.saveAll(entities); - checkIFAIncentive(entities); + checkIFAIncentive(entities,userId); return "Form data saved successfully"; } catch (Exception e) { e.printStackTrace(); @@ -68,16 +81,37 @@ public String saveFormData(List requests) { } } - private void checkIFAIncentive(List entities) { - IncentiveActivity incentiveActivityAM= incentivesRepo.findIncentiveMasterByNameAndGroup("NIPI_CHILDREN", GroupName.CHILD_HEALTH.getDisplayName()); - - if(incentiveActivityAM!=null){ - entities.forEach(ifaFormSubmissionData -> { - addIFAIncentive(ifaFormSubmissionData,incentiveActivityAM); + private void checkIFAIncentive(List entities,Integer userId) { + try { + List records = repository.findByUserId(userId); + List eligibleCoupleRegisters = eligibleCoupleRegisterRepo.getECRegRecords(userService.getUserDetail(userId).getUserName()); + int totalEligibleCouples = eligibleCoupleRegisters.size(); + int totalIFASubmissions = records.size(); + if(totalEligibleCouples>0){ + double percentage = + (totalIFASubmissions * 100.0) / totalEligibleCouples; + if(percentage>=70){ + Integer stateCode = userService.getUserDetail(userId).getStateId(); + if(stateCode.equals(StateCode.CG.getStateCode())){ + IncentiveActivity incentiveActivityCG= incentivesRepo.findIncentiveMasterByNameAndGroup("NATIONAL_IRON_PLUS", GroupName.ACTIVITY.getDisplayName()); + + if(incentiveActivityCG!=null){ + entities.forEach(ifaFormSubmissionData -> { + addIFAIncentive(ifaFormSubmissionData,incentiveActivityCG); + + }); + } + + } + } + } + }catch (Exception e){ + logger.error("Error while processing IFA incentive", e); - }); } + + } private void addIFAIncentive(IFAFormSubmissionData ifaFormSubmissionData, IncentiveActivity incentiveActivityAM) { @@ -87,10 +121,10 @@ private void addIFAIncentive(IFAFormSubmissionData ifaFormSubmissionData, Incent Timestamp ifaVisitDateTimestamp = Timestamp.valueOf(ifaVisitDate.atStartOfDay()); - IncentiveActivityRecord incentiveActivityRecord = incentiveRecordRepo.findRecordByActivityIdCreatedDateBenId(incentiveActivityAM.getId(),ifaVisitDateTimestamp,ifaFormSubmissionData.getBeneficiaryId()); + IncentiveActivityRecord incentiveActivityRecord = incentiveRecordRepo.findRecordByActivityIdCreatedDateBenId(incentiveActivityAM.getId(),ifaVisitDateTimestamp,ifaFormSubmissionData.getBeneficiaryId(),userServiceRoleRepo.getUserIdByName(ifaFormSubmissionData.getUserName())); if(incentiveActivityRecord==null){ incentiveActivityRecord = new IncentiveActivityRecord(); - incentiveActivityRecord.setActivityId(ifaFormSubmissionData.getId()); + incentiveActivityRecord.setActivityId(incentiveActivityAM.getId()); incentiveActivityRecord.setCreatedDate(ifaVisitDateTimestamp); incentiveActivityRecord.setStartDate(ifaVisitDateTimestamp); incentiveActivityRecord.setEndDate(ifaVisitDateTimestamp); @@ -100,6 +134,7 @@ private void addIFAIncentive(IFAFormSubmissionData ifaFormSubmissionData, Incent incentiveActivityRecord.setBenId(ifaFormSubmissionData.getBeneficiaryId()); incentiveActivityRecord.setAshaId(userServiceRoleRepo.getUserIdByName(ifaFormSubmissionData.getUserName())); incentiveActivityRecord.setAmount(Long.valueOf(incentiveActivityAM.getRate())); + incentiveActivityRecord.setIsEligible(true); incentiveRecordRepo.save(incentiveActivityRecord); } } diff --git a/src/main/java/com/iemr/flw/service/impl/IRSRoundServiceImpl.java b/src/main/java/com/iemr/flw/service/impl/IRSRoundServiceImpl.java index b294a3dc..26a5c1c7 100644 --- a/src/main/java/com/iemr/flw/service/impl/IRSRoundServiceImpl.java +++ b/src/main/java/com/iemr/flw/service/impl/IRSRoundServiceImpl.java @@ -17,7 +17,7 @@ public class IRSRoundServiceImpl implements IRSRoundService { private IRSRoundRepo irsRoundRepository; @Override - public List addRounds(List dtos) { + public List addRounds(List dtos,Integer userId,String userName) { // Validate input DTOs for (IRSRoundDTO dto : dtos) { if (dto.getDate() == null || dto.getHouseholdId() == null) { @@ -26,7 +26,7 @@ public List addRounds(List dtos) { } return irsRoundRepository.saveAll( dtos.stream() - .map(dto -> new IRSRound(null, dto.getDate(), dto.getRounds(), dto.getHouseholdId())) + .map(dto -> new IRSRound(null, dto.getDate(), dto.getRounds(), dto.getHouseholdId(),userName,userId)) .collect(Collectors.toList()) ); } diff --git a/src/main/java/com/iemr/flw/service/impl/IncentiveLogicImpl.java b/src/main/java/com/iemr/flw/service/impl/IncentiveLogicImpl.java new file mode 100644 index 00000000..b6e4b7c7 --- /dev/null +++ b/src/main/java/com/iemr/flw/service/impl/IncentiveLogicImpl.java @@ -0,0 +1,708 @@ +package com.iemr.flw.service.impl; + +import com.iemr.flw.domain.iemr.IncentiveActivity; +import com.iemr.flw.domain.iemr.IncentiveActivityRecord; +import com.iemr.flw.masterEnum.GroupName; +import com.iemr.flw.masterEnum.StateCode; +import com.iemr.flw.repo.iemr.IncentiveRecordRepo; +import com.iemr.flw.repo.iemr.IncentivesRepo; +import com.iemr.flw.repo.iemr.UserServiceRoleRepo; +import com.iemr.flw.service.IncentiveLogicService; +import com.iemr.flw.service.UserService; +import com.iemr.flw.utils.JwtUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.sql.Timestamp; +import java.util.Date; + +@Service +public class IncentiveLogicImpl implements IncentiveLogicService { + + private static final Logger logger = LoggerFactory.getLogger(IncentiveLogicImpl.class); + + @Autowired + private IncentivesRepo incentivesRepo; + + @Autowired + private IncentiveRecordRepo incentiveRecordRepo; + + @Autowired + private JwtUtil jwtUtil; + + @Autowired + private UserService userService; + + @Autowired + private UserServiceRoleRepo userServiceRoleRepo; + + @Override + public IncentiveActivityRecord incentiveForLeprosyPaucibacillaryConfirmed( + Long benId, + Date treatmentStartDate, + Date treatmentEndDate, + Integer userId) { + + try { + Integer stateCode = userService.getUserDetail(userId).getStateId(); + + if (stateCode == null) { + logger.warn("State code is null for user: {}", userId); + return null; + } + + String activityName = "NLEP_PB_TREATMENT"; + + if (stateCode.equals(StateCode.AM.getStateCode())) { + return processIncentive( + activityName, + GroupName.UMBRELLA_PROGRAMMES.getDisplayName(), + benId, + treatmentStartDate, + treatmentEndDate, + userId); + } + + if (stateCode.equals(StateCode.CG.getStateCode())) { + return processIncentive( + activityName, + GroupName.ACTIVITY.getDisplayName(), + benId, + treatmentStartDate, + treatmentEndDate, + userId); + } + + // state not supported + logger.info("No incentive mapping for stateCode: {}", stateCode); + return null; + + } catch (Exception e) { + logger.error("Check NLEP_PB_TREATMENT Incentive Exception: ", e); + return null; + } + } + + @Override + public IncentiveActivityRecord incentiveForTbSuspected(Long benId, Timestamp treatmentStartDate, Timestamp treatmentEndDate, Integer userId) { + try { + Integer stateCode = userService.getUserDetail(userId).getStateId(); + + if (stateCode == null) { + logger.warn("State code is null for user: {}", userId); + return null; + } + + String activityName = "INFORMANT_INCENTIVE"; + + if (stateCode.equals(StateCode.CG.getStateCode())) { + return processIncentive( + activityName, + GroupName.ACTIVITY.getDisplayName(), + benId, + treatmentStartDate, + treatmentEndDate, + userId); + } + + // state not supported + logger.info("No incentive mapping for stateCode: {}", stateCode); + return null; + + } catch (Exception e) { + logger.error("Check Tb Suspected Incentive Exception: ", e); + return null; + } + } + + @Override + public IncentiveActivityRecord incentiveForLeprosyMultibacillaryConfirmed( + Long benId, + Date treatmentStartDate, + Date treatmentEndDate, + Integer userId) { + + try { + Integer stateCode = userService.getUserDetail(userId).getStateId(); + + if (stateCode == null) { + logger.warn("State code is null for user: {}", userId); + return null; + } + + String activityName = "NLEP_MB_TREATMENT"; + + if (stateCode.equals(StateCode.AM.getStateCode())) { + return processIncentive( + activityName, + GroupName.UMBRELLA_PROGRAMMES.getDisplayName(), + benId, + treatmentStartDate, + treatmentEndDate, + userId); + } + + if (stateCode.equals(StateCode.CG.getStateCode())) { + return processIncentive( + activityName, + GroupName.ACTIVITY.getDisplayName(), + benId, + treatmentStartDate, + treatmentEndDate, + userId); + } + + // state not supported + logger.info("No incentive mapping for stateCode: {}", stateCode); + return null; + + } catch (Exception e) { + logger.error("Check NLEP_MB_TREATMENT Incentive Exception: ", e); + return null; + } + } + + @Override + public IncentiveActivityRecord incentiveForVhndMeeting(Long benId, Date treatmentStartDate, Date treatmentEndDate, Integer userId) { + try { + Integer stateCode = userService.getUserDetail(userId).getStateId(); + + if (stateCode == null) { + logger.warn("State code is null for user: {}", userId); + return null; + } + + String activityName = "VHND_PARTICIPATION"; + + if (stateCode.equals(StateCode.CG.getStateCode())) { + return processIncentive( + activityName, + GroupName.ACTIVITY.getDisplayName(), + benId, + treatmentStartDate, + treatmentEndDate, + userId); + } + + // state not supported + logger.info("No incentive mapping for stateCode: {}", stateCode); + return null; + + } catch (Exception e) { + logger.error("Check VHND_PARTICIPATION Incentive Exception: ", e); + return null; + } + } + + @Override + public IncentiveActivityRecord incentiveForClusterMeeting(Long benId, Date treatmentStartDate, Date treatmentEndDate, Integer userId) { + try { + Integer stateCode = userService.getUserDetail(userId).getStateId(); + + if (stateCode == null) { + logger.warn("State code is null for user: {}", userId); + return null; + } + + String activityName = "CLUSTER_MEETING"; + + if (stateCode.equals(StateCode.CG.getStateCode())) { + return processIncentive( + activityName, + GroupName.ACTIVITY.getDisplayName(), + benId, + treatmentStartDate, + treatmentEndDate, + userId); + } + + // state not supported + logger.info("No incentive mapping for stateCode: {}", stateCode); + return null; + + } catch (Exception e) { + logger.error("Check CLUSTER_MEETING Incentive Exception: ", e); + return null; + } + } + + @Override + public IncentiveActivityRecord incentiveForAttendingVhsnc(Long benId, Date treatmentStartDate, Date treatmentEndDate, Integer userId) { + try { + Integer stateCode = userService.getUserDetail(userId).getStateId(); + + if (stateCode == null) { + logger.warn("State code is null for user: {}", userId); + return null; + } + + String activityName = "VHSNC_MEETING"; + + if (stateCode.equals(StateCode.CG.getStateCode())) { + return processIncentive( + activityName, + GroupName.ACTIVITY.getDisplayName(), + benId, + treatmentStartDate, + treatmentEndDate, + userId); + } + + // state not supported + logger.info("No incentive mapping for stateCode: {}", stateCode); + return null; + + } catch (Exception e) { + logger.error("Check VHSNC_MEETING Incentive Exception: ", e); + return null; + } + } + + @Override + public IncentiveActivityRecord incentiveForChildBirthGap(Long benId, Date treatmentStartDate, Date treatmentEndDate, Integer userId) { + try { + Integer stateCode = userService.getUserDetail(userId).getStateId(); + + if (stateCode == null) { + logger.warn("State code is null for user: {}", userId); + return null; + } + + String activityName = "FP_DELAY_2Y"; + + if (stateCode.equals(StateCode.CG.getStateCode())) { + return processIncentive( + activityName, + GroupName.ACTIVITY.getDisplayName(), + benId, + treatmentStartDate, + treatmentEndDate, + userId); + } + + // state not supported + logger.info("No incentive mapping for stateCode: {}", stateCode); + return null; + + } catch (Exception e) { + logger.error("Check FP_DELAY_2Y Incentive Exception: ", e); + return null; + } + } + + @Override + public IncentiveActivityRecord incentiveForEyeSurgeyReferGovtHospital(Long benId, Timestamp treatmentStartDate, Timestamp treatmentEndDate, Integer userId) { + try { + Integer stateCode = userService.getUserDetail(userId).getStateId(); + + if (stateCode == null) { + logger.warn("State code is null for user: {}", userId); + return null; + } + + String activityName = "NPCB_GOVT_CATARACT"; + + if (stateCode.equals(StateCode.CG.getStateCode())) { + return processIncentive( + activityName, + GroupName.ACTIVITY.getDisplayName(), + benId, + treatmentStartDate, + treatmentEndDate, + userId); + } + + if (stateCode.equals(StateCode.AM.getStateCode())) { + return processIncentive( + activityName, + GroupName.UMBRELLA_PROGRAMMES.getDisplayName(), + benId, + treatmentStartDate, + treatmentEndDate, + userId); + } + + // state not supported + logger.info("No incentive mapping for stateCode: {}", stateCode); + return null; + + } catch (Exception e) { + logger.error("Check Eye surgery Incentive Exception: ", e); + return null; + } + } + + @Override + public IncentiveActivityRecord incentiveForEyeSurgeyReferPrivateHospital(Long benId, Timestamp treatmentStartDate, Timestamp treatmentEndDate, Integer userId) { + try { + Integer stateCode = userService.getUserDetail(userId).getStateId(); + + if (stateCode == null) { + logger.warn("State code is null for user: {}", userId); + return null; + } + + String activityName = "NPCB_PRIVATE_CATARACT"; + + if (stateCode.equals(StateCode.CG.getStateCode())) { + return processIncentive( + activityName, + GroupName.ACTIVITY.getDisplayName(), + benId, + treatmentStartDate, + treatmentEndDate, + userId); + } + + if (stateCode.equals(StateCode.AM.getStateCode())) { + return processIncentive( + activityName, + GroupName.UMBRELLA_PROGRAMMES.getDisplayName(), + benId, + treatmentStartDate, + treatmentEndDate, + userId); + } + + // state not supported + logger.info("No incentive mapping for stateCode: {}", stateCode); + return null; + + } catch (Exception e) { + logger.error("Check Eye surgery Incentive Exception: ", e); + return null; + } + } + + + + @Override + public IncentiveActivityRecord incentiveForGiveingIFA(Long benId, Timestamp treatmentStartDate, Timestamp treatmentEndDate, Integer userId) { + try { + Integer stateCode = userService.getUserDetail(userId).getStateId(); + + if (stateCode == null) { + logger.warn("State code is null for user: {}", userId); + return null; + } + + String activityName = "NIPI_CHILDREN"; + + if (stateCode.equals(StateCode.CG.getStateCode())) { + return processIncentive( + activityName, + GroupName.ACTIVITY.getDisplayName(), + benId, + treatmentStartDate, + treatmentStartDate, + userId); + } + + if (stateCode.equals(StateCode.AM.getStateCode())) { + return processIncentive( + activityName, + GroupName.CHILD_HEALTH.getDisplayName(), + benId, + treatmentStartDate, + treatmentEndDate, + userId); + } + + // state not supported + logger.info("No incentive mapping for stateCode: {}", stateCode); + return null; + + } catch (Exception e) { + logger.error("Check IFA Exception: ", e); + return null; + } } + + @Override + public IncentiveActivityRecord incentiveForTbFollowUpIsDrTb(Long benId, Timestamp treatmentStartDate, Timestamp treatmentEndDate, Integer userId) { + try { + Integer stateCode = userService.getUserDetail(userId).getStateId(); + + if (stateCode == null) { + logger.warn("State code is null for user: {}", userId); + return null; + } + + String activityName = "DRTB_TREATMENT"; + + if (stateCode.equals(StateCode.CG.getStateCode())) { + return processIncentive( + activityName, + GroupName.ACTIVITY.getDisplayName(), + benId, + treatmentStartDate, + treatmentStartDate, + userId); + } + + if (stateCode.equals(StateCode.AM.getStateCode())) { + return processIncentive( + activityName, + GroupName.UMBRELLA_PROGRAMMES.getDisplayName(), + benId, + treatmentStartDate, + treatmentEndDate, + userId); + } + + // state not supported + logger.info("No incentive mapping for stateCode: {}", stateCode); + return null; + + } catch (Exception e) { + logger.error("Check TB Incentive Exception: ", e); + return null; + } + } + + @Override + public IncentiveActivityRecord incentiveForTbFollowUp(Long benId, Timestamp treatmentStartDate, Timestamp treatmentEndDate, Integer userId) { + try { + Integer stateCode = userService.getUserDetail(userId).getStateId(); + + if (stateCode == null) { + logger.warn("State code is null for user: {}", userId); + return null; + } + + String activityName = "DSTB_TREATMENT"; + + if (stateCode.equals(StateCode.CG.getStateCode())) { + return processIncentive( + activityName, + GroupName.ACTIVITY.getDisplayName(), + benId, + treatmentStartDate, + treatmentStartDate, + userId); + } + + if (stateCode.equals(StateCode.AM.getStateCode())) { + return processIncentive( + activityName, + GroupName.UMBRELLA_PROGRAMMES.getDisplayName(), + benId, + treatmentStartDate, + treatmentEndDate, + userId); + } + + // state not supported + logger.info("No incentive mapping for stateCode: {}", stateCode); + return null; + + } catch (Exception e) { + logger.error("Check TB Incentive Exception: ", e); + return null; + } + } + + @Override + public IncentiveActivityRecord incentiveForMalariaFollowUp(Long benId, Timestamp treatmentStartDate, Timestamp treatmentEndDate, Integer userId) { + try { + Integer stateCode = userService.getUserDetail(userId).getStateId(); + + if (stateCode == null) { + logger.warn("State code is null for user: {}", userId); + return null; + } + + String activityName = "NVBDCP_MALARIA_TREATMENT"; + + if (stateCode.equals(StateCode.CG.getStateCode())) { + return processIncentive( + activityName, + GroupName.ACTIVITY.getDisplayName(), + benId, + treatmentStartDate, + treatmentStartDate, + userId); + } + + if (stateCode.equals(StateCode.AM.getStateCode())) { + return processIncentive( + activityName, + GroupName.UMBRELLA_PROGRAMMES.getDisplayName(), + benId, + treatmentStartDate, + treatmentEndDate, + userId); + } + + // state not supported + logger.info("No incentive mapping for stateCode: {}", stateCode); + return null; + + } catch (Exception e) { + logger.error("Check Eye surgery Incentive Exception: ", e); + return null; + } + } + + @Override + public IncentiveActivityRecord incentiveForSecondChildGap(Long benId, Timestamp secondChildDob, Timestamp secondChildDob1, Integer userId) { + try { + Integer stateCode = userService.getUserDetail(userId).getStateId(); + + if (stateCode == null) { + logger.warn("State code is null for user: {}", userId); + return null; + } + + String activityName = "1st_2nd_CHILD_GAP"; + + if (stateCode.equals(StateCode.CG.getStateCode())) { + return processIncentive( + activityName, + GroupName.ACTIVITY.getDisplayName(), + benId, + secondChildDob, + secondChildDob1, + userId); + } + + if (stateCode.equals(StateCode.AM.getStateCode())) { + return processIncentive( + activityName, + GroupName.FAMILY_PLANNING.getDisplayName(), + benId, + secondChildDob, + secondChildDob1, + userId); + } + + // state not supported + logger.info("No incentive mapping for stateCode: {}", stateCode); + return null; + + } catch (Exception e) { + logger.error("Check FP_DELAY_2Y Incentive Exception: ", e); + return null; + } + } + + + + @Override + public IncentiveActivityRecord incentiveForIdentificationLeprosy(Long benId, Date treatmentStartDate, Date treatmentEndDate, Integer userId) { + try { + Integer stateCode = userService.getUserDetail(userId).getStateId(); + + if (stateCode == null) { + logger.warn("State code is null for user: {}", userId); + return null; + } + + String activityName = "NLEP_CASE_DETECTION"; + + if (stateCode.equals(StateCode.AM.getStateCode())) { + return processIncentive( + activityName, + GroupName.UMBRELLA_PROGRAMMES.getDisplayName(), + benId, + treatmentStartDate, + treatmentEndDate, + userId); + } + + if (stateCode.equals(StateCode.CG.getStateCode())) { + return processIncentive( + activityName, + GroupName.ACTIVITY.getDisplayName(), + benId, + treatmentStartDate, + treatmentEndDate, + userId); + } + + // state not supported + logger.info("No incentive mapping for stateCode: {}", stateCode); + return null; + + } catch (Exception e) { + logger.error("Check Leprosy Incentive Exception: ", e); + return null; + } + } + + private IncentiveActivityRecord processIncentive(String activityName, String groupName, Long benId, + Date startDate, Date endDate, Integer userId) { + try { + IncentiveActivity activity = incentivesRepo.findIncentiveMasterByNameAndGroup(activityName, groupName); + + if (activity == null) { + logger.info("No IncentiveActivity found for name: {} and group: {}", activityName, groupName); + return null ; + } + + return saveIncentive(activity, benId, startDate, endDate, userId); + + } catch (Exception e) { + logger.error("Process Incentive Exception: ", e); + return null; + + } + } + + private IncentiveActivityRecord saveIncentive( + IncentiveActivity activity, + Long benId, + Date startDate, + Date endDate, + Integer userId) { + + try { + String userName = userServiceRoleRepo.getUserNamedByUserId(userId); + + if (activity == null || benId == null || startDate == null || endDate == null) { + logger.warn("Invalid input for saving incentive"); + return null; + } + + Timestamp startTimestamp = new Timestamp(startDate.getTime()); + Timestamp endTimestamp = new Timestamp(endDate.getTime()); + + + // 🔍 duplicate check + IncentiveActivityRecord existing = incentiveRecordRepo + .findRecordByActivityIdCreatedDateBenId( + activity.getId(), startTimestamp, benId,userId); + + if (existing != null) { + logger.info("Incentive already exists for benId: {}, activityId: {}", + benId, activity.getId()); + return existing; + } + + // 🆕 create record + IncentiveActivityRecord record = new IncentiveActivityRecord(); + record.setActivityId(activity.getId()); + record.setCreatedDate(startTimestamp); + record.setUpdatedDate(startTimestamp); + record.setStartDate(startTimestamp); + record.setEndDate(endTimestamp); + record.setCreatedBy(userName); + record.setUpdatedBy(userName); + record.setBenId(benId); + record.setAshaId(userId); + record.setAmount(Long.valueOf(activity.getRate())); + + record = incentiveRecordRepo.save(record); + + logger.info("Incentive saved successfully for benId: {}", benId); + + return record; + + } catch (Exception e) { + logger.error("Error Incentive Exception: ", e); + return null; + } + } + +} diff --git a/src/main/java/com/iemr/flw/service/impl/IncentiveServiceImpl.java b/src/main/java/com/iemr/flw/service/impl/IncentiveServiceImpl.java index 6a42b76d..cfdaaae2 100644 --- a/src/main/java/com/iemr/flw/service/impl/IncentiveServiceImpl.java +++ b/src/main/java/com/iemr/flw/service/impl/IncentiveServiceImpl.java @@ -1,239 +1,1014 @@ package com.iemr.flw.service.impl; +import com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.Gson; import com.google.gson.GsonBuilder; -import com.iemr.flw.domain.identity.RMNCHMBeneficiarydetail; -import com.iemr.flw.domain.iemr.IncentiveActivity; -import com.iemr.flw.domain.iemr.IncentiveActivityLangMapping; -import com.iemr.flw.domain.iemr.IncentiveActivityRecord; +import com.iemr.flw.domain.iemr.*; import com.iemr.flw.dto.identity.GetBenRequestHandler; import com.iemr.flw.dto.iemr.*; import com.iemr.flw.masterEnum.GroupName; +import com.iemr.flw.masterEnum.IncentiveApprovalStatus; import com.iemr.flw.masterEnum.StateCode; import com.iemr.flw.repo.identity.BeneficiaryRepo; -import com.iemr.flw.repo.iemr.IncentiveActivityLangMappingRepo; -import com.iemr.flw.repo.iemr.IncentiveRecordRepo; -import com.iemr.flw.repo.iemr.IncentivesRepo; -import com.iemr.flw.repo.iemr.UserServiceRoleRepo; +import com.iemr.flw.repo.iemr.*; import com.iemr.flw.service.IncentiveService; +import com.iemr.flw.service.MaaMeetingService; +import com.iemr.flw.service.NotificationService; +import com.iemr.flw.service.UserService; import com.iemr.flw.utils.JwtUtil; +import jakarta.persistence.criteria.CriteriaBuilder; import org.modelmapper.ModelMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.multipart.MultipartFile; -import java.math.BigInteger; import java.sql.Timestamp; import java.time.LocalDate; import java.time.LocalDateTime; +import java.time.Month; import java.util.*; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Function; import java.util.stream.Collectors; @Service public class IncentiveServiceImpl implements IncentiveService { - private final Logger logger = LoggerFactory.getLogger(ChildCareServiceImpl.class); + + private final Logger logger = LoggerFactory.getLogger(IncentiveServiceImpl.class); @Autowired private BeneficiaryRepo beneficiaryRepo; + @Autowired + private IncentiveActivityLangMappingRepo incentiveActivityLangMappingRepo; + @Autowired + private IncentivesRepo incentivesRepo; + @Autowired + private IncentiveRecordRepo recordRepo; + @Autowired + private IncentivePendingActivityRepository incentivePendingActivityRepository; + @Autowired + private UserServiceRoleRepo userRepo; + ; ModelMapper modelMapper = new ModelMapper(); @Autowired - private IncentiveActivityLangMappingRepo incentiveActivityLangMappingRepo; + private JwtUtil jwtUtil; + @Autowired - IncentivesRepo incentivesRepo; + private MaaMeetingService maaMeetingService; @Autowired - IncentiveRecordRepo recordRepo; + private BenReferDetailsRepo benReferDetailsRepo; @Autowired - private JwtUtil jwtUtil; + private CbacIemrDetailsRepo cbacIemrDetailsRepo; @Autowired - private UserServiceRoleRepo userRepo; + private UserService userService; + + @Autowired + private EligibleCoupleRegisterRepo eligibleCoupleRegisterRepo; + + @Autowired + private IFAFormSubmissionRepository ifaFormSubmissionRepository; + + private final ConcurrentHashMap lockMap = new ConcurrentHashMap<>(); + @Autowired + private NotificationService notificationService; + + @Autowired + private SupervisorDashboardRepo dashboardRepo; + + + + // ================= MASTER SAVE ================= @Override public String saveIncentivesMaster(List activityDTOS) { try { - activityDTOS.forEach(activityDTO -> { + for (IncentiveActivityDTO dto : activityDTOS) { IncentiveActivity activity = - incentivesRepo.findIncentiveMasterByNameAndGroup(activityDTO.getName(), activityDTO.getGroup()); + incentivesRepo.findIncentiveMasterByNameAndGroup(dto.getName(), dto.getGroup()); if (activity == null) { activity = new IncentiveActivity(); - modelMapper.map(activityDTO, activity); - } else { - Long id = activity.getId(); - modelMapper.map(activityDTO, activity); - activity.setId(id); } + + modelMapper.map(dto, activity); incentivesRepo.save(activity); - }); - String saved = ""; - activityDTOS.forEach(dto -> saved.concat(dto.getGroup() + ": " + dto.getName())); - return "saved master data for " + saved ; + } + return "Saved successfully"; } catch (Exception e) { - + logger.error("Error saving incentives", e); + return "Error"; } - return null; } + // ================= GET MASTER ================= @Override public String getIncentiveMaster(IncentiveRequestDTO incentiveRequestDTO) { try { + Integer stateCode = incentiveRequestDTO.getState(); + String langCode = incentiveRequestDTO.getLangCode(); - List incs = new ArrayList<>(); - if(incentiveRequestDTO.getState()==StateCode.CG.getStateCode()){ - incs = incentivesRepo.findAll().stream().filter(incentiveActivity -> incentiveActivity.getGroup().equals("ACTIVITY")).collect(Collectors.toList()); + // Integer comparison using intValue() — safe and reliable + boolean isCG = stateCode != null && stateCode.intValue() == StateCode.CG.getStateCode(); + boolean isAM = stateCode != null && stateCode.intValue() == StateCode.AM.getStateCode(); - }else { - incs = incentivesRepo.findAll().stream().filter(incentiveActivity -> !incentiveActivity.getGroup().equals("ACTIVITY")).collect(Collectors.toList()); + // Single filtered DB query instead of findAll() + stream filter + List incs = isCG + ? incentivesRepo.findByGroupAndIsDeleted("ACTIVITY", false) + : incentivesRepo.findByGroupNotAndIsDeleted("ACTIVITY", false); + if (incs.isEmpty()) { + return new Gson().toJson(Collections.emptyList()); } - List dtos = incs.stream().map(inc -> { - IncentiveActivityDTO dto = modelMapper.map(inc, IncentiveActivityDTO.class); - // Fetch language mapping - IncentiveActivityLangMapping mapping = incentiveActivityLangMappingRepo - .findByIdAndName(inc.getId(),inc.getName()); + // Fetch ALL lang mappings in ONE query instead of N queries in loop + List ids = incs.stream() + .map(IncentiveActivity::getId) + .collect(Collectors.toList()); + Map mappingById = incentiveActivityLangMappingRepo + .findAllByIdIn(ids) + .stream() + .collect(Collectors.toMap( + IncentiveActivityLangMapping::getId, + Function.identity() + )); + List dtos = incs.stream().map(inc -> { + IncentiveActivityDTO dto = modelMapper.map(inc, IncentiveActivityDTO.class); + IncentiveActivityLangMapping mapping = mappingById.get(inc.getId()); if (mapping != null) { dto.setName(mapping.getName()); - dto.setGroupName(mapping.getGroup()); - if(Objects.equals(incentiveRequestDTO.getLangCode(), "en")){ + + if (isCG) { + dto.setGroupName(""); + } else if (isAM) { + dto.setGroupName(mapping.getGroup()); + } + + // Set description based on language + if ("as".equals(langCode)) { + dto.setDescription(mapping.getAssameActivityDescription() != null + ? mapping.getAssameActivityDescription() + : mapping.getDescription()); + + } else if ("hi".equals(langCode)) { + dto.setDescription(mapping.getHindiActivityDescription() != null + ? mapping.getHindiActivityDescription() + : mapping.getDescription()); + + } else { + // default "en" dto.setDescription(inc.getDescription()); + } + } else { + if (isCG) { + dto.setGroupName(""); + } else { + dto.setGroupName(inc.getGroup()); - }else if(Objects.equals(incentiveRequestDTO.getLangCode(), "as")){ + } + } - if(mapping.getAssameActivityDescription()!=null){ - dto.setDescription(mapping.getAssameActivityDescription()); + return dto; + }).collect(Collectors.toList()); - }else { - dto.setDescription(mapping.getDescription()); + Gson gson = new GsonBuilder().setDateFormat("MMM dd, yyyy h:mm:ss a").create(); + return gson.toJson(dtos); - } + } catch (Exception e) { + logger.error("Error in getIncentiveMaster: ", e); + } + return null; + } + + @Override + public String getAllIncentivesByUserId(GetBenRequestHandler request) { + int page = 0; + int size = 20; + Page pageResult; + List finalDtos = new ArrayList<>(); + + Integer stateCode = userService.getUserDetail(request.getAshaId()).getStateId(); + String userName = userService.getUserDetail(request.getAshaId()).getUserName(); + try { + + if (stateCode.equals(StateCode.AM.getStateCode())) { + checkMonthlyAshaIncentive(request.getAshaId()); + } + if (stateCode.equals(StateCode.CG.getStateCode())) { + checkMonthlyAshaIncentiveForCg(request.getAshaId()); + + } + + } catch (Exception e) { + logger.error("Error in checkMonthlyAshaIncentive: ", e); + } + + try { + addIncentiveForIronTablets(request.getAshaId()); + incentiveOfNcdReferal(request.getAshaId(), request.getVillageID()); + + } catch (Exception e) { + logger.error("Error in incentiveOfNcdReferal: ", e); + } - }else if(Objects.equals(incentiveRequestDTO.getLangCode(), "hi")){ - if(mapping.getHindiActivityDescription()!=null){ - dto.setDescription(mapping.getHindiActivityDescription()); + boolean isCG = stateCode != null && stateCode.equals(StateCode.CG.getStateCode()); - }else { - dto.setDescription(mapping.getDescription()); + do { + Pageable pageable = PageRequest.of( + page, + size, + Sort.by("id").descending() // latest first + ); + + pageResult = recordRepo.findRecordsByAsha( + request.getAshaId(), + pageable + ); + + List entities = pageResult.getContent(); + + if (entities.isEmpty()) { + return new Gson().toJson(Collections.emptyList()); + } + + // Step 2: Collect all activityIds — fetch valid ones in ONE query + List activityIds = entities.stream() + .map(IncentiveActivityRecord::getActivityId) + .distinct() + .collect(Collectors.toList()); + + // Single bulk query instead of N individual findIncentiveMasterById() calls + Set validActivityIds = isCG + ? incentivesRepo.findValidActivityIds(activityIds, true) + : incentivesRepo.findValidActivityIds(activityIds, false); + + // Filter entities based on valid activity IDs + entities = entities.stream() + .filter(e -> validActivityIds.contains(e.getActivityId())) + .collect(Collectors.toList()); + + // Step 3: Collect all benIds that need name lookup (name == null and benId > 0) + List benIdsToFetch = entities.stream() + .filter(e -> e.getName() == null && e.getBenId() != null && e.getBenId() > 0) + .map(IncentiveActivityRecord::getBenId) + .distinct() + .collect(Collectors.toList()); + + // Step 4: Bulk fetch all beneficiary names in ONE query instead of 3 queries per record + Map benIdToNameMap = new HashMap<>(); + if (!benIdsToFetch.isEmpty()) { + List benDetails = beneficiaryRepo.findBenNamesByBenIds(benIdsToFetch); + for (Object[] row : benDetails) { + Long benId = ((Number) row[0]).longValue(); + String first = row[1] != null ? (String) row[1] : ""; + String last = row[2] != null ? (String) row[2] : ""; + benIdToNameMap.put(benId, (first + " " + last).trim()); + } + } + + // Step 5: Map entities to DTOs + List dtos = entities.stream().map(entry -> { + if (entry.getName() == null) { + if (entry.getBenId() != null && entry.getBenId() > 0) { + String name = benIdToNameMap.getOrDefault(entry.getBenId(), ""); + entry.setName(name); + if (isCG) { + entry.setIsEligible(true); } + } else { + entry.setName(""); + } + if (entry.getVerifiedByUserId() != null) { + entry.setSupervisorRole(userRepo.getUserRole(entry.getVerifiedByUserId()).get(0).getRoleName()); + entry.setVerifiedByUserName(userRepo.getUserRole(entry.getVerifiedByUserId()).get(0).getName()); } + if (entry.getAshaId() != null) { + if (entry.getCreatedBy() == null) { + entry.setCreatedBy(userName); + } + + if (entry.getUpdatedBy() == null) { + entry.setUpdatedBy(userName); + } + + if (entry.getIsEligible() == null) { + entry.setIsEligible(true); + } + } - }else { - dto.setGroupName(inc.getGroup()); } + return modelMapper.map(entry, IncentiveRecordDTO.class); + }).collect(Collectors.toList()); + finalDtos.addAll(dtos); + + page++; + + } while (pageResult.hasNext()); + + + + Gson gson = new GsonBuilder().setDateFormat("MMM dd, yyyy h:mm:ss a").create(); + return gson.toJson(finalDtos); + } + + // ================= GROUPED SUMMARY ================= + @Override + public String getAllIncentivesGroupedSummary(GetBenRequestHandler request,Integer userId) { + String roleName = userService.getUserDetail(userId).getRoleName(); + LocalDate start = LocalDate.of(request.getYear(), request.getMonth(), 1); + LocalDate end = start.withDayOfMonth(start.lengthOfMonth()); + + Timestamp startTs = Timestamp.valueOf(start.atStartOfDay()); + Timestamp endTs = Timestamp.valueOf(end.atTime(23, 59, 59)); + + Integer villageID = userRepo.getUserRole(request.getUserId()).get(0).getStateId(); + boolean isCG = villageID != null && villageID.intValue() == StateCode.CG.getStateCode(); + + List records = + recordRepo.findRecordsByAsha(request.getUserId()) + .stream() + .filter(r -> r.getCreatedDate() != null + && r.getEndDate() != null + && r.getEndDate().toLocalDateTime().getMonthValue() == request.getMonth() + && r.getEndDate().toLocalDateTime().getYear() == request.getYear() + && r.getIsClaimed()) + .toList(); + + + // Bulk fetch valid activity IDs — state wise + List activityIds = records.stream() + .map(IncentiveActivityRecord::getActivityId) + .collect(Collectors.toList()); + + Set validActivityIds = isCG + ? incentivesRepo.findValidActivityIds(activityIds, true) + : incentivesRepo.findValidActivityIds(activityIds, false); + + // Filter records based on valid activity IDs + if(isCG){ + if("ASHA Supervisor".equalsIgnoreCase(roleName)){ + records = records.stream() + .filter(r -> validActivityIds.contains(r.getActivityId()) && r.getIsDefaultActivity()) + .collect(Collectors.toList()); + }else if ("ANM".equalsIgnoreCase(roleName) || "CHO".equalsIgnoreCase(roleName)) { + if ("ANM".equalsIgnoreCase(roleName) || "CHO".equalsIgnoreCase(roleName)) { + records = records.stream() + .filter(r -> + validActivityIds.contains(r.getActivityId()) && + ( + r.getApprovalStatus() == 101 + || r.getApprovalStatus() == 103 + || r.getApprovalStatus() == 105 + || r.getApprovalStatus() == 104 + || ( + r.getApprovalStatus() == 102 + && ( + isAfter24Hours(r.getCalimedDate()) + || Boolean.FALSE.equals(r.getIsDefaultActivity()) + ) + ) + ) + ) + .collect(Collectors.toList()); + } + } + + }else { + records = records.stream() + .filter(r -> validActivityIds.contains(r.getActivityId())) + .collect(Collectors.toList()); + } + + + Map> grouped = + records.stream().collect(Collectors.groupingBy(IncentiveActivityRecord::getActivityId)); + + List> result = new ArrayList<>(); + + for (var entry : grouped.entrySet()) { + + Long activityId = entry.getKey(); + List list = entry.getValue(); + + IncentiveActivity activity = + incentivesRepo.findById(activityId).orElse(null); + + Integer approvalStatus = list.stream() + .max(Comparator.comparing(IncentiveActivityRecord::getCreatedDate)) + .map(IncentiveActivityRecord::getApprovalStatus) + .orElse(0); + + + if (activity == null) continue; + + long total = list.stream() + .mapToLong(r -> r.getAmount() != null ? r.getAmount() : 0) + .sum(); + + Map map = new HashMap<>(); + if(isCG){ + map.put("activityId", activityId); + map.put("activityDec", activity.getDescription()); + map.put("groupName", activity.getGroup()); + map.put("isDefault", activity.getIsDefaultActivity()); + map.put("approvalStatus", approvalStatus); + map.put("claimCount", list.size()); + map.put("totalAmount", total); + map.put("amount", activity.getRate()); + + }else { + map.put("activityId", activityId); + map.put("activityDec", activity.getDescription()); + map.put("groupName", activity.getGroup()); + map.put("claimCount", list.size()); + map.put("totalAmount", total); + map.put("amount", activity.getRate()); + + } + + result.add(map); + + + + } + + return new Gson().toJson(result); + } + + private boolean isAfter24Hours(Timestamp claimedDate) { + if (claimedDate == null) { + return false; + } + + long now = System.currentTimeMillis(); + long diff = now - claimedDate.getTime(); + + logger.info("Now: {}", new Timestamp(now)); + logger.info("ClaimedDate: {}", claimedDate); + logger.info("Diff(ms): {}", diff); + logger.info("Diff(hours): {}", diff / (1000 * 60 * 60.0)); + + return diff >= 24L * 60 * 60 * 1000; + } + @Override + public String getAllIncentivesGroupedActivity(GetBenRequestHandler request) { + try { + + LocalDate startDate = LocalDate.of(request.getYear(), request.getMonth(), 1); + LocalDate endDate = startDate.plusMonths(1); + + Timestamp startTs = Timestamp.valueOf(startDate.atStartOfDay()); + Timestamp endTs = Timestamp.valueOf(endDate.atStartOfDay()); + + List records = + recordRepo.findRecordsByAsha(request.getUserId()) + .stream() + .filter(r -> r.getActivityId() != null + && r.getActivityId().equals(request.getActivityId()) + && r.getCreatedDate() != null + && !r.getCreatedDate().before(startTs) + && r.getCreatedDate().before(endTs)) + .collect(Collectors.toList()); + + if (records.isEmpty()) { + return new Gson().toJson(new ArrayList<>()); + } + + // 🔹 Get beneficiary names + Set benIds = records.stream() + .map(IncentiveActivityRecord::getBenId) + .filter(id -> id != null && id > 0) + .collect(Collectors.toSet()); + + Map nameMap = new HashMap<>(); + + if (!benIds.isEmpty()) { + List data = beneficiaryRepo.findBenNamesByBenIds(new ArrayList<>(benIds)); + for (Object[] row : data) { + Long id = ((Number) row[0]).longValue(); + String name = (row[1] != null ? row[1] : "") + " " + + (row[2] != null ? row[2] : ""); + nameMap.put(id, name.trim()); + } + } + + // 🔹 Activity details + IncentiveActivity activity = + incentivesRepo.findById(request.getActivityId()).orElse(null); + + String groupName = activity != null ? activity.getGroup() : ""; + String description = activity != null ? activity.getDescription() : ""; + + // 🔹 Map result + List dtos = records.stream().map(r -> { + + if (r.getName() == null) { + if (r.getBenId() != null && r.getBenId() > 0) { + r.setName(nameMap.getOrDefault(r.getBenId(), "")); + } else { + r.setName(""); + } + } + + r.setGroupName(groupName); + r.setActivityDec(description); + + return modelMapper.map(r, IncentiveRecordDTO.class); - return dto; }).collect(Collectors.toList()); - Gson gson = new GsonBuilder().setDateFormat("MMM dd, yyyy h:mm:ss a").create(); + return new GsonBuilder() + .setDateFormat("MMM dd, yyyy h:mm:ss a") + .create() + .toJson(dtos); - return gson.toJson(dtos); } catch (Exception e) { - e.printStackTrace(); + logger.error("Error in getAllIncentivesGroupedActivity", e); + return null; } - return null; - } @Override - public String getAllIncentivesByUserId(GetBenRequestHandler request) { + public String updateIncentive(PendingActivityDTO pendingActivityDTO) { + try { + + if (pendingActivityDTO == null) { + return "Invalid request"; + } - if(request.getVillageID()!= StateCode.CG.getStateCode()){ - checkMonthlyAshaIncentive(request.getAshaId()); +// Optional optional = +// incentivePendingActivityRepository +// .findByUserIdAndModuleNameAndActivityId( +// pendingActivityDTO.getUserId(), +// pendingActivityDTO.getModuleName(), +// pendingActivityDTO.getId() +// ); +// +// if (!optional.isPresent()) { +// return "Record not found"; +// } + +// IncentivePendingActivity existing = optional.get(); + + if (pendingActivityDTO.getImages() == null || + pendingActivityDTO.getImages().isEmpty()) { + return "No images provided"; + } + + String module = pendingActivityDTO.getModuleName(); + + // 🔹 MAA MEETING + if ("MAA_MEETING".equalsIgnoreCase(module)) { + MaaMeetingRequestDTO dto = new MaaMeetingRequestDTO(); + dto.setMeetingImages( + pendingActivityDTO.getImages().toArray(new MultipartFile[0]) + ); + + //maaMeetingService.updateMeeting(dto); + return "Updated successfully"; + } + + // 🔹 HBNC + if ("HBNC".equalsIgnoreCase(module)) { +// childCareService.updateHbncFromFileUpload( +// pendingActivityDTO.getImages().toArray(new MultipartFile[0]), +// pendingActivityDTO.getId(), +// existing.getRecordId() +// ); + return "Updated successfully"; + } + + // 🔹 UWIN SESSION + if ("UWIN".equalsIgnoreCase(module)) { + UwinSessionRequestDTO dto = new UwinSessionRequestDTO(); + dto.setAttachments( + pendingActivityDTO.getImages().toArray(new MultipartFile[0]) + ); + +// uwinSessionService.updateSession( +// dto, +// existing.getRecordId(), +// pendingActivityDTO.getId() +// ); + + return "Updated successfully"; + } + + return "No matching module found"; + + } catch (Exception e) { + logger.error("Error in updateIncentive", e); + return "Error: " + e.getMessage(); } + } - List dtos = new ArrayList<>(); - List entities = recordRepo.findRecordsByAsha(request.getAshaId()); - if(request.getVillageID()==StateCode.CG.getStateCode()){ - entities = entities.stream().filter(entitie-> incentivesRepo.findIncentiveMasterById(entitie.getActivityId(),true)!=null).collect(Collectors.toList()); - }else { - entities = entities.stream().filter(entitie-> incentivesRepo.findIncentiveMasterById(entitie.getActivityId(),false)!=null).collect(Collectors.toList()); + // ================= UPDATE CLAIM ================= + @Transactional + public String updateClaimStatus(Integer ashaId, Integer month, Integer year, Boolean isClaimed, String token) { + String title = null; + try { + LocalDate start = LocalDate.of(year, month, 1); + LocalDate end = start.plusMonths(1); + + + int updated = recordRepo.updateClaimStatusByAshaAndDateRange( + ashaId, + isClaimed, + Timestamp.valueOf(LocalDateTime.now()), + Timestamp.valueOf(start.atStartOfDay()), + Timestamp.valueOf(end.atStartOfDay()) + ); + if(updated>0){ + Map data = new HashMap<>(); + data.put("notification_type", "INCENTIVE_APPROVAL"); + data.put("nav_id", "INCENTIVE_HISTORY"); + data.put("sender_user_id", String.valueOf(ashaId)); + data.put("receiver_user_id", String.valueOf(ashaId)); + data.put("month", String.valueOf(month)); + data.put("year", String.valueOf(year)); + data.put("approval_status", String.valueOf(102)); + data.put("priority", "HIGH"); + title = "Incentive Approved"; + + + + if (isClaimed) { + String body = "Your incentive Successfully claimed for month of" + Month.of(month).name() + " " + year; + notificationService.sendNotification( + "FLW","NA" , + title, + body, + "INCENTIVE_CLAIMED","INCENTIVE",ashaId + ); + } + if (isClaimed) { + String body = userService.getUserDetail(ashaId).getName()+" Successfully claimed Incentive for month of " + Month.of(month).name() + " " + year; + notificationService.sendNotification( + "FLW","NA" , + title, + body, + "INCENTIVE_CLAIMED","INCENTIVE",dashboardRepo.getSupervisorUserIdByAshaId(ashaId) + ); + } + + + + } + return updated > 0 ? "Success" : "No records"; + + } catch (Exception e) { + logger.error("Error", e); + return "Error"; } - entities.forEach(entry -> { - if(entry.getName()==null){ - if(entry.getBenId()!=0 && entry.getBenId()>0L){ - Long regId = beneficiaryRepo.getBenRegIdFromBenId(entry.getBenId()); - logger.info("rmnchBeneficiaryDetailsRmnch"+regId); - BigInteger benDetailId = beneficiaryRepo.findByBenRegIdFromMapping(BigInteger.valueOf(regId)).getBenDetailsId(); - RMNCHMBeneficiarydetail rmnchBeneficiaryDetails = beneficiaryRepo.findByBeneficiaryDetailsId(benDetailId); - String beneName = rmnchBeneficiaryDetails.getFirstName()+" "+rmnchBeneficiaryDetails.getLastName(); - entry.setName(beneName); + } + + private void incentiveOfNcdReferal(Integer ashaId, Integer stateId) { + try { + String userName = userRepo.getUserNamedByUserId(ashaId); + String groupName = resolveGroupName(stateId); + + Map activityMap = + incentivesRepo.findIncentiveMasterByNameAndGroup( + List.of("NCD_POP_ENUMERATION", "HWC_REFERRAL_10_CASES"), groupName + ).stream().collect(Collectors.toMap(IncentiveActivity::getName, Function.identity())); + + IncentiveActivity ncdPopEnumeration = activityMap.get("NCD_POP_ENUMERATION"); + + IncentiveActivity hwcReferralEnumeration = activityMap.get("HWC_REFERRAL_10_CASES"); + + CompletableFuture> benReferFuture = + CompletableFuture.supplyAsync(() -> benReferDetailsRepo.findByCreatedBy(userName)); + CompletableFuture> cbacFuture = + CompletableFuture.supplyAsync(() -> cbacIemrDetailsRepo.findByCreatedBy(userName)); + + List cbacDetailsImer = cbacFuture.get(); + + List recordsToSave = new ArrayList<>(); + - }else{ - entry.setName(""); + if (ncdPopEnumeration != null && !cbacDetailsImer.isEmpty()) { + final IncentiveActivity activity = ncdPopEnumeration; + cbacDetailsImer.forEach(cbacDetailsImer1 -> { + if(cbacDetailsImer1!=null){ + addNCDandCBACIncentiveRecord(activity, ashaId, cbacDetailsImer1.getBeneficiaryRegId(), cbacDetailsImer1.getCreatedDate(), userName); + } + }); + } + + if (hwcReferralEnumeration != null) { + logger.info("hwcReferralEnumeration :" + hwcReferralEnumeration.getDescription()); + logger.info("userName={}", userName); + + LocalDate now = LocalDate.now(); + + Long referralCount = benReferDetailsRepo.countMonthlyReferrals( + userName, + now.getMonthValue(), + now.getYear()); + + logger.info("referralCount :" + referralCount); + + + if (referralCount >= 10) { + + addHwcReferalAshaIncentiveRecord( + hwcReferralEnumeration, + ashaId, + userName + ); } + } + // ---- Single batch insert instead of N individual saves ---- + if (!recordsToSave.isEmpty()) { + recordRepo.saveAll(recordsToSave); } - dtos.add(modelMapper.map(entry, IncentiveRecordDTO.class)); + } catch (Exception e) { + logger.error("Error in incentiveOfNcdReferal for ashaId={}, stateId={}", ashaId, stateId, e); + } + } + + // Helper — record banana + private void addNCDandCBACIncentiveRecord(IncentiveActivity activity, Integer ashaId, + Long benId, Timestamp createdDate, String userName) { + + String lockKey = activity.getId() + "_" + benId + "_" + createdDate; + + Object lock = lockMap.computeIfAbsent(lockKey, k -> new Object()); + synchronized (lock){ + IncentiveActivityRecord incentiveActivityRecord = recordRepo.findRecordByActivityIdCreatedDateBenId(activity.getId(),createdDate,benId,ashaId); + if(incentiveActivityRecord==null){ + Timestamp now = Timestamp.valueOf(LocalDateTime.now()); + IncentiveActivityRecord record = new IncentiveActivityRecord(); + record.setActivityId(activity.getId()); + record.setCreatedDate(createdDate); + record.setCreatedBy(userName); + record.setStartDate(createdDate); + record.setEndDate(createdDate); + record.setUpdatedDate(now); + record.setUpdatedBy(userName); + record.setBenId(benId); + record.setAshaId(ashaId); + record.setAmount(Long.valueOf(activity.getRate())); + recordRepo.save(record); + } + lockMap.remove(lockKey, lock); + + } - }); - Gson gson = new GsonBuilder().setDateFormat("MMM dd, yyyy h:mm:ss a").create(); - return gson.toJson(dtos); } - private void checkMonthlyAshaIncentive(Integer ashaId){ - IncentiveActivity MOBILEBILLREIMB_ACTIVITY = incentivesRepo.findIncentiveMasterByNameAndGroup("MOBILE_BILL_REIMB", GroupName.OTHER_INCENTIVES.getDisplayName()); - IncentiveActivity ADDITIONAL_ASHA_INCENTIVE = incentivesRepo.findIncentiveMasterByNameAndGroup("ADDITIONAL_ASHA_INCENTIVE", GroupName.ADDITIONAL_INCENTIVE.getDisplayName()); - IncentiveActivity ASHA_MONTHLY_ROUTINE = incentivesRepo.findIncentiveMasterByNameAndGroup("ASHA_MONTHLY_ROUTINE", GroupName.ASHA_MONTHLY_ROUTINE.getDisplayName()); - if(MOBILEBILLREIMB_ACTIVITY!=null){ - addMonthlyAshaIncentiveRecord(MOBILEBILLREIMB_ACTIVITY,ashaId); + + private String resolveGroupName(Integer stateId) { + if (stateId.equals(StateCode.CG.getStateCode())) { + logger.info("State id: {}", StateCode.CG.getStateCode()); + return GroupName.ACTIVITY.getDisplayName(); } - if(ADDITIONAL_ASHA_INCENTIVE!=null){ - addMonthlyAshaIncentiveRecord(ADDITIONAL_ASHA_INCENTIVE,ashaId); + logger.info("State id: {}", stateId); + return GroupName.NCD.getDisplayName(); + } + + private void checkMonthlyAshaIncentive(Integer ashaId) { + + try { + String userName = userRepo.getUserNamedByUserId(ashaId); + + IncentiveActivity MOBILEBILLREIMB_ACTIVITY = incentivesRepo.findIncentiveMasterByNameAndGroup("MOBILE_BILL_REIMB", GroupName.OTHER_INCENTIVES.getDisplayName()); + IncentiveActivity ADDITIONAL_ASHA_INCENTIVE = incentivesRepo.findIncentiveMasterByNameAndGroup("ADDITIONAL_ASHA_INCENTIVE", GroupName.ADDITIONAL_INCENTIVE.getDisplayName()); + IncentiveActivity ASHA_MONTHLY_ROUTINE = incentivesRepo.findIncentiveMasterByNameAndGroup("ASHA_MONTHLY_ROUTINE", GroupName.ASHA_MONTHLY_ROUTINE.getDisplayName()); + if (MOBILEBILLREIMB_ACTIVITY != null) { + addMonthlyAshaIncentiveRecord(MOBILEBILLREIMB_ACTIVITY, ashaId, userName); + } + if (ADDITIONAL_ASHA_INCENTIVE != null) { + addMonthlyAshaIncentiveRecord(ADDITIONAL_ASHA_INCENTIVE, ashaId, userName); + + } + + if (ASHA_MONTHLY_ROUTINE != null) { + addMonthlyAshaIncentiveRecord(ASHA_MONTHLY_ROUTINE, ashaId, userName); + + } + } catch (Exception e) { + logger.error("Error in addMonthlyAshaIncentiveRecord", e); } - if(ASHA_MONTHLY_ROUTINE!=null){ - addMonthlyAshaIncentiveRecord(ASHA_MONTHLY_ROUTINE,ashaId); + } + + private void checkMonthlyAshaIncentiveForCg(Integer ashaId) { + try { + String userName = userRepo.getUserNamedByUserId(ashaId); + + IncentiveActivity MONTHLY_HONORARIUM = incentivesRepo.findIncentiveMasterByNameAndGroup("MONTHLY_HONORARIUM", GroupName.ACTIVITY.getDisplayName()); + IncentiveActivity MITANIN_REGISTER_5_INFO_FILL = incentivesRepo.findIncentiveMasterByNameAndGroup("MITANIN_REGISTER_5_INFO_FILL", GroupName.ACTIVITY.getDisplayName()); + IncentiveActivity MITANIN_REGISTER = incentivesRepo.findIncentiveMasterByNameAndGroup("MITANIN_REGISTER", GroupName.ACTIVITY.getDisplayName()); + if (MONTHLY_HONORARIUM != null) { + addMonthlyAshaIncentiveRecord(MONTHLY_HONORARIUM, ashaId, userName); + } + if (MITANIN_REGISTER_5_INFO_FILL != null) { + addMonthlyAshaIncentiveRecord(MITANIN_REGISTER_5_INFO_FILL, ashaId, userName); + + } + + if (MITANIN_REGISTER != null) { + addMonthlyAshaIncentiveRecord(MITANIN_REGISTER, ashaId, userName); + + } + } catch (Exception e) { + logger.error("Error in addMonthlyAshaIncentiveRecord", e); } + } - private void addMonthlyAshaIncentiveRecord(IncentiveActivity incentiveActivity, Integer ashaId){ - Timestamp timestamp = Timestamp.valueOf(LocalDateTime.now()); + private void addMonthlyAshaIncentiveRecord(IncentiveActivity incentiveActivity, Integer ashaId, String userName) { + try { + Timestamp timestamp = Timestamp.valueOf(LocalDateTime.now()); + + Timestamp startOfMonth = Timestamp.valueOf(LocalDate.now().withDayOfMonth(1).atStartOfDay()); + Timestamp endOfMonth = Timestamp.valueOf(LocalDate.now().withDayOfMonth(LocalDate.now().lengthOfMonth()).atTime(23, 59, 59)); + + IncentiveActivityRecord record = recordRepo.findRecordByActivityIdCreatedDateBenId( + incentiveActivity.getId(), + 0L, + ashaId, + startOfMonth, + endOfMonth + ); + + + if (record == null) { + record = new IncentiveActivityRecord(); + record.setActivityId(incentiveActivity.getId()); + record.setCreatedDate(timestamp); + record.setCreatedBy(userName); + record.setStartDate(timestamp); + record.setEndDate(timestamp); + record.setUpdatedDate(timestamp); + record.setUpdatedBy(userName); + record.setBenId(0L); + record.setAshaId(ashaId); + record.setIsEligible(true); + record.setIsDefaultActivity(true); + record.setAmount(Long.valueOf(incentiveActivity.getRate())); + recordRepo.save(record); + } + } catch (Exception e) { + logger.error("Error in addMonthlyAshaIncentiveRecord", e); + + } + + } + private void addHwcReferalAshaIncentiveRecord(IncentiveActivity incentiveActivity, Integer ashaId, String userName) { + try { + Timestamp timestamp = Timestamp.valueOf(LocalDateTime.now()); + + Timestamp startOfMonth = Timestamp.valueOf(LocalDate.now().withDayOfMonth(1).atStartOfDay()); + Timestamp endOfMonth = Timestamp.valueOf(LocalDate.now().withDayOfMonth(LocalDate.now().lengthOfMonth()).atTime(23, 59, 59)); + + IncentiveActivityRecord record = recordRepo.findRecordByActivityIdCreatedDateBenId( + incentiveActivity.getId(), + 0L, + ashaId, + startOfMonth, + endOfMonth + ); + + + if (record == null) { + record = new IncentiveActivityRecord(); + record.setActivityId(incentiveActivity.getId()); + record.setCreatedDate(timestamp); + record.setCreatedBy(userName); + record.setStartDate(timestamp); + record.setEndDate(timestamp); + record.setUpdatedDate(timestamp); + record.setUpdatedBy(userName); + record.setBenId(0L); + record.setAshaId(ashaId); + record.setIsEligible(true); + record.setIsDefaultActivity(false); + record.setAmount(Long.valueOf(incentiveActivity.getRate())); + recordRepo.save(record); + } + } catch (Exception e) { + logger.error("Error in addMonthlyAshaIncentiveRecord", e); + + } + + } + + private void addIncentiveForIronTablets(Integer userId) { + IncentiveActivity incentiveActivityAM = incentivesRepo.findIncentiveMasterByNameAndGroup("NATIONAL_IRON_PLUS", GroupName.CHILD_HEALTH.getDisplayName()); + IncentiveActivity incentiveActivityCG = incentivesRepo.findIncentiveMasterByNameAndGroup("NATIONAL_IRON_PLUS", GroupName.ACTIVITY.getDisplayName()); + + String userName = userService.getUserDetail(userId).getUserName(); + Integer stateId = userService.getUserDetail(userId).getStateId(); + if (userName != null) { + List eligibleCoupleRegisters = eligibleCoupleRegisterRepo.findByCreatedBy(userName); + List ifaFormSubmissionData = ifaFormSubmissionRepository.findByUserId(userId); + logger.info("eligibleCoupleRegisters :" + eligibleCoupleRegisters.size()); + logger.info("ifaFormSubmissionData :" + ifaFormSubmissionData.size()); + + if (!eligibleCoupleRegisters.isEmpty() && !ifaFormSubmissionData.isEmpty()) { + + int percentage = (int) (((double) ifaFormSubmissionData.size() + / eligibleCoupleRegisters.size()) * 100); + + logger.info("IFA Count : {}", ifaFormSubmissionData.size()); + logger.info("Eligible Couple Count : {}", eligibleCoupleRegisters.size()); + logger.info("Percentage : {}", percentage); + + if (percentage >= 50) { + + if (stateId.equals(StateCode.AM.getStateCode())) { + addIFAIncentive( + ifaFormSubmissionData.get(ifaFormSubmissionData.size() - 1), + incentiveActivityAM, + userId, + userName); + } + + if (stateId.equals(StateCode.CG.getStateCode())) { + addIFAIncentive( + ifaFormSubmissionData.get(ifaFormSubmissionData.size() - 1), + incentiveActivityCG, + userId, + userName); + } + } + } + + } + + + } + + private void addIFAIncentive(IFAFormSubmissionData ifaFormSubmissionData, IncentiveActivity incentiveActivityAM, Integer userId, String userName) { Timestamp startOfMonth = Timestamp.valueOf(LocalDate.now().withDayOfMonth(1).atStartOfDay()); Timestamp endOfMonth = Timestamp.valueOf(LocalDate.now().withDayOfMonth(LocalDate.now().lengthOfMonth()).atTime(23, 59, 59)); - IncentiveActivityRecord record = recordRepo.findRecordByActivityIdCreatedDateBenId( - incentiveActivity.getId(), - startOfMonth, - endOfMonth, - 0L, - ashaId - ); - - - if (record == null) { - record = new IncentiveActivityRecord(); - record.setActivityId(incentiveActivity.getId()); - record.setCreatedDate(timestamp); - record.setCreatedBy(userRepo.getUserNamedByUserId(ashaId)); - record.setStartDate(timestamp); - record.setEndDate(timestamp); - record.setUpdatedDate(timestamp); - record.setUpdatedBy(userRepo.getUserNamedByUserId(ashaId)); - record.setBenId(0L); - record.setAshaId(ashaId); - record.setAmount(Long.valueOf(incentiveActivity.getRate())); - recordRepo.save(record); + String lockKey = incentiveActivityAM.getId() + "_" + 0 + "_" + startOfMonth; + + Object lock = lockMap.computeIfAbsent(lockKey, k -> new Object()); + Timestamp timestamp = Timestamp.valueOf(LocalDateTime.now()); + + + logger.info("IFA incentive"); + synchronized (lock){ + IncentiveActivityRecord incentiveActivityRecord = recordRepo.findRecordByActivityIdCreatedDateBenId( + incentiveActivityAM.getId(), + 0L, + userId, + startOfMonth, + endOfMonth + ); + if (incentiveActivityRecord == null) { + incentiveActivityRecord = new IncentiveActivityRecord(); + incentiveActivityRecord.setActivityId(incentiveActivityAM.getId()); + incentiveActivityRecord.setCreatedDate(timestamp); + incentiveActivityRecord.setStartDate(timestamp); + incentiveActivityRecord.setEndDate(timestamp); + incentiveActivityRecord.setUpdatedDate(timestamp); + incentiveActivityRecord.setUpdatedBy(ifaFormSubmissionData.getUserName()); + incentiveActivityRecord.setCreatedBy(ifaFormSubmissionData.getUserName()); + incentiveActivityRecord.setBenId(0L); + incentiveActivityRecord.setAshaId(userId); + incentiveActivityRecord.setAmount(Long.valueOf(incentiveActivityAM.getRate())); + recordRepo.save(incentiveActivityRecord); + logger.info("saved IFA incentive"); + + } + } + lockMap.remove(lockKey, lock); + + } diff --git a/src/main/java/com/iemr/flw/service/impl/InfantServiceImpl.java b/src/main/java/com/iemr/flw/service/impl/InfantServiceImpl.java index 03af282a..bd040990 100644 --- a/src/main/java/com/iemr/flw/service/impl/InfantServiceImpl.java +++ b/src/main/java/com/iemr/flw/service/impl/InfantServiceImpl.java @@ -98,7 +98,7 @@ private void checkAndGenerateIncentive(List infantList) { } private void addIsSncuIncentive(IncentiveActivity activity, Long benId, String createdBy, Timestamp createdDate) { - IncentiveActivityRecord incentiveActivityRecord = incentiveRecordRepo.findRecordByActivityIdCreatedDateBenId(activity.getId(),createdDate,benId); + IncentiveActivityRecord incentiveActivityRecord = incentiveRecordRepo.findRecordByActivityIdCreatedDateBenId(activity.getId(),createdDate,benId,userServiceRoleRepo.getUserIdByName(createdBy)); if(incentiveActivityRecord==null){ incentiveActivityRecord = new IncentiveActivityRecord(); diff --git a/src/main/java/com/iemr/flw/service/impl/MalariaFollowUpServiceImpl.java b/src/main/java/com/iemr/flw/service/impl/MalariaFollowUpServiceImpl.java index 175c3666..f5ef4a09 100644 --- a/src/main/java/com/iemr/flw/service/impl/MalariaFollowUpServiceImpl.java +++ b/src/main/java/com/iemr/flw/service/impl/MalariaFollowUpServiceImpl.java @@ -1,14 +1,22 @@ package com.iemr.flw.service.impl; +import com.iemr.flw.controller.CoupleController; import com.iemr.flw.domain.iemr.MalariaFollowUp; import com.iemr.flw.dto.iemr.MalariaFollowListUpDTO; import com.iemr.flw.dto.iemr.MalariaFollowUpDTO; import com.iemr.flw.repo.iemr.MalariaFollowUpRepository; +import com.iemr.flw.service.IncentiveLogicService; import com.iemr.flw.service.MalariaFollowUpService; +import com.iemr.flw.utils.JwtUtil; +import io.jsonwebtoken.Jwt; +import org.checkerframework.checker.units.qual.A; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.sql.Timestamp; import java.util.Date; import java.util.List; import java.util.stream.Collectors; @@ -19,30 +27,48 @@ public class MalariaFollowUpServiceImpl implements MalariaFollowUpService { @Autowired private MalariaFollowUpRepository repository; - public Boolean saveFollowUp(MalariaFollowUpDTO malariaFollowUpDTO) { + @Autowired + private IncentiveLogicService incentiveLogicService; + + private final Logger logger = LoggerFactory.getLogger(MalariaFollowUpServiceImpl.class); + + @Autowired + private JwtUtil jwtUtil; + + public Boolean saveFollowUp(MalariaFollowUpDTO malariaFollowUpDTO,String token) { + try { + for(MalariaFollowListUpDTO dto : malariaFollowUpDTO.getMalariaFollowListUp()){ + if (dto.getTreatmentStartDate().after(new Date())) { + return false; + } - for(MalariaFollowListUpDTO dto : malariaFollowUpDTO.getMalariaFollowListUp()){ - if (dto.getTreatmentStartDate().after(new Date())) { - return false; - } + if (dto.getTreatmentCompletionDate() != null && + dto.getTreatmentCompletionDate().before(dto.getTreatmentStartDate())) { + return false; + } - if (dto.getTreatmentCompletionDate() != null && - dto.getTreatmentCompletionDate().before(dto.getTreatmentStartDate())) { - return false; - } - if (dto.getReferralDate() != null && - dto.getReferralDate().before(dto.getTreatmentStartDate())) { - return false; - } + dto.setUserId(jwtUtil.extractUserId(token)); + MalariaFollowUp entity = new MalariaFollowUp(); + BeanUtils.copyProperties(dto, entity); + repository.save(entity); + if(entity!=null){ + incentiveLogicService.incentiveForMalariaFollowUp( + entity.getBenId(), + new Timestamp(entity.getTreatmentStartDate().getTime()), + new Timestamp(entity.getTreatmentCompletionDate().getTime()), + entity.getUserId() + ); + } + return true; + } + }catch (Exception e){ + logger.error("Exception saveFollowUp: "+ e.getMessage()); + return false; + } - MalariaFollowUp entity = new MalariaFollowUp(); - BeanUtils.copyProperties(dto, entity); - repository.save(entity); - return true; - } return false; } diff --git a/src/main/java/com/iemr/flw/service/impl/MaternalHealthServiceImpl.java b/src/main/java/com/iemr/flw/service/impl/MaternalHealthServiceImpl.java index 53ba1d28..83d261ee 100644 --- a/src/main/java/com/iemr/flw/service/impl/MaternalHealthServiceImpl.java +++ b/src/main/java/com/iemr/flw/service/impl/MaternalHealthServiceImpl.java @@ -5,19 +5,31 @@ import com.iemr.flw.dto.identity.GetBenRequestHandler; import com.iemr.flw.dto.iemr.*; import com.iemr.flw.masterEnum.GroupName; +import com.iemr.flw.masterEnum.StateCode; import com.iemr.flw.repo.identity.BeneficiaryRepo; import com.iemr.flw.repo.iemr.*; +import com.iemr.flw.service.IncentiveLogicService; +import com.iemr.flw.service.IncentiveService; import com.iemr.flw.service.MaternalHealthService; +import com.iemr.flw.utils.JwtUtil; +import com.iemr.flw.utils.exception.IEMRException; +import jakarta.transaction.Transactional; import org.modelmapper.ModelMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.util.StringUtils; import java.sql.Timestamp; import java.time.LocalDate; import java.time.LocalTime; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; +import java.time.temporal.ChronoUnit; import java.util.*; +import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; @Service @@ -58,6 +70,12 @@ public class MaternalHealthServiceImpl implements MaternalHealthService { @Autowired private IncentiveRecordRepo recordRepo; + @Autowired + private JwtUtil jwtUtil; + + @Autowired + private IncentiveLogicService incentiveLogicService; + ObjectMapper mapper = new ObjectMapper(); @@ -67,10 +85,16 @@ public class MaternalHealthServiceImpl implements MaternalHealthService { @Autowired private SMSServiceImpl smsServiceImpl; + @Autowired + private AncCounsellingCareRepo ancCounsellingCareRepo; + public static final List PNC_PERIODS = Arrays.asList("1st Day", "3rd Day", "7th Day", "14th Day", "21st Day", "28th Day", "42nd Day"); + private final ConcurrentHashMap lockMap = new ConcurrentHashMap<>(); + + @Override public String registerPregnantWoman(List pregnantWomanDTOs) { @@ -122,39 +146,68 @@ public List getANCVisits(GetBenRequestHandler dto) { try { String user = beneficiaryRepo.getUserName(dto.getAshaId()); List ancVisits = ancVisitRepo.getANCForPW(user, dto.getFromDate(), dto.getToDate()); + checkAndAddIncentives(ancVisits, dto.getAshaId()); + return ancVisits.stream() .map(anc -> mapper.convertValue(anc, ANCVisitDTO.class)) .collect(Collectors.toList()); } catch (Exception e) { - logger.error(e.getMessage()); + logger.error("ANC VISIT :---------------------" + e.getMessage()); } return null; } @Override - public String saveANCVisit(List ancVisitDTOs) { + public String saveANCVisit(List ancVisitDTOs, Integer userId) { try { + + logger.info("ANC save process started. Total records: {}", ancVisitDTOs.size()); + List ancList = new ArrayList<>(); List ancCareList = new ArrayList<>(); + ancVisitDTOs.forEach(it -> { - ANCVisit ancVisit = + + logger.info("Processing Beneficiary Id: {} ANC Visit: {}", it.getBenId(), it.getAncVisit()); + logger.info("Processing Beneficiary Id: {} ANC Visit: {}", it.getBenId(), it.getIsPaiucdId()); + + List ancVisitList = ancVisitRepo.findANCVisitByBenIdAndAncVisitAndIsActive(it.getBenId(), it.getAncVisit(), true); + ANCVisit ancVisit = new ANCVisit(); + if (!ancVisitList.isEmpty()) { + ancVisit = ancVisitList.get(0); + } + logger.info("ANC visit fetch completed"); if (ancVisit != null) { + + logger.info("Existing ANC visit found for BenId: {}", it.getBenId()); + Long id = ancVisit.getId(); modelMapper.map(it, ancVisit); ancVisit.setId(id); + } else { + + logger.info("New ANC visit will be created for BenId: {}", it.getBenId()); + ancVisit = new ANCVisit(); modelMapper.map(it, ancVisit); ancVisit.setId(null); Long benRegId = beneficiaryRepo.getRegIDFromBenId(it.getBenId()); + logger.info("Beneficiary Reg Id fetched: {}", benRegId); + // Saving data in BenVisitDetails table - PregnantWomanRegister pwr = pregnantWomanRegisterRepo.findPregnantWomanRegisterByBenIdAndIsActive(it.getBenId(), true); + PregnantWomanRegister pwr = pregnantWomanRegisterRepo + .findPregnantWomanRegisterByBenIdAndIsActive(it.getBenId(), true); + + logger.info("PregnantWomanRegister fetched"); + BenVisitDetail benVisitDetail = new BenVisitDetail(); modelMapper.map(it, benVisitDetail); + benVisitDetail.setBeneficiaryRegId(benRegId); benVisitDetail.setVisitCategory("ANC"); benVisitDetail.setVisitReason("Follow Up"); @@ -163,37 +216,70 @@ public String saveANCVisit(List ancVisitDTOs) { benVisitDetail.setModifiedBy(it.getUpdatedBy()); benVisitDetail.setLastModDate(it.getUpdatedDate()); benVisitDetail.setProviderServiceMapID(it.getProviderServiceMapID()); + + logger.info("Saving BenVisitDetail"); + benVisitDetail = benVisitDetailsRepo.save(benVisitDetail); + logger.info("BenVisitDetail saved with id: {}", benVisitDetail.getBenVisitId()); + // Saving Data in AncCare table AncCare ancCare = new AncCare(); modelMapper.map(it, ancCare); + ancCare.setBenVisitId(benVisitDetail.getBenVisitId()); ancCare.setBeneficiaryRegId(benRegId); + if (pwr != null) { + + logger.info("LMP found calculating EDD"); + ancCare.setLastMenstrualPeriodLmp(pwr.getLmpDate()); + Calendar cal = Calendar.getInstance(); cal.setTime(pwr.getLmpDate()); cal.add(Calendar.DAY_OF_WEEK, 280); + ancCare.setExpectedDateofDelivery(new Timestamp(cal.getTime().getTime())); } + ancCare.setTrimesterNumber(it.getAncVisit().shortValue()); ancCare.setModifiedBy(it.getUpdatedBy()); ancCare.setLastModDate(it.getUpdatedDate()); ancCare.setProcessed("N"); + ancCareList.add(ancCare); + + logger.info("AncCare added to list"); } + ancList.add(ancVisit); + logger.info("ANCVisit added to list"); + }); + logger.info("Saving ANCVisit list"); + ancVisitRepo.saveAll(ancList); + + logger.info("ANCVisit saved successfully"); + + logger.info("Saving AncCare list"); + ancCareRepo.saveAll(ancCareList); - checkAndAddIncentives(ancList); + + logger.info("AncCare saved successfully"); + logger.info("ANC visit details saved"); + checkAndAddIncentives(ancList, userId); + return "no of anc details saved: " + ancList.size(); + } catch (Exception e) { - logger.info("Saving ANC visit details failed with error : " + e.getMessage()); - logger.info("Saving ANC visit details failed with error : " + e); + + logger.error("Saving ANC visit details failed with error : {}", e.getMessage()); + logger.error("Full exception : ", e); + } return null; } @@ -232,6 +318,7 @@ public String savePmsmaRecords(List pmsmaDTOs) { }); pmsmaRepo.saveAll(pmsmaList); logger.info("PMSMA details saved"); + checkAndAddHighRisk(pmsmaList); return "No. of PMSMA records saved: " + pmsmaList.size(); @@ -268,7 +355,7 @@ private void checkAndAddHighRisk(List pmsmaList) { private void addIncentiveForHighRisk(IncentiveActivity incentiveActivity, PMSMA pmsma) { IncentiveActivityRecord record = recordRepo - .findRecordByActivityIdCreatedDateBenId(incentiveActivity.getId(), pmsma.getCreatedDate(), pmsma.getBenId()); + .findRecordByActivityIdCreatedDateBenId(incentiveActivity.getId(), pmsma.getCreatedDate(), pmsma.getBenId(),userRepo.getUserIdByName(pmsma.getCreatedBy())); // get bene details if (record == null) { @@ -291,7 +378,7 @@ record = new IncentiveActivityRecord(); public List getPNCVisits(GetBenRequestHandler dto) { try { String user = beneficiaryRepo.getUserName(dto.getAshaId()); - List pncVisits = pncVisitRepo.getPNCForPW(user, dto.getFromDate(), dto.getToDate()); + List pncVisits = pncVisitRepo.getPNCForPW(user); return pncVisits.stream() .map(pnc -> mapper.convertValue(pnc, PNCVisitDTO.class)) .collect(Collectors.toList()); @@ -343,7 +430,7 @@ public String savePNCVisit(List pncVisitDTOs) { pncCare.setLastModDate(it.getUpdatedDate()); pncCare.setProcessed("N"); pncCareList.add(pncCare); - checkAndAddAntaraIncentive(pncList, pncVisit); + checkAndAddAntaraIncentive(pncVisit); } pncList.add(pncVisit); }); @@ -352,232 +439,447 @@ public String savePNCVisit(List pncVisitDTOs) { logger.info("PNC visit details saved"); return "no of pnc details saved: " + pncList.size(); } catch (Exception e) { - logger.info("Saving PNC visit details failed with error : " + e.getMessage()); + logger.error("Saving PNC visit details failed", e); + } return null; } - private void checkAndAddAntaraIncentive(List recordList, PNCVisit ect) { - Integer userId = userRepo.getUserIdByName(ect.getCreatedBy()); - logger.info("ContraceptionMethod:" + ect.getContraceptionMethod()); + @Override + @Transactional + public String saveANCVisitQuestions(List dtos, String authorization) throws IEMRException { + Integer userId = jwtUtil.extractUserId(authorization); + String userName = userRepo.getUserNamedByUserId(userId); - if (ect.getContraceptionMethod() != null && ect.getContraceptionMethod().equals("MALE STERILIZATION")) { + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy"); + List entities = new ArrayList<>(); - IncentiveActivity maleSterilizationActivityAM = - incentivesRepo.findIncentiveMasterByNameAndGroup("FP_MALE_STER", GroupName.FAMILY_PLANNING.getDisplayName()); + for (AncCounsellingCareDTO dto : dtos) { - IncentiveActivity maleSterilizationActivityCH = - incentivesRepo.findIncentiveMasterByNameAndGroup("FP_MALE_STER", GroupName.ACTIVITY.getDisplayName()); - if (maleSterilizationActivityAM != null) { - addIncenticeRecord(recordList, ect, userId, maleSterilizationActivityAM); - } - if (maleSterilizationActivityCH != null) { - addIncenticeRecord(recordList, ect, userId, maleSterilizationActivityCH); + if (!StringUtils.hasText(dto.getVisitDate())) { + throw new IllegalArgumentException("visitDate is mandatory"); } - } else if (ect.getContraceptionMethod() != null && ect.getContraceptionMethod().equals("FEMALE STERILIZATION")) { - - IncentiveActivity femaleSterilizationActivityAM = - incentivesRepo.findIncentiveMasterByNameAndGroup("FP_FEMALE_STER", GroupName.FAMILY_PLANNING.getDisplayName()); - - IncentiveActivity femaleSterilizationActivityCH = - incentivesRepo.findIncentiveMasterByNameAndGroup("FP_FEMALE_STER", GroupName.ACTIVITY.getDisplayName()); - if (femaleSterilizationActivityAM != null) { - addIncenticeRecord(recordList, ect, userId, femaleSterilizationActivityAM); + AncCounsellingCareListDTO fields = dto.getFields(); + if (fields == null) { + throw new IllegalArgumentException("fields object is mandatory"); } - if (femaleSterilizationActivityCH != null) { - addIncenticeRecord(recordList, ect, userId, femaleSterilizationActivityCH); + LocalDate visitDate; + try { + visitDate = LocalDate.parse(dto.getVisitDate(), formatter); + } catch (DateTimeParseException e) { + throw new IllegalArgumentException("Invalid visitDate format, expected dd-MM-yyyy"); } - } else if (ect.getContraceptionMethod() != null && ect.getContraceptionMethod().equals("MiniLap")) { - IncentiveActivity miniLapActivity = - incentivesRepo.findIncentiveMasterByNameAndGroup("FP_MINILAP", GroupName.FAMILY_PLANNING.getDisplayName()); - if (miniLapActivity != null) { - addIncenticeRecord(recordList, ect, userId, miniLapActivity); + LocalDate homeVisitDate; + try { + homeVisitDate = StringUtils.hasText(fields.getHomeVisitDate()) + ? LocalDate.parse(fields.getHomeVisitDate(), formatter) + : visitDate; // ✅ fallback + } catch (DateTimeParseException e) { + throw new IllegalArgumentException("Invalid home_visit_date format, expected dd-MM-yyyy"); } - } else if (ect.getContraceptionMethod() != null && ect.getContraceptionMethod().equals("Condom")) { - IncentiveActivity comdomActivity = - incentivesRepo.findIncentiveMasterByNameAndGroup("FP_CONDOM", GroupName.FAMILY_PLANNING.getDisplayName()); - if (comdomActivity != null) { - addIncenticeRecord(recordList, ect, userId, comdomActivity); - } - } else if (ect.getContraceptionMethod() != null && ect.getContraceptionMethod().equals("POST PARTUM IUCD (PPIUCD) WITHIN 48 HRS OF DELIVERY")) { + AncCounsellingCare entity = new AncCounsellingCare(); + entity.setBeneficiaryId(dto.getBeneficiaryId()); + entity.setAncVisitId(0L); + entity.setVisitDate(visitDate); + entity.setHomeVisitDate(homeVisitDate); + + entity.setSelectAll(yesNoToBoolean(fields.getSelectAll())); + entity.setSwelling(yesNoToBoolean(fields.getSwelling())); + entity.setHighBp(yesNoToBoolean(fields.getHighBp())); + entity.setConvulsions(yesNoToBoolean(fields.getConvulsions())); + entity.setAnemia(yesNoToBoolean(fields.getAnemia())); + entity.setReducedFetalMovement(yesNoToBoolean(fields.getReducedFetalMovement())); + entity.setAgeRisk(yesNoToBoolean(fields.getAgeRisk())); + entity.setChildGap(yesNoToBoolean(fields.getChildGap())); + entity.setShortHeight(yesNoToBoolean(fields.getShortHeight())); + entity.setPrePregWeight(yesNoToBoolean(fields.getPrePregWeight())); + entity.setBleeding(yesNoToBoolean(fields.getBleeding())); + entity.setMiscarriageHistory(yesNoToBoolean(fields.getMiscarriageHistory())); + entity.setFourPlusDelivery(yesNoToBoolean(fields.getFourPlusDelivery())); + entity.setFirstDelivery(yesNoToBoolean(fields.getFirstDelivery())); + entity.setTwinPregnancy(yesNoToBoolean(fields.getTwinPregnancy())); + entity.setCSectionHistory(yesNoToBoolean(fields.getCSectionHistory())); + entity.setPreExistingDisease(yesNoToBoolean(fields.getPreExistingDisease())); + entity.setFeverMalaria(yesNoToBoolean(fields.getFeverMalaria())); + entity.setJaundice(yesNoToBoolean(fields.getJaundice())); + entity.setSickleCell(yesNoToBoolean(fields.getSickleCell())); + entity.setProlongedLabor(yesNoToBoolean(fields.getProlongedLabor())); + entity.setMalpresentation(yesNoToBoolean(fields.getMalpresentation())); + + entity.setUserId(userId); + entity.setCreatedBy(userName); + entity.setUpdatedBy(userName); + + entities.add(entity); + } - IncentiveActivity PPIUCDActivityAM = - incentivesRepo.findIncentiveMasterByNameAndGroup("FP_PPIUCD", GroupName.FAMILY_PLANNING.getDisplayName()); + ancCounsellingCareRepo.saveAll(entities); - IncentiveActivity PPIUCDActivityCH = - incentivesRepo.findIncentiveMasterByNameAndGroup("FP_PPIUCD", GroupName.ACTIVITY.getDisplayName()); - if (PPIUCDActivityAM != null) { - addIncenticeRecord(recordList, ect, userId, PPIUCDActivityAM); - } + return "ANC Counselling & Care data saved successfully"; + } - if (PPIUCDActivityCH != null) { - addIncenticeRecord(recordList, ect, userId, PPIUCDActivityCH); - } - } else if (ect.getContraceptionMethod() != null && ect.getContraceptionMethod().equals("POST PARTUM STERILIZATION (PPS)")) { - IncentiveActivity ppsActivityAM = - incentivesRepo.findIncentiveMasterByNameAndGroup("FP_PPS", GroupName.FAMILY_PLANNING.getDisplayName()); + @Override + public List getANCCounselling(GetBenRequestHandler requestDTO) { + + List entities = + ancCounsellingCareRepo.findAllByUserId(requestDTO.getAshaId()); + + List responseDTOList = new ArrayList<>(); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy"); + + for (AncCounsellingCare entity : entities) { + + AncCounsellingCareResponseDTO responseDTO = new AncCounsellingCareResponseDTO(); + responseDTO.setFormId("anc_form_001"); + responseDTO.setBeneficiaryId(entity.getBeneficiaryId()); // Update with actual value + responseDTO.setVisitDate(entity.getVisitDate().format(formatter)); // Format visit.getVisitDate() + + // 🔹 fields map + Map fields = new HashMap<>(); + + fields.put("home_visit_date", + entity.getHomeVisitDate() != null + ? entity.getHomeVisitDate().format(formatter) + : null); + + fields.put("select_all", booleanToYesNo(entity.getSelectAll())); + fields.put("swelling", booleanToYesNo(entity.getSwelling())); + fields.put("high_bp", booleanToYesNo(entity.getHighBp())); + fields.put("convulsions", booleanToYesNo(entity.getConvulsions())); + fields.put("anemia", booleanToYesNo(entity.getAnemia())); + fields.put("reduced_fetal_movement", booleanToYesNo(entity.getReducedFetalMovement())); + fields.put("age_risk", booleanToYesNo(entity.getAgeRisk())); + fields.put("child_gap", booleanToYesNo(entity.getChildGap())); + fields.put("short_height", booleanToYesNo(entity.getShortHeight())); + fields.put("pre_preg_weight", booleanToYesNo(entity.getPrePregWeight())); + fields.put("bleeding", booleanToYesNo(entity.getBleeding())); + fields.put("miscarriage_history", booleanToYesNo(entity.getMiscarriageHistory())); + fields.put("four_plus_delivery", booleanToYesNo(entity.getFourPlusDelivery())); + fields.put("first_delivery", booleanToYesNo(entity.getFirstDelivery())); + fields.put("twin_pregnancy", booleanToYesNo(entity.getTwinPregnancy())); + fields.put("c_section_history", booleanToYesNo(entity.getCSectionHistory())); + fields.put("pre_existing_disease", booleanToYesNo(entity.getPreExistingDisease())); + fields.put("fever_malaria", booleanToYesNo(entity.getFeverMalaria())); + fields.put("jaundice", booleanToYesNo(entity.getJaundice())); + fields.put("sickle_cell", booleanToYesNo(entity.getSickleCell())); + fields.put("prolonged_labor", booleanToYesNo(entity.getProlongedLabor())); + fields.put("malpresentation", booleanToYesNo(entity.getMalpresentation())); + + responseDTO.setFields(fields); + responseDTOList.add(responseDTO); - IncentiveActivity ppsActivityCH = - incentivesRepo.findIncentiveMasterByNameAndGroup("FP_PPS", GroupName.ACTIVITY.getDisplayName()); - if (ppsActivityAM != null) { - addIncenticeRecord(recordList, ect, userId, ppsActivityAM); - } - if (ppsActivityCH != null) { - addIncenticeRecord(recordList, ect, userId, ppsActivityCH); - } } + return responseDTOList; } - private void addIncenticeRecord(List recordList, PNCVisit ect, Integer userId, IncentiveActivity antaraActivity) { - IncentiveActivityRecord record = recordRepo - .findRecordByActivityIdCreatedDateBenId(antaraActivity.getId(), ect.getCreatedDate(), ect.getBenId()); - // get bene details + private String booleanToYesNo(Boolean value) { + return Boolean.TRUE.equals(value) ? "Yes" : "No"; + } - if (record == null) { - record = new IncentiveActivityRecord(); - record.setActivityId(antaraActivity.getId()); - record.setCreatedDate(ect.getPncDate()); - record.setCreatedBy(ect.getCreatedBy()); - record.setStartDate(ect.getPncDate()); - record.setEndDate(ect.getPncDate()); - record.setUpdatedDate(ect.getPncDate()); - record.setUpdatedBy(ect.getCreatedBy()); - record.setBenId(ect.getBenId()); - record.setAshaId(userId); - record.setAmount(Long.valueOf(antaraActivity.getRate())); - recordRepo.save(record); - } + + private Boolean yesNoToBoolean(String value) { + return "Yes".equalsIgnoreCase(value); } - private void checkAndAddIncentives(List ancList) { + private void checkAndAddAntaraIncentive(PNCVisit ect) { + Integer userId = userRepo.getUserIdByName(ect.getCreatedBy()); + Integer stateId = userRepo.getUserRole(userId).get(0).getStateId(); - IncentiveActivity anc1Activity = - incentivesRepo.findIncentiveMasterByNameAndGroup("ANC_REGISTRATION_1ST_TRIM", GroupName.MATERNAL_HEALTH.getDisplayName()); + // logic for assam + if (stateId.equals(StateCode.AM.getStateCode())) { + if (ect.getContraceptionMethod() != null && ect.getContraceptionMethod().equals("MALE STERILIZATION")) { - IncentiveActivity ancFullActivityAM = - incentivesRepo.findIncentiveMasterByNameAndGroup("FULL_ANC", GroupName.MATERNAL_HEALTH.getDisplayName()); + IncentiveActivity maleSterilizationActivityAM = + incentivesRepo.findIncentiveMasterByNameAndGroup("FP_MALE_STER", GroupName.FAMILY_PLANNING.getDisplayName()); - IncentiveActivity ancFullActivityCH = - incentivesRepo.findIncentiveMasterByNameAndGroup("ANC_FOUR_CHECKUPS_SUPPORT", GroupName.ACTIVITY.getDisplayName()); + if (maleSterilizationActivityAM != null) { + addIncenticeRecord(ect, userId, maleSterilizationActivityAM); + } - IncentiveActivity comprehensiveAbortionActivityAM = incentivesRepo.findIncentiveMasterByNameAndGroup("COMPREHENSIVE_ABORTION_CARE", GroupName.MATERNAL_HEALTH.getDisplayName()); - IncentiveActivity comprehensiveAbortionActivityCH = incentivesRepo.findIncentiveMasterByNameAndGroup("COMPREHENSIVE_ABORTION_CARE", GroupName.ACTIVITY.getDisplayName()); - IncentiveActivity identifiedHrpActivityAM = incentivesRepo.findIncentiveMasterByNameAndGroup("EPMSMA_HRP_IDENTIFIED", GroupName.MATERNAL_HEALTH.getDisplayName()); - IncentiveActivity identifiedHrpActivityCH = incentivesRepo.findIncentiveMasterByNameAndGroup("EPMSMA_HRP_IDENTIFIED", GroupName.ACTIVITY.getDisplayName()); + } + if (ect.getContraceptionMethod() != null && ect.getContraceptionMethod().equals("FEMALE STERILIZATION")) { - IncentiveActivity paiucdActivityAM = - incentivesRepo.findIncentiveMasterByNameAndGroup("FP_PAIUCD", GroupName.FAMILY_PLANNING.getDisplayName()); + IncentiveActivity femaleSterilizationActivityAM = + incentivesRepo.findIncentiveMasterByNameAndGroup("FP_FEMALE_STER", GroupName.FAMILY_PLANNING.getDisplayName()); + if (femaleSterilizationActivityAM != null) { + addIncenticeRecord(ect, userId, femaleSterilizationActivityAM); + } - IncentiveActivity paiucdActivityCH = - incentivesRepo.findIncentiveMasterByNameAndGroup("FP_PAIUCD", GroupName.ACTIVITY.getDisplayName()); + } + if (ect.getContraceptionMethod() != null && (ect.getContraceptionMethod().equals("MiniLap") || ect.getContraceptionMethod().equals("POST PARTUM STERILIZATION (PPS)"))) { - ancList.forEach(ancVisit -> { - if (paiucdActivityAM != null) { - if (ancVisit.getIsPaiucd()!= null) { - if (ancVisit.getIsPaiucd().equals("Yes")) { - recordAncRelatedIncentive(paiucdActivityAM, ancVisit); + IncentiveActivity miniLapActivity = + incentivesRepo.findIncentiveMasterByNameAndGroup("FP_PPS", GroupName.FAMILY_PLANNING.getDisplayName()); + if (miniLapActivity != null) { + addIncenticeRecord(ect, userId, miniLapActivity); + } + } + if (ect.getContraceptionMethod() != null && ect.getContraceptionMethod().equals("Condom")) { - } + IncentiveActivity comdomActivity = + incentivesRepo.findIncentiveMasterByNameAndGroup("FP_CONDOM", GroupName.FAMILY_PLANNING.getDisplayName()); + if (comdomActivity != null) { + addIncenticeRecord(ect, userId, comdomActivity); + } + } + if (ect.getContraceptionMethod() != null && ect.getContraceptionMethod().equals("POST PARTUM IUCD (PPIUCD) WITHIN 48 HRS OF DELIVERY")) { + + IncentiveActivity PPIUCDActivityAM = + incentivesRepo.findIncentiveMasterByNameAndGroup("FP_PPIUCD", GroupName.FAMILY_PLANNING.getDisplayName()); + + if (PPIUCDActivityAM != null) { + addIncenticeRecord(ect, userId, PPIUCDActivityAM); } } - if (paiucdActivityCH != null) { - if (ancVisit.getIsPaiucd()!= null) { - if (ancVisit.getIsPaiucd().equals("Yes")) { + } + // logic for cg + if (stateId.equals(StateCode.CG.getStateCode())) { + if (ect.getAnyContraceptionMethod() != null) { + if (ect.getAnyContraceptionMethod()) { + IncentiveActivity femaleSterilizationActivityCH = + incentivesRepo.findIncentiveMasterByNameAndGroup("FP_FEMALE_STER", GroupName.ACTIVITY.getDisplayName()); + - recordAncRelatedIncentive(paiucdActivityCH, ancVisit); + IncentiveActivity PPIUCDActivityCH = + incentivesRepo.findIncentiveMasterByNameAndGroup("FP_PPIUCD", GroupName.ACTIVITY.getDisplayName()); + + IncentiveActivity maleSterilizationActivityCH = + incentivesRepo.findIncentiveMasterByNameAndGroup("FP_MALE_STER", GroupName.ACTIVITY.getDisplayName()); + + if (PPIUCDActivityCH != null) { + if (ect.getContraceptionMethod().equals("POST PARTUM IUCD (PPIUCD)")) { + addIncenticeRecord(ect, userId, PPIUCDActivityCH); + + } } - } - } - if (anc1Activity != null) { - if (ancVisit.getAncVisit() != null) { + if (femaleSterilizationActivityCH != null) { + if (ect.getContraceptionMethod().equals("FEMALE STERILIZATION")) { + addIncenticeRecord(ect, userId, femaleSterilizationActivityCH); - if (ancVisit.getAncVisit() == 1) { - recordAncRelatedIncentive(anc1Activity, ancVisit); + } + } + + + if (maleSterilizationActivityCH != null) { + if (ect.getContraceptionMethod().equals("MALE STERILIZATION")) { + addIncenticeRecord(ect, userId, maleSterilizationActivityCH); + + } } } + } - if (ancFullActivityAM != null) { - if (ancVisit.getAncVisit() == 4 || ancVisit.getAncVisit() == 2 || ancVisit.getAncVisit() == 3) { - recordAncRelatedIncentive(ancFullActivityAM, ancVisit); + + if (ect.getMotherDangerSign() != null + && !ect.getMotherDangerSign().isEmpty() + && ect.getPncPeriod() == 42) { + IncentiveActivity highRiskPostpartumCareActivityCH = + incentivesRepo.findIncentiveMasterByNameAndGroup("HIGH_RISK_POSTPARTUM_CARE", GroupName.ACTIVITY.getDisplayName()); + if (highRiskPostpartumCareActivityCH != null) { + addIncenticeRecord(ect, userId, highRiskPostpartumCareActivityCH); + } } - if (ancFullActivityCH != null) { - if (ancVisit.getAncVisit() != null) { - if (ancVisit.getAncVisit() == 4) { - recordAncRelatedIncentive(ancFullActivityCH, ancVisit); - } + if (ect.getPncPeriod() == 42) { + IncentiveActivity highRiskPostpartumHealthCareActivityCH = + incentivesRepo.findIncentiveMasterByNameAndGroup("HIGH_RISK_POSTPARTUM_HEALTH_CHECK", GroupName.ACTIVITY.getDisplayName()); + + if (highRiskPostpartumHealthCareActivityCH != null) { + addIncenticeRecord(ect, userId, highRiskPostpartumHealthCareActivityCH); + } + } - if (comprehensiveAbortionActivityAM != null) { - if (ancVisit.getIsAborted() != null) { - if (ancVisit.getIsAborted()) { - recordAncRelatedIncentive(comprehensiveAbortionActivityAM, ancVisit); - } - } + if (ect.getPncPeriod() == 1 || ect.getPncPeriod() == 3 || ect.getPncPeriod() == 7) { + if(ect.getContraceptionMethod()!=null){ + if ((ect.getContraceptionMethod().equals("POST PARTUM STERILIZATION (PPS)") + || ect.getContraceptionMethod().equals("MiniLap")) + && ect.getSterilisationDate() != null) { + IncentiveActivity ppsActivityCH = + incentivesRepo.findIncentiveMasterByNameAndGroup("FP_PPS", GroupName.ACTIVITY.getDisplayName()); + if (ppsActivityCH != null) { + + addIncenticeRecord(ect, userId, ppsActivityCH); + + } + } + } } - if (comprehensiveAbortionActivityCH != null) { - if (ancVisit.getIsAborted() != null) { - if (ancVisit.getIsAborted()) { - recordAncRelatedIncentive(comprehensiveAbortionActivityCH, ancVisit); - } - } + } + + + } + + + @Transactional + private void addIncenticeRecord(PNCVisit ect, Integer userId, IncentiveActivity antaraActivity) { + + String lockKey = antaraActivity.getId() + "_" + ect.getBenId() + "_" + ect.getPncDate(); + + Object lock = lockMap.computeIfAbsent(lockKey, k -> new Object()); + + synchronized (lock) { + IncentiveActivityRecord record = recordRepo + .findRecordByActivityIdCreatedDateBenId(antaraActivity.getId(), ect.getPncDate(), ect.getBenId(),userId); + if (record == null) { + record = new IncentiveActivityRecord(); + record.setActivityId(antaraActivity.getId()); + record.setCreatedDate(ect.getPncDate()); + record.setCreatedBy(ect.getCreatedBy()); + record.setStartDate(ect.getPncDate()); + record.setEndDate(ect.getPncDate()); + record.setUpdatedDate(ect.getPncDate()); + record.setUpdatedBy(ect.getCreatedBy()); + record.setBenId(ect.getBenId()); + record.setAshaId(userId); + record.setAmount(Long.valueOf(antaraActivity.getRate())); + recordRepo.save(record); } - if (identifiedHrpActivityAM != null) { - if (ancVisit.getIsHrpConfirmed() != null) { - if (ancVisit.getIsHrpConfirmed()) { - recordAncRelatedIncentive(identifiedHrpActivityAM, ancVisit); + } + + lockMap.remove(lockKey, lock); + } + + + private void checkAndAddIncentives(List ancList, Integer userId) { + + Integer stateId = userRepo.getUserRole(userId).get(0).getStateId(); + + + // ✅ State 5 — Assam + if (stateId.equals(5)) { + IncentiveActivity anc1Activity = incentivesRepo.findIncentiveMasterByNameAndGroup( + "ANC_REGISTRATION_1ST_TRIM", GroupName.MATERNAL_HEALTH.getDisplayName()); + IncentiveActivity ancFullActivityAM = incentivesRepo.findIncentiveMasterByNameAndGroup( + "FULL_ANC", GroupName.MATERNAL_HEALTH.getDisplayName()); + IncentiveActivity identifiedHrpActivityAM = incentivesRepo.findIncentiveMasterByNameAndGroup("EPMSMA_HRP_IDENTIFIED", GroupName.MATERNAL_HEALTH.getDisplayName()); + IncentiveActivity comprehensiveAbortionActivityAM = incentivesRepo.findIncentiveMasterByNameAndGroup( + "COMPREHENSIVE_ABORTION_CARE", GroupName.MATERNAL_HEALTH.getDisplayName()); + IncentiveActivity paiucdActivityAM = incentivesRepo.findIncentiveMasterByNameAndGroup( + "FP_PAIUCD", GroupName.FAMILY_PLANNING.getDisplayName()); + + IncentiveActivity iucdActivityAM = incentivesRepo.findIncentiveMasterByNameAndGroup( + "FP_IUCD", GroupName.FAMILY_PLANNING.getDisplayName()); + ancList.forEach(ancVisit -> { + if(ancVisit.getIsAborted()){ + if (paiucdActivityAM != null && ancVisit.getIsPaiucdId() != null && ancVisit.getIsPaiucdId()==2 && + ancVisit.getIsPaiucd().toString().contains("Tubectomy")) { + recordAncRelatedIncentive(paiucdActivityAM, ancVisit); + } + if (iucdActivityAM != null && ancVisit.getIsPaiucdId() != null && ancVisit.getIsPaiucdId()==1 && + ancVisit.getIsPaiucd().toString().contains("Copper-T")) { + recordAncRelatedIncentive(iucdActivityAM, ancVisit); } } - } - if (identifiedHrpActivityCH != null) { - if (ancVisit.getIsHrpConfirmed() != null) { - if (ancVisit.getIsHrpConfirmed()) { - recordAncRelatedIncentive(identifiedHrpActivityCH, ancVisit); + + if (anc1Activity != null && ancVisit.getAncVisit() != null + && ancVisit.getAncVisit() == 1) { + recordAncFirstTRIMIncentive(anc1Activity, ancVisit); + } + + if (ancFullActivityAM != null && ancVisit.getAncVisit() != null + && ancVisit.getAncVisit() == 4) { + recordFullAncIncentive(ancFullActivityAM, ancVisit); + } + + + if (comprehensiveAbortionActivityAM != null && ancVisit.getIsAborted() != null + && ancVisit.getIsAborted()) { + recordAncRelatedIncentive(comprehensiveAbortionActivityAM, ancVisit); + } + + + if (identifiedHrpActivityAM != null && ancVisit.getIsHrpConfirmed() != null + && ancVisit.getIsHrpConfirmed()) { + recordAncRelatedIncentive(identifiedHrpActivityAM, ancVisit); + } + + + }); + } + + // ✅ State 8 + if (stateId.equals(8)) { + + IncentiveActivity ancFullActivityCH = incentivesRepo.findIncentiveMasterByNameAndGroup( + "ANC_FOUR_CHECKUPS_SUPPORT", GroupName.ACTIVITY.getDisplayName()); + IncentiveActivity comprehensiveAbortionActivityCH = incentivesRepo.findIncentiveMasterByNameAndGroup( + "COMPREHENSIVE_ABORTION_CARE", GroupName.ACTIVITY.getDisplayName()); + IncentiveActivity identifiedHrpActivityCH = incentivesRepo.findIncentiveMasterByNameAndGroup( + "EPMSMA_HRP_IDENTIFIED", GroupName.ACTIVITY.getDisplayName()); + IncentiveActivity paiucdActivityCH = incentivesRepo.findIncentiveMasterByNameAndGroup( + "FP_PAIUCD", GroupName.ACTIVITY.getDisplayName()); + + IncentiveActivity iucdActivityCH = incentivesRepo.findIncentiveMasterByNameAndGroup( + "FP_IUCD", GroupName.ACTIVITY.getDisplayName()); + + ancList.forEach(ancVisit -> { + + + if (ancVisit.getIsAborted()) { + if (paiucdActivityCH != null && ancVisit.getIsPaiucdId() != null && ancVisit.getIsPaiucdId()==2 && + ancVisit.getIsPaiucd().toString().contains("Tubectomy")) { + recordAncRelatedIncentive(paiucdActivityCH, ancVisit); + } + if (iucdActivityCH != null && ancVisit.getIsPaiucdId() != null && ancVisit.getIsPaiucdId()==1 + && ancVisit.getIsPaiucd().toString().contains("Copper-T")) { + recordAncRelatedIncentive(iucdActivityCH, ancVisit); } } - } - }); + if (ancFullActivityCH != null && ancVisit.getAncVisit() != null + && ancVisit.getAncVisit() == 4) { + recordFullAncIncentive(ancFullActivityCH, ancVisit); + } + + + if (identifiedHrpActivityCH != null && ancVisit.getIsHrpConfirmed() != null + && ancVisit.getIsHrpConfirmed()) { + recordAncRelatedIncentive(identifiedHrpActivityCH, ancVisit); + } + }); + } } private void recordAncRelatedIncentive(IncentiveActivity incentiveActivity, ANCVisit ancVisit) { - IncentiveActivityRecord record = recordRepo.findRecordByActivityIdCreatedDateBenId(incentiveActivity.getId(), ancVisit.getCreatedDate(), ancVisit.getBenId()); Integer userId = userRepo.getUserIdByName(ancVisit.getCreatedBy()); + IncentiveActivityRecord record = recordRepo.findRecordByActivityIdCreatedDateBenId(incentiveActivity.getId(), ancVisit.getCreatedDate(), ancVisit.getBenId(),userId); + if (record == null) { record = new IncentiveActivityRecord(); record.setActivityId(incentiveActivity.getId()); - record.setCreatedDate(ancVisit.getCreatedDate()); + record.setCreatedDate(ancVisit.getAncDate()); record.setCreatedBy(ancVisit.getCreatedBy()); - record.setUpdatedDate(ancVisit.getCreatedDate()); + record.setUpdatedDate(ancVisit.getAncDate()); record.setUpdatedBy(ancVisit.getCreatedBy()); - record.setStartDate(ancVisit.getCreatedDate()); - record.setEndDate(ancVisit.getCreatedDate()); + record.setStartDate(ancVisit.getAncDate()); + record.setEndDate(ancVisit.getAncDate()); record.setBenId(ancVisit.getBenId()); record.setAshaId(userId); record.setAmount(Long.valueOf(incentiveActivity.getRate())); @@ -586,6 +888,74 @@ record = new IncentiveActivityRecord(); } + private void recordAncFirstTRIMIncentive(IncentiveActivity incentiveActivity, ANCVisit ancVisit) { + Integer userId = userRepo.getUserIdByName(ancVisit.getCreatedBy()); + + if (ancVisit.getAncDate() == null || ancVisit.getLmpDate() == null) { + return; + } + + LocalDate ancLocalDate = ancVisit.getAncDate().toLocalDateTime().toLocalDate(); + LocalDate lmpLocalDate = ancVisit.getLmpDate().toLocalDateTime().toLocalDate(); + + long weeksBetween = ChronoUnit.WEEKS.between(lmpLocalDate, ancLocalDate); + + if (weeksBetween < 0 || weeksBetween >= 12) { + return; + } + + IncentiveActivityRecord record = recordRepo.findRecordByActivityIdCreatedDateBenId( + incentiveActivity.getId(), + ancVisit.getCreatedDate(), + ancVisit.getBenId(),userId + ); + + + if (record == null) { + record = new IncentiveActivityRecord(); + record.setActivityId(incentiveActivity.getId()); + record.setCreatedDate(ancVisit.getLmpDate()); + record.setCreatedBy(ancVisit.getCreatedBy()); + record.setUpdatedDate(ancVisit.getLmpDate()); + record.setUpdatedBy(ancVisit.getCreatedBy()); + record.setStartDate(ancVisit.getLmpDate()); + record.setEndDate(ancVisit.getLmpDate()); + record.setBenId(ancVisit.getBenId()); + record.setAshaId(userId); + record.setAmount(Long.valueOf(incentiveActivity.getRate())); + recordRepo.save(record); + } + } + + private void recordFullAncIncentive(IncentiveActivity incentiveActivity, ANCVisit ancVisit) { + Integer userId = userRepo.getUserIdByName(ancVisit.getCreatedBy()); + if (userId == null) { + return; + } + IncentiveActivityRecord existRecord = recordRepo.findRecordByActivityIdCreatedDateBenId( + incentiveActivity.getId(), + ancVisit.getAncDate(), + ancVisit.getBenId(),userId + ); + + + if (existRecord != null) { + return; + } + + IncentiveActivityRecord record = new IncentiveActivityRecord(); + record.setActivityId(incentiveActivity.getId()); + record.setCreatedDate(ancVisit.getAncDate()); + record.setCreatedBy(ancVisit.getCreatedBy()); + record.setUpdatedDate(ancVisit.getAncDate()); + record.setUpdatedBy(ancVisit.getCreatedBy()); + record.setStartDate(ancVisit.getAncDate()); + record.setEndDate(ancVisit.getAncDate()); + record.setBenId(ancVisit.getBenId()); + record.setAshaId(userId); + record.setAmount(Long.valueOf(incentiveActivity.getRate())); + recordRepo.save(record); + } public void sendAncDueTomorrowNotifications(String ashaId) { try { diff --git a/src/main/java/com/iemr/flw/service/impl/NotificationSchedulerService.java b/src/main/java/com/iemr/flw/service/impl/NotificationSchedulerService.java index a5b6dd77..39314c8f 100644 --- a/src/main/java/com/iemr/flw/service/impl/NotificationSchedulerService.java +++ b/src/main/java/com/iemr/flw/service/impl/NotificationSchedulerService.java @@ -1,27 +1,19 @@ package com.iemr.flw.service.impl; -import com.iemr.flw.domain.iemr.M_User; +import com.iemr.flw.domain.iemr.User; import com.iemr.flw.service.EmployeeMasterInter; -import com.iemr.flw.service.impl.ChildCareServiceImpl; -import com.iemr.flw.service.impl.MaternalHealthServiceImpl; import com.iemr.flw.utils.CookieUtil; -import jakarta.annotation.PostConstruct; -import jakarta.servlet.http.HttpServletRequest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; -import org.springframework.web.context.request.RequestContextHolder; -import org.springframework.web.context.request.ServletRequestAttributes; @Service public class NotificationSchedulerService { final Logger logger = LoggerFactory.getLogger(this.getClass().getName()); - @Autowired - private NotificationService notificationService; @Autowired EmployeeMasterInter employeeMasterInter; @@ -49,19 +41,12 @@ public void triggerAncRemindersForAllAsha() { String topic = "All"; // or some user/topic identifier String title = "ANC Reminder"; - notificationService.sendNotification( - appType, - topic, - title, - body, - redirectPath - ); } @Scheduled(cron = "0 0 9 * * *") public void trigerTomorrowImmunizationReminders() { - for(M_User m_user: employeeMasterInter.getAllUsers()){ + for(User m_user: employeeMasterInter.getAllUsers()){ childCareService.getTomorrowImmunizationReminders(m_user.getUserID()); } diff --git a/src/main/java/com/iemr/flw/service/impl/NotificationService.java b/src/main/java/com/iemr/flw/service/impl/NotificationService.java deleted file mode 100644 index 838ff3ac..00000000 --- a/src/main/java/com/iemr/flw/service/impl/NotificationService.java +++ /dev/null @@ -1,81 +0,0 @@ -package com.iemr.flw.service.impl; - -import com.google.gson.Gson; -import jakarta.servlet.http.Cookie; -import jakarta.servlet.http.HttpServletRequest; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpMethod; -import org.springframework.http.ResponseEntity; -import org.springframework.stereotype.Service; -import org.springframework.util.LinkedMultiValueMap; -import org.springframework.util.MultiValueMap; -import org.springframework.web.client.RestTemplate; -import org.springframework.web.context.request.RequestContextHolder; -import org.springframework.web.context.request.ServletRequestAttributes; - -import java.util.HashMap; -import java.util.Map; - -@Service -public class NotificationService { - final Logger logger = LoggerFactory.getLogger(this.getClass().getName()); - - - private String NOTIFICATION_URL = "https://uatamrit.piramalswasthya.org/common-api/firebaseNotification/sendNotification"; - - public String sendNotification(String appType, String topic, String title, String body, String redirect) { - String authHeader = null; - String jwtToken = null; - - // Check if we have HTTP request context - ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); - if (attributes != null) { - HttpServletRequest httpServletRequest = attributes.getRequest(); - - authHeader = httpServletRequest.getHeader("Authorization"); - - if (httpServletRequest.getCookies() != null) { - for (Cookie cookie : httpServletRequest.getCookies()) { - if ("Jwttoken".equals(cookie.getName())) { - jwtToken = cookie.getValue(); - } - } - } - } - - // If no request context, set default (for scheduler/startup use) - if (authHeader == null) { - authHeader = "Bearer DEFAULT_TOKEN_IF_REQUIRED"; // or leave null if API allows - } - if (jwtToken == null) { - jwtToken = "DEFAULT_JWT_IF_REQUIRED"; - } - - RestTemplate restTemplate = new RestTemplate(); - MultiValueMap headers = new LinkedMultiValueMap<>(); - headers.add("Content-Type", "application/json"); -// headers.add("AUTHORIZATION", authHeader); -// headers.add("Cookie", "Jwttoken=" + jwtToken); - - Map requestBody = new HashMap<>(); - requestBody.put("appType", appType); - requestBody.put("token", topic); - requestBody.put("title", title); - requestBody.put("body", body); - - Map dataMap = new HashMap<>(); - dataMap.put("NotificationTypeId", redirect); - requestBody.put("data", dataMap); - - String jsonRequest = new Gson().toJson(requestBody); - - HttpEntity request = new HttpEntity<>(jsonRequest, headers); - - ResponseEntity response = restTemplate.exchange(NOTIFICATION_URL, HttpMethod.POST, request, String.class); - - return response.getBody(); - } -} diff --git a/src/main/java/com/iemr/flw/service/impl/NotificationServiceImpl.java b/src/main/java/com/iemr/flw/service/impl/NotificationServiceImpl.java new file mode 100644 index 00000000..8a157cb2 --- /dev/null +++ b/src/main/java/com/iemr/flw/service/impl/NotificationServiceImpl.java @@ -0,0 +1,222 @@ +package com.iemr.flw.service.impl; + +import com.google.gson.Gson; +import com.iemr.flw.domain.iemr.Notification; +import com.iemr.flw.domain.iemr.UserFcmTokenData; +import com.iemr.flw.dto.iemr.NotificationListDTO; +import com.iemr.flw.repo.iemr.NotificationRepository; +import com.iemr.flw.repo.iemr.UserFcmTokenRepo; +import com.iemr.flw.service.NotificationService; +import com.iemr.flw.utils.JwtUtil; +import jakarta.servlet.http.Cookie; +import jakarta.servlet.http.HttpServletRequest; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Service; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; +import org.springframework.web.client.HttpClientErrorException; +import org.springframework.web.client.HttpServerErrorException; +import org.springframework.web.client.RestTemplate; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +@Service +public class NotificationServiceImpl implements NotificationService { + final Logger logger = LoggerFactory.getLogger(this.getClass().getName()); + + @Value("${common-api-base-url}") + private String commonApiBaseUrl; + @Autowired + private JwtUtil jwtUtil; + + @Autowired + private NotificationRepository notificationRepository; + + @Autowired + private UserFcmTokenRepo userFcmTokenRepo; + + + private String NOTIFICATION_URL = "/firebaseNotification/sendNotification"; + + public String sendNotification(String appType, String topic, String title, + String body, String redirect, + String notificationType, Integer receiverId) { + + logger.info("Initiating notification. appType={}, receiverId={}, type={} , commonApiBaseUrl={}", + appType, receiverId, notificationType,commonApiBaseUrl); + + try { + String authHeader = null; + ServletRequestAttributes attributes = + (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); + + if (attributes != null) { + HttpServletRequest request = attributes.getRequest(); + authHeader = request.getHeader("jwtToken"); + } + + Integer senderId = jwtUtil.extractUserId(authHeader); + + logger.debug("Sender Id: {}", senderId); + + + UserFcmTokenData tokenData = userFcmTokenRepo.findByUserId(receiverId); + + if (tokenData == null) { + logger.warn("FCM token not found for receiverId={}", receiverId); + return "FCM token not found."; + } + + String token = tokenData.getToken(); + + + + RestTemplate restTemplate = new RestTemplate(); + + MultiValueMap headers = new LinkedMultiValueMap<>(); + headers.add("Content-Type", "application/json"); + headers.add("jwtToken", authHeader); + + Map requestBody = new HashMap<>(); + requestBody.put("appType", appType); + requestBody.put("token", token); + requestBody.put("title", title); + requestBody.put("body", body); + + Map dataMap = new HashMap<>(); + dataMap.put("notification_id", notificationType); + dataMap.put("notification_type", notificationType); + dataMap.put("nav_id", redirect); + dataMap.put("sender_user_id", senderId); + dataMap.put("receiver_user_id", receiverId); + dataMap.put("priority", "HIGH"); + dataMap.put("redirect", redirect); + + requestBody.put("data", dataMap); + + String jsonRequest = new Gson().toJson(requestBody); + + logger.debug("Notification request payload: {}", NOTIFICATION_URL); + HttpEntity request = new HttpEntity<>(jsonRequest, headers); + + ResponseEntity response = restTemplate.exchange( + commonApiBaseUrl+NOTIFICATION_URL, + HttpMethod.POST, + request, + String.class); + + logger.info("Notification API response. Status={}, Body={}", + response.getStatusCode(), + response.getBody()); + + saveNotificationRecord(appType, senderId, receiverId, title, body, + notificationType, "INCENTIVE_APPROVAL", redirect, "HIGH"); + + logger.info("Notification record saved successfully. receiverId={}", receiverId); + + return response.getBody(); + + } catch (HttpClientErrorException | HttpServerErrorException ex) { + + logger.error("Notification API failed. Status={}, Response={}", + ex.getStatusCode(), + ex.getResponseBodyAsString(), + ex); + + throw ex; + + } catch (Exception ex) { + + logger.error("Unexpected error while sending notification. receiverId={}, type={}", + receiverId, + notificationType, + ex); + + throw new RuntimeException("Failed to send notification.", ex); + } + } + + + private void saveNotificationRecord(String appType, Integer senderUserId, Integer receiverUserId, + String title, String body, String notificationType, + String navId, String redirect, String priority) { + try { + Notification notification = new Notification(); + notification.setAppType(appType); + notification.setSenderUserId(senderUserId); + notification.setReceiverUserId(receiverUserId); + notification.setTitle(title); + notification.setBody(body); + notification.setNotificationType(notificationType); + notification.setNavId(navId); + notification.setRedirect(redirect); + notification.setPriority(priority); + notification.setRead(false); + notificationRepository.save(notification); + } catch (Exception e) { + logger.error("Failed to persist notification record for receiver {}", receiverUserId, e); + } + } + + /** Paged list of a user's notifications, newest first, with unread count attached. */ + public NotificationListDTO getNotifications(Integer receiverUserId) { + + int page = 0; // First page + int size = 20; // Records per page + + Pageable pageable = PageRequest.of( + page, + size, + Sort.by(Sort.Direction.DESC, "createdDate") + ); + + Page result = + notificationRepository.findByReceiverUserIdOrderByCreatedDateDesc( + receiverUserId, + pageable + ); + + long unreadCount = + notificationRepository.countByReceiverUserIdAndIsReadFalse(receiverUserId); + + return new NotificationListDTO( + result.getContent(), + unreadCount, + result.getTotalElements(), + result.getNumber(), + result.getSize() + ); + } + + /** + * Lightweight call for a badge — no list, just the count. + */ + public Long getUnreadCount(Integer receiverUserId) { + return notificationRepository.countByReceiverUserIdAndIsReadFalse(receiverUserId); + } + + /** Marks a single notification read. Returns true if a row was updated. */ + public boolean markAsRead(Long notificationId) { + int updated = notificationRepository.markAsRead(notificationId, new Date()); + return updated > 0; + } + + /** Marks all of a user's notifications read. Returns the number updated. */ + public int markAllAsRead(Integer receiverUserId) { + return notificationRepository.markAllAsRead(receiverUserId, new Date()); + } +} diff --git a/src/main/java/com/iemr/flw/service/impl/SammelanServiceImpl.java b/src/main/java/com/iemr/flw/service/impl/SammelanServiceImpl.java index 53497981..e0460fe8 100644 --- a/src/main/java/com/iemr/flw/service/impl/SammelanServiceImpl.java +++ b/src/main/java/com/iemr/flw/service/impl/SammelanServiceImpl.java @@ -51,6 +51,8 @@ public class SammelanServiceImpl implements SammelanService { @Autowired private IncentivesRepo incentivesRepo; + @Autowired + private UpdateIncentivePendindDocService updateIncentivePendindDocService; @Override @@ -62,36 +64,36 @@ public SammelanResponseDTO submitSammelan(SammelanRequestDTO sammelanRequestDTO) // Save Sammelan record record = new SammelanRecord(); record.setAshaId(sammelanRequestDTO.getAshaId()); - logger.info("Meeting Date:"+sammelanRequestDTO.getDate()); + logger.info("Meeting Date:" + sammelanRequestDTO.getDate()); Timestamp timestamp = new Timestamp(sammelanRequestDTO.getDate()); record.setMeetingDate(timestamp); record.setPlace(sammelanRequestDTO.getPlace()); record.setParticipants(sammelanRequestDTO.getParticipants()); - if (sammelanRequestDTO.getSammelanImages() != null && sammelanRequestDTO.getSammelanImages().length > 0) { - List base64Images = Arrays.stream(sammelanRequestDTO.getSammelanImages()) - .filter(file -> !file.isEmpty()) - .map(file -> { - try { - return Base64.getEncoder().encodeToString(file.getBytes()); - } catch (IOException e) { - throw new RuntimeException("Error converting image to Base64", e); - } - }) - .collect(Collectors.toList()); - - String imagesJson = objectMapper.writeValueAsString(base64Images); - record .setAttachments(imagesJson); - } + if (sammelanRequestDTO.getSammelanImages() != null && sammelanRequestDTO.getSammelanImages().length > 0) { + List base64Images = Arrays.stream(sammelanRequestDTO.getSammelanImages()) + .filter(file -> !file.isEmpty()) + .map(file -> { + try { + return Base64.getEncoder().encodeToString(file.getBytes()); + } catch (IOException e) { + throw new RuntimeException("Error converting image to Base64", e); + } + }) + .collect(Collectors.toList()); - if(record!=null){ - record = recordRepo.save(record); - checkIncentive(record); + String imagesJson = objectMapper.writeValueAsString(base64Images); + record.setAttachments(imagesJson); + } + + if (record != null) { + record = recordRepo.save(record); + checkIncentive(record); - } - // Prepare Response DTO + } + // Prepare Response DTO response.setId(record.getId()); response.setAshaId(record.getAshaId()); Timestamp ts = Timestamp.valueOf(record.getMeetingDate().toLocalDateTime()); @@ -103,7 +105,7 @@ record = recordRepo.save(record); } catch (Exception e) { - logger.info("SammelanRecord Exception: "+e.getMessage()); + logger.info("SammelanRecord Exception: " + e.getMessage()); } return response; @@ -112,31 +114,43 @@ record = recordRepo.save(record); private void checkIncentive(SammelanRecord record) { IncentiveActivity incentiveActivity = incentivesRepo.findIncentiveMasterByNameAndGroup("FP_SAAS_BAHU", GroupName.FAMILY_PLANNING.getDisplayName()); - logger.info("SammelanRecord: "+incentiveActivity.getRate()); - if(incentiveActivity!=null){ - addSammelanIncentive(incentiveActivity,record); + logger.info("SammelanRecord: " + incentiveActivity.getRate()); + if (incentiveActivity != null) { + addSammelanIncentive(incentiveActivity, record); } } private void addSammelanIncentive(IncentiveActivity incentiveActivity, SammelanRecord record) { - IncentiveActivityRecord incentiveActivityRecord = incentiveRecordRepo .findRecordByActivityIdCreatedDateBenId(incentiveActivity.getId(), record.getMeetingDate(), 0L,record.getAshaId()); + String userName = userRepo.getUserNamedByUserId(record.getAshaId()); + IncentiveActivityRecord incentiveActivityRecord = incentiveRecordRepo.findRecordByActivityIdCreatedDateBenId(incentiveActivity.getId(), record.getMeetingDate(), 0L, record.getAshaId()); try { if (incentiveActivityRecord == null) { incentiveActivityRecord = new IncentiveActivityRecord(); incentiveActivityRecord.setActivityId(incentiveActivity.getId()); incentiveActivityRecord.setCreatedDate(record.getMeetingDate()); - incentiveActivityRecord.setCreatedBy(userRepo.getUserNamedByUserId(record.getAshaId())); + incentiveActivityRecord.setCreatedBy(userName); incentiveActivityRecord.setStartDate(record.getMeetingDate()); incentiveActivityRecord.setEndDate(record.getMeetingDate()); incentiveActivityRecord.setUpdatedDate(record.getMeetingDate()); - incentiveActivityRecord.setUpdatedBy(userRepo.getUserNamedByUserId(record.getAshaId())); + incentiveActivityRecord.setUpdatedBy(userName); incentiveActivityRecord.setBenId(0L); incentiveActivityRecord.setAshaId(record.getAshaId()); incentiveActivityRecord.setAmount(Long.valueOf(incentiveActivity.getRate())); - incentiveRecordRepo.save(incentiveActivityRecord); + if (record.getAttachments() != null) { + incentiveActivityRecord.setIsEligible(true); + incentiveRecordRepo.save(incentiveActivityRecord); + + } else { + incentiveActivityRecord.setIsEligible(false); + IncentiveActivityRecord activityRecord = incentiveRecordRepo.save(incentiveActivityRecord); + if (activityRecord != null) { + updateIncentivePendindDocService.updatePendingActivity(record.getAshaId(), record.getId(), activityRecord.getActivityId(), incentiveActivity.getId()); + + } + } } - }catch (Exception e){ - logger.info("SammelanRecord save Record: ",e); + } catch (Exception e) { + logger.info("SammelanRecord save Record: ", e); } diff --git a/src/main/java/com/iemr/flw/service/impl/SmsSchedulerService.java b/src/main/java/com/iemr/flw/service/impl/SmsSchedulerService.java index 637d011a..90b57fdd 100644 --- a/src/main/java/com/iemr/flw/service/impl/SmsSchedulerService.java +++ b/src/main/java/com/iemr/flw/service/impl/SmsSchedulerService.java @@ -2,22 +2,16 @@ import com.iemr.flw.domain.identity.RMNCHBeneficiaryDetailsRmnch; import com.iemr.flw.domain.iemr.ANCVisit; -import com.iemr.flw.domain.iemr.M_User; -import com.iemr.flw.dto.iemr.ANCVisitDTO; +import com.iemr.flw.domain.iemr.User; import com.iemr.flw.repo.identity.BeneficiaryRepo; import com.iemr.flw.repo.iemr.ANCVisitRepo; import com.iemr.flw.service.EmployeeMasterInter; import com.iemr.flw.utils.CookieUtil; -import jakarta.annotation.PostConstruct; -import jakarta.servlet.http.HttpServletRequest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; -import org.springframework.web.context.request.RequestContextHolder; -import org.springframework.web.context.request.ServletRequestAttributes; import java.math.BigInteger; import java.sql.Timestamp; @@ -25,7 +19,6 @@ import java.time.ZoneId; import java.util.List; import java.util.Optional; -import java.util.stream.Collectors; @Service public class SmsSchedulerService { @@ -94,7 +87,7 @@ public void sendAncReminders() { @Scheduled(cron = "0 0 9 * * *") public void trigerTomorrowImmunizationReminders() { - for (M_User m_user : employeeMasterInter.getAllUsers()) { + for (User m_user : employeeMasterInter.getAllUsers()) { childCareService.getTomorrowImmunizationReminders(m_user.getUserID()); } diff --git a/src/main/java/com/iemr/flw/service/impl/SupervisorDashboardServiceImpl.java b/src/main/java/com/iemr/flw/service/impl/SupervisorDashboardServiceImpl.java new file mode 100644 index 00000000..b90b9f73 --- /dev/null +++ b/src/main/java/com/iemr/flw/service/impl/SupervisorDashboardServiceImpl.java @@ -0,0 +1,986 @@ +package com.iemr.flw.service.impl; + +import java.math.BigDecimal; +import java.sql.Timestamp; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.Month; +import java.util.*; +import java.util.stream.Collectors; + +import com.google.gson.Gson; +import com.iemr.flw.domain.iemr.IncentiveActivityRecord; +import com.iemr.flw.dto.iemr.UserServiceRoleDTO; +import com.iemr.flw.masterEnum.IncentiveApprovalStatus; +import com.iemr.flw.masterEnum.StateCode; +import com.iemr.flw.repo.iemr.*; +import com.iemr.flw.service.NotificationService; +import com.iemr.flw.service.UserService; +import com.iemr.flw.utils.JwtUtil; +import org.json.JSONArray; +import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.iemr.flw.service.SupervisorDashboardService; +import org.springframework.transaction.annotation.Transactional; + +@Service +public class SupervisorDashboardServiceImpl implements SupervisorDashboardService { + + private final Logger logger = LoggerFactory.getLogger(this.getClass().getName()); + + @Autowired + private SupervisorDashboardRepo dashboardRepo; + + @Autowired + private AshaSupervisorLoginRepo ashaSupervisorLoginRepo; + + @Autowired + private FacilityLoginRepo facilityLoginRepo; + + @Autowired + private IncentiveRecordRepo incentiveRecordRepo; + + @Autowired + private JwtUtil jwtUtil; + + @Autowired + private UserService userService; + + @Autowired + private NotificationService notificationService; + + @Override + public String getSupervisorDashboard(Integer supervisorUserID, Integer month, Integer year) { + JSONObject result = new JSONObject(); + Integer stateId = userService.getUserDetail(supervisorUserID).getStateId(); + String rollName = userService.getUserDetail(supervisorUserID).getRoleName(); + + LocalDate today = LocalDate.now(); + + + // 1. Supervisor user details + List supervisorRows = dashboardRepo.getSupervisorUserDetails(supervisorUserID); + if (supervisorRows != null && !supervisorRows.isEmpty()) { + Object[] sRow = supervisorRows.get(0); + JSONObject supervisor = new JSONObject(); + supervisor.put("userId", sRow[0]); + supervisor.put("fullName", fullName(sRow[1], sRow[2])); + supervisor.put("employeeId", str(sRow[3]).isEmpty() ? JSONObject.NULL : str(sRow[3])); + supervisor.put("mobile", str(sRow[4]).isEmpty() ? JSONObject.NULL : str(sRow[4])); + supervisor.put("gender", str(sRow[5]).isEmpty() ? JSONObject.NULL : str(sRow[5])); + result.put("supervisor", supervisor); + } + logger.info("Supervisor id"+supervisorUserID); + + // 2. Get all ASHAs with facility info + logger.info("Fetching ASHA details for supervisorUserID: {}", supervisorUserID); + + + LocalDate dueDate = LocalDate.of(year, month, 1) + .plusMonths(1) + .withDayOfMonth(5); + + boolean isOverDue = + rollName.equalsIgnoreCase("ANM") + && today.isAfter(dueDate); + + + List ashaRows; + + if ("ANM".equalsIgnoreCase(rollName) || "CHO".equalsIgnoreCase(rollName)) { + + List facilityIDs = + facilityLoginRepo.getUserFacilityIDs(supervisorUserID); + + ashaRows = + facilityLoginRepo.getAshaListByFacilities(facilityIDs); + + } else { + + ashaRows = + dashboardRepo.getAshasWithFacilityInfo(supervisorUserID); + + } + + if (ashaRows == null || ashaRows.isEmpty()) { + logger.warn("No ASHA records found for supervisorUserID: {}", supervisorUserID); + + result.put("totalAshaCount", 0); + result.put("incentiveSummary", buildEmptyIncentiveSummary()); + result.put("facilities", new JSONArray()); + + logger.info("Returning empty dashboard response for supervisorUserID: {}", supervisorUserID); + + return result.toString(); + } + + + + // Collect distinct facility IDs and ASHA IDs + + logger.info("Processing {} ASHA records", ashaRows.size()); + + Set facilityIDSet = new HashSet<>(); + Set ashaIDSet = new HashSet<>(); + + for (Object[] row : ashaRows) { + + logger.info("Complete Row: {}", Arrays.toString(row)); + + for (int i = 0; i < row.length; i++) { + logger.info("row[{}] = {} ({})", + i, + row[i], + row[i] != null ? row[i].getClass().getName() : "null"); + } + + + if (row[4] != null) + facilityIDSet.add((Integer) row[4]); + + if (row[0] != null) + ashaIDSet.add((Integer) row[0]); + + } + + List facilityIDs = new ArrayList<>(facilityIDSet); + List ashaIDs = new ArrayList<>(ashaIDSet); + + logger.info("Facility IDs: {}", facilityIDs); + logger.info("ASHA IDs: {}", ashaIDs); + result.put("totalAshaCount", ashaIDs.size()); + + // 3. Location from first facility + List facilityRows = dashboardRepo.getFacilityDetails(facilityIDs); + if (facilityRows != null && !facilityRows.isEmpty()) { + Object[] fRow = facilityRows.get(0); + JSONObject location = new JSONObject(); + location.put("state", str(fRow[2])); + location.put("district", str(fRow[3])); + location.put("blockOrUlb", str(fRow[4])); + location.put("locationType", str(fRow[5])); + result.put("location", location); + } + + // 4. Build village map (facilityID -> villages) + Map> villageMap = new HashMap<>(); + List villageRows = dashboardRepo.getVillagesForFacilities(facilityIDs); + if (villageRows != null) { + for (Object[] vRow : villageRows) { + Integer facID = (Integer) vRow[0]; + JSONObject village = new JSONObject(); + village.put("villageId", vRow[1]); + village.put("villageName", str(vRow[2])); + villageMap.computeIfAbsent(facID, k -> new ArrayList<>()).add(village); + } + } + + // 5. Get incentive status per ASHA (verified, rejected, pending, totalAmount) + long overallVerified = 0, overallRejected = 0, overallPending = 0, overallOverDue =0; + long overallUnclaimed = 0; + + try { + logger.info("Month: {}", month); + logger.info("Year: {}", year); + + LocalDate startLocalDate = LocalDate.of(year, month, 1); + LocalDate endLocalDate = startLocalDate.plusMonths(1); + + logger.info("startLocalDate {}", startLocalDate); + logger.info("endLocalDate {}", endLocalDate); + + Timestamp startDate = Timestamp.valueOf(startLocalDate.atStartOfDay()); + Timestamp endDate = Timestamp.valueOf(endLocalDate.atStartOfDay()); + logger.info("Asha ID" + ashaIDs); + + if(stateId.equals(StateCode.AM.getStateCode())){ + List statusRows = dashboardRepo.getIncentiveStatusByAshaIds(ashaIDs, startDate, endDate); + if (statusRows != null) { + for (Object[] sRow : statusRows) { + long verified = ((Number) sRow[2]).longValue(); + long rejected = ((Number) sRow[3]).longValue(); + long pending = ((Number) sRow[4]).longValue(); + + if (verified > 0) overallVerified += 1; + if (rejected > 0) overallRejected += 1; + if (pending > 0) overallPending += 1; + } + } + List unclaimedRows = dashboardRepo.getUnclaimedCountByAshaIds(ashaIDs, startDate, endDate); + if (unclaimedRows != null) { + for (Object[] uRow : unclaimedRows) { + long count = ((Number) uRow[1]).longValue(); + if (count > 0) overallUnclaimed += 1; + } + } + }else if(stateId.equals(StateCode.CG.getStateCode())){ + if("ASHA Supervisor".equalsIgnoreCase(rollName)){ + List statusRows = dashboardRepo.getDefaultIncentiveStatusByAshaIds(ashaIDs, startDate, endDate); + if (statusRows != null) { + for (Object[] sRow : statusRows) { + long verified = ((Number) sRow[5]).longValue(); + long rejected = ((Number) sRow[3]).longValue(); + long pending = ((Number) sRow[4]).longValue(); + + + if (verified > 0) overallVerified += 1; + if (rejected > 0) overallRejected += 1; + if (pending > 0) overallPending += 1; + } + } + }else if("ANM".equalsIgnoreCase(rollName)){ + List statusRows = dashboardRepo.getIncentiveStatusByAshaIdsForAnm(ashaIDs, startDate, endDate); + if (statusRows != null) { + for (Object[] sRow : statusRows) { + long verified = ((Number) sRow[2]).longValue(); + long rejected = ((Number) sRow[3]).longValue(); + long pending = ((Number) sRow[5]).longValue(); + + if (verified > 0) overallVerified += 1; + if (rejected > 0) overallRejected += 1; + if (pending > 0) { + if (isOverDue) { + overallOverDue++; + } else { + overallPending++; + } + } + } + } + } + + List unclaimedRows = dashboardRepo.getUnclaimedCountByAshaIds(ashaIDs, startDate, endDate); + if (unclaimedRows != null) { + for (Object[] uRow : unclaimedRows) { + long count = ((Number) uRow[1]).longValue(); + if (count > 0) overallUnclaimed += 1; + } + } + } + + + } catch (Exception e) { + logger.error("Error fetching incentive status: " + e.getMessage(), e); + + } + + + + // Overall incentive summary across all ASHAs + JSONObject overallSummary = new JSONObject(); + overallSummary.put("verified", overallVerified); + overallSummary.put("rejected", overallRejected); + overallSummary.put("pending", overallPending); + overallSummary.put("overDue", overallOverDue); + overallSummary.put("unclaimed", overallUnclaimed); + result.put("incentiveSummary", overallSummary); + + // 7. Build facilities array with nested ASHAs + Map> ashasByFacility = new HashMap<>(); + for (Object[] row : ashaRows) { + Integer facID = (Integer) row[4]; + ashasByFacility.computeIfAbsent(facID, k -> new ArrayList<>()).add(row); + } + + Map facilityDetailsMap = new HashMap<>(); + if (facilityRows != null) { + for (Object[] fRow : facilityRows) { + facilityDetailsMap.put((Integer) fRow[0], fRow); + } + } + + JSONArray facilitiesArray = new JSONArray(); + for (Integer facID : facilityIDs) { + JSONObject facility = new JSONObject(); + facility.put("facilityId", facID); + + Object[] fDetails = facilityDetailsMap.get(facID); + if (fDetails != null) { + facility.put("facilityName", str(fDetails[1])); + facility.put("facilityType", str(fDetails[6])); + } + + + // ASHAs at this facility + JSONArray ashasArray = new JSONArray(); + List facAshaRows = ashasByFacility.get(facID); + if (facAshaRows != null) { + for (Object[] row : facAshaRows) { + Integer ashaId = (Integer) row[0]; + JSONObject asha = new JSONObject(); + asha.put("userId", ashaId); + asha.put("fullName", fullName(row[1], row[2])); + asha.put("employeeId", str(row[6]).isEmpty() ? JSONObject.NULL : str(row[6])); + asha.put("mobile", str(row[7]).isEmpty() ? JSONObject.NULL : str(row[7])); + + ashasArray.put(asha); + } + } + + facility.put("ashaCount", ashasArray.length()); + facilitiesArray.put(facility); + } + + result.put("facilities", facilitiesArray); + return result.toString(); + } + + + + @Override + public Map getAshasAtFacility(Integer supervisorId, Integer facilityId, + Integer month, Integer year, Integer approvalStatusID) { + Integer supervisorstateCode = userService.getUserDetail(supervisorId).getStateId(); + List rows = null; + LocalDate today = LocalDate.now(); + + + String roleName = userService.getUserDetail(supervisorId).getRoleName(); + + LocalDate currentMonthStartDate = LocalDate.of(year, month, 1); + LocalDate currentMonthendLocalDate = currentMonthStartDate.plusMonths(1); + + LocalDate dueDate = LocalDate.of(year, month, 1) + .plusMonths(1) + .withDayOfMonth(5); + + boolean isOverDue = + roleName.equalsIgnoreCase("ANM") + && today.isAfter(dueDate); + + + logger.info("Login user role:"+ roleName); + + boolean isCurrentMonth = + today.getYear() == year && + today.getMonthValue() == month; + logger.info("approvalStatusID:" + approvalStatusID); + + List superVisorRow = ashaSupervisorLoginRepo.getAllMappedAshas(supervisorId); + + List> ashaList = new ArrayList<>(); + + LocalDate startLocalDate = LocalDate.of(year, month, 1); + LocalDate endLocalDate = startLocalDate.plusMonths(1); + + Timestamp startDate = Timestamp.valueOf(startLocalDate.atStartOfDay()); + Timestamp endDate = Timestamp.valueOf(endLocalDate.atStartOfDay()); + + + + + if ("ANM".equalsIgnoreCase(roleName) || "CHO".equalsIgnoreCase(roleName)) { + logger.info("ANM:" + roleName); + + if (facilityId.equals(0)) { + + List facilityIDs = facilityLoginRepo.getUserFacilityIDs(supervisorId); + rows = facilityLoginRepo.getAshaListByFacilities(facilityIDs); + + } else { + + rows = facilityLoginRepo.getAshaListByFacilities( + Collections.singletonList(facilityId)); + + } + + } else { + logger.info("Other:" + roleName); + + if (facilityId.equals(0)) { + if(supervisorstateCode.equals(StateCode.CG.getStateCode())){ + if("ASHA Supervisor".equalsIgnoreCase(roleName)){ + rows = ashaSupervisorLoginRepo.getAshasAtFacilityCg( + supervisorId, approvalStatusID, startDate, endDate); + } + }else if(supervisorstateCode.equals(StateCode.AM.getStateCode())){ + rows = ashaSupervisorLoginRepo.getAshasAtFacility( + supervisorId, approvalStatusID, startDate, endDate); + } + } else { + if(supervisorstateCode.equals(StateCode.CG.getStateCode())){ + if("ASHA Supervisor".equalsIgnoreCase(roleName)){ + rows = ashaSupervisorLoginRepo.getAshasAtFacilityCg( + supervisorId, facilityId, approvalStatusID, startDate, endDate); + } + }else if(supervisorstateCode.equals(StateCode.AM.getStateCode())){ + rows = ashaSupervisorLoginRepo.getAshasAtFacility( + supervisorId, facilityId, approvalStatusID, startDate, endDate); + } + + + } + + } + logger.info("Other:" + rows); + + long overallVerified = 0, overallRejected = 0, overallPending = 0 , overallUnclaimed=0 ,overallOverDue =0; + + String facilityName = ""; + String facilityType = ""; + Integer facilityID = null; + + for (Object[] row : superVisorRow) { + facilityID = (Integer) row[3]; + facilityName = str(row[4]); + facilityType = str(row[5]); + } + + for (Object[] row : rows) { + + Map asha = new HashMap<>(); + + Integer ashaId = ((Number) row[0]).intValue(); + logger.info("ASHA_ID"+ashaId); + List countList =null; + + if(supervisorstateCode.equals(StateCode.CG.getStateCode())){ + if("ASHA Supervisor".equalsIgnoreCase(roleName)){ + if(approvalStatusID.equals(106)){ + countList = incentiveRecordRepo.getStatusUnclaimedCountByAshaId(ashaId, startDate, endDate); + + }else { + countList = incentiveRecordRepo.getStatusCountByAshaIdOfDefaultActivity(ashaId, startDate, endDate); + + } + + logger.info("countList = {}", Arrays.deepToString(countList.toArray())); + + + + }else if(("ANM".equalsIgnoreCase(roleName) || "CHO".equalsIgnoreCase(roleName) )){ + logger.info("ASHA_ID = {}", ashaId); + logger.info("Role = {}", roleName); + + if(approvalStatusID.equals(106)){ + countList = incentiveRecordRepo.getStatusUnclaimedCountByAshaId(ashaId, startDate, endDate); + + }else { + countList = incentiveRecordRepo.getStatusCountByAshaIdANM(ashaId, startDate, endDate); + + } + + + logger.info("Count List = {}", countList); + + } + + }else if(supervisorstateCode.equals(StateCode.AM.getStateCode())){ + countList = incentiveRecordRepo.getStatusCountByAshaId(ashaId, startDate, endDate); + + } + + Long totalAmount = null; + if (userService.getUserDetail(ashaId) != null) { + Integer stateCode = userService.getUserDetail(ashaId).getStateId(); + if(stateCode.equals(StateCode.AM.getStateCode())){ + totalAmount = incentiveRecordRepo.getTotalAmountByAsha( + ashaId, startDate, endDate, approvalStatusID, stateCode); + }else if(stateCode.equals(StateCode.CG.getStateCode())){ + if("ASHA Supervisor".equalsIgnoreCase(roleName)){ + if(approvalStatusID.equals(105)){ + totalAmount = incentiveRecordRepo.getDefaultActivityTotalAmountByAsha( + ashaId, startDate, endDate, 101, stateCode); + }else { + totalAmount = incentiveRecordRepo.getDefaultActivityTotalAmountByAsha( + ashaId, startDate, endDate, approvalStatusID, stateCode); + } + + }else if("ANM".equalsIgnoreCase(roleName) || "CHO".equalsIgnoreCase(roleName) ){ + if(approvalStatusID.equals(102)){ + totalAmount = incentiveRecordRepo.getTotalAmountByAshaANM( + ashaId, startDate, endDate, approvalStatusID, stateCode); + }else if(approvalStatusID.equals(104)){ + totalAmount = incentiveRecordRepo.getTotalAmountByAshaANM( + ashaId, startDate, endDate, 105, stateCode); + } else { + totalAmount = incentiveRecordRepo.getTotalAmountByAsha( + ashaId, startDate, endDate, approvalStatusID, stateCode); + } + + } + + } + + + } + Integer stateCode = userService.getUserDetail(ashaId).getStateId(); + + + List incentiveActivityRecord = null; + if(stateCode.equals(StateCode.AM.getStateCode())){ + List dbRecords = + incentiveRecordRepo.getRecordsByAsha(ashaId, startDate, endDate); + incentiveActivityRecord= dbRecords + .stream() + .filter(r -> approvalStatusID == 0 || + approvalStatusID.equals(r.getApprovalStatus())) + .collect(Collectors.toList()); + }else if(stateCode.equals(StateCode.CG.getStateCode())){ + List dbRecords = + incentiveRecordRepo.getRecordsByAsha(ashaId, startDate, endDate); + if("ASHA Supervisor".equalsIgnoreCase(roleName)){ + if(approvalStatusID.equals(105)){ + incentiveActivityRecord = dbRecords.stream() + .filter(r -> (r.getApprovalStatus().equals(101) || r.getApprovalStatus().equals(105)) && r.getIsDefaultActivity()) + .collect(Collectors.toList()); + }else if(approvalStatusID.equals(106)){ + incentiveActivityRecord = dbRecords.stream() + .filter(r ->r.getApprovalStatus().equals(102) && r.getIsDefaultActivity() && !r.getIsClaimed()) + .collect(Collectors.toList()); + }else if(approvalStatusID.equals(0)){ + incentiveActivityRecord = dbRecords.stream() + .filter(r ->((r.getApprovalStatus().equals(102) && r.getIsClaimed()) ||(r.getApprovalStatus().equals(102) && !r.getIsClaimed())|| r.getApprovalStatus().equals(103) || r.getApprovalStatus().equals(105) || r.getApprovalStatus().equals(101) || r.getApprovalStatus().equals(104)) && r.getIsDefaultActivity()) + .collect(Collectors.toList()); + }else{ + incentiveActivityRecord = dbRecords.stream() + .filter(r ->( approvalStatusID == 0 || + approvalStatusID.equals(r.getApprovalStatus())) && r.getIsDefaultActivity() && isWithin24Hours(r.getCalimedDate())) + .collect(Collectors.toList()); + } + + }else if ("ANM".equalsIgnoreCase(roleName) || "CHO".equalsIgnoreCase(roleName)) { + + if (approvalStatusID.equals(102)) { + + incentiveActivityRecord = dbRecords.stream() + .peek(r -> { + if (r.getApprovalStatus().equals(102) + && Boolean.FALSE.equals(r.getIsDefaultActivity()) + && r.getCalimedDate() != null + && isAfter24Hours(r.getCalimedDate())) { + + r.setApprovalStatus(105); + } + }) + .filter(r -> + r.getApprovalStatus().equals(105) + || (r.getApprovalStatus().equals(102) + && Boolean.FALSE.equals(r.getIsDefaultActivity()))) + .collect(Collectors.toList()); + totalAmount = incentiveActivityRecord.stream() + .mapToLong(r -> r.getAmount()) + .sum(); + incentiveActivityRecord.forEach(r -> logger.info( + "activityId={}, status={}, isDefaultActivity={}", + r.getActivityId(), + r.getApprovalStatus(), + r.getIsDefaultActivity() + )); + + }else if (approvalStatusID.equals(104)) { + if(isOverDue){ + incentiveActivityRecord = dbRecords.stream() + .filter(r -> + r.getApprovalStatus().equals(105) + || (r.getApprovalStatus().equals(104))) + .peek(r -> { + if (r.getApprovalStatus().equals(102) || r.getApprovalStatus().equals(105)) { + r.setApprovalStatus(104); + } + }) + .collect(Collectors.toList()); + } + + }else if(approvalStatusID.equals(106)){ + incentiveActivityRecord = dbRecords.stream() + .filter(r->!r.getIsClaimed() && r.getApprovalStatus().equals(102)).collect(Collectors.toList()); + }else if(approvalStatusID.equals(0)){ + incentiveActivityRecord = dbRecords.stream() + .filter(r->((r.getApprovalStatus().equals(102) && r.getIsClaimed()) ||(r.getApprovalStatus().equals(102) && !r.getIsClaimed()) || r.getApprovalStatus().equals(103) || r.getApprovalStatus().equals(105) || r.getApprovalStatus().equals(101) || r.getApprovalStatus().equals(104))).collect(Collectors.toList()); + } else{ + incentiveActivityRecord = dbRecords.stream() + .filter(r -> { + + if (approvalStatusID != 0 + && !approvalStatusID.equals(r.getApprovalStatus())) { + return false; + } + + // 102 should be visible only after 24 hours + if (r.getApprovalStatus().equals(102)) { + return true; + } + + // 105 should always be visible + return true; + }) + .peek(r -> { + if (r.getApprovalStatus().equals(102) + && isAfter24Hours(r.getCalimedDate())) { + r.setApprovalStatus(105); + } + }) + .collect(Collectors.toList()); + } + + } + + + logger.info("CG - Records after approvalStatus filter: {}", incentiveActivityRecord.size()); + + } + logger.info("Final incentiveActivityRecord count: {}", incentiveActivityRecord.size()); + logger.info("Final incentiveActivityRecord: {}", + new Gson().toJson(incentiveActivityRecord)); + + List> activityList = new ArrayList<>(); + for (IncentiveActivityRecord record : incentiveActivityRecord) { + Map activity = new HashMap<>(); + activity.put("reason", record.getReason()); + activity.put("otherReason", record.getOtherReason()); + activity.put("approvalDate", record.getApprovalDate()); + activity.put("approvalStatus", record.getApprovalStatus()); + if(record.getVerifiedByUserId()!=null){ + activity.put("verifiedByUserName", userService.getUserDetail(record.getVerifiedByUserId()).getName()); + activity.put("verifiedByUserId", record.getVerifiedByUserId()); + UserServiceRoleDTO roles = userService.getUserDetail(record.getVerifiedByUserId()); + activity.put("role", (roles != null ) ? roles.getRoleName() : null); + + + } + activity.put("isClaimed", record.getIsClaimed()); + activity.put("claimedDate", record.getCalimedDate()); + + + + activityList.add(activity); + } + + asha.put("facilityId", facilityID); + asha.put("facilityName", facilityName); + asha.put("facilityType", facilityType); + asha.put("userId", row[0]); + asha.put("fullName", fullName(row[1], row[2])); + asha.put("employeeId", row[0]); + asha.put("mobile", str(row[4]).isEmpty() ? null : str(row[4])); + asha.put("gender", str(row[5]).isEmpty() ? null : str(row[5])); + asha.put("totalAmount", totalAmount); + asha.put("activities", activityList); + + + + long pending = 0, verified = 0, rejected = 0 , unclaimedCount = 0 ; + + + if (countList != null && !countList.isEmpty()) { + Object[] counts = countList.get(0); + logger.info("Count List = {}", Arrays.deepToString(countList.toArray())); + logger.info("101={}, 102={}, 103={}, 101+105={}", + counts[0], counts[1], counts[2], counts[3]); + + if(stateCode.equals(StateCode.CG.getStateCode())){ + if(roleName.equalsIgnoreCase("ANM")){ + verified = counts[0] != null ? ((Number) counts[0]).longValue() : 0; + rejected = counts[2] != null ? ((Number) counts[2]).longValue() : 0; + if(approvalStatusID.equals(106)){ + unclaimedCount = counts[1] != null ? ((Number) counts[1]).longValue() : 0; + + }else if(approvalStatusID.equals(0)){ + unclaimedCount = counts[1] != null ? ((Number) counts[1]).longValue() : 0; + pending = counts[3] != null ? ((Number) counts[3]).longValue() : 0; + + }else{ + + pending = counts[3] != null ? ((Number) counts[3]).longValue() : 0; + + } + + + }else if(roleName.equalsIgnoreCase("ASHA Supervisor")) { + if (approvalStatusID.equals(105)) { + // 101 + 105 + verified = counts[3] != null ? ((Number) counts[3]).longValue() : 0; + } else { + // only 101 + verified = counts[0] != null ? ((Number) counts[0]).longValue() : 0; + } + if(approvalStatusID.equals(106)){ + unclaimedCount = counts[1] != null ? ((Number) counts[1]).longValue() : 0; + + }else if(approvalStatusID.equals(0)){ + unclaimedCount = counts[1] != null ? ((Number) counts[1]).longValue() : 0; + pending = counts[1] != null ? ((Number) counts[1]).longValue() : 0; + + }else{ + pending = counts[1] != null ? ((Number) counts[1]).longValue() : 0; + + } + rejected = counts[2] != null ? ((Number) counts[2]).longValue() : 0; + + + } + }else { + verified = counts[0] != null ? ((Number) counts[0]).longValue() : 0; + pending = counts[1] != null ? ((Number) counts[1]).longValue() : 0; + rejected = counts[2] != null ? ((Number) counts[2]).longValue() : 0; + } + + } + + + + if (verified > 0) overallVerified += 1; + if (rejected > 0) overallRejected += 1; + if (pending > 0) { + if (isOverDue) { + overallOverDue++; + } else { + overallPending++; + } + } + + if (unclaimedCount > 0) overallUnclaimed += 1; + + asha.put("pending", pending); + asha.put("verified", verified); + asha.put("rejected", rejected); + asha.put("unClaimed", unclaimedCount); + + int approvalStatus = 0; + + if (!activityList.isEmpty()) { + approvalStatus = (int) activityList.get(0).get("approvalStatus"); + } + if(!approvalStatusID.equals(106)){ + if (totalAmount == null || totalAmount <= 0) continue; + + } + + + if (pending == 0 && verified == 0 && rejected == 0 && unclaimedCount == 0) continue; + + if (approvalStatusID.equals(0)) { + asha.put("approvalStatus", approvalStatus); + + } else { + asha.put("approvalStatus", approvalStatusID); + + } + + ashaList.add(asha); + } + + Map response = new HashMap<>(); + + Map approvalStatus = new HashMap<>(); + approvalStatus.put("verified", overallVerified); + approvalStatus.put("pending", overallPending); + approvalStatus.put("rejected", overallRejected); + approvalStatus.put("unClaimed", overallUnclaimed); + + response.put("approvalStatus", approvalStatus); + response.put("data", ashaList); + response.put("statusCode", 200); + + return response; + } + + private boolean isAfter24Hours(Timestamp claimedDate) { + if (claimedDate == null) { + return false; + } + + long now = System.currentTimeMillis(); + long diff = now - claimedDate.getTime(); + + logger.info("Now: {}", new Timestamp(now)); + logger.info("ClaimedDate: {}", claimedDate); + logger.info("Diff(ms): {}", diff); + logger.info("Diff(hours): {}", diff / (1000 * 60 * 60.0)); + + return diff >= 24L * 60 * 60 * 1000; + } + private boolean isWithin24Hours(Timestamp claimedDate) { + if (claimedDate == null) { + return false; + } + + long diff = System.currentTimeMillis() - claimedDate.getTime(); + return diff <= 24 * 60 * 60 * 1000L; + } + + + private String getGroupNameByState(Integer stateCode) { + switch (stateCode) { + case 5: + return "! ACTIVITY"; + default: + return "ACTIVITY"; + } + } + + @Transactional + public int updateApprovalStatus(Integer ashaId, + Integer month, + Integer year, + Integer approvalStatus, + String incentiveIds, + String reason, + String otherReason, + String token) { + try { + String title = null; + String body = null; + Timestamp approvalDate = Timestamp.valueOf(LocalDateTime.now()); + + LocalDate startLocalDate = LocalDate.of(year, month, 1); + LocalDate endLocalDate = startLocalDate.plusMonths(1); + + Timestamp startDate = Timestamp.valueOf(startLocalDate.atStartOfDay()); + Timestamp endDate = Timestamp.valueOf(endLocalDate.atStartOfDay()); + + Integer ashaSupervisorUserId = jwtUtil.extractUserId(token); + logger.info("Asha Supervisor User Id : {}", ashaSupervisorUserId); + UserServiceRoleDTO ashaSupervisorDetails = userService.getUserDetail(ashaSupervisorUserId); + + int updatedCount = 0; + + if (approvalStatus.equals(IncentiveApprovalStatus.REJECTED.getCode())) { + updatedCount = incentiveRecordRepo.updateApprovalStatusById( + approvalStatus, + ashaId, + ashaSupervisorUserId, + ashaSupervisorDetails.getUserName(), + reason, + approvalDate, + otherReason + ); + } else { + + if (ashaSupervisorDetails.getStateId().equals(StateCode.AM.getStateCode())) { + + updatedCount = incentiveRecordRepo.updateApprovalStatusByAshaAndDateRange( + ashaId, approvalStatus, startDate, endDate, + approvalDate, ashaSupervisorUserId, + ashaSupervisorDetails.getUserName()); + + } else if(ashaSupervisorDetails.getStateId().equals(StateCode.CG.getStateCode())){ + + if ("ASHA Supervisor".equalsIgnoreCase(ashaSupervisorDetails.getRoleName())) { + + updatedCount = incentiveRecordRepo.updateApprovalStatusByAshaAndDateRange( + ashaId, 105, startDate, endDate, + approvalDate, ashaSupervisorUserId, + ashaSupervisorDetails.getUserName()); + + } else if("ANM".equalsIgnoreCase(ashaSupervisorDetails.getRoleName())){ + + updatedCount = incentiveRecordRepo.updateApprovalStatusByAshaAndDateRangeForDefaultActivity( + ashaId, approvalStatus, startDate, endDate, + approvalDate, ashaSupervisorUserId, + ashaSupervisorDetails.getUserName()); + + } + } + } + + if (updatedCount > 0) { + sendApprovalNotification( + approvalStatus, + ashaId, + month, + year, + reason, + otherReason, + ashaSupervisorUserId, + ashaSupervisorDetails + ); + } + + return updatedCount; + + } catch (Exception e) { + logger.error("Update claim :" + e.getMessage()); + e.printStackTrace(); + return 0; + } + } + + + private void sendApprovalNotification( + Integer approvalStatus, + Integer ashaId, + Integer month, + Integer year, + String reason, + String otherReason, + Integer supervisorId, + UserServiceRoleDTO supervisor) { + + String title; + String body; + + if (approvalStatus.equals(IncentiveApprovalStatus.REJECTED.getCode())) { + + title = "Incentive Rejected"; + + body = "Incentive claim for " + Month.of(month).name() + " " + year + " has been rejected."; + + notificationService.sendNotification( + "FLW", "NA", + title, + body, + "INCENTIVE_REJECTED", + "INCENTIVE", + supervisorId); + + body = supervisor.getName() + " has rejected your incentive claim for " + + Month.of(month).name() + " " + year + "due to "+reason+" "+otherReason; + + notificationService.sendNotification( + "FLW", "NA", + title, + body, + "INCENTIVE_REJECTED", + "INCENTIVE", + ashaId); + + } else { + + title = "Incentive Approved"; + + body = "Incentive claim for " + Month.of(month).name() + " " + year + " has been approved."; + + notificationService.sendNotification( + "FLW", "NA", + title, + body, + "INCENTIVE_CLAIMED", + "INCENTIVE", + supervisorId); + + body = supervisor.getName() + " has approved your incentive claim for " + + Month.of(month).name() + " " + year; + + notificationService.sendNotification( + "FLW", "NA", + title, + body, + "INCENTIVE_CLAIMED", + "INCENTIVE", + ashaId); + } + } + + private JSONObject buildEmptyIncentiveSummary() { + JSONObject summary = new JSONObject(); + summary.put("verified", 0); + summary.put("rejected", 0); + summary.put("pending", 0); + summary.put("totalAmount", 0); + return summary; + } + + private String str(Object val) { + return val != null ? val.toString() : ""; + } + + private String fullName(Object first, Object last) { + return (str(first) + " " + str(last)).trim(); + } +} \ No newline at end of file diff --git a/src/main/java/com/iemr/flw/service/impl/TBConfirmedCaseServiceImpl.java b/src/main/java/com/iemr/flw/service/impl/TBConfirmedCaseServiceImpl.java new file mode 100644 index 00000000..e89928de --- /dev/null +++ b/src/main/java/com/iemr/flw/service/impl/TBConfirmedCaseServiceImpl.java @@ -0,0 +1,182 @@ +package com.iemr.flw.service.impl; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.iemr.flw.domain.iemr.TBConfirmedCaseDTO; +import com.iemr.flw.domain.iemr.TBConfirmedCase; +import com.iemr.flw.dto.iemr.TBConfirmedCasesResponseDTO; +import com.iemr.flw.repo.iemr.TBConfirmedTreatmentRepository; +import com.iemr.flw.service.IncentiveLogicService; +import com.iemr.flw.service.TBConfirmedCaseService; +import com.iemr.flw.utils.JwtUtil; +import com.iemr.flw.utils.LocalDateAdapter; +import com.iemr.flw.utils.response.OutputResponse; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.sql.Timestamp; +import java.time.LocalDate; +import java.util.List; +import java.util.stream.Collectors; + +@Service +public class TBConfirmedCaseServiceImpl implements TBConfirmedCaseService { + + private static final Logger logger = LoggerFactory.getLogger(TBConfirmedCaseServiceImpl.class); + + private final TBConfirmedTreatmentRepository repository; + + @Autowired + private IncentiveLogicService incentiveLogicService; + + + @Autowired + private JwtUtil jwtUtil; + public TBConfirmedCaseServiceImpl(TBConfirmedTreatmentRepository repository) { + this.repository = repository; + } + + @Override + public String save(List request, String authorisation) throws Exception { + OutputResponse response = new OutputResponse(); + + try { + if (request != null) { + logger.info("Saving TB confirmed case: " + request); + + TBConfirmedCase entity = new TBConfirmedCase(); + for(TBConfirmedCaseDTO dto:request){ + entity.setBenId(dto.getBenId()); + entity.setUserId(jwtUtil.extractUserId(authorisation)); + entity.setRegimenType(dto.getRegimenType()); + entity.setTreatmentStartDate(dto.getTreatmentStartDate()); + entity.setExpectedTreatmentCompletionDate(dto.getExpectedTreatmentCompletionDate()); + entity.setFollowUpDate(dto.getFollowUpDate()); + entity.setMonthlyFollowUpDone(dto.getMonthlyFollowUpDone()); + entity.setAdherenceToMedicines(dto.getAdherenceToMedicines()); + entity.setAnyDiscomfort(dto.getAnyDiscomfort()); + entity.setTreatmentCompleted(dto.getTreatmentCompleted()); + entity.setActualTreatmentCompletionDate(dto.getActualTreatmentCompletionDate()); + entity.setTreatmentOutcome(dto.getTreatmentOutcome()); + entity.setDateOfDeath(dto.getDateOfDeath()); + entity.setPlaceOfDeath(dto.getPlaceOfDeath()); + entity.setReasonForDeath(dto.getReasonForDeath()); + entity.setReasonForNotCompleting(dto.getReasonForNotCompleting()); + if(entity!=null){ + repository.save(entity); + checkIncentive(entity); + + } + } + + response.setResponse("TB Confirmed case saved successfully"); + + } else { + response.setError(500, "Invalid/NULL request obj"); + } + } catch (Exception e) { + logger.error("Error saving TB confirmed case", e); + response.setError(5000, "Error saving TB confirmed case: " + e.getMessage()); + } + + return response.toString(); + } + + private void checkIncentive(TBConfirmedCase entity){ + String regimen = entity.getRegimenType(); + + boolean isDrTb = + "Shorter Regimen (9–12 Months)".equalsIgnoreCase(regimen) + || "Longer Regimen (18–24 Months)".equalsIgnoreCase(regimen); + + if (Boolean.TRUE.equals(entity.getTreatmentCompleted()) + && entity.getExpectedTreatmentCompletionDate() != null) { + + + if (isDrTb) { + incentiveLogicService.incentiveForTbFollowUpIsDrTb( + entity.getBenId(), + Timestamp.valueOf(entity.getExpectedTreatmentCompletionDate().atStartOfDay()), + Timestamp.valueOf(entity.getExpectedTreatmentCompletionDate().atStartOfDay()), + entity.getUserId() + ); + } else { + incentiveLogicService.incentiveForTbFollowUp( + entity.getBenId(), + Timestamp.valueOf(entity.getExpectedTreatmentCompletionDate().atStartOfDay()), + Timestamp.valueOf(entity.getExpectedTreatmentCompletionDate().atStartOfDay()), + entity.getUserId() + ); + + } + + } + } + + @Override + public String getByBenId(Long benId, String authorisation) throws Exception { + OutputResponse response = new OutputResponse(); + + try { + List list = repository.findByBenId(benId); + + if (list != null && !list.isEmpty()) { + List dtoList = list.stream() + .map(this::toDTO) + .collect(Collectors.toList()); + + response.setResponse(dtoList.toString()); + list.forEach(this::checkIncentive); + + } else { + response.setError(404, "No record found for benId: " + benId); + } + + } catch (Exception e) { + logger.error("Error getting TB confirmed case by benId", e); + response.setError(5000, "Error getting TB confirmed case: " + e.getMessage()); + } + + return response.toString(); + } + + @Override + public String getByUserId(String authorisation) throws Exception { + TBConfirmedCasesResponseDTO tbConfirmedCasesResponseDTO = new TBConfirmedCasesResponseDTO(); + Integer userId = jwtUtil.extractUserId(authorisation); + List list = repository.findByUserId(userId); + tbConfirmedCasesResponseDTO.setUserId(userId); + tbConfirmedCasesResponseDTO.setTbConfirmedCases(list); + Gson gson = new GsonBuilder() + .registerTypeAdapter(LocalDate.class, new LocalDateAdapter()) + .create(); + + return gson.toJson(tbConfirmedCasesResponseDTO); + } + + // Utility: convert entity -> DTO + private TBConfirmedCaseDTO toDTO(TBConfirmedCase entity) { + TBConfirmedCaseDTO dto = new TBConfirmedCaseDTO(); + + dto.setBenId(entity.getBenId()); + dto.setUserId(entity.getUserId()); + dto.setRegimenType(entity.getRegimenType()); + dto.setTreatmentStartDate(entity.getTreatmentStartDate()); + dto.setExpectedTreatmentCompletionDate(entity.getExpectedTreatmentCompletionDate()); + dto.setFollowUpDate(entity.getFollowUpDate()); + dto.setMonthlyFollowUpDone(entity.getMonthlyFollowUpDone()); + dto.setAdherenceToMedicines(entity.getAdherenceToMedicines()); + dto.setAnyDiscomfort(entity.getAnyDiscomfort()); + dto.setTreatmentCompleted(entity.getTreatmentCompleted()); + dto.setActualTreatmentCompletionDate(entity.getActualTreatmentCompletionDate()); + dto.setTreatmentOutcome(entity.getTreatmentOutcome()); + dto.setDateOfDeath(entity.getDateOfDeath()); + dto.setPlaceOfDeath(entity.getPlaceOfDeath()); + dto.setReasonForDeath(entity.getReasonForDeath()); + dto.setReasonForNotCompleting(entity.getReasonForNotCompleting()); + + return dto; + } +} diff --git a/src/main/java/com/iemr/flw/service/impl/TBScreeningServiceImpl.java b/src/main/java/com/iemr/flw/service/impl/TBScreeningServiceImpl.java index b6210169..a6c217b8 100644 --- a/src/main/java/com/iemr/flw/service/impl/TBScreeningServiceImpl.java +++ b/src/main/java/com/iemr/flw/service/impl/TBScreeningServiceImpl.java @@ -55,16 +55,21 @@ public String save(TBScreeningRequestDTO requestDTO) throws Exception { @Override public String getByUserId(GetBenRequestHandler request) { - List dtos = new ArrayList<>(); - List tbScreeningList = tbScreeningRepo.getByUserId(request.getAshaId(), request.getFromDate(), request.getToDate()); - tbScreeningList.forEach(tbScreening -> dtos.add(modelMapper.map(tbScreening, TBScreeningDTO.class))); - TBScreeningRequestDTO tbScreeningRequestDTO = new TBScreeningRequestDTO(); - tbScreeningRequestDTO.setTbScreeningList(dtos); - tbScreeningRequestDTO.setUserId(request.getAshaId()); - Gson gson = new GsonBuilder() - .setDateFormat("MMM dd, yyyy h:mm:ss a") // Set the desired date format - .create(); - return gson.toJson(tbScreeningRequestDTO); + try { + List dtos = new ArrayList<>(); + List tbScreeningList = tbScreeningRepo.getByUserId(request.getAshaId()); + tbScreeningList.forEach(tbScreening -> dtos.add(modelMapper.map(tbScreening, TBScreeningDTO.class))); + TBScreeningRequestDTO tbScreeningRequestDTO = new TBScreeningRequestDTO(); + tbScreeningRequestDTO.setTbScreeningList(dtos); + tbScreeningRequestDTO.setUserId(request.getAshaId()); + Gson gson = new GsonBuilder() + .setDateFormat("MMM dd, yyyy h:mm:ss a") // Set the desired date format + .create(); + return gson.toJson(tbScreeningRequestDTO); + }catch (Exception e){ + logger.error("TBScreening Exception:"+e.getMessage()); + } + return null; } } diff --git a/src/main/java/com/iemr/flw/service/impl/TBSuspectedServiceImpl.java b/src/main/java/com/iemr/flw/service/impl/TBSuspectedServiceImpl.java index 1b972be6..2a8c0eeb 100644 --- a/src/main/java/com/iemr/flw/service/impl/TBSuspectedServiceImpl.java +++ b/src/main/java/com/iemr/flw/service/impl/TBSuspectedServiceImpl.java @@ -7,6 +7,7 @@ import com.iemr.flw.dto.iemr.TBSuspectedDTO; import com.iemr.flw.dto.iemr.TBSuspectedRequestDTO; import com.iemr.flw.repo.iemr.TBSuspectedRepo; +import com.iemr.flw.service.IncentiveLogicService; import com.iemr.flw.service.TBSuspectedService; import org.modelmapper.ModelMapper; import org.slf4j.Logger; @@ -25,6 +26,9 @@ public class TBSuspectedServiceImpl implements TBSuspectedService { @Autowired private TBSuspectedRepo tbSuspectedRepo; + @Autowired + private IncentiveLogicService incentiveLogicService; + @Override public String getByBenId(Long benId, String authorisation) throws Exception { return null; @@ -49,6 +53,14 @@ public String save(TBSuspectedRequestDTO requestDTO) throws Exception { tbSuspected.setUserId(requestDTO.getUserId()); tbSuspectedRepo.save(tbSuspected); + if(tbSuspected!=null){ + if(tbSuspected.getIsConfirmed()){ + incentiveLogicService.incentiveForTbSuspected(tbSuspected.getBenId(),tbSuspected.getVisitDate(),tbSuspected.getVisitDate(),tbSuspected.getUserId()); + + } + + } + }); return "no of tb suspected items saved:" + requestDTO.getTbSuspectedList().size(); } @@ -57,7 +69,18 @@ public String save(TBSuspectedRequestDTO requestDTO) throws Exception { public String getByUserId(GetBenRequestHandler request) { List dtos = new ArrayList<>(); List tbSuspectedList = tbSuspectedRepo.getByUserId(request.getAshaId(), request.getFromDate(), request.getToDate()); - tbSuspectedList.forEach(tbSuspected -> dtos.add(modelMapper.map(tbSuspected, TBSuspectedDTO.class))); + tbSuspectedList.forEach(tbSuspected -> { + dtos.add(modelMapper.map(tbSuspected, TBSuspectedDTO.class)); + + if (tbSuspected != null && Boolean.TRUE.equals(tbSuspected.getIsConfirmed())) { + incentiveLogicService.incentiveForTbSuspected( + tbSuspected.getBenId(), + tbSuspected.getVisitDate(), + tbSuspected.getVisitDate(), + tbSuspected.getUserId() + ); + } + }); TBSuspectedRequestDTO tbSuspectedRequestDTO = new TBSuspectedRequestDTO(); tbSuspectedRequestDTO.setTbSuspectedList(dtos); tbSuspectedRequestDTO.setUserId(request.getAshaId()); diff --git a/src/main/java/com/iemr/flw/service/impl/UTPReronaPaymentJob.java b/src/main/java/com/iemr/flw/service/impl/UTPReronaPaymentJob.java new file mode 100644 index 00000000..c45f18e1 --- /dev/null +++ b/src/main/java/com/iemr/flw/service/impl/UTPReronaPaymentJob.java @@ -0,0 +1,195 @@ +package com.iemr.flw.service.impl; + +import com.google.gson.Gson; +import com.iemr.flw.domain.iemr.IncentiveActivity; +import com.iemr.flw.domain.iemr.IncentiveActivityRecord; +import com.iemr.flw.dto.iemr.PaymentItem; +import com.iemr.flw.dto.iemr.PaymentRequest; +import com.iemr.flw.dto.iemr.Period; +import com.iemr.flw.dto.iemr.VerifiedBy; +import com.iemr.flw.repo.iemr.IncentiveRecordRepo; +import com.iemr.flw.repo.iemr.IncentivesRepo; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +import java.sql.Timestamp; +import java.time.LocalDate; +import java.time.OffsetDateTime; +import java.time.format.DateTimeFormatter; +import java.util.*; +import java.util.stream.Collectors; + +@Slf4j +@Component +public class UTPReronaPaymentJob { + + @Autowired + private IncentiveRecordRepo recordRepo; + + @Autowired + private IncentivesRepo incentivesRepo; + + @Autowired + private UtpreronaPaymentIntegrationImpl paymentService; + + // Runs automatically on 1st of every month at midnight + @Scheduled(cron = "0 0 0 1 * *") + public void sendMonthlyPayments() { + triggerPayment(); + } + + // ✅ Separate method — call this for immediate testing + public void triggerPayment() { + + LocalDate today = LocalDate.now(); + +// ✅ Current month ka data — testing ke liye + LocalDate firstDay = today.withDayOfMonth(1); // Mar 1, 2026 + LocalDate lastDay = today; // Mar 11, 2026 (aaj) + + String periodStart = firstDay.format(DateTimeFormatter.ISO_DATE); + String periodEnd = lastDay.format(DateTimeFormatter.ISO_DATE); + + Timestamp startTs = Timestamp.valueOf(firstDay.atStartOfDay()); // 2026-03-01 00:00:00 + Timestamp endTs = Timestamp.valueOf(lastDay.plusDays(1).atStartOfDay()); // 2026-03-12 00:00:00 + + log.info("========================================"); + log.info("UTP Rerona Payment Job Started"); + log.info("Period: {} to {}", periodStart, periodEnd); + log.info("Start Timestamp: {}", startTs); + log.info("End Timestamp: {}", endTs); + log.info("========================================"); + + List allAshaIds = recordRepo.findDistinctAshaIdsByDateRange(startTs, endTs); + log.info("Total ASHAs found: {}", allAshaIds.size()); + + int success = 0, failed = 0, skipped = 0; + + for (Integer ashaId : allAshaIds) { + try { + log.info("----------------------------------------"); + log.info("Processing ASHA ID: {}", ashaId); + + // Fetch claimed and approved records for this ASHA + List entities = recordRepo + .findClaimedApprovedRecordsByAshaAndDateRange(ashaId, startTs, endTs); + + if (entities == null || entities.isEmpty()) { + log.warn("ASHA {} — No records found, skipping", ashaId); + skipped++; + continue; + } + + log.info("ASHA {} — Total records fetched: {}", ashaId, entities.size()); + + // Collect all unique activity IDs + Set activityIds = entities.stream() + .filter(e -> e.getActivityId() != null) + .map(IncentiveActivityRecord::getActivityId) + .collect(Collectors.toSet()); + + log.info("ASHA {} — Unique activity IDs: {}", ashaId, activityIds); + + // Fetch activity master data for all activity IDs at once + Map activityMap = incentivesRepo.findAllById(activityIds) + .stream() + .filter(a -> a.getIsDeleted() == null || !a.getIsDeleted()) + .collect(Collectors.toMap(IncentiveActivity::getId, a -> a)); + + // Group records by activity ID + Map> groupedByActivity = entities.stream() + .filter(e -> e.getActivityId() != null) + .collect(Collectors.groupingBy(IncentiveActivityRecord::getActivityId)); + + // Build items list from grouped records + List items = new ArrayList<>(); + + for (Map.Entry> entry : groupedByActivity.entrySet()) { + Long activityId = entry.getKey(); + List records = entry.getValue(); + + IncentiveActivity activity = activityMap.get(activityId); + if (activity == null) { + log.warn("ASHA {} — Activity ID {} not found in master, skipping", ashaId, activityId); + continue; + } + + // Calculate total incentive amount for this activity + Long totalAmount = records.stream() + .mapToLong(r -> r.getAmount() != null ? r.getAmount() : 0L) + .sum(); + + + String stateActivityCode = activity.getStateActivityCode() != null + ? String.valueOf(activity.getStateActivityCode()) + : null; + + if (stateActivityCode == null) { + log.warn("ASHA {} — ActivityId {} stateActivityCode null, skipping", ashaId, activityId); + continue; // ✅ Is item ko request mein add hi mat karo + } + + PaymentItem item = new PaymentItem(); + item.setActivityCode(stateActivityCode); + item.setCount(String.valueOf(records.size())); + item.setIncentiveAmount(String.valueOf(totalAmount)); + items.add(item); + + + + log.info("ASHA {} — Activity: {} | Count: {} | Total Amount: {}", + ashaId, activityId, records.size(), totalAmount); + } + + if (items.isEmpty()) { + log.warn("ASHA {} — No valid items found, skipping", ashaId); + skipped++; + continue; + } + + // Get verified by details from first record + IncentiveActivityRecord firstRecord = entities.get(0); + VerifiedBy verifiedBy = new VerifiedBy(); + verifiedBy.setEmployeeId(firstRecord.getVerifiedByUserId() != null + ? String.valueOf(firstRecord.getVerifiedByUserId()) : ""); + verifiedBy.setName(firstRecord.getVerifiedByUserName() != null + ? firstRecord.getVerifiedByUserName() : ""); + + // Build period object + Period period = new Period(); + period.setStart(periodStart); + period.setEnd(periodEnd); + + // Build final payment request + PaymentRequest paymentRequest = new PaymentRequest( + UUID.randomUUID().toString(), + "AMRIT", + period, + ashaId.toString(), + OffsetDateTime.now().format(DateTimeFormatter.ISO_OFFSET_DATE_TIME), + verifiedBy, + items + ); + + // Log full request before sending + log.info("ASHA {} — Request Payload: {}", ashaId, new Gson().toJson(paymentRequest)); + + // Send payment request to API + paymentService.sendPaymentRequest(paymentRequest); + + log.info("ASHA {} — Payment sent successfully", ashaId); + success++; + + } catch (Exception e) { + log.error("ASHA {} — Payment failed: {}", ashaId, e.getMessage(), e); + failed++; + } + } + + log.info("========================================"); + log.info("Job Complete | Success: {} | Failed: {} | Skipped: {}", success, failed, skipped); + log.info("========================================"); + } +} \ No newline at end of file diff --git a/src/main/java/com/iemr/flw/service/impl/UpdateIncentivePendindDocService.java b/src/main/java/com/iemr/flw/service/impl/UpdateIncentivePendindDocService.java new file mode 100644 index 00000000..bfda259f --- /dev/null +++ b/src/main/java/com/iemr/flw/service/impl/UpdateIncentivePendindDocService.java @@ -0,0 +1,48 @@ +package com.iemr.flw.service.impl; + +import com.iemr.flw.domain.iemr.IncentiveActivityRecord; +import com.iemr.flw.domain.iemr.IncentivePendingActivity; +import com.iemr.flw.repo.iemr.*; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.Optional; + +@Service +public class UpdateIncentivePendindDocService { + @Autowired + private IncentivesRepo incentivesRepo; + + @Autowired + private UserServiceRoleRepo userRepo; + + @Autowired + private IncentiveRecordRepo recordRepo; + + @Autowired + private IncentivePendingActivityRepository incentivePendingActivityRepository; + + public void updateIncentive(Long id) { + + Optional optionalRecord = recordRepo.findById(id); + + if (optionalRecord.isPresent()) { + IncentiveActivityRecord record = optionalRecord.get(); + record.setIsEligible(true); + recordRepo.save(record); + } + } + + public void updatePendingActivity(Integer userId, Long recordId, Long activityId, Long mIncentiveId) { + IncentivePendingActivity incentivePendingActivity = new IncentivePendingActivity(); + incentivePendingActivity.setActivityId(activityId); + incentivePendingActivity.setRecordId(recordId); + incentivePendingActivity.setUserId(userId); + incentivePendingActivity.setMincentiveId(mIncentiveId); + if (incentivePendingActivity != null) { + incentivePendingActivityRepository.save(incentivePendingActivity); + } + + } + +} diff --git a/src/main/java/com/iemr/flw/service/impl/UserServiceImpl.java b/src/main/java/com/iemr/flw/service/impl/UserServiceImpl.java index 4e680a1d..60ae973e 100644 --- a/src/main/java/com/iemr/flw/service/impl/UserServiceImpl.java +++ b/src/main/java/com/iemr/flw/service/impl/UserServiceImpl.java @@ -12,12 +12,17 @@ public class UserServiceImpl implements UserService { private final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class); + @Autowired private UserServiceRoleRepo userServiceRoleRepo; + @Autowired + private FacilityDataService facilityDataService; + public UserServiceRoleDTO getUserDetail(Integer userId) { logger.info("calling getUserRole for userId: " + userId); UserServiceRoleDTO userRole = userServiceRoleRepo.getUserRole(userId).get(0); + userRole.setFacilityData(facilityDataService.buildFacilityData(userId, userRole.getRoleName())); return userRole; } } diff --git a/src/main/java/com/iemr/flw/service/impl/UtpreronaPaymentIntegrationImpl.java b/src/main/java/com/iemr/flw/service/impl/UtpreronaPaymentIntegrationImpl.java new file mode 100644 index 00000000..f0e5a554 --- /dev/null +++ b/src/main/java/com/iemr/flw/service/impl/UtpreronaPaymentIntegrationImpl.java @@ -0,0 +1,66 @@ +package com.iemr.flw.service.impl; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.iemr.flw.dto.iemr.PaymentRequest; +import io.swagger.v3.oas.models.responses.ApiResponse; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; + +import java.io.IOException; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.time.Duration; + +@Service +public class UtpreronaPaymentIntegrationImpl { +// @Value("${ssdPortalUrl}") + private String API_URL = "https://nhmssd.assam.gov.in/APPMS_2024_25/api/utpreronaPayment.php"; + private String API_KEY = "41202fa384eab2725a17bbac58cf708bf04cd1dd5175010941606156dc36d6b5"; + private static final int TIMEOUT_SECONDS = 30; + + private final HttpClient httpClient; + private final ObjectMapper objectMapper; + + public UtpreronaPaymentIntegrationImpl() { + this.httpClient = HttpClient.newBuilder() + .connectTimeout(Duration.ofSeconds(TIMEOUT_SECONDS)) + .build(); + this.objectMapper = new ObjectMapper(); + } + + public ApiResponse sendPaymentRequest(PaymentRequest paymentRequest) + throws IOException, InterruptedException { + + String jsonBody = objectMapper.writeValueAsString(paymentRequest); + System.out.println("Sending Request:"); + System.out.println(objectMapper.writerWithDefaultPrettyPrinter() + .writeValueAsString(paymentRequest)); + + HttpRequest httpRequest = HttpRequest.newBuilder() + .uri(URI.create(API_URL)) + .timeout(Duration.ofSeconds(TIMEOUT_SECONDS)) + .header("Content-Type", "application/json") + .header("Accept", "application/json") + .header("x-api-key", API_KEY) + .POST(HttpRequest.BodyPublishers.ofString(jsonBody)) + .build(); + + HttpResponse httpResponse = httpClient.send( + httpRequest, + HttpResponse.BodyHandlers.ofString() + ); + + System.out.println("Response Status: " + httpResponse.statusCode()); + System.out.println("Response Body: " + httpResponse.body()); + + if (httpResponse.statusCode() == 200) { + return objectMapper.readValue(httpResponse.body(), ApiResponse.class); + } else { + throw new IOException("API Error - Status: " + httpResponse.statusCode() + + " | Body: " + httpResponse.body()); + } + } + +} diff --git a/src/main/java/com/iemr/flw/service/impl/UwinSessionServiceImpl.java b/src/main/java/com/iemr/flw/service/impl/UwinSessionServiceImpl.java index bb2a944c..d91d0c6f 100644 --- a/src/main/java/com/iemr/flw/service/impl/UwinSessionServiceImpl.java +++ b/src/main/java/com/iemr/flw/service/impl/UwinSessionServiceImpl.java @@ -8,6 +8,7 @@ import com.iemr.flw.dto.iemr.UwinSessionRequestDTO; import com.iemr.flw.dto.iemr.UwinSessionResponseDTO; import com.iemr.flw.masterEnum.GroupName; +import com.iemr.flw.masterEnum.StateCode; import com.iemr.flw.repo.iemr.IncentiveRecordRepo; import com.iemr.flw.repo.iemr.IncentivesRepo; import com.iemr.flw.repo.iemr.UserServiceRoleRepo; @@ -40,6 +41,9 @@ public class UwinSessionServiceImpl implements UwinSessionService { @Autowired private IncentiveRecordRepo recordRepo; + @Autowired + private UpdateIncentivePendindDocService updateIncentivePendindDocService; + @Override public UwinSessionResponseDTO saveSession(UwinSessionRequestDTO req) throws Exception { @@ -72,6 +76,7 @@ public UwinSessionResponseDTO saveSession(UwinSessionRequestDTO req) throws Exce session.setAshaId(req.getAshaId()); session.setDate(req.getDate()); session.setPlace(req.getPlace()); + session.setSessionDate(req.getDate()); session.setParticipants(req.getParticipants()); session.setCreatedBy(req.getCreatedBy()); if (req.getAttachments() != null && req.getAttachments().length > 0) { @@ -102,8 +107,45 @@ public UwinSessionResponseDTO saveSession(UwinSessionRequestDTO req) throws Exce dto.setParticipants(session.getParticipants()); dto.setAttachments(Collections.singletonList(session.getAttachmentsJson())); + return dto; } + @Override + public UwinSession updateSession(UwinSessionRequestDTO req, Long recordId, Long activityId) throws Exception { + Optional optionalUwinSession = repo.findById(recordId); + + if (optionalUwinSession.isPresent()) { + UwinSession session = optionalUwinSession.get(); // ✅ Use existing session + + // Update fields if present in request + if (req.getPlace() != null) session.setPlace(req.getPlace()); + if (req.getParticipants() != null) session.setParticipants(req.getParticipants()); + if (req.getDate() != null) session.setDate(req.getDate()); + + // Update attachments if present + if (req.getAttachments() != null && req.getAttachments().length > 0) { + List base64Images = Arrays.stream(req.getAttachments()) + .filter(file -> !file.isEmpty()) + .map(file -> { + try { + return Base64.getEncoder().encodeToString(file.getBytes()); + } catch (IOException e) { + throw new RuntimeException("Error converting image to Base64", e); + } + }) + .collect(Collectors.toList()); + + String imagesJson = objectMapper.writeValueAsString(base64Images); + session.setAttachmentsJson(imagesJson); + + } + + repo.save(session); // ✅ Save updated session + return session; + } else { + throw new RuntimeException("Session not found for id: " + recordId); + } + } @Override public List getSessionsByAsha(Integer ashaId) throws Exception { @@ -130,9 +172,9 @@ public List getSessionsByAsha(Integer ashaId) throws Exc private void checkAndAddIncentive(UwinSession session) { IncentiveActivity incentiveActivityAM = incentivesRepo.findIncentiveMasterByNameAndGroup("CHILD_MOBILIZATION_SESSIONS", GroupName.IMMUNIZATION.getDisplayName()); IncentiveActivity incentiveActivityCH = incentivesRepo.findIncentiveMasterByNameAndGroup("CHILD_MOBILIZATION_SESSIONS", GroupName.ACTIVITY.getDisplayName()); + Integer stateId = userRepo.getUserRole(session.getAshaId()).get(0).getStateId(); - - if(incentiveActivityAM!=null){ + if(incentiveActivityAM!=null && stateId!=null && stateId.equals(StateCode.AM.getStateCode())){ IncentiveActivityRecord record = recordRepo .findRecordByActivityIdCreatedDateBenId(incentiveActivityAM.getId(), session.getDate(), 0L,session.getAshaId()); if (record == null) { @@ -147,11 +189,27 @@ record = new IncentiveActivityRecord(); record.setBenId(0L); record.setAshaId(session.getAshaId()); record.setAmount(Long.valueOf(incentiveActivityAM.getRate())); + record.setIsEligible(true); recordRepo.save(record); + + +// if(session.getAttachmentsJson()!=null){ +// record.setIsEligible(true); +// recordRepo.save(record); +// +// }else { +// record.setIsEligible(false); +// IncentiveActivityRecord incentiveActivityRecord = recordRepo.save(record); +// if(incentiveActivityRecord!=null){ +// updateIncentivePendindDocService.updatePendingActivity(session.getAshaId(),session.getId(),incentiveActivityRecord.getId(),incentiveActivityAM.getId()); +// +// } +// } + } } - if(incentiveActivityCH!=null){ + if(incentiveActivityCH!=null && stateId!=null && stateId.equals(StateCode.CG.getStateCode())){ IncentiveActivityRecord record = recordRepo .findRecordByActivityIdCreatedDateBenId(incentiveActivityCH.getId(), session.getDate(), 0L,session.getAshaId()); if (record == null) { @@ -166,6 +224,14 @@ record = new IncentiveActivityRecord(); record.setBenId(0L); record.setAshaId(session.getAshaId()); record.setAmount(Long.valueOf(incentiveActivityCH.getRate())); + record.setIsEligible(true); + +// if(session.getAttachmentsJson()!=null){ +// record.setIsEligible(true); +// }else { +// record.setIsEligible(false); +// updateIncentivePendindDocService.updatePendingActivity(session.getAshaId(),session.getId(),record.getId(),incentiveActivityCH.getId()); +// } recordRepo.save(record); } } diff --git a/src/main/java/com/iemr/flw/service/impl/VillageLevelFormServiceImpl.java b/src/main/java/com/iemr/flw/service/impl/VillageLevelFormServiceImpl.java index 71bbdbad..c21cc9a5 100644 --- a/src/main/java/com/iemr/flw/service/impl/VillageLevelFormServiceImpl.java +++ b/src/main/java/com/iemr/flw/service/impl/VillageLevelFormServiceImpl.java @@ -24,11 +24,11 @@ */ package com.iemr.flw.service.impl; -import com.iemr.flw.controller.CoupleController; import com.iemr.flw.domain.iemr.*; import com.iemr.flw.dto.iemr.*; import com.iemr.flw.masterEnum.GroupName; import com.iemr.flw.repo.iemr.*; +import com.iemr.flw.service.IncentiveLogicService; import com.iemr.flw.service.VillageLevelFormService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -37,8 +37,10 @@ import java.sql.Timestamp; import java.time.LocalDate; +import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.util.Collections; +import java.util.Date; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; @@ -70,6 +72,9 @@ public class VillageLevelFormServiceImpl implements VillageLevelFormService { @Autowired private AHDFormRepo ahdFormRepo; + @Autowired + private IncentiveLogicService incentiveLogicService; + @Override public Boolean saveForm(VhndDto dto) { @@ -132,11 +137,20 @@ private Boolean saveVhncFormData(VhncFormDTO vhncFormDTO, Integer userID) { vhncForm.setImage2(vhncFormDTO.getImage2()); vhncForm.setImage1(vhncFormDTO.getImage1()); vhncForm.setPlace(vhncFormDTO.getPlace()); + vhncForm.setVillageName(vhncFormDTO.getVillageName()); + vhncForm.setAnm(vhncFormDTO.getAnm()); + vhncForm.setAww(vhncFormDTO.getAww()); + vhncForm.setNoOfPragnentWoment(vhncFormDTO.getNoOfPragnentWoment()); + vhncForm.setNoOfLactingMother(vhncFormDTO.getNoOfLactingMother()); + vhncForm.setNoOfCommittee(vhncFormDTO.getNoOfCommittee()); + vhncForm.setFollowupPrevius(vhncFormDTO.getFollowupPrevius()); + vhncForm.setNoOfBeneficiariesAttended(vhncFormDTO.getNoOfBeneficiariesAttended()); vhncForm.setFormType("VHNC"); vhncFormRepo.save(vhncForm); - checkAndAddIncentives(vhncForm.getVhncDate(), Math.toIntExact(vhncForm.getUserId()), "VHSNC_MEETING", vhncForm.getCreatedBy()); + + checkVhncIncentive(vhncForm.getCreatedDate(),Math.toIntExact(vhncForm.getUserId())); return true; } @@ -149,10 +163,15 @@ private Boolean savePhcForm(PhcReviewMeetingFormDTO dto, Integer userID) { phcReviewForm.setPlace(dto.getPlace()); phcReviewForm.setNoOfBeneficiariesAttended(dto.getNoOfBeneficiariesAttended()); phcReviewForm.setImage1(dto.getImage1()); + phcReviewForm.setVillageName(dto.getVillageName()); + phcReviewForm.setMitaninActivityCheckList(dto.getMitaninActivityCheckList()); + phcReviewForm.setPlaceId(dto.getPlaceId()); + phcReviewForm.setMitaninHistory(dto.getMitaninHistory()); phcReviewForm.setImage2(dto.getImage2()); phcReviewForm.setFormType("PHC"); phcReviewFormRepo.save(phcReviewForm); - checkAndAddIncentives(phcReviewForm.getPhcReviewDate(), Math.toIntExact(phcReviewForm.getUserId()), "CLUSTER_MEETING", phcReviewForm.getCreatedBy()); + + checkPhcMeetingIncentive(phcReviewForm.getCreatedDate(),Math.toIntExact(phcReviewForm.getUserId())); return true; } @@ -198,18 +217,184 @@ private Boolean saveVhndFormData(VHNDFormDTO vhndFormDTO, Integer userID) { VHNDForm vhndForm = new VHNDForm(); vhndForm.setUserId(userID); vhndForm.setVhndDate(vhndFormDTO.getVhndDate()); - vhndForm.setImage2(vhndFormDTO.getImage2()); - vhndForm.setImage1(vhndFormDTO.getImage1()); - vhndForm.setPlace(vhndFormDTO.getPlace()); - vhndForm.setNoOfBeneficiariesAttended(vhndFormDTO.getNoOfBeneficiariesAttended()); + vhndForm.setImage2(vhndFormDTO.getImage2() != null ? vhndFormDTO.getImage2() : ""); + vhndForm.setImage1(vhndFormDTO.getImage1() != null ? vhndFormDTO.getImage1() : ""); + vhndForm.setPlace(vhndFormDTO.getPlace() != null ? vhndFormDTO.getPlace() : ""); + + vhndForm.setNoOfBeneficiariesAttended( + vhndFormDTO.getNoOfBeneficiariesAttended() != null + ? vhndFormDTO.getNoOfBeneficiariesAttended() + : 0); + + vhndForm.setPregnantWomenAnc( + vhndFormDTO.getPregnantWomenAnc() != null + ? vhndFormDTO.getPregnantWomenAnc() + : null); + + vhndForm.setLactatingMothersPnc( + vhndFormDTO.getLactatingMothersPnc() != null + ? vhndFormDTO.getLactatingMothersPnc() + : null); + + vhndForm.setChildrenImmunization( + vhndFormDTO.getChildrenImmunization() != null + ? vhndFormDTO.getChildrenImmunization() + : null); + + vhndForm.setSelectAllEducation( + vhndFormDTO.getSelectAllEducation() != null + ? vhndFormDTO.getSelectAllEducation() + : false); + + vhndForm.setKnowledgeBalancedDiet( + vhndFormDTO.getKnowledgeBalancedDiet() != null + ? vhndFormDTO.getKnowledgeBalancedDiet() + : null); + + vhndForm.setCareDuringPregnancy( + vhndFormDTO.getCareDuringPregnancy() != null + ? vhndFormDTO.getCareDuringPregnancy() + : null); + + vhndForm.setImportanceBreastfeeding( + vhndFormDTO.getImportanceBreastfeeding() != null + ? vhndFormDTO.getImportanceBreastfeeding() + : null); + + vhndForm.setComplementaryFeeding( + vhndFormDTO.getComplementaryFeeding() != null + ? vhndFormDTO.getComplementaryFeeding() + : null); + + vhndForm.setHygieneSanitation( + vhndFormDTO.getHygieneSanitation() != null + ? vhndFormDTO.getHygieneSanitation() + : null); + + vhndForm.setFamilyPlanningHealthcare( + vhndFormDTO.getFamilyPlanningHealthcare() != null + ? vhndFormDTO.getFamilyPlanningHealthcare() + : null); vhndForm.setFormType("VHND"); vhndRepo.save(vhndForm); - checkAndAddIncentives(vhndForm.getVhndDate(), vhndForm.getUserId(), "VHND_PARTICIPATION", vhndForm.getCreatedBy()); + + + checkVhndIncentive(vhndForm.getCreatedDate(),vhndForm.getUserId()); + return true; } + private void checkVhndIncentive(Timestamp startTimestamp, Integer userId){ + + try { + IncentiveActivity activity = incentivesRepo.findIncentiveMasterByNameAndGroup("VHND_PARTICIPATION", GroupName.ACTIVITY.getDisplayName()); + if(activity!=null){ + IncentiveActivityRecord record = recordRepo + .findRecordByActivityIdCreatedDateBenId(activity.getId(), startTimestamp, 0L,userId); + if (record == null) { + record = new IncentiveActivityRecord(); + record.setActivityId(activity.getId()); + record.setCreatedDate(startTimestamp); + record.setCreatedBy(userRepo.getUserNamedByUserId(userId)); + record.setStartDate(startTimestamp); + record.setEndDate(startTimestamp); + record.setUpdatedDate(startTimestamp); + record.setUpdatedBy(userRepo.getUserNamedByUserId(userId)); + record.setAshaId(userId); + record.setBenId(0L); + record.setAmount(Long.valueOf(activity.getRate())); + record.setIsEligible(true); + recordRepo.save(record); + + } + } + + + + } catch (Exception e) { + logger.error("Process Incentive Exception: ", e); + + + } + + } + + private void checkPhcMeetingIncentive(Timestamp startTimestamp, Integer userId){ + + try { + IncentiveActivity activity = incentivesRepo.findIncentiveMasterByNameAndGroup("CLUSTER_MEETING", GroupName.ACTIVITY.getDisplayName()); + if(activity!=null){ + IncentiveActivityRecord record = recordRepo + .findRecordByActivityIdCreatedDateBenId(activity.getId(), startTimestamp, 0L,userId); + if (record == null) { + record = new IncentiveActivityRecord(); + record.setActivityId(activity.getId()); + record.setCreatedDate(startTimestamp); + record.setCreatedBy(userRepo.getUserNamedByUserId(userId)); + record.setStartDate(startTimestamp); + record.setEndDate(startTimestamp); + record.setUpdatedDate(startTimestamp); + record.setUpdatedBy(userRepo.getUserNamedByUserId(userId)); + record.setAshaId(userId); + record.setBenId(0L); + record.setIsDefaultActivity(true); + record.setAmount(Long.valueOf(activity.getRate())); + record.setIsEligible(true); + recordRepo.save(record); + + } + } + + + + } catch (Exception e) { + logger.error("Process Incentive Exception: ", e); + + + } + + } + + + private void checkVhncIncentive(Timestamp startTimestamp, Integer userId){ + + try { + IncentiveActivity activity = incentivesRepo.findIncentiveMasterByNameAndGroup("VHSNC_MEETING", GroupName.ACTIVITY.getDisplayName()); + if(activity!=null){ + IncentiveActivityRecord record = recordRepo + .findRecordByActivityIdCreatedDateBenId(activity.getId(), startTimestamp, 0L,userId); + if (record == null) { + record = new IncentiveActivityRecord(); + record.setActivityId(activity.getId()); + record.setCreatedDate(startTimestamp); + record.setCreatedBy(userRepo.getUserNamedByUserId(userId)); + record.setStartDate(startTimestamp); + record.setEndDate(startTimestamp); + record.setUpdatedDate(startTimestamp); + record.setUpdatedBy(userRepo.getUserNamedByUserId(userId)); + record.setAshaId(userId); + record.setBenId(0L); + record.setIsDefaultActivity(true); + record.setAmount(Long.valueOf(activity.getRate())); + record.setIsEligible(true); + recordRepo.save(record); + + } + } + + + + } catch (Exception e) { + logger.error("Process Incentive Exception: ", e); + + + } + + } + + @Override public List getAll(GetVillageLevelRequestHandler getVillageLevelRequestHandler) { if (Objects.equals(getVillageLevelRequestHandler.getFormType(), "VHND")) { @@ -244,6 +429,8 @@ public List getAll(GetVillageLevelRequestHandler getVillageLev private void checkAndAddIncentives(String date, Integer userID, String formType, String createdBY) { + String userName = userRepo.getUserNamedByUserId(userID); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy"); // Parse to LocalDate @@ -265,11 +452,11 @@ private void checkAndAddIncentives(String date, Integer userID, String formType, record = new IncentiveActivityRecord(); record.setActivityId(villageFormEntryActivityAM.getId()); record.setCreatedDate(timestamp); - record.setCreatedBy(userRepo.getUserNamedByUserId(userID)); + record.setCreatedBy(userName); record.setStartDate(timestamp); record.setEndDate(timestamp); record.setUpdatedDate(timestamp); - record.setUpdatedBy(userRepo.getUserNamedByUserId(userID)); + record.setUpdatedBy(userName); record.setAshaId(userID); record.setBenId(0L); record.setAmount(Long.valueOf(villageFormEntryActivityAM.getRate())); @@ -277,7 +464,6 @@ record = new IncentiveActivityRecord(); } } - if (villageFormEntryActivityCH != null) { IncentiveActivityRecord record = recordRepo .findRecordByActivityIdCreatedDateBenId(villageFormEntryActivityCH.getId(), timestamp, 0L,userID); @@ -285,12 +471,12 @@ record = new IncentiveActivityRecord(); record = new IncentiveActivityRecord(); record.setActivityId(villageFormEntryActivityCH.getId()); record.setCreatedDate(timestamp); - record.setCreatedBy(userRepo.getUserNamedByUserId(userID)); + record.setCreatedBy(userName); record.setStartDate(timestamp); record.setEndDate(timestamp); record.setBenId(0L); record.setUpdatedDate(timestamp); - record.setUpdatedBy(userRepo.getUserNamedByUserId(userID)); + record.setUpdatedBy(userName); record.setAshaId(userID); record.setAmount(Long.valueOf(villageFormEntryActivityCH.getRate())); recordRepo.save(record); diff --git a/src/main/java/com/iemr/flw/utils/JwtAuthenticationUtil.java b/src/main/java/com/iemr/flw/utils/JwtAuthenticationUtil.java index 9144ca3b..c44c41da 100644 --- a/src/main/java/com/iemr/flw/utils/JwtAuthenticationUtil.java +++ b/src/main/java/com/iemr/flw/utils/JwtAuthenticationUtil.java @@ -11,7 +11,7 @@ import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; -import com.iemr.flw.domain.iemr.M_User; +import com.iemr.flw.domain.iemr.User; import com.iemr.flw.repo.iemr.EmployeeMasterRepo; import com.iemr.flw.utils.exception.IEMRException; @@ -75,25 +75,25 @@ public boolean validateUserIdAndJwtToken(String jwtToken) throws IEMRException { String userId = claims.get("userId", String.class); // Check if user data is present in Redis - M_User user = getUserFromCache(userId); + User user = getUserFromCache(userId); if (user == null) { // If not in Redis, fetch from DB and cache the result user = fetchUserFromDB(userId); } if (user == null) { - throw new IEMRException("Invalid User ID."); + throw new IEMRException("Invalid User ID or user is deactivated please contact to admin."); } return true; // Valid userId and JWT token } catch (Exception e) { logger.error("Validation failed: " + e.getMessage(), e); - throw new IEMRException("Validation error: " + e.getMessage(), e); + throw new IEMRException("Validation error: Authentication failed", e); } } - private M_User getUserFromCache(String userId) { + private User getUserFromCache(String userId) { String redisKey = "user_" + userId; // The Redis key format - M_User user = (M_User) redisTemplate.opsForValue().get(redisKey); + User user = (User) redisTemplate.opsForValue().get(redisKey); if (user == null) { logger.warn("User not found in Redis. Will try to fetch from DB."); @@ -104,15 +104,15 @@ private M_User getUserFromCache(String userId) { return user; // Returns null if not found } - private M_User fetchUserFromDB(String userId) { + private User fetchUserFromDB(String userId) { // This method will only be called if the user is not found in Redis. String redisKey = "user_" + userId; // Redis key format // Fetch user from DB - M_User user = userLoginRepo.getUserByUserID(Integer.parseInt(userId)); + User user = userLoginRepo.findUserByUserID(Integer.parseInt(userId)); if (user != null) { - M_User userHash = new M_User(); + User userHash = new User(); userHash.setUserID(user.getUserID()); userHash.setUserName(user.getUserName()); diff --git a/src/main/java/com/iemr/flw/utils/JwtUserIdValidationFilter.java b/src/main/java/com/iemr/flw/utils/JwtUserIdValidationFilter.java index fcefd712..9cf022f0 100644 --- a/src/main/java/com/iemr/flw/utils/JwtUserIdValidationFilter.java +++ b/src/main/java/com/iemr/flw/utils/JwtUserIdValidationFilter.java @@ -116,7 +116,7 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo } catch (Exception e) { logger.error("Authorization error: ", e); - response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Authorization error: " + e.getMessage()); + response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Authorization error: Authentication failed"); } } @@ -152,7 +152,9 @@ private boolean shouldSkipPath(String path, String contextPath) { return path.equals(contextPath + "/user/userAuthenticate") || path.equalsIgnoreCase(contextPath + "/user/logOutUserFromConcurrentSession") || path.startsWith(contextPath + "/swagger-ui") || path.startsWith(contextPath + "/v3/api-docs") - || path.startsWith(contextPath + "/public"); + || path.startsWith(contextPath + "/public") + || path.equals(contextPath + "/health") + || path.equals(contextPath + "/version"); } private String getJwtTokenFromCookies(HttpServletRequest request) { diff --git a/src/main/java/com/iemr/flw/utils/JwtUtil.java b/src/main/java/com/iemr/flw/utils/JwtUtil.java index 7107d738..ab15e464 100644 --- a/src/main/java/com/iemr/flw/utils/JwtUtil.java +++ b/src/main/java/com/iemr/flw/utils/JwtUtil.java @@ -90,13 +90,4 @@ private Claims extractAllClaims(String token) { .getPayload(); } - public String getUserNameFromStorage() { - return userName; - - } - - public void setUserNameFromStorage(String loginUserName) { - this.userName = loginUserName; - - } } diff --git a/src/main/java/com/iemr/flw/utils/LocalDateAdapter.java b/src/main/java/com/iemr/flw/utils/LocalDateAdapter.java new file mode 100644 index 00000000..d0081c29 --- /dev/null +++ b/src/main/java/com/iemr/flw/utils/LocalDateAdapter.java @@ -0,0 +1,20 @@ +package com.iemr.flw.utils; + +import com.google.gson.*; +import java.lang.reflect.Type; +import java.time.LocalDate; + +public class LocalDateAdapter + implements JsonSerializer, JsonDeserializer { + + @Override + public JsonElement serialize(LocalDate src, Type typeOfSrc, JsonSerializationContext context) { + return new JsonPrimitive(src.toString()); // yyyy-MM-dd + } + + @Override + public LocalDate deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) + throws JsonParseException { + return LocalDate.parse(json.getAsString()); + } +} diff --git a/src/main/java/com/iemr/flw/utils/PathTraversalFilter.java b/src/main/java/com/iemr/flw/utils/PathTraversalFilter.java new file mode 100644 index 00000000..e63012bc --- /dev/null +++ b/src/main/java/com/iemr/flw/utils/PathTraversalFilter.java @@ -0,0 +1,56 @@ +package com.iemr.flw.utils; + +import jakarta.servlet.Filter; +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.ServletRequest; +import jakarta.servlet.ServletResponse; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.core.annotation.Order; +import org.springframework.stereotype.Component; + +import java.io.IOException; +import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; + +@Component +@Order(1) +public class PathTraversalFilter implements Filter { + + private final Logger logger = LoggerFactory.getLogger(this.getClass().getName()); + + @Override + public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) + throws IOException, ServletException { + + HttpServletRequest request = (HttpServletRequest) servletRequest; + HttpServletResponse response = (HttpServletResponse) servletResponse; + + String uri = request.getRequestURI(); + + // Decode once to catch %2e%2e and similar encoded variants + String decodedUri = URLDecoder.decode(uri, StandardCharsets.UTF_8); + + if (containsTraversalPattern(uri) || containsTraversalPattern(decodedUri)) { + logger.warn("Path traversal attempt blocked: {}", uri); + response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid request path"); + return; + } + + chain.doFilter(servletRequest, servletResponse); + } + + private boolean containsTraversalPattern(String path) { + if (path == null) return false; + String normalized = path.toLowerCase(); + return normalized.contains("../") + || normalized.contains("..\\") + || normalized.contains("..;") + || normalized.contains("%2e%2e") + || normalized.contains("%252e") // double-encoded + || normalized.endsWith(".."); + } +} diff --git a/src/main/java/com/iemr/flw/utils/http/HTTPRequestInterceptor.java b/src/main/java/com/iemr/flw/utils/http/HTTPRequestInterceptor.java index 884d9905..5c5aaf23 100644 --- a/src/main/java/com/iemr/flw/utils/http/HTTPRequestInterceptor.java +++ b/src/main/java/com/iemr/flw/utils/http/HTTPRequestInterceptor.java @@ -93,7 +93,7 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons if (remoteAddress == null || remoteAddress.trim().length() == 0) { remoteAddress = request.getRemoteAddr(); } -// validator.checkKeyExists(authorization, remoteAddress); + validator.checkKeyExists(authorization, remoteAddress); break; } } catch (Exception e) { diff --git a/src/main/java/com/iemr/flw/utils/redis/RedisConfig.java b/src/main/java/com/iemr/flw/utils/redis/RedisConfig.java index 0a279319..98acde7a 100644 --- a/src/main/java/com/iemr/flw/utils/redis/RedisConfig.java +++ b/src/main/java/com/iemr/flw/utils/redis/RedisConfig.java @@ -7,7 +7,7 @@ import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; -import com.iemr.flw.domain.iemr.M_User; +import com.iemr.flw.domain.iemr.User; @Configuration public class RedisConfig { @@ -20,7 +20,7 @@ public RedisTemplate redisTemplate(RedisConnectionFactory factor template.setKeySerializer(new StringRedisSerializer()); // Use Jackson2JsonRedisSerializer for values (Users objects) - Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer<>(M_User.class); + Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer<>(User.class); template.setValueSerializer(serializer); return template; diff --git a/src/main/java/com/iemr/flw/utils/sessionobject/SessionObject.java b/src/main/java/com/iemr/flw/utils/sessionobject/SessionObject.java index d84a1684..c18da820 100644 --- a/src/main/java/com/iemr/flw/utils/sessionobject/SessionObject.java +++ b/src/main/java/com/iemr/flw/utils/sessionobject/SessionObject.java @@ -81,7 +81,6 @@ private void updateConcurrentSessionObject(String key, String value, Boolean ext JsonElement jsnElmnt = jsnParser.parse(value); jsnOBJ = jsnElmnt.getAsJsonObject(); if (jsnOBJ.has("userName") && jsnOBJ.get("userName") != null) { - jwtUtil.setUserNameFromStorage(jsnOBJ.get("userName").getAsString()); objectStore.updateObject(jsnOBJ.get("userName").getAsString().trim().toLowerCase(), key, extendExpirationTime, sessionExpiryTime); } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8546550d..0b93c517 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -65,9 +65,10 @@ fhir-url= tm-url= springdoc.api-docs.enabled=true springdoc.swagger-ui.enabled=true + spring.redis.host=localhost spring.main.allow-bean-definition-overriding=true spring.redis.password= spring.redis.port=6379 -notificationurl=https://uatamrit.piramalswasthya.org/common-api/firebaseNotification/sendNotification \ No newline at end of file +notificationurl=firebaseNotification/sendNotification