Skip to content

Commit

Permalink
feat(api): make userToCreateRepository.create throw AlreadyRegistered…
Browse files Browse the repository at this point in the history
…EmailError
  • Loading branch information
lego-technix committed Oct 10, 2024
1 parent 0d334ab commit 33a27ca
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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';

/**
Expand Down Expand Up @@ -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();
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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 () {
Expand Down Expand Up @@ -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 = '[email protected]';
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);
});
});
});
});
});

0 comments on commit 33a27ca

Please sign in to comment.