Skip to content

Commit

Permalink
Merge branch 'master' into scala-steward-dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom-Hallett authored Jun 11, 2024
2 parents ff7fc73 + f5d367b commit ecfe327
Show file tree
Hide file tree
Showing 17 changed files with 18,733 additions and 12,051 deletions.
5 changes: 5 additions & 0 deletions app/configuration/ApplicationConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ class ApplicationConfig @Inject() (configuration: Configuration) {

val s3Endpoint: String = configuration.get[String]("s3.endpoint")

val snsEndpoint: String = configuration.get[String]("sns.endpoint")

val draft_metadata_s3_bucket_name: String = configuration.get[String]("draft_metadata_s3_bucket_name")

val draftMetadataFileName: String = configuration.get[String]("draftMetadata.fileName")

val notificationSnsTopicArn: String = get("notificationSnsTopicArn")
}
5 changes: 3 additions & 2 deletions app/controllers/DraftMetadataUploadController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,16 @@ class DraftMetadataUploadController @Inject() (
val successPage = routes.DraftMetadataChecksController.draftMetadataChecksPage(consignmentId)
val token = request.asInstanceOf[Request[AnyContent]].token
val uploadBucket = s"${applicationConfig.draft_metadata_s3_bucket_name}"
val uploadKey = s"$consignmentId/draft-metadata.csv"
val uploadFileName = applicationConfig.draftMetadataFileName
val uploadKey = s"$consignmentId/$uploadFileName"
val noDraftMetadataFileUploaded: String = "No meta data file provided"

def uploadDraftMetadata: IO[Result] = for {
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))
_ <- IO.fromFuture(IO(uploadService.uploadDraftMetadata(uploadBucket, uploadKey, draftMetadata)))
_ <- IO.fromFuture(IO { draftMetadataService.triggerDraftMetadataValidator(consignmentId, token.bearerAccessToken.getValue) })
_ <- IO.fromFuture(IO { draftMetadataService.triggerDraftMetadataValidator(consignmentId, uploadFileName, token.bearerAccessToken.getValue) })
successPage <- IO(play.api.mvc.Results.Redirect(successPage))
} yield successPage

Expand Down
26 changes: 19 additions & 7 deletions app/controllers/TransferCompleteController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import configuration.KeycloakConfiguration
import org.pac4j.play.scala.SecurityComponents
import play.api.i18n.I18nSupport
import play.api.mvc.{Action, AnyContent, Request}
import services.ConsignmentService
import services.MessagingService.TransferCompleteEvent
import services.{ConsignmentService, MessagingService}

