feat(connections): add an onDisconnect lifecycle hook to connection providers (#23538)

Platform half of the follow-up to
https://github.com/twentyhq/twenty/pull/22984#discussion_r3673946334.
The Slack app claims a `team_id` on connect and had no way to release
it, because connection providers only had an on-connect hook. Nothing
here is Slack-specific, so it targets `main`. The app side is #23540, on
top of `feat/slack-bot`, and waits on this plus an SDK release.

## What changes

`defineConnectionProvider` accepts `onDisconnectLogicFunction` alongside
`onConnectLogicFunction`. It is stored on
`connectionProvider.onDisconnectLogicFunctionUniversalIdentifier` (fast
instance command `2.26.0_...1785350000000`) and enqueued right after the
`ConnectedAccount` row is deleted, in the disconnecting workspace, with
the same payload as on-connect:

```ts
type OnDisconnectPayload = {
  connectionProviderId: string;
  connectionProviderName: string;
  connectedAccountId: string;
};
```

The `ConnectedAccount` is gone by the time the hook runs, so
`getConnection` no longer resolves. Anything the cleanup needs has to be
in the key-value store, written at connect time and keyed by
`connectedAccountId`. The docs section spells that out, along with the
fact that uninstalling an app drops its connections through a cascade
that never reaches this hook, where `uninstallLogicFunction` is the
right tool instead.

Both dispatches moved into a new
`ConnectionProviderLifecycleHookService`, so
`ConnectionProviderOAuthFlowService` no longer owns hook plumbing and
`ConnectedAccountMetadataService.delete` can reuse it. On-connect
behaviour is unchanged: best effort, never blocks the caller, failures
go to Sentry.

## Tests

- `connection-provider-lifecycle-hook.service.spec.ts`: the on-connect
cases moved over, plus on-disconnect dispatch, no-hook, and
missing-provider cases
- `connection-provider-oauth-flow.service.spec.ts`: now asserts
delegation to the lifecycle hook service
- SDK validation, manifest duplicate-identifier, and manifest to flat
converter specs extended

Server unit tests and typecheck for shared, sdk and server pass locally.
This commit is contained in:
Abdul Rahman
2026-08-04 08:17:23 +05:30
committed by GitHub
parent 5ffa121e59
commit 3a646ffcb0
24 changed files with 594 additions and 244 deletions
@@ -197,36 +197,39 @@ describe('manifestValidate', () => {
);
});
it('should not flag a connection provider referencing a logic function via onConnectLogicFunction as a duplicate', () => {
const logicFunctionId = '550e8400-e29b-41d4-a716-446655440040';
it.each(['onConnectLogicFunction', 'onDisconnectLogicFunction'] as const)(
'should not flag a connection provider referencing a logic function via %s as a duplicate',
(lifecycleHookKey) => {
const logicFunctionId = '550e8400-e29b-41d4-a716-446655440040';
const logicFunction = {
universalIdentifier: logicFunctionId,
name: 'onConnect',
sourceHandlerPath: 'src/logic-functions/on-connect.ts',
builtHandlerPath: 'dist/on-connect.js',
builtHandlerChecksum: '00000000-0000-4000-8000-000000000000',
handlerName: 'handler',
} as unknown as Manifest['logicFunctions'][number];
const logicFunction = {
universalIdentifier: logicFunctionId,
name: lifecycleHookKey,
sourceHandlerPath: 'src/logic-functions/lifecycle-hook.ts',
builtHandlerPath: 'dist/lifecycle-hook.js',
builtHandlerChecksum: '00000000-0000-4000-8000-000000000000',
handlerName: 'handler',
} as unknown as Manifest['logicFunctions'][number];
const connectionProvider = {
universalIdentifier: '550e8400-e29b-41d4-a716-446655440041',
name: 'slack',
displayName: 'Slack',
type: 'oauth',
oauth: {},
onConnectLogicFunction: { universalIdentifier: logicFunctionId },
} as unknown as NonNullable<Manifest['connectionProviders']>[number];
const connectionProvider = {
universalIdentifier: '550e8400-e29b-41d4-a716-446655440041',
name: 'slack',
displayName: 'Slack',
type: 'oauth',
oauth: {},
[lifecycleHookKey]: { universalIdentifier: logicFunctionId },
} as unknown as NonNullable<Manifest['connectionProviders']>[number];
const result = manifestValidate({
...validManifest,
logicFunctions: [logicFunction],
connectionProviders: [connectionProvider],
});
const result = manifestValidate({
...validManifest,
logicFunctions: [logicFunction],
connectionProviders: [connectionProvider],
});
expect(result.isValid).toBe(true);
expect(result.errors).toHaveLength(0);
});
expect(result.isValid).toBe(true);
expect(result.errors).toHaveLength(0);
},
);
it('should not flag a front component referenced via settingsFrontComponent as a duplicate', () => {
const frontComponentId = '550e8400-e29b-41d4-a716-446655440050';
@@ -626,18 +629,14 @@ describe('manifestValidate', () => {
expect(result.isValid).toBe(false);
expect(result.errors).toContainEqual(
expect.stringContaining(
'not "aggregateFieldMetadataId"',
),
expect.stringContaining('not "aggregateFieldMetadataId"'),
);
});
it('should ignore non-graph widgets that have no aggregate field', () => {
const result = manifestValidate({
...validManifest,
pageLayoutTabs: [
makeGraphWidgetTab({ configurationType: 'TIMELINE' }),
],
pageLayoutTabs: [makeGraphWidgetTab({ configurationType: 'TIMELINE' })],
});
expect(result.isValid).toBe(true);
@@ -75,6 +75,7 @@ const findUniversalIdentifiers = (obj: object): string[] => {
key === 'preInstallLogicFunction' ||
key === 'uninstallLogicFunction' ||
key === 'onConnectLogicFunction' ||
key === 'onDisconnectLogicFunction' ||
key === 'settingsFrontComponent'
) {
continue;