Skip to content

Commit

Permalink
improving readability of CreditCalculator
Browse files Browse the repository at this point in the history
  • Loading branch information
frj committed Sep 23, 2019
1 parent f91bb96 commit cedc94c
Showing 1 changed file with 10 additions and 22 deletions.
Expand Up @@ -2,7 +2,6 @@ package com.gu.holiday_stops

import java.time.LocalDate

import cats.data.NonEmptyList
import cats.syntax.either._
import com.gu.salesforce.holiday_stops.SalesforceHolidayStopRequestsDetail.SubscriptionName
import com.softwaremill.sttp.{Id, SttpBackend}
Expand Down Expand Up @@ -36,30 +35,19 @@ object CreditCalculator extends LazyLogging {
gwNforNProductRatePlanIds: List[String],
sundayVoucherRatePlanId: String,
stoppedPublicationDate: LocalDate
)(subscription: Subscription) = {
val creditCalculatorFunctions = NonEmptyList.of(
guardianWeeklyCredit(
guardianWeeklyProductRatePlanIds,
gwNforNProductRatePlanIds,
stoppedPublicationDate
) _,
)(subscription: Subscription): Either[ZuoraHolidayWriteError, Double] = {
guardianWeeklyCredit(
guardianWeeklyProductRatePlanIds,
gwNforNProductRatePlanIds,
stoppedPublicationDate
)(subscription) orElse {
sundayVoucherCredit(
sundayVoucherRatePlanId,
stoppedPublicationDate
) _
)

//Returns the result of the first function that returns a right
creditCalculatorFunctions
.tail
.foldRight(creditCalculatorFunctions.head(subscription)) { (creditCalculatorFunction, result) =>
result.recoverWith {
case _ => creditCalculatorFunction(subscription)
}
}
.leftMap { _ =>
ZuoraHolidayWriteError(s"Could not calculate credit for subscription: ${subscription.subscriptionNumber}")
}
)(subscription)
} orElse {
Left(ZuoraHolidayWriteError(s"Could not calculate credit for subscription: ${subscription.subscriptionNumber}"))
}
}

def guardianWeeklyCredit(guardianWeeklyProductRatePlanIds: List[String], gwNforNProductRatePlanIds: List[String], stoppedPublicationDate: LocalDate)(subscription: Subscription): Either[ZuoraHolidayWriteError, Double] =
Expand Down

0 comments on commit cedc94c

Please sign in to comment.