import java.util.UUID
import javax.inject.Inject
Expand All @@ -14,17 +15,28 @@ import scala.concurrent.ExecutionContext
class TransferCompleteController @Inject() (
val controllerComponents: SecurityComponents,
val keycloakConfiguration: KeycloakConfiguration,
val consignmentService: ConsignmentService
val consignmentService: ConsignmentService,
val messagingService: MessagingService
)(implicit val ec: ExecutionContext)
extends TokenSecurity
with I18nSupport {

def transferComplete(consignmentId: UUID): Action[AnyContent] = standardTypeAction(consignmentId) { implicit request: Request[AnyContent] =>
consignmentService
.getConsignmentRef(consignmentId, request.token.bearerAccessToken)
.map { consignmentReference =>
Ok(views.html.standard.transferComplete(consignmentId, consignmentReference, request.token.name))
}
for {
consignmentTransferSummary <- consignmentService.getConsignmentConfirmTransfer(consignmentId, request.token.bearerAccessToken)
} yield {
messagingService.sendTransferCompleteNotification(
TransferCompleteEvent(
transferringBodyName = consignmentTransferSummary.transferringBodyName,
consignmentReference = consignmentTransferSummary.consignmentReference,
consignmentId = consignmentId.toString,
seriesName = consignmentTransferSummary.seriesName,
userId = request.token.userId.toString,
userEmail = request.token.email
)
)
Ok(views.html.standard.transferComplete(consignmentId, consignmentTransferSummary.consignmentReference, request.token.name))
}
}

def judgmentTransferComplete(consignmentId: UUID): Action[AnyContent] = judgmentTypeAction(consignmentId) { implicit request: Request[AnyContent] =>
Expand Down
4 changes: 2 additions & 2 deletions app/services/DraftMetadataService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import scala.concurrent.{ExecutionContext, Future}

class DraftMetadataService @Inject() (val wsClient: WSClient, val configuration: Configuration)(implicit val executionContext: ExecutionContext) extends Logging {

def triggerDraftMetadataValidator(consignmentId: UUID, token: String): Future[Boolean] = {
val url = s"${configuration.get[String]("metadatavalidation.baseUrl")}/draft-metadata/validate/$consignmentId"
def triggerDraftMetadataValidator(consignmentId: UUID, uploadFileName: String, token: String): Future[Boolean] = {
val url = s"${configuration.get[String]("metadatavalidation.baseUrl")}/draft-metadata/validate/$consignmentId/$uploadFileName"
wsClient
.url(url)
.addHttpHeaders(("Authorization", token), ("Content-Type", "application/json"))
Expand Down
35 changes: 35 additions & 0 deletions app/services/MessagingService.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package services

import configuration.ApplicationConfig
import io.circe.Encoder
import io.circe.generic.semiauto.deriveEncoder
import io.circe.syntax.EncoderOps
import services.MessagingService.TransferCompleteEvent
import software.amazon.awssdk.services.sns.SnsClient
import software.amazon.awssdk.services.sns.model.PublishResponse
import uk.gov.nationalarchives.aws.utils.sns.SNSClients.sns
import uk.gov.nationalarchives.aws.utils.sns.SNSUtils

import javax.inject.Inject
import scala.concurrent.ExecutionContext

class MessagingService @Inject() (val applicationConfig: ApplicationConfig)(implicit val ec: ExecutionContext) {
val client: SnsClient = sns(applicationConfig.snsEndpoint)
val utils: SNSUtils = SNSUtils(client)

implicit val transferCompletedEventEncoder: Encoder[TransferCompleteEvent] = deriveEncoder[TransferCompleteEvent]
def sendTransferCompleteNotification(transferCompletedEvent: TransferCompleteEvent): PublishResponse = {
utils.publish(transferCompletedEvent.asJson.toString, applicationConfig.notificationSnsTopicArn)
}
}

object MessagingService {
case class TransferCompleteEvent(
transferringBodyName: Option[String],
consignmentReference: String,
consignmentId: String,
seriesName: Option[String],
userId: String,
userEmail: String
)
}
2 changes: 1 addition & 1 deletion app/views/standard/upload.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ <h1 class="govuk-heading-l">@pageHeading1stHalf</h1>

<h1 class="govuk-heading-l">@pageHeading1stHalf</h1>
<p class="govuk-body">Before uploading, all files and folders you wish to transfer must be put into a single, top-level folder.</p>
<p class="govuk-body">Individual files must be no bigger than 2GB and you must upload no more than 500 files per transfer. To transfer larger files, contact <a href="mailto:@Messages("nationalArchives.email")">@Messages("nationalArchives.email")</a>. If your folder contains file formats that we cannot accept, you may have to start again.</p>
<p class="govuk-body">Individual files must be no bigger than 2GB and you must upload no more than 3000 files per transfer. The total size of all files for transfer must be no more than 5GB. To transfer larger files, contact <a href="mailto:@Messages("nationalArchives.email")">@Messages("nationalArchives.email")</a>. If your folder contains file formats that we cannot accept, you may have to start again.</p>
<details class="govuk-details" data-module="govuk-details">
<summary class="govuk-details__summary">
<span class="govuk-details__summary-text">
Expand Down
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ libraryDependencies ++= Seq(
"uk.gov.nationalarchives" %% "tdr-generated-graphql" % "0.0.377",
"uk.gov.nationalarchives" %% "tdr-metadata-validation" % "0.0.25",
"uk.gov.nationalarchives" %% "s3-utils" % "0.1.183",
"uk.gov.nationalarchives" %% "sns-utils" % "0.1.173",
"com.github.tototoshi" %% "scala-csv" % "1.3.10",
"ch.qos.logback" % "logback-classic" % "1.5.6",
ws,
Expand Down
8 changes: 8 additions & 0 deletions conf/application.base.conf
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ logout.url="http://localhost:9000/signed-out"

s3.endpoint = "https://s3.eu-west-2.amazonaws.com/"

sns.endpoint = "https://sns.eu-west-2.amazonaws.com/"

play.filters.enabled += play.filters.https.RedirectHttpsFilter

play {
Expand Down Expand Up @@ -58,3 +60,9 @@ featureAccessBlock {
blockDraftMetadataUpload=${BLOCK_DRAFT_METADATA_UPLOAD}
blockAutomateJudgmentTransfers=${BLOCK_AUTOMATE_JUDGMENT_TRANSFERS}
}

draftMetadata {
fileName = "draft-metadata.csv"
}

notificationSnsTopicArn = ${NOTIFICATION_SNS_TOPIC_ARN}
Loading

0 comments on commit ecfe327

Please sign in to comment.