def getSupporterRatePlanItemFromState()

in support-workers/src/main/scala/com/gu/support/workers/lambdas/UpdateSupporterProductData.scala [74:191]


  def getSupporterRatePlanItemFromState(
      state: SendThankYouEmailState,
      catalogService: CatalogService,
  ): Either[String, Option[SupporterRatePlanItem]] =
    state match {
      case SendThankYouEmailContributionState(user, product, _, _, subscriptionNumber, _) =>
        catalogService
          .getProductRatePlan(Contribution, product.billingPeriod, NoFulfilmentOptions, NoProductOptions)
          .map(productRatePlan =>
            Some(
              supporterRatePlanItem(
                subscriptionName = subscriptionNumber,
                identityId = user.id,
                productRatePlanId = productRatePlan.id,
                productRatePlanName = s"support-workers added ${product.describe}",
                Some(ContributionAmount(product.amount, product.currency.iso)),
              ),
            ),
          )
          .toRight(s"Unable to create SupporterRatePlanItem from state $state")

      case SendThankYouEmailSupporterPlusState(user, product, _, _, _, _, subscriptionNumber, _) =>
        catalogService
          .getProductRatePlan(SupporterPlus, product.billingPeriod, NoFulfilmentOptions, NoProductOptions)
          .map(productRatePlan =>
            Some(
              supporterRatePlanItem(
                subscriptionName = subscriptionNumber,
                identityId = user.id,
                productRatePlanId = productRatePlan.id,
                productRatePlanName = s"support-workers added ${product.describe}",
                None, // We don't send the amount for S+, because it may be discounted
              ),
            ),
          )
          .toRight(s"Unable to create SupporterRatePlanItem from state $state")

      case SendThankYouEmailTierThreeState(user, product, _, _, _, _, subscriptionNumber, _, _) =>
        catalogService
          .getProductRatePlan(TierThree, product.billingPeriod, product.fulfilmentOptions, product.productOptions)
          .map(productRatePlan =>
            Some(
              supporterRatePlanItem(
                subscriptionName = subscriptionNumber,
                identityId = user.id,
                productRatePlanId = productRatePlan.id,
                productRatePlanName = s"support-workers added ${product.describe}",
                None,
              ),
            ),
          )
          .toRight(s"Unable to create SupporterRatePlanItem from state $state")

      case SendThankYouEmailDigitalSubscriptionState(user, product, _, _, _, _, subscriptionNumber, _) =>
        catalogService
          .getProductRatePlan(DigitalPack, product.billingPeriod, NoFulfilmentOptions, NoProductOptions)
          .map(productRatePlan =>
            Some(
              supporterRatePlanItem(
                subscriptionName = subscriptionNumber,
                identityId = user.id,
                productRatePlanId = productRatePlan.id,
                productRatePlanName = s"support-workers added ${product.describe}",
              ),
            ),
          )
          .toRight(s"Unable to create SupporterRatePlanItem from state $state")
      case SendThankYouEmailGuardianWeeklyState(user, product, giftRecipient, _, _, _, _, subscriptionNumber, _, _) =>
        val readerType = if (giftRecipient.isDefined) Gift else Direct
        catalogService
          .getProductRatePlan(
            GuardianWeekly,
            product.billingPeriod,
            product.fulfilmentOptions,
            NoProductOptions,
            readerType,
          )
          .map(productRatePlan =>
            Some(
              supporterRatePlanItem(
                subscriptionName = subscriptionNumber,
                identityId = user.id,
                productRatePlanId = productRatePlan.id,
                productRatePlanName = s"support-workers added ${product.describe}-$readerType",
              ),
            ),
          )
          .toRight(s"Unable to create SupporterRatePlanItem from state $state")
      case SendThankYouEmailPaperState(user, product, _, _, _, _, subscriptionNumber, _, _) =>
        catalogService
          .getProductRatePlan(Paper, product.billingPeriod, product.fulfilmentOptions, product.productOptions)
          .map(productRatePlan =>
            Some(
              supporterRatePlanItem(
                subscriptionName = subscriptionNumber,
                identityId = user.id,
                productRatePlanId = productRatePlan.id,
                productRatePlanName = s"support-workers added ${product.describe}",
              ),
            ),
          )
          .toRight(s"Unable to create SupporterRatePlanItem from state $state")
      case SendThankYouEmailGuardianAdLiteState(user, product, _, _, _, subscriptionNumber, _) =>
        catalogService
          .getProductRatePlan(GuardianAdLite, product.billingPeriod, NoFulfilmentOptions, NoProductOptions)
          .map(productRatePlan =>
            Some(
              supporterRatePlanItem(
                subscriptionName = subscriptionNumber,
                identityId = user.id,
                productRatePlanId = productRatePlan.id,
                productRatePlanName = s"support-workers added ${product.describe}",
              ),
            ),
          )
          .toRight(s"Unable to create SupporterRatePlanItem from state $state")
      case _ => Left(s"Unknown product in state: $state")
    }