Skip to content

The open alternative to Salesforce, designed for AI.

Stop webhook subscriptions when transferring connected account ownership (#26571)

neo773426b46f7d30b
diff --git a/packages/twenty-server/src/engine/metadata-modules/connected-account/connected-account-metadata.service.ts b/packages/twenty-server/src/engine/metadata-modules/connected-account/connected-account-metadata.service.ts
index 532a5c0edcd..f62cc136211 100644
--- a/packages/twenty-server/src/engine/metadata-modules/connected-account/connected-account-metadata.service.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/connected-account/connected-account-metadata.service.ts
@@ -384,6 +384,28 @@ export class ConnectedAccountMetadataService {
const connectedAccountIds = connectedAccounts.map((account) => account.id);
+ const [messageChannels, calendarChannels] = await Promise.all([
+ this.messageChannelRepository.find({
+ where: { connectedAccountId: In(connectedAccountIds), workspaceId },
+ select: { id: true },
+ }),
+ this.calendarChannelRepository.find({
+ where: { connectedAccountId: In(connectedAccountIds), workspaceId },
+ select: { id: true },
+ }),
+ ]);
+
+ await this.stopWebhookSubscriptions({
+ messageChannels,
+ calendarChannels,
+ workspaceId,
+ }).catch((error) =>
+ this.logger.warn(
+ `WorkspaceId: ${workspaceId} Failed to stop webhook subscriptions while transferring connected accounts from ${fromUserWorkspaceId}`,
+ error,
+ ),
+ );
+
await this.repository.manager.transaction(async (entityManager) => {
await entityManager.update(
ConnectedAccountEntity,
@@ -413,6 +435,26 @@ export class ConnectedAccountMetadataService {
{ connectedAccountId: In(connectedAccountIds), workspaceId },
{ isSyncEnabled: false },
);
+
+ await entityManager.update(
+ MessageChannelEntity,
+ {
+ connectedAccountId: In(connectedAccountIds),
+ workspaceId,
+ webhookSubscriptionStatus: WebhookSubscriptionStatus.PENDING,
+ },
+ { webhookSubscriptionStatus: WebhookSubscriptionStatus.EXPIRED },
+ );
+
+ await entityManager.update(
+ CalendarChannelEntity,
+ {
+ connectedAccountId: In(connectedAccountIds),
+ workspaceId,
+ webhookSubscriptionStatus: WebhookSubscriptionStatus.PENDING,
+ },
+ { webhookSubscriptionStatus: WebhookSubscriptionStatus.EXPIRED },
+ );
});
for (const connectedAccount of connectedAccounts) {