Files
wekan/models/lib/ruleCardTitleFilter.js
T
Lauri Ojansivu 8d1fae4f7d Convert client-reachable models/lib helpers from CommonJS to ES modules (1/2).
The rspack client build treats a `module.exports` file imported into the
client bundle as an ES module and throws
"ES Modules may not assign module.exports" at evaluation time, aborting
the whole client module chain (No such template: swimlane / notifications
/ boardButtons, blank board, missing top bar). This is not an rspack
version regression (the lockfile has pinned ~1.7.x since v8.40); it is
that WeKan added CommonJS helpers to the client bundle.

Convert the 42 client-reachable pure-logic models/lib helpers from
`module.exports = { ... }` to `export { ... }`, and calendarFilter's
internal require('./swimlaneFilter') to an import with a .js extension
(node ESM needs it; rspack/reify tolerate it). Server-only helpers that
use node built-ins (fs/path/child_process) stay CommonJS. The plain-Node
.cjs unit tests for these helpers now use `await import(...js)` (wrapped
in an async IIFE); a source-guard in importVirtualMembers was updated
from module.exports to export. Verified: the full tests/*.test.cjs suite
has the SAME 7 pre-existing failures as before this change and no new
ones.
2026-07-23 18:55:30 +03:00

81 lines
3.5 KiB
JavaScript

'use strict';
// #2345 "basic card title filter rules issue".
//
// The board-rule triggers ("when a card is created / moved / archived ...")
// offer a magnifier popup that lets the user restrict the trigger to cards
// whose title matches a filter. Three things were broken:
//
// 1. The generic "when a card is moved" and the archive/unarchive triggers
// never stored the filter at all — worse, they stored NO `cardTitle`
// field, and because the server matches triggers with
// `cardTitle: { $in: [...] }`, a trigger document that lacks the field
// can never match an activity that carries a card title, so those rules
// never fired for anything.
// 2. A stored filter never appeared in the trigger's human-readable
// description ("When a card is moved to list X" — no trace of "Part A"),
// which is exactly what the issue reporter complained about.
// 3. Old trigger documents created before the fix (without a `cardTitle`
// field) must keep behaving as "match every card".
//
// These helpers are pure (no Meteor imports) so both the client wizard and
// server/rulesHelper.js share one tested implementation.
// Normalize the raw popup input into the value stored on the trigger document.
// An empty / whitespace-only / missing filter means "match every card", which
// the rules engine expresses with the '*' wildcard (this matches the UI note:
// "leave a field empty to match every possible value").
function cardTitleFilterOrWildcard(raw) {
if (typeof raw !== 'string') return '*';
const trimmed = raw.trim();
return trimmed === '' ? '*' : trimmed;
}
// Build the list of trigger `cardTitle` values that match an activity whose
// card title is `activityCardTitle`, used as `cardTitle: { $in: [...] }` when
// looking up triggers. A trigger matches when its stored filter is:
// - the exact card title,
// - any single word of the card title (historic behaviour: the title is
// split on non-word characters),
// - the '*' wildcard (no filter set), or
// - null / a missing field (legacy triggers saved without a cardTitle —
// in MongoDB `$in: [null, ...]` also matches documents lacking the field,
// which keeps pre-fix rules firing as "every card").
function cardTitleMatchList(activityCardTitle) {
const matches = ['*', null];
if (activityCardTitle !== undefined && activityCardTitle !== null) {
const title = String(activityCardTitle);
return title
.split(/\W/)
.filter(word => word !== '')
.concat([title])
.concat(matches);
}
return matches;
}
// Pure predicate mirroring the $in semantics above: does a trigger stored with
// `filter` fire for a card titled `cardTitle`? (A missing/null filter — legacy
// trigger — matches everything, like the wildcard.)
function cardTitleFilterMatches(filter, cardTitle) {
if (filter === undefined || filter === null) return true;
return cardTitleMatchList(cardTitle).includes(filter);
}
// Append the filter to the trigger's human-readable description so the rule
// list / rule details show it ("when a card is moved to list x (card title
// filter: "Part A")"). A wildcard/empty filter leaves the description alone.
function appendCardTitleFilterToDesc(desc, cardTitle, label) {
const base = typeof desc === 'string' ? desc : '';
if (!cardTitle || cardTitle === '*') return base;
const tag = label && typeof label === 'string' ? label : 'card title filter';
return `${base} (${tag}: "${cardTitle}")`;
}
export {
cardTitleFilterOrWildcard,
cardTitleMatchList,
cardTitleFilterMatches,
appendCardTitleFilterToDesc,
};