Skip to content

Commit

Permalink
Merge 789cc3e into 1bd2afa
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario Galic committed Sep 9, 2019
2 parents 1bd2afa + 789cc3e commit 5cefc8d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 25 deletions.
Expand Up @@ -167,30 +167,18 @@ object Handler extends Logging {
pathParams <- req.pathParamsAsCaseClass[PotentialHolidayStopsV2PathParams]()(Json.reads[PotentialHolidayStopsV2PathParams])
productNamePrefix <- req.headers.flatMap(_.get(HEADER_PRODUCT_NAME_PREFIX)).toApiGatewayOp(s"missing '$HEADER_PRODUCT_NAME_PREFIX' header")
queryParams <- req.queryParamsAsCaseClass[PotentialHolidayStopsV2QueryParams]()
credit <- estimateCredit(queryParams, pathParams, config, backend)
} yield ApiGatewayResponse(
"200",
PotentialHolidayStopsResponse(
ActionCalculator.publicationDatesToBeStopped(queryParams.startDate, queryParams.endDate, ProductName(productNamePrefix))
.map(PotentialHolidayStop(_, credit))
)
)).apiResponse
}

def estimateCredit(
queryParams: PotentialHolidayStopsV2QueryParams,
pathParams: PotentialHolidayStopsV2PathParams,
config: Config,
backend: SttpBackend[Id, Nothing]
): ApiGatewayOp[Option[Double]] = {
if (queryParams.estimateCredit == Some("true")) {
CreditCalculator
.guardianWeeklyCredit(config, pathParams.subscriptionName, backend, /* FIXME: estimation for N-for-N will be wrong */ LocalDate.now)
.toApiGatewayOp("Failed to calculate credit")
.map(Some(_))
} else {
ContinueProcessing(None)
}
} yield {
val potentialHolidayStops =
ActionCalculator
.publicationDatesToBeStopped(queryParams.startDate, queryParams.endDate, ProductName(productNamePrefix))
.map { stoppedPublicationDate => // unfortunately necessary due to GW N-for-N requiring stoppedPublicationDate to calculate correct credit estimation
if (queryParams.estimateCredit.contains("true"))
PotentialHolidayStop(stoppedPublicationDate, CreditCalculator(config, pathParams.subscriptionName, backend, stoppedPublicationDate).toOption)
else
PotentialHolidayStop(stoppedPublicationDate, None)
}
ApiGatewayResponse("200", PotentialHolidayStopsResponse(potentialHolidayStops))
}).apiResponse
}

case class ListExistingPathParams(subscriptionName: Option[SubscriptionName])
Expand Down
Expand Up @@ -6,7 +6,7 @@ import com.gu.salesforce.holiday_stops.SalesforceHolidayStopRequestsDetail.Subsc
import com.softwaremill.sttp.{Id, SttpBackend}

object CreditCalculator {
def guardianWeeklyCredit(
def apply(
config: Config,
subscriptionName: SubscriptionName,
backend: SttpBackend[Id, Nothing],
Expand Down

0 comments on commit 5cefc8d

Please sign in to comment.