-
Notifications
You must be signed in to change notification settings - Fork 0
Ngr 1126 - Adding Page Rent Interim Court details #46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a36963d
[NGR-1126] WIP
JakeReid2020 debb3d9
[NGR-1126] Adding all tests
JakeReid2020 8f1e68a
[NGR-1126] Adding all the tests
JakeReid2020 1bc18f7
[NGR-1126] Fixing hyperlinks
JakeReid2020 b02c518
[NGR-1126] Code clean up
JakeReid2020 eee52cd
[NGR-1126] Code Clean-up
JakeReid2020 d5f9a9f
[NGR-1126] Allowing commas to be added
JakeReid2020 df4ce5c
[NGR-1126] Fixing test
JakeReid2020 9b78f16
[NGR-1126] Adding Repo test
JakeReid2020 46aa7c1
[NGR-1126] Changing rounding and adding to the RaldUserAnswers
JakeReid2020 7403545
[NGR-1126] Rebase
JakeReid2020 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
102 changes: 102 additions & 0 deletions
102
app/uk/gov/hmrc/ngrraldfrontend/controllers/InterimRentSetByTheCourtController.scala
This file contains hidden or 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,102 @@ | ||
/* | ||
* Copyright 2025 HM Revenue & Customs | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package uk.gov.hmrc.ngrraldfrontend.controllers | ||
|
||
import play.api.data.Form | ||
import play.api.i18n.{I18nSupport, Messages} | ||
import play.api.mvc.{Action, AnyContent, MessagesControllerComponents} | ||
import play.twirl.api.HtmlFormat | ||
import uk.gov.hmrc.govukfrontend.views.Aliases.{Hint, PrefixOrSuffix, Text} | ||
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.forms.InterimRentSetByTheCourtForm | ||
import uk.gov.hmrc.ngrraldfrontend.models.forms.InterimRentSetByTheCourtForm.form | ||
import uk.gov.hmrc.ngrraldfrontend.models.registration.CredId | ||
import uk.gov.hmrc.ngrraldfrontend.repo.RaldRepo | ||
import uk.gov.hmrc.ngrraldfrontend.views.html.InterimRentSetByTheCourtView | ||
import uk.gov.hmrc.ngrraldfrontend.views.html.components.InputText | ||
import uk.gov.hmrc.play.bootstrap.frontend.controller.FrontendController | ||
|
||
import javax.inject.Inject | ||
import scala.concurrent.{ExecutionContext, Future} | ||
|
||
class InterimRentSetByTheCourtController @Inject()(interimRentSetByTheCourtView: InterimRentSetByTheCourtView, | ||
authenticate: AuthRetrievals, | ||
hasLinkedProperties: PropertyLinkingAction, | ||
inputText: InputText, | ||
raldRepo: RaldRepo, | ||
mcc: MessagesControllerComponents)(implicit appConfig: AppConfig, ec: ExecutionContext) | ||
extends FrontendController(mcc) with I18nSupport { | ||
|
||
def generateInputText(form: Form[InterimRentSetByTheCourtForm], inputFieldName: String)(implicit messages: Messages): HtmlFormat.Appendable = { | ||
inputText( | ||
form = form, | ||
id = inputFieldName, | ||
name = inputFieldName, | ||
label = messages(s"interimRentSetByTheCourt.label.1"), | ||
headingMessageArgs = Seq("govuk-fieldset__legend govuk-fieldset__legend--s"), | ||
isPageHeading = true, | ||
isVisible = true, | ||
classes = Some("govuk-input govuk-input--width-5"), | ||
prefix = Some(PrefixOrSuffix(content = Text("£"))) | ||
) | ||
} | ||
|
||
def show: Action[AnyContent] = { | ||
(authenticate andThen hasLinkedProperties).async { implicit request => | ||
request.propertyLinking.map(property => | ||
Future.successful(Ok(interimRentSetByTheCourtView( | ||
form = form, | ||
propertyAddress = property.addressFull, | ||
interimAmount = generateInputText(form, "interimAmount") | ||
)))).getOrElse(throw new NotFoundException("Couldn't find property in mongo")) | ||
} | ||
} | ||
|
||
def submit: Action[AnyContent] = | ||
(authenticate andThen hasLinkedProperties).async { implicit request => | ||
form.bindFromRequest().fold( | ||
formWithErrors => { | ||
val correctedFormErrors = formWithErrors.errors.map { formError => | ||
(formError.key, formError.messages) match | ||
case (key, messages) if messages.contains("interimRentSetByTheCourt.startDate.before.1900.error") || | ||
messages.contains("interimRentSetByTheCourt.year.required.error") => | ||
formError.copy(key = "date.year") | ||
case ("date", messages) => | ||
formError.copy(key = "date.month") | ||
case _ => | ||
formError | ||
} | ||
val formWithCorrectedErrors = formWithErrors.copy(errors = correctedFormErrors) | ||
request.propertyLinking.map(property => | ||
Future.successful(BadRequest(interimRentSetByTheCourtView( | ||
form = formWithCorrectedErrors, | ||
propertyAddress = property.addressFull, | ||
interimAmount = generateInputText(formWithCorrectedErrors, "interimAmount") | ||
)))).getOrElse(throw new NotFoundException("Couldn't find property in mongo")) | ||
}, | ||
interimRent => | ||
raldRepo.insertInterimRentSetByTheCourt( | ||
credId = CredId(request.credId.getOrElse("")), | ||
amount = interimRent.amount, | ||
date = interimRent.date.makeString | ||
) | ||
Future.successful(Redirect(routes.CheckRentFreePeriodController.show.url)) | ||
) | ||
} | ||
} |
This file contains hidden or 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
27 changes: 27 additions & 0 deletions
27
app/uk/gov/hmrc/ngrraldfrontend/models/InterimRentSetByTheCourt.scala
This file contains hidden or 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,27 @@ | ||
/* | ||
* Copyright 2025 HM Revenue & Customs | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package uk.gov.hmrc.ngrraldfrontend.models | ||
|
||
import play.api.libs.json.{Json, OFormat} | ||
|
||
case class InterimRentSetByTheCourt(amount: String, | ||
date: String | ||
) | ||
|
||
object InterimRentSetByTheCourt { | ||
implicit val format: OFormat[InterimRentSetByTheCourt] = Json.format[InterimRentSetByTheCourt] | ||
} |
This file contains hidden or 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,55 @@ | ||
/* | ||
* Copyright 2025 HM Revenue & Customs | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package uk.gov.hmrc.ngrraldfrontend.models | ||
|
||
import play.api.data.Forms.{mapping, text} | ||
import play.api.data.Mapping | ||
import play.api.libs.json.{Json, OFormat} | ||
|
||
import java.time.LocalDate | ||
import java.time.format.{DateTimeFormatter, TextStyle} | ||
import java.util.Locale | ||
|
||
final case class NGRMonthYear(month: String, year: String) { | ||
def makeString: String = { | ||
val monthStr = f"${month.toIntOption.getOrElse(0)}%02d" | ||
s"$year-$monthStr" | ||
anna-shen-hmrc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
object NGRMonthYear { | ||
implicit val format: OFormat[NGRMonthYear] = Json.format[NGRMonthYear] | ||
|
||
def formatDate(dateString: String): String = { | ||
val date = LocalDate.parse(dateString) | ||
val outputFormatter = DateTimeFormatter.ofPattern("MMMM yyyy", Locale.UK) | ||
date.format(outputFormatter) | ||
} | ||
|
||
|
||
def unapply(ngrMonthYear: NGRMonthYear): Option[(String, String)] = | ||
Some(ngrMonthYear.month, ngrMonthYear.year) | ||
} | ||
|
||
trait MonthYearMappings { | ||
def monthYearMapping: Mapping[NGRMonthYear] = { | ||
mapping( | ||
"month" -> text().transform(_.strip(), identity), | ||
"year" -> text().transform(_.strip(), identity), | ||
)(NGRMonthYear.apply)(NGRMonthYear.unapply) | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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
73 changes: 73 additions & 0 deletions
73
app/uk/gov/hmrc/ngrraldfrontend/models/forms/InterimRentSetByTheCourtForm.scala
This file contains hidden or 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,73 @@ | ||
/* | ||
* Copyright 2025 HM Revenue & Customs | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package uk.gov.hmrc.ngrraldfrontend.models.forms | ||
|
||
import play.api.data.* | ||
import play.api.data.Forms.* | ||
import play.api.data.format.Formatter | ||
import play.api.libs.json.{Json, OFormat} | ||
import uk.gov.hmrc.ngrraldfrontend.models.* | ||
|
||
import scala.math.BigDecimal.RoundingMode | ||
import scala.util.Try | ||
|
||
|
||
final case class InterimRentSetByTheCourtForm(amount: BigDecimal, date: NGRMonthYear) | ||
|
||
object InterimRentSetByTheCourtForm extends CommonFormValidators with MonthYearMappings { | ||
implicit val format: OFormat[InterimRentSetByTheCourtForm] = Json.format[InterimRentSetByTheCourtForm] | ||
|
||
private lazy val howMuchEmptyError = "interimRentSetByTheCourt.interimAmount.required.error" | ||
private lazy val howMuchMaxError = "interimRentSetByTheCourt.interimAmount.tooLarge.error" | ||
private lazy val howMuchFormatError = "interimRentSetByTheCourt.interimAmount.format.error" | ||
|
||
def unapply(interimRentSetByTheCourtForm: InterimRentSetByTheCourtForm): Option[(BigDecimal, NGRMonthYear)] = Some(interimRentSetByTheCourtForm.amount, interimRentSetByTheCourtForm.date) | ||
|
||
private def errorKeys(whichDate: String): Map[DateErrorKeys, String] = Map( | ||
anna-shen-hmrc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Required -> s"$whichDate.required.error", | ||
Month -> s"$whichDate.month.required.error", | ||
Year -> s"$whichDate.year.required.error" | ||
) | ||
|
||
val form: Form[InterimRentSetByTheCourtForm] = Form( | ||
mapping( | ||
"interimAmount" -> text() | ||
.transform[String](_.strip().replaceAll("[£|,|\\s]", ""), identity) | ||
.verifying( | ||
firstError( | ||
isNotEmpty("interimAmount", howMuchEmptyError), | ||
regexp(amountRegex.pattern(), howMuchFormatError) | ||
) | ||
) | ||
.transform[BigDecimal](BigDecimal(_).setScale(2, RoundingMode.HALF_UP), _.toString) | ||
.verifying( | ||
maximumValue[BigDecimal](BigDecimal("9999999.99"), howMuchMaxError) | ||
), | ||
"date" -> monthYearMapping | ||
.verifying( | ||
firstError( | ||
isMonthYearEmpty(errorKeys("interimRentSetByTheCourt")), | ||
isMonthYearValid("interimRentSetByTheCourt.monthYear.format.error"), | ||
isMonthYearAfter1900("interimRentSetByTheCourt.startDate.before.1900.error") | ||
) | ||
), | ||
)(InterimRentSetByTheCourtForm.apply)(InterimRentSetByTheCourtForm.unapply) | ||
) | ||
|
||
} | ||
|
||
|
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please add spec for this?