Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update report name #1521

Merged
merged 1 commit into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/ReportFileController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class ReportFileController(
def downloadZip(reportId: UUID, origin: Option[ReportFileOrigin]) = Action.async { _ =>
for {
report <- reportRepository.get(reportId).flatMap(_.liftTo[Future](AppError.ReportNotFound(reportId)))
stream <- reportFileOrchestrator.downloadReportFilesArchive(reportId, origin)
stream <- reportFileOrchestrator.downloadReportFilesArchive(report, origin)
} yield Ok
.chunked(stream)
.as("application/zip")
Expand Down
6 changes: 3 additions & 3 deletions app/orchestrators/ReportFileOrchestrator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,17 @@ class ReportFileOrchestrator(
}

def downloadReportFilesArchive(
reportId: UUID,
report: Report,
origin: Option[ReportFileOrigin]
): Future[Source[ByteString, Future[IOResult]]] =
for {
reportFiles <- reportFileRepository
.retrieveReportFiles(reportId)
.retrieveReportFiles(report.id)

filteredFilesByOrigin = reportFiles.filter { f =>
origin.contains(f.origin) || origin.isEmpty
}
_ <- Future.successful(filteredFilesByOrigin).ensure(AppError.NoReportFiles)(_.nonEmpty)
} yield reportZipExportService.reportAttachmentsZip(filteredFilesByOrigin)
} yield reportZipExportService.reportAttachmentsZip(report.creationDate, filteredFilesByOrigin)

}
16 changes: 11 additions & 5 deletions app/orchestrators/ReportZipExportService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import services.PDFService
import services.S3ServiceInterface
import services.ZipBuilder

import java.time.OffsetDateTime
import java.time.format.DateTimeFormatter
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
Expand All @@ -30,8 +31,6 @@ class ReportZipExportService(
implicit val ec: ExecutionContext =
system.dispatchers.lookup("io-dispatcher")

println(s"------------------ ec = ${ec} ------------------")

private def getFileExtension(fileName: String): String =
fileName.lastIndexOf(".") match {
case -1 => "" // No extension found
Expand All @@ -43,7 +42,7 @@ class ReportZipExportService(
): Source[ByteString, Future[IOResult]] = {

val reportAttachmentSources = reportWithData.files.zipWithIndex.map { case (file, i) =>
buildReportAttachmentSource(file, i)
buildReportAttachmentSource(reportWithData.report.creationDate, file, i)
}
val reportPdfSummarySource = buildReportPdfSummarySource(reportWithData)

Expand All @@ -53,11 +52,12 @@ class ReportZipExportService(
}

def reportAttachmentsZip(
creationDate: OffsetDateTime,
reports: Seq[ReportFile]
): Source[ByteString, Future[IOResult]] = {

val reportAttachmentSources = reports.zipWithIndex.map { case (file, i) =>
buildReportAttachmentSource(file, i + 1)
buildReportAttachmentSource(creationDate, file, i + 1)
}

ZipBuilder.buildZip(reportAttachmentSources)
Expand All @@ -76,11 +76,17 @@ class ReportZipExportService(
}

private def buildReportAttachmentSource(
creationDate: OffsetDateTime,
reportFile: ReportFile,
index: Int
): (ReportZipEntryName, Source[ByteString, Unit]) = {
val source = s3Service.downloadFromBucket(reportFile.storageFilename).mapMaterializedValue(_ => ())
(ReportZipEntryName(s"PJ-$index.${getFileExtension(reportFile.filename)}"), source)
(
ReportZipEntryName(
s"${creationDate.format(DateTimeFormatter.ofPattern("dd-MM-yyyy"))}-PJ-$index.${getFileExtension(reportFile.filename)}"
),
source
)
}

}