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 | 6x 6x 6x 6x 13x 10x 10x 10x 2x 8x 2x 6x 1x 5x 4x 4x 3x 2x 184x 181x 396x 22x 22x 22x 1x 40x 21x 1x 20x 38x 2x 2x 1x 19x 21x 10x 10x 13x 13x 2x 2x 168x 6x | // @implements UC-WORKFLOW-007.1, UC-WORKFLOW-008.1, UC-WORKFLOW-009.1, UC-WORKFLOW-011.1
const { validateCondition } = require('./expressions/condition')
const { validateTimer } = require('./expressions/timers')
const { validateTransitionActions } = require('./emailTemplates')
const AUTO_KEYS = ['when']
function errorResponse(body) {
return {
valid: false,
errorResponse: {
statusCode: 400,
headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' },
body: JSON.stringify(body)
}
}
}
/**
* Validates the automatic part (`auto`) of one transition (UC-WORKFLOW-007.1).
* Since conditions are checked against the entity's new fields, a field
* deleted (or an option/state removed) while a condition still uses it is
* rejected here too.
*
* @returns {{ valid: true } | { valid: false, errorResponse: object }}
*/
function validateAutoTransition(field, transition, fields, options) {
const context = { fieldId: field.fieldId, from: transition.from, to: transition.to }
const { auto } = transition
if (!auto || typeof auto !== 'object' || Array.isArray(auto)) {
return errorResponse({ error: 'INVALID_AUTO_TRANSITION', ...context })
}
if (Object.keys(auto).some((key) => !AUTO_KEYS.includes(key))) {
return errorResponse({ error: 'INVALID_AUTO_TRANSITION', ...context })
}
// An automatic transition is fired by the system: no permission can guard it
if (transition.requiresPermission) {
return errorResponse({ error: 'AUTO_TRANSITION_WITH_PERMISSION', ...context })
}
if ('when' in auto) {
const result = validateCondition(auto.when, fields, options)
if (!result.valid) {
return errorResponse({ error: 'INVALID_WORKFLOW_CONDITION', ...context, ...result.error })
}
}
return { valid: true }
}
/**
* Validates workflow fields in an entity's fields array.
* Each workflow field must have a startStateId that references an existing
* state, and each automatic transition a valid condition.
*
* @param {Array} fields - The entity fields array from the request body
* @param {{ linkedEntities?: Map<string, object>, emailTemplates?: object[] }} [options]
* `linkedEntities`: the entities targeted by conditions on linked items (see
* expressions/linkedItems.js loadLinkedEntities) — without it, those rows are
* only checked for shape; `emailTemplates`: the entity's templates, which
* the transitions' Email actions refer to
* @returns {{ valid: boolean, errorResponse?: object }} - valid=true or an API error response to return directly
*/
function validateWorkflowFields(fields, options = {}) {
if (!Array.isArray(fields)) return { valid: true }
for (const field of fields) {
if (field.type !== 'workflow' || !field.workflowDefinition) continue
const { states = [], startStateId, transitions = [] } = field.workflowDefinition
const fieldName = field.name || field.fieldId || 'unknown'
if (!startStateId) {
return errorResponse({
error: `Workflow field "${fieldName}" is missing a start state (startStateId). Please set a start state.`
})
}
const stateIds = states.map((s) => s.id)
if (!stateIds.includes(startStateId)) {
return errorResponse({
error: `Workflow field "${fieldName}" has an invalid startStateId "${startStateId}". It must reference an existing state.`
})
}
// A state may have one timer, waking its items up (UC-WORKFLOW-009.1)
for (const state of states) {
if (state?.timer === undefined) continue
const result = validateTimer(state.timer, fields)
if (!result.valid) {
return errorResponse({
error: 'INVALID_WORKFLOW_TIMER',
fieldId: field.fieldId,
stateId: state.id,
code: result.code
})
}
}
for (const transition of Array.isArray(transitions) ? transitions : []) {
if (transition?.auto !== undefined) {
const result = validateAutoTransition(field, transition, fields, options)
if (!result.valid) return result
}
// Actions run when the transition fires (UC-WORKFLOW-011.1)
const actions = validateTransitionActions(transition?.actions, {
fields,
emailTemplates: options.emailTemplates,
linkedEntities: options.linkedEntities
})
if (!actions.valid) {
const { valid, ...details } = actions // eslint-disable-line no-unused-vars
return errorResponse({
...details,
error: 'INVALID_TRANSITION_ACTION',
fieldId: field.fieldId,
from: transition.from,
to: transition.to,
code: details.code
})
}
}
}
return { valid: true }
}
module.exports = { validateWorkflowFields }
|