Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #409 from guardian/sunday-holiday-stops-skeleton
Sunday holiday stops skeleton
- Loading branch information
Showing
19 changed files
with
461 additions
and
328 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...ocessor/src/main/scala/com/gu/holidaystopprocessor/GuardianWeeklyHolidayStopProcess.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package com.gu.holidaystopprocessor | ||
|
||
import java.time.LocalDate | ||
|
||
import com.gu.holiday_stops.{CreditCalculator, CurrentGuardianWeeklySubscription, ExtendedTerm, GuardianWeeklyHolidayStopConfig, HolidayCreditProduct, HolidayCreditUpdate, HolidayStop, OverallFailure, SalesforceHolidayWriteError, Subscription, ZuoraHolidayWriteError} | ||
import com.gu.salesforce.holiday_stops.SalesforceHolidayStopRequestsDetail.{HolidayStopRequestsDetail, HolidayStopRequestsDetailChargeCode, HolidayStopRequestsDetailChargePrice, ProductName, StoppedPublicationDate, SubscriptionName} | ||
import cats.implicits._ | ||
import com.gu.holiday_stops.ActionCalculator.{GuardianWeeklySuspensionConstants, suspensionConstantsByProduct} | ||
|
||
object GuardianWeeklyHolidayStopProcess { | ||
def processHolidayStops( | ||
config: GuardianWeeklyHolidayStopConfig, | ||
getHolidayStopRequestsFromSalesforce: (ProductName, LocalDate) => Either[OverallFailure, List[HolidayStopRequestsDetail]], | ||
getSubscription: SubscriptionName => Either[ZuoraHolidayWriteError, Subscription], | ||
updateSubscription: (Subscription, HolidayCreditUpdate) => Either[ZuoraHolidayWriteError, Unit], | ||
writeHolidayStopsToSalesforce: List[HolidayStopResponse] => Either[SalesforceHolidayWriteError, Unit], | ||
processDateOverride: Option[LocalDate] | ||
): ProcessResult = { | ||
getHolidayStopRequestsFromSalesforce(ProductName("Guardian Weekly"), calculateProcessDate(processDateOverride)) match { | ||
case Left(overallFailure) => | ||
ProcessResult(overallFailure) | ||
|
||
case Right(holidayStopRequestsFromSalesforce) => | ||
val holidayStops = holidayStopRequestsFromSalesforce.distinct.map(HolidayStop(_)) | ||
val alreadyActionedHolidayStops = holidayStopRequestsFromSalesforce.flatMap(_.Charge_Code__c).distinct | ||
val allZuoraHolidayStopResponses = | ||
holidayStops.map( | ||
writeHolidayStopToZuora( | ||
config.holidayCreditProduct, | ||
config.productRatePlanIds, | ||
config.nForNProductRatePlanIds, | ||
getSubscription, | ||
updateSubscription | ||
) | ||
) | ||
val (failedZuoraResponses, successfulZuoraResponses) = allZuoraHolidayStopResponses.separate | ||
val notAlreadyActionedHolidays = successfulZuoraResponses.filterNot(v => alreadyActionedHolidayStops.contains(v.chargeCode)) | ||
val salesforceExportResult = writeHolidayStopsToSalesforce(notAlreadyActionedHolidays) | ||
ProcessResult( | ||
holidayStops, | ||
allZuoraHolidayStopResponses, | ||
notAlreadyActionedHolidays, | ||
OverallFailure(failedZuoraResponses, salesforceExportResult) | ||
) | ||
} | ||
} | ||
|
||
private def calculateProcessDate(processDateOverride: Option[LocalDate]) = { | ||
processDateOverride.getOrElse(LocalDate.now.plusDays(GuardianWeeklySuspensionConstants.processorRunLeadTimeDays)) | ||
} | ||
|
||
/** | ||
* This is the main business logic for writing holiday stop to Zuora | ||
*/ | ||
def writeHolidayStopToZuora( | ||
holidayCreditProduct: HolidayCreditProduct, | ||
guardianWeeklyProductRatePlanIds: List[String], | ||
gwNforNProductRatePlanIds: List[String], | ||
getSubscription: SubscriptionName => Either[ZuoraHolidayWriteError, Subscription], | ||
updateSubscription: (Subscription, HolidayCreditUpdate) => Either[ZuoraHolidayWriteError, Unit] | ||
)(stop: HolidayStop): Either[ZuoraHolidayWriteError, HolidayStopResponse] = | ||
for { | ||
subscription <- getSubscription(stop.subscriptionName) | ||
_ <- if (subscription.autoRenew) Right(()) else Left(ZuoraHolidayWriteError("Cannot currently process non-auto-renewing subscription")) | ||
currentGuardianWeeklySubscription <- CurrentGuardianWeeklySubscription(subscription, guardianWeeklyProductRatePlanIds, gwNforNProductRatePlanIds) | ||
nextInvoiceStartDate = NextBillingPeriodStartDate(currentGuardianWeeklySubscription, stop.stoppedPublicationDate) | ||
maybeExtendedTerm = ExtendedTerm(nextInvoiceStartDate, subscription) | ||
holidayCredit <- CreditCalculator.guardianWeeklyCredit(guardianWeeklyProductRatePlanIds, gwNforNProductRatePlanIds, stop.stoppedPublicationDate)(subscription) | ||
holidayCreditUpdate <- HolidayCreditUpdate(holidayCreditProduct, subscription, stop.stoppedPublicationDate, nextInvoiceStartDate, maybeExtendedTerm, holidayCredit) | ||
_ <- if (subscription.hasHolidayStop(stop)) Right(()) else updateSubscription(subscription, holidayCreditUpdate) | ||
updatedSubscription <- getSubscription(stop.subscriptionName) | ||
addedCharge <- updatedSubscription.ratePlanCharge(stop).toRight(ZuoraHolidayWriteError("Failed to add charge to subscription")) | ||
} yield { | ||
HolidayStopResponse( | ||
stop.requestId, | ||
stop.subscriptionName, | ||
stop.productName, | ||
HolidayStopRequestsDetailChargeCode(addedCharge.number), | ||
stop.estimatedCharge, | ||
HolidayStopRequestsDetailChargePrice(addedCharge.price), | ||
StoppedPublicationDate(addedCharge.HolidayStart__c.getOrElse(LocalDate.MIN)) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...cessor/src/main/scala/com/gu/holidaystopprocessor/SundayVoucherHolidayStopProcessor.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.gu.holidaystopprocessor | ||
|
||
import java.time.LocalDate | ||
|
||
import com.gu.holiday_stops._ | ||
import com.gu.salesforce.holiday_stops.SalesforceHolidayStopRequestsDetail.{HolidayStopRequestsDetail, ProductName, SubscriptionName} | ||
|
||
object SundayVoucherHolidayStopProcessor { | ||
def processHolidayStops( | ||
config: SundayVoucherHolidayStopConfig, | ||
getHolidayStopRequestsFromSalesforce: (ProductName, LocalDate) => Either[OverallFailure, List[HolidayStopRequestsDetail]], | ||
getSubscription: SubscriptionName => Either[ZuoraHolidayWriteError, Subscription], | ||
updateSubscription: (Subscription, HolidayCreditUpdate) => Either[ZuoraHolidayWriteError, Unit], | ||
writeHolidayStopsToSalesforce: List[HolidayStopResponse] => Either[SalesforceHolidayWriteError, Unit], | ||
processDateOverride: Option[LocalDate] | ||
): ProcessResult = | ||
ProcessResult(Nil, Nil, Nil, None) | ||
} |
Oops, something went wrong.