Skip to content

Commit

Permalink
feat(api): change instruction to instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
rouxxi authored Nov 22, 2024
1 parent 1b72c0b commit e1bff15
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 85 deletions.
2 changes: 1 addition & 1 deletion api/src/school/application/assessment-controller.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as challengeSerializer from '../../shared/infrastructure/serializers/jsonapi/challenge-serializer.js';
import { usecases } from '../domain/usecases/index.js';
import * as activitySerializer from '../infrastructure/serializers/activity-serializer.js';
import * as assessmentSerializer from '../infrastructure/serializers/assessment-serializer.js';
import * as challengeSerializer from '../infrastructure/serializers/challenge-serializer.js';

const getNextChallengeForPix1d = async function (request, h, dependencies = { challengeSerializer }) {
const assessmentId = request.params.id;
Expand Down
2 changes: 1 addition & 1 deletion api/src/school/application/challenge-controller.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as challengeRepository from '../../shared/infrastructure/repositories/challenge-repository.js';
import * as challengeSerializer from '../../shared/infrastructure/serializers/jsonapi/challenge-serializer.js';
import { usecases } from '../domain/usecases/index.js';
import * as challengeSerializer from '../infrastructure/serializers/challenge-serializer.js';

const get = async function (request, h, dependencies = { challengeRepository, challengeSerializer }) {
const challenge = await usecases.getChallenge({ id: request.params.id });
Expand Down
7 changes: 1 addition & 6 deletions api/src/school/domain/services/challenge.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const challengeService = { getAlternativeVersion, mapChallenge };
export const challengeService = { getAlternativeVersion };

function getAlternativeVersion({ mission, activities, activityInfo }) {
const alreadyPlayedAlternativeVersions = activities
Expand Down Expand Up @@ -32,8 +32,3 @@ function getAlternativeVersion({ mission, activities, activityInfo }) {
function _randomIndexForChallenges(length, random = Math.random()) {
return Math.floor(random * length);
}

function mapChallenge(challenge) {
challenge.instruction = challenge.instruction?.split('***');
return challenge;
}
5 changes: 1 addition & 4 deletions api/src/school/domain/usecases/get-challenge.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import { challengeService } from '../services/challenge.js';

export async function getChallenge({ id, challengeRepository }) {
const challenge = await challengeRepository.get(id);
return challengeService.mapChallenge(challenge);
return challengeRepository.get(id);
}
4 changes: 1 addition & 3 deletions api/src/school/domain/usecases/get-next-challenge.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Activity } from '../models/Activity.js';
import { ActivityInfo } from '../models/ActivityInfo.js';
import { challengeService } from '../services/challenge.js';

export async function getNextChallenge({
assessmentId,
Expand All @@ -26,6 +25,5 @@ export async function getNextChallenge({
});

await assessmentRepository.updateWhenNewChallengeIsAsked({ id: assessmentId, lastChallengeId: challengeId });
const challenge = await challengeRepository.get(challengeId);
return challengeService.mapChallenge(challenge);
return challengeRepository.get(challengeId);
}
32 changes: 32 additions & 0 deletions api/src/school/infrastructure/serializers/challenge-serializer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import jsonapiSerializer from 'jsonapi-serializer';

const { Serializer } = jsonapiSerializer;

const serialize = function (challenges) {
return new Serializer('challenge', {
attributes: [
'type',
'instructions',
'proposals',
'illustrationUrl',
'embedUrl',
'embedTitle',
'embedHeight',
'webComponentTagName',
'webComponentProps',
'illustrationAlt',
'format',
'autoReply',
'focused',
'shuffled',
],
transform: (challenge) => {
return {
...challenge,
instructions: challenge.instruction?.split('***'),
};
},
}).serialize(challenges);
};

export { serialize };
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('Integration | Controller | challenge-controller', function () {
});

expect(response.statusCode).to.equal(200);
expect(response.result.data.attributes.instruction).to.deep.equal(expectedResult);
expect(response.result.data.attributes.instructions).to.deep.equal(expectedResult);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -102,57 +102,6 @@ describe('Integration | Usecase | get-next-challenge', function () {
expect(challenge).to.be.instanceOf(Challenge);
expect(updatedAssessment.lastChallengeId).to.equal('second_va_challenge_on_step_2_id');
});
it('should return next challenge with the instruction in an array', async function () {
const { assessmentId, missionId } = databaseBuilder.factory.buildMissionAssessment();

databaseBuilder.factory.buildActivity({
assessmentId,
level: Activity.levels.VALIDATION,
status: Activity.status.STARTED,
stepIndex: 0,
createdAt: new Date('2022-09-15'),
});

await databaseBuilder.commit();

mockLearningContent({
challenges: [
learningContentBuilder.buildChallenge({
id: 'multi_instruction_id',
skillId: 'skill_id',
instruction: 'Une première bulle.<br/>Pour tout mettre***Une deuxième bulle\n sur plusieurs lignes',
}),
],
skills: [learningContentBuilder.buildSkill({ id: 'skill_id' })],
missions: [
learningContentBuilder.buildMission({
id: missionId,
content: {
steps: [
{
validationChallenges: [['multi_instruction_id']],
},
],
},
}),
],
});

const challenge = await getNextChallenge({
assessmentId,
activityRepository,
activityAnswerRepository,
challengeRepository,
assessmentRepository,
missionAssessmentRepository,
missionRepository,
});

expect(challenge.instruction).to.deep.equal([
'Une première bulle.<br/>Pour tout mettre',
'Une deuxième bulle\n sur plusieurs lignes',
]);
});
});
});
});
18 changes: 0 additions & 18 deletions api/tests/school/unit/domain/services/challenge_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,22 +222,4 @@ describe('Unit | Service | Challenge', function () {
});
});
});

