Skip to content

Commit

Permalink
[MIBM-112][JR] /remove-goods and /goods-removed (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanrowe authored Oct 9, 2020
1 parent e5f4d21 commit b4bae9d
Show file tree
Hide file tree
Showing 47 changed files with 690 additions and 328 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2020 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.merchandiseinbaggagefrontend.controllers

import play.api.mvc.WrappedRequest
import uk.gov.hmrc.merchandiseinbaggagefrontend.model.core.GoodsEntry

final class DeclarationGoodsRequest[A](declarationJourneyRequest: DeclarationJourneyRequest[A], val goodsEntry: GoodsEntry)
extends WrappedRequest[A](declarationJourneyRequest) {

val declarationJourney = declarationJourneyRequest.declarationJourney
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@ class DeclarationJourneyActionProvider @Inject()(defaultActionBuilder: DefaultAc
val journeyAction: ActionBuilder[DeclarationJourneyRequest, AnyContent] =
defaultActionBuilder andThen journeyActionRefiner

def goodsAction(idx: Int): ActionBuilder[DeclarationGoodsRequest, AnyContent] =
defaultActionBuilder andThen journeyActionRefiner andThen goodsActionRefiner(idx)

def invalidRequest: Result = Redirect(routes.InvalidRequestController.onPageLoad())

def invalidRequestF: Future[Result] = Future.successful(invalidRequest)

def journeyActionRefiner: ActionRefiner[Request, DeclarationJourneyRequest] =
private def journeyActionRefiner: ActionRefiner[Request, DeclarationJourneyRequest] =
new ActionRefiner[Request, DeclarationJourneyRequest] {

override protected def refine[A](request: Request[A]): Future[Either[Result, DeclarationJourneyRequest[A]]] = {
Expand All @@ -49,6 +52,19 @@ class DeclarationJourneyActionProvider @Inject()(defaultActionBuilder: DefaultAc
}
}

override protected def executionContext: ExecutionContext = ec
}

private def goodsActionRefiner(idx: Int): ActionRefiner[DeclarationJourneyRequest, DeclarationGoodsRequest] =
new ActionRefiner[DeclarationJourneyRequest, DeclarationGoodsRequest] {

override protected def refine[A](request: DeclarationJourneyRequest[A]): Future[Either[Result, DeclarationGoodsRequest[A]]] = {
request.declarationJourney.goodsEntries.entries.lift(idx - 1) match {
case None => Future successful Left(invalidRequest)
case Some(goodsEntry) => Future successful Right( new DeclarationGoodsRequest(request, goodsEntry))
}
}

override protected def executionContext: ExecutionContext = ec
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
package uk.gov.hmrc.merchandiseinbaggagefrontend.controllers

import play.api.i18n.Messages
import play.api.mvc.{Action, AnyContent, Request}
import play.api.mvc.{Action, AnyContent, Request, Result}
import uk.gov.hmrc.merchandiseinbaggagefrontend.model.core.GoodsEntry
import uk.gov.hmrc.play.bootstrap.frontend.controller.FrontendBaseController

import scala.concurrent.Future

trait DeclarationJourneyController extends FrontendBaseController {
implicit def messages(implicit request: Request[_]): Messages = controllerComponents.messagesApi.preferred(request)

Expand All @@ -34,6 +37,12 @@ trait IndexedDeclarationJourneyController extends FrontendBaseController {
implicit def messages(implicit request: Request[_]): Messages = controllerComponents.messagesApi.preferred(request)

def onPageLoad(idx: Int): Action[AnyContent]

def withGoodsCategory(goodsEntry: GoodsEntry)(f: String => Future[Result]): Future[Result] =
goodsEntry.maybeCategoryQuantityOfGoods match {
case Some(c) => f(c.category)
case None => Future successful Redirect(routes.InvalidRequestController.onPageLoad())
}
}

trait IndexedDeclarationJourneyUpdateController extends IndexedDeclarationJourneyController {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
package uk.gov.hmrc.merchandiseinbaggagefrontend.controllers

import javax.inject.{Inject, Singleton}
import play.api.data.Form
import play.api.mvc.{Action, AnyContent, MessagesControllerComponents}
import uk.gov.hmrc.merchandiseinbaggagefrontend.config.AppConfig
import uk.gov.hmrc.merchandiseinbaggagefrontend.forms.ExciseAndRestrictedGoodsFormProvider
import uk.gov.hmrc.merchandiseinbaggagefrontend.forms.ExciseAndRestrictedGoodsForm.form
import uk.gov.hmrc.merchandiseinbaggagefrontend.repositories.DeclarationJourneyRepository
import uk.gov.hmrc.merchandiseinbaggagefrontend.views.html.ExciseAndRestrictedGoodsView

Expand All @@ -29,14 +28,11 @@ import scala.concurrent.{ExecutionContext, Future}
@Singleton
class ExciseAndRestrictedGoodsController @Inject()(override val controllerComponents: MessagesControllerComponents,
actionProvider: DeclarationJourneyActionProvider,
formProvider: ExciseAndRestrictedGoodsFormProvider,
repo: DeclarationJourneyRepository,
view: ExciseAndRestrictedGoodsView)
(implicit ec: ExecutionContext, appConfig: AppConfig)
extends DeclarationJourneyUpdateController {

val form: Form[Boolean] = formProvider()

val onPageLoad: Action[AnyContent] = actionProvider.journeyAction { implicit request =>
Ok(view(request.declarationJourney.maybeExciseOrRestrictedGoods.fold(form)(form.fill)))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,22 @@
package uk.gov.hmrc.merchandiseinbaggagefrontend.controllers

import javax.inject.{Inject, Singleton}
import play.api.data.Form
import play.api.mvc.{Action, AnyContent, MessagesControllerComponents}
import uk.gov.hmrc.merchandiseinbaggagefrontend.config.AppConfig
import uk.gov.hmrc.merchandiseinbaggagefrontend.forms.GoodsDestinationFormProvider
import uk.gov.hmrc.merchandiseinbaggagefrontend.model.core.GoodsDestination
import uk.gov.hmrc.merchandiseinbaggagefrontend.forms.GoodsDestinationForm.form
import uk.gov.hmrc.merchandiseinbaggagefrontend.repositories.DeclarationJourneyRepository
import uk.gov.hmrc.merchandiseinbaggagefrontend.views.html.GoodsDestinationView

import scala.concurrent.{ExecutionContext, Future}

@Singleton
class GoodsDestinationController @Inject()(
override val controllerComponents: MessagesControllerComponents,
actionProvider: DeclarationJourneyActionProvider,
formProvider: GoodsDestinationFormProvider,
repo: DeclarationJourneyRepository,
view: GoodsDestinationView
override val controllerComponents: MessagesControllerComponents,
actionProvider: DeclarationJourneyActionProvider,
repo: DeclarationJourneyRepository,
view: GoodsDestinationView
)(implicit ec: ExecutionContext, appConfig: AppConfig) extends DeclarationJourneyUpdateController {

val form: Form[GoodsDestination] = formProvider()

val onPageLoad: Action[AnyContent] = actionProvider.journeyAction { implicit request =>
Ok(view(request.declarationJourney.maybeGoodsDestination.fold(form)(form.fill)))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2020 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.merchandiseinbaggagefrontend.controllers

import javax.inject.{Inject, Singleton}
import play.api.mvc.{Action, AnyContent, MessagesControllerComponents}
import uk.gov.hmrc.merchandiseinbaggagefrontend.config.AppConfig
import uk.gov.hmrc.merchandiseinbaggagefrontend.views.html.GoodsRemovedView

@Singleton
class GoodsRemovedController @Inject()(override val controllerComponents: MessagesControllerComponents,
actionProvider: DeclarationJourneyActionProvider,
view: GoodsRemovedView)(implicit val appConfig: AppConfig)
extends DeclarationJourneyController {

val onPageLoad: Action[AnyContent] = actionProvider.journeyAction { implicit request =>
Ok(view())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@
package uk.gov.hmrc.merchandiseinbaggagefrontend.controllers

import javax.inject.{Inject, Singleton}
import play.api.data.Form
import play.api.mvc.{Action, AnyContent, MessagesControllerComponents}
import uk.gov.hmrc.merchandiseinbaggagefrontend.config.AppConfig
import uk.gov.hmrc.merchandiseinbaggagefrontend.forms.GoodsVatRateFormProvider
import uk.gov.hmrc.merchandiseinbaggagefrontend.model.core.{GoodsEntries, GoodsVatRate}
import uk.gov.hmrc.merchandiseinbaggagefrontend.forms.GoodsVatRateForm.form
import uk.gov.hmrc.merchandiseinbaggagefrontend.repositories.DeclarationJourneyRepository
import uk.gov.hmrc.merchandiseinbaggagefrontend.views.html.GoodsVatRateView

Expand All @@ -31,48 +29,39 @@ import scala.concurrent.{ExecutionContext, Future}
class GoodsVatRateController @Inject()(
override val controllerComponents: MessagesControllerComponents,
actionProvider: DeclarationJourneyActionProvider,
formProvider: GoodsVatRateFormProvider,
repo: DeclarationJourneyRepository,
view: GoodsVatRateView)
(implicit ec: ExecutionContext, appConfig: AppConfig)
extends IndexedDeclarationJourneyUpdateController {

val form: Form[GoodsVatRate] = formProvider()
def onPageLoad(idx: Int): Action[AnyContent] = actionProvider.goodsAction(idx).async { implicit request =>
withGoodsCategory(request.goodsEntry) { category =>
val preparedForm = request.goodsEntry.maybeGoodsVatRate.fold(form)(form.fill)

def onPageLoad(idx: Int): Action[AnyContent] = actionProvider.journeyAction { implicit request =>
request.declarationJourney.goodsEntries.entries.lift(idx - 1).fold(actionProvider.invalidRequest) { goodsEntry =>
goodsEntry.maybeCategoryQuantityOfGoods.fold(actionProvider.invalidRequest) { categoryQuantityOfGoods =>
val preparedForm = goodsEntry.maybeGoodsVatRate.fold(form)(form.fill)

Ok(view(preparedForm, idx, categoryQuantityOfGoods.category))
}
Future successful Ok(view(preparedForm, idx, category))
}
}

def onSubmit(idx: Int): Action[AnyContent] = actionProvider.journeyAction.async { implicit request =>
request.declarationJourney.goodsEntries.entries.lift(idx - 1).fold(actionProvider.invalidRequestF) { goodsEntry =>
goodsEntry.maybeCategoryQuantityOfGoods.fold(actionProvider.invalidRequestF) { categoryQuantityOfGoods =>
form
.bindFromRequest()
.fold(
formWithErrors =>
Future.successful(BadRequest(view(formWithErrors, idx, categoryQuantityOfGoods.category))),
goodsVatRate => {
val updatedGoodsEntries =
request.declarationJourney.goodsEntries.entries.updated(
idx - 1,
goodsEntry.copy(maybeGoodsVatRate = Some(goodsVatRate)))

repo.upsert(
request.declarationJourney.copy(
goodsEntries = GoodsEntries(updatedGoodsEntries)
def onSubmit(idx: Int): Action[AnyContent] = actionProvider.goodsAction(idx).async { implicit request =>
withGoodsCategory(request.goodsEntry) { category =>
form
.bindFromRequest()
.fold(
formWithErrors =>
Future.successful(BadRequest(view(formWithErrors, idx, category))),
goodsVatRate => {
repo.upsert(
request.declarationJourney.copy(
goodsEntries = request.declarationJourney.goodsEntries.patch(
idx,
request.goodsEntry.copy(maybeGoodsVatRate = Some(goodsVatRate))
)
).map { _ =>
Redirect(routes.SearchGoodsCountryController.onPageLoad(idx))
}
)
).map { _ =>
Redirect(routes.SearchGoodsCountryController.onPageLoad(idx))
}
)
}
}
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@
package uk.gov.hmrc.merchandiseinbaggagefrontend.controllers

import javax.inject.{Inject, Singleton}
import play.api.data.Form
import play.api.mvc.{Action, AnyContent, MessagesControllerComponents}
import uk.gov.hmrc.merchandiseinbaggagefrontend.config.AppConfig
import uk.gov.hmrc.merchandiseinbaggagefrontend.forms.InvoiceNumberForm
import uk.gov.hmrc.merchandiseinbaggagefrontend.model.core.GoodsEntries
import uk.gov.hmrc.merchandiseinbaggagefrontend.forms.InvoiceNumberForm.form
import uk.gov.hmrc.merchandiseinbaggagefrontend.repositories.DeclarationJourneyRepository
import uk.gov.hmrc.merchandiseinbaggagefrontend.views.html.InvoiceNumberView

Expand All @@ -36,45 +34,37 @@ class InvoiceNumberController @Inject()(
)(implicit ec: ExecutionContext, appConfig: AppConfig)
extends IndexedDeclarationJourneyUpdateController {

val form: Form[String] = InvoiceNumberForm.form
def onPageLoad(idx: Int): Action[AnyContent] = actionProvider.goodsAction(idx).async { implicit request =>
withGoodsCategory(request.goodsEntry) { category =>
val preparedForm = request.goodsEntry.maybeInvoiceNumber.fold(form)(form.fill)

def onPageLoad(idx: Int): Action[AnyContent] = actionProvider.journeyAction { implicit request =>
request.declarationJourney.goodsEntries.entries.lift(idx - 1).fold(actionProvider.invalidRequest) { goodsEntry =>
goodsEntry.maybeCategoryQuantityOfGoods.fold(actionProvider.invalidRequest) { categoryQuantityOfGoods =>
val preparedForm = goodsEntry.maybeInvoiceNumber.fold(form)(form.fill)

Ok(view(preparedForm, idx, categoryQuantityOfGoods.category))
}
Future successful Ok(view(preparedForm, idx, category))
}
}

def onSubmit(idx: Int): Action[AnyContent] = actionProvider.journeyAction.async { implicit request =>
request.declarationJourney.goodsEntries.entries.lift(idx - 1).fold(actionProvider.invalidRequestF) { goodsEntry =>
goodsEntry.maybeCategoryQuantityOfGoods.fold(actionProvider.invalidRequestF) { categoryQuantityOfGoods =>
form
.bindFromRequest()
.fold(
formWithErrors =>
Future.successful(BadRequest(view(formWithErrors, idx, categoryQuantityOfGoods.category))),
invoiceNumber => {
val updatedGoodsEntries =
request.declarationJourney.goodsEntries.entries.updated(
idx - 1,
goodsEntry.copy(
def onSubmit(idx: Int): Action[AnyContent] = actionProvider.goodsAction(idx).async { implicit request =>
withGoodsCategory(request.goodsEntry) { category =>
form
.bindFromRequest()
.fold(
formWithErrors =>
Future.successful(BadRequest(view(formWithErrors, idx, category))),
invoiceNumber => {
repo.upsert(
request.declarationJourney.copy(
goodsEntries = request.declarationJourney.goodsEntries.patch(
idx,
request.goodsEntry.copy(
maybeInvoiceNumber = Some(invoiceNumber),
maybeTaxDue = Some(BigDecimal(-999.99))
))

repo.upsert(
request.declarationJourney.copy(
goodsEntries = GoodsEntries(updatedGoodsEntries)
)
)
).map { _ =>
Redirect(routes.ReviewGoodsController.onPageLoad())
}
)
).map { _ =>
Redirect(routes.ReviewGoodsController.onPageLoad())
}
)
}
}
)
}
}

Expand Down
Loading

0 comments on commit b4bae9d

Please sign in to comment.