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 | 2x 70x 70x 70x 70x 51x 38x 38x 13x 13x 5x 5x 8x 3x 3x 5x 3x 3x 2x 70x 17x 7x 17x 7x 7x 6x 17x 17x 6x 3x 3x 17x 6x 6x 5x 17x 2x | const { buildDeletedPlaceholder } = require('./deletedPlaceholder')
/**
* Resolves an entity's `entity_ref` fields for import: a field whose target
* is another entity in the *same* bundle gets its `reference.entityId`
* rewritten to that entity's final (newly created or merge-target) id. A
* field targeting something outside the bundle is kept as-is when that
* target is still accessible from the destination flow (owned by it, or
* public — resolvable live, no rewrite needed), otherwise dropped. A
* mandatory field that would have to be dropped fails the whole entity
* instead (a schema can't declare a field mandatory with no way to supply
* it), reported via `droppedMandatoryField`.
*
* @param {Array} fields
* @param {Map<string,string>} tempIdToFinalEntityId - bundle tempId -> final entityId
* @param {(targetId: string) => Promise<boolean>} isAccessibleFromDestination
* @returns {Promise<{ fields: Array, droppedFieldsCount: number, droppedMandatoryField: object|null }>}
*/
async function resolveEntityRefFields(fields, tempIdToFinalEntityId, isAccessibleFromDestination) {
const resolvedFields = []
let droppedFieldsCount = 0
let droppedMandatoryField = null
for (const field of fields || []) {
if (field.type !== 'entity_ref' || !field.reference?.entityId) {
resolvedFields.push(field)
continue
}
const targetId = field.reference.entityId
if (tempIdToFinalEntityId.has(targetId)) {
resolvedFields.push({
...field,
reference: { ...field.reference, entityId: tempIdToFinalEntityId.get(targetId) }
})
continue
}
if (await isAccessibleFromDestination(targetId)) {
resolvedFields.push(field)
continue
}
if (field.mandatory) {
droppedMandatoryField = field
break
}
droppedFieldsCount += 1
}
return { fields: resolvedFields, droppedFieldsCount, droppedMandatoryField }
}
/**
* Remaps `entity_ref` field VALUES on a single item (in place) through
* `originalItemIdToNewItemId` — item ids re-created earlier in the same
* import (see entities/import.js's item-creation pass). A value absent from
* that map is left untouched: it refers to an item outside the bundle,
* still valid wherever it already lives.
*/
function remapEntityRefValues(item, entityRefFieldIds, originalItemIdToNewItemId) {
const remapOne = (value) =>
originalItemIdToNewItemId.has(value) ? originalItemIdToNewItemId.get(value) : value
for (const fieldId of entityRefFieldIds) {
const value = item[fieldId]
if (!value) continue
item[fieldId] = Array.isArray(value) ? value.map(remapOne) : remapOne(value)
}
}
/**
* Replaces `user`-type field VALUES on a single item (in place) that
* reference a userId who isn't a member of the destination flow with the
* `::deleted::` placeholder — mirrors the cascade-on-delete convention used
* elsewhere (utils/deletedPlaceholder.js) so downstream validation/UI treats
* an unresolved cross-flow membership the same as a removed member.
*
* @param {object} item
* @param {string[]} userFieldIds
* @param {(userId: string) => Promise<boolean>} isMember
* @param {(userId: string) => Promise<string>} getUserLabel
* @returns {Promise<number>} number of values replaced
*/
async function resolveUserFieldValues(item, userFieldIds, isMember, getUserLabel) {
let unresolvedCount = 0
const resolveOne = async (userId) => {
if (await isMember(userId)) return userId
unresolvedCount += 1
return buildDeletedPlaceholder(await getUserLabel(userId))
}
for (const fieldId of userFieldIds) {
const value = item[fieldId]
if (!value) continue
item[fieldId] = Array.isArray(value)
? await Promise.all(value.map(resolveOne))
: await resolveOne(value)
}
return unresolvedCount
}
module.exports = {
resolveEntityRefFields,
remapEntityRefValues,
resolveUserFieldValues
}
|