Skip to content

Commit cb504d0

Browse files
handling merchant not found
1 parent f201a60 commit cb504d0

File tree

3 files changed

+32
-24
lines changed

3 files changed

+32
-24
lines changed

src/main/java/it/gov/pagopa/idpay/transactions/connector/rest/MerchantRestClientImpl.java

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package it.gov.pagopa.idpay.transactions.connector.rest;
22

33
import it.gov.pagopa.common.reactive.utils.PerformanceLogger;
4+
import it.gov.pagopa.common.web.exception.ClientExceptionWithBody;
45
import it.gov.pagopa.idpay.transactions.connector.rest.dto.MerchantDetailDTO;
56
import it.gov.pagopa.idpay.transactions.connector.rest.dto.PointOfSaleDTO;
67
import java.time.Duration;
@@ -13,12 +14,16 @@
1314
import org.springframework.cache.annotation.Cacheable;
1415
import org.springframework.http.HttpEntity;
1516
import org.springframework.http.HttpMethod;
17+
import org.springframework.http.HttpStatus;
1618
import org.springframework.stereotype.Service;
1719
import org.springframework.web.reactive.function.client.WebClient;
1820
import org.springframework.web.reactive.function.client.WebClientResponseException;
1921
import reactor.core.publisher.Mono;
2022
import reactor.util.retry.Retry;
2123

24+
import static it.gov.pagopa.idpay.transactions.utils.ExceptionConstants.ExceptionCode.MERCHANT_NOT_FOUND;
25+
import static it.gov.pagopa.idpay.transactions.utils.ExceptionConstants.ExceptionMessage.ERROR_MESSAGE_MERCHANT_NOT_FOUND;
26+
2227
@Service
2328
@Slf4j
2429
@CacheConfig
@@ -90,39 +95,36 @@ public Mono<PointOfSaleDTO> getPointOfSale(String merchantId, String pointOfSale
9095
public Mono<MerchantDetailDTO> getMerchantDetail(String merchantId, String initiativeId) {
9196
log.info("Sending request to merchant {} to get merchant details", Utilities.sanitizeString(merchantId));
9297

93-
return PerformanceLogger.logTimingOnNext(
94-
"MERCHANT_INTEGRATION",
95-
webClient
96-
.get()
97-
.uri(URI_MERCHANT_DETAIL, Map.of("initiativeId", initiativeId))
98-
.header("x-merchant-id", merchantId)
99-
.retrieve()
100-
.bodyToMono(MerchantDetailDTO.class),
101-
x -> "received merchant detail"
98+
return webClient
99+
.get()
100+
.uri(URI_MERCHANT_DETAIL, Map.of("initiativeId", initiativeId))
101+
.header("x-merchant-id", merchantId)
102+
.retrieve()
103+
.onStatus(HttpStatus.NOT_FOUND::equals, response ->
104+
response.bodyToMono(String.class)
105+
.flatMap(body -> {
106+
log.warn("Merchant not found for merchantId {}", merchantId);
107+
return Mono.error(new ClientExceptionWithBody(
108+
HttpStatus.NOT_FOUND,
109+
MERCHANT_NOT_FOUND,
110+
ERROR_MESSAGE_MERCHANT_NOT_FOUND.formatted(merchantId, initiativeId)
111+
));
112+
})
102113
)
114+
.bodyToMono(MerchantDetailDTO.class)
103115
.retryWhen(
104116
Retry.fixedDelay(maxAttempts, Duration.ofMillis(retryDelay))
105-
.filter(ex -> {
106-
boolean retry =
107-
ex instanceof WebClientResponseException.TooManyRequests ||
108-
ex.getMessage().startsWith("Connection refused");
109-
110-
if (retry) {
111-
log.info("[MERCHANT_INTEGRATION] Retrying invocation due to exception: {}: {}",
112-
ex.getClass().getSimpleName(), ex.getMessage());
113-
}
114-
return retry;
115-
})
117+
.filter(ex ->
118+
ex instanceof WebClientResponseException.TooManyRequests ||
119+
ex.getMessage().startsWith("Connection refused")
120+
)
116121
)
117-
.onErrorResume(WebClientResponseException.NotFound.class, ex -> {
118-
log.warn("Merchant detail not found for merchant {}", Utilities.sanitizeString(merchantId));
119-
return Mono.empty();
120-
})
121122
.onErrorResume(WebClientResponseException.BadRequest.class, ex -> {
122123
log.warn("Invalid request for merchant {}", Utilities.sanitizeString(merchantId));
123124
return Mono.empty();
124125
});
125126
}
126127

127128

129+
128130
}

src/main/java/it/gov/pagopa/idpay/transactions/service/ReportServiceImpl.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ public Mono<ReportDTO> generateMerchantTransactionsReport(String merchantId,
133133
RewardBatchAssignee operatorLevel = resolveOperatorLevel(organizationRole);
134134

135135
return merchantRestClient.getMerchantDetail(merchantId, initiativeId)
136+
.switchIfEmpty(Mono.error(new ClientExceptionWithBody(
137+
HttpStatus.NOT_FOUND,
138+
MERCHANT_NOT_FOUND,
139+
ERROR_MESSAGE_MERCHANT_NOT_FOUND.formatted(merchantId, initiativeId) )))
136140
.flatMap(merchant -> {
137141

138142
String formattedDate = LocalDateTime.now().format(FILE_NAME_FORMAT);

src/main/java/it/gov/pagopa/idpay/transactions/utils/ExceptionConstants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ private ExceptionCode(){}
3939
public static final String REWARD_BATCH_PREVIOUS_NOT_SENT = "REWARD_BATCH_PREVIOUS_NOT_SENT";
4040
public static final String INVALID_CHECKS_ERROR = "INVALID_CHECKS_ERROR";
4141
public static final String REPORT_NOT_FOUND = "REPORT_NOT_FOUND";
42+
public static final String MERCHANT_NOT_FOUND = "MERCHANT_NOT_FOUND";
4243
}
4344

4445
public static final class ExceptionMessage {
@@ -79,5 +80,6 @@ private ExceptionMessage(){}
7980

8081
public static final String ERROR_MESSAGE_INVALID_CHECKS_ERROR = "At least one checksError field must be true";
8182
public static final String ERROR_MESSAGE_REPORT_NOT_FOUND = "Report %s not found for initiative %s ";
83+
public static final String ERROR_MESSAGE_MERCHANT_NOT_FOUND = "Merchant %s not found for initiative %s ";
8284
}
8385
}

0 commit comments

Comments
 (0)