-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d33b0e5
commit 45dd3b4
Showing
4 changed files
with
132 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
} |