-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathattach-badges.js
165 lines (141 loc) · 6.1 KB
/
attach-badges.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* @typedef {import ('../../domain/usecases/index.js').ComplementaryCertificationBadgesRepository} ComplementaryCertificationBadgesRepository
* @typedef {import ('../models/ComplementaryCertification.js').ComplementaryCertification} ComplementaryCertification
* @typedef {import ('../../../shared/domain/models/ComplementaryCertificationBadge.js').ComplementaryCertificationBadge} ComplementaryCertificationBadge
*/
import lodash from 'lodash';
import { DomainTransaction } from '../../../../../lib/infrastructure/DomainTransaction.js';
import { MissingAttributesError, NotFoundError } from '../../../../shared/domain/errors.js';
import { InvalidBadgeLevelError } from '../errors.js';
import { BadgeToAttach } from '../models/BadgeToAttach.js';
const { isNil, uniq } = lodash;
/**
* @param {Object} params
* @param {ComplementaryCertification} params.complementaryCertification
* @param {number} params.userId
* @param {number|null} params.targetProfileIdToDetach
* @param {Array<ComplementaryCertificationBadge>} params.complementaryCertificationBadgesToAttachDTO
* @param {ComplementaryCertificationBadgesRepository} params.complementaryCertificationBadgesRepository
*/
const attachBadges = async function ({
complementaryCertification,
userId,
targetProfileIdToDetach,
complementaryCertificationBadgesToAttachDTO,
complementaryCertificationBadgesRepository,
}) {
_verifyThatLevelsAreConsistent({
complementaryCertificationBadgesToAttachDTO,
});
await _verifyThatBadgesToAttachExist({
complementaryCertificationBadgesToAttachDTO,
complementaryCertificationBadgesRepository,
});
if (complementaryCertification.hasExternalJury) {
if (_isRequiredInformationMissing(complementaryCertificationBadgesToAttachDTO))
throw new MissingAttributesError(
'Certificate and temporary certificate messages are required for complementary certification with external jury',
);
}
const complementaryCertificationBadges = complementaryCertificationBadgesToAttachDTO.map((badgeToAttachDTO) => {
return BadgeToAttach.from({
...badgeToAttachDTO,
complementaryCertificationId: complementaryCertification.id,
userId,
});
});
await DomainTransaction.execute(async () => {
if (targetProfileIdToDetach) {
const relatedComplementaryCertificationBadgesIds =
await complementaryCertificationBadgesRepository.getAllIdsByTargetProfileId({
targetProfileId: targetProfileIdToDetach,
});
await _detachExistingComplementaryCertificationBadge({
complementaryCertificationBadgesRepository,
relatedComplementaryCertificationBadgesIds,
});
}
await _attachNewComplementaryCertificationBadges({
complementaryCertificationBadgesRepository,
complementaryCertificationBadges,
});
});
};
export { attachBadges };
function _isRequiredInformationMissing(complementaryCertificationBadgesToAttachDTO) {
return complementaryCertificationBadgesToAttachDTO.some(
(complementaryCertificationBadge) =>
isNil(complementaryCertificationBadge.certificateMessage) ||
isNil(complementaryCertificationBadge.temporaryCertificateMessage),
);
}
/**
* @param {Object} params
* @param {ComplementaryCertificationBadgesRepository} params.complementaryCertificationBadgesRepository
*/
async function _attachNewComplementaryCertificationBadges({
complementaryCertificationBadgesRepository,
complementaryCertificationBadges,
}) {
return complementaryCertificationBadgesRepository.attach({
complementaryCertificationBadges,
});
}
/**
* @param {Object} params
* @param {ComplementaryCertificationBadgesRepository} params.complementaryCertificationBadgesRepository
*/
async function _detachExistingComplementaryCertificationBadge({
complementaryCertificationBadgesRepository,
relatedComplementaryCertificationBadgesIds,
}) {
if (relatedComplementaryCertificationBadgesIds.length === 0) {
throw new NotFoundError('No badges for this target profile.');
}
await complementaryCertificationBadgesRepository.detachByIds({
complementaryCertificationBadgeIds: relatedComplementaryCertificationBadgesIds,
});
}
function _compareLevels(level1, level2) {
return level1 - level2;
}
function _ifLevelIsUniq({ sortedUniqLevels, complementaryCertificationBadgesToAttachDTO }) {
return sortedUniqLevels.length == complementaryCertificationBadgesToAttachDTO.length;
}
function _isFirstLevelDifferentThanOne(sortedUniqLevels) {
return sortedUniqLevels[0] !== 1;
}
function _isLastLevelDifferentThanExpectedMaximum({ sortedUniqLevels, complementaryCertificationBadgesToAttachDTO }) {
return sortedUniqLevels.at(-1) !== complementaryCertificationBadgesToAttachDTO.length;
}
function _verifyThatLevelsAreConsistent({ complementaryCertificationBadgesToAttachDTO }) {
const extractedLevelsFromBadges =
complementaryCertificationBadgesToAttachDTO?.map((badge) => badge.level).filter(Number.isInteger) ?? [];
const sortedUniqLevels = uniq([...extractedLevelsFromBadges]).sort(_compareLevels);
if (!_ifLevelIsUniq({ sortedUniqLevels, complementaryCertificationBadgesToAttachDTO })) {
throw new InvalidBadgeLevelError();
}
if (_isFirstLevelDifferentThanOne(sortedUniqLevels)) {
throw new InvalidBadgeLevelError();
}
if (_isLastLevelDifferentThanExpectedMaximum({ sortedUniqLevels, complementaryCertificationBadgesToAttachDTO })) {
throw new InvalidBadgeLevelError();
}
}
/**
* @param {Object} params
* @param {ComplementaryCertificationBadgesRepository} params.complementaryCertificationBadgesRepository
*/
async function _verifyThatBadgesToAttachExist({
complementaryCertificationBadgesToAttachDTO,
complementaryCertificationBadgesRepository,
}) {
if (complementaryCertificationBadgesToAttachDTO?.length <= 0) {
throw new NotFoundError('One or several badges are not attachable.');
}
const ids = complementaryCertificationBadgesToAttachDTO.map((ccBadgeToAttach) => ccBadgeToAttach.badgeId);
const badges = await complementaryCertificationBadgesRepository.findAttachableBadgesByIds({ ids });
if (badges?.length !== ids.length) {
throw new NotFoundError('One or several badges are not attachable.');
}
}