@programisto/edrm-exams 0.2.6 → 0.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,11 @@
1
- import { enduranceListener, enduranceEventTypes } from '@programisto/endurance-core';
1
+ import { enduranceListener, enduranceEventTypes, enduranceEmitter } from '@programisto/endurance-core';
2
2
  import TestQuestion from '../models/test-question.model.js';
3
3
  import { generateLiveMessage } from '../lib/openai.js';
4
4
  import TestResult, { TestState } from '../models/test-result.model.js';
5
+ import CandidateModel from '../models/candidate.model.js';
6
+ import ContactModel from '../models/contact.model.js';
7
+ import TestModel from '../models/test.model.js';
5
8
  async function correctTest(options) {
6
- console.log('Correcting test', { options });
7
9
  if (!options.testId)
8
10
  throw new Error('TestId is required');
9
11
  if (!options.responses)
@@ -17,6 +19,7 @@ async function correctTest(options) {
17
19
  throw new Error('Test result not found');
18
20
  }
19
21
  let finalscore = 0;
22
+ let maxScore = 0;
20
23
  // Pour chaque réponse enregistrée en base, on cherche la correction correspondante
21
24
  for (const dbResponse of result.responses) {
22
25
  const correction = options.responses.find(r => r.questionId.toString() === dbResponse.questionId.toString());
@@ -27,6 +30,7 @@ async function correctTest(options) {
27
30
  console.error('Question not found', { questionId: dbResponse.questionId });
28
31
  continue;
29
32
  }
33
+ maxScore += question.maxScore;
30
34
  const scoreResponse = await generateLiveMessage('correctQuestion', {
31
35
  question: {
32
36
  _id: question._id.toString(),
@@ -53,15 +57,33 @@ async function correctTest(options) {
53
57
  result.state = TestState.Finish;
54
58
  // Forcer la sauvegarde des sous-documents responses
55
59
  result.markModified('responses');
60
+ const scorePercentage = (finalscore / maxScore) * 100;
56
61
  // Sauvegarder les modifications avec findByIdAndUpdate pour éviter les conflits de version
57
62
  await TestResult.findByIdAndUpdate(result._id, {
58
63
  $set: {
59
64
  responses: result.responses,
60
- score: result.score,
65
+ score: scorePercentage,
61
66
  state: result.state
62
67
  }
63
68
  });
64
- console.log('Test correction completed and saved', { finalScore: finalscore });
69
+ const test = await TestModel.findById(result.testId);
70
+ const candidate = await CandidateModel.findById(result.candidateId);
71
+ if (candidate) {
72
+ const contact = await ContactModel.findById(candidate.contact);
73
+ if (contact) {
74
+ enduranceEmitter.emit(enduranceEventTypes.SEND_EMAIL, {
75
+ template: 'test-result',
76
+ to: contact.email,
77
+ data: {
78
+ firstname: contact.firstname,
79
+ lastname: contact.lastname,
80
+ score: result.score,
81
+ testName: test?.title || '',
82
+ testLink: process.env.TEST_INVITATION_LINK || ''
83
+ }
84
+ });
85
+ }
86
+ }
65
87
  }
66
88
  catch (err) {
67
89
  if (err instanceof Error) {
@@ -1,5 +1,5 @@
1
1
  import { EnduranceRouter, EnduranceAuthMiddleware, enduranceEmitter, enduranceEventTypes } from '@programisto/endurance-core';
2
- import CandidateModel from '../models/candidate.models.js';
2
+ import CandidateModel from '../models/candidate.model.js';
3
3
  import ContactModel from '../models/contact.model.js';
4
4
  import TestResult from '../models/test-result.model.js';
5
5
  import Test from '../models/test.model.js';
@@ -3,7 +3,7 @@ import Test from '../models/test.model.js';
3
3
  import TestQuestion from '../models/test-question.model.js';
4
4
  import TestResult from '../models/test-result.model.js';
5
5
  import TestCategory from '../models/test-category.models.js';
6
- import Candidate from '../models/candidate.models.js';
6
+ import Candidate from '../models/candidate.model.js';
7
7
  import ContactModel from '../models/contact.model.js';
8
8
  import { generateLiveMessage } from '../lib/openai.js';
9
9
  class ExamsRouter extends EnduranceRouter {
@@ -565,6 +565,8 @@ class ExamsRouter extends EnduranceRouter {
565
565
  emailUser,
566
566
  emailPassword,
567
567
  data: {
568
+ firstname: contact.firstname,
569
+ testName: test?.title || '',
568
570
  testLink
569
571
  }
570
572
  });
@@ -810,15 +812,23 @@ class ExamsRouter extends EnduranceRouter {
810
812
  if (categoriesToUse.length === 0) {
811
813
  return res.status(400).json({ message: 'Aucune catégorie disponible pour générer des questions' });
812
814
  }
813
- const questionsPerCategory = Math.ceil(numberOfQuestions / categoriesToUse.length);
814
815
  const generatedQuestions = [];
815
- for (const categoryInfo of categoriesToUse) {
816
+ let questionsGenerated = 0;
817
+ // Mélanger les catégories pour une répartition aléatoire
818
+ const shuffledCategories = [...categoriesToUse].sort(() => Math.random() - 0.5);
819
+ for (const categoryInfo of shuffledCategories) {
820
+ // Arrêter si on a déjà généré le nombre de questions demandé
821
+ if (questionsGenerated >= numberOfQuestions)
822
+ break;
816
823
  const categoryDoc = await TestCategory.findById(categoryInfo.categoryId);
817
824
  if (!categoryDoc)
818
825
  continue;
819
826
  const otherQuestionsIds = test.questions.map(question => question.questionId);
820
827
  const otherQuestions = await TestQuestion.find({ _id: { $in: otherQuestionsIds } });
821
- for (let i = 0; i < questionsPerCategory; i++) {
828
+ // Calculer combien de questions générer pour cette catégorie
829
+ const remainingQuestions = numberOfQuestions - questionsGenerated;
830
+ const questionsForThisCategory = Math.min(remainingQuestions, 1); // Au maximum 1 question par catégorie
831
+ for (let i = 0; i < questionsForThisCategory; i++) {
822
832
  const generatedQuestion = await generateLiveMessage('createQuestion', {
823
833
  job: test.targetJob,
824
834
  seniority: test.seniorityLevel,
@@ -837,6 +847,7 @@ class ExamsRouter extends EnduranceRouter {
837
847
  await question.save();
838
848
  generatedQuestions.push(question);
839
849
  test.questions.push({ questionId: question._id, order: test.questions.length });
850
+ questionsGenerated++;
840
851
  }
841
852
  catch (parseError) {
842
853
  console.error('Erreur lors du parsing de la question générée:', parseError);
@@ -1,5 +1,5 @@
1
1
  import { EnduranceRouter, EnduranceAuthMiddleware, enduranceEmitter, enduranceEventTypes } from '@programisto/endurance-core';
2
- import CandidateModel from '../models/candidate.models.js';
2
+ import CandidateModel from '../models/candidate.model.js';
3
3
  import TestResult, { TestState } from '../models/test-result.model.js';
4
4
  import Test from '../models/test.model.js';
5
5
  class ResultRouter extends EnduranceRouter {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "@programisto/edrm-exams",
4
- "version": "0.2.6",
4
+ "version": "0.2.7",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },