Skip to content

Commit

Permalink
GFORMS-3100 - Add new functions getSicDescription and getCountry to u…
Browse files Browse the repository at this point in the history
…se in expressions to retrieve values from lookups (#2370)
  • Loading branch information
DursunGundogan authored Jan 2, 2025
1 parent 1fb177a commit 68f1218
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 5 deletions.
29 changes: 27 additions & 2 deletions app/uk/gov/hmrc/gform/eval/EvaluationResults.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ import uk.gov.hmrc.gform.gform.SummarySubstituter._
import uk.gov.hmrc.gform.gform.{ AuthContextPrepop, Substituter, SummarySubstitutions }
import uk.gov.hmrc.gform.graph.RecData
import uk.gov.hmrc.gform.graph.processor.UserCtxEvaluatorProcessor
import uk.gov.hmrc.gform.lookup.{ LocalisedLookupOptions, LookupLabel, LookupOptions }
import uk.gov.hmrc.gform.lookup.{ LocalisedLookupOptions, LookupLabel, LookupOptions, LookupRegistry }
import uk.gov.hmrc.gform.models.helpers.DateHelperFunctions.getMonthValue
import uk.gov.hmrc.gform.models.ids.ModelComponentId.Atomic
import uk.gov.hmrc.gform.models.ids.{ IndexedComponentId, ModelComponentId }
import uk.gov.hmrc.gform.sharedmodel._
import uk.gov.hmrc.gform.sharedmodel.form.FormComponentIdToFileIdMapping
import uk.gov.hmrc.gform.sharedmodel.formtemplate.InternalLink.PageLink
import uk.gov.hmrc.gform.sharedmodel.formtemplate.LookupFnc.{ CountryName, SicDescription }
import uk.gov.hmrc.gform.sharedmodel.formtemplate._
import uk.gov.hmrc.gform.views.summary.TextFormatter
import uk.gov.hmrc.play.bootstrap.binders.RedirectUrl
Expand Down Expand Up @@ -388,7 +389,8 @@ case class EvaluationResults(
case ChoicesSelected(formComponentId) => getChoicesSelected(formComponentId, evaluationContext)
case ChoicesAvailable(formComponentId) =>
getChoicesAvailable(formComponentId, evaluationContext, booleanExprResolver, recData)
case TaskStatus(_) => unsupportedOperation("Number")(expr)
case TaskStatus(_) => unsupportedOperation("Number")(expr)
case LookupOps(_, _) => unsupportedOperation("Number")(expr)
}

loop(typeInfo.expr)
Expand Down Expand Up @@ -729,6 +731,16 @@ case class EvaluationResults(
}
)
StringResult(str)
case LookupOps(expr, lookupFnc) =>
loop(expr).withStringResult(ExpressionResult.empty)(value =>
lookupFnc match {
case CountryName =>
getLookupLabelById(value, Register.Country, evaluationContext.lookupRegistry, evaluationContext.lang)
case SicDescription =>
getLookupLabelById(value, Register.SicCode, evaluationContext.lookupRegistry, evaluationContext.lang)
}
)

case Concat(exprs) => evalConcat(exprs, recData, booleanExprResolver, evaluationContext)
case CountryOfItmpAddress =>
val itmpRetrievals = evaluationContext.thirdPartyData.itmpRetrievals
Expand Down Expand Up @@ -766,6 +778,19 @@ case class EvaluationResults(
}
.getOrElse(Empty)

private def getLookupLabelById(value: String, register: Register, lookupRegistry: LookupRegistry, lang: LangADT) = {
val lookupOptions: LocalisedLookupOptions = lookupRegistry
.get(register)
.collect { case uk.gov.hmrc.gform.lookup.AjaxLookup(lookupOptions, _, _) => lookupOptions }
.get

lookupOptions.fold[ExpressionResult](Empty) { options =>
options.options
.collectFirst { case (l, lo) if lo.id.id === value => StringResult(l.label) }
.getOrElse(Empty)
}(lang)
}

private def evalLookupColumnCount(
fcId: FormComponentId,
column: String,
Expand Down
1 change: 1 addition & 0 deletions app/uk/gov/hmrc/gform/gform/ConcatFormatSubstituter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ object ConcatFormatSubstituter {
case e: ChoicesSelected => e
case e: ChoicesAvailable => e
case e: TaskStatus => e
case LookupOps(e, lookupFnc) => LookupOps(substitute(substitutions, e), lookupFnc)
}
}

Expand Down
1 change: 1 addition & 0 deletions app/uk/gov/hmrc/gform/gform/ExprUpdater.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class ExprUpdater(index: Int, baseIds: List[FormComponentId]) {
case ChoicesSelected(formComponentId) => ChoicesSelected(expandFcId(formComponentId))
case ChoicesAvailable(formComponentId) => ChoicesAvailable(expandFcId(formComponentId))
case TaskStatus(_) => expr
case LookupOps(expr, lookupFnc) => LookupOps(expandExpr(expr), lookupFnc)
}

private def expandDateFunc(dateFunc: DateProjection): DateProjection = dateFunc match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ object FormComponentIdSubstituter {
case ChoicesSelected(fcId) => ChoicesSelected(substitutions.updateFormComponentId(fcId))
case ChoicesAvailable(fcId) => ChoicesAvailable(substitutions.updateFormComponentId(fcId))
case e: TaskStatus => e
case LookupOps(e, lookupFnc) => LookupOps(substitute(substitutions, e), lookupFnc)
}
}

