Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(automation): skip a rule with malformed conditions on its own

Yudhi Armyndharis873951909b81
diff --git a/src/modules/automation/automation-rules.service.spec.ts b/src/modules/automation/automation-rules.service.spec.ts
index db93b706c..0372eba36 100644
--- a/src/modules/automation/automation-rules.service.spec.ts
+++ b/src/modules/automation/automation-rules.service.spec.ts
@@ -236,6 +236,27 @@ describe('AutomationRulesService', () => {
expect(sends.map(s => s.text)).toEqual(['first-reply']);
});
+ it.each([
+ ['a null condition', { conditions: [null] }, inbound()],
+ ['a non-list conditions value on a broadcast message', { conditions: 'x' }, inbound({ kind: 'broadcast' })],
+ ])('a rule with %s is skipped on its own; later rules still answer', async (_label, conditions, message) => {
+ const broken = await service.create('sessA', { name: 'broken', replyText: 'broken-reply', cooldownSeconds: 0 });
+ // Stored the way a restore writes it: the DTO would refuse this value.
+ await ds
+ .getRepository(AutomationRule)
+ .update(broken.id, { conditions: conditions as never, createdAt: new Date('2026-01-01T00:00:00Z') });
+ await service.create('sessA', {
+ name: 'valid',
+ replyText: 'valid-reply',
+ cooldownSeconds: 0,
+ conditions: { conditions: [{ field: 'kind', operator: 'is', value: ['individual', 'broadcast'] }] },
+ });
+
+ await expect(service.evaluateInbound('sessA', message)).resolves.toBeUndefined();
+
+ expect(sends.map(s => s.text)).toEqual(['valid-reply']);
+ });
+
it('cooldown: the same rule stays quiet in the same chat, other chats unaffected', async () => {
await service.create('sessA', { name: 'all', replyText: 'ack', cooldownSeconds: 300 });
diff --git a/src/modules/automation/automation-rules.service.ts b/src/modules/automation/automation-rules.service.ts
index aa8759913..8fc398a16 100644
--- a/src/modules/automation/automation-rules.service.ts
+++ b/src/modules/automation/automation-rules.service.ts
@@ -171,12 +171,24 @@ export class AutomationRulesService {
// First match wins: one inbound message never produces more than one automated reply, and rule
// order (creation order) is the tiebreak the operator can reason about. A rule without a `kind`
- // condition skips the opt-in chat kinds; naming the kind is how a rule reaches them.
- const rule = rules.find(
- candidate =>
- (!optInOnly || candidate.conditions?.conditions?.some(c => c.field === 'kind')) &&
- evaluateFilters(candidate.conditions, 'message.received', message, resolveLid),
- );
+ // condition skips the opt-in chat kinds; naming the kind is how a rule reaches them. A rule whose
+ // stored conditions are malformed (a restore bypasses the DTO) is skipped on its own, so it cannot
+ // silence every other rule of the session.
+ const rule = rules.find(candidate => {
+ try {
+ return (
+ (!optInOnly || candidate.conditions?.conditions?.some(c => c.field === 'kind')) &&
+ evaluateFilters(candidate.conditions, 'message.received', message, resolveLid)
+ );
+ } catch (error) {
+ this.logger.warn('Skipping automation rule with malformed conditions', {
+ sessionId,
+ ruleId: candidate.id,
+ error: error instanceof Error ? error.message : String(error),
+ });
+ return false;
+ }
+ });
if (!rule) return;
if (this.inCooldown(rule, chatId)) return;
// Enter the cooldown BEFORE the send: a burst of matching messages must collapse to one reply