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 | 1x 1x 1x 1x 1x 1x 1x 1x 12x 12x 12x 12x 12x 12x 3x 9x 2x 7x 1x 6x 6x 4x 2x 1x 1x 1x 12x 12x 1x 1x | /**
* Adjust User Credits Lambda
* POST /v1/platform-admin/credits/{userId}/adjust
*
* Requires `isPlatformAdmin: true`. Manual grant/correction of a user's
* credits balance — the stop-gap for crediting a user before Stripe
* purchases exist (see docs/architecture/USAGE_METERING.md "Phased
* rollout" step 8). Every adjustment is logged to CreditsLedgerTable
* (pool: 'admin_adjustment') with the acting admin's id and a required
* reason, same as any other credits movement — see utils/creditsLedger.js.
*
* @implements UC-CREDITS-006.1
*/
const { DynamoDBClient } = require('@aws-sdk/client-dynamodb')
const { DynamoDBDocumentClient } = require('@aws-sdk/lib-dynamodb')
const { requirePlatformAdmin } = require('../utils/requirePlatformAdmin')
const { adjustCreditsBalance } = require('../utils/creditsLedger')
const client = new DynamoDBClient({})
const docClient = DynamoDBDocumentClient.from(client)
const corsHeaders = {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
}
const MAX_NOTE_LENGTH = 500
async function adjustCreditsHandler(event) {
const adminUserId = event.requestContext?.authorizer?.claims?.sub
const userId = event.pathParameters?.userId
Iif (!userId) {
return {
statusCode: 400,
headers: corsHeaders,
body: JSON.stringify({ error: 'Missing required path parameter: userId' })
}
}
const body = JSON.parse(event.body || '{}')
const { amount, reason } = body
if (!Number.isInteger(amount) || amount === 0) {
return {
statusCode: 400,
headers: corsHeaders,
body: JSON.stringify({ error: 'amount must be a non-zero integer' })
}
}
if (typeof reason !== 'string' || reason.trim().length === 0) {
return {
statusCode: 400,
headers: corsHeaders,
body: JSON.stringify({ error: 'reason is required' })
}
}
if (reason.length > MAX_NOTE_LENGTH) {
return {
statusCode: 400,
headers: corsHeaders,
body: JSON.stringify({ error: 'REASON_TOO_LONG', maxLength: MAX_NOTE_LENGTH })
}
}
try {
const { balanceAfter, transactionId } = await adjustCreditsBalance(docClient, {
usersTableName: process.env.USERS_TABLE_NAME,
creditsLedgerTableName: process.env.CREDITS_LEDGER_TABLE_NAME,
userId,
amount,
note: reason.trim(),
adminUserId
})
return {
statusCode: 200,
headers: corsHeaders,
body: JSON.stringify({ balance: balanceAfter, transactionId })
}
} catch (error) {
if (error.name === 'ConditionalCheckFailedException') {
return {
statusCode: 404,
headers: corsHeaders,
body: JSON.stringify({ error: 'User not found' })
}
}
throw error
}
}
exports.handler = requirePlatformAdmin(async (event) => {
try {
return await adjustCreditsHandler(event)
} catch (error) {
console.error('Error adjusting user credits:', error)
return {
statusCode: 500,
headers: corsHeaders,
body: JSON.stringify({ error: 'Failed to adjust credits' })
}
}
})
|