Skip to content

Commit

Permalink
[TECH] Pix1D - Utiliser le modèle "Assessment" du contexte "School"
Browse files Browse the repository at this point in the history
  • Loading branch information
pix-service-auto-merge authored Nov 20, 2023
2 parents 02a3b8d + 4fdb8eb commit 64c1434
Show file tree
Hide file tree
Showing 17 changed files with 103 additions and 64 deletions.
3 changes: 1 addition & 2 deletions 1d/app/models/activity.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Model, { attr, belongsTo } from '@ember-data/model';
import Model, { attr } from '@ember-data/model';

export default class Activity extends Model {
@attr('string') level;
@belongsTo('assessment', { async: true, inverse: 'activities' }) assessment;
}
14 changes: 2 additions & 12 deletions 1d/app/pods/assessment/model.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
import Model, { attr, hasMany } from '@ember-data/model';
import Model, { attr } from '@ember-data/model';

export default class Assessment extends Model {
// attributes
@attr('string') certificationNumber;
@attr('string') codeCampaign;
@attr('string') state;
@attr('string') title;
@attr('string') type;
@attr('string') lastQuestionState;
@attr('string') method;

// references
@attr('string') competenceId;
@attr('string') missionId;
@hasMany('activity', { async: true, inverse: 'assessment' }) activities;
@attr('string') organizationLearnerId;
}
8 changes: 4 additions & 4 deletions api/src/school/application/assessments/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ const createForPix1d = async function (request, h, dependencies = { assessmentSe
return h.response(dependencies.assessmentSerializer.serialize(createdAssessment)).created();
};

const getByAssessmentId = async function (request, h, dependencies = { assessmentSerializer }) {
const getById = async function (request, h, dependencies = { assessmentSerializer }) {
const assessmentId = request.params.id;
const missionAssessment = await usecases.getMissionAssessmentByAssessmentId({ assessmentId });
return dependencies.assessmentSerializer.serialize(missionAssessment);
const assessment = await usecases.getAssessmentById({ assessmentId });
return dependencies.assessmentSerializer.serialize(assessment);
};

const assessmentController = {
getNextChallengeForPix1d,
createForPix1d,
getByAssessmentId,
getById,
};

export { assessmentController };
2 changes: 1 addition & 1 deletion api/src/school/application/assessments/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const register = async function (server) {
}),
},
pre: [{ method: securityPreHandlers.checkPix1dActivated }],
handler: assessmentController.getByAssessmentId,
handler: assessmentController.getById,
tags: ['api'],
},
},
Expand Down
10 changes: 10 additions & 0 deletions api/src/school/domain/models/Assessment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Assessment as SharedAssessment } from '../../../shared/domain/models/Assessment.js';
class Assessment extends SharedAssessment {
constructor({ id, missionId, organizationLearnerId, state } = {}) {
super({ id, state });
this.missionId = missionId;
this.organizationLearnerId = organizationLearnerId;
}
}

export { Assessment };
6 changes: 3 additions & 3 deletions api/src/school/domain/usecases/create-mission-assessment.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Assessment } from '../../../shared/domain/models/Assessment.js';
import { MissionAssessment } from '../models/MissionAssessment.js';
import { Assessment } from '../models/Assessment.js';
import { MissionAssessment } from '../../infrastructure/models/mission-assessment.js';

const createMissionAssessment = async function ({
missionId,
Expand All @@ -17,7 +17,7 @@ const createMissionAssessment = async function ({
});
await missionAssessmentRepository.save({ missionAssessment });

return missionAssessment;
return new Assessment({ ...persistedAssessment, ...missionAssessment });
};

export { createMissionAssessment };
9 changes: 9 additions & 0 deletions api/src/school/domain/usecases/get-assessment-by-id.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Assessment } from '../models/Assessment.js';

const getAssessmentById = async function ({ assessmentId, missionAssessmentRepository, assessmentRepository }) {
const rawAssessment = await assessmentRepository.get(assessmentId);
const rawMissionAssessment = await missionAssessmentRepository.getByAssessmentId(assessmentId);
return new Assessment({ ...rawAssessment, ...rawMissionAssessment });
};

export { getAssessmentById };

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { knex } from '../../../../db/knex-database-connection.js';
import { MissionAssessment } from '../../domain/models/MissionAssessment.js';
import { Assessment } from '../../../shared/domain/models/Assessment.js';
import { MissionAssessment } from '../models/mission-assessment.js';

