All files / store get.js

100% Statements 31/31
100% Branches 20/20
100% Functions 3/3
100% Lines 26/26

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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77                              1x 1x         1x 1x 1x 1x 1x   1x       10x 8x 4x         3x             1x 14x 14x 14x 13x 13x   13x 12x   11x 11x   10x           10x               1x 1x      
/**
 * GET /v1/store/{packId}
 *
 * A pack and the schema of its current version. A replaced version can no
 * longer be opened.
 *
 * Who may publish its next version from the active flow (holding
 * `packs:publish`, and being its author or the flow's owner) is told so, with
 * what the current version was published from (`source`): the flow's entities
 * chosen, the dependencies left out and the entities whose items it carries.
 * Anyone else never gets them, as they name the flow's own entities.
 *
 * @implements UC-STORE-002.1, UC-STORE-008.2
 */
 
const { DynamoDBClient } = require('@aws-sdk/client-dynamodb')
const { DynamoDBDocumentClient } = require('@aws-sdk/lib-dynamodb')
const {
  authenticateModerator: authenticateUser,
  errorResponse,
  jsonResponse
} = require('../utils/moderation/access')
const { hasPermission } = require('../shared/auth-helper')
const { PUBLISH_PERMISSION, getPack, buildPackPresenter, maintainsPack } = require('../utils/packs')
const { readSnapshot } = require('../utils/packSnapshot')
const { canDeletePack } = require('../utils/packAccess')
 
const docClient = DynamoDBDocumentClient.from(new DynamoDBClient({}))
 
/** From the pack's own flow only, as store/publish.js would accept it. */
async function mayPublishVersion(auth, pack) {
  if (!auth.flowId || auth.flowId !== pack.flowId) return false
  if (!(await hasPermission(auth, PUBLISH_PERMISSION))) return false
  return maintainsPack(pack, auth.userId)
}
 
/** Packs published before it was kept start from nothing. */
function presentSource(source) {
  return {
    entityIds: source?.entityIds ?? [],
    excludeEntityIds: source?.excludeEntityIds ?? [],
    itemsEntityIds: source?.itemsEntityIds ?? []
  }
}
 
exports.handler = async (event) => {
  try {
    const caller = await authenticateUser(event)
    if (caller.response) return caller.response
    const { auth } = caller
    const { userId } = auth
 
    const pack = await getPack(docClient, event.pathParameters?.packId, { consistent: true })
    if (!pack) return errorResponse(404, 'Pack not found')
 
    const bundle = await readSnapshot(pack.snapshotKey)
    if (!bundle) return errorResponse(404, 'Pack not found')
 
    const [present, canDelete, canPublishVersion] = await Promise.all([
      buildPackPresenter(docClient, [pack], userId),
      canDeletePack(docClient, pack, userId),
      mayPublishVersion(auth, pack)
    ])
 
    return jsonResponse(200, {
      ...present(pack),
      bundle,
      canDelete,
      canPublishVersion,
      ...(canPublishVersion && { source: presentSource(pack.source) })
    })
  } catch (error) {
    console.error('[store/get] Exception:', error.message, error.stack)
    return errorResponse(500, 'Failed to load pack')
  }
}