Skip to content

Commit

Permalink
feat(api): find certification candidates from a user id
Browse files Browse the repository at this point in the history
  • Loading branch information
bpetetot committed Nov 14, 2024
1 parent bd43f7a commit 0e53452
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ export async function findBySessionId({ sessionId }) {
return candidatesData.map(toDomain);
}

/**
* @function
* @param {Object} params
* @param {number} params.userId
*
* @return {Promise<Array<Candidate>>}
*/
export async function findByUserId({ userId }) {
const knexTransaction = DomainTransaction.getConnection();
const candidatesData = await buildBaseReadQuery(knexTransaction).where({ 'certification-candidates.userId': userId });
return candidatesData.map(toDomain);
}

/**
* @function
* @param {Object} candidate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as candidateRepository from '../../../../../../src/certification/enrolm
import { SUBSCRIPTION_TYPES } from '../../../../../../src/certification/shared/domain/constants.js';
import { catchErr, databaseBuilder, domainBuilder, expect, knex } from '../../../../../test-helper.js';

describe('Integration | Certification | Session | Repository | Candidate', function () {
describe('Integration | Certification | Enrolment | Repository | Candidate', function () {
describe('#get', function () {
context('when the candidate exists', function () {
it('should return the candidate', async function () {
Expand Down Expand Up @@ -128,6 +128,49 @@ describe('Integration | Certification | Session | Repository | Candidate', funct
});
});

describe('#findByUserId', function () {
context('when there are candidates', function () {
it('should return the candidates', async function () {
// given
const candidate1 = databaseBuilder.factory.buildCertificationCandidate();
databaseBuilder.factory.buildCoreSubscription({ certificationCandidateId: candidate1.id });
const userId = candidate1.userId;

const candidate2 = databaseBuilder.factory.buildCertificationCandidate({ userId });
databaseBuilder.factory.buildCoreSubscription({ certificationCandidateId: candidate2.id });

const candidate3 = databaseBuilder.factory.buildCertificationCandidate();
databaseBuilder.factory.buildCoreSubscription({ certificationCandidateId: candidate3.id });
await databaseBuilder.commit();

// when
const result = await candidateRepository.findByUserId({ userId });

// then
expect(result).to.deepEqualArray([
domainBuilder.certification.enrolment.buildCandidate({
...candidate1,
subscriptions: [domainBuilder.buildCoreSubscription({ certificationCandidateId: candidate1.id })],
}),
domainBuilder.certification.enrolment.buildCandidate({
...candidate2,
subscriptions: [domainBuilder.buildCoreSubscription({ certificationCandidateId: candidate2.id })],
}),
]);
});
});

context('when there are no candidates', function () {
it('returns an empty array', async function () {
//when
const result = await candidateRepository.findByUserId({ userId: 123 });

// then
expect(result).to.be.empty;
});
});
});

describe('#update', function () {
context('when the candidate exists', function () {
it('should update the candidate', async function () {
Expand Down

0 comments on commit 0e53452

Please sign in to comment.