Skip to content

Commit

Permalink
[TECH] Déplacement de la fonctionnalité du kit surveillant dans le do…
Browse files Browse the repository at this point in the history
…ssier src (PIX-9461).

 #7420
  • Loading branch information
pix-service-auto-merge authored Nov 9, 2023
2 parents f3cfbfa + 3afb35d commit 55872e0
Show file tree
Hide file tree
Showing 34 changed files with 499 additions and 485 deletions.
18 changes: 0 additions & 18 deletions api/lib/application/sessions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,24 +132,6 @@ const register = async function (server) {
tags: ['api', 'finalized-sessions'],
},
},
{
method: 'GET',
path: '/api/sessions/{id}/supervisor-kit',
config: {
auth: false,
validate: {
params: Joi.object({
id: identifiersType.sessionId,
}),
},
handler: sessionController.getSupervisorKitPdf,
tags: ['api', 'sessions', 'supervisor'],
notes: [
'- **Cette route est restreinte aux utilisateurs appartenant à un centre de certification ayant créé la session**\n' +
'- Cette route permet de télécharger le kit surveillant au format pdf',
],
},
},
{
method: 'GET',
path: '/api/sessions/{id}/candidates-import-sheet',
Expand Down
24 changes: 0 additions & 24 deletions api/lib/application/sessions/session-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import * as queryParamsUtils from '../../infrastructure/utils/query-params-utils
import * as requestResponseUtils from '../../infrastructure/utils/request-response-utils.js';
import { getSessionCertificationResultsCsv } from '../../infrastructure/utils/csv/certification-results/get-session-certification-results-csv.js';
import { fillCandidatesImportSheet } from '../../infrastructure/files/candidates-import/fill-candidates-import-sheet.js';
import * as supervisorKitPdf from '../../infrastructure/utils/pdf/supervisor-kit-pdf.js';
import * as certificationAttestationPdf from '../../infrastructure/utils/pdf/certification-attestation-pdf.js';
import lodash from 'lodash';
import { UserLinkedToCertificationCandidate } from '../../domain/events/UserLinkedToCertificationCandidate.js';
Expand Down Expand Up @@ -66,28 +65,6 @@ const update = async function (request, h, dependencies = { sessionSerializer })
return dependencies.sessionSerializer.serialize({ session: updatedSession });
};

const getSupervisorKitPdf = async function (
request,
h,
dependencies = { tokenService, requestResponseUtils, supervisorKitPdf },
) {
const sessionId = request.params.id;
const token = request.query.accessToken;
const userId = dependencies.tokenService.extractUserId(token);
const { lang } = request.query;
const sessionForSupervisorKit = await usecases.getSupervisorKitSessionInfo({ sessionId, userId });

const { buffer, fileName } = await dependencies.supervisorKitPdf.getSupervisorKitPdfBuffer({
sessionForSupervisorKit,
lang,
});

return h
.response(buffer)
.header('Content-Disposition', `attachment; filename=${fileName}`)
.header('Content-Type', 'application/pdf');
};

