All files / pricing update.js

100% Statements 32/32
90.9% Branches 10/11
100% Functions 5/5
100% Lines 29/29

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 109 110 111 112 113 114 115                        1x 1x 1x 1x   1x 1x   1x         8x     7x 7x 7x 7x   7x 1x                 6x 1x           1x 1x                               5x 2x                 3x 3x             2x 2x                               1x 7x 7x   1x 1x              
/**
 * Update Pricing Lambda
 * PUT /v1/platform-admin/pricing/{id}
 *
 * Sets (or, with `value: null`, clears) an override of one operation's
 * credit cost. `id` must be one of PRICING_DEFINITIONS' known pricing keys —
 * this table only ever holds overrides of built-in operations, never
 * admin-defined ones. Requires `isPlatformAdmin: true`.
 *
 * @implements UC-CREDITS-009.1
 */
 
const { DynamoDBClient } = require('@aws-sdk/client-dynamodb')
const { DynamoDBDocumentClient, PutCommand, DeleteCommand } = require('@aws-sdk/lib-dynamodb')
const { requirePlatformAdmin } = require('../utils/requirePlatformAdmin')
const { PRICING_DEFINITIONS } = require('../utils/pricingConfig')
 
const client = new DynamoDBClient({})
const docClient = DynamoDBDocumentClient.from(client)
 
const corsHeaders = {
  'Content-Type': 'application/json',
  'Access-Control-Allow-Origin': '*'
}
 
const KNOWN_IDS = new Set(PRICING_DEFINITIONS.map((def) => def.id))
 
async function updatePricingHandler(event) {
  const userId = event.requestContext?.authorizer?.claims?.sub
  const pricingId = event.pathParameters?.id
  const body = JSON.parse(event.body || '{}')
  const { value } = body
 
  if (!KNOWN_IDS.has(pricingId)) {
    return {
      statusCode: 404,
      headers: corsHeaders,
      body: JSON.stringify({ error: 'Unknown pricing key' })
    }
  }
 
  // `value: null` clears the override and reverts to PRICING_DEFINITIONS'
  // default — the row is deleted rather than kept around with a null value.
  if (value === null) {
    await docClient.send(
      new DeleteCommand({
        TableName: process.env.PRICING_CONFIG_TABLE_NAME,
        Key: { id: pricingId }
      })
    )
    const def = PRICING_DEFINITIONS.find((d) => d.id === pricingId)
    return {
      statusCode: 200,
      headers: corsHeaders,
      body: JSON.stringify({
        id: pricingId,
        description: def.description,
        unit: def.unit,
        defaultValue: def.defaultValue,
        value: def.defaultValue,
        isOverridden: false,
        updatedAt: null,
        updatedBy: null
      })
    }
  }
 
  if (typeof value !== 'number' || !Number.isFinite(value) || value < 0) {
    return {
      statusCode: 400,
      headers: corsHeaders,
      body: JSON.stringify({
        error: 'value must be a non-negative finite number, or null to reset'
      })
    }
  }
 
  const updatedAt = new Date().toISOString()
  await docClient.send(
    new PutCommand({
      TableName: process.env.PRICING_CONFIG_TABLE_NAME,
      Item: { id: pricingId, value, updatedAt, updatedBy: userId }
    })
  )
 
  const def = PRICING_DEFINITIONS.find((d) => d.id === pricingId)
  return {
    statusCode: 200,
    headers: corsHeaders,
    body: JSON.stringify({
      id: pricingId,
      description: def.description,
      unit: def.unit,
      defaultValue: def.defaultValue,
      value,
      isOverridden: true,
      updatedAt,
      updatedBy: userId
    })
  }
}
 
exports.handler = requirePlatformAdmin(async (event) => {
  try {
    return await updatePricingHandler(event)
  } catch (error) {
    console.error('Error updating pricing:', error)
    return {
      statusCode: 500,
      headers: corsHeaders,
      body: JSON.stringify({ error: 'Failed to update pricing' })
    }
  }
})