diff --git a/app/uk/gov/hmrc/gform/eval/ExpressionResultWithTypeInfo.scala b/app/uk/gov/hmrc/gform/eval/ExpressionResultWithTypeInfo.scala index d5001bb4d..2f72132ae 100644 --- a/app/uk/gov/hmrc/gform/eval/ExpressionResultWithTypeInfo.scala +++ b/app/uk/gov/hmrc/gform/eval/ExpressionResultWithTypeInfo.scala @@ -19,6 +19,8 @@ package uk.gov.hmrc.gform.eval import cats.syntax.eq._ import play.api.i18n.Messages +import java.time.LocalDate + case class ExpressionResultWithTypeInfo(expressionResult: ExpressionResult, typeInfo: TypeInfo) { def stringRepresentation(implicit messages: Messages): String = @@ -30,6 +32,8 @@ case class ExpressionResultWithTypeInfo(expressionResult: ExpressionResult, type def optionRepresentation: Option[Seq[String]] = expressionResult.optionRepresentation + def dateRepresentation: Option[LocalDate] = expressionResult.dateRepresentation(typeInfo) + def listRepresentation(implicit messages: Messages): List[String] = expressionResult.listRepresentation(typeInfo, messages) diff --git a/app/uk/gov/hmrc/gform/gform/csv/CsvTaxRateAdapter.scala b/app/uk/gov/hmrc/gform/gform/csv/CsvTaxRateAdapter.scala index 66bd9ec56..fff4d58b8 100644 --- a/app/uk/gov/hmrc/gform/gform/csv/CsvTaxRateAdapter.scala +++ b/app/uk/gov/hmrc/gform/gform/csv/CsvTaxRateAdapter.scala @@ -27,7 +27,6 @@ import java.time.format.DateTimeFormatter class CsvTaxRateAdapter extends CsvDataRetrieveAdapter[TaxRate] { private val hmrcTaxRateDateFormat: DateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy") - private val hmrcTaxRateRequestFormat: DateTimeFormatter = DateTimeFormatter.ofPattern("d MMMM yyyy") override val data: List[TaxRate] = CsvUtils .readCsvWithColumns("HMRCTaxRates.csv") @@ -43,18 +42,16 @@ class CsvTaxRateAdapter extends CsvDataRetrieveAdapter[TaxRate] { override def search(request: DataRetrieve.Request): Option[JsValue] = request.json.validate[TaxRateRequest] match { - case JsSuccess(taxRateRequest, _) => - val regimeRates: Option[List[TaxRate]] = data.groupBy(_.regime).get(taxRateRequest.regime) - val requestedDate: LocalDate = LocalDate.parse(taxRateRequest.date, hmrcTaxRateRequestFormat) - + case JsSuccess(params, _) => + val regimeRates: Option[List[TaxRate]] = data.groupBy(_.regime).get(params.regime) val response: Option[TaxRate] = regimeRates.flatMap(_.find { rate => - rate.code === taxRateRequest.code && ( - rate.startDate.isEqual(requestedDate) || rate.startDate.isBefore(requestedDate) + rate.code === params.code && ( + rate.startDate.isEqual(params.date) || rate.startDate.isBefore(params.date) ) && ( - rate.endDate.isEqual(requestedDate) || rate.endDate.isAfter(requestedDate) + rate.endDate.isEqual(params.date) || rate.endDate.isAfter(params.date) ) }) - response.map(rate => Json.toJson(rate)) + response.map(Json.toJson(_)) case JsError(e) => throw new Exception( s"An error occurred attempting to unmarshal request: ${Json.stringify(request.json)}. Error: ${Json @@ -68,7 +65,7 @@ object TaxRate { implicit val format: OFormat[TaxRate] = derived.oformat() } -case class TaxRateRequest(regime: String, code: String, date: String) +case class TaxRateRequest(regime: String, code: String, date: LocalDate) object TaxRateRequest { implicit val format: OFormat[TaxRateRequest] = Json.format[TaxRateRequest] } diff --git a/app/uk/gov/hmrc/gform/sharedmodel/DataRetrieve.scala b/app/uk/gov/hmrc/gform/sharedmodel/DataRetrieve.scala index c243fca3d..57574ecbd 100644 --- a/app/uk/gov/hmrc/gform/sharedmodel/DataRetrieve.scala +++ b/app/uk/gov/hmrc/gform/sharedmodel/DataRetrieve.scala @@ -20,12 +20,13 @@ import cats.Eq import julienrf.json.derived import play.api.i18n.Messages import play.api.libs.json._ -import scala.util.matching.Regex import uk.gov.hmrc.gform.eval.ExpressionResultWithTypeInfo import uk.gov.hmrc.gform.models.optics.{ DataOrigin, FormModelVisibilityOptics } import uk.gov.hmrc.gform.sharedmodel.form.Form import uk.gov.hmrc.gform.sharedmodel.formtemplate.{ Expr, IncludeIf, JsonUtils } +import scala.util.matching.Regex + final case class Fetch(path: List[String]) extends AnyVal object Fetch { @@ -219,12 +220,14 @@ object DataRetrieve { def asString(expressionResult: ExpressionResultWithTypeInfo)(implicit messages: Messages): String = this match { case ParamType.String => expressionResult.stringRepresentation case ParamType.Integer => expressionResult.numberRepresentation.map(_.toInt.toString).getOrElse("") + case ParamType.Date => expressionResult.dateRepresentation.map(_.toString).getOrElse("") } } object ParamType { case object String extends ParamType case object Integer extends ParamType + case object Date extends ParamType implicit val format: OFormat[ParamType] = derived.oformat() } diff --git a/test/uk/gov/hmrc/gform/gform/csv/CsvTaxRateAdapterSpec.scala b/test/uk/gov/hmrc/gform/gform/csv/CsvTaxRateAdapterSpec.scala index 891eeef95..78adf2367 100644 --- a/test/uk/gov/hmrc/gform/gform/csv/CsvTaxRateAdapterSpec.scala +++ b/test/uk/gov/hmrc/gform/gform/csv/CsvTaxRateAdapterSpec.scala @@ -18,20 +18,19 @@ package uk.gov.hmrc.gform.gform.csv import play.api.libs.json.{ JsValue, Json } import uk.gov.hmrc.gform.Spec -import uk.gov.hmrc.gform.sharedmodel.DataRetrieve import uk.gov.hmrc.gform.gform.csv.TaxRateRequest._ +import uk.gov.hmrc.gform.sharedmodel.DataRetrieve import java.time.LocalDate -import scala.math.BigDecimal.RoundingMode class CsvTaxRateAdapterSpec extends Spec { val csvTaxRateAdapter: CsvTaxRateAdapter = new CsvTaxRateAdapter() - val request: TaxRateRequest = TaxRateRequest("APD", "BANDB-RDCD", "25 October 2023") + val request: TaxRateRequest = TaxRateRequest("APD", "BANDB-RDCD", LocalDate.parse("2023-10-25")) val response: TaxRate = TaxRate( "APD", "BANDB-RDCD", - BigDecimal("87").setScale(2, RoundingMode.HALF_UP), + BigDecimal("87").setScale(2), LocalDate.parse("2023-04-01"), LocalDate.parse("2024-03-31") ) @@ -47,7 +46,7 @@ class CsvTaxRateAdapterSpec extends Spec { } it should "not find a rate for" in { - val request: TaxRateRequest = TaxRateRequest("APD", "NOT-EXISTS", "25 October 2023") + val request: TaxRateRequest = TaxRateRequest("APD", "NOT-EXISTS", LocalDate.parse("2023-10-25")) val dataRetrieveRequest: DataRetrieve.Request = DataRetrieve.Request(Json.toJson(request), List.empty) csvTaxRateAdapter.search(dataRetrieveRequest) shouldBe None }