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 | 12x 12x 12x 12x 122x 74x 74x 74x 74x 211x 211x 504x 45x 64x 64x 30x 504x 103x 103x 12x 12x 103x 99x 99x 99x 99x 1x 99x 103x 103x 12x 103x 3x 3x 3x 3x 4x 4x 4x 4x 4x 3x 108x 108x 4x 8x 4x 4x 8x 8x 7x 7x 4x 4x 12x | // @implements UC-WORKFLOW-008.1, UC-WORKFLOW-011.1
/**
* Reads what condition rows on linked items (`{ via, field, ... }`, see
* ./condition.js) need: the entities their `via` fields point to, and the
* items an item links to through them.
*
* Bounded by construction: a `via` field is a single link (checked when the
* entity is saved), so one item per `via` field, read once per item write in
* one batch — never a Scan or a Query over an entity.
*/
const { GetCommand, BatchGetCommand } = require('@aws-sdk/lib-dynamodb')
const { isLinkableReference, linkedItemKey, referenceValues } = require('./condition')
const BATCH_GET_MAX_KEYS = 100
const BATCH_GET_MAX_ATTEMPTS = 3
function collectViaFieldIds(node, ids) {
if (!node || typeof node !== 'object') return
if (Array.isArray(node.all)) node.all.forEach((child) => collectViaFieldIds(child, ids))
if (Array.isArray(node.any)) node.any.forEach((child) => collectViaFieldIds(child, ids))
if (node.not) collectViaFieldIds(node.not, ids)
if (typeof node.via === 'string') ids.add(node.via)
}
/**
* The entity_ref fields used as `via` by the conditions of an entity's
* workflow transitions: automatic transitions and Email actions' `when`.
*
* @param {object[]} fields - Entity field definitions
* @returns {object[]}
*/
function viaFields(fields) {
const ids = new Set()
for (const field of fields || []) {
if (field.type !== 'workflow') continue
for (const transition of field.workflowDefinition?.transitions || []) {
collectViaFieldIds(transition?.auto?.when, ids)
for (const action of Array.isArray(transition?.actions) ? transition.actions : []) {
collectViaFieldIds(action?.when, ids)
}
}
}
return (fields || []).filter((field) => ids.has(field.fieldId) && isLinkableReference(field))
}
async function getEntities(docClient, entitiesTableName, entityIds) {
const entities = new Map()
for (const entityId of new Set(entityIds)) {
const result = await docClient.send(
new GetCommand({ TableName: entitiesTableName, Key: { id: entityId } })
)
if (result.Item) entities.set(entityId, result.Item)
}
return entities
}
/**
* The entities targeted by the `via` rows of an entity being saved, to
* validate those rows against their fields. An entity referencing itself is
* validated against its new fields.
*
* @param {object} params
* @param {import('@aws-sdk/lib-dynamodb').DynamoDBDocumentClient} params.docClient
* @param {string} params.entitiesTableName
* @param {object[]} params.fields - The entity's new field definitions
* @param {string} [params.entityId] - The entity being saved
* @param {string} params.flowId - Its flow
* @returns {Promise<Map<string, object>>} readable target entities by id
*/
async function loadLinkedEntities({ docClient, entitiesTableName, fields, entityId, flowId }) {
const targetIds = viaFields(fields).map((field) => field.reference.entityId)
const others = targetIds.filter((id) => id !== entityId)
const entities = await getEntities(docClient, entitiesTableName, others)
if (entityId && targetIds.includes(entityId)) {
entities.set(entityId, { id: entityId, flowId, fields })
}
return readableEntities(entities, flowId)
}
// Another flow's entity is only readable when public, as for any read
function readableEntities(entities, flowId) {
const readable = new Map()
for (const [id, entity] of entities) {
if (entity.flowId === flowId || entity.isPublic === 'true') readable.set(id, entity)
}
return readable
}
async function batchGetItems(docClient, itemsTableName, keys) {
const found = new Map()
for (let i = 0; i < keys.length; i += BATCH_GET_MAX_KEYS) {
let requestItems = { [itemsTableName]: { Keys: keys.slice(i, i + BATCH_GET_MAX_KEYS) } }
for (let attempt = 0; attempt < BATCH_GET_MAX_ATTEMPTS && requestItems; attempt++) {
const result = await docClient.send(new BatchGetCommand({ RequestItems: requestItems }))
for (const item of result.Responses?.[itemsTableName] || []) {
found.set(linkedItemKey(item.entityId, item.id), item)
}
const unprocessed = result.UnprocessedKeys
requestItems = unprocessed && Object.keys(unprocessed).length > 0 ? unprocessed : null
}
}
return found
}
/**
* The linked items the automatic transitions of an item write may look at:
* those the item links to through the `via` fields its entity's conditions
* use, when their entity is readable from the item's flow.
*
* @param {object} params
* @param {import('@aws-sdk/lib-dynamodb').DynamoDBDocumentClient} params.docClient
* @param {string} params.entitiesTableName
* @param {string} params.itemsTableName
* @param {object[]} params.fields - The item's entity fields
* @param {object} params.item - The item as it is about to be written
* @param {string} params.flowId - The item's flow
* @returns {Promise<{ entities: Map<string, object>, items: Map<string, object> } | undefined>}
* undefined when no condition looks at linked items
*/
async function loadLinkedItems({
docClient,
entitiesTableName,
itemsTableName,
fields,
item,
flowId
}) {
const links = viaFields(fields)
if (links.length === 0) return undefined
const entities = readableEntities(
await getEntities(
docClient,
entitiesTableName,
links.map((field) => field.reference.entityId)
),
flowId
)
const keys = new Map()
for (const field of links) {
const entityId = field.reference.entityId
if (!entities.has(entityId)) continue
const [id] = referenceValues(item[field.fieldId])
if (typeof id === 'string') keys.set(linkedItemKey(entityId, id), { entityId, id })
}
const items =
keys.size > 0 ? await batchGetItems(docClient, itemsTableName, [...keys.values()]) : new Map()
return { entities, items }
}
module.exports = { viaFields, loadLinkedEntities, loadLinkedItems }
|