All files / store check.js

100% Statements 30/30
93.75% Branches 15/16
100% Functions 1/1
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                      1x 1x         1x 1x 1x   1x   1x 10x 10x 10x 9x 9x 8x 1x       7x 7x   1x   6x 6x   5x 1x   4x           3x   2x         1x 1x      
/**
 * POST /v1/store/check
 *
 * What publishing a name from the active flow would do, for the publish panel:
 * the version it would create, or why it would be refused. Body: { name,
 * packId? }, `packId` being the pack of which to publish a new version.
 * Writes nothing.
 *
 * @implements 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, resolvePublication, validatePackInput } = require('../utils/packs')
 
const docClient = DynamoDBDocumentClient.from(new DynamoDBClient({}))
 
exports.handler = async (event) => {
  try {
    const caller = await authenticateUser(event)
    if (caller.response) return caller.response
    const { auth } = caller
    if (!auth.flowId) return errorResponse(403, 'NO_ACTIVE_FLOW')
    if (!(await hasPermission(auth, PUBLISH_PERMISSION))) {
      return errorResponse(403, 'PACK_PUBLISH_FORBIDDEN')
    }
 
    let payload
    try {
      payload = JSON.parse(event.body || '{}')
    } catch {
      return errorResponse(400, 'Invalid JSON body')
    }
    const input = validatePackInput(payload)
    if (!input.valid) return errorResponse(400, input.error)
 
    if (payload.packId !== undefined && typeof payload.packId !== 'string') {
      return errorResponse(400, 'Invalid packId')
    }
    const publication = await resolvePublication(docClient, {
      flowId: auth.flowId,
      userId: auth.userId,
      name: input.name,
      packId: payload.packId
    })
    if (publication.refusal) return jsonResponse(200, { refusal: publication.refusal })
 
    return jsonResponse(200, {
      version: publication.version,
      isNewPack: publication.isNew
    })
  } catch (error) {
    console.error('[store/check] Exception:', error.message, error.stack)
    return errorResponse(500, 'Failed to check pack name')
  }
}