in lambda/src/main/scala/pricemigrationengine/handlers/AmendmentHandler.scala [106:289]
private def doAmendment(
cohortSpec: CohortSpec,
catalogue: ZuoraProductCatalogue,
item: CohortItem
): ZIO[Zuora with Logging, Failure, SuccessfulAmendmentResult] = {
MigrationType(cohortSpec) match {
case SupporterPlus2024 => {
for {
subscriptionBeforeUpdate <- fetchSubscription(item)
startDate <- ZIO.fromOption(item.startDate).orElseFail(DataExtractionFailure(s"No start date in $item"))
oldPrice <- ZIO.fromOption(item.oldPrice).orElseFail(DataExtractionFailure(s"No old price in $item"))
estimatedNewPrice <-
ZIO
.fromOption(item.estimatedNewPrice)
.orElseFail(DataExtractionFailure(s"No estimated new price in $item"))
invoicePreviewTargetDate = startDate.plusMonths(13)
account <- Zuora.fetchAccount(
subscriptionBeforeUpdate.accountNumber,
subscriptionBeforeUpdate.subscriptionNumber
)
_ <- renewSubscription(subscriptionBeforeUpdate, subscriptionBeforeUpdate.termEndDate, account)
order <- ZIO.fromEither(
SupporterPlus2024Migration.amendmentOrderPayload(
orderDate = LocalDate.now(),
accountNumber = account.basicInfo.accountNumber,
subscriptionNumber = subscriptionBeforeUpdate.subscriptionNumber,
effectDate = startDate,
subscription = subscriptionBeforeUpdate,
oldPrice = oldPrice,
estimatedNewPrice = estimatedNewPrice,
priceCap = SupporterPlus2024Migration.priceCap
)
)
_ <- Logging.info(
s"Amending subscription ${subscriptionBeforeUpdate.subscriptionNumber} with order ${order}"
)
_ <- Zuora.applyAmendmentOrder(subscriptionBeforeUpdate, order)
subscriptionAfterUpdate <- fetchSubscription(item)
invoicePreviewAfterUpdate <-
Zuora.fetchInvoicePreview(subscriptionAfterUpdate.accountId, invoicePreviewTargetDate)
newPrice <-
ZIO.fromEither(
AmendmentData.totalChargeAmount(
subscriptionAfterUpdate,
invoicePreviewAfterUpdate,
startDate
)
)
whenDone <- Clock.instant
} yield SuccessfulAmendmentResult(
item.subscriptionName,
startDate,
oldPrice,
newPrice,
estimatedNewPrice,
subscriptionAfterUpdate.id,
whenDone
)
}
case _ => {
for {
subscriptionBeforeUpdate <- fetchSubscription(item)
startDate <- ZIO.fromOption(item.startDate).orElseFail(DataExtractionFailure(s"No start date in $item"))
oldPrice <- ZIO.fromOption(item.oldPrice).orElseFail(DataExtractionFailure(s"No old price in $item"))
estimatedNewPrice <-
ZIO
.fromOption(item.estimatedNewPrice)
.orElseFail(DataExtractionFailure(s"No estimated new price in $item"))
invoicePreviewTargetDate = startDate.plusMonths(13)
account <- Zuora.fetchAccount(
subscriptionBeforeUpdate.accountNumber,
subscriptionBeforeUpdate.subscriptionNumber
)
_ <- renewSubscription(subscriptionBeforeUpdate, subscriptionBeforeUpdate.termEndDate, account)
invoicePreviewBeforeUpdate <-
Zuora.fetchInvoicePreview(subscriptionBeforeUpdate.accountId, invoicePreviewTargetDate)
update <- MigrationType(cohortSpec) match {
case Newspaper2024 =>
ZIO.fromEither(
newspaper2024Migration.Amendment.zuoraUpdate(
subscriptionBeforeUpdate,
startDate,
)
)
case GW2024 =>
ZIO.fromEither(
GW2024Migration.zuoraUpdate(
subscriptionBeforeUpdate,
startDate,
oldPrice,
estimatedNewPrice,
GW2024Migration.priceCap
)
)
case SupporterPlus2024 =>
ZIO.fromEither(
SupporterPlus2024Migration.zuoraUpdate(
subscriptionBeforeUpdate,
startDate,
oldPrice,
estimatedNewPrice,
SupporterPlus2024Migration.priceCap
)
)
case Default =>
ZIO.fromEither(
ZuoraSubscriptionUpdate
.zuoraUpdate(
account,
catalogue,
subscriptionBeforeUpdate,
invoicePreviewBeforeUpdate,
startDate,
Some(PriceCap.priceCapLegacy(oldPrice, estimatedNewPrice))
)
)
}
_ <- Logging.info(
s"Amending subscription ${subscriptionBeforeUpdate.subscriptionNumber} with update ${update}"
)
newSubscriptionId <- Zuora.updateSubscription(subscriptionBeforeUpdate, update)
subscriptionAfterUpdate <- fetchSubscription(item)
invoicePreviewAfterUpdate <-
Zuora.fetchInvoicePreview(subscriptionAfterUpdate.accountId, invoicePreviewTargetDate)
newPrice <-
ZIO.fromEither(
AmendmentData.totalChargeAmount(
subscriptionAfterUpdate,
invoicePreviewAfterUpdate,
startDate
)
)
_ <-
if (shouldPerformFinalPriceCheck(cohortSpec: CohortSpec) && (newPrice > estimatedNewPrice)) {
ZIO.fail(
DataExtractionFailure(
s"[e9054daa] Item ${item} has gone through the amendment step but has failed the final price check. Estimated price was ${estimatedNewPrice}, but the final price was ${newPrice}"
)
)
} else {
ZIO.succeed(())
}
whenDone <- Clock.instant
} yield SuccessfulAmendmentResult(
item.subscriptionName,
startDate,
oldPrice,
newPrice,
estimatedNewPrice,
subscriptionAfterUpdate.id,
whenDone
)
}
}
}