-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy patharea-repository.js
129 lines (113 loc) · 4.53 KB
/
area-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
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
import { PIX_ORIGIN } from '../../domain/constants.js';
import { NotFoundError } from '../../domain/errors.js';
import { Area } from '../../domain/models/Area.js';
import { getTranslatedKey } from '../../domain/services/get-translated-text.js';
import { child, SCOPES } from '../utils/logger.js';
import * as competenceRepository from './competence-repository.js';
import { LearningContentDatasource } from './learning-content-datasource.js';
const TABLE_NAME = 'learningcontent.areas';
const logger = child('learningcontent:repository', { event: SCOPES.LEARNING_CONTENT });
export async function list({ locale } = {}) {
const cacheKey = 'list()';
const listCallback = (knex) => knex.orderBy('id');
const areaDtos = await getInstance().find(cacheKey, listCallback);
return areaDtos.map((areaDto) => toDomain(areaDto, locale));
}
export async function listWithPixCompetencesOnly({ locale } = {}) {
const cacheKey = 'listWithPixCompetencesOnly()';
const listPixAreasCallback = (knex) =>
knex
.join('learningcontent.frameworks', 'learningcontent.frameworks.id', `${TABLE_NAME}.frameworkId`)
.where('learningcontent.frameworks.name', PIX_ORIGIN)
.orderBy(`${TABLE_NAME}.name`);
const areaDtos = await getInstance().find(cacheKey, listPixAreasCallback);
return toDomainWithPixCompetences(areaDtos, locale);
}
export async function findByFrameworkIdWithCompetences({ frameworkId, locale }) {
const cacheKey = `findByFrameworkIdWithCompetences({ frameworkId: ${frameworkId} })`;
const findAreasByFrameworkIdCallback = (knex) => knex.where('frameworkId', frameworkId).orderBy('id');
const areaDtos = await getInstance().find(cacheKey, findAreasByFrameworkIdCallback);
return toDomainWithCompetences(areaDtos, locale);
}
export async function findByFrameworkId({ frameworkId, locale }) {
const cacheKey = `findByFrameworkId({ frameworkId: ${frameworkId} })`;
const findAreasByFrameworkIdCallback = (knex) => knex.where('frameworkId', frameworkId).orderBy('id');
const areaDtos = await getInstance().find(cacheKey, findAreasByFrameworkIdCallback);
return areaDtos.map((areaDto) => toDomain(areaDto, locale));
}
export async function findByRecordIds({ areaIds, locale }) {
const areaDtos = await getInstance().getMany(areaIds);
return areaDtos
.filter((areaDto) => areaDto)
.sort(byId)
.map((areaDto) => toDomain(areaDto, locale));
}
export async function getAreaCodeByCompetenceId(competenceId) {
const cacheKey = `getAreaCodeByCompetenceId(${competenceId})`;
const findByCompetenceIdCallback = (knex) => knex.whereRaw('?=ANY(??)', [competenceId, 'competenceIds']).limit(1);
const [areaDto] = await getInstance().find(cacheKey, findByCompetenceIdCallback);
return areaDto?.code;
}
export async function get({ id, locale }) {
const areaDto = await getInstance().load(id);
if (!areaDto) {
logger.warn({ areaId: id }, 'Domaine introuvable');
throw new NotFoundError(`Area "${id}" not found.`);
}
return toDomain(areaDto, locale);
}
export function clearCache(id) {
return getInstance().clearCache(id);
}
function byId(entityA, entityB) {
return entityA.id < entityB.id ? -1 : 1;
}
function toDomain(areaDto, locale) {
const translatedTitle = getTranslatedKey(areaDto.title_i18n, locale);
return new Area({
id: areaDto.id,
code: areaDto.code,
name: areaDto.name,
title: translatedTitle,
color: areaDto.color,
frameworkId: areaDto.frameworkId,
});
}
async function toDomainWithPixCompetences(areaDtos, locale) {
const areas = [];
for (const areaDto of areaDtos) {
const competences = [];
for (const competenceId of areaDto.competenceIds) {
const competence = await competenceRepository.get({ id: competenceId, locale });
if (competence.origin === PIX_ORIGIN) {
competences.push(competence);
}
}
const area = toDomain(areaDto, locale);
area.competences = competences;
areas.push(area);
}
return areas;
}
async function toDomainWithCompetences(areaDtos, locale) {
const areas = [];
for (const areaDto of areaDtos) {
const competences = [];
for (const competenceId of areaDto.competenceIds) {
const competence = await competenceRepository.get({ id: competenceId, locale });
competences.push(competence);
}
const area = toDomain(areaDto, locale);
area.competences = competences;
areas.push(area);
}
return areas;
}
/** @type {LearningContentDatasource} */
let instance;
function getInstance() {
if (!instance) {
instance = new LearningContentDatasource({ tableName: TABLE_NAME });
}
return instance;
}