Skip to content

Refactor models #6889

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,109 +1,30 @@
package com.gu.support.workers.states

import com.gu.i18n.Country
import com.gu.salesforce.Salesforce.SalesforceContactRecords
import com.gu.support.acquisitions.AcquisitionData
import com.gu.support.encoding.Codec
import com.gu.support.encoding.CustomCodecs._
import com.gu.support.encoding.Codec.deriveCodec
import com.gu.support.encoding.{Codec, DiscriminatedType}
import com.gu.support.workers.{PaymentMethod, SalesforceContactRecord, User, _}
import org.joda.time.LocalDate
import com.gu.support.encoding.CustomCodecs.{decodeCountry, decodeLocalTime, encodeCountryAsAlpha2, encodeLocalTime}
import com.gu.support.encoding.{Codec, DiscriminatedType}
import com.gu.support.workers._
import org.joda.time.LocalDate

import java.util.UUID

case class CreateZuoraSubscriptionState(
productSpecificState: CreateZuoraSubscriptionProductState,
requestId: UUID,
user: User,
giftRecipient: Option[GiftRecipient],
product: ProductType,
paymentMethod: PaymentMethod,
analyticsInfo: AnalyticsInfo,
firstDeliveryDate: Option[LocalDate],
appliedPromotion: Option[AppliedPromotion],
csrUsername: Option[String],
salesforceCaseId: Option[String],
acquisitionData: Option[AcquisitionData],
salesforceContacts: SalesforceContactRecords,
) extends FailureHandlerState

object CreateZuoraSubscriptionState {
implicit val codec: Codec[CreateZuoraSubscriptionState] = deriveCodec[CreateZuoraSubscriptionState]
}

sealed trait CreateZuoraSubscriptionProductState

