Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(webhook): finish a missed attempts-0 clear on a repeat failure
Yudhi Armyndharis3c63a27703fe
diff --git a/src/modules/webhook/utils/record-delivery-failure.spec.ts b/src/modules/webhook/utils/record-delivery-failure.spec.tsindex b4f901f73..add500a87 100644--- a/src/modules/webhook/utils/record-delivery-failure.spec.ts+++ b/src/modules/webhook/utils/record-delivery-failure.spec.ts@@ -87,6 +87,7 @@ describe('recordWebhookDeliveryFailure', () => {const repo = {insert: jest.fn(),update,+ delete: jest.fn().mockResolvedValue({}),count: jest.fn().mockResolvedValue(1),} as unknown as Repository<WebhookDeliveryFailure>;@@ -95,6 +96,43 @@ describe('recordWebhookDeliveryFailure', () => {expect(update).not.toHaveBeenCalled();});+ it('removes an attempts-0 row an earlier terminal record failed to clear', async () => {+ // The first terminal record inserted its row, then its attempts-0 delete failed (or the process+ // stopped in between). The next replay finds the terminal row, and must finish that clear, or+ // the lost event stays listed twice.+ const insert = jest.fn();+ const del = jest.fn().mockResolvedValue({});+ const repo = {+ insert,+ update: jest.fn(),+ delete: del,+ count: jest.fn().mockResolvedValue(1),+ } as unknown as Repository<WebhookDeliveryFailure>;+ const logger = { error: jest.fn() };++ await expect(recordWebhookDeliveryFailure(repo, logger, input)).resolves.toBe(false);++ expect(insert).not.toHaveBeenCalled();+ expect(del).toHaveBeenCalledWith({ webhookId: 'wh-1', idempotencyKey: 'k', attempts: 0 });+ expect(logger.error).not.toHaveBeenCalled();+ });++ it('removes nothing when an unsent delivery is recorded again', async () => {+ const del = jest.fn();+ const repo = {+ insert: jest.fn(),+ update: jest.fn().mockResolvedValue({}),+ delete: del,+ count: jest.fn().mockResolvedValue(1),+ } as unknown as Repository<WebhookDeliveryFailure>;++ await expect(recordWebhookDeliveryFailure(repo, { error: jest.fn() }, { ...input, attempts: 0 })).resolves.toBe(+ false,+ );++ expect(del).not.toHaveBeenCalled();+ });+it('still reports an unsent duplicate as not recorded when refreshing its reason fails', async () => {// A true here would bump the failure metric for a loss that is already counted.const repo = {diff --git a/src/modules/webhook/utils/record-delivery-failure.ts b/src/modules/webhook/utils/record-delivery-failure.tsindex b7556306a..447df2498 100644--- a/src/modules/webhook/utils/record-delivery-failure.ts+++ b/src/modules/webhook/utils/record-delivery-failure.ts@@ -103,13 +103,18 @@ export async function recordWebhookDeliveryFailure(},),);+ } else {+ // A crash or a failed delete right after an earlier terminal insert can leave the+ // attempts-0 row behind; nothing else reconciles it, so every repeat finishes the job.+ await clearDeliveryFailureRows(repo, logger, input.webhookId, input.idempotencyKey, true);}return false;}}await repo.insert({ ...input, lastStatusCode: input.lastStatusCode ?? null });if (terminal) {- // Only after the insert, so a crash in between leaves both rows, never none.+ // Only after the insert, so a crash in between leaves both rows, never none. A later terminal+ // record of the same delivery repeats this clear.await clearDeliveryFailureRows(repo, logger, input.webhookId, input.idempotencyKey, true);}return true;