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:
@@ -45,6 +45,9 @@ export default defineConnectionProvider({
|
||||
// Optional: a logic function in this app to run right after a connection is
|
||||
// established. See "Run a logic function on connect".
|
||||
// onConnectLogicFunction: { universalIdentifier: '3a2b1c0d-...-...' },
|
||||
// Optional: a logic function in this app to run right after a connection is
|
||||
// removed. See "Run a logic function on disconnect".
|
||||
// onDisconnectLogicFunction: { universalIdentifier: '4d5e6f70-...-...' },
|
||||
});
|
||||
```
|
||||
|
||||
@@ -124,6 +127,42 @@ From there use `getConnection(connectedAccountId)` to read the fresh access toke
|
||||
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="Run a logic function on disconnect" description="Clean up when a connection is removed">
|
||||
|
||||
Anything an app claims at connect time has to be released when the connection goes away. A Slack integration that claims a `team_id` on connect, for instance, has to release that claim so another workspace can connect the same Slack team. Set `onDisconnectLogicFunction` to reference a logic function in the same app, and it runs right after the `ConnectedAccount` is deleted.
|
||||
|
||||
```ts src/connection-providers/slack-connection.ts
|
||||
export default defineConnectionProvider({
|
||||
universalIdentifier: '...',
|
||||
name: 'slack',
|
||||
displayName: 'Slack',
|
||||
type: 'oauth',
|
||||
oauth: {
|
||||
/* ... */
|
||||
},
|
||||
// Runs releaseSlackTeam after every Slack disconnection.
|
||||
onDisconnectLogicFunction: {
|
||||
universalIdentifier: '4470aba8-5ff5-4800-88db-2a427cd8677c',
|
||||
},
|
||||
});
|
||||
```
|
||||
|
||||
Like the on-connect hook it runs **asynchronously in the disconnecting workspace** and never blocks the disconnect. The handler receives the same payload shape:
|
||||
|
||||
```ts
|
||||
type OnDisconnectPayload = {
|
||||
connectionProviderId: string;
|
||||
connectionProviderName: string; // e.g. 'slack'
|
||||
connectedAccountId: string;
|
||||
};
|
||||
```
|
||||
|
||||
The `ConnectedAccount` is already gone when the hook runs, so `getConnection(connectedAccountId)` no longer resolves. Anything the cleanup needs (a `team_id`, an external subscription id) must have been written to the [key-value store](/developers/extend/apps/logic/key-value-store) at connect time, keyed by `connectedAccountId`.
|
||||
|
||||
The hook fires when a connection is removed on its own. Uninstalling the app drops its connections through a database cascade instead, so the hook does not run there. Declare an `uninstallLogicFunction` on `defineApplication` for that path: it runs before the app's metadata is deleted, so it can still call `listConnections` and clean up whatever is left.
|
||||
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="listConnections / getConnection" description="Use connections from a logic function">
|
||||
|
||||
Inside a logic function handler, `listConnections({ providerName })` returns this app's `ConnectedAccount` rows for the given provider, with refreshed access tokens.
|
||||
|
||||
Reference in New Issue
Block a user