Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(integration): answer 404 when an instance is deleted mid-patch

Yudhi Armyndhariseb627c01a8bf
diff --git a/src/modules/integration/integration-instance.controller.spec.ts b/src/modules/integration/integration-instance.controller.spec.ts
index 33fe35740..0d7956359 100644
--- a/src/modules/integration/integration-instance.controller.spec.ts
+++ b/src/modules/integration/integration-instance.controller.spec.ts
@@ -799,6 +799,26 @@ describe('IntegrationInstanceController update audit', () => {
expect(audit.logInfo).not.toHaveBeenCalledWith(AuditAction.INTEGRATION_INSTANCE_UPDATED, expect.anything());
});
+ it('answers 404, not 500, when the instance is deleted while the patch is in flight', async () => {
+ const { controller, audit, update, setPluginSessions } = build();
+ update.mockResolvedValue(null);
+
+ await expect(controller.patch('chatwoot-adapter', 'acct1', { enabled: false })).rejects.toThrow(NotFoundException);
+ expect(setPluginSessions).not.toHaveBeenCalled();
+ expect(audit.logInfo).not.toHaveBeenCalledWith(AuditAction.INTEGRATION_INSTANCE_UPDATED, expect.anything());
+ });
+
+ it('answers 404, not 500, when the instance is deleted while regenerate-secret is in flight', async () => {
+ const { controller, audit, instances } = build();
+ Object.assign(instances, { regenerateSecret: jest.fn().mockResolvedValue(null) });
+
+ await expect(controller.regenerate('chatwoot-adapter', 'acct1')).rejects.toThrow(NotFoundException);
+ expect(audit.logInfo).not.toHaveBeenCalledWith(
+ AuditAction.INTEGRATION_INSTANCE_SECRET_REGENERATED,
+ expect.anything(),
+ );
+ });
+
// `enabled` rides in the same update() as scope and config, so the row is written by ONE save. A
// second write could fail after the first landed, leaving the row half-patched with the scope
// binding and the audit entry skipped.
diff --git a/src/modules/integration/integration-instance.controller.ts b/src/modules/integration/integration-instance.controller.ts
index 3c2ea2221..f78f953db 100644
--- a/src/modules/integration/integration-instance.controller.ts
+++ b/src/modules/integration/integration-instance.controller.ts
@@ -114,6 +114,8 @@ export class IntegrationInstanceController {
): Promise<InstanceView> {
await this.resolveVisible(pluginId, instanceId, apiKey);
const inst = await this.instances.regenerateSecret(pluginId, instanceId);
+ // regenerateSecret() answers null when the row was deleted after resolveVisible read it.
+ if (!inst) throw new NotFoundException('instance not found');
void this.audit.logInfo(AuditAction.INTEGRATION_INSTANCE_SECRET_REGENERATED, {
metadata: { pluginId, instanceId },
});
@@ -143,8 +145,10 @@ export class IntegrationInstanceController {
{ enabled: dto.enabled, sessionScope: dto.sessionScope, config: dto.config },
this.schemaFor(pluginId),
);
+ // update() answers null when the row was deleted after resolveVisible read it.
+ if (!inst) throw new NotFoundException('instance not found');
}
- const updated = inst as PluginInstance;
+ const updated = inst;
// If the bound session changed, tear down the OLD scope (incl. a wildcard/null scope) so it stops
// firing with stale config. The new scope is (re)bound right after; teardown runs first with the new
// scope already persisted, so the wildcard retirement check sees the current state correctly.