def forStage()

in handlers/holiday-stop-processor/src/main/scala/com/gu/holidaystopprocessor/HolidayCreditProduct.scala [114:143]


  def forStage(stage: Stage): CreditProductForSubscription = {

    val creditProductForStage = stages.getOrElse(stage, throw new RuntimeException(s"stage $stage does not exist"))

    def creditProduct: PartialFunction[RatePlan, CreditProduct] = { plan =>
      (plan.productName, plan.ratePlanName) match {
        case (s"Guardian Weekly$_", _) => creditProductForStage.GuardianWeekly
        // We need to match 'Tier Three' here because that is what the product is called in Zuora,
        // however the delivery product we are doing a credit for is Guardian Weekly
        case ("Tier Three", _) => creditProductForStage.GuardianWeekly
        case ("Newspaper Delivery", "Sunday") => creditProductForStage.HomeDeliveryObserverOnly
        case ("Newspaper Delivery", _) => creditProductForStage.HomeDelivery
        case ("Newspaper Voucher", "Sunday") => creditProductForStage.VoucherObserverOnly
        case ("Newspaper Voucher", _) => creditProductForStage.Voucher
        case ("Newspaper Digital Voucher", "Sunday") => creditProductForStage.DigitalVoucherObserverOnly
        case ("Newspaper Digital Voucher", _) => creditProductForStage.DigitalVoucher
        case ("Newspaper - National Delivery", _) => creditProductForStage.NationalDelivery
      }
    }

    subscription =>
      subscription.ratePlans
        .flatMap(creditProduct.lift)
        .headOption
        .getOrElse(
          throw new IllegalArgumentException(
            s"No holiday credit product available for subscription ${subscription.subscriptionNumber}",
          ),
        )
  }