-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathget-user-certification-eligibility.js
118 lines (108 loc) · 4.72 KB
/
get-user-certification-eligibility.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
* @typedef {import ('./index.js').ComplementaryCertificationBadgeWithOffsetVersionRepository} ComplementaryCertificationBadgeWithOffsetVersionRepository
*/
import _ from 'lodash';
import { AssessmentResult } from '../../../../shared/domain/models/index.js';
import { ComplementaryCertificationKeys } from '../../../shared/domain/models/ComplementaryCertificationKeys.js';
import { CertificationEligibility, UserCertificationEligibility } from '../read-models/UserCertificationEligibility.js';
/**
* @param {Object} params
* @param {ComplementaryCertificationBadgeWithOffsetVersionRepository} params.complementaryCertificationBadgeWithOffsetVersionRepository
*/
const getUserCertificationEligibility = async function ({
userId,
limitDate = new Date(),
placementProfileService,
certificationBadgesService,
complementaryCertificationCourseRepository,
pixCertificationRepository,
complementaryCertificationBadgeWithOffsetVersionRepository,
}) {
const placementProfile = await placementProfileService.getPlacementProfile({ userId, limitDate });
const isCertifiable = placementProfile.isCertifiable();
const userAcquiredBadges = await certificationBadgesService.findLatestBadgeAcquisitions({
userId,
limitDate,
});
const complementaryCertificationCourseWithResultsAcquiredByUser =
await complementaryCertificationCourseRepository.findByUserId({
userId,
});
const userPixCertifications = await pixCertificationRepository.findByUserId({ userId });
const allComplementaryCertificationBadges =
await complementaryCertificationBadgeWithOffsetVersionRepository.findAll();
const certificationEligibilities = [];
for (const acquiredBadge of userAcquiredBadges) {
const acquiredComplementaryCertificationBadge = allComplementaryCertificationBadges.find(
({ id }) => id === acquiredBadge.complementaryCertificationBadgeId,
);
let areEligibilityConditionsFulfilled = false;
const isClea = acquiredBadge.complementaryCertificationKey === ComplementaryCertificationKeys.CLEA;
if (isClea) {
areEligibilityConditionsFulfilled = isCertifiable;
} else {
areEligibilityConditionsFulfilled = _checkComplementaryEligibilityConditions(
allComplementaryCertificationBadges,
userPixCertifications,
acquiredComplementaryCertificationBadge.id,
);
}
const isAcquiredExpectedLevel = _hasAcquiredComplementaryCertificationForExpectedLevel(
complementaryCertificationCourseWithResultsAcquiredByUser,
acquiredComplementaryCertificationBadge,
);
const badgeIsOutdatedByOneVersionAndUserHasNoComplementaryCertificationForIt =
acquiredComplementaryCertificationBadge?.offsetVersion === 1 && !isAcquiredExpectedLevel;
const badgeIsNotOutdated = acquiredComplementaryCertificationBadge?.offsetVersion === 0;
if (
(badgeIsNotOutdated || badgeIsOutdatedByOneVersionAndUserHasNoComplementaryCertificationForIt) &&
areEligibilityConditionsFulfilled
) {
certificationEligibilities.push(
new CertificationEligibility({
label: acquiredBadge.complementaryCertificationBadgeLabel,
imageUrl: acquiredBadge.complementaryCertificationBadgeImageUrl,
isOutdated: acquiredBadge.isOutdated,
isAcquiredExpectedLevel,
}),
);
}
}
return new UserCertificationEligibility({
id: userId,
isCertifiable,
certificationEligibilities,
});
};
function _checkComplementaryEligibilityConditions(
allComplementaryCertificationBadges,
userPixCertifications,
complementaryCertificationBadgeId,
) {
const scoreRequired = allComplementaryCertificationBadges.find(
(complementaryCertificationBadge) => complementaryCertificationBadge.id === complementaryCertificationBadgeId,
).requiredPixScore;
const validatedUserPixCertifications = userPixCertifications.filter(
(pixCertification) =>
!pixCertification.isCancelled &&
!pixCertification.isRejectedForFraud &&
pixCertification.status === AssessmentResult.status.VALIDATED,
);
if (validatedUserPixCertifications.length === 0) {
return false;
} else {
const highestObtainedScore = _.maxBy(validatedUserPixCertifications, 'pixScore').pixScore;
return highestObtainedScore >= scoreRequired;
}
}
function _hasAcquiredComplementaryCertificationForExpectedLevel(
complementaryCertificationCourseWithResultsAcquiredByUser,
acquiredComplementaryCertificationBadge,
) {
return complementaryCertificationCourseWithResultsAcquiredByUser.some(
(certificationTakenByUser) =>
certificationTakenByUser.isAcquiredExpectedLevelByPixSource() &&
acquiredComplementaryCertificationBadge?.id === certificationTakenByUser.complementaryCertificationBadgeId,
);
}
export { getUserCertificationEligibility };