diff --git a/app/uk/gov/hmrc/tai/model/domain/TaxCodeChange.scala b/app/uk/gov/hmrc/tai/model/domain/TaxCodeChange.scala index e3c86e950..6f3a82f07 100644 --- a/app/uk/gov/hmrc/tai/model/domain/TaxCodeChange.scala +++ b/app/uk/gov/hmrc/tai/model/domain/TaxCodeChange.scala @@ -25,11 +25,11 @@ case class TaxCodeChange(previous: List[TaxCodeRecord], current: List[TaxCodeRec require(current.nonEmpty, "No current records for Tax Code Change. Current date cannot be determined.") - val currentPensionCount: Int = current.count(_.pensionIndicator) - val currentEmploymentCount: Int = current.count(!_.pensionIndicator) - val mostRecentTaxCodeChangeDate: LocalDate = DateHelper.mostRecentDate(current.map(_.startDate)) - val mostRecentPreviousTaxCodeChangeDate: LocalDate = - DateHelper.mostRecentDate(previous.map(_.startDate)) + val currentPensionCount: Int = current.count(_.pensionIndicator) + val currentEmploymentCount: Int = current.count(!_.pensionIndicator) + val mostRecentTaxCodeChangeDate: LocalDate = DateHelper.mostRecentDate(current.map(_.startDate)) + val mostRecentPreviousTaxCodeChangeDate: Option[LocalDate] = + DateHelper.mostRecentDateOption(previous.map(_.startDate)) lazy val uniqueTaxCodes: Seq[String] = (previous ++ current).map(_.taxCode).distinct } diff --git a/app/uk/gov/hmrc/tai/util/DateHelper.scala b/app/uk/gov/hmrc/tai/util/DateHelper.scala index 37a8673b7..7988c6e7d 100644 --- a/app/uk/gov/hmrc/tai/util/DateHelper.scala +++ b/app/uk/gov/hmrc/tai/util/DateHelper.scala @@ -34,6 +34,9 @@ object DateHelper { def mostRecentDate(dates: Seq[LocalDate]): LocalDate = dates.min + def mostRecentDateOption(dates: Seq[LocalDate]): Option[LocalDate] = + dates.reduceOption((a, b) => if (a.isAfter(b)) a else b) + def monthOfYear(date: String): String = { val monthRegex = "[A-Za-z]+".r monthRegex.findFirstIn(date).getOrElse("") diff --git a/test/uk/gov/hmrc/tai/model/domain/TaxCodeChangeSpec.scala b/test/uk/gov/hmrc/tai/model/domain/TaxCodeChangeSpec.scala index 7d758792f..848af3e3f 100644 --- a/test/uk/gov/hmrc/tai/model/domain/TaxCodeChangeSpec.scala +++ b/test/uk/gov/hmrc/tai/model/domain/TaxCodeChangeSpec.scala @@ -56,7 +56,7 @@ class TaxCodeChangeSpec extends PlaySpec { val model = TaxCodeChange(List(previousTaxCodeRecord1, fullYearTaxCode), List(currentTaxCodeRecord1, fullYearTaxCode)) - model.mostRecentPreviousTaxCodeChangeDate mustEqual startDate + model.mostRecentPreviousTaxCodeChangeDate mustEqual Some(startDate) } }