All files / store delete.js

100% Statements 21/21
100% Branches 6/6
100% Functions 1/1
100% Lines 19/19

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                  1x 1x         1x 1x 1x 1x   1x   1x 7x 7x 7x   6x 5x 4x 1x     3x 3x   1x 1x      
/**
 * DELETE /v1/store/{packId}
 *
 * Deletes a pack with all its versions, for its author, the owner of its flow or a
 * platform admin. Installed copies are unaffected, and the name is free again.
 *
 * @implements UC-STORE-007.1
 */
 
const { DynamoDBClient } = require('@aws-sdk/client-dynamodb')
const { DynamoDBDocumentClient } = require('@aws-sdk/lib-dynamodb')
const {
  authenticateModerator: authenticateUser,
  errorResponse,
  jsonResponse
} = require('../utils/moderation/access')
const { getPack } = require('../utils/packs')
const { canDeletePack } = require('../utils/packAccess')
const { deletePackData } = require('../utils/packCleanup')
 
const docClient = DynamoDBDocumentClient.from(new DynamoDBClient({}))
 
exports.handler = async (event) => {
  try {
    const caller = await authenticateUser(event)
    if (caller.response) return caller.response
 
    const pack = await getPack(docClient, event.pathParameters?.packId)
    if (!pack) return errorResponse(404, 'Pack not found')
    if (!(await canDeletePack(docClient, pack, caller.auth.userId))) {
      return errorResponse(403, 'Permission denied')
    }
 
    await deletePackData(docClient, pack)
    return jsonResponse(200, { id: pack.id })
  } catch (error) {
    console.error('[store/delete] Exception:', error.message, error.stack)
    return errorResponse(500, 'Failed to delete pack')
  }
}