private[updateamount] def subscriptionUpdateAmount()

in handlers/product-move-api/src/main/scala/com/gu/productmove/endpoint/updateamount/UpdateSupporterPlusAmountSteps.scala [22:95]


  private[updateamount] def subscriptionUpdateAmount(subscriptionName: SubscriptionName, postData: ExpectedInput): RIO[
    GetSubscription with GetAccount with SubscriptionUpdate with SQS with Stage,
    OutputBody,
  ] = {
    val maybeResult: ZIO[
      SQS with SubscriptionUpdate with Stage with GetAccount with GetSubscription,
      OutputBody | Throwable,
      Success,
    ] = for {
      _ <- ZIO.log(s"Update Supporter Plus Amount - PostData: ${postData.toString}")

      subscription <- GetSubscription.get(subscriptionName)
      _ <- ZIO.log("Subscription: " + subscription)

      account <- GetAccount.get(subscription.accountNumber)
      currency <- account.basicInfo.currency.toI18nCurrency

      supporterPlusZuoraIds <- getSupporterPlusIds

      supporterPlusData <- getSupporterPlusData(subscription, supporterPlusZuoraIds)
      _ <- ZIO.log("supporter plus data: " + subscription)

      _ <- validateNewAmount(currency, postData.newPaymentAmount, supporterPlusData.isAnnual)

      newVariableChargeAmount = postData.newPaymentAmount - supporterPlusData.basePrice

      applyFromDate = supporterPlusData.contributionCharge.chargedThroughDate.getOrElse(
        supporterPlusData.contributionCharge.effectiveStartDate,
      )

      updateRequestBody = UpdateSubscriptionAmount(
        List(
          UpdateSubscriptionAmountItem(
            applyFromDate,
            applyFromDate,
            applyFromDate,
            supporterPlusData.ratePlanId,
            List(
              ChargeUpdateDetails(
                price = newVariableChargeAmount,
                ratePlanChargeId = supporterPlusData.contributionCharge.id,
              ),
            ),
          ),
        ),
      )

      _ <- SubscriptionUpdate.update[SubscriptionUpdateResponse](SubscriptionName(subscription.id), updateRequestBody)

      billingPeriod <- ZIO
        .fromOption(supporterPlusData.contributionCharge.billingPeriod)
        .orElseFail(
          new Throwable(s"Missing billing period for rate plan charge ${supporterPlusData.contributionCharge}"),
        )
      billingPeriodValue <- billingPeriod.value

      _ <- SQS
        .sendEmail(
          EmailMessage.updateAmountEmail(
            account,
            postData.newPaymentAmount,
            account.basicInfo.currency,
            billingPeriodValue,
            applyFromDate,
          ),
        )
    } yield Success(
      s"Successfully updated payment amount for Supporter Plus subscription ${subscriptionName.value} with amount ${postData.newPaymentAmount}",
    )
    maybeResult.catchAll {
      case failure: OutputBody => ZIO.succeed(failure)
      case other: Throwable => ZIO.fail(other)
    }
  }