Files
wekan/server/lib/advancedFilterMatch.js
T
Lauri Ojansivu b8a6de1c89 Let a rule's trigger/action be edited in place, and email the card's description too.
#2713 asked for two things.

Editing a rule used to mean delete it and rebuild it from scratch, losing
its position and its identity. rulesList's rule row gets a second "Edit
trigger/action" button next to the existing title-rename pencil, opening
the same trigger/action wizard used to create a rule (rulesMain.js /
rulesTriggers / rulesActions and the board/card/checklist/mail action
templates), pre-filled with the rule's current title. A new server method,
rules.updateRule (server/rulesButton.js), mirrors rules.createRule's
authorization and cross-board destination checks but REPLACES the existing
trigger/action documents in place (full-document update by their existing
_id, not remove+insert), so the rule keeps its own _id, and its trigger/
action keep theirs, across an edit. Every action-template click handler
that used to call rules.createRule or insert Triggers/Actions/Rules
directly now goes through one shared helper,
client/components/rules/rulesSaveHelper.js, which picks create vs. update
based on whether the wizard was opened to edit an existing rule.

The "send an email" rule action already appended the card's title and a
link to it automatically (#3301), even when the user's own template used
none of the {card}/{cardLink} tokens; it did not do the same for the card's
description. server/rulesHelper.js now appends "Description: ..." to the
footer alongside the existing title/link lines, using the vars.description
value buildRuleVars already computed.

Actually attaching the card's FILE ATTACHMENTS to the outgoing email is
deferred - see CHANGELOG TODO Later. Every WeKan email today goes through
either Meteor's `Email.sendAsync`/`EmailLocalization.sendEmail`, which take
{ to, from, subject, text }, with no `attachments` parameter anywhere in
this codebase's call sites; wiring a real file through would need the
mailer wrapper itself to grow attachment support and code to read the file
back out of whichever storage backend (local disk / GridFS / S3) holds it,
which is mailer-level scope well past this rule action.

tests/ruleUpdateInPlace.test.cjs pins rules.updateRule's authorization,
its RuleBleed cross-board guard, and - the part that actually makes it an
edit rather than a second create - that it keeps the rule/trigger/action
_ids stable, including a logic-level round trip against a small in-memory
mock proving a changed actionType does not leave the old type's fields
behind. tests/ruleEmailIncludesDescription.test.cjs pins the new
description line, guarded the same way the existing title/link lines are
(only appended when non-empty).
tests/ruleCardActionSchema.test.cjs (#6630's guard, that desc lives on the
Action document and never leaks onto the Rule document) is updated for the
new saveRuleTriggerAction call shape.

Thanks to kabi178 and xet7 !
2026-09-10 20:38:21 +03:00

56 lines
2.8 KiB
JavaScript

// #3092: let a Rule's trigger fire when a card matches the same "Advanced
// Filter" criteria already available in the board's Filter sidebar.
//
// The selector-building algorithm (parenthesised sub-expressions, the
// `field = value` / `!=` / `>` / … comparisons, and `and`/`or`/`not`), AND
// the three resolvers it needs (custom field name -> id, dropdown value ->
// id, date-typed custom field range), are NOT duplicated here: both live
// once, in /imports/lib/advancedFilter.js
// (advancedFilterStringToSelector / buildAdvancedFilterResolversFromCustomFields),
// and both the sidebar (client/lib/filter.js AdvancedFilter class) and this
// module call the same functions. The only thing that differs between the
// two callers is how the custom fields are FETCHED: the sidebar reads them
// (synchronously, on the client) from ReactiveCache; this module pre-fetches
// the board's custom fields once — Meteor 3 collections are async on the
// server — and hands the same plain array to the shared resolver builder, so
// the shared selector-building function itself stays plain, synchronous code
// with no Meteor/ReactiveCache import on either side.
//
// Matching the card against the built selector reuses the real Cards
// collection (`Cards.findOneAsync({ ...selector, _id: card._id })`) rather
// than a hand-rolled in-memory selector matcher — the same Mongo/FerretDB
// query engine the client's minimongo mirrors decides the match, so "does
// this card match the advanced filter" is answered identically here and in
// the sidebar.
import {
advancedFilterStringToSelector,
buildAdvancedFilterResolversFromCustomFields,
} from '/imports/lib/advancedFilter';
import CustomFields from '/models/customFields';
import Cards from '/models/cards';
export { buildAdvancedFilterResolversFromCustomFields as buildServerAdvancedFilterResolvers };
/**
* Evaluate a rule's stored "Advanced Filter" trigger string against a card's
* CURRENT state (the same criteria language, and the same matching
* semantics, as the board Filter sidebar's Advanced Filter). Returns false
* for an empty/unset filter string (nothing to match), and false rather than
* throwing when the filter string fails to parse (mirrors the sidebar, which
* falls back to its last valid selector — here there is none yet, so no
* match).
*/
export async function cardMatchesAdvancedFilter(card, filterString) {
if (!card || !filterString || !filterString.trim()) return false;
let selector;
try {
const customFields = await CustomFields.find({ boardId: card.boardId }).fetchAsync();
const resolvers = buildAdvancedFilterResolversFromCustomFields(customFields);
selector = advancedFilterStringToSelector(filterString, resolvers);
} catch (e) {
return false;
}
const match = await Cards.findOneAsync({ ...selector, _id: card._id });
return !!match;
}