From 94ed177f1d5b13b357738a9d9782b2cb5f375cf0 Mon Sep 17 00:00:00 2001 From: VimleshGupta Date: Wed, 13 Nov 2024 11:32:55 +0000 Subject: [PATCH] TDRD-591 - Set DraftMetadata status to InProgress while uploading draft metadata --- app/controllers/DraftMetadataUploadController.scala | 5 +++++ test/controllers/DraftMetadataUploadControllerSpec.scala | 9 +++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/app/controllers/DraftMetadataUploadController.scala b/app/controllers/DraftMetadataUploadController.scala index 1a2e0dce1..0590fda50 100644 --- a/app/controllers/DraftMetadataUploadController.scala +++ b/app/controllers/DraftMetadataUploadController.scala @@ -5,11 +5,13 @@ import cats.effect.IO import cats.effect.IO.fromOption import cats.effect.unsafe.implicits.global import configuration.{ApplicationConfig, KeycloakConfiguration} +import graphql.codegen.types.ConsignmentStatusInput import org.pac4j.play.scala.SecurityComponents import play.api._ import play.api.i18n.I18nSupport import play.api.libs.Files.TemporaryFile import play.api.mvc._ +import services.Statuses.{DraftMetadataType, InProgressValue} import services._ import viewsapi.Caching.preventCaching @@ -26,6 +28,7 @@ class DraftMetadataUploadController @Inject() ( val consignmentService: ConsignmentService, val uploadService: UploadService, val draftMetadataService: DraftMetadataService, + val consignmentStatusService: ConsignmentStatusService, val applicationConfig: ApplicationConfig )(implicit val ec: ExecutionContext) extends TokenSecurity @@ -54,8 +57,10 @@ class DraftMetadataUploadController @Inject() ( val uploadFileName = applicationConfig.draftMetadataFileName val uploadKey = s"$consignmentId/$uploadFileName" val noDraftMetadataFileUploaded: String = "No meta data file provided" + val consignmentStatusInput = ConsignmentStatusInput(consignmentId, DraftMetadataType.id, Some(InProgressValue.value)) def uploadDraftMetadata: IO[Result] = for { + _ <- IO.fromFuture(IO(consignmentStatusService.updateConsignmentStatus(consignmentStatusInput, token.bearerAccessToken))) firstFilePart <- fromOption(request.body.files.headOption)(new RuntimeException(noDraftMetadataFileUploaded)) file <- fromOption(request.body.file(firstFilePart.key))(new RuntimeException(noDraftMetadataFileUploaded)) draftMetadata <- fromOption(Using(scala.io.Source.fromFile(file.ref.getAbsoluteFile))(_.mkString).toOption)(new RuntimeException(noDraftMetadataFileUploaded)) diff --git a/test/controllers/DraftMetadataUploadControllerSpec.scala b/test/controllers/DraftMetadataUploadControllerSpec.scala index 2c81ef6b9..1f4bdde14 100644 --- a/test/controllers/DraftMetadataUploadControllerSpec.scala +++ b/test/controllers/DraftMetadataUploadControllerSpec.scala @@ -15,7 +15,7 @@ import play.api.mvc.{MultipartFormData, Result} import play.api.test.CSRFTokenHelper._ import play.api.test.Helpers.{status => playStatus, _} import play.api.test.{FakeHeaders, FakeRequest} -import services.{ConsignmentService, DraftMetadataService, FileError, UploadService} +import services.{ConsignmentService, ConsignmentStatusService, DraftMetadataService, FileError, UploadService} import software.amazon.awssdk.services.s3.model.PutObjectResponse import testUtils.FrontEndTestHelper @@ -41,7 +41,7 @@ class DraftMetadataUploadControllerSpec extends FrontEndTestHelper { wiremockServer.stop() } - "DraftMetadataUploadControllerSpec GET" should { + "DraftMetadataUploadController GET" should { "render 'draft metadata upload' page when 'blockDraftMetadataUpload' set to 'false'" in { val controller = instantiateDraftMetadataUploadController(blockDraftMetadataUpload = false) @@ -101,12 +101,15 @@ class DraftMetadataUploadControllerSpec extends FrontEndTestHelper { .apply(FakeRequest(GET, "/draft-metadata/upload").withCSRFToken) playStatus(draftMetadataUploadPage) mustBe FORBIDDEN } + } + "DraftMetadataUploadController saveDraftMetadata" should { "redirect to draft metadata checks page when upload successful" in { val uploadServiceMock = mock[UploadService] when(configuration.get[String]("draftMetadata.fileName")).thenReturn(uploadFilename) val putObjectResponse = PutObjectResponse.builder().eTag("testEtag").build() when(uploadServiceMock.uploadDraftMetadata(anyString, anyString, anyString)).thenReturn(Future.successful(putObjectResponse)) + setUpdateConsignmentStatus(wiremockServer) val draftMetadataServiceMock = mock[DraftMetadataService] when(draftMetadataServiceMock.triggerDraftMetadataValidator(any[UUID], anyString, anyString)).thenReturn(Future.successful(true)) @@ -163,6 +166,7 @@ class DraftMetadataUploadControllerSpec extends FrontEndTestHelper { val applicationConfig: ApplicationConfig = new ApplicationConfig(configuration) val graphQLConfiguration = new GraphQLConfiguration(app.configuration) val consignmentService = new ConsignmentService(graphQLConfiguration) + val consignmentStatusService = new ConsignmentStatusService(graphQLConfiguration) new DraftMetadataUploadController( securityComponents, @@ -171,6 +175,7 @@ class DraftMetadataUploadControllerSpec extends FrontEndTestHelper { consignmentService, uploadService, draftMetadataService, + consignmentStatusService, applicationConfig ) }