private def getSupporterPlusData()

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


  private def getSupporterPlusData(
      subscription: GetSubscriptionResponse,
      ids: SupporterPlusZuoraIds,
  ): Task[SupporterPlusCharges] =
    for {
      ratePlan <- asSingle(subscription.ratePlans.filterNot(_.lastChangeType.contains("Remove")), "ratePlan")
      charges <- asNonEmptyList(ratePlan.ratePlanCharges, "ratePlanCharge")
      chargesById <- {
        val (errors, chargesById) = charges.groupBy(_.productRatePlanChargeId).partitionMap {
          case (id, NonEmptyList(charge, Nil)) => Right((id, charge))
          case multiple => Left(multiple.toString)
        }
        if (errors.nonEmpty)
          ZIO.fail(new Throwable("subscription had duplicate charges")).logError(s"errors: $errors")
        else ZIO.succeed(chargesById.toMap)
      }
      chargeIdsOfInterest = {
        import ids.*
        List(
          annual.productRatePlanChargeId,
          monthly.productRatePlanChargeId,
          annualV2.productRatePlanChargeId,
          monthlyV2.productRatePlanChargeId,
          annualV2.contributionProductRatePlanChargeId,
          monthlyV2.contributionProductRatePlanChargeId,
        ).map(_.value)
      }
      supporterPlusCharges <- chargeIdsOfInterest.map(chargesById.get) match {
        case List(Some(annual), None, None, None, None, None) =>
          ZIO.succeed(SupporterPlusCharges(ratePlan.id, annual, true, BigDecimal(0)))
        case List(None, Some(monthly), None, None, None, None) =>
          ZIO.succeed(SupporterPlusCharges(ratePlan.id, monthly, false, BigDecimal(0)))
        case List(None, None, Some(annual), None, Some(contr), None) if annual.price.isDefined =>
          ZIO.succeed(SupporterPlusCharges(ratePlan.id, contr, true, BigDecimal(annual.price.get)))
        case List(None, None, None, Some(monthly), None, Some(contr)) if monthly.price.isDefined =>
          ZIO.succeed(SupporterPlusCharges(ratePlan.id, contr, false, BigDecimal(monthly.price.get)))
        case other => ZIO.fail(new Throwable("subscription was not in valid state")).logError(s"charges: $other")
      }

    } yield supporterPlusCharges