All files / entities get-import-job.js

85.71% Statements 18/21
92.3% Branches 12/13
100% Functions 2/2
85.71% Lines 18/21

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 42 43 44 451x 1x 1x     6x                     6x 6x 6x       6x 6x 6x 3x     3x   3x 1x 2x 1x     3x             1x  
const { getActiveFlowId } = require('../shared/activeFlow')
const { requirePermission } = require('../utils/requirePermission')
const { getJob } = require('../utils/jobsTable')
 
function jsonResponse(statusCode, body) {
  return {
    statusCode,
    headers: {
      'Content-Type': 'application/json',
      'Access-Control-Allow-Origin': '*'
    },
    body: JSON.stringify(body)
  }
}
 
async function getImportJobHandler(event) {
  try {
    const flowId = await getActiveFlowId(event)
    Iif (!flowId) {
      return jsonResponse(401, { error: 'Missing flow context' })
    }
 
    const jobId = event.pathParameters?.jobId
    const job = await getJob(jobId)
    if (!job || job.type !== 'import' || job.flowId !== flowId) {
      return jsonResponse(404, { error: 'Import job not found' })
    }
 
    const response = { status: job.status, ...(job.progress && { progress: job.progress }) }
 
    if (job.status === 'completed') {
      response.result = job.result
    } else if (job.status === 'failed') {
      response.error = job.error
    }
 
    return jsonResponse(200, response)
  } catch (error) {
    console.error('[get-import-job] Exception:', error.message, error.stack)
    return jsonResponse(500, { error: 'Failed to get import job' })
  }
}
 
exports.handler = requirePermission(getImportJobHandler, { permission: 'entities:create' })