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 | 1x 1x 1x 1x 1x 1x 1x 1x 12x 1x 12x 12x 12x 11x 11x 11x 11x 3x 3x 2x 1x 2x 9x 8x 1x 1x | /**
* GET /v1/notifications
*
* The caller's notifications, newest first, plus when they last marked them
* read (`lastReadAt`), so the client can tell which ones are new. A Query on
* the caller's own partition, restricted to notification rows (`N#` prefix),
* which leaves out their state row.
*
* Query params:
* - limit: page size (default 20, max 50)
* - nextToken: opaque cursor returned by the previous page
*
* @implements UC-NOTIF-001.1
*/
const { DynamoDBClient } = require('@aws-sdk/client-dynamodb')
const { DynamoDBDocumentClient, GetCommand, QueryCommand } = require('@aws-sdk/lib-dynamodb')
const {
NOTIFICATION_PREFIX,
STATE_SORT_KEY,
toPublicNotification
} = require('../utils/notifications')
const client = new DynamoDBClient({})
const docClient = DynamoDBDocumentClient.from(client)
const CORS_HEADERS = {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
}
const DEFAULT_LIMIT = 20
const MAX_LIMIT = 50
const respond = (statusCode, body) => ({
statusCode,
headers: CORS_HEADERS,
body: JSON.stringify(body)
})
exports.handler = async (event) => {
try {
const userId = event.requestContext?.authorizer?.claims?.sub
if (!userId) return respond(401, { error: 'Unauthorized' })
const rawLimit = parseInt(event.queryStringParameters?.limit, 10)
const limit = Number.isFinite(rawLimit)
? Math.min(Math.max(rawLimit, 1), MAX_LIMIT)
: DEFAULT_LIMIT
let exclusiveStartKey
const nextToken = event.queryStringParameters?.nextToken
if (nextToken) {
try {
const key = JSON.parse(Buffer.from(nextToken, 'base64').toString())
// A client-controlled cursor must not point the Query at another user
if (key?.userId !== userId || typeof key.sortKey !== 'string') throw new Error('bad')
exclusiveStartKey = { userId, sortKey: key.sortKey }
} catch {
return respond(400, { error: 'Invalid nextToken' })
}
}
const [page, state] = await Promise.all([
docClient.send(
new QueryCommand({
TableName: process.env.NOTIFICATIONS_TABLE_NAME,
KeyConditionExpression: 'userId = :userId AND begins_with(sortKey, :prefix)',
ExpressionAttributeValues: { ':userId': userId, ':prefix': NOTIFICATION_PREFIX },
ScanIndexForward: false,
Limit: limit,
ExclusiveStartKey: exclusiveStartKey
})
),
docClient.send(
new GetCommand({
TableName: process.env.NOTIFICATIONS_TABLE_NAME,
Key: { userId, sortKey: STATE_SORT_KEY }
})
)
])
return respond(200, {
notifications: (page.Items || []).map(toPublicNotification),
lastReadAt: state.Item?.lastReadAt ?? null,
nextToken: page.LastEvaluatedKey
? Buffer.from(JSON.stringify(page.LastEvaluatedKey)).toString('base64')
: null
})
} catch (error) {
console.error('[notifications/list] Exception:', error.message, error.stack)
return respond(500, { error: 'Failed to list notifications' })
}
}
|