describe('#mapChallenge', function () {
context('when challenge has *** separator in instruction key', function () {
it('should split the text into an Array', async function () {
const instruction = 'Une première bulle.<br/>Pour tout mettre\n***\nUne deuxième bulle \n sur plusieurs lignes';
const challenge = domainBuilder.buildChallenge({
instruction,
});

const mappedChallenge = await challengeService.mapChallenge(challenge);

expect(mappedChallenge.instruction).to.deep.equal([
'Une première bulle.<br/>Pour tout mettre\n',
'\nUne deuxième bulle \n sur plusieurs lignes',
]);
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import * as serializer from '../../../../../src/school/infrastructure/serializers/challenge-serializer.js';
import { Challenge } from '../../../../../src/shared/domain/models/index.js';
import { expect } from '../../../../test-helper.js';

describe('Unit | Serializer | challenge-serializer', function () {
describe('#serialize()', function () {
it('should convert a Challenge model object into JSON API data', function () {
// given
const challenge = new Challenge({
id: 'challenge_id',
instruction: 'Une première bulle.<br/>Pour tout mettre***Une deuxième bulle\n sur plusieurs lignes',
proposals:
'- Ils sont bio.\n- Ils pèsent plus de 63 grammes.\n- Ce sont des oeufs frais.\n- Ils sont destinés aux consommateurs.\n- Ils ne sont pas lavés.\n',
type: 'QCM',
illustrationUrl: 'http://illustration.url',
timer: 300,
competenceId: 'competence_id',
embedUrl: 'url',
embedTitle: 'title',
embedHeight: 12,
webComponentTagName: 'tagName',
webComponentProps: {
prop1: 'value 1',
prop2: 'value 2',
},
format: 'mots',
shuffled: false,
locales: ['fr', 'en'],
focused: false,
illustrationAlt: 'alt',
autoReply: false,
});

// when
const json = serializer.serialize(challenge);

// then
expect(json).to.deep.equal({
data: {
type: 'challenges',
id: challenge.id,
attributes: {
instructions: ['Une première bulle.<br/>Pour tout mettre', 'Une deuxième bulle\n sur plusieurs lignes'],
proposals: challenge.proposals,
type: challenge.type,
'illustration-url': challenge.illustrationUrl,
format: 'mots',
shuffled: false,
focused: false,
'embed-url': 'url',
'embed-title': 'title',
'embed-height': 12,
'web-component-tag-name': 'tagName',
'web-component-props': {
prop1: 'value 1',
prop2: 'value 2',
},
'illustration-alt': 'alt',
'auto-reply': false,
},
},
});
});
});
});

0 comments on commit e1bff15

Please sign in to comment.