Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(integration): keep concrete scopes active when a wildcard is retired

Yudhi Armyndharisd9d6f754a130
diff --git a/src/modules/integration/scope-binding.service.spec.ts b/src/modules/integration/scope-binding.service.spec.ts
index 558726af2..2f16bb86f 100644
--- a/src/modules/integration/scope-binding.service.spec.ts
+++ b/src/modules/integration/scope-binding.service.spec.ts
@@ -362,6 +362,19 @@ describe('ScopeBindingService.applyScopeBinding retires an instance without leak
expect(setPluginSessions).toHaveBeenCalledWith('chatwoot', []);
});
+ // Activating a wildcard overwrote activeSessions with ['*'], so the concrete scopes still-enabled
+ // siblings bind are gone from it; retiring the wildcard must put them back, not leave the plugin on [].
+ it('restores the scopes of enabled concrete siblings when the last wildcard is retired', async () => {
+ const { svc, setPluginSessions } = build(['*']);
+ await svc([row('a', true, 'sess-1'), row('b', false, null), row('c', false, 'sess-3')]).applyScopeBinding(
+ 'chatwoot',
+ null,
+ {},
+ false,
+ );
+ expect(setPluginSessions).toHaveBeenCalledWith('chatwoot', ['sess-1']);
+ });
+
// Activation is untouched: it must still write the instance's config, not an empty slice.
it('leaves the activation path writing the instance config', async () => {
const { svc, setPluginSessionConfig } = build([]);
diff --git a/src/modules/integration/scope-binding.service.ts b/src/modules/integration/scope-binding.service.ts
index d7f14a183..84ea9b8e7 100644
--- a/src/modules/integration/scope-binding.service.ts
+++ b/src/modules/integration/scope-binding.service.ts
@@ -196,15 +196,14 @@ export class ScopeBindingService implements OnApplicationBootstrap {
this.loader.setPluginSessions(pluginId, ['*']);
return;
}
- const anyWildcardLeft = (await this.instances.list(pluginId)).some(
- i => i.enabled && (!i.sessionScope || i.sessionScope === '*'),
- );
+ const rows = await this.instances.list(pluginId);
+ const anyWildcardLeft = rows.some(i => i.enabled && (!i.sessionScope || i.sessionScope === '*'));
if (!anyWildcardLeft) {
+ // Activating a wildcard replaced activeSessions with ['*'], dropping the scopes enabled
+ // concrete siblings bind, so put those back rather than leave them silenced until a restart.
const current = this.loader.getPlugin(pluginId)?.activeSessions ?? [];
- this.loader.setPluginSessions(
- pluginId,
- current.filter(s => s !== '*'),
- );
+ const concrete = rows.filter(i => i.enabled && i.sessionScope).map(i => i.sessionScope as string);
+ this.loader.setPluginSessions(pluginId, [...new Set([...current.filter(s => s !== '*'), ...concrete])]);
}
return;
}