From f971b6147381ffbbd58cf8d6f76de871e1ff8adb Mon Sep 17 00:00:00 2001 From: Anatoly Karlov Date: Thu, 31 Oct 2024 19:24:32 +0700 Subject: [PATCH] create package .result.* --- .../ProviderDisputesCallbackHandler.java | 2 +- .../schedule/core/AdjustmentsService.java | 31 +++++++++---------- .../schedule/core/CreatedDisputesService.java | 12 +++---- .../schedule/core/PendingDisputesService.java | 2 +- .../DisputeCreateResultHandler.java | 2 +- .../DisputeStatusResultHandler.java | 2 +- .../schedule/result/ErrorResultHandler.java | 26 ++++++++++++++++ 7 files changed, 50 insertions(+), 27 deletions(-) rename src/main/java/dev/vality/disputes/schedule/{handler => result}/DisputeCreateResultHandler.java (99%) rename src/main/java/dev/vality/disputes/schedule/{handler => result}/DisputeStatusResultHandler.java (99%) create mode 100644 src/main/java/dev/vality/disputes/schedule/result/ErrorResultHandler.java diff --git a/src/main/java/dev/vality/disputes/callback/ProviderDisputesCallbackHandler.java b/src/main/java/dev/vality/disputes/callback/ProviderDisputesCallbackHandler.java index 2b16eed..1c7fd28 100644 --- a/src/main/java/dev/vality/disputes/callback/ProviderDisputesCallbackHandler.java +++ b/src/main/java/dev/vality/disputes/callback/ProviderDisputesCallbackHandler.java @@ -2,7 +2,7 @@ package dev.vality.disputes.callback; import dev.vality.disputes.dao.DisputeDao; import dev.vality.disputes.dao.ProviderDisputeDao; -import dev.vality.disputes.schedule.handler.DisputeStatusResultHandler; +import dev.vality.disputes.schedule.result.DisputeStatusResultHandler; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.thrift.TException; diff --git a/src/main/java/dev/vality/disputes/schedule/core/AdjustmentsService.java b/src/main/java/dev/vality/disputes/schedule/core/AdjustmentsService.java index bed028d..28025d3 100644 --- a/src/main/java/dev/vality/disputes/schedule/core/AdjustmentsService.java +++ b/src/main/java/dev/vality/disputes/schedule/core/AdjustmentsService.java @@ -10,6 +10,7 @@ import dev.vality.disputes.domain.tables.pojos.Dispute; import dev.vality.disputes.schedule.converter.InvoicePaymentCapturedAdjustmentParamsConverter; import dev.vality.disputes.schedule.converter.InvoicePaymentCashFlowAdjustmentParamsConverter; import dev.vality.disputes.schedule.converter.InvoicePaymentFailedAdjustmentParamsConverter; +import dev.vality.disputes.schedule.result.ErrorResultHandler; import dev.vality.disputes.schedule.service.AdjustmentExtractor; import dev.vality.disputes.service.external.InvoicingService; import lombok.RequiredArgsConstructor; @@ -34,6 +35,7 @@ public class AdjustmentsService { private final InvoicePaymentCashFlowAdjustmentParamsConverter invoicePaymentCashFlowAdjustmentParamsConverter; private final InvoicePaymentFailedAdjustmentParamsConverter invoicePaymentFailedAdjustmentParamsConverter; private final AdjustmentExtractor adjustmentExtractor; + private final ErrorResultHandler errorResultHandler; @Transactional(propagation = Propagation.REQUIRED) public List getDisputesForHgCall(int batchSize) { @@ -62,16 +64,13 @@ public class AdjustmentsService { log.debug("GetDisputeForUpdateSkipLocked has been found {}", dispute); var invoicePayment = getInvoicePayment(dispute); if (invoicePayment == null || !invoicePayment.isSetRoute()) { - updateFailed(dispute, ErrorReason.PAYMENT_NOT_FOUND); + errorResultHandler.updateFailed(dispute, ErrorReason.PAYMENT_NOT_FOUND); return; } if (!adjustmentExtractor.isCashFlowAdjustmentByDisputeExist(invoicePayment, dispute) && !Objects.equals(dispute.getAmount(), dispute.getChangedAmount())) { var params = invoicePaymentCashFlowAdjustmentParamsConverter.convert(dispute); - var paymentAdjustment = createAdjustment(dispute, params); - if (paymentAdjustment == null) { - var errorReason = ErrorReason.INVOICE_NOT_FOUND; - updateFailed(dispute, errorReason); + if (!createAdjustment(dispute, params)) { return; } } else { @@ -80,16 +79,12 @@ public class AdjustmentsService { if (!adjustmentExtractor.isCapturedAdjustmentByDisputeExist(invoicePayment, dispute)) { if (invoicePayment.getPayment().getStatus().isSetCaptured()) { var params = invoicePaymentFailedAdjustmentParamsConverter.convert(dispute); - var paymentAdjustment = createAdjustment(dispute, params); - if (paymentAdjustment == null) { - updateFailed(dispute, ErrorReason.INVOICE_NOT_FOUND); + if (!createAdjustment(dispute, params)) { return; } } var params = invoicePaymentCapturedAdjustmentParamsConverter.convert(dispute); - var paymentAdjustment = createAdjustment(dispute, params); - if (paymentAdjustment == null) { - updateFailed(dispute, ErrorReason.INVOICE_NOT_FOUND); + if (!createAdjustment(dispute, params)) { return; } } else { @@ -100,14 +95,18 @@ public class AdjustmentsService { log.debug("Dispute status has been set to succeeded {}", dispute.getId()); } + @Transactional(propagation = Propagation.REQUIRED) - void updateFailed(Dispute dispute, String errorReason) { - log.error("Trying to set failed Dispute status with {} error reason {}", errorReason, dispute.getId()); - disputeDao.update(dispute.getId(), DisputeStatus.failed, errorReason); - log.debug("Dispute status has been set to failed {}", dispute.getId()); + boolean createAdjustment(Dispute dispute, InvoicePaymentAdjustmentParams params) { + var paymentAdjustment = createPaymentAdjustment(dispute, params); + if (paymentAdjustment == null) { + errorResultHandler.updateFailed(dispute, ErrorReason.INVOICE_NOT_FOUND); + return false; + } + return true; } - private InvoicePaymentAdjustment createAdjustment(Dispute dispute, InvoicePaymentAdjustmentParams params) { + private InvoicePaymentAdjustment createPaymentAdjustment(Dispute dispute, InvoicePaymentAdjustmentParams params) { return invoicingService.createPaymentAdjustment(dispute.getInvoiceId(), dispute.getPaymentId(), params); } diff --git a/src/main/java/dev/vality/disputes/schedule/core/CreatedDisputesService.java b/src/main/java/dev/vality/disputes/schedule/core/CreatedDisputesService.java index 0abf144..1bd4e7b 100644 --- a/src/main/java/dev/vality/disputes/schedule/core/CreatedDisputesService.java +++ b/src/main/java/dev/vality/disputes/schedule/core/CreatedDisputesService.java @@ -9,8 +9,9 @@ import dev.vality.disputes.provider.DisputeCreatedResult; import dev.vality.disputes.schedule.catcher.WRuntimeExceptionCatcher; import dev.vality.disputes.schedule.client.DefaultRemoteClient; import dev.vality.disputes.schedule.client.RemoteClient; -import dev.vality.disputes.schedule.handler.DisputeCreateResultHandler; import dev.vality.disputes.schedule.model.ProviderData; +import dev.vality.disputes.schedule.result.DisputeCreateResultHandler; +import dev.vality.disputes.schedule.result.ErrorResultHandler; import dev.vality.disputes.schedule.service.AttachmentsService; import dev.vality.disputes.schedule.service.ProviderDataService; import dev.vality.disputes.service.external.InvoicingService; @@ -40,6 +41,7 @@ public class CreatedDisputesService { private final ProviderDataService providerDataService; private final DefaultRemoteClient defaultRemoteClient; private final DisputeCreateResultHandler disputeCreateResultHandler; + private final ErrorResultHandler errorResultHandler; private final WRuntimeExceptionCatcher wRuntimeExceptionCatcher; @Transactional(propagation = Propagation.REQUIRED) @@ -61,16 +63,12 @@ public class CreatedDisputesService { log.debug("GetDisputeForUpdateSkipLocked has been found {}", dispute); var invoicePayment = getInvoicePayment(dispute); if (invoicePayment == null || !invoicePayment.isSetRoute()) { - log.error("Trying to set failed Dispute status with PAYMENT_NOT_FOUND error reason {}", dispute.getId()); - disputeDao.update(dispute.getId(), DisputeStatus.failed, ErrorReason.PAYMENT_NOT_FOUND); - log.debug("Dispute status has been set to failed {}", dispute.getId()); + errorResultHandler.updateFailed(dispute, ErrorReason.PAYMENT_NOT_FOUND); return; } var attachments = attachmentsService.getAttachments(dispute); if (attachments == null || attachments.isEmpty()) { - log.error("Trying to set failed Dispute status with NO_ATTACHMENTS error reason {}", dispute.getId()); - disputeDao.update(dispute.getId(), DisputeStatus.failed, ErrorReason.NO_ATTACHMENTS); - log.debug("Dispute status has been set to failed {}", dispute.getId()); + errorResultHandler.updateFailed(dispute, ErrorReason.NO_ATTACHMENTS); return; } var providerData = providerDataService.getProviderData(dispute.getProviderId(), dispute.getTerminalId()); diff --git a/src/main/java/dev/vality/disputes/schedule/core/PendingDisputesService.java b/src/main/java/dev/vality/disputes/schedule/core/PendingDisputesService.java index 31a750b..eef7903 100644 --- a/src/main/java/dev/vality/disputes/schedule/core/PendingDisputesService.java +++ b/src/main/java/dev/vality/disputes/schedule/core/PendingDisputesService.java @@ -9,7 +9,7 @@ import dev.vality.disputes.polling.PollingInfoService; import dev.vality.disputes.provider.DisputeStatusResult; import dev.vality.disputes.schedule.catcher.WRuntimeExceptionCatcher; import dev.vality.disputes.schedule.client.RemoteClient; -import dev.vality.disputes.schedule.handler.DisputeStatusResultHandler; +import dev.vality.disputes.schedule.result.DisputeStatusResultHandler; import dev.vality.disputes.schedule.service.ProviderDataService; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; diff --git a/src/main/java/dev/vality/disputes/schedule/handler/DisputeCreateResultHandler.java b/src/main/java/dev/vality/disputes/schedule/result/DisputeCreateResultHandler.java similarity index 99% rename from src/main/java/dev/vality/disputes/schedule/handler/DisputeCreateResultHandler.java rename to src/main/java/dev/vality/disputes/schedule/result/DisputeCreateResultHandler.java index 9fa3ffc..f98ce46 100644 --- a/src/main/java/dev/vality/disputes/schedule/handler/DisputeCreateResultHandler.java +++ b/src/main/java/dev/vality/disputes/schedule/result/DisputeCreateResultHandler.java @@ -1,4 +1,4 @@ -package dev.vality.disputes.schedule.handler; +package dev.vality.disputes.schedule.result; import dev.vality.disputes.admin.callback.CallbackNotifier; import dev.vality.disputes.admin.management.MdcTopicProducer; diff --git a/src/main/java/dev/vality/disputes/schedule/handler/DisputeStatusResultHandler.java b/src/main/java/dev/vality/disputes/schedule/result/DisputeStatusResultHandler.java similarity index 99% rename from src/main/java/dev/vality/disputes/schedule/handler/DisputeStatusResultHandler.java rename to src/main/java/dev/vality/disputes/schedule/result/DisputeStatusResultHandler.java index f3ba4ce..e85f20a 100644 --- a/src/main/java/dev/vality/disputes/schedule/handler/DisputeStatusResultHandler.java +++ b/src/main/java/dev/vality/disputes/schedule/result/DisputeStatusResultHandler.java @@ -1,4 +1,4 @@ -package dev.vality.disputes.schedule.handler; +package dev.vality.disputes.schedule.result; import dev.vality.disputes.admin.callback.CallbackNotifier; import dev.vality.disputes.admin.management.MdcTopicProducer; diff --git a/src/main/java/dev/vality/disputes/schedule/result/ErrorResultHandler.java b/src/main/java/dev/vality/disputes/schedule/result/ErrorResultHandler.java new file mode 100644 index 0000000..3db83be --- /dev/null +++ b/src/main/java/dev/vality/disputes/schedule/result/ErrorResultHandler.java @@ -0,0 +1,26 @@ +package dev.vality.disputes.schedule.result; + +import dev.vality.disputes.dao.DisputeDao; +import dev.vality.disputes.domain.enums.DisputeStatus; +import dev.vality.disputes.domain.tables.pojos.Dispute; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Propagation; +import org.springframework.transaction.annotation.Transactional; + +@Slf4j +@Service +@RequiredArgsConstructor +@SuppressWarnings({"ParameterName", "LineLength", "MissingSwitchDefault"}) +public class ErrorResultHandler { + + private final DisputeDao disputeDao; + + @Transactional(propagation = Propagation.REQUIRED) + public void updateFailed(Dispute dispute, String errorReason) { + log.error("Trying to set failed Dispute status with {} error reason {}", errorReason, dispute.getId()); + disputeDao.update(dispute.getId(), DisputeStatus.failed, errorReason); + log.debug("Dispute status has been set to failed {}", dispute.getId()); + } +}