From 7b840151dc3425427bcd423ab9e3f33858064332 Mon Sep 17 00:00:00 2001 From: Benjamin Petetot Date: Wed, 13 Nov 2024 10:59:52 +0100 Subject: [PATCH] feat(api): add public API checking if a user has been candidate --- .../application/api/candidates-api.js | 17 +++++++++++ .../application/api/candidates-api_test.js | 30 +++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 api/src/certification/enrolment/application/api/candidates-api.js create mode 100644 api/tests/certification/enrolment/unit/application/api/candidates-api_test.js diff --git a/api/src/certification/enrolment/application/api/candidates-api.js b/api/src/certification/enrolment/application/api/candidates-api.js new file mode 100644 index 00000000000..c5c5dbe850b --- /dev/null +++ b/api/src/certification/enrolment/application/api/candidates-api.js @@ -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} + * @throws {TypeError} preconditions failed + */ +export const hasBeenCandidate = async ({ userId }) => { + if (!userId) { + throw new TypeError('user identifier is required'); + } + return usecases.hasBeenCandidate({ userId }); +}; diff --git a/api/tests/certification/enrolment/unit/application/api/candidates-api_test.js b/api/tests/certification/enrolment/unit/application/api/candidates-api_test.js new file mode 100644 index 00000000000..1675d21ae2c --- /dev/null +++ b/api/tests/certification/enrolment/unit/application/api/candidates-api_test.js @@ -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'); + }); + }); +});