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 108 109 110 111 112 113 114 115 116 117 | 1x 1x 1x 1x 1x 20x 1x 20x 20x 20x 1x 19x 18x 18x 4x 17x 14x 17x 13x 13x 18x 16x 16x 16x 16x 13x 2x 2x | const { DynamoDBClient } = require('@aws-sdk/client-dynamodb')
const { DynamoDBDocumentClient, QueryCommand, BatchGetCommand } = require('@aws-sdk/lib-dynamodb')
const { permissionChecker } = require('../utils/PermissionChecker')
const client = new DynamoDBClient({})
const docClient = DynamoDBDocumentClient.from(client)
const getCorsHeaders = () => ({
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
})
exports.handler = async (event) => {
try {
const userId = event.requestContext?.authorizer?.claims?.sub
if (!userId) {
return {
statusCode: 401,
headers: getCorsHeaders(),
body: JSON.stringify({ error: 'Not authenticated' })
}
}
// Query all memberships for this user. Strongly consistent so a role change
// (see PermissionChecker.getUserRoles) is reflected immediately here too —
// otherwise the UI can compute permissions from stale roleIds right after a
// reload and show/hide actions the backend's own (consistent) check disagrees with.
const membershipsResult = await docClient.send(
new QueryCommand({
TableName: process.env.FLOW_MEMBERSHIPS_TABLE_NAME,
KeyConditionExpression: 'userId = :userId',
FilterExpression: '#status = :status',
ExpressionAttributeNames: {
'#status': 'status'
},
ExpressionAttributeValues: {
':userId': userId,
':status': 'active'
},
ConsistentRead: true
})
)
const memberships = membershipsResult.Items || []
if (memberships.length === 0) {
return {
statusCode: 200,
headers: getCorsHeaders(),
body: JSON.stringify([])
}
}
// Batch get flow details
const flowIds = memberships.map((m) => m.flowId)
const flowsResult = await docClient.send(
new BatchGetCommand({
RequestItems: {
[process.env.FLOWS_TABLE_NAME]: {
Keys: flowIds.map((id) => ({ id }))
}
}
})
)
const flows = flowsResult.Responses?.[process.env.FLOWS_TABLE_NAME] || []
// Merge flow info with membership info, resolving the caller's own effective
// permissions here so the frontend doesn't need `roles:list` just to know what
// it's allowed to do (e.g. show/hide a "create" button).
const result = await Promise.all(
memberships.map(async (membership) => {
const flow = flows.find((f) => f.id === membership.flowId)
const isOwner = flow?.ownerId === userId
// The owner always holds the flow's system (admin) role, so their real
// permissions already resolve to full access — no need to special-case them.
const permissionsResult = await permissionChecker.getUserPermissions(
userId,
membership.flowId
)
const permissions = permissionsResult.permissions || []
return {
flowId: membership.flowId,
flowName: flow?.name || 'Unknown',
flowSlug: flow?.slug,
roleIds: membership.roleIds,
permissions,
status: membership.status,
joinedAt: membership.joinedAt,
createdAt: flow?.createdAt,
isOwner,
defaultLanguage: flow?.defaultLanguage || 'en',
enabledLanguages: flow?.enabledLanguages || ['en', 'fr'],
...(flow?.description && { description: flow.description }),
...(flow?.picture && { picture: flow.picture }),
...(flow?.icon && { icon: flow.icon }),
...(flow?.space && { space: flow.space })
}
})
)
return {
statusCode: 200,
headers: getCorsHeaders(),
body: JSON.stringify(result)
}
} catch (error) {
console.error('Error:', error)
return {
statusCode: 500,
headers: getCorsHeaders(),
body: JSON.stringify({ error: 'Failed to fetch flows' })
}
}
}
|