Skip to content

Commit

Permalink
Add TraderProfile model
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNickWilson committed May 10, 2024
1 parent d33b0e5 commit 45dd3b4
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 2 deletions.
34 changes: 34 additions & 0 deletions app/models/TraderProfile.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package models

import cats.data.{Ior, IorNec}
import cats.implicits._
import pages.{HasNiphlPage, HasNirmsPage, NiphlNumberPage, NirmsNumberPage, UkimsNumberPage}
import queries.Query

final case class TraderProfile(
ukimsNumber: String,
nirmsNumber: Option[String],
niphlNumber: Option[String]
)

object TraderProfile {

def build(answers: UserAnswers): IorNec[Query, TraderProfile] =
(
answers.getIor(UkimsNumberPage),
getNirms(answers),
getNiphl(answers)
).parMapN(TraderProfile.apply)

private def getNirms(answers: UserAnswers): IorNec[Query, Option[String]] =
answers.getIor(HasNirmsPage).flatMap {
case true => answers.getIor(NirmsNumberPage).map(Some(_))
case false => Ior.Right(None)
}

private def getNiphl(answers: UserAnswers): IorNec[Query, Option[String]] =
answers.getIor(HasNiphlPage).flatMap {
case true => answers.getIor(NiphlNumberPage).map(Some(_))
case false => Ior.Right(None)
}
}
7 changes: 6 additions & 1 deletion app/models/UserAnswers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@

package models

import cats.data.{IorNec, NonEmptyChain}
import cats.implicits._
import play.api.libs.json._
import queries.{Gettable, Settable}
import queries.{Gettable, Query, Settable}
import uk.gov.hmrc.mongo.play.json.formats.MongoJavatimeFormats

import java.time.Instant
Expand All @@ -32,6 +34,9 @@ final case class UserAnswers(
def get[A](page: Gettable[A])(implicit rds: Reads[A]): Option[A] =
Reads.optionNoError(Reads.at(page.path)).reads(data).getOrElse(None)

def getIor[A](page: Gettable[A])(implicit rds: Reads[A]): IorNec[Query, A] =
get(page).toRightIor(NonEmptyChain.one(page))

def set[A](page: Settable[A], value: A)(implicit writes: Writes[A]): Try[UserAnswers] = {

val updatedData = data.setObject(page.path, Json.toJson(value)) match {
Expand Down
3 changes: 2 additions & 1 deletion project/AppDependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ object AppDependencies {
play.sbt.PlayImport.ws,
"uk.gov.hmrc" %% "play-frontend-hmrc-play-30" % "9.5.0",
"uk.gov.hmrc" %% "bootstrap-frontend-play-30" % bootstrapVersion,
"uk.gov.hmrc.mongo" %% "hmrc-mongo-play-30" % hmrcMongoVersion
"uk.gov.hmrc.mongo" %% "hmrc-mongo-play-30" % hmrcMongoVersion,
"org.typelevel" %% "cats-core" % "2.3.0"
)

val test = Seq(
Expand Down
90 changes: 90 additions & 0 deletions test/models/TraderProfileSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package models

import org.scalatest.{OptionValues, TryValues}
import org.scalatest.freespec.AnyFreeSpec
import org.scalatest.matchers.must.Matchers
import pages._

class TraderProfileSpec extends AnyFreeSpec with Matchers with TryValues with OptionValues {

".build" - {

"must return a TraderProfile when all mandatory questions are answered" - {

"and all optional data is present" in {

val answers =
UserAnswers("id")
.set(UkimsNumberPage, "1").success.value
.set(HasNirmsPage, true).success.value
.set(NirmsNumberPage, "2").success.value
.set(HasNiphlPage, true).success.value
.set(NiphlNumberPage, "3").success.value

val (errors, data) = TraderProfile.build(answers).pad

data.value mustEqual TraderProfile("1", Some("2"), Some("3"))
errors must not be defined
}

"and all optional data is missing" in {

val answers =
UserAnswers("id")
.set(UkimsNumberPage, "1").success.value
.set(HasNirmsPage, false).success.value
.set(HasNiphlPage, false).success.value

val (errors, data) = TraderProfile.build(answers).pad

data.value mustEqual TraderProfile("1", None, None)
errors must not be defined
}
}

"must return errors" - {

"when all mandatory answers are missing" in {

val answers = UserAnswers("id")

val (errors, data) = TraderProfile.build(answers).pad

data must not be defined
errors.value.toChain.toList must contain theSameElementsAs Seq(
UkimsNumberPage,
HasNirmsPage,
HasNiphlPage
)
}

"when the user said they have a Nirms number but it is missing" in {

val answers =
UserAnswers("id")
.set(UkimsNumberPage, "1").success.value
.set(HasNirmsPage, true).success.value
.set(HasNiphlPage, false).success.value

val (errors, data) = TraderProfile.build(answers).pad

data must not be defined
errors.value.toChain.toList must contain only NirmsNumberPage
}

"when the user said they have a Niphl number but it is missing" in {

val answers =
UserAnswers("id")
.set(UkimsNumberPage, "1").success.value
.set(HasNirmsPage, false).success.value
.set(HasNiphlPage, true).success.value

val (errors, data) = TraderProfile.build(answers).pad

data must not be defined
errors.value.toChain.toList must contain only NiphlNumberPage
}
}
}
}

0 comments on commit 45dd3b4

Please sign in to comment.