Skip to content

Commit

Permalink
Move holiday-stop credit calculation (#392)
Browse files Browse the repository at this point in the history
Move holiday-stop credit calculation
  • Loading branch information
kelvin-chappell committed Aug 28, 2019
1 parent dc189e7 commit 4ef26b2
Show file tree
Hide file tree
Showing 28 changed files with 89 additions and 52 deletions.
20 changes: 17 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,19 @@ lazy val `holiday-stops` = all(project in file("lib/holiday-stops"))
testDep
)
.settings(
libraryDependencies ++= Seq(okhttp3, scalaz, playJson, scalatest, playJsonExtensions) ++ logging
)
libraryDependencies ++= Seq(
okhttp3,
scalaz,
playJson,
scalatest,
scalaCheck,
playJsonExtensions,
circe,
circeParser,
sttp,
sttpCirce
) ++ logging
)

lazy val restHttp = all(project in file("lib/restHttp"))
.settings(
Expand Down Expand Up @@ -245,7 +256,10 @@ lazy val `braze-to-salesforce-file-upload` = all(project in file("handlers/braze

lazy val `holiday-stop-processor` = all(project in file("handlers/holiday-stop-processor"))
.enablePlugins(RiffRaffArtifact)
.dependsOn(`holiday-stops`, effects)
.dependsOn(
`holiday-stops` % "compile->compile;test->test",
effects
)

lazy val `metric-push-api` = all(project in file("handlers/metric-push-api"))
.enablePlugins(RiffRaffArtifact)
Expand Down
9 changes: 1 addition & 8 deletions handlers/holiday-stop-processor/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,10 @@ riffRaffUploadManifestBucket := Option("riffraff-builds")
riffRaffManifestProjectName := s"MemSub::Membership Admin::${name.value}"
riffRaffArtifactResources += (file(s"handlers/${name.value}/cfn.yaml"), "cfn/cfn.yaml")

val sttpVersion = "1.5.17"

libraryDependencies ++= Seq(
"io.github.mkotsur" %% "aws-lambda-scala" % "0.1.1",
"com.softwaremill.sttp" %% "core" % sttpVersion,
"com.softwaremill.sttp" %% "circe" % sttpVersion,
"io.circe" %% "circe-generic" % "0.11.1",
"com.amazonaws" % "aws-java-sdk-s3" % "1.11.566",
"org.slf4j" % "slf4j-api" % "1.7.25",
"ch.qos.logback" % "logback-classic" % "1.2.3",
"com.typesafe.scala-logging" %% "scala-logging" % "3.9.2",
"org.scalatest" %% "scalatest" % "3.0.7" % Test,
"org.scalacheck" %% "scalacheck" % "1.14.0" % Test
"com.typesafe.scala-logging" %% "scala-logging" % "3.9.2"
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import java.time.LocalDate

import cats.implicits._
import com.amazonaws.services.lambda.runtime.Context
import com.gu.holiday_stops.Config
import io.circe.generic.auto._
import io.github.mkotsur.aws.handler.Lambda
import io.github.mkotsur.aws.handler.Lambda._
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.gu.holidaystopprocessor

import java.time.LocalDate

import cats.implicits._
import com.gu.holiday_stops._
import com.gu.salesforce.holiday_stops.SalesforceHolidayStopRequestsDetail._

object HolidayStopProcess {
Expand Down Expand Up @@ -65,7 +67,7 @@ object HolidayStopProcess {
currentGuardianWeeklySubscription <- CurrentGuardianWeeklySubscription(subscription, guardianWeeklyProductRatePlanIds)
nextInvoiceStartDate = NextBillingPeriodStartDate(currentGuardianWeeklySubscription)
maybeExtendedTerm = ExtendedTerm(nextInvoiceStartDate, subscription)
holidayCredit = HolidayCredit(currentGuardianWeeklySubscription)
holidayCredit <- CreditCalculator.guardianWeeklyCredit(guardianWeeklyProductRatePlanIds)(subscription)
holidayCreditUpdate <- HolidayCreditUpdate(holidayCreditProduct, subscription, stop.stoppedPublicationDate, nextInvoiceStartDate, maybeExtendedTerm, holidayCredit)
_ <- if (subscription.hasHolidayStop(stop)) Right(()) else updateSubscription(subscription, holidayCreditUpdate)
updatedSubscription <- getSubscription(stop.subscriptionName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.gu.holidaystopprocessor

import java.time.LocalDate

import com.gu.holiday_stops.CurrentGuardianWeeklySubscription

/**
* Holiday credit is applied to the next invoice on the first day of the next billing period.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.gu.holidaystopprocessor

import com.gu.holiday_stops.{HolidayStop, OverallFailure, ZuoraHolidayWriteError}
import com.typesafe.scalalogging.LazyLogging

case class ProcessResult(holidayStopsToApply: List[HolidayStop], holidayStopResults: List[Either[ZuoraHolidayWriteError, HolidayStopResponse]], resultsToExport: List[HolidayStopResponse], overallFailure: Option[OverallFailure])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.gu.salesforce.holiday_stops.SalesforceHolidayStopRequestsDetail._
import com.gu.util.resthttp.JsonHttp
import scalaz.{-\/, \/-}
import com.gu.holiday_stops.ActionCalculator.suspensionConstantsByProduct
import com.gu.holiday_stops.{OverallFailure, SalesforceHolidayWriteError}

object Salesforce {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.gu.holidaystopprocessor

import java.time.LocalDate

import com.gu.holiday_stops.Config

// This is just for functional testing locally.
object StandaloneApp extends App {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.gu.holidaystopprocessor

import java.time.LocalDate

import cats.implicits._
import com.gu.holidaystopprocessor.Fixtures._
import com.gu.holiday_stops.Fixtures._
import com.gu.holiday_stops._
import com.gu.salesforce.holiday_stops.SalesforceHolidayStopRequestsDetail.{HolidayStopRequestsDetail, ProductName, SubscriptionName}
import org.scalatest._

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package com.gu.holidaystopprocessor

import java.time.LocalDate

import com.gu.holidaystopprocessor.Fixtures.config
import com.gu.holiday_stops.Fixtures.config
import com.gu.holiday_stops._
import org.scalatest.{EitherValues, FlatSpec, Matchers}

class SubscriptionUpdateTest extends FlatSpec with Matchers with EitherValues {
class HolidayCreditUpdateTest extends FlatSpec with Matchers with EitherValues {

val guardianWeeklyProductRatePlanIds = Fixtures.config.guardianWeeklyProductRatePlanIds

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package com.gu.holidaystopprocessor

import java.time.LocalDate

import com.gu.holidaystopprocessor.Fixtures.{config, mkSubscription}
import com.gu.salesforce.holiday_stops.SalesforceHolidayStopRequestsDetail.{HolidayStopRequestsDetail, HolidayStopRequestsDetailChargeCode, HolidayStopRequestsDetailChargePrice, HolidayStopRequestsDetailId, ProductName, StoppedPublicationDate, SubscriptionName}
import com.gu.holiday_stops.Fixtures.{config, mkSubscription}
import com.gu.holiday_stops._
import com.gu.salesforce.holiday_stops.SalesforceHolidayStopRequestsDetail._
import org.scalatest.{EitherValues, FlatSpec, Matchers, OptionValues}

class HolidayStopProcessTest extends FlatSpec with Matchers with EitherValues with OptionValues {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package com.gu.holidaystopprocessor
package com.gu.holiday_stops

case class AccessToken(access_token: String)
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.gu.holidaystopprocessor
package com.gu.holiday_stops

import com.amazonaws.auth.profile.ProfileCredentialsProvider
import com.amazonaws.regions.Regions.EU_WEST_1
import com.amazonaws.services.s3.AmazonS3Client
import com.gu.holiday_stops.OverallFailure
import com.gu.salesforce.SalesforceAuthenticate.SFAuthConfig
import io.circe.Decoder
import io.circe.generic.auto._
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.gu.holiday_stops

import com.gu.salesforce.holiday_stops.SalesforceHolidayStopRequestsDetail.SubscriptionName

object CreditCalculator {

def guardianWeeklyCredit(config: Config, subscriptionName: SubscriptionName): Either[HolidayError, Double] =
for {
accessToken <- Zuora.accessTokenGetResponse(config.zuoraConfig)
subscription <- Zuora.subscriptionGetResponse(config, accessToken)(subscriptionName)
credit <- guardianWeeklyCredit(config.guardianWeeklyProductRatePlanIds)(subscription)
} yield credit

def guardianWeeklyCredit(guardianWeeklyProductRatePlanIds: List[String])(subscription: Subscription): Either[ZuoraHolidayWriteError, Double] =
CurrentGuardianWeeklySubscription(subscription, guardianWeeklyProductRatePlanIds).map(HolidayCredit(_))
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.gu.holidaystopprocessor
package com.gu.holiday_stops

import java.time.LocalDate

import scala.util.Try

/**
Expand Down Expand Up @@ -95,6 +96,7 @@ case class CurrentInvoicedPeriod(
* attached to Guardian Weekly product that satisfies all of the CurrentGuardianWeeklyRatePlanPredicates.
*/
object CurrentGuardianWeeklySubscription {

def apply(subscription: Subscription, guardianWeeklyProductRatePlanIds: List[String]): Either[ZuoraHolidayWriteError, CurrentGuardianWeeklySubscription] =
subscription
.ratePlans
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.gu.holidaystopprocessor
package com.gu.holiday_stops

import java.time.LocalDate
import java.time.temporal.ChronoUnit.DAYS
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.gu.holidaystopprocessor
package com.gu.holiday_stops

sealed trait HolidayError {
val reason: String
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.gu.holidaystopprocessor
package com.gu.holiday_stops

import scala.math.BigDecimal.RoundingMode

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.gu.holidaystopprocessor
package com.gu.holiday_stops

import java.time.LocalDate

import com.gu.holiday_stops

case class HolidayCreditUpdate(
currentTerm: Option[Int],
currentTermPeriodType: Option[String],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.gu.holidaystopprocessor
package com.gu.holiday_stops

import java.time.LocalDate

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.gu.holidaystopprocessor
package com.gu.holiday_stops

import java.time.LocalDate

Expand All @@ -12,28 +12,18 @@ case class Subscription(
ratePlans: List[RatePlan]
) {

val originalRatePlanCharge: Option[RatePlanCharge] = {
val chronologicallyOrderedRatePlans = ratePlans.sortBy { plan =>
plan.ratePlanCharges.map(_.effectiveStartDate.toString).headOption.getOrElse("")
}
for {
ratePlan <- chronologicallyOrderedRatePlans.headOption
charge <- ratePlan.ratePlanCharges.headOption
} yield charge
}

def ratePlanCharge(stop: HolidayStop): Option[RatePlanCharge] = {

def isMatchingPlan(plan: RatePlan): Boolean = plan.productName == "Discounts"

def isMatchingCharge(charge: RatePlanCharge): Boolean =
charge.name == "Holiday Credit" &&
charge.HolidayStart__c.exists { start =>
start.isEqual(stop.stoppedPublicationDate) || start.isBefore(stop.stoppedPublicationDate)
} &&
charge.HolidayEnd__c.exists { end =>
end.isEqual(stop.stoppedPublicationDate) || end.isAfter(stop.stoppedPublicationDate)
}
charge.HolidayStart__c.exists { start =>
start.isEqual(stop.stoppedPublicationDate) || start.isBefore(stop.stoppedPublicationDate)
} &&
charge.HolidayEnd__c.exists { end =>
end.isEqual(stop.stoppedPublicationDate) || end.isAfter(stop.stoppedPublicationDate)
}

val charges = for {
plan <- ratePlans if isMatchingPlan(plan)
Expand Down Expand Up @@ -63,5 +53,3 @@ case class RatePlanCharge(
HolidayEnd__c: Option[LocalDate],
processedThroughDate: Option[LocalDate],
)


Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.gu.holidaystopprocessor
package com.gu.holiday_stops

import com.gu.salesforce.holiday_stops.SalesforceHolidayStopRequestsDetail.SubscriptionName
import com.softwaremill.sttp._
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.gu.holidaystopprocessor
package com.gu.holiday_stops

case class ZuoraStatusResponse(
success: Boolean,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.gu.holidaystopprocessor
package com.gu.holiday_stops

import java.time.LocalDate

import com.gu.salesforce.SalesforceAuthenticate.SFAuthConfig
import com.gu.salesforce.holiday_stops.SalesforceHolidayStopRequest._
import com.gu.salesforce.holiday_stops.SalesforceHolidayStopRequest.{HolidayStopRequest, HolidayStopRequestActionedCount, HolidayStopRequestEndDate, HolidayStopRequestStartDate}
import com.gu.salesforce.holiday_stops.SalesforceHolidayStopRequestsDetail._

object Fixtures {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.gu.holidaystopprocessor
package com.gu.holiday_stops

import java.time.LocalDate

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.gu.holidaystopprocessor
package com.gu.holiday_stops

import org.scalatest.{EitherValues, FlatSpec, Matchers}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.gu.holidaystopprocessor
package com.gu.holiday_stops

import java.time.LocalDate

Expand Down
10 changes: 9 additions & 1 deletion project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ object Dependencies {

val awsVersion = "1.11.574"

val circeVersion = "0.11.1"
val sttpVersion = "1.5.17"

val okhttp3 = "com.squareup.okhttp3" % "okhttp" % "3.9.1"
val logging = Seq(
"ch.qos.logback" % "logback-classic" % "1.2.3",
Expand All @@ -12,11 +15,16 @@ object Dependencies {
val scalaz = "org.scalaz" %% "scalaz-core" % "7.2.18"
val playJson = "com.typesafe.play" %% "play-json" % "2.6.9"
val playJsonExtensions = "ai.x" %% "play-json-extensions" % "0.30.1"
val scalatest = "org.scalatest" %% "scalatest" % "3.0.1" % "test"
val scalatest = "org.scalatest" %% "scalatest" % "3.0.1" % Test
val scalaCheck = "org.scalacheck" %% "scalacheck" % "1.14.0" % Test
val jacksonDatabind = "com.fasterxml.jackson.core" % "jackson-databind" % "2.8.11.1"
val awsS3 = "com.amazonaws" % "aws-java-sdk-s3" % awsVersion
val awsSQS = "com.amazonaws" % "aws-java-sdk-sqs" % awsVersion
val awsSES = "com.amazonaws" % "aws-java-sdk-ses" % awsVersion
val awsLambda = "com.amazonaws" % "aws-lambda-java-core" % "1.2.0"
val supportInternationalisation = "com.gu" %% "support-internationalisation" % "0.9"
val circe = "io.circe" %% "circe-generic" % circeVersion
val circeParser = "io.circe" %% "circe-parser" % circeVersion
val sttp = "com.softwaremill.sttp" %% "core" % sttpVersion
val sttpCirce = "com.softwaremill.sttp" %% "circe" % sttpVersion
}

0 comments on commit 4ef26b2

Please sign in to comment.