Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(automation): serialize rule updates

Yudhi Armyndharis8ed2f71debdf
diff --git a/src/modules/automation/automation-rules.service.spec.ts b/src/modules/automation/automation-rules.service.spec.ts
index f39ebc173..437bade85 100644
--- a/src/modules/automation/automation-rules.service.spec.ts
+++ b/src/modules/automation/automation-rules.service.spec.ts
@@ -9,6 +9,7 @@ import { AutomationRulesService } from './automation-rules.service';
import { AutomationRule } from './entities/automation-rule.entity';
import { Session, SessionStatus } from '../session/entities/session.entity';
import type { MessageService } from '../message/message.service';
+import type { WebhookFilters } from '../webhook/filters/filter-types';
import type { ModuleRef } from '@nestjs/core';
import type { ConfigService } from '@nestjs/config';
@@ -116,6 +117,63 @@ describe('AutomationRulesService', () => {
expect((await ds.getRepository(AutomationRule).findOneByOrFail({ id: rule.id })).replyText).toBe('hi');
});
+ // Stalls the next findOne after it has read, so a second request can run between the read and
+ // the write of the first, which is the window a read-modify-save update loses writes in.
+ const stallNextRead = (): { release: () => void } => {
+ const repository = ds.getRepository(AutomationRule);
+ const realFindOne = repository.findOne.bind(repository);
+ let release: () => void = () => undefined;
+ const gate = new Promise<void>(resolve => (release = resolve));
+ jest.spyOn(repository, 'findOne').mockImplementationOnce(async options => {
+ const row = await realFindOne(options);
+ await gate;
+ return row;
+ });
+ return { release };
+ };
+
+ it('an overlapping edit does not write back columns it never touched', async () => {
+ const rule = await service.create('sessA', { name: 'r', replyText: 'old' });
+ const { release } = stallNextRead();
+
+ const slow = service.update('sessA', rule.id, { replyText: 'new' });
+ await service.update('sessA', rule.id, { enabled: false });
+ release();
+ await slow;
+
+ const stored = await ds.getRepository(AutomationRule).findOneByOrFail({ id: rule.id });
+ expect(stored.replyText).toBe('new');
+ expect(stored.enabled).toBe(false);
+ });
+
+ it('an edit that loses a race with a delete answers 404 and does not resurrect the rule', async () => {
+ const rule = await service.create('sessA', { name: 'r', replyText: 'old' });
+ const { release } = stallNextRead();
+
+ const slow = service.update('sessA', rule.id, { replyText: 'new' });
+ await service.remove('sessA', rule.id);
+ release();
+
+ await expect(slow).rejects.toThrow(NotFoundException);
+ expect(await ds.getRepository(AutomationRule).countBy({ id: rule.id })).toBe(0);
+ });
+
+ it('update stores conditions and returns the stored row', async () => {
+ const rule = await service.create('sessA', { name: 'r', replyText: 'hi' });
+ const conditions: WebhookFilters = { conditions: [{ field: 'body', operator: 'contains', value: 'refund' }] };
+
+ const updated = await service.update('sessA', rule.id, { conditions, cooldownSeconds: 0 });
+
+ expect(updated.conditions).toEqual(conditions);
+ expect(updated.cooldownSeconds).toBe(0);
+ expect(updated.replyText).toBe('hi');
+ });
+
+ it('update with no fields returns the rule untouched', async () => {
+ const rule = await service.create('sessA', { name: 'r', replyText: 'hi' });
+ expect((await service.update('sessA', rule.id, {})).replyText).toBe('hi');
+ });
+
it('remove refuses (404) a rule owned by another session and does not delete it', async () => {
const rule = await service.create('sessA', { name: 'r', replyText: 'hi' });
await expect(service.remove('sessB', rule.id)).rejects.toThrow(NotFoundException);
diff --git a/src/modules/automation/automation-rules.service.ts b/src/modules/automation/automation-rules.service.ts
index cb38db277..5727c1a2b 100644
--- a/src/modules/automation/automation-rules.service.ts
+++ b/src/modules/automation/automation-rules.service.ts
@@ -122,12 +122,21 @@ export class AutomationRulesService {
async update(sessionId: string, id: string, dto: UpdateAutomationRuleDto): Promise<AutomationRule> {
const rule = await this.findOne(sessionId, id);
- if (dto.name !== undefined) rule.name = dto.name;
- if (dto.replyText !== undefined) rule.replyText = dto.replyText;
- if (dto.conditions !== undefined) rule.conditions = dto.conditions;
- if (dto.cooldownSeconds !== undefined) rule.cooldownSeconds = dto.cooldownSeconds;
- if (dto.enabled !== undefined) rule.enabled = dto.enabled;
- return this.ruleRepository.save(rule);
+ const patch: Partial<Pick<AutomationRule, 'name' | 'replyText' | 'conditions' | 'cooldownSeconds' | 'enabled'>> =
+ {};
+ if (dto.name !== undefined) patch.name = dto.name;
+ if (dto.replyText !== undefined) patch.replyText = dto.replyText;
+ if (dto.conditions !== undefined) patch.conditions = dto.conditions;
+ if (dto.cooldownSeconds !== undefined) patch.cooldownSeconds = dto.cooldownSeconds;
+ if (dto.enabled !== undefined) patch.enabled = dto.enabled;
+ if (Object.keys(patch).length === 0) return rule;
+
+ // A conditional UPDATE of the carried fields, not save() of the entity read above: save() would
+ // write back stale values of every column this request did not touch (switching a rule off could
+ // be undone by an overlapping edit) and re-insert a rule a concurrent DELETE removed.
+ const result = await this.ruleRepository.update({ id, sessionId }, patch);
+ if (!result.affected) throw new NotFoundException(`Automation rule ${id} not found`);
+ return this.findOne(sessionId, id);
}
async remove(sessionId: string, id: string): Promise<void> {