Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(catalog): report product send outcomes to the send breaker
Yudhi Armyndhariseb6de8153e75
diff --git a/src/modules/catalog/catalog.service.spec.ts b/src/modules/catalog/catalog.service.spec.tsindex ef045cc6b..ef3b006c5 100644--- a/src/modules/catalog/catalog.service.spec.ts+++ b/src/modules/catalog/catalog.service.spec.ts@@ -2,6 +2,7 @@ import { NotFoundException, NotImplementedException } from '@nestjs/common';import { CatalogService } from './catalog.service';import { EngineRegistry } from '../../engine/engine-registry.service';import { EngineNotSupportedError } from '../../common/errors/engine-not-supported.error';+import { EngineRefusedError } from '../../common/errors/engine-refused.error';import type { SendPacingService } from '../message/send-pacing.service';import type {IWhatsAppEngine,@@ -28,7 +29,12 @@ describe('CatalogService', () => {const makeService = (engine: Partial<IWhatsAppEngine> | undefined, pacing?: { assertSendAllowed: jest.Mock }) => {const engines = new EngineRegistry();if (engine) engines.set('s1', engine as IWhatsAppEngine);- const sendPacing = pacing ?? { assertSendAllowed: jest.fn().mockResolvedValue(undefined) };+ const sendPacing = {+ assertSendAllowed: jest.fn().mockResolvedValue(undefined),+ recordSendSuccess: jest.fn(),+ recordSendFailure: jest.fn(),+ ...pacing,+ };return { svc: new CatalogService(engines, sendPacing as unknown as SendPacingService), pacing: sendPacing };};@@ -135,5 +141,26 @@ describe('CatalogService', () => {const sendProduct = jest.fn().mockRejectedValue(down);await expect(makeService({ sendProduct }).svc.sendProduct('s1', '[email protected]', 'prod-1')).rejects.toBe(down);});++ it('reports the engine outcome of a product send to the pacing breaker', async () => {+ const sendProduct = jest.fn().mockResolvedValue(sent);+ const { svc, pacing } = makeService({ sendProduct });+ await svc.sendProduct('s1', '[email protected]', 'prod-1');+ expect(pacing.recordSendSuccess).toHaveBeenCalledWith('s1');++ const refused = new EngineRefusedError('refused');+ sendProduct.mockRejectedValue(refused);+ await expect(svc.sendProduct('s1', '[email protected]', 'prod-1')).rejects.toBe(refused);+ expect(pacing.recordSendFailure).toHaveBeenCalledWith('s1');+ });++ it('does not feed a client-fault or unsupported refusal to the breaker', async () => {+ const sendProduct = jest.fn().mockRejectedValue(new NotFoundException('Product not found'));+ const sendCatalog = jest.fn().mockRejectedValue(new EngineNotSupportedError('sendCatalog'));+ const { svc, pacing } = makeService({ sendProduct, sendCatalog });+ await expect(svc.sendProduct('s1', '[email protected]', 'prod-404')).rejects.toBeInstanceOf(NotFoundException);+ await expect(svc.sendCatalog('s1', '[email protected]')).rejects.toBeInstanceOf(EngineNotSupportedError);+ expect(pacing.recordSendFailure).not.toHaveBeenCalled();+ });});});diff --git a/src/modules/catalog/catalog.service.ts b/src/modules/catalog/catalog.service.tsindex 4dd2864c4..2dac77296 100644--- a/src/modules/catalog/catalog.service.ts+++ b/src/modules/catalog/catalog.service.ts@@ -6,7 +6,7 @@ import type {PaginatedProducts,MessageResult,} from '../../engine/interfaces/whatsapp-engine.interface';-import { SendPacingService } from '../message/send-pacing.service';+import { SendPacingService, countsTowardSendBreaker } from '../message/send-pacing.service';@Injectable()export class CatalogService {@@ -51,7 +51,7 @@ export class CatalogService {sessionId,() => new NotFoundException(`Session ${sessionId} not found or not connected`),);- return engine.sendProduct(chatId, productId, body);+ return this.recordedSend(sessionId, () => engine.sendProduct(chatId, productId, body));}/**@@ -65,6 +65,23 @@ export class CatalogService {sessionId,() => new NotFoundException(`Session ${sessionId} not found or not connected`),);- return engine.sendCatalog(chatId, body);+ return this.recordedSend(sessionId, () => engine.sendCatalog(chatId, body));+ }++ /**+ * Report the engine send's outcome to the pacing breaker, as every other send path does. The pacing+ * check and the session lookup stay outside, so a policy 429 or a 404 never feeds the breaker.+ */+ private async recordedSend(sessionId: string, send: () => Promise<MessageResult>): Promise<MessageResult> {+ try {+ const result = await send();+ this.pacing.recordSendSuccess(sessionId);+ return result;+ } catch (error) {+ if (countsTowardSendBreaker(error)) {+ this.pacing.recordSendFailure(sessionId);+ }+ throw error;+ }}}