Skip to content

Commit

Permalink
refactor(auth): remove database trigger and update user creation
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
AMoreaux committed Dec 23, 2024
1 parent c12bfc2 commit 3ce581b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 56 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,20 @@ export class SignInUpService {
lastName: string;
picture: SignInUpServiceInput['picture'];
}) {
const user: Partial<User> = {
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(
Expand All @@ -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,
});
Expand All @@ -377,7 +383,7 @@ export class SignInUpService {
value: true,
});

return user;
return newUser;
}

async uploadPicture(
Expand Down

0 comments on commit 3ce581b

Please sign in to comment.