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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 22x 22x 21x 21x 21x 21x 3x 18x 18x 18x 18x 15x 15x 15x 4x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 10x 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 { validateWorkflowFields } = require('../utils/validateWorkflowFields')
const { validateFieldNames } = require('../utils/validateFieldNames')
const { resolveForms } = require('../utils/resolveEntityForms')
const client = new DynamoDBClient({})
const docClient = DynamoDBDocumentClient.from(client)
const createEntityHandler = async (event) => {
try {
const body = JSON.parse(event.body || '{}')
// Extract flowId from JWT claims (Cognito authorizer)
const flowId = event.requestContext?.authorizer?.claims?.['custom:flowId']
const userId = event.requestContext?.authorizer?.claims?.sub
Iif (!flowId) {
return {
statusCode: 401,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({ error: 'Missing flow 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' })
}
}
// Validate workflow fields: each workflow field must have a valid startStateId
const workflowValidation = validateWorkflowFields(body.fields)
Iif (!workflowValidation.valid) return workflowValidation.errorResponse
// Validate field names: every field must have a non-empty name
const fieldNameValidation = validateFieldNames(body.fields)
if (!fieldNameValidation.valid) return fieldNameValidation.errorResponse
// 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(', ')}`
})
}
}
const visibility = body.visibility ?? 'flow-members'
// Validate visibility if provided
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 flow configuration for language defaults
let flowDefaultLanguage = 'en'
let flowEnabledLanguages = ['en', 'fr']
try {
const flowResult = await docClient.send(
new GetCommand({
TableName: process.env.FLOWS_TABLE_NAME,
Key: { id: flowId }
})
)
Eif (flowResult.Item) {
flowDefaultLanguage = flowResult.Item.defaultLanguage || 'en'
flowEnabledLanguages = flowResult.Item.enabledLanguages || ['en', 'fr']
}
} catch (error) {
console.warn('Could not fetch flow configuration, using defaults:', error)
// Continue with defaults if flow fetch fails
}
const forms = resolveForms({ fields: body.fields, forms: body.forms })
const entity = {
id: randomUUID(),
flowId,
visibility: visibility,
name: body.name,
fields: body.fields,
defaultLanguage: body.defaultLanguage || flowDefaultLanguage,
enabledLanguages: body.enabledLanguages || flowEnabledLanguages,
...(body.picture && { picture: body.picture }),
...(body.icon && { icon: body.icon }),
...(body.description && { description: body.description }),
...(forms && { forms }),
...(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'
})
|