Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(webhook): count a redrive replay that was refused before sending
Yudhi Armyndharis9c30bfb43a09
diff --git a/src/modules/webhook/webhook-redrive.service.spec.ts b/src/modules/webhook/webhook-redrive.service.spec.tsindex 3bf6a309d..ea35ef3c9 100644--- a/src/modules/webhook/webhook-redrive.service.spec.ts+++ b/src/modules/webhook/webhook-redrive.service.spec.ts@@ -69,6 +69,9 @@ describe('WebhookRedriveService', () => {ReturnType<WebhookDeliveryService['redeliver']>,Parameters<WebhookDeliveryService['redeliver']>>(async (webhook, sessionId, event, key, data) => {+ // A preflight refusal (a body over the payload cap after the hooks): reported as failed, with+ // nothing recorded, so the row keeps the attempt count it had.+ if (data.refuse === true) return 'failed';if (data.fail === true) {const recorded = await recordWebhookDeliveryFailure(failures,@@ -129,6 +132,24 @@ describe('WebhookRedriveService', () => {expect(await service.redrive({})).toMatchObject({ delivered: 0, remaining: 0 });});+ it('counts a replay the delivery path refused before sending, so that row does not head every batch', async () => {+ const refused = await addFailure({ attempts: 1, payload: { id: 'big', refuse: true } });+ const other = await addFailure({ attempts: 1, createdAt: new Date(Date.now() - 30_000) });++ expect(await service.redrive({ limit: 1 })).toMatchObject({ failed: 1, remaining: 2 });+ // The recorder raised nothing for a refusal, so the redrive counts the attempt itself, once.+ expect((await failures.findOneByOrFail({ id: refused.id })).attempts).toBe(2);++ await service.redrive({ limit: 1 });+ expect(delivery.redeliver.mock.calls.map(call => call[3])).toEqual([refused.idempotencyKey, other.idempotencyKey]);+ });++ it('raises the attempts of a replay the recorder already counted by one, not two', async () => {+ const row = await addFailure({ attempts: 3, payload: { id: 'm', fail: true } });+ await service.redrive({});+ expect((await failures.findOneByOrFail({ id: row.id })).attempts).toBe(4);+ });+it('reads each retained payload only as its replay starts, never the whole batch at once', async () => {for (let i = 0; i < 6; i++) await addFailure({ payload: { id: `m${i}`, body: 'x'.repeat(200) } });const statements: string[] = [];diff --git a/src/modules/webhook/webhook-redrive.service.ts b/src/modules/webhook/webhook-redrive.service.tsindex e3fd67ade..df92d6af0 100644--- a/src/modules/webhook/webhook-redrive.service.ts+++ b/src/modules/webhook/webhook-redrive.service.ts@@ -218,8 +218,10 @@ export class WebhookRedriveService implements OnModuleDestroy {else if (outcome === 'cancelled') result.skipped++;else {result.failed++;- // Terminal failures update their own attempt count in the shared recorder.- if (outcome !== 'failed') await this.bumpAttempts(row);+ // A failure the delivery path records raises this row's attempts itself. One it refuses before+ // sending (a body over the payload cap after the hooks) leaves the row as it was, so the attempt is+ // counted here; otherwise that row would head every batch.+ if (outcome !== 'failed' || (await this.attemptsUnchanged(row))) await this.bumpAttempts(row);}} catch (error) {result.failed++;@@ -243,6 +245,15 @@ export class WebhookRedriveService implements OnModuleDestroy {return row?.payload ?? null;}+ /** Whether the row still carries the attempt count the batch read, that is, nothing recorded the failure. */+ private async attemptsUnchanged(row: WebhookDeliveryFailure): Promise<boolean> {+ const current = await this.failureRepository.findOne({+ where: { id: row.id },+ select: { id: true, attempts: true },+ });+ return current !== null && current.attempts === row.attempts;+ }+/** Count the failed replay on the row. Best-effort: the row is already on record either way. */private async bumpAttempts(row: WebhookDeliveryFailure): Promise<void> {try {