From 3ce581bbe36339dcd88a5f109c85e8464e7db02c Mon Sep 17 00:00:00 2001 From: Antoine Moreaux Date: Mon, 23 Dec 2024 14:11:12 +0100 Subject: [PATCH] refactor(auth): remove database trigger and update user creation Removed the database trigger for setting `canImpersonate` for the first user and implemented the logic directly in the `sign-in-up.service`. This simplifies the code by handling the first user creation within the application layer, ensuring better maintainability. --- ...to-update-can-impersonate-on-first-user.ts | 44 ------------------- .../auth/services/sign-in-up.service.ts | 30 ++++++++----- 2 files changed, 18 insertions(+), 56 deletions(-) delete mode 100644 packages/twenty-server/src/database/typeorm/core/migrations/common/1734950137596-add-trigger-to-update-can-impersonate-on-first-user.ts diff --git a/packages/twenty-server/src/database/typeorm/core/migrations/common/1734950137596-add-trigger-to-update-can-impersonate-on-first-user.ts b/packages/twenty-server/src/database/typeorm/core/migrations/common/1734950137596-add-trigger-to-update-can-impersonate-on-first-user.ts deleted file mode 100644 index 6bb19ab9f529..000000000000 --- a/packages/twenty-server/src/database/typeorm/core/migrations/common/1734950137596-add-trigger-to-update-can-impersonate-on-first-user.ts +++ /dev/null @@ -1,44 +0,0 @@ -import { MigrationInterface, QueryRunner } from 'typeorm'; - -export class AddTriggerToUpdateCanImpersonateOnFirstUser1734950137596 - implements MigrationInterface -{ - name = 'AddTriggerToUpdateCanImpersonateOnFirstUser1734950137596'; - - public async up(queryRunner: QueryRunner): Promise { - queryRunner.query(` -CREATE OR REPLACE FUNCTION set_can_impersonate_to_true_on_first_user() - RETURNS TRIGGER AS $$ -BEGIN - IF NOT EXISTS (SELECT 1 FROM core."user") THEN - NEW."canImpersonate" := true; - PERFORM delete_first_user_trigger(); - END IF; - RETURN NEW; -END; -$$ LANGUAGE plpgsql; -`); - queryRunner.query(` -CREATE TRIGGER insert_user_trigger - BEFORE INSERT ON core."user" - FOR EACH ROW -EXECUTE FUNCTION set_can_impersonate_to_true_on_first_user(); -`); - queryRunner.query(` -CREATE OR REPLACE FUNCTION delete_first_user_trigger() - RETURNS void AS $$ -BEGIN - EXECUTE 'DROP TRIGGER IF EXISTS insert_user_trigger ON core.user'; -END; -$$ LANGUAGE plpgsql; -`); - } - - public async down(queryRunner: QueryRunner): Promise { - queryRunner.query(` -DROP TRIGGER IF EXISTS insert_user_trigger ON core."user"; -DROP FUNCTION IF EXISTS set_can_impersonate_to_true_on_first_user(); -DROP FUNCTION IF EXISTS delete_first_user_trigger(); -`); - } -} 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 2f9a3beb8b2c..149096733ab6 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 @@ -324,9 +324,20 @@ export class SignInUpService { lastName: string; picture: SignInUpServiceInput['picture']; }) { + const user: Partial = { + email, + firstName, + lastName, + canImpersonate: false, + passwordHash, + }; + if (!this.environmentService.get('IS_MULTIWORKSPACE_ENABLED')) { const workspacesCount = await this.workspaceRepository.count(); + // if the workspace doesn't exist it means it's the first user of the workspace + user.canImpersonate = true; + // let the creation of the first workspace if (workspacesCount > 0) { throw new AuthException( @@ -351,23 +362,18 @@ export class SignInUpService { const workspace = await this.workspaceRepository.save(workspaceToCreate); - const imagePath = await this.uploadPicture(picture, workspace.id); + user.defaultAvatarUrl = await this.uploadPicture(picture, workspace.id); - const userToCreate = this.userRepository.create({ - email: email, - firstName: firstName, - lastName: lastName, - defaultAvatarUrl: imagePath, - canImpersonate: false, - passwordHash, + const userCreated = this.userRepository.create({ + ...user, defaultWorkspace: workspace, }); - const user = await this.userRepository.save(userToCreate); + const newUser = await this.userRepository.save(userCreated); - await this.userWorkspaceService.create(user.id, workspace.id); + await this.userWorkspaceService.create(newUser.id, workspace.id); - await this.activateOnboardingForUser(user, workspace, { + await this.activateOnboardingForUser(newUser, workspace, { firstName, lastName, }); @@ -377,7 +383,7 @@ export class SignInUpService { value: true, }); - return user; + return newUser; } async uploadPicture(