Skip to content

Commit

Permalink
Merge efaccf2 into 53ccadf
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario Galic committed Jul 29, 2019
2 parents 53ccadf + efaccf2 commit 306be18
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 31 deletions.
Expand Up @@ -13,7 +13,7 @@ object HolidayStopProcess {
getRequests = Salesforce.holidayStopRequests(config.sfConfig),
getSubscription = Zuora.subscriptionGetResponse(config, zuoraAccessToken),
updateSubscription = Zuora.subscriptionUpdateResponse(config, zuoraAccessToken),
exportAddedCharges = Salesforce.holidayStopUpdateResponse(config.sfConfig)
writeHolidayStopsToSalesforce = Salesforce.holidayStopUpdateResponse(config.sfConfig)
)
} fold (ProcessResult.fromOverallFailure, identity)

Expand All @@ -22,39 +22,24 @@ object HolidayStopProcess {
getRequests: ProductName => Either[OverallFailure, Seq[HolidayStopRequestsDetail]],
getSubscription: SubscriptionName => Either[HolidayStopFailure, Subscription],
updateSubscription: (Subscription, HolidayCreditUpdate) => Either[HolidayStopFailure, Unit],
exportAddedCharges: Seq[HolidayStopResponse] => Either[OverallFailure, Unit]
writeHolidayStopsToSalesforce: Seq[HolidayStopResponse] => Either[OverallFailure, Unit]
): ProcessResult = {
val result = for {
(for {
requests <- getRequests(ProductName("Guardian Weekly"))
holidayStops <- Right(requests.distinct.map(HolidayStop(_)))
alreadyExportedChargeCodes <- Right(requests.flatMap(_.Charge_Code__c).distinct)
} yield {
val responses = holidayStops map {
processHolidayStop(
config,
getSubscription,
updateSubscription
)
}
val toExport = responses collect {
case Right(success) if !alreadyExportedChargeCodes.contains(success.chargeCode) =>
success
}
val exportResult = exportAddedCharges(toExport)
ProcessResult(
holidayStopsToApply = holidayStops,
holidayStopResults = responses,
resultsToExport = toExport,
overallFailure = exportResult.left.toOption
)
}
result.left.map(ProcessResult.fromOverallFailure).merge
val allZuoraHolidayStopResponses = holidayStops.map(writeHolidayStopToZuora(config, getSubscription, updateSubscription))
val successfulZuoraResponses = allZuoraHolidayStopResponses collect { case Right(success) if !alreadyExportedChargeCodes.contains(success.chargeCode) => success } // FIXME: We should make it clearer we are discarding failures.
val salesforceExportResult = writeHolidayStopsToSalesforce(successfulZuoraResponses).left.toOption
ProcessResult(holidayStops, allZuoraHolidayStopResponses, successfulZuoraResponses, salesforceExportResult)
}).left.map(ProcessResult.fromOverallFailure).merge
}

/**
* This is the main business logic
* This is the main business logic for writing holiday stop to Zuora
*/
def processHolidayStop(
def writeHolidayStopToZuora(
config: Config,
getSubscription: SubscriptionName => Either[HolidayStopFailure, Subscription],
updateSubscription: (Subscription, HolidayCreditUpdate) => Either[HolidayStopFailure, Unit]
Expand Down
Expand Up @@ -41,7 +41,7 @@ class HolidayStopProcessTest extends FlatSpec with Matchers with EitherValues wi
_ => amendmentExport

"HolidayStopProcess" should "give correct added charge" in {
val response = HolidayStopProcess.processHolidayStop(
val response = HolidayStopProcess.writeHolidayStopToZuora(
config,
getSubscription(Right(Fixtures.mkSubscriptionWithHolidayStops())),
updateSubscription(Right(()))
Expand All @@ -58,7 +58,7 @@ class HolidayStopProcessTest extends FlatSpec with Matchers with EitherValues wi
}

it should "give an exception message if update fails" in {
val response = HolidayStopProcess.processHolidayStop(
val response = HolidayStopProcess.writeHolidayStopToZuora(
config,
getSubscription(Right(subscription)),
updateSubscription(Left(HolidayStopFailure("update went wrong")))
Expand All @@ -67,7 +67,7 @@ class HolidayStopProcessTest extends FlatSpec with Matchers with EitherValues wi
}

it should "give an exception message if getting subscription details fails" in {
val response = HolidayStopProcess.processHolidayStop(
val response = HolidayStopProcess.writeHolidayStopToZuora(
config,
getSubscription(Left(HolidayStopFailure("get went wrong"))),
updateSubscription(Right(()))
Expand All @@ -76,7 +76,7 @@ class HolidayStopProcessTest extends FlatSpec with Matchers with EitherValues wi
}

it should "give an exception message if subscription isn't auto-renewing" in {
val response = HolidayStopProcess.processHolidayStop(
val response = HolidayStopProcess.writeHolidayStopToZuora(
config,
getSubscription(Right(subscription.copy(autoRenew = false))),
updateSubscription(Right(()))
Expand All @@ -86,7 +86,7 @@ class HolidayStopProcessTest extends FlatSpec with Matchers with EitherValues wi
}

it should "just give charge added without applying an update if holiday stop has already been applied" in {
val response = HolidayStopProcess.processHolidayStop(
val response = HolidayStopProcess.writeHolidayStopToZuora(
config,
getSubscription(Right(Fixtures.mkSubscriptionWithHolidayStops())),
updateSubscription(Left(HolidayStopFailure("shouldn't need to apply an update")))
Expand All @@ -103,7 +103,7 @@ class HolidayStopProcessTest extends FlatSpec with Matchers with EitherValues wi
}

it should "give a failure if subscription has no added charge" in {
val response = HolidayStopProcess.processHolidayStop(
val response = HolidayStopProcess.writeHolidayStopToZuora(
config,
getSubscription(Right(subscription)),
updateSubscription(Left(HolidayStopFailure("shouldn't need to apply an update")))
Expand Down

0 comments on commit 306be18

Please sign in to comment.