Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(webhook): keep two poll votes within one second apart

Yudhi Armyndharisca2e977a953b
diff --git a/src/modules/webhook/utils/idempotency.util.spec.ts b/src/modules/webhook/utils/idempotency.util.spec.ts
index 4615c4bc1..fb5840e3c 100644
--- a/src/modules/webhook/utils/idempotency.util.spec.ts
+++ b/src/modules/webhook/utils/idempotency.util.spec.ts
@@ -319,8 +319,12 @@ describe('Idempotency Utils', () => {
const at = '2026-07-20T00:00:00.000Z';
const data = { sessionId: 'A', pollMessageId: 'POLL1', voterId: 'V1', selectedOptions: ['Yes'], timestamp: 100 };
const key = generateIdempotencyKey('poll.vote', data, at);
- expect(key).toBe('vote_A_POLL1_V1_100');
+ expect(key).toMatch(/^vote_A_POLL1_V1_100_[0-9a-f]{12}$/);
expect(generateIdempotencyKey('poll.vote', data, '2026-07-20T01:00:00.000Z')).toBe(key);
+ // An identical redelivery dedupes, a different selection in the same second does not.
+ expect(generateIdempotencyKey('poll.vote', { ...data }, at)).toBe(key);
+ expect(generateIdempotencyKey('poll.vote', { ...data, selectedOptions: ['Yes', 'No'] }, at)).not.toBe(key);
+ expect(generateIdempotencyKey('poll.vote', { ...data, selectedOptions: [] }, at)).not.toBe(key);
// Going back to an earlier selection later is a new change, never deduped against the first.
expect(generateIdempotencyKey('poll.vote', { ...data, timestamp: 200 }, at)).not.toBe(key);
// Two voters on the same poll in the same second are two events.
diff --git a/src/modules/webhook/utils/idempotency.util.ts b/src/modules/webhook/utils/idempotency.util.ts
index 88eecad0a..316f232f6 100644
--- a/src/modules/webhook/utils/idempotency.util.ts
+++ b/src/modules/webhook/utils/idempotency.util.ts
@@ -121,8 +121,9 @@ export function generateIdempotencyKey(event: string, data: Record<string, unkno
case 'poll.vote':
// A vote carries no id of its own, but each change of a voter's selection is stamped by the
// voter's clock, so (session, poll, voter, timestamp) is distinct per change and stable across
- // retries. A voter who goes back to an earlier selection is a new change with a new timestamp.
- return `vote_${toStr(data.sessionId)}_${toStr(data.pollMessageId)}_${toStr(data.voterId)}_${toStr(data.timestamp)}`;
+ // retries. The stamp is whole seconds, so the selection is hashed in as well: two taps on a
+ // multiple-choice poll within one second are two changes and must not share a key.
+ return `vote_${toStr(data.sessionId)}_${toStr(data.pollMessageId)}_${toStr(data.voterId)}_${toStr(data.timestamp)}_${hashData({ selectedOptions: data.selectedOptions ?? null })}`;
case 'presence.update':
// Keyed on the chat and salted per occurrence. Only genuine state CHANGES are dispatched, and