@programisto/edrm-exams 0.3.9 → 0.3.10

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,3 +1,4 @@
1
+ import { Types } from 'mongoose';
1
2
  import { EnduranceSchema } from '@programisto/endurance';
2
3
  import Company from './company.model.js';
3
4
  import TestQuestion from './test-question.model.js';
@@ -32,6 +33,8 @@ declare class Test extends EnduranceSchema {
32
33
  title: string;
33
34
  description: string;
34
35
  companyId: typeof Company;
36
+ /** Identifiant de l'entité (portail multi-entités). Optionnel pour rétrocompatibilité. */
37
+ entityId?: Types.ObjectId;
35
38
  userId: typeof User;
36
39
  questions: TestQuestions[];
37
40
  state: TestState;
@@ -7,6 +7,7 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
7
7
  var __metadata = (this && this.__metadata) || function (k, v) {
8
8
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
9
  };
10
+ import { Types } from 'mongoose';
10
11
  import { EnduranceSchema, EnduranceModelType } from '@programisto/endurance';
11
12
  import Company from './company.model.js';
12
13
  import TestJob from './test-job.model.js';
@@ -44,6 +45,8 @@ let Test = class Test extends EnduranceSchema {
44
45
  title;
45
46
  description;
46
47
  companyId;
48
+ /** Identifiant de l'entité (portail multi-entités). Optionnel pour rétrocompatibilité. */
49
+ entityId;
47
50
  userId;
48
51
  questions;
49
52
  state;
@@ -90,6 +93,10 @@ __decorate([
90
93
  EnduranceModelType.prop({ ref: () => Company, required: false }),
91
94
  __metadata("design:type", Object)
92
95
  ], Test.prototype, "companyId", void 0);
96
+ __decorate([
97
+ EnduranceModelType.prop({ required: false }),
98
+ __metadata("design:type", Types.ObjectId)
99
+ ], Test.prototype, "entityId", void 0);
93
100
  __decorate([
94
101
  EnduranceModelType.prop({ ref: () => User, required: false }),
95
102
  __metadata("design:type", Object)
@@ -404,7 +404,8 @@ class ExamsRouter extends EnduranceRouter {
404
404
  targetJob: targetJobId,
405
405
  seniorityLevel,
406
406
  state,
407
- categories: processedCategories
407
+ categories: processedCategories,
408
+ ...(req.entity?._id && { entityId: req.entity._id })
408
409
  });
409
410
  await newTest.save();
410
411
  res.status(201).json({ message: 'test created with sucess', data: newTest });
@@ -451,6 +452,9 @@ class ExamsRouter extends EnduranceRouter {
451
452
  if (!test) {
452
453
  return res.status(404).json({ message: 'Test non trouvé' });
453
454
  }
455
+ if (req.entity?._id && test.entityId && !test.entityId.equals(req.entity._id)) {
456
+ return res.status(404).json({ message: 'Test non trouvé' });
457
+ }
454
458
  if (title)
455
459
  test.title = title;
456
460
  if (description)
@@ -539,6 +543,9 @@ class ExamsRouter extends EnduranceRouter {
539
543
  if (!test) {
540
544
  return res.status(404).json({ message: 'Test not found' });
541
545
  }
546
+ if (req.entity?._id && test.entityId && !test.entityId.equals(req.entity._id)) {
547
+ return res.status(404).json({ message: 'Test not found' });
548
+ }
542
549
  for (let i = 0; i < test.questions.length; i++) {
543
550
  await TestQuestion.findByIdAndDelete(test.questions[i].questionId);
544
551
  }
@@ -579,6 +586,10 @@ class ExamsRouter extends EnduranceRouter {
579
586
  if (!test) {
580
587
  return res.status(404).json({ message: 'no test founded with this id' });
581
588
  }
589
+ // Vérifier que le test appartient à l'entité courante (multi-entités)
590
+ if (req.entity?._id && test.entityId && !test.entityId.equals(req.entity._id)) {
591
+ return res.status(404).json({ message: 'no test founded with this id' });
592
+ }
582
593
  // Migration automatique si nécessaire
583
594
  await migrateTestIfNeeded(test);
584
595
  const questions = [];
@@ -666,6 +677,10 @@ class ExamsRouter extends EnduranceRouter {
666
677
  const sortOrder = req.query.sortOrder || 'desc';
667
678
  // Construction de la requête de recherche
668
679
  const query = {};
680
+ // Filtre par entité (contexte multi-entités)
681
+ if (req.entity?._id) {
682
+ query.entityId = req.entity._id;
683
+ }
669
684
  // Filtres
670
685
  if (targetJob !== 'all') {
671
686
  // Si on filtre par targetJob, on cherche d'abord le TestJob correspondant
@@ -784,9 +799,13 @@ class ExamsRouter extends EnduranceRouter {
784
799
  const category = await TestCategory.findOne({ name: categoryName });
785
800
  if (!category)
786
801
  return res.status(404).json({ message: 'Category not found' });
787
- const test = await Test.findByIdAndUpdate(testId, { $pull: { categories: { categoryId: category._id } } }, { new: true });
802
+ let test = await Test.findById(testId);
788
803
  if (!test)
789
804
  return res.status(404).json({ message: 'Test not found' });
805
+ if (req.entity?._id && test.entityId && !test.entityId.equals(req.entity._id)) {
806
+ return res.status(404).json({ message: 'Test not found' });
807
+ }
808
+ test = await Test.findByIdAndUpdate(testId, { $pull: { categories: { categoryId: category._id } } }, { new: true });
790
809
  res.status(200).json({ message: 'Category removed', test });
791
810
  }
792
811
  catch (err) {
@@ -839,6 +858,9 @@ class ExamsRouter extends EnduranceRouter {
839
858
  if (!test) {
840
859
  return res.status(404).json({ message: 'Test not found' });
841
860
  }
861
+ if (req.entity?._id && test.entityId && !test.entityId.equals(req.entity._id)) {
862
+ return res.status(404).json({ message: 'Test not found' });
863
+ }
842
864
  const categoryExists = test.categories.some(cat => cat.categoryId.equals(category._id));
843
865
  if (categoryExists) {
844
866
  return res.status(200).json({ message: 'Category already exists in the test' });
@@ -909,6 +931,9 @@ class ExamsRouter extends EnduranceRouter {
909
931
  if (!test) {
910
932
  return res.status(404).json({ message: 'Test not found' });
911
933
  }
934
+ if (req.entity?._id && test.entityId && !test.entityId.equals(req.entity._id)) {
935
+ return res.status(404).json({ message: 'Test not found' });
936
+ }
912
937
  const questions = [];
913
938
  for (const questionId of test.questions) {
914
939
  const question = await TestQuestion.findById(questionId);
@@ -959,6 +984,9 @@ class ExamsRouter extends EnduranceRouter {
959
984
  if (!test) {
960
985
  return res.status(404).json({ message: 'no test founded with this id' });
961
986
  }
987
+ if (req.entity?._id && test.entityId && !test.entityId.equals(req.entity._id)) {
988
+ return res.status(404).json({ message: 'no test founded with this id' });
989
+ }
962
990
  // Supprimer la question du tableau questions en filtrant par questionId
963
991
  test.questions = test.questions.filter(q => q.questionId.toString() !== questionId);
964
992
  // Recalculer les ordres pour que ça se suive
@@ -995,6 +1023,9 @@ class ExamsRouter extends EnduranceRouter {
995
1023
  if (!test) {
996
1024
  return res.status(404).json({ message: 'no test founded with this id' });
997
1025
  }
1026
+ if (req.entity?._id && test.entityId && !test.entityId.equals(req.entity._id)) {
1027
+ return res.status(404).json({ message: 'no test founded with this id' });
1028
+ }
998
1029
  for (const questionId of test.questions) {
999
1030
  await TestQuestion.findByIdAndDelete(questionId);
1000
1031
  }
@@ -1104,6 +1135,9 @@ class ExamsRouter extends EnduranceRouter {
1104
1135
  if (!test) {
1105
1136
  return res.status(404).json({ message: 'no test founded with this id' });
1106
1137
  }
1138
+ if (req.entity?._id && test.entityId && !test.entityId.equals(req.entity._id)) {
1139
+ return res.status(404).json({ message: 'no test founded with this id' });
1140
+ }
1107
1141
  const question = new TestQuestion({
1108
1142
  questionType,
1109
1143
  instruction,
@@ -1162,6 +1196,9 @@ class ExamsRouter extends EnduranceRouter {
1162
1196
  if (!test) {
1163
1197
  return res.status(404).json({ message: 'no test founded with this id' });
1164
1198
  }
1199
+ if (req.entity?._id && test.entityId && !test.entityId.equals(req.entity._id)) {
1200
+ return res.status(404).json({ message: 'no test founded with this id' });
1201
+ }
1165
1202
  const otherQuestionsIds = test.questions.map(question => question.questionId);
1166
1203
  const otherQuestions = await TestQuestion.find({ _id: { $in: otherQuestionsIds } });
1167
1204
  const jobName = await getJobName(test.targetJob);
@@ -1212,6 +1249,9 @@ class ExamsRouter extends EnduranceRouter {
1212
1249
  if (!test) {
1213
1250
  return res.status(404).json({ message: 'Test not found' });
1214
1251
  }
1252
+ if (req.entity?._id && test.entityId && !test.entityId.equals(req.entity._id)) {
1253
+ return res.status(404).json({ message: 'Test not found' });
1254
+ }
1215
1255
  for (let i = test.questions.length - 1; i > 0; i--) {
1216
1256
  const j = Math.floor(Math.random() * (i + 1));
1217
1257
  [test.questions[i], test.questions[j]] = [test.questions[j], test.questions[i]];
@@ -1262,6 +1302,9 @@ class ExamsRouter extends EnduranceRouter {
1262
1302
  if (!test) {
1263
1303
  return res.status(404).json({ message: 'no test founded with this id' });
1264
1304
  }
1305
+ if (req.entity?._id && test.entityId && !test.entityId.equals(req.entity._id)) {
1306
+ return res.status(404).json({ message: 'no test founded with this id' });
1307
+ }
1265
1308
  test.invitationText = invitationText;
1266
1309
  await test.save();
1267
1310
  res.status(200).json({
@@ -1856,6 +1899,9 @@ class ExamsRouter extends EnduranceRouter {
1856
1899
  if (!test) {
1857
1900
  return res.status(404).json({ message: 'Test non trouvé' });
1858
1901
  }
1902
+ if (req.entity?._id && test.entityId && !test.entityId.equals(req.entity._id)) {
1903
+ return res.status(404).json({ message: 'Test non trouvé' });
1904
+ }
1859
1905
  let categoriesToUse = [];
1860
1906
  if (category && category !== 'ALL') {
1861
1907
  const categoryInfo = test.categories.find(cat => cat.categoryId.toString() === category);
@@ -1988,6 +2034,9 @@ class ExamsRouter extends EnduranceRouter {
1988
2034
  if (!test) {
1989
2035
  return res.status(404).json({ message: 'Test non trouvé' });
1990
2036
  }
2037
+ if (req.entity?._id && test.entityId && !test.entityId.equals(req.entity._id)) {
2038
+ return res.status(404).json({ message: 'Test non trouvé' });
2039
+ }
1991
2040
  // Construction de la requête
1992
2041
  const query = { testId };
1993
2042
  if (state !== 'all') {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "@programisto/edrm-exams",
4
- "version": "0.3.9",
4
+ "version": "0.3.10",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },