All files / items newItemData.js

100% Statements 9/9
100% Branches 4/4
100% Functions 1/1
100% Lines 9/9

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            5x 5x                   43x 77x 6x       43x                             43x   43x     5x  
/**
 * Server-side completion of a new item's data before its write, shared by
 * items/create.js and the approval of a membership request
 * (membership-requests/decide.js), so an item created either way gets the same
 * workflow start states and automatic transitions.
 */
const { resolveAutomaticTransitions } = require('./workflow-transitions')
const { loadLinkedItems } = require('../utils/expressions/linkedItems')
 
/**
 * Sets every workflow field to its start state (never trusted from the client:
 * the create form doesn't show workflow fields), then fires the automatic
 * transitions leaving those states, in memory (UC-WORKFLOW-007.1).
 * Mutates and returns `data`.
 * @returns {Promise<{ data: object, wakeUpAt: string | undefined }>}
 */
async function prepareNewItemData({ docClient, fields, data, actor, now, flowId }) {
  for (const field of fields) {
    if (field.type === 'workflow' && field.workflowDefinition?.startStateId) {
      data[field.fieldId] = field.workflowDefinition.startStateId
    }
  }
 
  const automatic = resolveAutomaticTransitions({
    fields,
    before: {},
    after: data,
    actor,
    now,
    linked: await loadLinkedItems({
      docClient,
      entitiesTableName: process.env.ENTITIES_TABLE_NAME,
      itemsTableName: process.env.TABLE_NAME,
      fields,
      item: data,
      flowId
    })
  })
  Object.assign(data, automatic.changes, automatic.system)
 
  return { data, wakeUpAt: automatic.wakeUpAt }
}
 
module.exports = { prepareNewItemData }