const save = async function ({ missionAssessment }) {
await knex('mission-assessments').insert({ ...missionAssessment });
Expand Down
12 changes: 3 additions & 9 deletions api/src/school/infrastructure/serializers/assessment.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import { Serializer } from 'jsonapi-serializer';

const serialize = function (missionAssessment) {
const serialize = function (assessment) {
return new Serializer('assessment', {
transform: (missionAssessment) => {
return {
...missionAssessment,
id: missionAssessment.assessmentId,
};
},
attributes: ['missionId', 'organizationLearnerId'],
}).serialize(missionAssessment);
attributes: ['missionId', 'organizationLearnerId', 'state'],
}).serialize(assessment);
};

export { serialize };
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import { expect, hFake, sinon } from '../../../../test-helper.js';
import { assessmentController } from '../../../../../src/school/application/assessments/controller.js';
import { usecases } from '../../../../../src/school/shared/usecases/index.js';
import { MissionAssessment } from '../../../../../src/school/domain/models/MissionAssessment.js';
import { Assessment } from '../../../../../src/school/domain/models/Assessment.js';

describe('Integration | Controller | assessment-controller', function () {
describe('#createForPix1d', function () {
it('should call the expected usecase and return the serialized assessment', async function () {
const missionId = 'mission-id';
const assessmentId = 1234;
const organizationLearnerId = 5678;
const createdAssessment = new MissionAssessment({ missionId, assessmentId, organizationLearnerId });
const createdAssessment = new Assessment({
id: assessmentId,
missionId,
assessmentId,
organizationLearnerId,
state: Assessment.states.STARTED,
});
sinon
.stub(usecases, 'createMissionAssessment')
.withArgs({ missionId, organizationLearnerId: 34567 })
Expand All @@ -24,29 +30,33 @@ describe('Integration | Controller | assessment-controller', function () {
attributes: {
'mission-id': missionId,
'organization-learner-id': organizationLearnerId,
state: Assessment.states.STARTED,
},
type: 'assessments',
});
});
});
describe('#getByAssessmentId', function () {
describe('#getById', function () {
it('should call the expected usecase and return the serialized assessment', async function () {
const missionId = 'mission-id';
const assessmentId = 1234;
const organizationLearnerId = 5678;
const createdMissionAssessment = new MissionAssessment({ missionId, assessmentId, organizationLearnerId });
sinon
.stub(usecases, 'getMissionAssessmentByAssessmentId')
.withArgs({ assessmentId })
.resolves(createdMissionAssessment);
const createdMissionAssessment = new Assessment({
missionId,
id: assessmentId,
organizationLearnerId,
state: Assessment.states.STARTED,
});
sinon.stub(usecases, 'getAssessmentById').withArgs({ assessmentId }).resolves(createdMissionAssessment);
const request = { params: { id: assessmentId } };

const result = await assessmentController.getByAssessmentId(request, hFake);
const result = await assessmentController.getById(request, hFake);
expect(result.data).to.deep.equal({
id: assessmentId.toString(),
attributes: {
'mission-id': missionId,
'organization-learner-id': organizationLearnerId,
state: Assessment.states.STARTED,
},
type: 'assessments',
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { databaseBuilder, expect, knex } from '../../../../test-helper.js';
import * as assessmentRepository from '../../../../../src/shared/infrastructure/repositories/assessment-repository.js';
import * as missionAssessmentRepository from '../../../../../src/school/infrastructure/repositories/mission-assessment-repository.js';
import { createMissionAssessment } from '../../../../../src/school/domain/usecases/create-mission-assessment.js';
import { Assessment } from '../../../../../src/shared/domain/models/Assessment.js';
import { Assessment } from '../../../../../src/school/domain/models/Assessment.js';

describe('Integration | UseCases | create-mission-assessment', function () {
let dependencies;
Expand All @@ -20,7 +20,7 @@ describe('Integration | UseCases | create-mission-assessment', function () {
const organizationLearnerId = databaseBuilder.factory.buildOrganizationLearner().id;
await databaseBuilder.commit();

const missionAssessment = await createMissionAssessment({
const result = await createMissionAssessment({
missionId,
organizationLearnerId,
...dependencies,
Expand All @@ -31,7 +31,7 @@ describe('Integration | UseCases | create-mission-assessment', function () {
type: Assessment.types.PIX1D_MISSION,
method: Assessment.methods.PIX1D,
};
const record = await knex('assessments').where({ id: missionAssessment.assessmentId }).first();
const record = await knex('assessments').where({ id: result.id }).first();

expect(_.pick(record, Object.keys(assessment))).to.deep.equal(assessment);
});
Expand All @@ -40,13 +40,38 @@ describe('Integration | UseCases | create-mission-assessment', function () {
const organizationLearnerId = databaseBuilder.factory.buildOrganizationLearner().id;
await databaseBuilder.commit();

const missionAssessment = await createMissionAssessment({
const result = await createMissionAssessment({
missionId,
organizationLearnerId,
...dependencies,
});

const record = await knex('mission-assessments').where({ assessmentId: missionAssessment.assessmentId }).first();
expect(_.pick(record, Object.keys(missionAssessment))).to.deep.equal(missionAssessment);
const expectedMissionAssesment = {
missionId,
organizationLearnerId,
assessmentId: result.id,
};
const missionAssesment = await knex('mission-assessments').where({ assessmentId: result.id }).first();
expect(_.pick(missionAssesment, Object.keys(expectedMissionAssesment))).to.deep.equal(expectedMissionAssesment);
});

it('should return a school assessment', async function () {
const organizationLearnerId = databaseBuilder.factory.buildOrganizationLearner().id;
await databaseBuilder.commit();

const assessment = await createMissionAssessment({
missionId,
organizationLearnerId,
...dependencies,
});

const expectedAssessment = new Assessment({
missionId,
organizationLearnerId,
id: assessment.id,
state: Assessment.states.STARTED,
});

expect(assessment).to.deep.equal(expectedAssessment);
});
});
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
import { databaseBuilder, expect } from '../../../../test-helper.js';
import { usecases } from '../../../../../src/school/shared/usecases/index.js';
import { MissionAssessment } from '../../../../../src/school/domain/models/MissionAssessment.js';
import { Assessment } from '../../../../../src/school/domain/models/Assessment.js';
import * as missionAssessmentRepository from '../../../../../src/school/infrastructure/repositories/mission-assessment-repository.js';
import * as assessmentRepository from '../../../../../src/shared/infrastructure/repositories/assessment-repository.js';

describe('Integration | UseCase | get-mission-assessment-by-assessment-id', function () {
describe('Integration | UseCase | getAssessmentById', function () {
it('should return the missionAssessment corresponding to the assessmentId', async function () {
const assessmentId = databaseBuilder.factory.buildPix1dAssessment().id;
const missionAssessment = databaseBuilder.factory.buildMissionAssessment({ assessmentId });
await databaseBuilder.commit();

const expectedMissionAssessment = new MissionAssessment({
const result = await usecases.getAssessmentById({
assessmentId,
missionAssessmentRepository,
assessmentRepository,
});

const expectedMissionAssessment = new Assessment({
id: result.id,
assessmentId,
organizationLearnerId: missionAssessment.organizationLearnerId,
missionId: missionAssessment.missionId,
state: Assessment.states.STARTED,
});

const result = await usecases.getMissionAssessmentByAssessmentId({ assessmentId, missionAssessmentRepository });

expect(result).to.deep.equal(expectedMissionAssessment);
});
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { databaseBuilder, expect } from '../../../../test-helper.js';
import * as missionAssessmentRepository from '../../../../../src/school/infrastructure/repositories/mission-assessment-repository.js';
import { MissionAssessment } from '../../../../../src/school/domain/models/MissionAssessment.js';
import { Assessment } from '../../../../../src/shared/domain/models/Assessment.js';
import { MissionAssessment } from '../../../../../src/school/infrastructure/models/mission-assessment.js';

describe('Integration | Repository | mission-assessment-repository', function () {
describe('#getByAssessmentId', function () {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { catchErr, databaseBuilder, expect } from '../../../test-helper.js';
import * as schoolRepository from '../../../../src/school/infrastructure/repositories/school-repository.js';
import { School } from '../../../../src/school/domain/models/School.js';
import { NotFoundError } from '../../../../lib/domain/errors.js';
import { Organization } from '../../../../lib/domain/models/index.js';
import { catchErr, databaseBuilder, expect } from '../../../../test-helper.js';
import * as schoolRepository from '../../../../../src/school/infrastructure/repositories/school-repository.js';
import { School } from '../../../../../src/school/domain/models/School.js';
import { NotFoundError } from '../../../../../lib/domain/errors.js';
import { Organization } from '../../../../../lib/domain/models/index.js';

describe('Integration | Repository | School', function () {
describe('#save', function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('Unit | Application | Router | assessment-router', function () {
const url = '/api/pix1d/assessments/1';

it('should return 200', async function () {
sinon.stub(assessmentController, 'getByAssessmentId').callsFake((request, h) => h.response('ok').code(200));
sinon.stub(assessmentController, 'getById').callsFake((request, h) => h.response('ok').code(200));
const httpTestServer = new HttpTestServer();
await httpTestServer.register(moduleUnderTest);

Expand Down

0 comments on commit 64c1434

Please sign in to comment.