def sendNotification()

in lambda/src/main/scala/pricemigrationengine/handlers/NotificationHandler.scala [124:219]


  def sendNotification(
      cohortSpec: CohortSpec,
      cohortItem: CohortItem,
      sfSubscription: SalesforceSubscription
  ): ZIO[Zuora with EmailSender with SalesforceClient with CohortTable with Logging, Failure, Unit] =
    for {
      _ <- Logging.info(s"Processing subscription: ${cohortItem.subscriptionName}")
      contact <- SalesforceClient.getContact(sfSubscription.Buyer__c)
      firstName <- ZIO.fromEither(firstName(contact))
      lastName <- ZIO.fromEither(requiredField(contact.LastName, "Contact.LastName"))
      address <- ZIO.fromEither(targetAddress(cohortSpec, contact))
      street <- ZIO.fromEither(requiredField(address.street, "Contact.OtherAddress.street"))
      postalCode = address.postalCode.getOrElse("")
      country <- ZIO.fromEither(country(cohortSpec, address))
      oldPrice <- ZIO.fromEither(requiredField(cohortItem.oldPrice, "CohortItem.oldPrice"))
      estimatedNewPrice <- ZIO.fromEither(requiredField(cohortItem.estimatedNewPrice, "CohortItem.estimatedNewPrice"))
      startDate <- ZIO.fromEither(requiredField(cohortItem.startDate.map(_.toString()), "CohortItem.startDate"))
      billingPeriod <- ZIO.fromEither(requiredField(cohortItem.billingPeriod, "CohortItem.billingPeriod"))
      paymentFrequency <- paymentFrequency(billingPeriod)
      currencyISOCode <- ZIO.fromEither(requiredField(cohortItem.currency, "CohortItem.currency"))
      currencySymbol <- currencyISOtoSymbol(currencyISOCode)

      priceWithOptionalCappingWithCurrencySymbol = MigrationType(cohortSpec) match {
        case Default       => s"${currencySymbol}${PriceCap.priceCapLegacy(oldPrice, estimatedNewPrice)}"
        case Newspaper2024 => s"${currencySymbol}${estimatedNewPrice}"
        case GW2024 =>
          s"${currencySymbol}${PriceCap.priceCapForNotification(oldPrice, estimatedNewPrice, GW2024Migration.priceCap)}"
        case SupporterPlus2024 => s"${currencySymbol}${estimatedNewPrice}"
      }

      _ <- logMissingEmailAddress(cohortItem, contact)

      // ----------------------------------------------------
      // Data for SupporterPlus2024

      supporterPlus2024NotificationData <- buildSupporterPlus2024NotificationData(
        cohortSpec,
        cohortItem.subscriptionName
      )

      sp2024ContributionAmountWithCurrencySymbol = supporterPlus2024NotificationData.contributionAmount
        .map(a => s"${currencySymbol}${a.toString()}")
      sp2024PreviousCombinedAmountWithCurrencySymbol = supporterPlus2024NotificationData.previousCombinedAmount
        .map(a => s"${currencySymbol}${a.toString()}")
      sp2024NewCombinedAmountWithCurrencySymbol = supporterPlus2024NotificationData.newCombinedAmount
        .map(a => s"${currencySymbol}${a.toString()}")
      // ----------------------------------------------------

      brazeName <- brazeName(cohortSpec, cohortItem.subscriptionName)

      _ <- EmailSender.sendEmail(
        message = EmailMessage(
          EmailPayload(
            Address = contact.Email,
            ContactAttributes = EmailPayloadContactAttributes(
              SubscriberAttributes = EmailPayloadSubscriberAttributes(
                title = contact.FirstName flatMap (_ =>
                  contact.Salutation // if no first name, we use salutation as first name and leave this field empty
                ),
                first_name = firstName,
                last_name = lastName,
                billing_address_1 = street,
                billing_address_2 = None, // See 'Billing Address Format' section in the readme
                billing_city = address.city,
                billing_postal_code = postalCode,
                billing_state = address.state,
                billing_country = country,
                payment_amount = priceWithOptionalCappingWithCurrencySymbol, // [1]
                next_payment_date = startDateConversion(startDate),
                payment_frequency = paymentFrequency,
                subscription_id = cohortItem.subscriptionName,
                product_type = sfSubscription.Product_Type__c.getOrElse(""),

                // -------------------------------------------------------------
                // [1]
                // (Comment group: 7992fa98)
                // For SupporterPlus2024, we did not use that value. Instead we used the data provided by the
                // extension below. That value was the new base price, but we needed a different data distribution
                // to be able to fill the email template. That distribution is given by the next section.

                // SupporterPlus 2024 extension
                sp2024_contribution_amount = sp2024ContributionAmountWithCurrencySymbol,
                sp2024_previous_combined_amount = sp2024PreviousCombinedAmountWithCurrencySymbol,
                sp2024_new_combined_amount = sp2024NewCombinedAmountWithCurrencySymbol
                // -------------------------------------------------------------
              )
            )
          ),
          brazeName,
          contact.Id,
          contact.IdentityID__c
        )
      )

      _ <- updateCohortItemStatus(cohortItem.subscriptionName, NotificationSendComplete)
    } yield ()