diff --git a/handlers/product-move-api/src/main/scala/com/gu/productmove/switchtype/RecurringContributionToSupporterPlus.scala b/handlers/product-move-api/src/main/scala/com/gu/productmove/switchtype/RecurringContributionToSupporterPlus.scala index a5ec269a61..4f88786194 100644 --- a/handlers/product-move-api/src/main/scala/com/gu/productmove/switchtype/RecurringContributionToSupporterPlus.scala +++ b/handlers/product-move-api/src/main/scala/com/gu/productmove/switchtype/RecurringContributionToSupporterPlus.scala @@ -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 diff --git a/handlers/product-move-api/src/main/scala/com/gu/productmove/zuora/TermRenewal.scala b/handlers/product-move-api/src/main/scala/com/gu/productmove/zuora/TermRenewal.scala index 5218e6a623..81f7bf944a 100644 --- a/handlers/product-move-api/src/main/scala/com/gu/productmove/zuora/TermRenewal.scala +++ b/handlers/product-move-api/src/main/scala/com/gu/productmove/zuora/TermRenewal.scala @@ -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} @@ -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(_)) @@ -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( @@ -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, @@ -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( diff --git a/handlers/product-move-api/src/main/scala/com/gu/productmove/zuora/rest/ZuoraClient.scala b/handlers/product-move-api/src/main/scala/com/gu/productmove/zuora/rest/ZuoraClient.scala index f8f7989b59..e9e2234342 100644 --- a/handlers/product-move-api/src/main/scala/com/gu/productmove/zuora/rest/ZuoraClient.scala +++ b/handlers/product-move-api/src/main/scala/com/gu/productmove/zuora/rest/ZuoraClient.scala @@ -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, @@ -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 diff --git a/handlers/product-move-api/src/test/scala/com/gu/productmove/zuora/MockTermRenewal.scala b/handlers/product-move-api/src/test/scala/com/gu/productmove/zuora/MockTermRenewal.scala index 67ba50a29f..b50db70979 100644 --- a/handlers/product-move-api/src/test/scala/com/gu/productmove/zuora/MockTermRenewal.scala +++ b/handlers/product-move-api/src/test/scala/com/gu/productmove/zuora/MockTermRenewal.scala @@ -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(()) } } diff --git a/handlers/product-move-api/src/test/scala/com/gu/productmove/zuora/TermRenewalSpec.scala b/handlers/product-move-api/src/test/scala/com/gu/productmove/zuora/TermRenewalSpec.scala index 5731089a6e..8bb155ad28 100644 --- a/handlers/product-move-api/src/test/scala/com/gu/productmove/zuora/TermRenewalSpec.scala +++ b/handlers/product-move-api/src/test/scala/com/gu/productmove/zuora/TermRenewalSpec.scala @@ -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,