def getNewRatePlanCharges()

in lambda/src/main/scala/pricemigrationengine/migrations/GuardianWeeklyMigration.scala [27:99]


  def getNewRatePlanCharges(
      account: ZuoraAccount,
      catalogue: ZuoraProductCatalogue,
      ratePlanCharges: Seq[ZuoraRatePlanCharge]
  ): Either[DataExtractionFailure, GuardianWeeklyMigration] = {
    val billingPeriod = ratePlanCharges.head.billingPeriod match {
      case Some(value) =>
        value match {
          case "Quarter" => "Quarterly"
          case "Month"   => "Monthly"
          case "Annual"  => "Annual"
          case default   => Left(DataExtractionFailure(s"billingPeriod is $default for ratePlan"))
        }

      case None => Left(DataExtractionFailure("billingPeriod is null for ratePlan"))
    }

    val guardianWeeklyRatePlans =
      newGuardianWeeklyRatePlans(catalogue)
    val deliveryCountry = account.soldToContact.country

    def fetchPlan(
        currentCharges: Seq[ZuoraRatePlanCharge],
        ratePlanName: String
    ): Either[DataExtractionFailure, GuardianWeeklyMigration] = {
      val newRatePlan = guardianWeeklyRatePlans
        .find(_.name == ratePlanName)
        .find(_.productRatePlanCharges.head.billingPeriod == currentCharges.head.billingPeriod)

      newRatePlan match {
        case Some(plan) =>
          val chargePairs =
            for ((chargeFromSub, catalogueCharge) <- currentCharges zip plan.productRatePlanCharges)
              yield RatePlanChargePair(chargeFromSub, catalogueCharge)

          Right(GuardianWeeklyMigration(plan, chargePairs))
        case None =>
          Left(
            DataExtractionFailure(
              s"Failed to find new rate plan for Guardian Weekly sub: $ratePlanName, ratePlanCharges: ${ratePlanCharges.mkString(", ")}"
            )
          )
      }
    }

    /*
      if USD {
        if deliveryAddress == 'United States' then 'Domestic' else 'ROW'
      } else {
        'Domestic'
      }
     */

    def aaaa(
        currency: Currency,
        ratePlanCharges: Seq[ZuoraRatePlanCharge]
    ): Either[DataExtractionFailure, GuardianWeeklyMigration] = {
      if (ratePlanCharges.head.currency == "USD") {
        for {
          ratePlan <-
            if (deliveryCountry == "United States" || deliveryCountry == "USA")
              fetchPlan(ratePlanCharges, s"GW Oct 18 - $billingPeriod - Domestic")
            else fetchPlan(ratePlanCharges, s"GW Oct 18 - $billingPeriod - ROW")
        } yield ratePlan
      } else {
        fetchPlan(ratePlanCharges, s"GW Oct 18 - $billingPeriod - Domestic")
      }
    }

    for {
      ratePlanChargePairs <- aaaa(ratePlanCharges.head.currency, ratePlanCharges)
    } yield ratePlanChargePairs
  }