Skip to content

Commit

Permalink
[MIBM-97][JR] session init (#15)
Browse files Browse the repository at this point in the history
[MIBM-97][JR] session init and  all declaration pages after /start require a session id and saved journey
  • Loading branch information
jordanrowe authored Sep 29, 2020
1 parent bf3f11f commit 9b50f7a
Show file tree
Hide file tree
Showing 11 changed files with 293 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,14 @@ 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.CannotUseServiceView
import uk.gov.hmrc.play.bootstrap.frontend.controller.FrontendBaseController

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


def onPageLoad(): Action[AnyContent] = Action { implicit request =>
val onPageLoad: Action[AnyContent] = actionProvider.journeyAction { implicit request =>
Ok(view())
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package uk.gov.hmrc.merchandiseinbaggagefrontend.controllers

import com.google.inject.Inject
import controllers.Assets.Redirect
import play.api.mvc._
import uk.gov.hmrc.http.SessionKeys
import uk.gov.hmrc.merchandiseinbaggagefrontend.model.declaration.SessionId
Expand All @@ -35,11 +36,13 @@ class DeclarationJourneyActionProvider @Inject()(defaultActionBuilder: DefaultAc
override protected def refine[A](request: Request[A]): Future[Either[Result, DeclarationJourneyRequest[A]]] = {
def error = throw new RuntimeException("Unable to retrieve declaration journey")

request.session.get(SessionKeys.sessionId).fold(error){ sessionId =>
repo.findBySessionId(SessionId(sessionId)).map{
case Some(declarationJourney) => Right(new DeclarationJourneyRequest(declarationJourney, request))
case _ => Left(error)
}
request.session.get(SessionKeys.sessionId) match {
case None => Future successful Left(Redirect(routes.StartController.onPageLoad()))
case Some(sessionId) =>
repo.findBySessionId(SessionId(sessionId)).map{
case Some(declarationJourney) => Right(new DeclarationJourneyRequest(declarationJourney, request))
case _ => Left(error)
}
}
}

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

import javax.inject.Inject
import play.api.mvc.{Action, AnyContent, MessagesControllerComponents}
import play.api.i18n.Messages
import play.api.mvc.{Action, AnyContent, MessagesControllerComponents, Request}
import uk.gov.hmrc.merchandiseinbaggagefrontend.config.AppConfig
import uk.gov.hmrc.merchandiseinbaggagefrontend.views.html.SkeletonPage
import uk.gov.hmrc.play.bootstrap.frontend.controller.FrontendController
Expand All @@ -29,59 +30,62 @@ import scala.concurrent.ExecutionContext
*
* It is envisaged this controller will be re-factored away action-by-action as we build out the journey
*/
class SkeletonJourneyController @Inject()(mcc: MessagesControllerComponents, page: SkeletonPage)
class SkeletonJourneyController @Inject()(mcc: MessagesControllerComponents,
actionProvider: DeclarationJourneyActionProvider,
page: SkeletonPage)
(implicit val ec: ExecutionContext, appConfig: AppConfig)
extends FrontendController(mcc) {
implicit def messages(implicit request: Request[_]): Messages = controllerComponents.messagesApi.preferred(request)

val selectDeclarationType: Action[AnyContent] = Action { implicit request =>
val selectDeclarationType: Action[AnyContent] = actionProvider.journeyAction { implicit request =>
Ok(page(s"selectDeclarationType.title", routes.ExciseAndRestrictedGoodsController.onPageLoad()))
}

val valueWeightOfGoods: Action[AnyContent] = Action { implicit request =>
val valueWeightOfGoods: Action[AnyContent] = actionProvider.journeyAction { implicit request =>
Ok(page(s"valueWeightOfGoods.title", routes.SkeletonJourneyController.searchGoods()))
}

val searchGoods: Action[AnyContent] = Action { implicit request =>
val searchGoods: Action[AnyContent] = actionProvider.journeyAction { implicit request =>
Ok(page(s"searchGoods.title", routes.SkeletonJourneyController.searchGoodsCountry()))
}

val searchGoodsCountry: Action[AnyContent] = Action { implicit request =>
val searchGoodsCountry: Action[AnyContent] = actionProvider.journeyAction { implicit request =>
Ok(page(s"searchGoodsCountry.title", routes.SkeletonJourneyController.purchaseDetails()))
}

val purchaseDetails: Action[AnyContent] = Action { implicit request =>
val purchaseDetails: Action[AnyContent] = actionProvider.journeyAction { implicit request =>
Ok(page(s"purchaseDetails.title", routes.SkeletonJourneyController.reviewGoods()))
}

val reviewGoods: Action[AnyContent] = Action { implicit request =>
val reviewGoods: Action[AnyContent] = actionProvider.journeyAction { implicit request =>
Ok(page(s"reviewGoods.title", routes.SkeletonJourneyController.taxCalculation()))
}

val taxCalculation: Action[AnyContent] = Action { implicit request =>
val taxCalculation: Action[AnyContent] = actionProvider.journeyAction { implicit request =>
Ok(page(s"taxCalculation.title", routes.SkeletonJourneyController.customsAgent()))
}

val customsAgent: Action[AnyContent] = Action { implicit request =>
val customsAgent: Action[AnyContent] = actionProvider.journeyAction { implicit request =>
Ok(page(s"customsAgent.title", routes.SkeletonJourneyController.traderDetails()))
}

val traderDetails: Action[AnyContent] = Action { implicit request =>
val traderDetails: Action[AnyContent] = actionProvider.journeyAction { implicit request =>
Ok(page(s"traderDetails.title", routes.SkeletonJourneyController.enterTraderAddress()))
}

val enterTraderAddress: Action[AnyContent] = Action { implicit request =>
val enterTraderAddress: Action[AnyContent] = actionProvider.journeyAction { implicit request =>
Ok(page(s"enterTraderAddress.title", routes.SkeletonJourneyController.selectTraderAddress()))
}

val selectTraderAddress: Action[AnyContent] = Action { implicit request =>
val selectTraderAddress: Action[AnyContent] = actionProvider.journeyAction { implicit request =>
Ok(page(s"selectTraderAddress.title", routes.SkeletonJourneyController.enterEoriNumber()))
}

val enterEoriNumber: Action[AnyContent] = Action { implicit request =>
val enterEoriNumber: Action[AnyContent] = actionProvider.journeyAction { implicit request =>
Ok(page(s"enterEoriNumber.title", routes.SkeletonJourneyController.traderJourneyDetails()))
}

val traderJourneyDetails: Action[AnyContent] = Action { implicit request =>
val traderJourneyDetails: Action[AnyContent] = actionProvider.journeyAction { implicit request =>
Ok(page(s"traderJourneyDetails.title", routes.CheckYourAnswersController.onPageLoad()))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,32 @@ package uk.gov.hmrc.merchandiseinbaggagefrontend.controllers

import javax.inject.{Inject, Singleton}
import play.api.mvc.{Action, AnyContent, MessagesControllerComponents}
import uk.gov.hmrc.http.SessionKeys.sessionId
import uk.gov.hmrc.merchandiseinbaggagefrontend.config.AppConfig
import uk.gov.hmrc.merchandiseinbaggagefrontend.model.declaration.{DeclarationJourney, SessionId}
import uk.gov.hmrc.merchandiseinbaggagefrontend.repositories.DeclarationJourneyRepository
import uk.gov.hmrc.merchandiseinbaggagefrontend.views.html.StartView
import uk.gov.hmrc.play.bootstrap.frontend.controller.FrontendBaseController

import scala.concurrent.ExecutionContext

@Singleton
class StartController @Inject()(
override val controllerComponents: MessagesControllerComponents,
view: StartView)(implicit val appConfig: AppConfig) extends FrontendBaseController {
repo: DeclarationJourneyRepository,
view: StartView)(implicit val ec: ExecutionContext, appConfig: AppConfig) extends FrontendBaseController {

def onPageLoad(): Action[AnyContent] = Action { implicit request =>
Ok(view())
}

def onSubmit(): Action[AnyContent] = Action.async {
val declarationJourney = DeclarationJourney(sessionId = SessionId())

repo.insert(declarationJourney).map { _ =>
Redirect(routes.SkeletonJourneyController.selectDeclarationType())
.withSession((sessionId, declarationJourney.sessionId.value))
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
*@

@import uk.gov.hmrc.govukfrontend.views.html.components
@import uk.gov.hmrc.merchandiseinbaggagefrontend.controllers.routes._

@this(
layout: Layout,
formHelper: FormWithCSRF,
h1: components.h1,
h4: components.h4,
p: components.p,
Expand Down Expand Up @@ -66,7 +68,9 @@

@p(Html(messages("start.p1")))

@button("site.start", Some("/merchandise-in-baggage/select-declaration-type"))
@formHelper(action = StartController.onSubmit(), 'autoComplete -> "off", 'novalidate -> "novalidate") {
@button("site.start")
}

</div>

Expand Down
1 change: 1 addition & 0 deletions conf/app.routes
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ GET /trader-journey-details uk.gov.hmrc.merchandiseinbaggagefronten

# Start
GET /start uk.gov.hmrc.merchandiseinbaggagefrontend.controllers.StartController.onPageLoad()
POST /start uk.gov.hmrc.merchandiseinbaggagefrontend.controllers.StartController.onSubmit()

# ExciseAndRestrictedGoods
GET /excise-and-restricted-goods uk.gov.hmrc.merchandiseinbaggagefrontend.controllers.ExciseAndRestrictedGoodsController.onPageLoad
Expand Down
4 changes: 2 additions & 2 deletions conf/messages
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ goodsDestination.error.required = Select one of the options below
# ValueWeightOfGoods
valueWeightOfGoods.ni.title = Is the total value of the goods over £873 or 1000 kilograms (kg)?
valueWeightOfGoods.gb.title = Is the total value of the goods over £1500 or 1000 kilograms (kg)?
valueWeightOfGoods.ni.title = Is the total value of the goods over £873 or 1000 kilograms (kg)?
valueWeightOfGoods.gb.title = Is the total value of the goods over £1500 or 1000 kilograms (kg)?
valueWeightOfGoods.ni.heading = Is the total value of the goods over £873 or 1000 kilograms (kg)?
valueWeightOfGoods.gb.heading = Is the total value of the goods over £1500 or 1000 kilograms (kg)?
valueWeightOfGoods.error.required = Select one of the options below


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,23 @@ import play.api.test.Helpers._
import uk.gov.hmrc.merchandiseinbaggagefrontend.views.html.CannotUseServiceView

class CannotUseServiceControllerSpec extends DeclarationJourneyControllerSpec {
private val view = injector.instanceOf[CannotUseServiceView]
private val actionProvider = injector.instanceOf[DeclarationJourneyActionProvider]
private val controller = new CannotUseServiceController(controllerComponents, actionProvider, view)
private val url = routes.CannotUseServiceController.onPageLoad().url

"onPageLoad" must {
"return OK and render the view" in {
val view = injector.instanceOf[CannotUseServiceView]
val controller = new CannotUseServiceController(controllerComponents, view)
val request = buildGet(routes.CannotUseServiceController.onPageLoad().url)
val result = controller.onPageLoad()(request)
behave like anEndpointRequiringASessionIdAndLinkedDeclarationJourneyToLoad(controller, url)

"return OK and render the view" when{
"a declaration journey has been started" in {
givenADeclarationJourneyIsPersisted(startedDeclarationJourney)
val request = buildGet(url, sessionId)
val result = controller.onPageLoad()(request)

status(result) mustEqual OK
contentAsString(result) mustEqual view()(request, messagesApi.preferred(request), appConfig).toString
status(result) mustEqual OK
contentAsString(result) mustEqual view()(request, messagesApi.preferred(request), appConfig).toString
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import play.api.i18n.MessagesApi
import play.api.mvc.{AnyContentAsEmpty, MessagesControllerComponents}
import play.api.test.CSRFTokenHelper._
import play.api.test.FakeRequest
import play.api.test.Helpers.{GET, POST}
import play.api.test.Helpers._
import uk.gov.hmrc.http.SessionKeys
import uk.gov.hmrc.merchandiseinbaggagefrontend.config.AppConfig
import uk.gov.hmrc.merchandiseinbaggagefrontend.model.declaration.{DeclarationJourney, SessionId}
Expand Down Expand Up @@ -52,15 +52,18 @@ trait DeclarationJourneyControllerSpec extends BaseSpecWithApplication with Core
declarationJourneyRepository.insert(declarationJourney).futureValue

def anEndpointRequiringASessionIdAndLinkedDeclarationJourneyToLoad(controller: DeclarationJourneyController, url: String): Unit = {
"error" when {
"redirect to /start" when {
"no session id is set" in {
givenADeclarationJourneyIsPersisted(startedDeclarationJourney)

intercept[Exception] {
controller.onPageLoad(buildGet(url)).futureValue
}.getMessage mustBe "Unable to retrieve declaration journey"
val result = controller.onPageLoad()(buildGet(url))

status(result) mustBe SEE_OTHER
redirectLocation(result).get mustBe routes.StartController.onPageLoad().url
}
}

"error" when {
"a declaration has not been started" in {
intercept[Exception] {
controller.onPageLoad()(buildGet(url, sessionId)).futureValue
Expand All @@ -70,18 +73,21 @@ trait DeclarationJourneyControllerSpec extends BaseSpecWithApplication with Core
}

def anEndpointRequiringASessionIdAndLinkedDeclarationJourneyToUpdate(controller: DeclarationJourneyUpdateController, url: String): Unit = {
"error" when {
"redirect to /start" when {
"no session id is set" in {
givenADeclarationJourneyIsPersisted(startedDeclarationJourney)

intercept[Exception] {
controller.onPageLoad(buildPost(url)).futureValue
}.getMessage mustBe "Unable to retrieve declaration journey"
val result = controller.onSubmit()(buildPost(url))

status(result) mustBe SEE_OTHER
redirectLocation(result).get mustBe routes.StartController.onPageLoad().url
}
}

"error" when {
"a declaration has not been started" in {
intercept[Exception] {
controller.onPageLoad()(buildPost(url, sessionId)).futureValue
controller.onSubmit()(buildPost(url, sessionId)).futureValue
}.getCause.getMessage mustBe "Unable to retrieve declaration journey"
}
}
Expand Down
Loading

0 comments on commit 9b50f7a

Please sign in to comment.