Files
wekan/models/lib/ruleParts.js
T
Lauri Ojansivu 69eeb00c84 Let a rule have several triggers (any fires it) and several actions (in order).
Maintainer decision for #4294 and #2953: a rule keeps its own trigger and
action and may add up to ten more of each. It fires when any of its
triggers fires - once per activity, even when several match - and runs its
actions in order, its own first. Rule details lists the extra parts with
Add another trigger / Add another action and Remove; the new
rules.addPart/removePart methods keep the board-admin and cross-board
destination checks of rules.createRule and refuse a button as an extra
trigger.

Ordinary, scheduled and button rules, the durable Sync rule plan, rule
History (snapshot, part edits and restore), deletion, board copy, WeKan
export/import, the rules JSON export/import, the REST representation and
the admin rules report all handle every part. Two rule-engine test
harnesses now receive the new helpers they evaluate.

New strings are in en.i18n.json, pending translation.

Thanks to xet7 !

Fixes #2953,
2026-09-29 14:34:18 +03:00

52 lines
2.1 KiB
JavaScript

'use strict';
// A rule's triggers and actions (maintainer decision 2026-09-29, #4294 /
// #2953): a rule keeps its original triggerId/actionId and may add more in
// extraTriggerIds/extraActionIds. It fires when ANY of its triggers fires,
// once per activity, and runs its actions in order - the original first.
const MAX_EXTRA_RULE_PARTS = 10;
function ids(first, extra) {
const list = [first].concat(Array.isArray(extra) ? extra : []);
return [...new Set(list.filter(id => typeof id === 'string' && id))];
}
function ruleTriggerIds(rule) {
return rule ? ids(rule.triggerId, rule.extraTriggerIds) : [];
}
function ruleActionIds(rule) {
return rule ? ids(rule.actionId, rule.extraActionIds) : [];
}
// A rule matched through several of its triggers still runs once.
function uniqueRules(rules) {
const seen = new Set();
return (rules || []).filter(rule => {
if (!rule || !rule._id || seen.has(rule._id)) return false;
seen.add(rule._id);
return true;
});
}
// The selector for "the rule this trigger belongs to".
function ruleForTriggerSelector(triggerId) {
return { $or: [{ triggerId }, { extraTriggerIds: triggerId }] };
}
// A copied or imported rule points at the copies of ALL its triggers and
// actions. An extra part that was not copied is dropped rather than left
// pointing at the source board's document.
function remapRuleParts(rule, triggersMap, actionsMap) {
// Extras are unique and never repeat the rule's own trigger/action.
const mapped = (ids, map, own) => (Array.isArray(ids)
? [...new Set(ids.map(id => map[id]).filter(id => id && id !== own))] : ids);
rule.triggerId = triggersMap[rule.triggerId];
rule.actionId = actionsMap[rule.actionId];
if (rule.extraTriggerIds !== undefined) rule.extraTriggerIds = mapped(rule.extraTriggerIds, triggersMap, rule.triggerId);
if (rule.extraActionIds !== undefined) rule.extraActionIds = mapped(rule.extraActionIds, actionsMap, rule.actionId);
return rule;
}
module.exports = { MAX_EXTRA_RULE_PARTS, ruleTriggerIds, ruleActionIds, uniqueRules, ruleForTriggerSelector, remapRuleParts };