Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(webhook): keep the recorded reason when a replay budget runs out
Yudhi Armyndharis54d7a2716cd4
diff --git a/src/modules/webhook/webhook-delivery.service.ts b/src/modules/webhook/webhook-delivery.service.tsindex 74143c7b0..aef78d23e 100644--- a/src/modules/webhook/webhook-delivery.service.ts+++ b/src/modules/webhook/webhook-delivery.service.ts@@ -1002,6 +1002,12 @@ export class WebhookDeliveryService implements OnModuleInit, OnModuleDestroy {where: { webhookId: row.webhookId, idempotencyKey: row.idempotencyKey, attempts: MoreThan(0) },});if (existing > 0) return true;+ // A delivery that was shed or refused before sending was recorded with its reason as an attempts-0 row,+ // which the terminal row below replaces; keep that reason on it.+ const unsent = await this.failureRepository.findOne({+ where: { webhookId: row.webhookId, idempotencyKey: row.idempotencyKey, attempts: 0 },+ select: { id: true, lastError: true },+ });const recorded = await recordWebhookDeliveryFailure(this.failureRepository, this.logger, {webhookId: row.webhookId,sessionId: row.sessionId,@@ -1009,7 +1015,9 @@ export class WebhookDeliveryService implements OnModuleInit, OnModuleDestroy {url,idempotencyKey: row.idempotencyKey,attempts: Math.max(1, row.attempts),- lastError: 'Webhook replay budget exhausted',+ lastError: unsent?.lastError+ ? `Webhook replay budget exhausted; last error: ${unsent.lastError}`+ : 'Webhook replay budget exhausted',...(this.keepsFailurePayload() ? { payload: row.payload } : {}),});if (recorded === true) incrementWebhookDeliveryFailures();diff --git a/src/modules/webhook/webhook-recovery-boundaries.spec.ts b/src/modules/webhook/webhook-recovery-boundaries.spec.tsindex 64180901f..211e7c8bd 100644--- a/src/modules/webhook/webhook-recovery-boundaries.spec.ts+++ b/src/modules/webhook/webhook-recovery-boundaries.spec.ts@@ -372,6 +372,54 @@ describe('webhook recovery across live ownership and configuration changes', ()expect(await failures.count()).toBe(0);});+ describe('a spent replay budget', () => {+ const spent = (idempotencyKey: string) => ({+ id: 'outbox-row',+ webhookId: primary.id,+ sessionId: primary.sessionId,+ event: 'message.received',+ idempotencyKey,+ payload: { id: 1 },+ attempts: 5,+ });+ const build = () =>+ new WebhookDeliveryService(+ webhooks,+ failures,+ new ConfigService({ webhook: {} }),+ new HookManager(),+ new WebhookOutboxService(ds.getRepository(WebhookOutboxEvent)),+ );++ it('keeps the reason a shed delivery was recorded with, which the generic row would replace', async () => {+ await failures.save(+ failures.create({+ webhookId: primary.id,+ sessionId: primary.sessionId,+ event: 'message.received',+ url: primary.url,+ idempotencyKey: 'key-shed',+ attempts: 0,+ lastError: 'Webhook payload is 210 bytes after webhook:before hooks, exceeds 50',+ }),+ );++ expect(await build().recordReplayExhaustion(spent('key-shed'), primary.url)).toBe(true);++ const rows = await failures.find();+ expect(rows).toHaveLength(1);+ expect(rows[0].attempts).toBe(5);+ expect(rows[0].lastError).toBe(+ 'Webhook replay budget exhausted; last error: Webhook payload is 210 bytes after webhook:before hooks, exceeds 50',+ );+ });++ it('records the plain exhaustion reason when nothing was recorded before', async () => {+ expect(await build().recordReplayExhaustion(spent('key-plain'), primary.url)).toBe(true);+ expect((await failures.find()).map(row => row.lastError)).toEqual(['Webhook replay budget exhausted']);+ });+ });+it('does not replay a row whose pre-checks were under way when destroy began', async () => {const repository = ds.getRepository(WebhookOutboxEvent);const outbox = new WebhookOutboxService(repository);