diff --git a/src/controllers/user-management/user.controller.ts b/src/controllers/user-management/user.controller.ts index 81fbab4e..0eff0a94 100644 --- a/src/controllers/user-management/user.controller.ts +++ b/src/controllers/user-management/user.controller.ts @@ -2,8 +2,10 @@ import { BadRequestException, Body, Controller, + ForbiddenException, Get, InternalServerErrorException, + Logger, NotFoundException, Param, ParseIntPipe, @@ -12,10 +14,7 @@ import { Query, Req, UseGuards, - ForbiddenException, - UnauthorizedException, } from "@nestjs/common"; -import { Logger } from "@nestjs/common"; import { ApiBearerAuth, ApiForbiddenResponse, @@ -34,8 +33,8 @@ import { UpdateUserDto } from "@dto/user-management/update-user.dto"; import { UserResponseDto } from "@dto/user-response.dto"; import { ErrorCodes } from "@entities/enum/error-codes.enum"; import { - checkIfUserIsGlobalAdmin, checkIfUserHasAccessToOrganization, + checkIfUserIsGlobalAdmin, OrganizationAccessScope, } from "@helpers/security-helper"; import { UserService } from "@services/user-management/user.service"; @@ -171,6 +170,12 @@ export class UserController { return user; } catch (err) { AuditLog.fail(ActionType.UPDATE, User.name, req.user.userId, id); + if ( + err instanceof QueryFailedError && + err.message.startsWith("duplicate key value violates unique constraint") + ) { + throw new BadRequestException(ErrorCodes.EmailAlreadyInUse); + } throw err; } } diff --git a/src/services/user-management/user.service.ts b/src/services/user-management/user.service.ts index 3c8826bc..fe7b9871 100644 --- a/src/services/user-management/user.service.ts +++ b/src/services/user-management/user.service.ts @@ -1,9 +1,9 @@ import { BadRequestException, + forwardRef, Inject, Injectable, Logger, - forwardRef, } from "@nestjs/common"; import { InjectRepository } from "@nestjs/typeorm"; import * as bcrypt from "bcryptjs"; @@ -22,9 +22,7 @@ import { Profile } from "passport-saml"; import { ListAllUsersMinimalResponseDto } from "@dto/list-all-users-minimal-response.dto"; import { ListAllEntitiesDto } from "@dto/list-all-entities.dto"; import { CreateNewKombitUserDto } from "@dto/user-management/create-new-kombit-user.dto"; -import * as nodemailer from "nodemailer"; import { Organization } from "@entities/organization.entity"; -import SMTPTransport from "nodemailer/lib/smtp-transport"; import { PermissionType } from "@enum/permission-type.enum"; import { ConfigService } from "@nestjs/config"; import { isPermissionType } from "@helpers/security-helper"; @@ -200,11 +198,8 @@ export class UserService { private async setPasswordHash(mappedUser: User, password: string) { this.checkPassword(password); // Hash password with bcrpyt - // this.logger.verbose("Generating salt"); const salt = await bcrypt.genSalt(10); - // this.logger.verbose("Generating hash"); mappedUser.passwordHash = await bcrypt.hash(password, salt); - // this.logger.verbose(`Generated hash: '${mappedUser.passwordHash}'`); } private checkPassword(password: string) {