diff --git a/packages/twenty-server/src/engine/subscriptions/event-stream.resolver.ts b/packages/twenty-server/src/engine/subscriptions/event-stream.resolver.ts index 3bddb18cef..481bd1e953 100644 --- a/packages/twenty-server/src/engine/subscriptions/event-stream.resolver.ts +++ b/packages/twenty-server/src/engine/subscriptions/event-stream.resolver.ts @@ -64,16 +64,31 @@ export class EventStreamResolver { ) { const eventStreamChannelId = eventStreamIdToChannelId(eventStreamId); - const streamData = await this.eventStreamService.getStreamData( + const existingStreamData = await this.eventStreamService.getStreamData( workspace.id, eventStreamChannelId, ); - if (isDefined(streamData)) { - throw new EventStreamException( - 'Event stream already exists', - EventStreamExceptionCode.EVENT_STREAM_ALREADY_EXISTS, - ); + if (isDefined(existingStreamData)) { + const isAuthorized = await this.eventStreamService.isAuthorized({ + streamData: existingStreamData, + authContext: { + userWorkspaceId, + apiKeyId: apiKey?.id, + }, + }); + + if (!isAuthorized) { + throw new EventStreamException( + 'Event stream already exists', + EventStreamExceptionCode.EVENT_STREAM_ALREADY_EXISTS, + ); + } + + await this.eventStreamService.destroyEventStream({ + workspaceId: workspace.id, + eventStreamChannelId, + }); } await this.eventStreamService.createEventStream({