All files / ribbons update.js

100% Statements 42/42
88.88% Branches 16/18
100% Functions 2/2
100% Lines 41/41

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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108                1x 1x 1x 1x   1x 1x   1x           7x 7x 7x 7x   7x 1x 1x 1x               6x 2x 2x 1x       5x 1x             4x     3x 1x             2x 2x 2x 2x   2x 6x 2x 2x 2x   2x   2x                     2x             1x 7x 7x   1x 1x              
/**
 * Update Ribbon Lambda
 * PUT /v1/platform-admin/ribbons/{id}
 *
 * Partial update — only the fields present in the body are changed.
 * Requires `isPlatformAdmin: true`.
 */
 
const { DynamoDBClient } = require('@aws-sdk/client-dynamodb')
const { DynamoDBDocumentClient, GetCommand, UpdateCommand } = require('@aws-sdk/lib-dynamodb')
const { requirePlatformAdmin } = require('../utils/requirePlatformAdmin')
const { validateRibbonMessage, validateRibbonColor } = require('../utils/validateRibbon')
 
const client = new DynamoDBClient({})
const docClient = DynamoDBDocumentClient.from(client)
 
const corsHeaders = {
  'Content-Type': 'application/json',
  'Access-Control-Allow-Origin': '*'
}
 
async function updateRibbonHandler(event) {
  const userId = event.requestContext?.authorizer?.claims?.sub
  const ribbonId = event.pathParameters?.id
  const body = JSON.parse(event.body || '{}')
  const { message, color, active } = body
 
  if (message !== undefined) {
    const messageError = validateRibbonMessage(message)
    Eif (messageError) {
      return {
        statusCode: 400,
        headers: corsHeaders,
        body: JSON.stringify({ error: messageError })
      }
    }
  }
 
  if (color !== undefined) {
    const colorError = validateRibbonColor(color)
    if (colorError) {
      return { statusCode: 400, headers: corsHeaders, body: JSON.stringify({ error: colorError }) }
    }
  }
 
  if (active !== undefined && typeof active !== 'boolean') {
    return {
      statusCode: 400,
      headers: corsHeaders,
      body: JSON.stringify({ error: 'Active must be a boolean' })
    }
  }
 
  const existing = await docClient.send(
    new GetCommand({ TableName: process.env.RIBBONS_TABLE_NAME, Key: { id: ribbonId } })
  )
  if (!existing.Item) {
    return {
      statusCode: 404,
      headers: corsHeaders,
      body: JSON.stringify({ error: 'Ribbon not found' })
    }
  }
 
  const updates = { message, color, active }
  const setClauses = []
  const names = { '#updatedAt': 'updatedAt', '#updatedBy': 'updatedBy' }
  const values = { ':updatedAt': new Date().toISOString(), ':updatedBy': userId }
 
  for (const [field, value] of Object.entries(updates)) {
    if (value === undefined) continue
    setClauses.push(`#${field} = :${field}`)
    names[`#${field}`] = field
    values[`:${field}`] = value
  }
  setClauses.push('#updatedAt = :updatedAt', '#updatedBy = :updatedBy')
 
  const result = await docClient.send(
    new UpdateCommand({
      TableName: process.env.RIBBONS_TABLE_NAME,
      Key: { id: ribbonId },
      UpdateExpression: `SET ${setClauses.join(', ')}`,
      ExpressionAttributeNames: names,
      ExpressionAttributeValues: values,
      ReturnValues: 'ALL_NEW'
    })
  )
 
  return {
    statusCode: 200,
    headers: corsHeaders,
    body: JSON.stringify(result.Attributes)
  }
}
 
exports.handler = requirePlatformAdmin(async (event) => {
  try {
    return await updateRibbonHandler(event)
  } catch (error) {
    console.error('Error updating ribbon:', error)
    return {
      statusCode: 500,
      headers: corsHeaders,
      body: JSON.stringify({ error: 'Failed to update ribbon' })
    }
  }
})