All files / ribbons delete.js

100% Statements 19/19
100% Branches 4/4
100% Functions 3/3
100% Lines 18/18

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           3x   3x                 2x 1x     2x 1x             1x             1x 3x 3x   1x 1x              
/**
 * Delete Ribbon Lambda
 * DELETE /v1/platform-admin/ribbons/{id}
 *
 * Requires `isPlatformAdmin: true`.
 */
 
const { DynamoDBClient } = require('@aws-sdk/client-dynamodb')
const { DynamoDBDocumentClient, DeleteCommand } = require('@aws-sdk/lib-dynamodb')
const { requirePlatformAdmin } = require('../utils/requirePlatformAdmin')
 
const client = new DynamoDBClient({})
const docClient = DynamoDBDocumentClient.from(client)
 
const corsHeaders = {
  'Content-Type': 'application/json',
  'Access-Control-Allow-Origin': '*'
}
 
async function deleteRibbonHandler(event) {
  const ribbonId = event.pathParameters?.id
 
  const result = await docClient
    .send(
      new DeleteCommand({
        TableName: process.env.RIBBONS_TABLE_NAME,
        Key: { id: ribbonId },
        ConditionExpression: 'attribute_exists(id)'
      })
    )
    .catch((error) => {
      if (error.name === 'ConditionalCheckFailedException') return null
      throw error
    })
 
  if (result === null) {
    return {
      statusCode: 404,
      headers: corsHeaders,
      body: JSON.stringify({ error: 'Ribbon not found' })
    }
  }
 
  return {
    statusCode: 204,
    headers: corsHeaders,
    body: ''
  }
}
 
exports.handler = requirePlatformAdmin(async (event) => {
  try {
    return await deleteRibbonHandler(event)
  } catch (error) {
    console.error('Error deleting ribbon:', error)
    return {
      statusCode: 500,
      headers: corsHeaders,
      body: JSON.stringify({ error: 'Failed to delete ribbon' })
    }
  }
})