const getCandidatesImportSheet = async function (
request,
h,
Expand Down Expand Up @@ -394,7 +371,6 @@ const sessionController = {
getJurySession,
get,
update,
getSupervisorKitPdf,
getCandidatesImportSheet,
getCertificationCandidates,
getCertificationPDFAttestationsForSession,
Expand Down
19 changes: 0 additions & 19 deletions api/lib/domain/usecases/get-supervisor-kit-session-info.js

This file was deleted.

2 changes: 0 additions & 2 deletions api/lib/domain/usecases/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ import * as scorecardService from '../../domain/services/scorecard-service.js';
import * as scoringCertificationService from '../../domain/services/scoring/scoring-certification-service.js';
import * as sessionCodeService from '../../../src/certification/session/domain/services/session-code-service.js';
import * as sessionForSupervisingRepository from '../../infrastructure/repositories/sessions/session-for-supervising-repository.js';
import * as sessionForSupervisorKitRepository from '../../infrastructure/repositories/sessions/session-for-supervisor-kit-repository.js';
import * as sessionJuryCommentRepository from '../../infrastructure/repositories/sessions/session-jury-comment-repository.js';
import * as sessionPublicationService from '../../domain/services/session-publication-service.js';
import * as sessionRepository from '../../../src/certification/session/infrastructure/repositories/session-repository.js';
Expand Down Expand Up @@ -363,7 +362,6 @@ const dependencies = {
scoringCertificationService,
sessionCodeService,
sessionForSupervisingRepository,
sessionForSupervisorKitRepository,
sessionJuryCommentRepository,
sessionPublicationService,
sessionRepository,
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { usecases } from '../../shared/domain/usecases/index.js';
import { tokenService } from '../../../../lib/domain/services/token-service.js';
import * as requestResponseUtils from '../../../../lib/infrastructure/utils/request-response-utils.js';
import * as invigilatorKitPdf from '../infrastructure/utils/pdf/invigilator-kit-pdf.js';

const getInvigilatorKitPdf = async function (
request,
h,
dependencies = { tokenService, requestResponseUtils, invigilatorKitPdf },
) {
const sessionId = request.params.id;
const token = request.query.accessToken;
const userId = dependencies.tokenService.extractUserId(token);
const { lang } = request.query;
const sessionForInvigilatorKit = await usecases.getInvigilatorKitSessionInfo({ sessionId, userId });

const { buffer, fileName } = await dependencies.invigilatorKitPdf.getInvigilatorKitPdfBuffer({
sessionForInvigilatorKit,
lang,
});

return h
.response(buffer)
.header('Content-Disposition', `attachment; filename=${fileName}`)
.header('Content-Type', 'application/pdf');
};

const invigilatorKitController = {
getInvigilatorKitPdf,
};
export { invigilatorKitController };
29 changes: 29 additions & 0 deletions api/src/certification/session/application/invigilator-kit-route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import Joi from 'joi';
import { identifiersType } from '../../../../lib/domain/types/identifiers-type.js';
import { invigilatorKitController } from './invigilator-kit-controller.js';

const register = async function (server) {
server.route([
{
method: 'GET',
path: '/api/sessions/{id}/supervisor-kit',
config: {
auth: false,
validate: {
params: Joi.object({
id: identifiersType.sessionId,
}),
},
handler: invigilatorKitController.getInvigilatorKitPdf,
tags: ['api', 'sessions', 'invigilator'],
notes: [
'- **Cette route est restreinte aux utilisateurs appartenant à un centre de certification ayant créé la session**\n' +
'- Cette route permet de télécharger le kit surveillant au format pdf',
],
},
},
]);
};

const name = 'invigilator-kit-api';
export { register, name };
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
class SessionForSupervisorKit {
constructor({ id, date, time, address, room, examiner, accessCode, supervisorPassword, version }) {
class SessionForInvigilatorKit {
constructor({ id, date, time, address, room, examiner, accessCode, invigilatorPassword, version }) {
this.id = id;
this.date = date;
this.time = time;
this.address = address;
this.room = room;
this.examiner = examiner;
this.accessCode = accessCode;
this.supervisorPassword = supervisorPassword;
this.invigilatorPassword = invigilatorPassword;
this.version = version;
}
}

export { SessionForSupervisorKit };
export { SessionForInvigilatorKit };
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { UserNotAuthorizedToAccessEntityError } from '../../../../shared/domain/errors.js';

const getInvigilatorKitSessionInfo = async function ({
userId,
sessionId,
sessionRepository,
sessionForInvigilatorKitRepository,
}) {
const hasMembership = await sessionRepository.doesUserHaveCertificationCenterMembershipForSession(userId, sessionId);
if (!hasMembership) {
throw new UserNotAuthorizedToAccessEntityError('User is not allowed to access session.');
}

return await sessionForInvigilatorKitRepository.get(sessionId);
};

export { getInvigilatorKitSessionInfo };
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { knex } from '../../../../db/knex-database-connection.js';
import { SessionForSupervisorKit } from '../../../domain/read-models/SessionForSupervisorKit.js';
import { knex } from '../../../../../db/knex-database-connection.js';
import { SessionForInvigilatorKit } from '../../domain/read-models/SessionForInvigilatorKit.js';

const get = async function (idSession) {
const results = await knex
Expand All @@ -24,7 +24,8 @@ const get = async function (idSession) {
export { get };

function _toDomain(results) {
return new SessionForSupervisorKit({
return new SessionForInvigilatorKit({
...results,
invigilatorPassword: results.supervisorPassword,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { PDFDocument, rgb } from 'pdf-lib';

import pdfLibFontkit from '@pdf-lib/fontkit';
import * as url from 'url';
import { LOCALE } from '../../../../src/shared/domain/constants.js';
import { PIX_CERTIF } from '../../../domain/constants.js';
import { LOCALE } from '../../../../../shared/domain/constants.js';
import { PIX_CERTIF } from '../../../../../../lib/domain/constants.js';

const { ENGLISH_SPOKEN, FRENCH_SPOKEN } = LOCALE;

Expand All @@ -16,26 +16,26 @@ const SESSION_DETAIL_FONT_SIZE = 7;
const SESSION_DETAIL_LINE_HEIGHT = 8;
const { CURRENT_CERTIFICATION_VERSION } = PIX_CERTIF;

async function getSupervisorKitPdfBuffer({
sessionForSupervisorKit,
async function getInvigilatorKitPdfBuffer({
sessionForInvigilatorKit,
dirname = __dirname,
fontkit = pdfLibFontkit,
creationDate = new Date(),
lang = FRENCH_SPOKEN,
} = {}) {
let templatePath;
let fileName;
const fileVersion = sessionForSupervisorKit.version === CURRENT_CERTIFICATION_VERSION ? '' : '-v3';
const fileVersion = sessionForInvigilatorKit.version === CURRENT_CERTIFICATION_VERSION ? '' : '-v3';

switch (lang) {
case ENGLISH_SPOKEN:
templatePath = `${dirname}/files/invigilator-kit_template.pdf`;
fileName = `invigilator-kit-${sessionForSupervisorKit.id}.pdf`;
fileName = `invigilator-kit-${sessionForInvigilatorKit.id}.pdf`;
break;

default:
templatePath = `${dirname}/files/kit-surveillant_template${fileVersion}.pdf`;
fileName = `kit-surveillant-${sessionForSupervisorKit.id}${fileVersion}.pdf`;
fileName = `kit-surveillant-${sessionForInvigilatorKit.id}${fileVersion}.pdf`;
break;
}

Expand All @@ -48,19 +48,19 @@ async function getSupervisorKitPdfBuffer({

pdfDoc.registerFontkit(fontkit);

const fontFile = await readFile(`${dirname}/files/Roboto-Medium.ttf`);
const fontFile = await readFile(`${dirname}/../../../../shared/infrastructure/utils/pdf/files/Roboto-Medium.ttf`);
const robotFont = await pdfDoc.embedFont(fontFile, { subset: true, customName: 'Roboto-Medium.ttf' });

const [page] = pdfDoc.getPages();

_drawSessionDate({ lang, sessionForSupervisorKit, page, robotFont });
_drawSessionStartTime(sessionForSupervisorKit, page, robotFont);
_drawSessionAddress(sessionForSupervisorKit, page, robotFont);
_drawSessionExaminer(sessionForSupervisorKit, page, robotFont);
_drawSessionRoom(sessionForSupervisorKit, page, robotFont);
_drawSessionId(sessionForSupervisorKit, page, robotFont);
_drawSupervisorPassword(sessionForSupervisorKit, page, robotFont);
_drawAccessCode(sessionForSupervisorKit, page, robotFont);
_drawSessionDate({ lang, sessionForInvigilatorKit, page, robotFont });
_drawSessionStartTime(sessionForInvigilatorKit, page, robotFont);
_drawSessionAddress(sessionForInvigilatorKit, page, robotFont);
_drawSessionExaminer(sessionForInvigilatorKit, page, robotFont);
_drawSessionRoom(sessionForInvigilatorKit, page, robotFont);
_drawSessionId(sessionForInvigilatorKit, page, robotFont);
_drawInvigilatorPassword(sessionForInvigilatorKit, page, robotFont);
_drawAccessCode(sessionForInvigilatorKit, page, robotFont);

const pdfBytes = await pdfDoc.save();
const buffer = Buffer.from(pdfBytes);
Expand All @@ -71,8 +71,8 @@ async function getSupervisorKitPdfBuffer({
};
}

function _drawSessionDate({ lang, sessionForSupervisorKit, page, font }) {
const date = new Date(sessionForSupervisorKit.date);
function _drawSessionDate({ lang, sessionForInvigilatorKit, page, font }) {
const date = new Date(sessionForInvigilatorKit.date);
const day = date.getDate();
const year = date.getFullYear();
const options = { month: 'short' };
Expand All @@ -95,8 +95,8 @@ function _drawSessionDate({ lang, sessionForSupervisorKit, page, font }) {
});
}

function _drawSessionStartTime(sessionForSupervisorKit, page, font) {
const [hours, minutes] = sessionForSupervisorKit.time.split(':');
function _drawSessionStartTime(sessionForInvigilatorKit, page, font) {
const [hours, minutes] = sessionForInvigilatorKit.time.split(':');
const hour = `${hours}h${minutes}`;
page.drawText(hour, {
x: 182,
Expand All @@ -107,9 +107,9 @@ function _drawSessionStartTime(sessionForSupervisorKit, page, font) {
});
}

function _drawSessionAddress(sessionForSupervisorKit, page, font) {
function _drawSessionAddress(sessionForInvigilatorKit, page, font) {
const addressArray = _toArrayOfFixedWidthConservingWords(
sessionForSupervisorKit.address,
sessionForInvigilatorKit.address,
font,
MAX_SESSION_DETAIL_WIDTH,
);
Expand All @@ -124,8 +124,8 @@ function _drawSessionAddress(sessionForSupervisorKit, page, font) {
});
}

function _drawSessionRoom(sessionForSupervisorKit, page, font) {
const roomArray = _toArrayOfFixedWidthConservingWords(sessionForSupervisorKit.room, font, MAX_SESSION_DETAIL_WIDTH);
function _drawSessionRoom(sessionForInvigilatorKit, page, font) {
const roomArray = _toArrayOfFixedWidthConservingWords(sessionForInvigilatorKit.room, font, MAX_SESSION_DETAIL_WIDTH);
roomArray.forEach((room, index) => {
page.drawText(room, {
x: 60,
Expand All @@ -137,9 +137,9 @@ function _drawSessionRoom(sessionForSupervisorKit, page, font) {
});
}

function _drawSessionExaminer(sessionForSupervisorKit, page, font) {
function _drawSessionExaminer(sessionForInvigilatorKit, page, font) {
const examinerArray = _toArrayOfFixedWidthConservingWords(
sessionForSupervisorKit.examiner,
sessionForInvigilatorKit.examiner,
font,
MAX_SESSION_DETAIL_WIDTH,
);
Expand All @@ -154,8 +154,8 @@ function _drawSessionExaminer(sessionForSupervisorKit, page, font) {
});
}

function _drawSessionId(sessionForSupervisorKit, page, font) {
const sessionId = String(sessionForSupervisorKit.id);
function _drawSessionId(sessionForInvigilatorKit, page, font) {
const sessionId = String(sessionForInvigilatorKit.id);
const textWidth = font.widthOfTextAtSize(sessionId, 10);
page.drawText(sessionId, {
x: 277 - textWidth / 2,
Expand All @@ -166,10 +166,10 @@ function _drawSessionId(sessionForSupervisorKit, page, font) {
});
}

function _drawSupervisorPassword(sessionForSupervisorKit, page, font) {
const supervisorPassword = `C-${sessionForSupervisorKit.supervisorPassword}`;
const textWidth = font.widthOfTextAtSize(supervisorPassword, 10);
page.drawText(supervisorPassword, {
function _drawInvigilatorPassword(sessionForInvigilatorKit, page, font) {
const invigilatorPassword = `C-${sessionForInvigilatorKit.invigilatorPassword}`;
const textWidth = font.widthOfTextAtSize(invigilatorPassword, 10);
page.drawText(invigilatorPassword, {
x: 383 - textWidth / 2,
y: 594,
size: 10,
Expand All @@ -178,8 +178,8 @@ function _drawSupervisorPassword(sessionForSupervisorKit, page, font) {
});
}

function _drawAccessCode(sessionForSupervisorKit, page, font) {
const accessCode = sessionForSupervisorKit.accessCode;
function _drawAccessCode(sessionForInvigilatorKit, page, font) {
const accessCode = sessionForInvigilatorKit.accessCode;
const textWidth = font.widthOfTextAtSize(accessCode, 10);
page.drawText(accessCode, {
x: 486 - textWidth / 2,
Expand Down Expand Up @@ -208,4 +208,4 @@ function _toArrayOfFixedWidthConservingWords(str, font, maxWidth) {
return result.map((str) => str.trim());
}

export { getSupervisorKitPdfBuffer };
export { getInvigilatorKitPdfBuffer };
Loading

0 comments on commit 55872e0

Please sign in to comment.