Skip to content

Commit

Permalink
TermRenewal.startNewTermFromToday can return Unit
Browse files Browse the repository at this point in the history
  • Loading branch information
rupertbates committed Nov 6, 2023
1 parent 0e8ffd1 commit 008f52c
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ object RecurringContributionToSupporterPlus {
)

// Start a new term when we do the switch to avoid issues with billing dates
_ <- TermRenewal.startNewTermFromToday[RenewalResponse](subscriptionName)
_ <- TermRenewal.startNewTermFromToday(subscriptionName)

account <- accountFuture.join

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import com.gu.productmove.endpoint.move.ProductMoveEndpointTypes.{ErrorResponse,
import com.gu.productmove.zuora.GetSubscription.GetSubscriptionResponse
import com.gu.productmove.zuora.model.{SubscriptionId, SubscriptionName}
import com.gu.productmove.zuora.rest.ZuoraGet
import com.gu.productmove.zuora.rest.ZuoraRestBody.ZuoraSuccessCheck
import com.gu.productmove.zuora.rest.ZuoraRestBody.{ZuoraSuccessResultsArray, ZuoraSuccessCheck, ZuoraSuccessLowercase}
import com.gu.util.config
import sttp.capabilities.zio.ZioStreams
import sttp.capabilities.{Effect, WebSockets}
Expand All @@ -32,9 +32,9 @@ import java.time.LocalDate
import java.time.temporal.ChronoUnit

trait TermRenewal:
def startNewTermFromToday[R: JsonDecoder](
def startNewTermFromToday(
subscriptionName: SubscriptionName,
): ZIO[Stage with GetSubscription, ErrorResponse, R]
): ZIO[Stage with GetSubscription, ErrorResponse, Unit]

object TermRenewalLive:
val layer: URLayer[ZuoraGet, TermRenewal] = ZLayer.fromFunction(TermRenewalLive(_))
Expand All @@ -57,20 +57,20 @@ private class TermRenewalLive(zuoraGet: ZuoraGet) extends TermRenewal:
In order to balance these out, we can use the `applyCreditBalance` parameter in the renewal request.
*/

override def startNewTermFromToday[R: JsonDecoder](
override def startNewTermFromToday(
subscriptionName: SubscriptionName,
): ZIO[Stage with GetSubscription, ErrorResponse, R] = for {
): ZIO[Stage with GetSubscription, ErrorResponse, Unit] = for {
today <- Clock.currentDateTime.map(_.toLocalDate)
subscription <- GetSubscription.get(subscriptionName)
_ <- amendTermEndDateToToday(SubscriptionId(subscription.id), today, subscription.termStartDate)
response <- renewSubscription(subscriptionName)
} yield response
} yield ()

private def amendTermEndDateToToday[R: JsonDecoder](
private def amendTermEndDateToToday(
subscriptionId: SubscriptionId,
today: LocalDate,
termStartDate: LocalDate,
): ZIO[Stage, ErrorResponse, R] = {
): ZIO[Stage, ErrorResponse, Unit] = {
val newLength =
ChronoUnit.DAYS.between(termStartDate, today).toInt
val requestBody = AmendTermLengthRequest(
Expand All @@ -87,16 +87,18 @@ private class TermRenewalLive(zuoraGet: ZuoraGet) extends TermRenewal:
),
),
)
zuoraGet.put[AmendTermLengthRequest, R](
relativeUrl = uri"action/amend",
input = requestBody,
zuoraSuccessCheck = ZuoraSuccessCheck.SuccessCheckResultsArray,
)
zuoraGet
.put[AmendTermLengthRequest, ZuoraSuccessResultsArray](
relativeUrl = uri"action/amend",
input = requestBody,
zuoraSuccessCheck = ZuoraSuccessCheck.SuccessCheckResultsArray,
)
.unit
}

