diff --git a/app/controllers/ConfirmTransferController.scala b/app/controllers/ConfirmTransferController.scala index 4e1c5d56a..559ce2989 100644 --- a/app/controllers/ConfirmTransferController.scala +++ b/app/controllers/ConfirmTransferController.scala @@ -8,8 +8,9 @@ import play.api.data.Form import play.api.data.Forms.{boolean, mapping} import play.api.i18n.{I18nSupport, Lang, Langs} import play.api.mvc._ -import services.Statuses.{CompletedValue, ExportType, FailedValue, InProgressValue} -import services.{ConfirmTransferService, ConsignmentExportService, ConsignmentService, ConsignmentStatusService} +import play.api.mvc.Results._ +import services.Statuses.{ClientChecksType, CompletedValue, ExportType, FailedValue, InProgressValue, SeriesType, TransferAgreementType, UploadType} +import services.{ConfirmTransferService, ConsignmentExportService, ConsignmentService, ConsignmentStatusService, Statuses} import viewsapi.Caching.preventCaching import java.util.UUID @@ -49,19 +50,33 @@ class ConfirmTransferController @Inject() ( implicit request: Request[AnyContent] ): Future[Result] = { consignmentStatusService.getConsignmentStatuses(consignmentId, request.token.bearerAccessToken).flatMap { consignmentStatuses => - val exportTransferStatus = consignmentStatusService.getStatusValues(consignmentStatuses, ExportType).values.headOption.flatten - exportTransferStatus match { - case Some(InProgressValue.value) | Some(CompletedValue.value) | Some(FailedValue.value) => - consignmentService.getConsignmentRef(consignmentId, request.token.bearerAccessToken).map { consignmentRef => - Ok(views.html.transferAlreadyCompleted(consignmentId, consignmentRef, request.token.name)).uncache() - } - case None => - getConsignmentSummary(request, consignmentId).map { consignmentSummary => - httpStatus(views.html.standard.confirmTransfer(consignmentId, consignmentSummary, finalTransferForm, request.token.name)).uncache() + val consignmentStatusValues: Map[Statuses.StatusType, Option[String]] = + consignmentStatusService.getStatusValues(consignmentStatuses, SeriesType, TransferAgreementType, UploadType, ClientChecksType, ExportType) + val exportTransferStatus = consignmentStatusValues.get(ExportType).headOption.flatten + val incompleteStatuses = consignmentStatusValues.filter { case (_, statusValue) => !statusValue.contains(CompletedValue.value) } + + Seq(SeriesType, TransferAgreementType, UploadType, ClientChecksType) + .find(statusType => incompleteStatuses.contains(statusType)) + .map { + case SeriesType => Future(Redirect(routes.SeriesDetailsController.seriesDetails(consignmentId))) + case TransferAgreementType => Future(Redirect(routes.TransferAgreementPart1Controller.transferAgreement(consignmentId))) + case UploadType => Future(Redirect(routes.UploadController.uploadPage(consignmentId))) + case ClientChecksType => Future(Redirect(routes.FileChecksController.fileChecksPage(consignmentId, Some("false")))) + } + .getOrElse( + exportTransferStatus match { + case Some(InProgressValue.value) | Some(CompletedValue.value) | Some(FailedValue.value) => + consignmentService.getConsignmentRef(consignmentId, request.token.bearerAccessToken).map { consignmentRef => + Ok(views.html.transferAlreadyCompleted(consignmentId, consignmentRef, request.token.name)).uncache() + } + case None => + getConsignmentSummary(request, consignmentId).map { consignmentSummary => + httpStatus(views.html.standard.confirmTransfer(consignmentId, consignmentSummary, finalTransferForm, request.token.name)).uncache() + } + case _ => + throw new IllegalStateException(s"Unexpected Export status: $exportTransferStatus for consignment $consignmentId") } - case _ => - throw new IllegalStateException(s"Unexpected Export status: $exportTransferStatus for consignment $consignmentId") - } + ) } } diff --git a/test/controllers/ConfirmTransferControllerSpec.scala b/test/controllers/ConfirmTransferControllerSpec.scala index 7022d6929..b146d77d7 100644 --- a/test/controllers/ConfirmTransferControllerSpec.scala +++ b/test/controllers/ConfirmTransferControllerSpec.scala @@ -66,7 +66,9 @@ class ConfirmTransferControllerSpec extends FrontEndTestHelper { } "ConfirmTransferController GET" should { - "render the confirm transfer page with an authenticated user" in { + "render the series page with an authenticated user if series status is not 'Completed'" in { + val consignmentId = UUID.randomUUID() + val client = new GraphQLConfiguration(app.configuration).getClient[gcs.Data, gcs.Variables]() val controller = instantiateConfirmTransferController(getAuthorisedSecurityComponents) setConsignmentStatusResponse(app.configuration, wiremockServer) @@ -76,6 +78,102 @@ class ConfirmTransferControllerSpec extends FrontEndTestHelper { val dataString: String = data.asJson.printWith(Printer(dropNullValues = false, "")) mockGraphqlConsignmentSummaryResponse(dataString) + val confirmTransferPage = controller + .confirmTransfer(consignmentId) + .apply(FakeRequest(GET, s"/consignment/$consignmentId/confirm-transfer").withCSRFToken) + + playStatus(confirmTransferPage) mustBe SEE_OTHER + redirectLocation(confirmTransferPage).get must equal(s"/consignment/$consignmentId/series") + } + + "redirect to the transfer agreement page with an authenticated user if the transfer agreement for that consignment has not been signed or agreed to" in { + val consignmentId = UUID.randomUUID() + + val client = new GraphQLConfiguration(app.configuration).getClient[gcs.Data, gcs.Variables]() + val controller = instantiateConfirmTransferController(getAuthorisedSecurityComponents) + val consignmentStatuses = List( + ConsignmentStatuses(UUID.randomUUID(), UUID.randomUUID(), "Series", "Completed", someDateTime, None) + ) + setConsignmentStatusResponse(app.configuration, wiremockServer, consignmentStatuses = consignmentStatuses) + + val consignmentSummaryResponse: gcs.GetConsignment = getConsignmentSummaryResponse + val data: client.GraphqlData = client.GraphqlData(Some(gcs.Data(Some(consignmentSummaryResponse))), List()) + val dataString: String = data.asJson.printWith(Printer(dropNullValues = false, "")) + mockGraphqlConsignmentSummaryResponse(dataString) + + val confirmTransferPage = controller + .confirmTransfer(consignmentId) + .apply(FakeRequest(GET, s"/consignment/$consignmentId/confirm-transfer").withCSRFToken) + + playStatus(confirmTransferPage) mustBe SEE_OTHER + redirectLocation(confirmTransferPage).get must equal(s"/consignment/$consignmentId/transfer-agreement") + } + + "redirect to the upload page with an authenticated user if the upload status is not 'Completed'" in { + val consignmentId = UUID.randomUUID() + + val client = new GraphQLConfiguration(app.configuration).getClient[gcs.Data, gcs.Variables]() + val controller = instantiateConfirmTransferController(getAuthorisedSecurityComponents) + val consignmentStatuses = List( + ConsignmentStatuses(UUID.randomUUID(), UUID.randomUUID(), "Series", "Completed", someDateTime, None), + ConsignmentStatuses(UUID.randomUUID(), UUID.randomUUID(), "TransferAgreement", "Completed", someDateTime, None) + ) + setConsignmentStatusResponse(app.configuration, wiremockServer, consignmentStatuses = consignmentStatuses) + + val consignmentSummaryResponse: gcs.GetConsignment = getConsignmentSummaryResponse + val data: client.GraphqlData = client.GraphqlData(Some(gcs.Data(Some(consignmentSummaryResponse))), List()) + val dataString: String = data.asJson.printWith(Printer(dropNullValues = false, "")) + mockGraphqlConsignmentSummaryResponse(dataString) + + val confirmTransferPage = controller + .confirmTransfer(consignmentId) + .apply(FakeRequest(GET, s"/consignment/$consignmentId/confirm-transfer").withCSRFToken) + + playStatus(confirmTransferPage) mustBe SEE_OTHER + redirectLocation(confirmTransferPage).get must equal(s"/consignment/$consignmentId/upload") + } + + "redirect to the file checks page with an authenticated user if the file checks status is not 'Completed'" in { + val consignmentId = UUID.randomUUID() + + val client = new GraphQLConfiguration(app.configuration).getClient[gcs.Data, gcs.Variables]() + val controller = instantiateConfirmTransferController(getAuthorisedSecurityComponents) + val consignmentStatuses = List( + ConsignmentStatuses(UUID.randomUUID(), UUID.randomUUID(), "Series", "Completed", someDateTime, None), + ConsignmentStatuses(UUID.randomUUID(), UUID.randomUUID(), "TransferAgreement", "Completed", someDateTime, None), + ConsignmentStatuses(UUID.randomUUID(), UUID.randomUUID(), "Upload", "Completed", someDateTime, None) + ) + setConsignmentStatusResponse(app.configuration, wiremockServer, consignmentStatuses = consignmentStatuses) + + val consignmentSummaryResponse: gcs.GetConsignment = getConsignmentSummaryResponse + val data: client.GraphqlData = client.GraphqlData(Some(gcs.Data(Some(consignmentSummaryResponse))), List()) + val dataString: String = data.asJson.printWith(Printer(dropNullValues = false, "")) + mockGraphqlConsignmentSummaryResponse(dataString) + + val confirmTransferPage = controller + .confirmTransfer(consignmentId) + .apply(FakeRequest(GET, s"/consignment/$consignmentId/confirm-transfer").withCSRFToken) + + playStatus(confirmTransferPage) mustBe SEE_OTHER + redirectLocation(confirmTransferPage).get must equal(s"/consignment/$consignmentId/file-checks?uploadFailed=false") + } + + "render the confirm transfer page with an authenticated user when all relevant statuses are 'Completed'" in { + val client = new GraphQLConfiguration(app.configuration).getClient[gcs.Data, gcs.Variables]() + val controller = instantiateConfirmTransferController(getAuthorisedSecurityComponents) + val consignmentStatuses = List( + ConsignmentStatuses(UUID.randomUUID(), UUID.randomUUID(), "Series", "Completed", someDateTime, None), + ConsignmentStatuses(UUID.randomUUID(), UUID.randomUUID(), "TransferAgreement", "Completed", someDateTime, None), + ConsignmentStatuses(UUID.randomUUID(), UUID.randomUUID(), "Upload", "Completed", someDateTime, None), + ConsignmentStatuses(UUID.randomUUID(), UUID.randomUUID(), "ClientChecks", "Completed", someDateTime, None) + ) + setConsignmentStatusResponse(app.configuration, wiremockServer, consignmentStatuses = consignmentStatuses) + + val consignmentSummaryResponse: gcs.GetConsignment = getConsignmentSummaryResponse + val data: client.GraphqlData = client.GraphqlData(Some(gcs.Data(Some(consignmentSummaryResponse))), List()) + val dataString: String = data.asJson.printWith(Printer(dropNullValues = false, "")) + mockGraphqlConsignmentSummaryResponse(dataString) + val confirmTransferPage = controller .confirmTransfer(consignmentId) .apply(FakeRequest(GET, s"/consignment/$consignmentId/confirm-transfer").withCSRFToken) @@ -211,7 +309,13 @@ class ConfirmTransferControllerSpec extends FrontEndTestHelper { val data: client.GraphqlData = client.GraphqlData(Some(gcs.Data(Some(consignmentSummaryResponse))), List()) val dataString: String = data.asJson.printWith(Printer(dropNullValues = false, "")) mockGraphqlConsignmentSummaryResponse(dataString) - setConsignmentStatusResponse(app.configuration, wiremockServer) + val consignmentStatuses = List( + ConsignmentStatuses(UUID.randomUUID(), UUID.randomUUID(), "Series", "Completed", someDateTime, None), + ConsignmentStatuses(UUID.randomUUID(), UUID.randomUUID(), "TransferAgreement", "Completed", someDateTime, None), + ConsignmentStatuses(UUID.randomUUID(), UUID.randomUUID(), "Upload", "Completed", someDateTime, None), + ConsignmentStatuses(UUID.randomUUID(), UUID.randomUUID(), "ClientChecks", "Completed", someDateTime, None) + ) + setConsignmentStatusResponse(app.configuration, wiremockServer, consignmentStatuses = consignmentStatuses) val controller = instantiateConfirmTransferController(getAuthorisedSecurityComponents) val finalTransferConfirmationSubmitResult = controller @@ -549,8 +653,8 @@ class ConfirmTransferControllerSpec extends FrontEndTestHelper { forAll(consignmentStatuses) { consignmentStatus => s"render the confirm transfer 'already confirmed' page with an authenticated $userType user if export status is '$consignmentStatus'" in { val controller = instantiateConfirmTransferController(getAuthorisedSecurityComponents) - val consignmentStatuses = List(ConsignmentStatuses(UUID.randomUUID(), UUID.randomUUID(), "Export", consignmentStatus, someDateTime, None)) - setConsignmentStatusResponse(app.configuration, wiremockServer, consignmentStatuses = consignmentStatuses) + + setConsignmentStatusResponse(app.configuration, wiremockServer, consignmentStatuses = getMockConsignmentStatuses(consignmentStatus)) setConsignmentReferenceResponse(wiremockServer) setConsignmentTypeResponse(wiremockServer, "standard") @@ -569,8 +673,8 @@ class ConfirmTransferControllerSpec extends FrontEndTestHelper { s"render the confirm transfer 'already confirmed' page with an authenticated user if the $userType user navigates back to the " + s"confirmTransfer after previously successfully submitting the transfer and the export status is '$consignmentStatus'" in { val controller = instantiateConfirmTransferController(getAuthorisedSecurityComponents) - val consignmentStatuses = List(ConsignmentStatuses(UUID.randomUUID(), UUID.randomUUID(), "Export", consignmentStatus, someDateTime, None)) - setConsignmentStatusResponse(app.configuration, wiremockServer, consignmentStatuses = consignmentStatuses) + + setConsignmentStatusResponse(app.configuration, wiremockServer, consignmentStatuses = getMockConsignmentStatuses(consignmentStatus)) setConsignmentReferenceResponse(wiremockServer) setConsignmentTypeResponse(wiremockServer, "standard") @@ -590,8 +694,7 @@ class ConfirmTransferControllerSpec extends FrontEndTestHelper { s"confirmTransfer after previously submitting an incorrect form and the export status is '$consignmentStatus'" in { val controller = instantiateConfirmTransferController(getAuthorisedSecurityComponents) val incompleteTransferConfirmationForm = Seq() - val consignmentStatuses = List(ConsignmentStatuses(UUID.randomUUID(), UUID.randomUUID(), "Export", consignmentStatus, someDateTime, None)) - setConsignmentStatusResponse(app.configuration, wiremockServer, consignmentStatuses = consignmentStatuses) + setConsignmentStatusResponse(app.configuration, wiremockServer, consignmentStatuses = getMockConsignmentStatuses(consignmentStatus)) setConsignmentReferenceResponse(wiremockServer) setConsignmentTypeResponse(wiremockServer, "standard") @@ -683,6 +786,16 @@ class ConfirmTransferControllerSpec extends FrontEndTestHelper { new gcs.GetConsignment(seriesCode, transferringBodyName, totalFiles, consignmentReference) } + private def getMockConsignmentStatuses(exportStatus: String): List[ConsignmentStatuses] = { + List( + ConsignmentStatuses(UUID.randomUUID(), UUID.randomUUID(), "Series", "Completed", someDateTime, None), + ConsignmentStatuses(UUID.randomUUID(), UUID.randomUUID(), "TransferAgreement", "Completed", someDateTime, None), + ConsignmentStatuses(UUID.randomUUID(), UUID.randomUUID(), "Upload", "Completed", someDateTime, None), + ConsignmentStatuses(UUID.randomUUID(), UUID.randomUUID(), "ClientChecks", "Completed", someDateTime, None), + ConsignmentStatuses(UUID.randomUUID(), UUID.randomUUID(), "Export", exportStatus, someDateTime, None) + ) + } + private def createFinalTransferConfirmationResponse = new aftc.AddFinalTransferConfirmation( consignmentId, legalCustodyTransferConfirmed = true