Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(integration): accept a null sessionScope on instance PATCH

Yudhi Armyndharis9b3b96d5b432
diff --git a/src/modules/integration/dto/instance.dto.spec.ts b/src/modules/integration/dto/instance.dto.spec.ts
new file mode 100644
index 000000000..7936497a8
--- /dev/null
+++ b/src/modules/integration/dto/instance.dto.spec.ts
@@ -0,0 +1,32 @@
+import { BadRequestException, ValidationPipe } from '@nestjs/common';
+import { DECORATORS } from '@nestjs/swagger';
+import { UpdateInstanceDto } from './instance.dto';
+import { GLOBAL_VALIDATION_OPTIONS } from '../../../config/app-validation';
+
+describe('UpdateInstanceDto', () => {
+ const pipe = new ValidationPipe(GLOBAL_VALIDATION_OPTIONS);
+ const through = (value: object): Promise<unknown> =>
+ pipe.transform(value, { type: 'body', metatype: UpdateInstanceDto });
+
+ // Omitting sessionScope leaves it unchanged, so null is the only way to return a bound instance to
+ // all sessions. It must reach the service as null, not as a string.
+ it('accepts sessionScope null, which resets the instance to all sessions', async () => {
+ await expect(through({ sessionScope: null })).resolves.toEqual({ sessionScope: null });
+ });
+
+ it('still rejects an empty sessionScope', async () => {
+ await expect(through({ sessionScope: '' })).rejects.toBeInstanceOf(BadRequestException);
+ });
+
+ // Generated clients read the published schema, so it has to say null is allowed and what it means.
+ it('publishes sessionScope as a nullable string that null resets to all sessions', () => {
+ const property = Reflect.getMetadata(
+ DECORATORS.API_MODEL_PROPERTIES,
+ UpdateInstanceDto.prototype,
+ 'sessionScope',
+ ) as { nullable?: boolean; type?: unknown; description?: string } | undefined;
+ expect(property?.nullable).toBe(true);
+ expect(property?.type).toBe(String);
+ expect(property?.description).toMatch(/null/);
+ });
+});
diff --git a/src/modules/integration/dto/instance.dto.ts b/src/modules/integration/dto/instance.dto.ts
index 0e0ba4c17..a6c64aff7 100644
--- a/src/modules/integration/dto/instance.dto.ts
+++ b/src/modules/integration/dto/instance.dto.ts
@@ -69,12 +69,19 @@ export class UpdateInstanceDto {
@IsBoolean()
enabled?: boolean;
- @ApiPropertyOptional({ description: 'Session id the instance is scoped to. Omit for all sessions.' })
+ // @IsOptional skips null as well as undefined, so null reaches the service, which stores it as the
+ // same null an unscoped create does.
+ @ApiPropertyOptional({
+ description: 'Session id to bind the instance to, or null for all sessions. Omit to leave the scope unchanged.',
+ nullable: true,
+ // `string | null` reduces to `Object` under emitDecoratorMetadata; declare the real type.
+ type: String,
+ })
@IsOptional()
@IsString()
- @IsNotEmpty({ message: 'sessionScope must not be empty (omit it for all sessions)' })
+ @IsNotEmpty({ message: 'sessionScope must not be empty (send null for all sessions)' })
@MaxLength(256)
- sessionScope?: string;
+ sessionScope?: string | null;
@ApiPropertyOptional({ description: 'Per-instance config slice passed to the adapter.' })
@IsOptional()
diff --git a/src/modules/integration/integration-instance.controller.spec.ts b/src/modules/integration/integration-instance.controller.spec.ts
index b9d80ae74..619aa6a92 100644
--- a/src/modules/integration/integration-instance.controller.spec.ts
+++ b/src/modules/integration/integration-instance.controller.spec.ts
@@ -197,6 +197,43 @@ describe('IntegrationInstanceController provisioning bridge', () => {
expect(setPluginSessionConfig).toHaveBeenCalledWith('chatwoot-adapter', 'sess-2', { baseUrl: 'https://y' }); // new bound
});
+ it('returns a bound instance to all sessions on PATCH sessionScope:null', async () => {
+ const { loader, audit, setPluginSessionConfig, setPluginSessions, updatePluginConfig } = build();
+ (loader.getPlugin as jest.Mock).mockReturnValue({
+ manifest: { id: 'chatwoot-adapter', ingress: [{ route: 'chatwoot' }], permissions: ['webhook:ingress'] },
+ activeSessions: ['sess-1'],
+ });
+ const base = { pluginId: 'chatwoot-adapter', instanceId: 'acct1', config: { baseUrl: 'https://x' }, enabled: true };
+ const update = jest.fn().mockResolvedValue({ ...base, sessionScope: null });
+ const instances = {
+ resolve: jest.fn().mockResolvedValue({ ...base, sessionScope: 'sess-1' }),
+ setEnabled: jest.fn(),
+ update,
+ // The row is already unscoped when the old scope's teardown lists instances.
+ list: jest.fn().mockResolvedValue([{ ...base, sessionScope: null }]),
+ maskedView: (i: unknown) => i,
+ } as unknown as PluginInstanceService;
+ const controller = new IntegrationInstanceController(
+ instances,
+ loader,
+ audit,
+ new ScopeBindingService(instances, loader, audit, sessions),
+ );
+
+ const view = await controller.patch('chatwoot-adapter', 'acct1', { sessionScope: null });
+
+ expect(update).toHaveBeenCalledWith(
+ 'chatwoot-adapter',
+ 'acct1',
+ { sessionScope: null, config: undefined },
+ undefined,
+ );
+ expect(view.sessionScope).toBeNull();
+ expect(setPluginSessionConfig).toHaveBeenCalledWith('chatwoot-adapter', 'sess-1', {}); // old scope torn down
+ expect(updatePluginConfig).toHaveBeenCalledWith('chatwoot-adapter', { baseUrl: 'https://x' });
+ expect(setPluginSessions).toHaveBeenLastCalledWith('chatwoot-adapter', ['*']);
+ });
+
it('keeps the session but CLEARS its config when a disabled instance shares its scope with a sibling', async () => {
const { loader, audit, setPluginSessionConfig, setPluginSessions } = build();
(loader.getPlugin as jest.Mock).mockReturnValue({
@@ -541,9 +578,9 @@ describe('IntegrationInstanceController session-scope fence', () => {
ForbiddenException,
);
// An explicit null (all sessions) is likewise outside a scoped key's fence.
- await expect(
- controller.patch('chatwoot-adapter', 'acct1', { sessionScope: null as unknown as string }, scopedKey),
- ).rejects.toThrow(ForbiddenException);
+ await expect(controller.patch('chatwoot-adapter', 'acct1', { sessionScope: null }, scopedKey)).rejects.toThrow(
+ ForbiddenException,
+ );
expect(update).not.toHaveBeenCalled();
});
diff --git a/src/modules/integration/plugin-instance.service.spec.ts b/src/modules/integration/plugin-instance.service.spec.ts
index bddcd4ad2..58aefb499 100644
--- a/src/modules/integration/plugin-instance.service.spec.ts
+++ b/src/modules/integration/plugin-instance.service.spec.ts
@@ -171,4 +171,18 @@ describe('PluginInstanceService provisioning', () => {
const patched = await service.update('chatwoot', 'acct1', { sessionScope: '' });
expect(patched?.sessionScope).toBeNull();
});
+
+ // PATCH sessionScope:null is how an operator returns a bound instance to all sessions. It must land
+ // as the same null an unscoped create stores, since ingress and the sandbox bridge pass a stored
+ // '*' on as a literal session id.
+ it('update clears a bound sessionScope to null, the value an unscoped create stores', async () => {
+ const unscoped = await service.create('chatwoot', 'acct0', {});
+ await service.create('chatwoot', 'acct1', { sessionScope: 'sess-1' });
+
+ const cleared = await service.update('chatwoot', 'acct1', { sessionScope: null });
+
+ expect(cleared?.sessionScope).toBeNull();
+ expect(unscoped.sessionScope).toBeNull();
+ expect((await service.resolve('chatwoot', 'acct1'))?.sessionScope).toBeNull();
+ });
});
diff --git a/src/modules/integration/plugin-instance.service.ts b/src/modules/integration/plugin-instance.service.ts
index dcbdbcde5..aa031f4c7 100644
--- a/src/modules/integration/plugin-instance.service.ts
+++ b/src/modules/integration/plugin-instance.service.ts
@@ -113,7 +113,7 @@ export class PluginInstanceService implements PluginInstancePort {
async update(
pluginId: string,
instanceId: string,
- patch: { sessionScope?: string; config?: Record<string, unknown> },
+ patch: { sessionScope?: string | null; config?: Record<string, unknown> },
schema?: PluginConfigSchema,
): Promise<PluginInstance | null> {
const inst = await this.resolve(pluginId, instanceId);