From 0753bb400729798e798ae2c843ed7d0ca869e7a0 Mon Sep 17 00:00:00 2001 From: JakeReid2020 <75027964+JakeReid2020@users.noreply.github.com> Date: Fri, 29 Aug 2025 14:48:00 +0100 Subject: [PATCH] [NGR-1088] Fixing Answers on page --- .../controllers/RentPeriodsController.scala | 37 ++++++++++--------- .../hmrc/ngrraldfrontend/models/NGRDate.scala | 16 ++++++++ .../hmrc/ngrraldfrontend/repo/RaldRepo.scala | 14 +++---- .../RentPeriodsControllerSpec.scala | 12 +++--- .../ngrraldfrontend/repo/RaldRepoSpec.scala | 20 +++++++++- 5 files changed, 66 insertions(+), 33 deletions(-) diff --git a/app/uk/gov/hmrc/ngrraldfrontend/controllers/RentPeriodsController.scala b/app/uk/gov/hmrc/ngrraldfrontend/controllers/RentPeriodsController.scala index e21dd8d4..8fef65d5 100644 --- a/app/uk/gov/hmrc/ngrraldfrontend/controllers/RentPeriodsController.scala +++ b/app/uk/gov/hmrc/ngrraldfrontend/controllers/RentPeriodsController.scala @@ -24,7 +24,7 @@ import uk.gov.hmrc.govukfrontend.views.viewmodels.table.{Table, TableRow} import uk.gov.hmrc.http.NotFoundException import uk.gov.hmrc.ngrraldfrontend.actions.{AuthRetrievals, PropertyLinkingAction} import uk.gov.hmrc.ngrraldfrontend.config.AppConfig -import uk.gov.hmrc.ngrraldfrontend.models.RaldUserAnswers +import uk.gov.hmrc.ngrraldfrontend.models.{NGRDate, RaldUserAnswers} import uk.gov.hmrc.ngrraldfrontend.models.components.NGRRadio import uk.gov.hmrc.ngrraldfrontend.models.components.NGRRadio.buildRadios import uk.gov.hmrc.ngrraldfrontend.models.components.NavBarPageContents.createDefaultNavBar @@ -46,6 +46,7 @@ class RentPeriodsController @Inject()(view: RentPeriodView, mcc: MessagesControllerComponents )(implicit appConfig: AppConfig, ec:ExecutionContext) extends FrontendController(mcc) with I18nSupport { + def firstTable(userAnswers: RaldUserAnswers)(implicit messages:Messages): Table = Table( rows = Seq( @@ -55,7 +56,7 @@ class RentPeriodsController @Inject()(view: RentPeriodView, ), TableRow( content = Text(userAnswers.provideDetailsOfFirstSecondRentPeriod.map{ dates => - dates.firstDateStart + NGRDate.formatDate(dates.firstDateStart) }.getOrElse("")) ) ), @@ -65,22 +66,22 @@ class RentPeriodsController @Inject()(view: RentPeriodView, ), TableRow( content = Text(userAnswers.provideDetailsOfFirstSecondRentPeriod.map{ dates => - dates.firstDateEnd + NGRDate.formatDate(dates.firstDateEnd) }.getOrElse("")) ) ), - if(userAnswers.provideDetailsOfFirstSecondRentPeriod.nonEmpty){ - Seq( - TableRow( - content = Text(messages("rentPeriods.first.rentValue")) - ), - TableRow( - content = Text(userAnswers.provideDetailsOfFirstSecondRentPeriod.map { dates => - dates.firstRentPeriodAmount.get - }.getOrElse("")) - ) - ) - }else(Seq()), + userAnswers.provideDetailsOfFirstSecondRentPeriod.map{ userAnswers => + userAnswers.firstRentPeriodAmount match + case Some(answer) => Seq( + TableRow( + content = Text(messages("rentPeriods.first.rentValue")) + ), + TableRow( + content = Text(answer) + ) + ) + case None => Seq() + }.getOrElse(Seq()), Seq( TableRow( content = Text(messages("rentPeriods.first.doYouPay")) @@ -108,7 +109,7 @@ class RentPeriodsController @Inject()(view: RentPeriodView, ), TableRow( content = Text(userAnswers.provideDetailsOfFirstSecondRentPeriod.map{ dates => - dates.secondDateStart + NGRDate.formatDate(dates.secondDateStart) }.getOrElse("")) ) ), @@ -118,7 +119,7 @@ class RentPeriodsController @Inject()(view: RentPeriodView, ), TableRow( content = Text(userAnswers.provideDetailsOfFirstSecondRentPeriod.map { dates => - dates.secondDateEnd + NGRDate.formatDate(dates.secondDateEnd) }.getOrElse("")) ) ), @@ -128,7 +129,7 @@ class RentPeriodsController @Inject()(view: RentPeriodView, ), TableRow( content = Text(userAnswers.provideDetailsOfFirstSecondRentPeriod.map { dates => - dates.firstRentPeriodAmount.get + dates.secondHowMuchIsRent }.getOrElse("")) ) ) diff --git a/app/uk/gov/hmrc/ngrraldfrontend/models/NGRDate.scala b/app/uk/gov/hmrc/ngrraldfrontend/models/NGRDate.scala index b470953c..1e64f02d 100644 --- a/app/uk/gov/hmrc/ngrraldfrontend/models/NGRDate.scala +++ b/app/uk/gov/hmrc/ngrraldfrontend/models/NGRDate.scala @@ -21,6 +21,8 @@ import play.api.data.Mapping import play.api.libs.json.{Json, OFormat} import java.time.LocalDate +import java.time.format.DateTimeFormatter +import java.util.Locale final case class NGRDate(day: String, month: String, year: String) { lazy val ngrDate: LocalDate = LocalDate.of(year.toInt, month.toInt, day.toInt) @@ -33,6 +35,20 @@ final case class NGRDate(day: String, month: String, year: String) { object NGRDate { implicit val format: OFormat[NGRDate] = Json.format[NGRDate] + + def formatDate(dateString: String): String = { + val parts = dateString.split("-").map(_.toInt) + val year = parts(0) + val month = f"${parts(1)}%02d" + val day = f"${parts(2)}%02d" + val paddedDate = s"$year-$month-$day" + val inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd") + val outputFormatter = DateTimeFormatter.ofPattern("d MMMM yyyy", Locale.ENGLISH) + val date = LocalDate.parse(paddedDate, inputFormatter) + date.format(outputFormatter) + } + + def unapply(ngrDate: NGRDate): Option[(String, String, String)] = Some(ngrDate.day, ngrDate.month, ngrDate.year) } diff --git a/app/uk/gov/hmrc/ngrraldfrontend/repo/RaldRepo.scala b/app/uk/gov/hmrc/ngrraldfrontend/repo/RaldRepo.scala index 38f76aa2..bbcf0d85 100644 --- a/app/uk/gov/hmrc/ngrraldfrontend/repo/RaldRepo.scala +++ b/app/uk/gov/hmrc/ngrraldfrontend/repo/RaldRepo.scala @@ -136,19 +136,19 @@ case class RaldRepo @Inject()(mongo: MongoComponent, ): Future[Option[RaldUserAnswers]] = { val updates = Seq( - Updates.set("ProvideDetailsOfFirstSecondRentPeriod.firstDateStart", firstDateStart), - Updates.set("ProvideDetailsOfFirstSecondRentPeriod.firstDateEnd", firstDateEnd), - Updates.set("ProvideDetailsOfFirstSecondRentPeriod.firstRentPeriodRadio", firstRentPeriodRadio match { + Updates.set("provideDetailsOfFirstSecondRentPeriod.firstDateStart", firstDateStart), + Updates.set("provideDetailsOfFirstSecondRentPeriod.firstDateEnd", firstDateEnd), + Updates.set("provideDetailsOfFirstSecondRentPeriod.firstRentPeriodRadio", firstRentPeriodRadio match { case answer if (answer == "yesPayedRent") => true case _ => false }), - Updates.set("ProvideDetailsOfFirstSecondRentPeriod.firstRentPeriodAmount", firstRentPeriodAmount match { + Updates.set("provideDetailsOfFirstSecondRentPeriod.firstRentPeriodAmount", firstRentPeriodAmount match { case Some(value) => value.toString() case _ => null }), - Updates.set("ProvideDetailsOfFirstSecondRentPeriod.secondDateStart", secondDateStart), - Updates.set("ProvideDetailsOfFirstSecondRentPeriod.secondDateEnd", secondDateEnd), - Updates.set("ProvideDetailsOfFirstSecondRentPeriod.secondHowMuchIsRent", secondHowMuchIsRent.toString) + Updates.set("provideDetailsOfFirstSecondRentPeriod.secondDateStart", secondDateStart), + Updates.set("provideDetailsOfFirstSecondRentPeriod.secondDateEnd", secondDateEnd), + Updates.set("provideDetailsOfFirstSecondRentPeriod.secondHowMuchIsRent", secondHowMuchIsRent.toString) ) findAndUpdateByCredId(credId, updates: _*) diff --git a/test/uk/gov/hmrc/ngrraldfrontend/controllers/RentPeriodsControllerSpec.scala b/test/uk/gov/hmrc/ngrraldfrontend/controllers/RentPeriodsControllerSpec.scala index f2ab645b..ad950977 100644 --- a/test/uk/gov/hmrc/ngrraldfrontend/controllers/RentPeriodsControllerSpec.scala +++ b/test/uk/gov/hmrc/ngrraldfrontend/controllers/RentPeriodsControllerSpec.scala @@ -55,13 +55,13 @@ class RentPeriodsControllerSpec extends ControllerSpecSupport { selectedProperty = property, provideDetailsOfFirstSecondRentPeriod = Some(ProvideDetailsOfFirstSecondRentPeriod( - firstDateStart = "", - firstDateEnd = "", + firstDateStart = "2016-12-12", + firstDateEnd = "2017-12-12", firstRentPeriodRadio = true, - firstRentPeriodAmount = Some(""), - secondDateStart = "", - secondDateEnd = "", - secondHowMuchIsRent = ""))))) + firstRentPeriodAmount = Some("10000"), + secondDateStart = "2018-12-12", + secondDateEnd = "2019-12-12", + secondHowMuchIsRent = "10000"))))) val result = controller.show()(authenticatedFakeRequest()) status(result) mustBe OK val content = contentAsString(result) diff --git a/test/uk/gov/hmrc/ngrraldfrontend/repo/RaldRepoSpec.scala b/test/uk/gov/hmrc/ngrraldfrontend/repo/RaldRepoSpec.scala index 11a4aca6..17098233 100644 --- a/test/uk/gov/hmrc/ngrraldfrontend/repo/RaldRepoSpec.scala +++ b/test/uk/gov/hmrc/ngrraldfrontend/repo/RaldRepoSpec.scala @@ -264,8 +264,24 @@ class RaldRepoSpec extends TestSupport with TestData result mustBe actual } + "handle yesPayedRent value correctly by setting boolean to true and take first rent period amount" in { - val result = await(repository.insertProvideDetailsOfFirstSecondRentPeriod( + val expected = RaldUserAnswers( + credId, + NewAgreement, + property, + provideDetailsOfFirstSecondRentPeriod = Some(ProvideDetailsOfFirstSecondRentPeriod( + firstDateStart = "2025-01-01", + firstDateEnd = "2025-01-31", + firstRentPeriodRadio = true, + firstRentPeriodAmount = Some("1000"), + secondDateStart = "2025-02-01", + secondDateEnd = "2025-02-28", + secondHowMuchIsRent = "1000" + )) + ) + + await(repository.insertProvideDetailsOfFirstSecondRentPeriod( credId = credId, firstDateStart = "2025-01-01", firstDateEnd = "2025-01-31", @@ -278,7 +294,7 @@ class RaldRepoSpec extends TestSupport with TestData val actual = await(repository.findByCredId(credId)) - result mustBe actual + actual mustBe Some(expected) } "insert rent based on with other desc successfully" in {