All files / utils exportJobView.js

100% Statements 11/11
87.5% Branches 7/8
100% Functions 1/1
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 422x 2x   2x                     6x               6x 2x         2x         4x 1x     6x     2x  
const { S3Client, GetObjectCommand } = require('@aws-sdk/client-s3')
const { getSignedUrl } = require('@aws-sdk/s3-request-presigner')
 
const s3Client = new S3Client({})
 
/**
 * Shared view-model builder for the singular and list export-job endpoints.
 * `downloadUrl` is generated fresh on every call rather than stored on the
 * job — presigned URLs expire in 900s (see export-worker.js), so a job the
 * caller doesn't immediately open would otherwise hand back a dead link.
 * Presigning is a local/offline SDK operation (no network round trip), so
 * doing it for every completed job on a list page is cheap.
 */
async function buildExportJobView(job) {
  const view = {
    id: job.id,
    createdAt: job.createdAt,
    status: job.status,
    requestedEntityCount: job.request?.entityIds?.length ?? 0,
    ...(job.progress && { progress: job.progress })
  }
 
  if (job.status === 'completed') {
    const downloadUrl = await getSignedUrl(
      s3Client,
      new GetObjectCommand({ Bucket: process.env.ATTACHMENTS_BUCKET_NAME, Key: job.result.zipKey }),
      { expiresIn: 900 }
    )
    view.result = {
      downloadUrl,
      entityCount: job.result.entityCount,
      cyclesBroken: job.result.cyclesBroken
    }
  } else if (job.status === 'failed') {
    view.error = job.error
  }
 
  return view
}
 
module.exports = { buildExportJobView }