Skip to content

Commit

Permalink
Estimate price with region code if results are missing
Browse files Browse the repository at this point in the history
  • Loading branch information
katherine-signal authored Feb 6, 2024
1 parent 46a3bf8 commit f7a1264
Showing 1 changed file with 11 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,16 @@ protected void analyzeAttempts() {
@Override
protected CompletableFuture<AttemptAnalysis> analyzeAttempt(final AttemptPendingAnalysis attemptPendingAnalysis) {
return getSmsReport(attemptPendingAnalysis)
.thenApply(maybeReport -> maybeReport.map(report -> {
final MccMnc mccMnc = MccMnc.fromString(report.getMccMnc());
.thenApply(maybeReport -> {
final Optional<Money> maybeActualPrice = maybeReport.flatMap(report -> extractPrice(report.getPrice()));
final Optional<MccMnc> maybeMccMnc = maybeReport.map(report -> MccMnc.fromString(report.getMccMnc()));
final Optional<Money> maybeEstimatedPriceByMccMnc = maybeMccMnc.flatMap(mccMnc -> estimatePrice(mccMnc.toString()));
return new AttemptAnalysis(
extractPrice(report.getPrice()),
estimatePrice(mccMnc, attemptPendingAnalysis),
Optional.ofNullable(mccMnc.mcc()),
Optional.ofNullable(mccMnc.mnc()));
}).orElse(AttemptAnalysis.EMPTY))
maybeActualPrice,
maybeEstimatedPriceByMccMnc.or(() -> estimatePrice(attemptPendingAnalysis.getRegion())),
maybeMccMnc.map(MccMnc::mcc),
maybeMccMnc.map(MccMnc::mnc));
})
.exceptionally(ignored -> AttemptAnalysis.EMPTY);
}

Expand Down Expand Up @@ -133,9 +135,8 @@ private Optional<Money> extractPrice(final SmsPrice smsPrice) {
: Optional.empty();
}

private Optional<Money> estimatePrice(final MccMnc mccMnc, final AttemptPendingAnalysis attemptPendingAnalysis) {
return defaultSmsPricesRepository.get(mccMnc.toString())
.or(() -> defaultSmsPricesRepository.get(attemptPendingAnalysis.getRegion()))
private Optional<Money> estimatePrice(final String key) {
return defaultSmsPricesRepository.get(key)
.map(price -> new Money(price, defaultPriceCurrency));
}

Expand Down

0 comments on commit f7a1264

Please sign in to comment.