Skip to content

Commit

Permalink
✨ api: get subscriptions in usecase
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandrecoin authored and P-Jeremy committed Oct 10, 2024
1 parent 7a8329f commit 6f40493
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class CertificationCandidateSubscription {
constructor({ id, sessionId, eligibleSubscription, nonEligibleSubscription, sessionVersion }) {
constructor({ id, sessionId, eligibleSubscriptions, nonEligibleSubscription, sessionVersion }) {
this.id = id;
this.sessionId = sessionId;
this.eligibleSubscription = eligibleSubscription;
this.eligibleSubscriptions = eligibleSubscriptions;
this.nonEligibleSubscription = nonEligibleSubscription;
this.sessionVersion = sessionVersion;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const getCertificationCandidateSubscription = async function ({
return new CertificationCandidateSubscription({
id: certificationCandidateId,
sessionId: certificationCandidate.sessionId,
eligibleSubscription: null,
eligibleSubscriptions: [],
nonEligibleSubscription: null,
sessionVersion: session.version,
});
Expand All @@ -27,10 +27,11 @@ const getCertificationCandidateSubscription = async function ({
id: session.certificationCenterId,
});

let eligibleSubscription = null;
let eligibleSubscriptions = [];
let nonEligibleSubscription = null;
const certifiableBadgeAcquisitions = await certificationBadgesService.findStillValidBadgeAcquisitions({
userId: certificationCandidate.userId,
limitDate: certificationCandidate.reconciledAt,
});

if (center.isHabilitated(certificationCandidate.complementaryCertification.key)) {
Expand All @@ -40,7 +41,12 @@ const getCertificationCandidateSubscription = async function ({
);

if (isSubscriptionEligible) {
eligibleSubscription = certificationCandidate.complementaryCertification;
eligibleSubscriptions = certificationCandidate.subscriptions.map((subscription) => {
return {
label: subscription.type === 'COMPLEMENTARY' ? certificationCandidate.complementaryCertification.label : null,
type: subscription.type,
};
});
} else {
nonEligibleSubscription = certificationCandidate.complementaryCertification;
}
Expand All @@ -49,7 +55,7 @@ const getCertificationCandidateSubscription = async function ({
return new CertificationCandidateSubscription({
id: certificationCandidateId,
sessionId: certificationCandidate.sessionId,
eligibleSubscription,
eligibleSubscriptions,
nonEligibleSubscription,
sessionVersion: session.version,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe('Certification | Enrolment | Unit | Domain | UseCase | get-certificatio
userId,
sessionId,
subscriptions: [domainBuilder.buildCoreSubscription()],
reconciledAt: new Date('2024-10-09'),
};

sessionRepository = {
Expand Down Expand Up @@ -90,7 +91,7 @@ describe('Certification | Enrolment | Unit | Domain | UseCase | get-certificatio
domainBuilder.buildCertificationCandidateSubscription({
id: certificationCandidateId,
sessionId,
eligibleSubscription: null,
eligibleSubscriptions: [],
nonEligibleSubscription: null,
sessionVersion: 2,
}),
Expand Down Expand Up @@ -128,6 +129,7 @@ describe('Certification | Enrolment | Unit | Domain | UseCase | get-certificatio
const candidateWithoutComplementaryCertification = domainBuilder.buildCertificationCandidate({
...certificationCandidateData,
complementaryCertification: null,
subscriptions: [],
});
certificationCandidateRepository.getWithComplementaryCertification
.withArgs({ id: certificationCandidateId })
Expand All @@ -143,7 +145,82 @@ describe('Certification | Enrolment | Unit | Domain | UseCase | get-certificatio
centerRepository.getById.withArgs({ id: 777 }).resolves(center);

certificationBadgesService.findStillValidBadgeAcquisitions
.withArgs({ userId })
.withArgs({ userId, limitDate: certificationCandidateData.reconciledAt })
.resolves([certifiableBadgeAcquisition]);

// when
const certificationCandidateSubscription = await getCertificationCandidateSubscription({
certificationCandidateId,
certificationBadgesService,
certificationCandidateRepository,
centerRepository,
sessionRepository,
});

// then
expect(certificationCandidateSubscription).to.deep.equal(
domainBuilder.buildCertificationCandidateSubscription({
id: certificationCandidateId,
sessionId,
eligibleSubscriptions: [],
nonEligibleSubscription: null,
sessionVersion: 2,
}),
);
});
});

context('when the candidate is registered and eligible to one complementary certification', function () {
it('should return the candidate with one complementary certification', async function () {
// given
const certificationCandidateId = 123;
const userId = 456;
const sessionId = 789;

const complementaryCertification = domainBuilder.buildComplementaryCertification({ key: 'PIX+' });

const center = domainBuilder.certification.enrolment.buildCenter({
habilitations: [
domainBuilder.certification.enrolment.buildHabilitation({
key: 'PIX+',
}),
],
});

const certifiableBadgeAcquisition = domainBuilder.buildCertifiableBadgeAcquisition({
badge: domainBuilder.buildBadge({
key: 'PIX+_BADGE',
isCertifiable: true,
}),
complementaryCertificationKey: complementaryCertification.key,
complementaryCertification,
});

const candidateWithoutComplementaryCertification = domainBuilder.buildCertificationCandidate({
...certificationCandidateData,
complementaryCertification,
subscriptions: [
domainBuilder.certification.enrolment.buildComplementarySubscription({
certificationCandidateId,
complementaryCertificationId: complementaryCertification.id,
}),
],
});
certificationCandidateRepository.getWithComplementaryCertification
.withArgs({ id: certificationCandidateId })
.resolves(candidateWithoutComplementaryCertification);

sessionRepository.get.withArgs({ id: sessionId }).resolves(
domainBuilder.certification.enrolment.buildSession({
certificationCenterId: 777,
version: 2,
}),
);

centerRepository.getById.withArgs({ id: 777 }).resolves(center);

certificationBadgesService.findStillValidBadgeAcquisitions
.withArgs({ userId, limitDate: certificationCandidateData.reconciledAt })
.resolves([certifiableBadgeAcquisition]);

// when
Expand All @@ -160,12 +237,96 @@ describe('Certification | Enrolment | Unit | Domain | UseCase | get-certificatio
domainBuilder.buildCertificationCandidateSubscription({
id: certificationCandidateId,
sessionId,
eligibleSubscription: null,
eligibleSubscriptions: [
{
label: 'Complementary certification name',
type: 'COMPLEMENTARY',
},
],
nonEligibleSubscription: null,
sessionVersion: 2,
}),
);
});
});

context('when the candidate is registered and not eligible to any complementary certification', function () {
it('should return the candidate without any complementary certification', async function () {
// given
const certificationCandidateId = 123;
const userId = 456;
const sessionId = 789;

const complementaryCertification = domainBuilder.buildComplementaryCertification({ key: 'PIX+' });

const center = domainBuilder.certification.enrolment.buildCenter({
habilitations: [
domainBuilder.certification.enrolment.buildHabilitation({
key: 'PIX+',
}),
],
});

const certifiableBadgeAcquisition = domainBuilder.buildCertifiableBadgeAcquisition({
badge: domainBuilder.buildBadge({
key: 'PIX+_BADGE',
isCertifiable: true,
}),
complementaryCertificationKey: 'OTHER PIX+ KEY',
complementaryCertification,
});

const candidateWithoutComplementaryCertification = domainBuilder.buildCertificationCandidate({
...certificationCandidateData,
complementaryCertification,
subscriptions: [
domainBuilder.certification.enrolment.buildComplementarySubscription({
certificationCandidateId,
complementaryCertificationId: complementaryCertification.id,
}),
],
});
certificationCandidateRepository.getWithComplementaryCertification
.withArgs({ id: certificationCandidateId })
.resolves(candidateWithoutComplementaryCertification);

sessionRepository.get.withArgs({ id: sessionId }).resolves(
domainBuilder.certification.enrolment.buildSession({
certificationCenterId: 777,
version: 2,
}),
);

centerRepository.getById.withArgs({ id: 777 }).resolves(center);

certificationBadgesService.findStillValidBadgeAcquisitions
.withArgs({ userId, limitDate: certificationCandidateData.reconciledAt })
.resolves([certifiableBadgeAcquisition]);

// when
const certificationCandidateSubscription = await getCertificationCandidateSubscription({
certificationCandidateId,
certificationBadgesService,
certificationCandidateRepository,
centerRepository,
sessionRepository,
});

// then
expect(certificationCandidateSubscription).to.deep.equal(
domainBuilder.buildCertificationCandidateSubscription({
id: certificationCandidateId,
sessionId,
eligibleSubscriptions: [],
nonEligibleSubscription: {
id: 1,
key: 'PIX+',
label: 'Complementary certification name',
},
sessionVersion: 2,
}),
);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { CertificationCandidateSubscription } from '../../../../src/certificatio
const buildCertificationCandidateSubscription = function ({
id = 1234,
sessionId = 1234,
eligibleSubscription = null,
eligibleSubscriptions = [],
nonEligibleSubscription = null,
sessionVersion = 2,
} = {}) {
return new CertificationCandidateSubscription({
id,
sessionId,
eligibleSubscription,
eligibleSubscriptions,
nonEligibleSubscription,
sessionVersion,
});
Expand Down

0 comments on commit 6f40493

Please sign in to comment.