Skip to content

Commit

Permalink
Merge 19941d2 into 1ee0744
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario Galic committed Jul 18, 2019
2 parents 1ee0744 + 19941d2 commit b76905a
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 25 deletions.
Expand Up @@ -12,7 +12,7 @@ case class HolidayStop(
stoppedPublicationDate: LocalDate
)

object HolidayStop {
object HolidayStops {

def apply(request: HolidayStopRequest): Seq[HolidayStop] =
ActionCalculator.publicationDatesToBeStopped(request) map { date =>
Expand Down
Expand Up @@ -27,7 +27,7 @@ object HolidayStopProcess {
): ProcessResult = {
val result = for {
requests <- getRequests(ProductName("Guardian Weekly"))
holidayStops <- Right(requests.map(_.request).distinct.flatMap(HolidayStop(_)))
holidayStops <- Right(requests.map(_.request).distinct.flatMap(HolidayStops(_)))
alreadyExportedChargeCodes <- Right(requests.flatMap(_.zuoraRefs.getOrElse(Nil).map(_.chargeCode)).distinct)
} yield {
val responses = holidayStops map {
Expand Down
Expand Up @@ -6,31 +6,18 @@ import org.joda.time.{DateTimeConstants, Days, LocalDate}
object ActionCalculator {

// TODO this will likely need to change to return an array of days of week (when we support more than just GW)
def productNameToDayOfWeek(productName: ProductName): Int = productName.value match {
def dayOfPublicationByProduct(productName: ProductName): Int = productName.value match {
case s if s.startsWith("Guardian Weekly") => DateTimeConstants.FRIDAY
}

def publicationDatesToBeStopped(hsr: HolidayStopRequest): List[LocalDate] = {

def applicableDates(
fromInclusive: LocalDate,
toInclusive: LocalDate,
p: LocalDate => Boolean
): List[LocalDate] = {
val dateRange = 0 to Days.daysBetween(fromInclusive, toInclusive).getDays
dateRange.foldLeft(List.empty[LocalDate]) { (acc, i) =>
val d = fromInclusive.plusDays(i)
if (p(d)) acc :+ d
else acc
}
}

val dayOfWeekForProduct = productNameToDayOfWeek(hsr.Product_Name__c)
applicableDates(
fromInclusive = hsr.Start_Date__c.value,
toInclusive = hsr.End_Date__c.value,
{ _.getDayOfWeek == dayOfWeekForProduct }
)
val fromInclusive = hsr.Start_Date__c.value
val toInclusive = hsr.End_Date__c.value
val dayOfPublication = dayOfPublicationByProduct(hsr.Product_Name__c)
def isPublicationDay(currentDayWithinHoliday: Int) = fromInclusive.plusDays(currentDayWithinHoliday).getDayOfWeek == dayOfPublication
def stoppedDate(currentDayWithinHoliday: Int) = fromInclusive.plusDays(currentDayWithinHoliday)
val holidayLengthInDays = 0 to Days.daysBetween(fromInclusive, toInclusive).getDays
holidayLengthInDays.toList.collect { case day if isPublicationDay(day) => stoppedDate(day) }
}

}
Expand Up @@ -10,10 +10,10 @@ class ActionCalculatorTest extends FlatSpec with Matchers {

it should "convert ProductName to a day of the week" in {

ActionCalculator.productNameToDayOfWeek(gwProductName) shouldEqual DateTimeConstants.FRIDAY
ActionCalculator.dayOfPublicationByProduct(gwProductName) shouldEqual DateTimeConstants.FRIDAY

assertThrows[MatchError] {
ActionCalculator.productNameToDayOfWeek(ProductName("blah"))
ActionCalculator.dayOfPublicationByProduct(ProductName("blah"))
}

}
Expand Down

0 comments on commit b76905a

Please sign in to comment.