All files / utils/expressions timers.js

100% Statements 33/33
97.43% Branches 38/39
100% Functions 4/4
100% Lines 27/27

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                              17x   17x 17x 17x   17x                   20x 2x   25x 1x   17x   15x 7x 4x   3x         11x 8x 2x   6x       2x   4x                       22x 8x 8x 6x 6x 5x     17x  
// @implements UC-WORKFLOW-009.1
/**
 * State timers of workflows (see docs/architecture/AUTOMATION.md "Timers"):
 * a workflow state may have one timer, which wakes an item in that state up
 * so its automatic transitions are evaluated again — the passing of time can
 * make a condition true ("updated more than 3 days ago", "due date passed").
 *
 *   { after: "3d" }                  3 days after the item's last save
 *   { at: "f_due", offset: "+1d" }   one day after the item's Due date
 *
 * Each save of the item (entering the state included) starts the timer
 * again; a wake-up that moves the item to another state starts the new
 * state's timer; a wake-up that leaves it in place ends it.
 */
 
const { durationMs } = require('./condition')
 
const AFTER_PATTERN = /^\d+[hd]$/
const OFFSET_PATTERN = /^[+-]\d+[hd]$/
const TIMER_KEYS = ['after', 'at', 'offset']
// A shorter timer could turn a periodic reminder into a fast loop
const MIN_AFTER_MS = 60 * 60 * 1000
 
/**
 * Validates a state's `timer` against the entity's fields.
 *
 * @param {unknown} timer
 * @param {object[]} fields - The entity's (new) field definitions
 * @returns {{ valid: true } | { valid: false, code: string }}
 */
function validateTimer(timer, fields) {
  if (!timer || typeof timer !== 'object' || Array.isArray(timer)) {
    return { valid: false, code: 'INVALID_TIMER' }
  }
  if (Object.keys(timer).some((key) => !TIMER_KEYS.includes(key))) {
    return { valid: false, code: 'INVALID_TIMER' }
  }
  if ('after' in timer === 'at' in timer) return { valid: false, code: 'INVALID_TIMER' }
 
  if ('after' in timer) {
    if ('offset' in timer || typeof timer.after !== 'string' || !AFTER_PATTERN.test(timer.after)) {
      return { valid: false, code: 'INVALID_TIMER' }
    }
    return durationMs(timer.after) < MIN_AFTER_MS
      ? { valid: false, code: 'TIMER_TOO_SHORT' }
      : { valid: true }
  }
 
  const atField = (fields || []).find((field) => field.fieldId === timer.at)
  if (timer.at !== 'createdAt' && atField?.type !== 'date') {
    return { valid: false, code: 'INVALID_TIMER_DATE_FIELD' }
  }
  if (
    'offset' in timer &&
    (typeof timer.offset !== 'string' || !OFFSET_PATTERN.test(timer.offset))
  ) {
    return { valid: false, code: 'INVALID_TIMER' }
  }
  return { valid: true }
}
 
/**
 * When a timer started at `startMs` is due, for an item.
 *
 * @param {{ after?: string, at?: string, offset?: string }} timer
 * @param {object} item
 * @param {number} startMs - When the timer starts: the item's save
 * @returns {number | null} ms, or null when it can't be due (empty date)
 */
function timerDueAt(timer, item, startMs) {
  if (timer.after) return startMs + durationMs(timer.after)
  const value = item[timer.at]
  if (typeof value !== 'string' || value === '') return null
  const base = Date.parse(value)
  if (Number.isNaN(base)) return null
  return base + (timer.offset ? durationMs(timer.offset) : 0)
}
 
module.exports = { MIN_AFTER_MS, validateTimer, timerDueAt }