def build()

in support-workers/src/main/scala/com/gu/zuora/subscriptionBuilders/GuardianWeeklySubscriptionBuilder.scala [21:84]


  def build(
      state: GuardianWeeklyState,
      csrUsername: Option[String],
      salesforceCaseId: Option[String],
  ): Either[PromoError, SubscribeItem] = {
    val contractEffectiveDate = dateGenerator.today
    val readerType = if (state.giftRecipient.isDefined) ReaderType.Gift else ReaderType.Direct
    val recurringProductRatePlanId =
      validateRatePlan(weeklyRatePlan(state.product, environment, readerType), state.product.describe)

    val (initialTerm, autoRenew, initialTermPeriodType) =
      if (readerType == Gift)
        (
          initialTermInDays(contractEffectiveDate, state.firstDeliveryDate, state.product.billingPeriod.monthsInPeriod),
          false,
          Day,
        )
      else
        (12, true, Month)

    val subscriptionData = subscribeItemBuilder.buildProductSubscription(
      recurringProductRatePlanId,
      contractAcceptanceDate = state.firstDeliveryDate,
      contractEffectiveDate = contractEffectiveDate,
      readerType = ReaderType.impliedBySomeAppliedPromotion(state.appliedPromotion) getOrElse readerType,
      autoRenew = autoRenew,
      initialTerm = initialTerm,
      initialTermPeriodType = initialTermPeriodType,
      csrUsername = csrUsername,
      salesforceCaseId = salesforceCaseId,
    )

    applyPromoCodeIfPresent(
      promotionService,
      state.appliedPromotion,
      recurringProductRatePlanId,
      subscriptionData,
    ).map { subscriptionData =>
      val soldToContact = state.giftRecipient match {
        case None =>
          SubscribeItemBuilder.buildContactDetails(
            Some(state.user.primaryEmailAddress),
            state.user.firstName,
            state.user.lastName,
            state.user.deliveryAddress.get,
            None,
          )
        case Some(gR) =>
          SubscribeItemBuilder.buildContactDetails(
            gR.email,
            gR.firstName,
            gR.lastName,
            state.user.deliveryAddress.get,
            None,
          )
      }
      subscribeItemBuilder.build(
        subscriptionData,
        state.salesforceContacts.recipient,
        Some(state.paymentMethod),
        Some(soldToContact),
      )
    }
  }