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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | 1x 1x 1x 1x 1x 1x 1x 1x 17x 17x 16x 16x 16x 16x 3x 13x 13x 13x 4x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 8x 2x 2x 1x | const { DynamoDBClient } = require('@aws-sdk/client-dynamodb')
const { DynamoDBDocumentClient, PutCommand, GetCommand } = require('@aws-sdk/lib-dynamodb')
const { randomUUID } = require('crypto')
const { requirePermission } = require('../utils/requirePermission')
const { RESERVED_ENTITY_IDS, VISIBILITY_LEVELS } = require('../utils/constants')
const client = new DynamoDBClient({})
const docClient = DynamoDBDocumentClient.from(client)
const createEntityHandler = async (event) => {
try {
const body = JSON.parse(event.body || '{}')
// Extract tenantId from JWT claims (Cognito authorizer)
const tenantId = event.requestContext?.authorizer?.claims?.['custom:tenantId']
const userId = event.requestContext?.authorizer?.claims?.sub
Iif (!tenantId) {
return {
statusCode: 401,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({ error: 'Missing tenant context' })
}
}
if (!body.name || !body.fields) {
return {
statusCode: 400,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({ error: 'Missing required fields: name, fields' })
}
}
// Extract entity name for validation (support both string and multilingual object)
const entityName =
typeof body.name === 'string'
? body.name
: body.name.en || body.name.fr || Object.values(body.name)[0] || ''
// Validate that entity name is not a reserved system ID
const proposedId = entityName.toLowerCase().replace(/[^a-z0-9-]/g, '-')
if (RESERVED_ENTITY_IDS.includes(proposedId)) {
return {
statusCode: 400,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({
error: `Entity name "${entityName}" conflicts with reserved system entity. Reserved names: ${RESERVED_ENTITY_IDS.join(', ')}`
})
}
}
// Validate visibility if provided
const visibility = body.visibility || 'flow-members'
Iif (!VISIBILITY_LEVELS.includes(visibility)) {
return {
statusCode: 400,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({
error: `Invalid visibility level. Must be one of: ${VISIBILITY_LEVELS.join(', ')}`
})
}
}
// Fetch tenant configuration for language defaults
let tenantDefaultLanguage = 'en'
let tenantEnabledLanguages = ['en', 'fr']
try {
const tenantResult = await docClient.send(
new GetCommand({
TableName: process.env.TENANTS_TABLE_NAME,
Key: { id: tenantId }
})
)
Eif (tenantResult.Item) {
tenantDefaultLanguage = tenantResult.Item.defaultLanguage || 'en'
tenantEnabledLanguages = tenantResult.Item.enabledLanguages || ['en', 'fr']
}
} catch (error) {
console.warn('Could not fetch tenant configuration, using defaults:', error)
// Continue with defaults if tenant fetch fails
}
const entity = {
id: randomUUID(),
tenantId: tenantId,
visibility: visibility,
isPublic: body.isPublic === true ? 'true' : 'false', // String for GSI (backward compatibility)
name: body.name,
fields: body.fields,
defaultLanguage: body.defaultLanguage || tenantDefaultLanguage,
enabledLanguages: body.enabledLanguages || tenantEnabledLanguages,
...(body.picture && { picture: body.picture }),
...(body.icon && { icon: body.icon }),
...(body.description && { description: body.description }),
...(body.publicPermissions &&
visibility === 'public' && { publicPermissions: body.publicPermissions }),
createdBy: userId,
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString()
}
await docClient.send(
new PutCommand({
TableName: process.env.TABLE_NAME,
Item: entity
})
)
return {
statusCode: 201,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify(entity)
}
} catch (error) {
console.error('Error:', error)
return {
statusCode: 500,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({ error: 'Failed to create entity' })
}
}
}
// Wrap with permission check: require 'entity:create' permission
exports.handler = requirePermission(createEntityHandler, {
permission: 'entity:create'
})
|