Skip to content

Commit

Permalink
GFORMS-3018 - Add date parameter type to DataRetrieve (#2309)
Browse files Browse the repository at this point in the history
  • Loading branch information
cmanson authored Nov 5, 2024
1 parent cf1c10d commit ec467a6
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
4 changes: 4 additions & 0 deletions app/uk/gov/hmrc/gform/eval/ExpressionResultWithTypeInfo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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)

Expand Down
17 changes: 7 additions & 10 deletions app/uk/gov/hmrc/gform/gform/csv/CsvTaxRateAdapter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand All @@ -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]
}
5 changes: 4 additions & 1 deletion app/uk/gov/hmrc/gform/sharedmodel/DataRetrieve.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
}
Expand Down
9 changes: 4 additions & 5 deletions test/uk/gov/hmrc/gform/gform/csv/CsvTaxRateAdapterSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
Expand All @@ -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
}
Expand Down

0 comments on commit ec467a6

Please sign in to comment.