All files / pricing list.js

100% Statements 19/19
87.5% Branches 7/8
100% Functions 4/4
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 64 65 66 67 68 69 70 71                        1x 1x 1x 1x   1x 1x   1x                 4x           3x   3x 24x 24x                       3x             1x 4x 4x   1x 1x              
/**
 * List Pricing Lambda
 * GET /v1/platform-admin/pricing
 *
 * Returns the credit cost of every metered operation — each entry merges
 * PRICING_DEFINITIONS' default with any override row in PricingConfigTable,
 * so the admin UI always shows every known operation, even ones nobody has
 * ever edited. Requires `isPlatformAdmin: true`.
 *
 * @implements UC-CREDITS-009.1
 */
 
const { DynamoDBClient } = require('@aws-sdk/client-dynamodb')
const { DynamoDBDocumentClient, ScanCommand } = 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': '*'
}
 
async function listPricingHandler() {
  // Strongly consistent: an admin resetting or overriding a rate expects the
  // very next list call to reflect it immediately, not after eventual-consistency
  // catches up — this table is small enough that the extra RCU cost is negligible.
  const result = await docClient.send(
    new ScanCommand({
      TableName: process.env.PRICING_CONFIG_TABLE_NAME,
      ConsistentRead: true
    })
  )
  const overrides = new Map((result.Items || []).map((item) => [item.id, item]))
 
  const pricing = PRICING_DEFINITIONS.map((def) => {
    const override = overrides.get(def.id)
    return {
      id: def.id,
      description: def.description,
      unit: def.unit,
      defaultValue: def.defaultValue,
      value: override ? override.value : def.defaultValue,
      isOverridden: Boolean(override),
      updatedAt: override?.updatedAt || null,
      updatedBy: override?.updatedBy || null
    }
  })
 
  return {
    statusCode: 200,
    headers: corsHeaders,
    body: JSON.stringify(pricing)
  }
}
 
exports.handler = requirePlatformAdmin(async () => {
  try {
    return await listPricingHandler()
  } catch (error) {
    console.error('Error listing pricing:', error)
    return {
      statusCode: 500,
      headers: corsHeaders,
      body: JSON.stringify({ error: 'Failed to list pricing' })
    }
  }
})