From 0659ba957cf741feabef32445a3a2f96c7e59006 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Ovejero?= Date: Wed, 7 May 2025 13:21:33 +0200 Subject: [PATCH] fix(core): Do not cache dynamic webhooks (#15176) --- .../__tests__/webhook.service.test.ts | 26 +++++++++++++++++++ packages/cli/src/webhooks/webhook.service.ts | 15 +++++------ 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/packages/cli/src/webhooks/__tests__/webhook.service.test.ts b/packages/cli/src/webhooks/__tests__/webhook.service.test.ts index 8737b57df05..ff623cc6df9 100644 --- a/packages/cli/src/webhooks/__tests__/webhook.service.test.ts +++ b/packages/cli/src/webhooks/__tests__/webhook.service.test.ts @@ -360,4 +360,30 @@ describe('WebhookService', () => { expect(nodeType.webhook).toHaveBeenCalled(); }); }); + + describe('findCached()', () => { + test('should not cache dynamic webhooks', async () => { + const method = 'GET'; + const webhookId = uuid(); + const fullPath = `${webhookId}/user/123/posts`; + const dynamicWebhook = createWebhook(method, 'user/:id/posts', webhookId, 3); + + webhookRepository.findOneBy.mockResolvedValueOnce(null); // static lookup + webhookRepository.findBy.mockResolvedValueOnce([dynamicWebhook]); // dynamic lookup + + const result1 = await webhookService.findWebhook(method, fullPath); + expect(result1).toBe(dynamicWebhook); + + expect(cacheService.set).not.toHaveBeenCalled(); + + webhookRepository.findOneBy.mockResolvedValueOnce(null); + webhookRepository.findBy.mockResolvedValueOnce([dynamicWebhook]); + + const result2 = await webhookService.findWebhook(method, fullPath); + expect(result2).toBe(dynamicWebhook); + + expect(webhookRepository.findOneBy).toHaveBeenCalledTimes(2); + expect(webhookRepository.findBy).toHaveBeenCalledTimes(2); + }); + }); }); diff --git a/packages/cli/src/webhooks/webhook.service.ts b/packages/cli/src/webhooks/webhook.service.ts index cfd8fb036fe..026eece5c89 100644 --- a/packages/cli/src/webhooks/webhook.service.ts +++ b/packages/cli/src/webhooks/webhook.service.ts @@ -41,19 +41,18 @@ export class WebhookService { private async findCached(method: Method, path: string) { const cacheKey = `webhook:${method}-${path}`; - const cachedWebhook = await this.cacheService.get(cacheKey); + const cachedStaticWebhook = await this.cacheService.get(cacheKey); - if (cachedWebhook) return this.webhookRepository.create(cachedWebhook); + if (cachedStaticWebhook) return this.webhookRepository.create(cachedStaticWebhook); - let dbWebhook = await this.findStaticWebhook(method, path); + const dbStaticWebhook = await this.findStaticWebhook(method, path); - if (dbWebhook === null) { - dbWebhook = await this.findDynamicWebhook(method, path); + if (dbStaticWebhook) { + void this.cacheService.set(cacheKey, dbStaticWebhook); + return dbStaticWebhook; } - void this.cacheService.set(cacheKey, dbWebhook); - - return dbWebhook; + return await this.findDynamicWebhook(method, path); } /**