Skip to content

The open alternative to Salesforce, designed for AI.

Fix QueryRunnerAlreadyReleasedError in sign-in-up service (#20734)

Weiko265d2edc83bc
diff --git a/packages/twenty-server/src/engine/core-modules/auth/services/sign-in-up.service.ts b/packages/twenty-server/src/engine/core-modules/auth/services/sign-in-up.service.ts
index 116d681a2e3..d1bccf47805 100644
--- a/packages/twenty-server/src/engine/core-modules/auth/services/sign-in-up.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/auth/services/sign-in-up.service.ts
@@ -502,113 +502,110 @@ export class SignInUpService {
const workspaceId = v4();
const workspaceCustomApplicationId = v4();
- const queryRunner = this.dataSource.createQueryRunner();
-
- await queryRunner.connect();
- await queryRunner.startTransaction();
try {
- const workspaceToCreate = this.workspaceRepository.create({
- id: workspaceId,
- subdomain: await this.subdomainManagerService.generateSubdomain(
- isWorkEmailFound ? { userEmail: email } : {},
- ),
- workspaceCustomApplicationId,
- displayName: '',
- inviteHash: v4(),
- activationStatus: WorkspaceActivationStatus.PENDING_CREATION,
- });
-
- const workspace = await queryRunner.manager.save(
- WorkspaceEntity,
- workspaceToCreate,
- );
-
- const customApplication =
- await this.applicationService.createWorkspaceCustomApplication(
- {
- workspaceId,
- applicationId: workspaceCustomApplicationId,
- },
- queryRunner,
- );
-
- if (isWorkEmailFound) {
- const logoUrl = `${TWENTY_ICONS_BASE_URL}/${getDomainNameByEmail(email)}`;
- const logoFile =
- await this.fileCorePictureService.uploadWorkspaceLogoFromUrl({
- imageUrl: logoUrl,
- workspaceId,
- applicationUniversalIdentifier:
- customApplication.universalIdentifier,
- queryRunner,
+ const { user, workspace } = await this.dataSource.transaction(
+ async (entityManager) => {
+ const queryRunner = entityManager.queryRunner as QueryRunner;
+
+ const workspaceToCreate = this.workspaceRepository.create({
+ id: workspaceId,
+ subdomain: await this.subdomainManagerService.generateSubdomain(
+ isWorkEmailFound ? { userEmail: email } : {},
+ ),
+ workspaceCustomApplicationId,
+ displayName: '',
+ inviteHash: v4(),
+ activationStatus: WorkspaceActivationStatus.PENDING_CREATION,
});
- if (isDefined(logoFile)) {
- await queryRunner.manager.update(
+ const workspace = await queryRunner.manager.save(
WorkspaceEntity,
- { id: workspaceId },
- { logoFileId: logoFile.id },
+ workspaceToCreate,
);
- }
- }
-
- const isExistingUser = userData.type === 'existingUser';
- const user = isExistingUser
- ? userData.existingUser
- : await this.saveNewUser(
- userData.newUserWithPicture,
+
+ const customApplication =
+ await this.applicationService.createWorkspaceCustomApplication(
+ {
+ workspaceId,
+ applicationId: workspaceCustomApplicationId,
+ },
+ queryRunner,
+ );
+
+ if (isWorkEmailFound) {
+ const logoUrl = `${TWENTY_ICONS_BASE_URL}/${getDomainNameByEmail(email)}`;
+ const logoFile =
+ await this.fileCorePictureService.uploadWorkspaceLogoFromUrl({
+ imageUrl: logoUrl,
+ workspaceId,
+ applicationUniversalIdentifier:
+ customApplication.universalIdentifier,
+ queryRunner,
+ });
+
+ if (isDefined(logoFile)) {
+ await queryRunner.manager.update(
+ WorkspaceEntity,
+ { id: workspaceId },
+ { logoFileId: logoFile.id },
+ );
+ }
+ }
+
+ const isExistingUser = userData.type === 'existingUser';
+ const user = isExistingUser
+ ? userData.existingUser
+ : await this.saveNewUser(
+ userData.newUserWithPicture,
+ {
+ canImpersonate: shouldGrantServerAdmin,
+ canAccessFullAdminPanel: shouldGrantServerAdmin,
+ },
+ queryRunner,
+ );
+
+ await this.userWorkspaceService.create(
{
- canImpersonate: shouldGrantServerAdmin,
- canAccessFullAdminPanel: shouldGrantServerAdmin,
+ userId: user.id,
+ workspaceId: workspace.id,
+ isExistingUser,
+ pictureUrl: isExistingUser
+ ? undefined
+ : userData.newUserWithPicture.picture,
+ applicationUniversalIdentifier:
+ customApplication.universalIdentifier,
},
queryRunner,
);
- await this.userWorkspaceService.create(
- {
- userId: user.id,
- workspaceId: workspace.id,
- isExistingUser,
- pictureUrl: isExistingUser
- ? undefined
- : userData.newUserWithPicture.picture,
- applicationUniversalIdentifier: customApplication.universalIdentifier,
- },
- queryRunner,
- );
+ await this.activateOnboardingForUser(
+ {
+ user,
+ workspace,
+ shouldShowConnectAccountStep: true,
+ },
+ queryRunner,
+ );
- await this.activateOnboardingForUser(
- {
- user,
- workspace,
- shouldShowConnectAccountStep: true,
- },
- queryRunner,
- );
+ await this.onboardingService.setOnboardingInviteTeamPending(
+ {
+ workspaceId: workspace.id,
+ value: true,
+ },
+ queryRunner,
+ );
- await this.onboardingService.setOnboardingInviteTeamPending(
- {
- workspaceId: workspace.id,
- value: true,
+ return { user, workspace };
},
- queryRunner,
);
- await queryRunner.commitTransaction();
-
void this.auditService
.createContext({ workspaceId })
.insertWorkspaceEvent(WORKSPACE_CREATED_EVENT, {});
return { user, workspace };
- } catch (error) {
- if (queryRunner.isTransactionActive) {
- await queryRunner.rollbackTransaction();
- }
- throw error;
} finally {
- await queryRunner.release();
await this.workspaceCacheService.invalidateAndRecompute(workspaceId, [
'flatApplicationMaps',
]);