def forStage()

in handlers/delivery-problem-credit-processor/src/main/scala/com/gu/deliveryproblemcreditprocessor/DeliveryCreditProduct.scala [61:87]


  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 match {
        case s"Guardian Weekly$_" => creditProductForStage.GuardianWeekly
        case s"Tier Three" => creditProductForStage.GuardianWeekly
        case "Newspaper Delivery" =>
          if (plan.ratePlanName == "Sunday")
            creditProductForStage.HomeDeliveryObserverOnly
          else
            creditProductForStage.HomeDelivery
        case "Newspaper - National Delivery" => creditProductForStage.NationalDelivery
      }
    }

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