object CreateZuoraSubscriptionProductState {

case class ContributionState(
product: Contribution,
paymentMethod: PaymentMethod,
salesForceContact: SalesforceContactRecord,
) extends CreateZuoraSubscriptionProductState

case class SupporterPlusState(
billingCountry: Country,
product: SupporterPlus,
paymentMethod: PaymentMethod,
appliedPromotion: Option[AppliedPromotion],
salesForceContact: SalesforceContactRecord,
) extends CreateZuoraSubscriptionProductState

case class TierThreeState(
user: User,
product: TierThree,
paymentMethod: PaymentMethod,
firstDeliveryDate: LocalDate,
appliedPromotion: Option[AppliedPromotion],
salesForceContact: SalesforceContactRecord,
) extends CreateZuoraSubscriptionProductState

case class GuardianAdLiteState(
product: GuardianAdLite,
paymentMethod: PaymentMethod,
salesForceContact: SalesforceContactRecord,
) extends CreateZuoraSubscriptionProductState

case class DigitalSubscriptionState(
billingCountry: Country,
product: DigitalPack,
paymentMethod: PaymentMethod,
appliedPromotion: Option[AppliedPromotion],
salesForceContact: SalesforceContactRecord,
) extends CreateZuoraSubscriptionProductState

case class PaperState(
user: User,
product: Paper,
paymentMethod: PaymentMethod,
firstDeliveryDate: LocalDate,
appliedPromotion: Option[AppliedPromotion],
salesForceContact: SalesforceContactRecord,
) extends CreateZuoraSubscriptionProductState

case class GuardianWeeklyState(
user: User,
giftRecipient: Option[GiftRecipient],
product: GuardianWeekly,
paymentMethod: PaymentMethod,
firstDeliveryDate: LocalDate,
appliedPromotion: Option[AppliedPromotion],
salesforceContacts: SalesforceContactRecords,
) extends CreateZuoraSubscriptionProductState

import ExecutionTypeDiscriminators._

private val discriminatedType = new DiscriminatedType[CreateZuoraSubscriptionProductState](fieldName)
implicit val codec: Codec[CreateZuoraSubscriptionProductState] = discriminatedType.codec(
List(
discriminatedType.variant[ContributionState](contribution),
discriminatedType.variant[SupporterPlusState](supporterPlus),
discriminatedType.variant[DigitalSubscriptionState](digitalSubscription),
discriminatedType.variant[PaperState](paper),
discriminatedType.variant[GuardianWeeklyState](guardianWeekly),
discriminatedType.variant[TierThreeState](tierThree),
discriminatedType.variant[GuardianAdLiteState](guardianAdLite),
),
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -260,23 +260,31 @@ object Fixtures {
def createContributionZuoraSubscriptionJson(billingPeriod: BillingPeriod = Monthly): String =
s"""
{
"productType": "Contribution",
$requestIdJson,
$userJson,
"analyticsInfo": {
"paymentProvider": "PayPal",
"isGiftPurchase": false
},
"product": ${contribution(billingPeriod = billingPeriod)},
"paymentMethod": $stripePaymentMethod,
"salesForceContact": $salesforceContactJson
"salesforceContacts": $salesforceContactsJson
}
"""
def createDigiPackZuoraSubscriptionJson: String =
s"""
{
"productType": "DigitalSubscription",
$requestIdJson,
$userJson,
"billingCountry": "GB",
"product": $digitalPackJson,
"paymentMethod": $stripePaymentMethod,
"salesForceContact": $salesforceContactJson
"salesforceContacts": $salesforceContactsJson,
"analyticsInfo": {
"paymentProvider": "PayPal",
"isGiftPurchase": false
}
}
"""

val zuoraErrorResponse =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.gu.support.workers

import com.gu.i18n.{Country, Currency}
import com.gu.salesforce.Salesforce.SalesforceContactRecords
import com.gu.support.SerialisationTestHelpers
import com.gu.support.catalog.RestOfWorld
import com.gu.support.workers.Fixtures._
import com.gu.support.workers.states.CreateZuoraSubscriptionProductState.DigitalSubscriptionState
import com.gu.support.workers.states._
import com.typesafe.scalalogging.LazyLogging
import org.scalatest.EitherValues
Expand Down Expand Up @@ -37,10 +37,10 @@ class SerialisationSpec extends AnyFlatSpec with SerialisationTestHelpers with L
testDecoding[CreateSalesforceContactState](createSalesforceContactJson)
}

"CreateZuoraSubscription" should "deserialise correctly" in {
testDecoding[CreateZuoraSubscriptionProductState](createContributionZuoraSubscriptionJson())
testDecoding[CreateZuoraSubscriptionProductState](createContributionZuoraSubscriptionJson(Annual))
testDecoding[CreateZuoraSubscriptionProductState](createDigiPackZuoraSubscriptionJson)
"CreateZuoraSubscriptionState" should "deserialise correctly" in {
testDecoding[CreateZuoraSubscriptionState](createContributionZuoraSubscriptionJson())
testDecoding[CreateZuoraSubscriptionState](createContributionZuoraSubscriptionJson(Annual))
testDecoding[CreateZuoraSubscriptionState](createDigiPackZuoraSubscriptionJson)
}

"FailureHandlerState" should "deserialise correctly from any lambda" in {
Expand Down Expand Up @@ -95,22 +95,18 @@ object StatesTestData {
)

val createZuoraSubscriptionState: CreateZuoraSubscriptionState = CreateZuoraSubscriptionState(
DigitalSubscriptionState(
Country.UK,
product = DigitalPack(Currency.GBP, Monthly),
paymentMethod = PayPalReferenceTransaction("baid", "[email protected]"),
appliedPromotion = None,
salesForceContact = SalesforceContactRecord("sfbuy", "sfbuyacid"),
),
UUID.fromString("f7651338-5d94-4f57-85fd-262030de9ad5"),
User("111222", "[email protected]", None, "bertha", "smith", Address(None, None, None, None, None, Country.UK)),
DigitalPack(Currency.GBP, Monthly),
AnalyticsInfo(false, StripeApplePay),
None,
None,
requestId = UUID.fromString("f7651338-5d94-4f57-85fd-262030de9ad5"),
user = User("111222", "[email protected]", None, "bertha", "smith", Address(None, None, None, None, None, Country.UK)),
giftRecipient = None,
product = DigitalPack(Currency.GBP, Monthly),
analyticsInfo = AnalyticsInfo(false, StripeApplePay),
paymentMethod = PayPalReferenceTransaction("baid", "[email protected]"),
firstDeliveryDate = None,
appliedPromotion = None,
csrUsername = None,
salesforceCaseId = None,
None,
acquisitionData = None,
salesforceContacts = SalesforceContactRecords(SalesforceContactRecord("sfbuy", "sfbuyacid"), None),
)

val thankYouEmailProductTypeState: SendThankYouEmailState =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.gu.salesforce.Salesforce.SalesforceErrorResponse
import com.gu.stripe.StripeError
import com.gu.support.workers.lambdas.StateNotValidException
import com.gu.support.zuora.api.response.ZuoraErrorResponse
import com.gu.zuora.productHandlers.BuildSubscribePromoError
import com.gu.zuora.productHandlers.BuildSubscribeError
import io.circe.syntax._
import io.circe.{DecodingFailure, ParsingFailure}

Expand All @@ -31,7 +31,7 @@ object ErrorHandler extends SafeLogging {
case e: PayPalError => e.asRetryException
case e: ZuoraErrorResponse => e.asRetryException
case e: SalesforceErrorResponse => e.asRetryException
case e: BuildSubscribePromoError => new RetryNone(e.cause.msg, cause = e)
case e: BuildSubscribeError => new RetryNone(e.cause, cause = e)
case e: StateNotValidException => new RetryNone(e.message, cause = e)
case e: BadRequestException => new RetryNone(e.getMessage, cause = e)
case wshe: WebServiceHelperError[_] if wshe.cause.isInstanceOf[DecodingFailure] =>
Expand Down
Loading