diff --git a/api/src/identity-access-management/infrastructure/repositories/user-to-create.repository.js b/api/src/identity-access-management/infrastructure/repositories/user-to-create.repository.js index 61ceede393..56395b3459 100644 --- a/api/src/identity-access-management/infrastructure/repositories/user-to-create.repository.js +++ b/api/src/identity-access-management/infrastructure/repositories/user-to-create.repository.js @@ -1,7 +1,10 @@ import { PGSQL_UNIQUE_CONSTRAINT_VIOLATION_ERROR } from '../../../../db/pgsql-errors.js'; import { DomainTransaction } from '../../../../lib/infrastructure/DomainTransaction.js'; import { STUDENT_RECONCILIATION_ERRORS } from '../../../shared/domain/constants.js'; -import { OrganizationLearnerAlreadyLinkedToUserError } from '../../../shared/domain/errors.js'; +import { + AlreadyRegisteredEmailError, + OrganizationLearnerAlreadyLinkedToUserError, +} from '../../../shared/domain/errors.js'; import { User } from '../../domain/models/User.js'; /** @@ -55,8 +58,14 @@ async function _createWithUsername({ knexConnection, user }) { * @private */ async function _createWithoutUsername({ knexConnection, user }) { - const result = await knexConnection('users').insert(user).returning('*'); - return _toUserDomain(result[0]); + try { + const result = await knexConnection('users').insert(user).returning('*'); + return _toUserDomain(result[0]); + } catch (error) { + if (error.constraint === 'users_email_unique' && error.code === PGSQL_UNIQUE_CONSTRAINT_VIOLATION_ERROR) { + throw new AlreadyRegisteredEmailError(); + } + } } /** diff --git a/api/tests/identity-access-management/integration/infrastructure/repositories/user-to-create.repository.test.js b/api/tests/identity-access-management/integration/infrastructure/repositories/user-to-create.repository.test.js index e484a5d0d2..f5c6371986 100644 --- a/api/tests/identity-access-management/integration/infrastructure/repositories/user-to-create.repository.test.js +++ b/api/tests/identity-access-management/integration/infrastructure/repositories/user-to-create.repository.test.js @@ -1,7 +1,10 @@ import { User } from '../../../../../src/identity-access-management/domain/models/User.js'; import { UserToCreate } from '../../../../../src/identity-access-management/domain/models/UserToCreate.js'; import { userToCreateRepository } from '../../../../../src/identity-access-management/infrastructure/repositories/user-to-create.repository.js'; -import { OrganizationLearnerAlreadyLinkedToUserError } from '../../../../../src/shared/domain/errors.js'; +import { + AlreadyRegisteredEmailError, + OrganizationLearnerAlreadyLinkedToUserError, +} from '../../../../../src/shared/domain/errors.js'; import { catchErr, databaseBuilder, expect, knex } from '../../../../test-helper.js'; describe('Integration | Identity Access Management | Infrastructure | Repository | user-to-create', function () { @@ -80,6 +83,30 @@ describe('Integration | Identity Access Management | Infrastructure | Repository expect(userSaved.cgu).to.equal(user.cgu); expect(userSaved.locale).to.equal(user.locale); }); + + context('when email is already taken', function () { + it('throws an AlreadyRegisteredEmailError', async function () { + // given + const alreadyExistingEmail = 'already_used_for_another_account@example.net'; + databaseBuilder.factory.buildUser({ id: 7, email: alreadyExistingEmail }); + await databaseBuilder.commit(); + + const now = new Date('2022-02-01'); + const user = new UserToCreate({ + firstName: 'laura', + lastName: 'lune', + email: alreadyExistingEmail, + createdAt: now, + updatedAt: now, + }); + + // when + const error = await catchErr(userToCreateRepository.create)({ user }); + + // then + expect(error).to.be.instanceOf(AlreadyRegisteredEmailError); + }); + }); }); }); });