-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] API verifiant si un utilisateur a été candidat de certif (P…
- Loading branch information
Showing
6 changed files
with
175 additions
and
1 deletion.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
api/src/certification/enrolment/application/api/candidates-api.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { usecases } from '../../../enrolment/domain/usecases/index.js'; | ||
|
||
/** | ||
* Checks if a user has been candidate to a certification | ||
* | ||
* @function | ||
* @param {Object} params | ||
* @param {number} params.userId user id to search for candidates | ||
* @returns {Promise<boolean>} | ||
* @throws {TypeError} preconditions failed | ||
*/ | ||
export const hasBeenCandidate = async ({ userId }) => { | ||
if (!userId) { | ||
throw new TypeError('user identifier is required'); | ||
} | ||
return usecases.hasBeenCandidate({ userId }); | ||
}; |
13 changes: 13 additions & 0 deletions
13
api/src/certification/enrolment/domain/usecases/has-been-candidate.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
api/tests/certification/enrolment/unit/application/api/candidates-api_test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { hasBeenCandidate } from '../../../../../../src/certification/enrolment/application/api/candidates-api.js'; | ||
import { usecases } from '../../../../../../src/certification/enrolment/domain/usecases/index.js'; | ||
import { catchErr, expect, sinon } from '../../../../../test-helper.js'; | ||
|
||
describe('Unit | Certification | Enrolment | API | candidates-api', function () { | ||
describe('hasBeenCandidate', function () { | ||
it('should check if a user has been candidate', async function () { | ||
// given | ||
sinon.stub(usecases, 'hasBeenCandidate').resolves(); | ||
|
||
// when | ||
await hasBeenCandidate({ userId: 12 }); | ||
|
||
// then | ||
expect(usecases.hasBeenCandidate).to.have.been.calledOnceWithExactly({ userId: 12 }); | ||
}); | ||
|
||
it('should reject calls without a userId', async function () { | ||
// given | ||
sinon.stub(usecases, 'hasBeenCandidate').resolves(); | ||
|
||
// when | ||
const error = await catchErr(() => hasBeenCandidate({ userId: null }))(); | ||
|
||
// then | ||
expect(error).to.be.instanceOf(TypeError); | ||
expect(error.message).to.equals('user identifier is required'); | ||
}); | ||
}); | ||
}); |
58 changes: 58 additions & 0 deletions
58
api/tests/certification/enrolment/unit/domain/usecases/has-been-candidate_test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
}); | ||
}); |