Skip to content

Commit

Permalink
refactor(api): move reconcileSupOrganizationLearner route into prescr…
Browse files Browse the repository at this point in the history
…iption
  • Loading branch information
lionelB authored Nov 20, 2024
1 parent cd561bf commit 6d4823a
Show file tree
Hide file tree
Showing 10 changed files with 327 additions and 358 deletions.
46 changes: 0 additions & 46 deletions api/lib/application/sup-organization-learners/index.js

This file was deleted.

This file was deleted.

2 changes: 0 additions & 2 deletions api/lib/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import * as organizations from './application/organizations/index.js';
import * as passwords from './application/passwords/index.js';
import * as scoOrganizationLearners from './application/sco-organization-learners/index.js';
import * as sessions from './application/sessions/index.js';
import * as supOrganizationLearners from './application/sup-organization-learners/index.js';
import * as tags from './application/tags/index.js';
import * as targetProfiles from './application/target-profiles/index.js';
import * as userOrgaSettings from './application/user-orga-settings/index.js';
Expand All @@ -33,7 +32,6 @@ const routes = [
organizations,
passwords,
scoOrganizationLearners,
supOrganizationLearners,
sessions,
tags,
targetProfiles,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,31 @@ const updateStudentNumber = async function (request, h) {
return h.response().code(204);
};

const reconcileSupOrganizationLearner = async function (request, h) {
const userId = request.auth.credentials.userId;
const payload = request.payload.data.attributes;

const campaignCode = payload['campaign-code'];

const reconciliationInfo = {
userId,
studentNumber: payload['student-number'],
firstName: payload['first-name'],
lastName: payload['last-name'],
birthdate: payload['birthdate'],
};

await usecases.reconcileSupOrganizationLearner({ campaignCode, reconciliationInfo });

return h.response(null).code(204);
};

const supOrganizationManagementController = {
getOrganizationLearnersCsvTemplate,
importSupOrganizationLearners,
replaceSupOrganizationLearners,
updateStudentNumber,
reconcileSupOrganizationLearner,
};

export { supOrganizationManagementController };
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,38 @@ const ERRORS = {

const register = async function (server) {
server.route([
{
method: 'POST',
path: '/api/sup-organization-learners/association',
config: {
handler: supOrganizationManagementController.reconcileSupOrganizationLearner,
validate: {
options: {
allowUnknown: false,
},
payload: Joi.object({
data: {
attributes: {
'student-number': Joi.string().empty(Joi.string().regex(/^\s*$/)).required(),
'first-name': Joi.string().empty(Joi.string().regex(/^\s*$/)).required(),
'last-name': Joi.string().empty(Joi.string().regex(/^\s*$/)).required(),
birthdate: Joi.date().format('YYYY-MM-DD').required(),
'campaign-code': Joi.string().empty(Joi.string().regex(/^\s*$/)).required(),
},
type: 'sup-organization-learners',
},
}),
failAction: (request, h) => {
return sendJsonApiError(new UnprocessableEntityError('Un des champs saisis n’est pas valide.'), h);
},
},
notes: [
'- **Cette route est restreinte aux utilisateurs authentifiés**\n' +
'- Elle réconcilie l’utilisateur à l’inscription d’un étudiant dans cette organisation',
],
tags: ['api', 'sup-organization-learner'],
},
},
{
method: 'POST',
path: '/api/organizations/{organizationId}/sup-organization-learners/replace-csv',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,57 +13,6 @@ describe('Acceptance | Controller | sup-organization-learners', function () {
server = await createServer();
});

describe('POST /api/sup-organization-learners/association', function () {
let organization;
let campaign;
let options;
let user;

beforeEach(async function () {
// given
options = {
method: 'POST',
url: '/api/sup-organization-learners/association',
headers: {},
payload: {},
};

user = databaseBuilder.factory.buildUser();
organization = databaseBuilder.factory.buildOrganization();
campaign = databaseBuilder.factory.buildCampaign({ organizationId: organization.id });
databaseBuilder.factory.buildOrganizationLearner({
firstName: 'Jean',
lastName: 'Michel',
birthdate: new Date('2010-01-01'),
studentNumber: '12345',
organizationId: organization.id,
userId: null,
});

await databaseBuilder.commit();
});

it('should return an 204 status after updating higher organization learner', async function () {
// given
options.headers.authorization = generateValidRequestAuthorizationHeader(user.id);
options.payload.data = {
attributes: {
'student-number': '12345',
'first-name': 'Jean',
'last-name': 'Michel',
birthdate: '2010-01-01',
'campaign-code': campaign.code,
},
type: 'sup-organization-learners',
};

// when
const response = await server.inject(options);
// then
expect(response.statusCode).to.equal(204);
});
});

describe('PATCH /api/organizations/id/sup-organization-learners/organizationLearnerId', function () {
let organizationId;
const studentNumber = '54321';
Expand Down
Loading

0 comments on commit 6d4823a

Please sign in to comment.