Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/github_actions/actions/setup-java…
Browse files Browse the repository at this point in the history
…-3.12.0
  • Loading branch information
shtukas authored Jul 31, 2023
2 parents 81b1f52 + 8962bca commit ba30443
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = 3.7.10
version = 3.7.11
runner.dialect = scala213

maxColumn = 120
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import java.time.LocalDate
object AmendmentHandler extends CohortHandler {

// TODO: move to config
private val batchSize = 150
private val batchSize = 100

private def main(cohortSpec: CohortSpec): ZIO[Logging with CohortTable with Zuora, Failure, HandlerOutput] =
for {
Expand Down Expand Up @@ -59,14 +59,12 @@ object AmendmentHandler extends CohortHandler {
.filterOrFail(_.status != "Cancelled")(CancelledSubscriptionFailure(item.subscriptionName))

def checkExpirationTiming(
cohortSpec: CohortSpec,
item: CohortItem,
subscription: ZuoraSubscription
): Either[Failure, Unit] = {
// We check that the subscription's end of effective period is after the startDate, to avoid Zuora's error:
// ```
// The Contract effective date should not be later than the term end date of the basic subscription
// ```
// Note that this check will be duplicated in the estimation handler (this check in the Amendment handler came first)
// We check that the subscription's end of effective period is after the startDate, to avoid a Zuora's error
// Note that we do not make that check for digital products (notably: Membership2023Annuals and SupporterPlus2023V1V2MA)

item.startDate match {
case None =>
Expand All @@ -76,19 +74,37 @@ object AmendmentHandler extends CohortHandler {
)
) // This case won't really happen in practice, but item.startDate is an option
case Some(startDate) => {
if (subscription.termEndDate.isAfter(startDate)) {
Right(())
} else {
Left(
ExpiringSubscriptionFailure(
s"Cohort item: ${item.subscriptionName}. The item startDate (price increase date), ${item.startDate}, is after the subscription's end of effective period (${subscription.termEndDate.toString})"
)
)
MigrationType(cohortSpec) match {
case Membership2023Annuals =>
Right(())
case SupporterPlus2023V1V2MA =>
Right(())
case _ =>
if (subscription.termEndDate.isAfter(startDate)) {
Right(())
} else {
Left(
ExpiringSubscriptionFailure(
s"Cohort item: ${item.subscriptionName}. The item startDate (price increase date), ${item.startDate}, is after the subscription's end of effective period (${subscription.termEndDate.toString})"
)
)
}
}
}
}
}

private def renewSubscriptionIfNeeded(
subscription: ZuoraSubscription,
startDate: LocalDate
): ZIO[Zuora, Failure, Unit] = {
if (subscription.termEndDate.isBefore(startDate)) {
Zuora.renewSubscription(subscription.subscriptionNumber)
} else {
ZIO.succeed(())
}
}

private def doAmendment(
cohortSpec: CohortSpec,
catalogue: ZuoraProductCatalogue,
Expand All @@ -109,7 +125,9 @@ object AmendmentHandler extends CohortHandler {

subscriptionBeforeUpdate <- fetchSubscription(item)

_ <- ZIO.fromEither(checkExpirationTiming(item, subscriptionBeforeUpdate))
_ <- ZIO.fromEither(checkExpirationTiming(cohortSpec, item, subscriptionBeforeUpdate))

_ <- renewSubscriptionIfNeeded(subscriptionBeforeUpdate, startDate)

account <- Zuora.fetchAccount(subscriptionBeforeUpdate.accountNumber, subscriptionBeforeUpdate.subscriptionNumber)

Expand Down Expand Up @@ -188,19 +206,14 @@ object AmendmentHandler extends CohortHandler {
}

def handle(input: CohortSpec): ZIO[Logging, Failure, HandlerOutput] = {
MigrationType(input) match {
case Membership2023Annuals => ZIO.succeed(HandlerOutput(isComplete = true))
case SupporterPlus2023V1V2MA => ZIO.succeed(HandlerOutput(isComplete = true))
case _ =>
main(input).provideSome[Logging](
EnvConfig.cohortTable.layer,
EnvConfig.zuora.layer,
EnvConfig.stage.layer,
DynamoDBZIOLive.impl,
DynamoDBClientLive.impl,
CohortTableLive.impl(input),
ZuoraLive.impl
)
}
main(input).provideSome[Logging](
EnvConfig.cohortTable.layer,
EnvConfig.zuora.layer,
EnvConfig.stage.layer,
DynamoDBZIOLive.impl,
DynamoDBClientLive.impl,
CohortTableLive.impl(input),
ZuoraLive.impl
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ case class CohortUpdateFailure(reason: String) extends Failure
case class ZuoraFailure(reason: String) extends Failure
case class ZuoraFetchFailure(reason: String) extends Failure
case class ZuoraUpdateFailure(reason: String) extends Failure
case class ZuoraRenewalFailure(reason: String) extends Failure

case class AmendmentDataFailure(reason: String) extends Failure
case class CancelledSubscriptionFailure(reason: String) extends Failure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ trait Zuora {
subscription: ZuoraSubscription,
update: ZuoraSubscriptionUpdate
): ZIO[Any, ZuoraUpdateFailure, ZuoraSubscriptionId]

def renewSubscription(subscriptionNumber: String): ZIO[Any, ZuoraRenewalFailure, Unit]
}

object Zuora {
Expand All @@ -40,4 +42,7 @@ object Zuora {
update: ZuoraSubscriptionUpdate
): ZIO[Zuora, ZuoraUpdateFailure, ZuoraSubscriptionId] =
ZIO.environmentWithZIO(_.get.updateSubscription(subscription, update))

def renewSubscription(subscriptionNumber: String): ZIO[Zuora, ZuoraRenewalFailure, Unit] =
ZIO.environmentWithZIO(_.get.renewSubscription(subscriptionNumber))
}
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,18 @@ object ZuoraLive {
response => response.subscriptionId
)
) <* logging.info(s"Updated subscription ${subscription.subscriptionNumber} with: $update")

override def renewSubscription(subscriptionNumber: String): ZIO[Any, ZuoraRenewalFailure, Unit] =
retry(
put[Unit](
path = s"subscriptions/${subscriptionNumber}/renew",
body = "{}"
).mapBoth(
e => ZuoraRenewalFailure(s"Failed to renew subscription number ${subscriptionNumber}"),
response => ()
)
) <* logging.info(s"renewed subscription ${subscriptionNumber}")

}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -326,20 +326,23 @@ class AmendmentHandlerTest extends munit.FunSuite {
}

test("Check subscription's end date versus the cohort item's start (price increase) date") {
val cohortSpec =
CohortSpec("NAME", "Campaign1", LocalDate.of(2023, 7, 14), LocalDate.of(2023, 8, 21))

// Stage 1
val subscription1 = Fixtures.subscriptionFromJson("Membership2023/Batch1/GBP/subscription.json")
val item1 =
CohortItem("SUBSCRIPTION-NUMBER", NotificationSendDateWrittenToSalesforce, Some(LocalDate.of(2023, 4, 10)))
// subscription1.termEndDate is 2023-11-09
// item's startDate is LocalDate.of(2023, 4, 10)
// This is the good case
assertEquals(checkExpirationTiming(item1, subscription1), Right(()))
assertEquals(checkExpirationTiming(cohortSpec, item1, subscription1), Right(()))

// Stage 2
val subscription2 = Fixtures.subscriptionFromJson("Membership2023/Batch1/GBP/subscription.json")
val item2 = CohortItem("SUBSCRIPTION-NUMBER", NotificationSendDateWrittenToSalesforce, None)
// item's startDate is None, this triggers the AmendmentDataFailure
assertEquals(checkExpirationTiming(item2, subscription2).isLeft, true)
assertEquals(checkExpirationTiming(cohortSpec, item2, subscription2).isLeft, true)

// Stage 3
val subscription3 = Fixtures.subscriptionFromJson("Membership2023/Batch1/GBP/subscription.json")
Expand All @@ -348,7 +351,7 @@ class AmendmentHandlerTest extends munit.FunSuite {
// subscription3.termEndDate is 2023-11-09
// item's startDate is LocalDate.of(2024, 1, 1)
// This triggers the ExpiringSubscriptionFailure case
assertEquals(checkExpirationTiming(item3, subscription3).isLeft, true)
assertEquals(checkExpirationTiming(cohortSpec, item3, subscription3).isLeft, true)
}

test("SupporterPlus2023V1V2 Amendment (monthly standard)") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ object MockZuora extends Mock[Zuora] {
object UpdateSubscription
extends Effect[(ZuoraSubscription, ZuoraSubscriptionUpdate), ZuoraUpdateFailure, ZuoraSubscriptionId]

object RenewSubscription extends Effect[String, ZuoraRenewalFailure, Unit]

val compose: URLayer[Proxy, Zuora] = ZLayer.fromZIO(ZIO.service[Proxy].map { proxy =>
new Zuora {

Expand All @@ -35,6 +37,9 @@ object MockZuora extends Mock[Zuora] {
override def updateSubscription(subscription: ZuoraSubscription, update: ZuoraSubscriptionUpdate)
: ZIO[Any, ZuoraUpdateFailure, ZuoraSubscriptionId] =
proxy(UpdateSubscription, subscription, update)

override def renewSubscription(subscriptionNumber: String): ZIO[Any, ZuoraRenewalFailure, Unit] =
proxy(RenewSubscription, subscriptionNumber)
}
})
}

0 comments on commit ba30443

Please sign in to comment.