|
| 1 | +/* |
| 2 | + * Copyright 2026 HM Revenue & Customs |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package controllers.land |
| 18 | + |
| 19 | +import connectors.StampDutyLandTaxConnector |
| 20 | +import controllers.actions.* |
| 21 | +import forms.land.RemoveLandFormProvider |
| 22 | +import models.land.DeleteLandRequest |
| 23 | +import models.{Land, ReturnVersionUpdateRequest} |
| 24 | +import pages.land.RemoveLandPage |
| 25 | +import play.api.data.Form |
| 26 | +import play.api.i18n.{I18nSupport, MessagesApi} |
| 27 | +import play.api.mvc.{Action, AnyContent, MessagesControllerComponents} |
| 28 | +import uk.gov.hmrc.play.bootstrap.frontend.controller.FrontendBaseController |
| 29 | +import views.html.land.RemoveLandView |
| 30 | + |
| 31 | +import javax.inject.Inject |
| 32 | +import scala.concurrent.{ExecutionContext, Future} |
| 33 | + |
| 34 | +class RemoveLandController @Inject()( |
| 35 | + override val messagesApi: MessagesApi, |
| 36 | + identify: IdentifierAction, |
| 37 | + getData: DataRetrievalAction, |
| 38 | + requireData: DataRequiredAction, |
| 39 | + formProvider: RemoveLandFormProvider, |
| 40 | + val controllerComponents: MessagesControllerComponents, |
| 41 | + backendConnector: StampDutyLandTaxConnector, |
| 42 | + view: RemoveLandView |
| 43 | + )(implicit ec: ExecutionContext) extends FrontendBaseController with I18nSupport { |
| 44 | + |
| 45 | + val form: Form[Boolean] = formProvider() |
| 46 | + |
| 47 | + def onPageLoad(): Action[AnyContent] = (identify andThen getData andThen requireData) { |
| 48 | + implicit request => |
| 49 | + |
| 50 | + //TODO update to take removeLandId from user answers overview page DTR-2498. |
| 51 | + val removeLandId = "LND-REF-001" |
| 52 | + // request.userAnswers.get(LandOverviewRemovePage).map { removeLandId => |
| 53 | + |
| 54 | + val maybeReturnLandToRemove = request.userAnswers.fullReturn.flatMap(_.land.flatMap(_.find(_.landResourceRef.contains(removeLandId)))) |
| 55 | + val addressLine1 = maybeReturnLandToRemove.flatMap(_.address1).getOrElse("") |
| 56 | + |
| 57 | + maybeReturnLandToRemove match { |
| 58 | + case None => |
| 59 | + Redirect(controllers.routes.JourneyRecoveryController.onPageLoad()) |
| 60 | + |
| 61 | + case Some(land) => |
| 62 | + val preparedForm = request.userAnswers.get(RemoveLandPage) match { |
| 63 | + case None => form |
| 64 | + case Some(value) => form.fill(value) |
| 65 | + } |
| 66 | + Ok(view(preparedForm, addressLine1)) |
| 67 | + }//TODO remove bellow comments post overview page DTR-2498 implementation. |
| 68 | + /*.getOrElse( |
| 69 | + Redirect(controllers.land.routes.LandOverviewController.onPageLoad()) |
| 70 | + )*/ |
| 71 | + } |
| 72 | + |
| 73 | + def onSubmit(): Action[AnyContent] = (identify andThen getData andThen requireData).async { |
| 74 | + implicit request => |
| 75 | + |
| 76 | + //TODO update to take ID from user answers overview page DTR-2498 |
| 77 | + val removeLandId = "LND-REF-001" |
| 78 | + // request.userAnswers.get(LandOverviewRemovePage).map { removeLandId => |
| 79 | + val maybeLandToDelete: Option[Land] = for { |
| 80 | + fullReturn <- request.userAnswers.fullReturn |
| 81 | + allLands <- fullReturn.land |
| 82 | + returnLandToDelete <- allLands.find(_.landResourceRef.contains(removeLandId)) |
| 83 | + } yield returnLandToDelete |
| 84 | + |
| 85 | + maybeLandToDelete match { |
| 86 | + case None => |
| 87 | + Future.successful(Redirect(controllers.routes.JourneyRecoveryController.onPageLoad())) |
| 88 | + |
| 89 | + case Some(maybeLandToDelete) => |
| 90 | + val addressLine1 = maybeLandToDelete.address1.getOrElse("") |
| 91 | + form.bindFromRequest().fold( |
| 92 | + formWithErrors => |
| 93 | + Future.successful(BadRequest(view(formWithErrors, addressLine1))), |
| 94 | + |
| 95 | + value => |
| 96 | + if (value) { |
| 97 | + (for { |
| 98 | + updateReturnVersionRequest <- ReturnVersionUpdateRequest.from(request.userAnswers) |
| 99 | + returnVersion <- backendConnector.updateReturnVersion(updateReturnVersionRequest) |
| 100 | + deleteLandRequest <- DeleteLandRequest.from(request.userAnswers, removeLandId) if returnVersion.newVersion.isDefined |
| 101 | + deleteLandReturn <- backendConnector.deleteLand(deleteLandRequest) if returnVersion.newVersion.isDefined |
| 102 | + } yield { |
| 103 | + //Redirect(controllers.land.routes.LandOverviewController.onPageLoad()).flashing("landDeleted" -> addressLine1) |
| 104 | + Redirect(controllers.land.routes.RemoveLandController.onPageLoad()).flashing("landDeleted" -> addressLine1) //TODO update to land overview page DTR-2498 |
| 105 | + }).recover { |
| 106 | + case _ => |
| 107 | + // Redirect(controllers.land.routes.LandOverviewController.onPageLoad()) |
| 108 | + Redirect(controllers.routes.ReturnTaskListController.onPageLoad()) //TODO update to land overview page DTR-2498 |
| 109 | + |
| 110 | + } |
| 111 | + } else { |
| 112 | + //Future.successful(Redirect(controllers.land.routes.LandOverviewController.onPageLoad())) |
| 113 | + Future.successful(Redirect(controllers.routes.ReturnTaskListController.onPageLoad())) //TODO update to land overview page DTR-2498 |
| 114 | + } |
| 115 | + ) |
| 116 | + }//TODO remove bellow comments post overview page DTR-2498 implementation. |
| 117 | + /*.getOrElse( |
| 118 | + Redirect(controllers.land.routes.LandOverviewController.onPageLoad()) |
| 119 | + )*/ |
| 120 | + } |
| 121 | +} |
0 commit comments