Skip to content

Commit

Permalink
feat(api): add use-case checking if a user has been candidate
Browse files Browse the repository at this point in the history
  • Loading branch information
bpetetot committed Nov 14, 2024
1 parent 0e53452 commit 8a20902
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* @typedef {import('./index.js').CandidateRepository} CandidateRepository
*/

/**
* @param {Object} params
* @param {number} params.userId
* @param {CandidateRepository} params.candidateRepository
*/
export async function hasBeenCandidate({ userId, candidateRepository }) {
const candidates = await candidateRepository.findByUserId({ userId });
return candidates.some((candidate) => candidate.isReconciled());
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { hasBeenCandidate } from '../../../../../../src/certification/enrolment/domain/usecases/has-been-candidate.js';
import { domainBuilder, expect, sinon } from '../../../../../test-helper.js';

describe('Certification | Enrolment | Unit | UseCase | has-been-candidate', function () {
let candidateRepository;

beforeEach(function () {
candidateRepository = { findByUserId: sinon.stub() };
});

context('when at least one candidate is reconciled', function () {
it('should return true', async function () {
// given
const candidate1 = domainBuilder.certification.enrolment.buildCandidate({
userId: 4321,
reconciledAt: new Date(),
});
const candidate2 = domainBuilder.certification.enrolment.buildCandidate({ userId: 4321 });

candidateRepository.findByUserId.withArgs({ userId: 4321 }).resolves([candidate1, candidate2]);

// when
const result = await hasBeenCandidate({ userId: 4321, candidateRepository });

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

context('when no candidates are reconciled', function () {
it('should return false', async function () {
// given
const candidate1 = domainBuilder.certification.enrolment.buildCandidate({ userId: 4321 });
const candidate2 = domainBuilder.certification.enrolment.buildCandidate({ userId: 4321 });

candidateRepository.findByUserId.withArgs({ userId: 4321 }).resolves([candidate1, candidate2]);

// when
const result = await hasBeenCandidate({ userId: 4321, candidateRepository });

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

context('when no candidates are returned', function () {
it('should return false', async function () {
// given
candidateRepository.findByUserId.withArgs({ userId: 4321 }).resolves([]);

// when
const result = await hasBeenCandidate({ userId: 4321, candidateRepository });

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

0 comments on commit 8a20902

Please sign in to comment.