Skip to content

Commit

Permalink
Merge pull request #2058 from guardian/jd-new-prod-api-refactor
Browse files Browse the repository at this point in the history
new product api refactor
  • Loading branch information
johnduffell authored Oct 3, 2023
2 parents 42d24fd + 9b4b85a commit 4c38af8
Show file tree
Hide file tree
Showing 13 changed files with 95 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import com.gu.newproduct.api.addsubscription.zuora.GetAccount.WireModel.ZuoraAcc
import com.gu.newproduct.api.addsubscription.zuora.GetAccountSubscriptions.WireModel.ZuoraSubscriptionsResponse
import com.gu.newproduct.api.addsubscription.zuora.GetContacts.Contacts
import com.gu.newproduct.api.addsubscription.zuora.GetContacts.WireModel.GetContactsResponse
import com.gu.newproduct.api.addsubscription.zuora.GetPaymentMethod.{DirectDebit, PaymentMethod, PaymentMethodWire}
import com.gu.newproduct.api.addsubscription.zuora.GetPaymentMethod.{PaymentMethod, PaymentMethodWire}
import com.gu.newproduct.api.addsubscription.zuora.{GetAccount, GetAccountSubscriptions, GetContacts, GetPaymentMethod}
import com.gu.newproduct.api.productcatalog.PlanId.MonthlyContribution
import com.gu.newproduct.api.productcatalog.ZuoraIds.{HasPlanAndChargeIds, ProductRatePlanId, ZuoraIds}
Expand Down Expand Up @@ -49,7 +49,7 @@ object AddContribution {
request.planId,
account.currency,
).toApiGatewayOp.toAsync
acceptanceDate = request.startDate.plusDays(paymentDelayFor(paymentMethod))
acceptanceDate = request.startDate
planAndCharge <- getPlanAndCharge(request.planId)
.toApiGatewayContinueProcessing(internalServerError(s"no Zuora id for ${request.planId}!"))
.toAsync
Expand Down Expand Up @@ -117,11 +117,6 @@ object AddContribution {

}

def paymentDelayFor(paymentMethod: PaymentMethod): Long = paymentMethod match {
case d: DirectDebit => 10L
case _ => 0L
}

