diff --git a/handlers/holiday-stop-processor/src/main/scala/com/gu/holidaystopprocessor/HolidayStopProcess.scala b/handlers/holiday-stop-processor/src/main/scala/com/gu/holidaystopprocessor/HolidayStopProcess.scala index 401997aeb9..617e9c62e7 100644 --- a/handlers/holiday-stop-processor/src/main/scala/com/gu/holidaystopprocessor/HolidayStopProcess.scala +++ b/handlers/holiday-stop-processor/src/main/scala/com/gu/holidaystopprocessor/HolidayStopProcess.scala @@ -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) @@ -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] diff --git a/handlers/holiday-stop-processor/src/test/scala/com/gu/holidaystopprocessor/HolidayStopProcessTest.scala b/handlers/holiday-stop-processor/src/test/scala/com/gu/holidaystopprocessor/HolidayStopProcessTest.scala index 4d3d3000bb..1aa32fb887 100644 --- a/handlers/holiday-stop-processor/src/test/scala/com/gu/holidaystopprocessor/HolidayStopProcessTest.scala +++ b/handlers/holiday-stop-processor/src/test/scala/com/gu/holidaystopprocessor/HolidayStopProcessTest.scala @@ -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(())) @@ -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"))) @@ -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(())) @@ -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(())) @@ -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"))) @@ -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")))