-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathmission-repository.js
72 lines (64 loc) · 2.94 KB
/
mission-repository.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
import { config } from '../../../shared/config.js';
import { LOCALE } from '../../../shared/domain/constants.js';
import { getTranslatedKey } from '../../../shared/domain/services/get-translated-text.js';
import { LearningContentDatasource } from '../../../shared/infrastructure/repositories/learning-content-datasource.js';
import { Mission, MissionContent, MissionStep } from '../../domain/models/Mission.js';
import { MissionNotFoundError } from '../../domain/school-errors.js';
const { FRENCH_SPOKEN } = LOCALE;
const TABLE_NAME = 'learningcontent.missions';
export async function get(id, locale = FRENCH_SPOKEN) {
const parsedIntId = parseInt(id, 10);
if (isNaN(parsedIntId)) {
throw new MissionNotFoundError(id);
}
const missionDto = await getInstance().load(parsedIntId);
if (!missionDto) {
throw new MissionNotFoundError(id);
}
return toDomain(missionDto, locale);
}
export async function findAllActiveMissions(locale = FRENCH_SPOKEN) {
const cacheKey = 'findAllActiveMissions()';
const acceptedStatuses = config.featureToggles.showExperimentalMissions
? ['VALIDATED', 'EXPERIMENTAL']
: ['VALIDATED'];
const findActiveCallback = (knex) => knex.whereIn('status', acceptedStatuses).orderBy('id');
const missionDtos = await getInstance().find(cacheKey, findActiveCallback);
return missionDtos.map((missionDto) => toDomain(missionDto, locale));
}
export function clearCache(id) {
return getInstance().clearCache(id);
}
function getTranslatedContent(content, locale) {
const contentWithTranslatedSteps =
content?.steps?.map((step) => new MissionStep({ ...step, name: getTranslatedKey(step.name_i18n, locale) })) || [];
return new MissionContent({ ...content, steps: contentWithTranslatedSteps });
}
function toDomain(missionDto, locale) {
const translatedName = getTranslatedKey(missionDto.name_i18n, locale);
const translatedLearningObjectives = getTranslatedKey(missionDto.learningObjectives_i18n, locale);
const translatedValidatedObjectives = getTranslatedKey(missionDto.validatedObjectives_i18n, locale);
const translatedIntroductionMediaAlt = getTranslatedKey(missionDto.introductionMediaAlt_i18n, locale);
const translatedContent = getTranslatedContent(missionDto.content, locale);
return new Mission({
id: missionDto.id,
name: translatedName,
cardImageUrl: missionDto.cardImageUrl,
competenceId: missionDto.competenceId,
learningObjectives: translatedLearningObjectives,
validatedObjectives: translatedValidatedObjectives,
introductionMediaUrl: missionDto.introductionMediaUrl,
introductionMediaType: missionDto.introductionMediaType,
introductionMediaAlt: translatedIntroductionMediaAlt,
documentationUrl: missionDto.documentationUrl,
content: translatedContent,
});
}
/** @type {LearningContentDatasource} */
let instance;
function getInstance() {
if (!instance) {
instance = new LearningContentDatasource({ tableName: TABLE_NAME, idType: 'integer' });
}
return instance;
}