Skip to content

Commit

Permalink
Merge pull request #4287 from nationalarchives/TDRD-591-update-consig…
Browse files Browse the repository at this point in the history
…nment-status

TDRD-591 - Set DraftMetadata status to InProgress while uploading draft metadata
  • Loading branch information
vimleshtna authored Nov 15, 2024
2 parents 5189a01 + b2ae317 commit b99e33b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
5 changes: 5 additions & 0 deletions app/controllers/DraftMetadataUploadController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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))
Expand Down
9 changes: 7 additions & 2 deletions test/controllers/DraftMetadataUploadControllerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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,
Expand All @@ -171,6 +175,7 @@ class DraftMetadataUploadControllerSpec extends FrontEndTestHelper {
consignmentService,
uploadService,
draftMetadataService,
consignmentStatusService,
applicationConfig
)
}
Expand Down

0 comments on commit b99e33b

Please sign in to comment.