From 26f566528cd6e7c017614235f617794cdbca59a6 Mon Sep 17 00:00:00 2001 From: Taniya-eGov <103186387+Taniya-eGov@users.noreply.github.com> Date: Wed, 29 May 2024 17:12:57 +0530 Subject: [PATCH 1/3] Duplicate bill changes --- .../java/org/egov/demand/model/BillV2.java | 3 ++ .../demand/repository/BillRepositoryV2.java | 18 ++++---- .../querybuilder/BillQueryBuilder.java | 15 ++++--- .../repository/rowmapper/BillRowMapperV2.java | 1 + .../egov/demand/service/BillServicev2.java | 41 ++++++++++++++++++- .../java/org/egov/demand/util/Constants.java | 14 +++++++ .../demand/web/validator/BillValidator.java | 32 +++++++++++++++ ...1130141020__egbs_bill_alter_add_userid.sql | 1 + ...4420__egbs_bill_alter_add_consumercode.sql | 7 ++++ 9 files changed, 115 insertions(+), 17 deletions(-) create mode 100644 business-services/billing-service/src/main/resources/db/migration/main/V20221130141020__egbs_bill_alter_add_userid.sql create mode 100644 business-services/billing-service/src/main/resources/db/migration/main/V20221206104420__egbs_bill_alter_add_consumercode.sql diff --git a/business-services/billing-service/src/main/java/org/egov/demand/model/BillV2.java b/business-services/billing-service/src/main/java/org/egov/demand/model/BillV2.java index ca69ebbad..c4c281313 100644 --- a/business-services/billing-service/src/main/java/org/egov/demand/model/BillV2.java +++ b/business-services/billing-service/src/main/java/org/egov/demand/model/BillV2.java @@ -28,6 +28,9 @@ public class BillV2 { @Size(max = 256) private String id; + @JsonProperty("userId") + private String userId; + @JsonProperty("mobileNumber") @Pattern(regexp = "^[0-9]{10}$", message = "MobileNumber should be 10 digit number") private String mobileNumber; diff --git a/business-services/billing-service/src/main/java/org/egov/demand/repository/BillRepositoryV2.java b/business-services/billing-service/src/main/java/org/egov/demand/repository/BillRepositoryV2.java index c650f4985..cdf43681b 100644 --- a/business-services/billing-service/src/main/java/org/egov/demand/repository/BillRepositoryV2.java +++ b/business-services/billing-service/src/main/java/org/egov/demand/repository/BillRepositoryV2.java @@ -69,6 +69,8 @@ public void setValues(PreparedStatement ps, int index) throws SQLException { AuditDetails auditDetails = bill.getAuditDetails(); ps.setString(1, bill.getId()); + ps.setString(15, bill.getUserId()); + ps.setString(16, bill.getConsumerCode()); ps.setString(2, bill.getTenantId()); ps.setString(3, bill.getPayerName()); ps.setString(4, bill.getPayerAddress()); @@ -82,8 +84,8 @@ public void setValues(PreparedStatement ps, int index) throws SQLException { ps.setString(12, bill.getMobileNumber()); ps.setString(13, bill.getStatus().toString()); ps.setObject(14, util.getPGObject(bill.getAdditionalDetails())); - ps.setString(15, bill.getConsumerCode()); - ps.setString(16, bill.getTenantId()); + // ps.setString(15, bill.getConsumerCode()); + // ps.setString(16, bill.getTenantId()); } @@ -93,12 +95,12 @@ public int getBatchSize() { } }); - for (int i = 0; i < saveBill.length; i++) { - if(0 == saveBill[i]) - throw new CustomException("EG_BS_DUPLICATE_ACTIVE_BILL_INSERTION_ERROR", - "Insertion failed due to presence of ACTIVE bill in DB for consumer-code : " - + bills.get(i).getConsumerCode()); - } + // for (int i = 0; i < saveBill.length; i++) { + // if(0 == saveBill[i]) + // throw new CustomException("EG_BS_DUPLICATE_ACTIVE_BILL_INSERTION_ERROR", + // "Insertion failed due to presence of ACTIVE bill in DB for consumer-code : " + // + bills.get(i).getConsumerCode()); + // } saveBillDetails(billRequest); } diff --git a/business-services/billing-service/src/main/java/org/egov/demand/repository/querybuilder/BillQueryBuilder.java b/business-services/billing-service/src/main/java/org/egov/demand/repository/querybuilder/BillQueryBuilder.java index b17622f6e..df7ae022e 100644 --- a/business-services/billing-service/src/main/java/org/egov/demand/repository/querybuilder/BillQueryBuilder.java +++ b/business-services/billing-service/src/main/java/org/egov/demand/repository/querybuilder/BillQueryBuilder.java @@ -22,10 +22,9 @@ public class BillQueryBuilder { public static final String BILL_STATUS_UPDATE_BASE_QUERY = "UPDATE egbs_bill_v1 SET status=?, lastmodifieddate=? {replace} WHERE status='ACTIVE' AND tenantId = ? "; public static final String INSERT_BILL_QUERY = "INSERT into egbs_bill_v1 " - + " (id, tenantid, payername, payeraddress, payeremail, isactive, iscancelled, createdby, createddate, lastmodifiedby, lastmodifieddate, mobilenumber, status, additionaldetails)" - + " SELECT ?,?,?,?,?,?,?,?,?,?,?,?,?,? " - + " WHERE NOT EXISTS (SELECT distinct(consumercode) FROM egbs_bill_v1 bill INNER JOIN egbs_billdetail_v1 billd ON bill.id=billd.billid " - + " WHERE status = 'ACTIVE' AND consumerCode = ? AND bill.tenantid = ?)"; + +"(id, tenantid, payername, payeraddress, payeremail, isactive, iscancelled, createdby, createddate, lastmodifiedby, lastmodifieddate," + +" mobilenumber, status, additionaldetails, payerid, consumercode)" + +"values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; public static final String INSERT_BILLDETAILS_QUERY = "INSERT into egbs_billdetail_v1 " @@ -49,7 +48,7 @@ public class BillQueryBuilder { + " INNER JOIN (SELECT bd_consumercode, min(b_createddate) as mindate FROM billresult GROUP BY bd_consumercode) as uniqbill" + " ON uniqbill.bd_consumercode=billresult.bd_consumercode AND uniqbill.mindate=billresult.b_createddate "; - public static final String BILL_BASE_QUERY = "SELECT b.id AS b_id,b.mobilenumber, b.tenantid AS b_tenantid," + public static final String BILL_BASE_QUERY = "SELECT b.id AS b_id,b.mobilenumber, b.tenantid AS b_tenantid,b.payerid AS b_payerid," + " b.payername AS b_payername, b.payeraddress AS b_payeraddress, b.payeremail AS b_payeremail,b.filestoreid AS b_fileStoreId," + " b.isactive AS b_isactive, b.iscancelled AS b_iscancelled, b.createdby AS b_createdby, b.status as b_status," + " b.createddate AS b_createddate, b.lastmodifiedby AS b_lastmodifiedby, b.lastmodifieddate AS b_lastmodifieddate," @@ -82,9 +81,9 @@ public String getBillQuery(BillSearchCriteria billSearchCriteria, List p } addWhereClause(billQuery, preparedStatementValues, billSearchCriteria); - if(billSearchCriteria!=null && billSearchCriteria.getReturnAllBills()) { - return billQuery.toString(); - } + // if(billSearchCriteria!=null && billSearchCriteria.getReturnAllBills()) { + // return billQuery.toString(); + // } StringBuilder maxQuery = addPagingClause(billQuery, preparedStatementValues, billSearchCriteria); return maxQuery.toString(); diff --git a/business-services/billing-service/src/main/java/org/egov/demand/repository/rowmapper/BillRowMapperV2.java b/business-services/billing-service/src/main/java/org/egov/demand/repository/rowmapper/BillRowMapperV2.java index caa8e6e8b..ade4777fc 100644 --- a/business-services/billing-service/src/main/java/org/egov/demand/repository/rowmapper/BillRowMapperV2.java +++ b/business-services/billing-service/src/main/java/org/egov/demand/repository/rowmapper/BillRowMapperV2.java @@ -50,6 +50,7 @@ public List extractData(ResultSet rs) throws SQLException { .id(billId) .totalAmount(BigDecimal.ZERO) .tenantId(rs.getString("b_tenantid")) + .userId(rs.getString("b_payerid")) .payerName(rs.getString("b_payername")) .payerAddress(rs.getString("b_payeraddress")) .payerEmail(rs.getString("b_payeremail")) diff --git a/business-services/billing-service/src/main/java/org/egov/demand/service/BillServicev2.java b/business-services/billing-service/src/main/java/org/egov/demand/service/BillServicev2.java index d485ac915..3f53c587a 100644 --- a/business-services/billing-service/src/main/java/org/egov/demand/service/BillServicev2.java +++ b/business-services/billing-service/src/main/java/org/egov/demand/service/BillServicev2.java @@ -62,6 +62,7 @@ import java.util.UUID; import java.util.function.Function; import java.util.stream.Collectors; +import java.util.stream.Stream; import org.egov.common.contract.request.RequestInfo; import org.egov.demand.config.ApplicationProperties; @@ -99,6 +100,10 @@ import org.springframework.util.CollectionUtils; import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; +import org.egov.demand.producer.Producer; +import org.egov.demand.web.contract.UserResponse; +import org.egov.demand.web.contract.UserSearchRequest; +import com.fasterxml.jackson.databind.ObjectMapper; import lombok.extern.slf4j.Slf4j; @@ -138,9 +143,20 @@ public class BillServicev2 { @Autowired private BillValidator billValidator; + + @Autowired + private Producer producer; + + @Autowired + private ObjectMapper mapper; + + @Value("${kafka.topics.cancel.bill.topic.name}") + private String billCancelTopic; @Value("${kafka.topics.billgen.topic.name}") private String notifTopicName; + + private static List ownerPlainRequestFieldsList; /** * Cancell bill operation can be carried by this method, based on consumerCodes @@ -154,6 +170,7 @@ public class BillServicev2 { public Integer cancelBill(UpdateBillRequest updateBillRequest) { UpdateBillCriteria cancelBillCriteria = updateBillRequest.getUpdateBillCriteria(); + billValidator.validateBillSearchRequest(cancelBillCriteria); Set consumerCodes = cancelBillCriteria.getConsumerCodes(); cancelBillCriteria.setStatusToBeUpdated(BillStatus.CANCELLED); @@ -162,10 +179,31 @@ public Integer cancelBill(UpdateBillRequest updateBillRequest) { throw new CustomException("EG_BS_CANCEL_BILL_ERROR", "Only one consumer code can be provided in the Cancel request"); } else { - return billRepository.updateBillStatus(cancelBillCriteria); + int result = billRepository.updateBillStatus(cancelBillCriteria); + sendNotificationForBillCancellation(updateBillRequest.getRequestInfo(), cancelBillCriteria); + return result; } } + private void sendNotificationForBillCancellation(RequestInfo requestInfo, UpdateBillCriteria cancelBillCriteria) { + Set consumerCodes = cancelBillCriteria.getConsumerCodes(); + if(CollectionUtils.isEmpty(consumerCodes)) + return; + + List bills = billRepository.findBill(BillSearchCriteria.builder() + .service(cancelBillCriteria.getBusinessService()) + .tenantId(cancelBillCriteria.getTenantId()) + .consumerCode(consumerCodes) + .build()); + + if (CollectionUtils.isEmpty(bills)) + return; + + BillRequestV2 req = BillRequestV2.builder().bills(bills).requestInfo(requestInfo).build(); + producer.push(billCancelTopic, req); + + } + /** * Fetches the bill for given parameters * @@ -428,6 +466,7 @@ private List prepareBill(List demands, RequestInfo requestInfo) .status(BillStatus.ACTIVE) .billDetails(billDetails) .totalAmount(billAmount) + .userId(payer.getUuid()) .billNumber(billNumber) .tenantId(tenantId) .id(billId) diff --git a/business-services/billing-service/src/main/java/org/egov/demand/util/Constants.java b/business-services/billing-service/src/main/java/org/egov/demand/util/Constants.java index 7ddff20a9..d516bedfd 100644 --- a/business-services/billing-service/src/main/java/org/egov/demand/util/Constants.java +++ b/business-services/billing-service/src/main/java/org/egov/demand/util/Constants.java @@ -171,4 +171,18 @@ private Constants() {} public static final String PAID_CANCEL_BILL_MSG = "You cannot cancel the bill in the workflow"; public static final String COUNT_REPLACE_CANCEL_BILL_MSG = "{count}"; + + // Bill cancel + + public static final String CANCELLATION_REASON_MSG= "reasonMessage"; + + public static final String CANCELLATION_REASON_CODE= "reason"; + + public static final String CANCELL_REASON_MSG_NOT_FOUND = "EG_BS_CANCELL_REASON_MSG_NOT"; + public static final String CANCELL_REASON_MSG_NOT_FOUND_MSG = "Cancell Reason message with key " + "'" + CANCELLATION_REASON_MSG + "'" + " is madnatory Text field in additionalDetails of Bill cancell Request"; + public static final String CANCELL_REASON_MSG_EMPTY_MSG = "Cancell Reason message cannot be empty"; + + public static final String CANCELL_REASON_CODE_NOT_FOUND = "EG_BS_CANCELL_REASON_CODE_NOT"; + public static final String CANCELL_REASON_CODE_NOT_FOUND_MSG = "Cancellation Reason with key " + "'" + CANCELLATION_REASON_CODE + "'" + " is a madnatory Text field in additionalDetails of Bill cancell Request"; + public static final String CANCELL_REASON_CODE_EMPTY_MSG = "Cancell Reason code cannot be empty"; } \ No newline at end of file diff --git a/business-services/billing-service/src/main/java/org/egov/demand/web/validator/BillValidator.java b/business-services/billing-service/src/main/java/org/egov/demand/web/validator/BillValidator.java index d44b7ac3d..709d5a33d 100644 --- a/business-services/billing-service/src/main/java/org/egov/demand/web/validator/BillValidator.java +++ b/business-services/billing-service/src/main/java/org/egov/demand/web/validator/BillValidator.java @@ -1,10 +1,13 @@ package org.egov.demand.web.validator; +import java.util.HashMap; +import java.util.Map; import static org.egov.demand.util.Constants.BILL_GEN_MANDATORY_FIELDS_MISSING_KEY; import static org.egov.demand.util.Constants.BILL_GEN_MANDATORY_FIELDS_MISSING_MSG; import org.egov.common.contract.request.RequestInfo; import org.egov.demand.model.BillSearchCriteria; +import org.egov.demand.model.BillV2.BillStatus; import org.egov.demand.model.GenerateBillCriteria; import org.egov.demand.util.Util; import org.egov.tracer.model.CustomException; @@ -12,6 +15,11 @@ import org.springframework.stereotype.Component; import org.springframework.util.CollectionUtils; import org.springframework.validation.Errors; +import org.egov.demand.model.UpdateBillCriteria; +import org.egov.demand.util.Constants; +import org.springframework.util.StringUtils; + +import com.fasterxml.jackson.databind.JsonNode; @Component public class BillValidator { @@ -57,4 +65,28 @@ public void validateBillSearchCriteria(BillSearchCriteria billCriteria, RequestI } } + public void validateBillSearchRequest(UpdateBillCriteria cancelBillCriteria) { + cancelBillCriteria.setStatusToBeUpdated(BillStatus.CANCELLED); + JsonNode additionalDetails = cancelBillCriteria.getAdditionalDetails(); + Map errorMap = new HashMap<>(); + JsonNode reasonMsg = additionalDetails.get(Constants.CANCELLATION_REASON_MSG); + JsonNode reasonCode = additionalDetails.get(Constants.CANCELLATION_REASON_CODE); + if (null != reasonCode && reasonCode.isTextual()) { + if (StringUtils.isEmpty(reasonCode.textValue())) + errorMap.put(Constants.CANCELL_REASON_CODE_NOT_FOUND, Constants.CANCELL_REASON_CODE_EMPTY_MSG); + } else { + errorMap.put(Constants.CANCELL_REASON_CODE_NOT_FOUND, Constants.CANCELL_REASON_CODE_NOT_FOUND_MSG); + } + + if (null != reasonMsg && reasonMsg.isTextual()) { + if (StringUtils.isEmpty(reasonMsg.textValue())) + errorMap.put(Constants.CANCELL_REASON_MSG_NOT_FOUND, Constants.CANCELL_REASON_MSG_EMPTY_MSG); + } else { + errorMap.put(Constants.CANCELL_REASON_MSG_NOT_FOUND, Constants.CANCELL_REASON_MSG_NOT_FOUND_MSG); + } + if (!CollectionUtils.isEmpty(errorMap)) { + throw new CustomException(errorMap); + } + } + } diff --git a/business-services/billing-service/src/main/resources/db/migration/main/V20221130141020__egbs_bill_alter_add_userid.sql b/business-services/billing-service/src/main/resources/db/migration/main/V20221130141020__egbs_bill_alter_add_userid.sql new file mode 100644 index 000000000..e37239ab9 --- /dev/null +++ b/business-services/billing-service/src/main/resources/db/migration/main/V20221130141020__egbs_bill_alter_add_userid.sql @@ -0,0 +1 @@ +ALTER TABLE egbs_bill_v1 add column payerid character varying (128); \ No newline at end of file diff --git a/business-services/billing-service/src/main/resources/db/migration/main/V20221206104420__egbs_bill_alter_add_consumercode.sql b/business-services/billing-service/src/main/resources/db/migration/main/V20221206104420__egbs_bill_alter_add_consumercode.sql new file mode 100644 index 000000000..a8ed241df --- /dev/null +++ b/business-services/billing-service/src/main/resources/db/migration/main/V20221206104420__egbs_bill_alter_add_consumercode.sql @@ -0,0 +1,7 @@ +ALTER TABLE egbs_bill_v1 ADD COLUMN IF NOT EXISTS consumercode CHARACTER VARYING(256); + +UPDATE egbs_bill_v1 b SET consumerCode = bd.consumercode FROM egbs_billdetail_v1 bd WHERE bd.billid = b.id; + +ALTER TABLE egbs_bill_v1 ALTER COLUMN consumercode SET NOT NULL; + +CREATE UNIQUE INDEX IF NOT EXISTS index_egbs_UNIQUE_ACTIVE_BILL ON egbs_bill_v1 (consumercode, tenantid, status) where status='ACTIVE'; \ No newline at end of file From 3c9743437502acfc6bef812a563ebff334351e28 Mon Sep 17 00:00:00 2001 From: Taniya-eGov <103186387+Taniya-eGov@users.noreply.github.com> Date: Thu, 30 May 2024 16:02:16 +0530 Subject: [PATCH 2/3] updated BillRepositoryV2 --- .../org/egov/demand/repository/BillRepositoryV2.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/business-services/billing-service/src/main/java/org/egov/demand/repository/BillRepositoryV2.java b/business-services/billing-service/src/main/java/org/egov/demand/repository/BillRepositoryV2.java index cdf43681b..dfb0514de 100644 --- a/business-services/billing-service/src/main/java/org/egov/demand/repository/BillRepositoryV2.java +++ b/business-services/billing-service/src/main/java/org/egov/demand/repository/BillRepositoryV2.java @@ -95,12 +95,12 @@ public int getBatchSize() { } }); - // for (int i = 0; i < saveBill.length; i++) { - // if(0 == saveBill[i]) - // throw new CustomException("EG_BS_DUPLICATE_ACTIVE_BILL_INSERTION_ERROR", - // "Insertion failed due to presence of ACTIVE bill in DB for consumer-code : " - // + bills.get(i).getConsumerCode()); - // } + for (int i = 0; i < saveBill.length; i++) { + if(0 == saveBill[i]) + throw new CustomException("EG_BS_DUPLICATE_ACTIVE_BILL_INSERTION_ERROR", + "Insertion failed due to presence of ACTIVE bill in DB for consumer-code : " + + bills.get(i).getConsumerCode()); + } saveBillDetails(billRequest); } From b4e6559de6e8003ae539589743386af9d6678658 Mon Sep 17 00:00:00 2001 From: Taniya-eGov <103186387+Taniya-eGov@users.noreply.github.com> Date: Wed, 12 Jun 2024 12:49:07 +0530 Subject: [PATCH 3/3] updated application.properties --- .../billing-service/src/main/resources/application.properties | 1 + 1 file changed, 1 insertion(+) diff --git a/business-services/billing-service/src/main/resources/application.properties b/business-services/billing-service/src/main/resources/application.properties index b38493e0d..d5c30b522 100644 --- a/business-services/billing-service/src/main/resources/application.properties +++ b/business-services/billing-service/src/main/resources/application.properties @@ -113,6 +113,7 @@ egov.apportion.endpoint=/apportion-service/v2/demand/_apportion # billgen notif topic kafka.topics.billgen.topic.name=billing-billgen-topic-name bill.notification.fallback.locale=en_IN +kafka.topics.cancel.bill.topic.name=bill-cancel-topic-name # notif topic kafka.topics.notification.sms=egov.core.notification.sms