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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 18x 18x 18x 18x 18x 18x 18x 18x 1x 17x 17x 17x 17x 17x 2x 15x 15x 15x 15x 15x 15x 1x | /**
* GET /v1/entities/{entityId}/fork-preview
*
* Read-only counterpart to fork.js's credit preflight — same accessibility
* rule, same cost estimate (utils/forkCostEstimate.js) and same balance check,
* but it never writes anything and never debits. Lets the fork confirmation
* dialog show an item count, the estimated cost and whether the caller can
* afford it *before* they commit, so the answer here matches what fork.js will
* decide. The estimate is an approximation (see forkCostEstimate.js) —
* documented on the response schema.
*/
const { getActiveFlowId } = require('../shared/activeFlow')
const { DynamoDBClient } = require('@aws-sdk/client-dynamodb')
const { DynamoDBDocumentClient, GetCommand } = require('@aws-sdk/lib-dynamodb')
const { requirePermission } = require('../utils/requirePermission')
const { permissionChecker } = require('../utils/PermissionChecker')
const { getCreditsBalance } = require('../utils/creditsLedger')
const { getPricingConfig } = require('../utils/pricingConfig')
const { estimateForkCost, loadForkEstimateInputs } = require('../utils/forkCostEstimate')
const client = new DynamoDBClient({})
const docClient = DynamoDBDocumentClient.from(client)
const forkPreviewEntityHandler = async (event) => {
try {
const sourceEntityId = event.pathParameters?.entityId
const targetFlowId = await getActiveFlowId(event)
const userId = event.requestContext?.authorizer?.claims?.sub
const copyItems = event.queryStringParameters?.copyItems === 'true'
Iif (!sourceEntityId) {
return {
statusCode: 400,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({ error: 'Missing source entity id' })
}
}
const sourceResult = await docClient.send(
new GetCommand({
TableName: process.env.TABLE_NAME,
Key: { id: sourceEntityId }
})
)
if (!sourceResult.Item) {
return {
statusCode: 404,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({ error: 'Source entity not found' })
}
}
const sourceEntity = sourceResult.Item
const isSourcePublic = sourceEntity.visibility === 'public'
const isOwnEntity = sourceEntity.flowId === targetFlowId
const cannotReadRestrictedSource =
sourceEntity.visibility === 'restricted' &&
!permissionChecker.grantsEntityAndItemsRead(
(await permissionChecker.getUserPermissions(userId, targetFlowId)).permissions,
sourceEntity
)
if ((!isSourcePublic && !isOwnEntity) || cannotReadRestrictedSource) {
return {
statusCode: 403,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({ error: 'This entity cannot be forked' })
}
}
const { items, totalItemCount } = await loadForkEstimateInputs({
docClient,
entityId: sourceEntityId,
copyItems
})
const pricing = await getPricingConfig(docClient, process.env.PRICING_CONFIG_TABLE_NAME)
const { totalCredits: estimatedCredits } = await estimateForkCost({
fields: Array.isArray(sourceEntity.fields) ? sourceEntity.fields : [],
forms: Array.isArray(sourceEntity.forms) ? sourceEntity.forms : [],
picture: sourceEntity.picture,
items,
totalItemCount,
pricing
})
// Same rule as fork.js's preflight, including the platform-admin bypass.
const { balance, isPlatformAdmin } = userId
? await getCreditsBalance(docClient, process.env.USERS_TABLE_NAME, userId)
: { balance: 0, isPlatformAdmin: false }
const insufficientCredits = !userId || (!isPlatformAdmin && balance < estimatedCredits)
return {
statusCode: 200,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({
itemCount: totalItemCount,
estimatedCredits,
balance,
insufficientCredits
})
}
} catch (error) {
console.error('[fork-preview] Exception:', error.message, error.stack)
return {
statusCode: 500,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({ error: 'Failed to preview fork' })
}
}
}
exports.handler = requirePermission(forkPreviewEntityHandler, {
permission: 'entities:create'
})
|