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 | 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 2x 4x 1x 3x 1x 1x 2x 2x 1x 1x 6x 6x 1x 1x | /**
* Update Lambda Billing Override Lambda
* PUT /v1/platform-admin/lambda-billing
*
* Marks one Lambda function (`${resource}/${operation}`, e.g. `items/like`)
* as always free, or clears that override — see
* utils/lambdaBillingConfig.js. `functionKey` travels in the body rather
* than the path because it itself contains a `/`. A function's mere
* presence in LambdaBillingTable means "free"; there is no partial/
* percentage override, so `free: false` deletes the row rather than storing
* it with a falsy value — same "only overrides are stored" shape as
* PricingConfigTable. Requires `isPlatformAdmin: true`.
*/
const { DynamoDBClient } = require('@aws-sdk/client-dynamodb')
const { DynamoDBDocumentClient, PutCommand, 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': '*'
}
// `${resource}/${operation}`, mirroring how usage-entrypoint.js builds a
// functionKey — both segments are the lowercase-and-hyphens shape every
// existing resource/operation pair already follows (see LambdaStatsTable).
const FUNCTION_KEY_PATTERN = /^[a-z][a-z0-9-]*\/[a-z][a-z0-9-]*$/
async function updateLambdaBillingHandler(event) {
const adminUserId = event.requestContext?.authorizer?.claims?.sub
const body = JSON.parse(event.body || '{}')
const { functionKey, free } = body
if (typeof functionKey !== 'string' || !FUNCTION_KEY_PATTERN.test(functionKey)) {
return {
statusCode: 400,
headers: corsHeaders,
body: JSON.stringify({
error: 'functionKey must be `${resource}/${operation}`, e.g. items/like'
})
}
}
if (typeof free !== 'boolean') {
return {
statusCode: 400,
headers: corsHeaders,
body: JSON.stringify({ error: 'free must be a boolean' })
}
}
if (!free) {
await docClient.send(
new DeleteCommand({
TableName: process.env.LAMBDA_BILLING_TABLE_NAME,
Key: { functionKey }
})
)
return {
statusCode: 200,
headers: corsHeaders,
body: JSON.stringify({ functionKey, free: false, updatedAt: null, updatedBy: null })
}
}
const updatedAt = new Date().toISOString()
await docClient.send(
new PutCommand({
TableName: process.env.LAMBDA_BILLING_TABLE_NAME,
Item: { functionKey, updatedAt, updatedBy: adminUserId }
})
)
return {
statusCode: 200,
headers: corsHeaders,
body: JSON.stringify({ functionKey, free: true, updatedAt, updatedBy: adminUserId })
}
}
exports.handler = requirePlatformAdmin(async (event) => {
try {
return await updateLambdaBillingHandler(event)
} catch (error) {
console.error('Error updating lambda billing override:', error)
return {
statusCode: 500,
headers: corsHeaders,
body: JSON.stringify({ error: 'Failed to update lambda billing override' })
}
}
})
|