private def renewSubscription[R: JsonDecoder](
private def renewSubscription(
subscriptionName: SubscriptionName,
): ZIO[Stage, ErrorResponse, R] = {
): ZIO[Stage, ErrorResponse, Unit] = {
val today = LocalDate.now
val requestBody = RenewalRequest(
today,
Expand All @@ -105,18 +107,20 @@ private class TermRenewalLive(zuoraGet: ZuoraGet) extends TermRenewal:
applyCreditBalance = true,
runBilling = true,
)
zuoraGet.put[RenewalRequest, R](
relativeUrl = uri"subscriptions/${subscriptionName.value}/renew",
input = requestBody,
zuoraSuccessCheck = ZuoraSuccessCheck.SuccessCheckLowercase,
)
zuoraGet
.put[RenewalRequest, ZuoraSuccessLowercase](
relativeUrl = uri"subscriptions/${subscriptionName.value}/renew",
input = requestBody,
zuoraSuccessCheck = ZuoraSuccessCheck.SuccessCheckLowercase,
)
.unit
}

object TermRenewal {
def startNewTermFromToday[R: JsonDecoder](
def startNewTermFromToday(
subscriptionName: SubscriptionName,
): ZIO[TermRenewal with Stage with GetSubscription, ErrorResponse, R] =
ZIO.serviceWithZIO[TermRenewal](_.startNewTermFromToday[R](subscriptionName))
): ZIO[TermRenewal with Stage with GetSubscription, ErrorResponse, Unit] =
ZIO.serviceWithZIO[TermRenewal](_.startNewTermFromToday(subscriptionName))
}
case class AmendmentRequest(Amendments: List[Amendment])
case class Amendment(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,21 @@ object ZuoraRestBody {
message: String,
)
given JsonDecoder[Reason] = DeriveJsonDecoder.gen
given JsonDecoder[ZuoraResultsArrayItem] = DeriveJsonDecoder.gen

sealed trait ZuoraSuccessCheckResponse
case class ZuoraSuccessCapitalised(Success: Boolean, reasons: Option[List[Reason]]) extends ZuoraSuccessCheckResponse
case class ZuoraSuccessLowercase(success: Boolean, reasons: Option[List[Reason]]) extends ZuoraSuccessCheckResponse
case class ZuoraSuccessSize(size: Option[Int]) extends ZuoraSuccessCheckResponse
case class ZuoraResultsArray(results: List[ZuoraResultsArrayItem]) extends ZuoraSuccessCheckResponse
case class ZuoraSuccessCapitalised(Success: Boolean, reasons: Option[List[Reason]])
given JsonDecoder[ZuoraSuccessCapitalised] = DeriveJsonDecoder.gen
case class ZuoraSuccessLowercase(success: Boolean, reasons: Option[List[Reason]])
given JsonDecoder[ZuoraSuccessLowercase] = DeriveJsonDecoder.gen
case class ZuoraSuccessSize(size: Option[Int])
given JsonDecoder[ZuoraSuccessSize] = DeriveJsonDecoder.gen
case class ZuoraSuccessResultsArray(results: List[ZuoraResultsArrayItem])
given JsonDecoder[ZuoraSuccessResultsArray] = DeriveJsonDecoder.gen
case class ZuoraResultsArrayItem(Success: Boolean)
given JsonDecoder[ZuoraResultsArrayItem] = DeriveJsonDecoder.gen

def attemptDecode[A <: ZuoraSuccessCheckResponse](body: String) = DeriveJsonDecoder
.gen[A]
.decodeJson(body)
.left
.map(InternalServerError.apply)
def attemptDecode[A](body: String)(implicit decoder: JsonDecoder[A]) =
body.fromJson[A].left.map(InternalServerError.apply)

def parseIfSuccessful[A: JsonDecoder](
body: String,
Expand Down Expand Up @@ -153,7 +154,7 @@ object ZuoraRestBody {

case ZuoraSuccessCheck.SuccessCheckResultsArray =>
for {
zuoraResponse <- attemptDecode[ZuoraResultsArray](body)
zuoraResponse <- attemptDecode[ZuoraSuccessResultsArray](body)
isSuccessful <-
if (zuoraResponse.results.forall(_.Success)) Right(())
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ class MockTermRenewal(

def requests = mutableStore.reverse

override def startNewTermFromToday[R: JsonDecoder](
override def startNewTermFromToday(
subscriptionName: SubscriptionName,
): ZIO[Any, ErrorResponse, R] = {
): ZIO[Any, ErrorResponse, Unit] = {
mutableStore = (subscriptionName) :: mutableStore

ZIO.succeed(AmendmentResponse(List(AmendmentResult(Nil, true))).asInstanceOf[R])
ZIO.succeed(())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ object TermRenewalSpec extends ZIOSpecDefault {
test("Run startNewTermFromToday locally") {
for {
_ <- TermRenewal
.startNewTermFromToday[RenewalResponse](SubscriptionName("A-S00688596"))
.startNewTermFromToday(SubscriptionName("A-S00688596"))
.provide(
ZuoraClientLive.layer,
SttpClientLive.layer,
Expand Down

0 comments on commit 008f52c

Please sign in to comment.