Expand Down
1 change: 0 additions & 1 deletion app/uk/gov/hmrc/gform/gform/GformModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,6 @@ class GformModule(
val languageSwitchController: LanguageSwitchController =
new LanguageSwitchController(
controllersModule.authenticatedRequestActions,
configModule.playConfiguration,
new LanguageUtils(playBuiltInsModule.langs, configModule.playConfiguration)(playBuiltInsModule.messagesApi),
lookupRegistry,
gformBackendModule.gformConnector,
Expand Down
2 changes: 0 additions & 2 deletions app/uk/gov/hmrc/gform/gform/LanguageSwitchController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package uk.gov.hmrc.gform.gform

import cats.implicits._
import play.api.Configuration
import play.api.i18n.{ I18nSupport, Lang }
import play.api.mvc.{ Action, ActionBuilder, AnyContent, ControllerComponents, Request }
import scala.concurrent.{ ExecutionContext, Future }
Expand All @@ -37,7 +36,6 @@ import uk.gov.hmrc.gform.lookup.{ AjaxLookup, RadioLookup }

class LanguageSwitchController(
auth: AuthenticatedRequestActionsAlgebra[Future],
configuration: Configuration,
languageUtils: LanguageUtils,
lookupRegistry: LookupRegistry,
gformConnector: GformConnector,
Expand Down
1 change: 1 addition & 0 deletions app/uk/gov/hmrc/gform/gform/SummaryExprSubstituter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ object SummarySubstituter {
case e: ChoicesSelected => e
case e: ChoicesAvailable => e
case e: TaskStatus => e
case LookupOps(e, lookupFnc) => LookupOps(substitute(substitutions, e), lookupFnc)
}
}

Expand Down
2 changes: 2 additions & 0 deletions app/uk/gov/hmrc/gform/sharedmodel/LangADT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

package uk.gov.hmrc.gform.sharedmodel
import cats.Eq
import play.api.i18n.{ Lang, Langs, MessagesApi }
import play.api.libs.json._
import play.api.mvc.RequestHeader
Expand Down Expand Up @@ -61,5 +62,6 @@ object LangADT {

implicit val langADTReads: Reads[LangADT] = Reads.apply[LangADT](convertToLang)
implicit val format: OFormat[LangADT] = OFormatWithTemplateReadFallback(langADTReads)
implicit val equal: Eq[LangADT] = Eq.fromUniversalEquals

}
12 changes: 12 additions & 0 deletions app/uk/gov/hmrc/gform/sharedmodel/formtemplate/Expr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ sealed trait Expr extends Product with Serializable {
case ChoicesSelected(_) => expr :: Nil
case ChoicesAvailable(_) => expr :: Nil
case TaskStatus(_) => expr :: Nil
case LookupOps(_, _) => expr :: Nil

}
loop(this).headOption
Expand Down Expand Up @@ -145,6 +146,7 @@ sealed trait Expr extends Product with Serializable {
case ChoicesSelected(formComponentId) => FormCtx(formComponentId) :: Nil
case ChoicesAvailable(formComponentId) => FormCtx(formComponentId) :: Nil
case TaskStatus(_) => this :: Nil
case LookupOps(expr, _) => expr.leafs(formModel)
}

def sums: List[Sum] = this match {
Expand Down Expand Up @@ -191,6 +193,7 @@ sealed trait Expr extends Product with Serializable {
case ChoicesSelected(_) => Nil
case ChoicesAvailable(_) => Nil
case TaskStatus(_) => Nil
case LookupOps(_, _) => Nil
}

def leafs(): List[Expr] = this match {
Expand Down Expand Up @@ -238,6 +241,7 @@ sealed trait Expr extends Product with Serializable {
case ChoicesSelected(formComponentId) => FormCtx(formComponentId) :: Nil
case ChoicesAvailable(formComponentId) => FormCtx(formComponentId) :: Nil
case TaskStatus(_) => this :: Nil
case LookupOps(expr, _) => expr.leafs()
}

def allFormComponentIds(): List[FormComponentId] =
Expand Down Expand Up @@ -271,6 +275,7 @@ final case object LangCtx extends Expr
final case class DataRetrieveCtx(id: DataRetrieveId, attribute: DataRetrieve.Attribute) extends Expr
final case class DataRetrieveCount(id: DataRetrieveId) extends Expr
final case class LookupColumn(formComponentId: FormComponentId, column: String) extends Expr
final case class LookupOps(expr: Expr, lookupFnc: LookupFnc) extends Expr
final case class CsvCountryCountCheck(formComponentId: FormComponentId, column: String, value: String) extends Expr
final case class Size(formComponentId: FormComponentId, index: SizeRefType) extends Expr
final case class Typed(expr: Expr, tpe: ExplicitExprType) extends Expr
Expand Down Expand Up @@ -491,3 +496,10 @@ object LoginInfo {

implicit val format: OFormat[LoginInfo] = derived.oformat()
}

sealed trait LookupFnc
object LookupFnc {
case object CountryName extends LookupFnc
case object SicDescription extends LookupFnc
implicit val format: OFormat[LookupFnc] = derived.oformat()
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ object ExprPrettyPrint {
case ChoicesSelected(_) => "ChoicesSelected"
case ChoicesAvailable(_) => "ChoicesAvailable"
case TaskStatus(_) => "TaskStatus"
case LookupOps(_, lookupFnc) => lookupFnc.toString
}

def prettyPrintItmpNameFocus(focus: ItmpNameFocus): String = focus match {
Expand Down

0 comments on commit 68f1218

Please sign in to comment.