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 | 1x 1x 1x 1x 1x 1x 1x 2x 5x 3x 3x 2x 1x 2x 1x 1x 1x 1x 1x 8x 8x 8x 6x 6x 1x 5x 5x 3x 2x 2x 2x 1x 1x 1x 1x 1x 1x | // @implements UC-SPACE-005.1
/**
* GET /v1/flows/{flowId}/membership-requests?status=pending|banned&nextToken=…
*
* A flow's requests of one status (pending ones oldest first, banned ones by
* ban date), one Query on `flowStatusIndex` a page at a time, with the
* requesters' names and nicknames (never their email).
*/
const { DynamoDBClient } = require('@aws-sdk/client-dynamodb')
const { DynamoDBDocumentClient, QueryCommand, BatchGetCommand } = require('@aws-sdk/lib-dynamodb')
const { requirePermission } = require('../utils/requirePermission')
const { respond, verifiedPathFlowId } = require('./shared')
const docClient = DynamoDBDocumentClient.from(new DynamoDBClient({}))
const LISTED_STATUSES = ['pending', 'banned']
const PAGE_SIZE = 25
const encodeToken = (key) => (key ? Buffer.from(JSON.stringify(key)).toString('base64url') : null)
function decodeToken(token, flowId) {
if (!token) return undefined
try {
const key = JSON.parse(Buffer.from(token, 'base64url').toString('utf8'))
// A token of another flow would page through that flow's index
return key && key.flowId === flowId ? key : null
} catch {
return null
}
}
async function loadUsers(userIds) {
if (userIds.length === 0) return new Map()
const result = await docClient.send(
new BatchGetCommand({
RequestItems: {
[process.env.USERS_TABLE_NAME]: {
Keys: userIds.map((id) => ({ id })),
ProjectionExpression: 'id, #name, nickname, profilePicture',
ExpressionAttributeNames: { '#name': 'name' }
}
}
})
)
const users = result.Responses?.[process.env.USERS_TABLE_NAME] || []
return new Map(users.map((user) => [user.id, user]))
}
const listHandler = async (event) => {
try {
const flowId = await verifiedPathFlowId(event)
if (!flowId) return respond(403, { error: 'Access denied - not your active flow' })
const status = event.queryStringParameters?.status || 'pending'
if (!LISTED_STATUSES.includes(status)) {
return respond(400, { error: `status must be one of: ${LISTED_STATUSES.join(', ')}` })
}
const startKey = decodeToken(event.queryStringParameters?.nextToken, flowId)
if (startKey === null) return respond(400, { error: 'Invalid nextToken' })
const result = await docClient.send(
new QueryCommand({
TableName: process.env.MEMBERSHIP_REQUESTS_TABLE_NAME,
IndexName: 'flowStatusIndex',
KeyConditionExpression: 'flowId = :flowId AND begins_with(statusCreatedAt, :status)',
ExpressionAttributeValues: { ':flowId': flowId, ':status': `${status}#` },
ScanIndexForward: true,
Limit: PAGE_SIZE,
ExclusiveStartKey: startKey
})
)
const requests = result.Items || []
const users = await loadUsers(requests.map((request) => request.userId))
return respond(200, {
requests: requests.map((request) => {
const user = users.get(request.userId)
return {
userId: request.userId,
status: request.status,
name: user?.name,
nickname: user?.nickname,
profilePicture: user?.profilePicture || null,
createdAt: request.createdAt,
...(request.decidedAt && { decidedAt: request.decidedAt }),
...(request.answers && { answers: request.answers })
}
}),
nextToken: encodeToken(result.LastEvaluatedKey)
})
} catch (error) {
console.error('Error listing membership requests:', error)
return respond(500, { error: 'Failed to list membership requests' })
}
}
exports.handler = requirePermission(listHandler, { permission: 'members:approve' })
exports.listHandler = listHandler
|