def toContributionEmailData(
request: AddSubscriptionRequest,
plan: Plan,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import com.gu.newproduct.api.addsubscription.zuora.GetAccount.WireModel.ZuoraAcc
import com.gu.newproduct.api.addsubscription.zuora.GetAccountSubscriptions.WireModel.ZuoraSubscriptionsResponse
import com.gu.newproduct.api.addsubscription.zuora.GetContacts.Contacts
import com.gu.newproduct.api.addsubscription.zuora.GetContacts.WireModel.GetContactsResponse
import com.gu.newproduct.api.addsubscription.zuora.GetPaymentMethod.{DirectDebit, PaymentMethod, PaymentMethodWire}
import com.gu.newproduct.api.addsubscription.zuora.GetPaymentMethod.{PaymentMethod, PaymentMethodWire}
import com.gu.newproduct.api.addsubscription.zuora.{GetAccount, GetAccountSubscriptions, GetContacts, GetPaymentMethod}
import com.gu.newproduct.api.productcatalog.PlanId.MonthlySupporterPlus
import com.gu.newproduct.api.productcatalog.ZuoraIds.{PlanAndCharges, ProductRatePlanId, ZuoraIds}
Expand Down Expand Up @@ -53,7 +53,7 @@ object AddSupporterPlus {
request.planId,
account.currency,
).toApiGatewayOp.toAsync
acceptanceDate = request.startDate.plusDays(paymentDelayFor(paymentMethod))
acceptanceDate = request.startDate
plan = getPlan(request.planId)
planAndCharge <- getPlanAndCharge(request.planId)
.toApiGatewayContinueProcessing(internalServerError(s"no Zuora id for ${request.planId}!"))
Expand Down Expand Up @@ -123,11 +123,6 @@ object AddSupporterPlus {

}

def paymentDelayFor(paymentMethod: PaymentMethod): Long = paymentMethod match {
case d: DirectDebit => 10L
case _ => 0L
}

def toSupporterPlusEmailData(
request: AddSubscriptionRequest,
plan: Plan,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,31 @@ import com.gu.newproduct.api.productcatalog.{HomeDeliveryPlanId, PlanId}
object PaperAddressValidator {
def apply(planId: PlanId, soldToAddress: SoldToAddress): ValidationResult[Unit] =
for {
_ <-
(soldToAddress.country == Country.UK) orFailWith s"Invalid country: ${soldToAddress.country.name}, only UK addresses are allowed"
- <- postCodeValidationFor(planId)(soldToAddress.postcode)
_ <- (soldToAddress.country == Country.UK) orFailWith cantDeliverOutsideUK(soldToAddress)
_ <- postCodeValidationFor(planId)(soldToAddress.postcode)
} yield ()

def postCodeValidationFor(planId: PlanId): Option[Postcode] => ValidationResult[Any] = planId match {
private def cantDeliverOutsideUK(soldToAddress: SoldToAddress): String =
s"Invalid country: ${soldToAddress.country.name}, only UK addresses are allowed"

private def postCodeValidationFor(planId: PlanId): Option[Postcode] => ValidationResult[Any] = planId match {
case _: HomeDeliveryPlanId => validatePostCodeForHomeDelivery _
case _ => (postCode: Option[Postcode]) => Passed(())
}

def validatePostCodeForHomeDelivery(postcode: Option[Postcode]): ValidationResult[Postcode] = for {
private def validatePostCodeForHomeDelivery(postcode: Option[Postcode]): ValidationResult[Postcode] = for {
deliveryPostcode <- postcode getOrFailWith ("delivery postcode is required")
_ <- isWithinM25(
deliveryPostcode,
) orFailWith (s"Invalid postcode ${deliveryPostcode.value}: postcode must be within M25")
} yield (deliveryPostcode)

def isWithinM25(postcode: Postcode): Boolean = {
private def isWithinM25(postcode: Postcode): Boolean = {
val normalisedPostCode = postcode.value.toUpperCase.filterNot(_.isWhitespace)
M25_POSTCODE_PREFIXES.exists(normalisedPostCode.startsWith(_))
}

val M25_POSTCODE_PREFIXES = List(
private val M25_POSTCODE_PREFIXES = List(
"BR1",
"BR2",
"BR3",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@ import java.time.LocalDate

class ContributionsPlans(today: LocalDate) {

private val ContributionStartDateWindowSize = WindowSizeDays(1)

private val contributionsRule = StartDateRules(
private val rule = StartDateRules(
windowRule = WindowRule(
startDate = today,
maybeSize = Some(ContributionStartDateWindowSize),
maybeSize = Some(WindowSizeDays(1)),
),
)

val planInfo: List[(PlanId, PlanDescription, StartDateRules, BillingPeriod)] = List(
(MonthlyContribution, PlanDescription("Monthly"), contributionsRule, Monthly),
(AnnualContribution, PlanDescription("Annual"), contributionsRule, Monthly),
(MonthlyContribution, PlanDescription("Monthly"), rule, Monthly),
(AnnualContribution, PlanDescription("Annual"), rule, Monthly),
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,18 @@ import java.time.LocalDate

class DigitalPackPlans(today: LocalDate) {

private val DigiPackFreeTrialPeriodDays = 14
private val DigiPackStartDateWindowSize = WindowSizeDays(90)
private val FreeTrialPeriodDays = 14

private val digiPackWindowRule = WindowRule(
startDate = today.plusDays(DigiPackFreeTrialPeriodDays.toLong),
maybeSize = Some(DigiPackStartDateWindowSize),
)

private val digipackStartRules = StartDateRules(
windowRule = digiPackWindowRule,
private val startRules = StartDateRules(
windowRule = WindowRule(
startDate = today.plusDays(FreeTrialPeriodDays.toLong),
maybeSize = Some(WindowSizeDays(90)),
),
)

val planInfo: List[(PlanId, PlanDescription, StartDateRules, BillingPeriod)] = List(
(DigipackAnnual, PlanDescription("Annual"), digipackStartRules, Annual),
(DigipackMonthly, PlanDescription("Monthly"), digipackStartRules, Monthly),
(DigipackAnnual, PlanDescription("Annual"), startRules, Annual),
(DigipackMonthly, PlanDescription("Monthly"), startRules, Monthly),
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,81 +5,77 @@ import com.gu.newproduct.api.productcatalog._

import java.time.{DayOfWeek, LocalDate}

class DigitalVoucherPlans(
getStartDateFromFulfilmentFiles: (ProductType, List[DayOfWeek]) => LocalDate
) {
class DigitalVoucherPlans(getStartDateFromFulfilmentFiles: (ProductType, List[DayOfWeek]) => LocalDate) {

import PaperDays._
private val DigitalVoucherStartDateWindowSize = WindowSizeDays(1)

private def digitalVoucherStartDateRule(daysOfWeek: List[DayOfWeek]) =
private def startDateRule(daysOfWeek: List[DayOfWeek]) =
StartDateRules(
daysOfWeekRule = Some(DaysOfWeekRule(daysOfWeek)),
windowRule = WindowRule(
startDate = getStartDateFromFulfilmentFiles(ProductType.NewspaperDigitalVoucher, daysOfWeek),
maybeSize = Some(DigitalVoucherStartDateWindowSize),
maybeSize = Some(WindowSizeDays(1)),
),
)

val planInfo: List[(PlanId, PlanDescription, StartDateRules, BillingPeriod)] = List(
(
DigitalVoucherWeekend,
PlanDescription("Weekend"),
digitalVoucherStartDateRule(weekendDays),
startDateRule(weekendDays),
Monthly,
),
(
DigitalVoucherWeekendPlus,
PlanDescription("Weekend+"),
digitalVoucherStartDateRule(weekendDays),
startDateRule(weekendDays),
Monthly,
),
(
DigitalVoucherEveryday,
PlanDescription("Everyday"),
digitalVoucherStartDateRule(everyDayDays),
startDateRule(everyDayDays),
Monthly,
),
(
DigitalVoucherEverydayPlus,
PlanDescription("Everyday+"),
digitalVoucherStartDateRule(everyDayDays),
startDateRule(everyDayDays),
Monthly,
),
(
DigitalVoucherSaturday,
PlanDescription("Saturday"),
digitalVoucherStartDateRule(saturdayDays),
startDateRule(saturdayDays),
Monthly,
),
(
DigitalVoucherSaturdayPlus,
PlanDescription("Saturday+"),
digitalVoucherStartDateRule(saturdayDays),
startDateRule(saturdayDays),
Monthly,
),
(
DigitalVoucherSunday,
PlanDescription("Sunday"),
digitalVoucherStartDateRule(sundayDays),
startDateRule(sundayDays),
Monthly,
),
(
DigitalVoucherSundayPlus,
PlanDescription("Sunday+"),
digitalVoucherStartDateRule(sundayDays),
startDateRule(sundayDays),
Monthly,
),
(
DigitalVoucherSixday,
PlanDescription("Sixday"),
digitalVoucherStartDateRule(sixDayDays),
startDateRule(sixDayDays),
Monthly,
),
(
DigitalVoucherSixdayPlus,
PlanDescription("Sixday+"),
digitalVoucherStartDateRule(sixDayDays),
startDateRule(sixDayDays),
Monthly,
),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,57 +5,53 @@ import com.gu.newproduct.api.productcatalog._

import java.time.{DayOfWeek, LocalDate}

class GuardianWeeklyPlans(
getStartDateFromFulfilmentFiles: (ProductType, List[DayOfWeek]) => LocalDate,
) {
class GuardianWeeklyPlans(getStartDateFromFulfilmentFiles: (ProductType, List[DayOfWeek]) => LocalDate) {

private val GuardianWeeklySubscriptionStartDateWindowSize = WindowSizeDays(28)

private val guardianWeeklyIssueDays = List(DayOfWeek.FRIDAY)
private val guardianWeeklyStartDateRules =
private val issueDays = List(DayOfWeek.FRIDAY)
private val startDateRules =
StartDateRules(
daysOfWeekRule = Some(DaysOfWeekRule(guardianWeeklyIssueDays)),
daysOfWeekRule = Some(DaysOfWeekRule(issueDays)),
windowRule = WindowRule(
startDate = getStartDateFromFulfilmentFiles(ProductType.GuardianWeekly, guardianWeeklyIssueDays),
maybeSize = Some(GuardianWeeklySubscriptionStartDateWindowSize),
startDate = getStartDateFromFulfilmentFiles(ProductType.GuardianWeekly, issueDays),
maybeSize = Some(WindowSizeDays(28)),
),
)

val planInfo: List[(PlanId, PlanDescription, StartDateRules, BillingPeriod)] = List(
(
GuardianWeeklyDomestic6for6,
PlanDescription("GW Oct 18 - Six for Six - Domestic"),
guardianWeeklyStartDateRules,
startDateRules,
SixWeeks,
),
(
GuardianWeeklyDomesticQuarterly,
PlanDescription("GW Oct 18 - Quarterly - Domestic"),
guardianWeeklyStartDateRules,
startDateRules,
Quarterly,
),
(
GuardianWeeklyDomesticAnnual,
PlanDescription("GW Oct 18 - Annual - Domestic"),
guardianWeeklyStartDateRules,
startDateRules,
Annual,
),
(
GuardianWeeklyROW6for6,
PlanDescription("GW Oct 18 - Six for Six - ROW"),
guardianWeeklyStartDateRules,
startDateRules,
SixWeeks,
),
(
GuardianWeeklyROWQuarterly,
PlanDescription("GW Oct 18 - Quarterly - ROW"),
guardianWeeklyStartDateRules,
startDateRules,
Quarterly,
),
(
GuardianWeeklyROWAnnual,
PlanDescription("GW Oct 18 - Annual - ROW"),
guardianWeeklyStartDateRules,
startDateRules,
Annual,
),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,39 @@ import com.gu.newproduct.api.productcatalog._

import java.time.{DayOfWeek, LocalDate}

class HomeDeliveryPlans(
getStartDateFromFulfilmentFiles: (ProductType, List[DayOfWeek]) => LocalDate,
) {
class HomeDeliveryPlans(getStartDateFromFulfilmentFiles: (ProductType, List[DayOfWeek]) => LocalDate) {

import PaperDays._
private val HomeDeliverySubscriptionStartDateWindowSize = WindowSizeDays(28)

private def homeDeliveryWindowRule(issueDays: List[DayOfWeek]) = WindowRule(
private def windowRule(issueDays: List[DayOfWeek]) = WindowRule(
startDate = getStartDateFromFulfilmentFiles(ProductType.NewspaperHomeDelivery, issueDays),
maybeSize = Some(HomeDeliverySubscriptionStartDateWindowSize),
maybeSize = Some(WindowSizeDays(28)),
)

private def homeDeliveryDateRules(allowedDays: List[DayOfWeek]) = StartDateRules(
private def dateRules(allowedDays: List[DayOfWeek]) = StartDateRules(
Some(DaysOfWeekRule(allowedDays)),
homeDeliveryWindowRule(allowedDays),
windowRule(allowedDays),
)

private val homeDeliveryEveryDayRules = homeDeliveryDateRules(
private val everyDayRules = dateRules(
everyDayDays,
)

private val homeDeliverySixDayRules = homeDeliveryDateRules(sixDayDays)
private val homeDeliverySundayDateRules = homeDeliveryDateRules(sundayDays)
private val homeDeliverySaturdayDateRules = homeDeliveryDateRules(saturdayDays)
private val homeDeliveryWeekendRules = homeDeliveryDateRules(weekendDays)
private val sixDayRules = dateRules(sixDayDays)
private val sundayDateRules = dateRules(sundayDays)
private val saturdayDateRules = dateRules(saturdayDays)
private val weekendRules = dateRules(weekendDays)

val planInfo: List[(PlanId, PlanDescription, StartDateRules, BillingPeriod)] = List(
(HomeDeliveryEveryDay, PlanDescription("Everyday"), homeDeliveryEveryDayRules, Monthly),
(HomeDeliverySixDay, PlanDescription("Sixday"), homeDeliverySixDayRules, Monthly),
(HomeDeliveryWeekend, PlanDescription("Weekend"), homeDeliveryWeekendRules, Monthly),
(HomeDeliverySunday, PlanDescription("Sunday"), homeDeliverySundayDateRules, Monthly),
(HomeDeliverySaturday, PlanDescription("Saturday"), homeDeliverySaturdayDateRules, Monthly),
(HomeDeliveryEveryDayPlus, PlanDescription("Everyday+"), homeDeliveryEveryDayRules, Monthly),
(HomeDeliverySixDayPlus, PlanDescription("Sixday+"), homeDeliverySixDayRules, Monthly),
(HomeDeliveryWeekendPlus, PlanDescription("Weekend+"), homeDeliveryWeekendRules, Monthly),
(HomeDeliverySundayPlus, PlanDescription("Sunday+"), homeDeliverySundayDateRules, Monthly),
(HomeDeliverySaturdayPlus, PlanDescription("Saturday+"), homeDeliverySaturdayDateRules, Monthly),
(HomeDeliveryEveryDay, PlanDescription("Everyday"), everyDayRules, Monthly),
(HomeDeliverySixDay, PlanDescription("Sixday"), sixDayRules, Monthly),
(HomeDeliveryWeekend, PlanDescription("Weekend"), weekendRules, Monthly),
(HomeDeliverySunday, PlanDescription("Sunday"), sundayDateRules, Monthly),
(HomeDeliverySaturday, PlanDescription("Saturday"), saturdayDateRules, Monthly),
(HomeDeliveryEveryDayPlus, PlanDescription("Everyday+"), everyDayRules, Monthly),
(HomeDeliverySixDayPlus, PlanDescription("Sixday+"), sixDayRules, Monthly),
(HomeDeliveryWeekendPlus, PlanDescription("Weekend+"), weekendRules, Monthly),
(HomeDeliverySundayPlus, PlanDescription("Sunday+"), sundayDateRules, Monthly),
(HomeDeliverySaturdayPlus, PlanDescription("Saturday+"), saturdayDateRules, Monthly),
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import com.gu.newproduct.api.productcatalog._

import java.time.{DayOfWeek, LocalDate}

class NationalDeliveryPlans(
getStartDateFromFulfilmentFiles: (ProductType, List[DayOfWeek]) => LocalDate,
) {
class NationalDeliveryPlans(getStartDateFromFulfilmentFiles: (ProductType, List[DayOfWeek]) => LocalDate) {

import PaperDays._
private val SubscriptionStartDateWindowSize = WindowSizeDays(28)
Expand Down
Loading

0 comments on commit 4c38af8

Please sign in to comment.