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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | 6x 6x 6x 6x 6x 1x 1x 2x 16x 16x 16x 16x 16x 22x 22x 22x 6x 6x 16x 16x 16x 16x 16x 16x 16x 16x 16x 5x 5x 5x 16x 16x 1x 1x 1x 1x 1x 1x 16x 16x 5x 16x 6x | const { UpdateCommand, DeleteCommand, GetCommand } = require('@aws-sdk/lib-dynamodb')
const { findEntityNameConflict, findPublicEntityNameConflictForOwner } = require('./nameUniqueness')
const { batchWrite } = require('./batchWrite')
const {
diffCatalogSearchTokens,
buildCatalogSearchPutRequest,
buildCatalogSearchDeleteRequest
} = require('./catalogSearchIndex')
function appendNameSuffix(name, suffix) {
if (typeof name === 'string') return `${name} (${suffix})`
Eif (name && typeof name === 'object') {
return Object.fromEntries(
Object.entries(name).map(([lang, value]) => [lang, `${value} (${suffix})`])
)
}
return name
}
/**
* Finds a name for the transferred entity that doesn't collide with anything
* already in the new owner's flow, nor with another public entity elsewhere
* among the new owner's flows (public entity names must be unique per owner
* — see findPublicEntityNameConflictForOwner). Tries "name (2)", "name (3)",
* ... until a free one is found: the suffix must increment past whatever's
* already taken, since an earlier auto-renamed transfer may already occupy
* "name (2)".
*/
async function resolveTransferName(
docClient,
{ entitiesTableName, flowsTableName, entityId, newFlowId, baseName }
) {
const newFlowResult = await docClient.send(
new GetCommand({ TableName: flowsTableName, Key: { id: newFlowId } })
)
const newOwnerId = newFlowResult.Item?.ownerId
let candidateName = baseName
let suffix = 1
for (;;) {
const flowConflict = await findEntityNameConflict(docClient, {
tableName: entitiesTableName,
flowId: newFlowId,
name: candidateName,
excludeEntityId: entityId
})
const ownerConflict =
!flowConflict && newOwnerId
? await findPublicEntityNameConflictForOwner(docClient, {
entitiesTableName,
flowsTableName,
ownerId: newOwnerId,
name: candidateName,
excludeEntityId: entityId
})
: null
if (!flowConflict && !ownerConflict) return candidateName
suffix += 1
candidateName = appendNameSuffix(baseName, suffix)
}
}
/**
* Reassigns a public entity to a new owner flow — used when the previous
* owner (or its whole flow) is being deleted and the entity has active
* external links. The entity is never deleted in this path: ownership moves,
* items stay untouched, and the new owner's write access follows naturally
* since every write handler re-checks `entity.flowId`/`item.flowId` fresh on
* every request (see items/create.js, update.js, delete.js).
*
* The name-uniqueness invariant enforced at create/update time (per-flow,
* and per-owner for public entities) isn't otherwise checked on this path —
* ownership moves the entity into a flow/owner namespace it was never
* validated against — so it's re-checked and auto-renamed on conflict here.
*
* @param {import('@aws-sdk/lib-dynamodb').DynamoDBDocumentClient} docClient
* @param {{
* entitiesTableName: string,
* entityLinksTableName: string,
* flowsTableName: string,
* fieldValueIndexTableName?: string,
* entityId: string,
* previousFlowId: string,
* newFlowId: string,
* otherLinkedFlowIds: string[]
* }} params
*/
async function transferEntityOwnership(
docClient,
{
entitiesTableName,
entityLinksTableName,
flowsTableName,
fieldValueIndexTableName,
entityId,
previousFlowId,
newFlowId,
otherLinkedFlowIds
}
) {
const transferredAt = new Date().toISOString()
const entityResult = await docClient.send(
new GetCommand({ TableName: entitiesTableName, Key: { id: entityId } })
)
const baseName = entityResult.Item?.name
const resolvedName = await resolveTransferName(docClient, {
entitiesTableName,
flowsTableName,
entityId,
newFlowId,
baseName
})
const renamed = resolvedName !== baseName
const updateExpressionParts = [
'flowId = :newFlowId',
'transferredFromFlowId = :previousFlowId',
'transferredAt = :transferredAt'
]
const expressionAttributeValues = {
':newFlowId': newFlowId,
':previousFlowId': previousFlowId,
':transferredAt': transferredAt
}
const expressionAttributeNames = {}
if (renamed) {
updateExpressionParts.push('#name = :name')
expressionAttributeNames['#name'] = 'name'
expressionAttributeValues[':name'] = resolvedName
}
await docClient.send(
new UpdateCommand({
TableName: entitiesTableName,
Key: { id: entityId },
UpdateExpression: 'SET ' + updateExpressionParts.join(', '),
...(Object.keys(expressionAttributeNames).length > 0 && {
ExpressionAttributeNames: expressionAttributeNames
}),
ExpressionAttributeValues: expressionAttributeValues
})
)
// A transfer-triggered auto-rename (see resolveTransferName above) must
// keep the catalog-search index in sync too — entities/update.js isn't
// involved in this path at all, so this is the only place that can catch
// it. Best-effort/non-transactional, same reasoning as entities/update.js.
if (renamed && fieldValueIndexTableName) {
const { toRelease, toClaim } = diffCatalogSearchTokens(baseName, resolvedName)
const catalogSearchRequests = [
...toRelease.map(({ kind, token }) => buildCatalogSearchDeleteRequest(kind, token, entityId)),
...toClaim.map(({ kind, token }) => buildCatalogSearchPutRequest(kind, token, entityId))
]
Eif (catalogSearchRequests.length > 0) {
try {
await batchWrite(docClient, fieldValueIndexTableName, catalogSearchRequests)
} catch (catalogSearchError) {
console.error(
'Failed to update catalog-search index rows on ownership transfer:',
catalogSearchError
)
}
}
}
// The new owner is no longer a linker — link.js forbids linking your own
// entity, so this row would otherwise be an inconsistent self-link.
await docClient.send(
new DeleteCommand({
TableName: entityLinksTableName,
Key: { flowId: newFlowId, entityId }
})
)
// Remaining links denormalize sourceFlowId for fast reads (see
// EntityLinksTable schema in PUBLIC_ENTITIES.md) — keep it in sync so it
// doesn't keep pointing at the flow that no longer owns the entity.
await Promise.all(
(otherLinkedFlowIds || []).map((flowId) =>
docClient.send(
new UpdateCommand({
TableName: entityLinksTableName,
Key: { flowId, entityId },
UpdateExpression: 'SET sourceFlowId = :newFlowId',
ExpressionAttributeValues: { ':newFlowId': newFlowId }
})
)
)
)
return {
entityId,
previousFlowId,
newFlowId,
transferredAt,
...(renamed && { renamedTo: resolvedName })
}
}
module.exports = { transferEntityOwnership }
|