Skip to content

Commit 6a5cca3

Browse files
laura-bergoensxav-car
authored andcommittedFeb 18, 2025
tech(api): add api in learning content to expose skills by ids
1 parent 33312a3 commit 6a5cca3

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export class SkillDTO {
2+
constructor({ id, difficulty, tubeId }) {
3+
this.id = id;
4+
this.difficulty = difficulty;
5+
this.tubeId = tubeId;
6+
}
7+
}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import * as skillRepository from '../../../shared/infrastructure/repositories/skill-repository.js';
2+
import { SkillDTO } from './models/SkillDTO.js';
3+
4+
/**
5+
* @typedef SkillDTO
6+
* @type {object}
7+
* @property {string} id
8+
* @property {number} difficulty
9+
* @property {string} tubeId
10+
*/
11+
12+
/**
13+
* @function
14+
* @name findByIds
15+
*
16+
* @param {Object} params
17+
* @param {Array<string>} params.ids
18+
* @returns {Promise<Array<SkillDTO>>}
19+
*/
20+
export async function findByIds({ ids = [] }) {
21+
if (!ids?.length) {
22+
return [];
23+
}
24+
const skills = await skillRepository.findByRecordIds(ids);
25+
26+
return skills.map(_toApi);
27+
}
28+
29+
const _toApi = (skill) => new SkillDTO(skill);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { SkillDTO } from '../../../../../src/learning-content/application/api/models/SkillDTO.js';
2+
import * as skillsApi from '../../../../../src/learning-content/application/api/skills-api.js';
3+
import { databaseBuilder, expect } from '../../../../test-helper.js';
4+
5+
describe('Integration | LearningContent | Application | Api | skills', function () {
6+
describe('#findByIds', function () {
7+
it('should return an empty array when wrong ids collection provided', async function () {
8+
// given
9+
const emptySkillIds = [];
10+
const brokenSkillIds = null;
11+
12+
// when
13+
const resultForEmpty = await skillsApi.findByIds({ ids: emptySkillIds });
14+
const resultForBroken = await skillsApi.findByIds({ ids: brokenSkillIds });
15+
16+
// then
17+
expect(resultForEmpty).to.deepEqualArray([]);
18+
expect(resultForBroken).to.deepEqualArray([]);
19+
});
20+
21+
it('should return the skillDTOs collection corresponding to provided skill ids', async function () {
22+
// given
23+
databaseBuilder.factory.learningContent.buildSkill({
24+
id: 'skillA',
25+
level: 4,
26+
tubeId: 'tubeA',
27+
});
28+
databaseBuilder.factory.learningContent.buildSkill({
29+
id: 'skillC',
30+
level: 1,
31+
tubeId: 'tubeC',
32+
});
33+
databaseBuilder.factory.learningContent.buildSkill({
34+
id: 'skillB',
35+
level: 3,
36+
tubeId: 'tubeB',
37+
});
38+
await databaseBuilder.commit();
39+
const skillIds = ['skillA', 'skillB'];
40+
41+
// when
42+
const result = await skillsApi.findByIds({ ids: skillIds });
43+
44+
// then
45+
expect(result).to.deepEqualArray([
46+
new SkillDTO({ id: 'skillA', difficulty: 4, tubeId: 'tubeA' }),
47+
new SkillDTO({ id: 'skillB', difficulty: 3, tubeId: 'tubeB' }),
48+
]);
49+
});
50+
});
51+
});

0 commit comments

Comments
 (0)