@programisto/edrm-exams 0.3.10 → 0.3.11

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.
@@ -25,6 +25,23 @@ async function getJobName(targetJob) {
25
25
  }
26
26
  return 'Job inconnu';
27
27
  }
28
+ /** Entité par défaut : les tests sans entityId lui sont rattachés (slug programisto/progamisto ou isDefault). */
29
+ function isDefaultEntity(req) {
30
+ if (!req?.entity)
31
+ return false;
32
+ const slug = req.entity.slug;
33
+ return req.entity.isDefault === true || slug === 'programisto' || slug === 'progamisto';
34
+ }
35
+ /** Vérifie qu'un test appartient à l'entité courante (pour GET/UPDATE/DELETE). Pour l'entité par défaut, un test sans entityId est accepté. */
36
+ function testBelongsToEntity(test, req) {
37
+ if (!req?.entity?._id)
38
+ return true;
39
+ const testEid = test?.entityId?.toString?.() ?? (test?.entityId != null ? String(test.entityId) : '');
40
+ const reqEid = req.entity._id?.toString?.() ?? String(req.entity._id);
41
+ if (isDefaultEntity(req))
42
+ return testEid === reqEid || testEid === '';
43
+ return testEid === reqEid;
44
+ }
28
45
  // Fonction pour migrer automatiquement un test si nécessaire
29
46
  async function migrateTestIfNeeded(test) {
30
47
  if (typeof test.targetJob === 'string') {
@@ -452,7 +469,7 @@ class ExamsRouter extends EnduranceRouter {
452
469
  if (!test) {
453
470
  return res.status(404).json({ message: 'Test non trouvé' });
454
471
  }
455
- if (req.entity?._id && test.entityId && !test.entityId.equals(req.entity._id)) {
472
+ if (!testBelongsToEntity(test, req)) {
456
473
  return res.status(404).json({ message: 'Test non trouvé' });
457
474
  }
458
475
  if (title)
@@ -543,7 +560,7 @@ class ExamsRouter extends EnduranceRouter {
543
560
  if (!test) {
544
561
  return res.status(404).json({ message: 'Test not found' });
545
562
  }
546
- if (req.entity?._id && test.entityId && !test.entityId.equals(req.entity._id)) {
563
+ if (!testBelongsToEntity(test, req)) {
547
564
  return res.status(404).json({ message: 'Test not found' });
548
565
  }
549
566
  for (let i = 0; i < test.questions.length; i++) {
@@ -587,7 +604,7 @@ class ExamsRouter extends EnduranceRouter {
587
604
  return res.status(404).json({ message: 'no test founded with this id' });
588
605
  }
589
606
  // 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)) {
607
+ if (!testBelongsToEntity(test, req)) {
591
608
  return res.status(404).json({ message: 'no test founded with this id' });
592
609
  }
593
610
  // Migration automatique si nécessaire
@@ -677,9 +694,22 @@ class ExamsRouter extends EnduranceRouter {
677
694
  const sortOrder = req.query.sortOrder || 'desc';
678
695
  // Construction de la requête de recherche
679
696
  const query = {};
680
- // Filtre par entité (contexte multi-entités)
697
+ // Filtre par entité. Entité par défaut (programisto/progamisto ou isDefault) = tests sans entityId inclus.
681
698
  if (req.entity?._id) {
682
- query.entityId = req.entity._id;
699
+ if (isDefaultEntity(req)) {
700
+ query.$and = query.$and || [];
701
+ query.$and.push({
702
+ $or: [
703
+ { entityId: req.entity._id },
704
+ { entityId: req.entity._id.toString?.() ?? String(req.entity._id) },
705
+ { entityId: { $exists: false } },
706
+ { entityId: null }
707
+ ]
708
+ });
709
+ }
710
+ else {
711
+ query.entityId = req.entity._id;
712
+ }
683
713
  }
684
714
  // Filtres
685
715
  if (targetJob !== 'all') {
@@ -802,7 +832,7 @@ class ExamsRouter extends EnduranceRouter {
802
832
  let test = await Test.findById(testId);
803
833
  if (!test)
804
834
  return res.status(404).json({ message: 'Test not found' });
805
- if (req.entity?._id && test.entityId && !test.entityId.equals(req.entity._id)) {
835
+ if (!testBelongsToEntity(test, req)) {
806
836
  return res.status(404).json({ message: 'Test not found' });
807
837
  }
808
838
  test = await Test.findByIdAndUpdate(testId, { $pull: { categories: { categoryId: category._id } } }, { new: true });
@@ -858,7 +888,7 @@ class ExamsRouter extends EnduranceRouter {
858
888
  if (!test) {
859
889
  return res.status(404).json({ message: 'Test not found' });
860
890
  }
861
- if (req.entity?._id && test.entityId && !test.entityId.equals(req.entity._id)) {
891
+ if (!testBelongsToEntity(test, req)) {
862
892
  return res.status(404).json({ message: 'Test not found' });
863
893
  }
864
894
  const categoryExists = test.categories.some(cat => cat.categoryId.equals(category._id));
@@ -931,7 +961,7 @@ class ExamsRouter extends EnduranceRouter {
931
961
  if (!test) {
932
962
  return res.status(404).json({ message: 'Test not found' });
933
963
  }
934
- if (req.entity?._id && test.entityId && !test.entityId.equals(req.entity._id)) {
964
+ if (!testBelongsToEntity(test, req)) {
935
965
  return res.status(404).json({ message: 'Test not found' });
936
966
  }
937
967
  const questions = [];
@@ -984,7 +1014,7 @@ class ExamsRouter extends EnduranceRouter {
984
1014
  if (!test) {
985
1015
  return res.status(404).json({ message: 'no test founded with this id' });
986
1016
  }
987
- if (req.entity?._id && test.entityId && !test.entityId.equals(req.entity._id)) {
1017
+ if (!testBelongsToEntity(test, req)) {
988
1018
  return res.status(404).json({ message: 'no test founded with this id' });
989
1019
  }
990
1020
  // Supprimer la question du tableau questions en filtrant par questionId
@@ -1023,7 +1053,7 @@ class ExamsRouter extends EnduranceRouter {
1023
1053
  if (!test) {
1024
1054
  return res.status(404).json({ message: 'no test founded with this id' });
1025
1055
  }
1026
- if (req.entity?._id && test.entityId && !test.entityId.equals(req.entity._id)) {
1056
+ if (!testBelongsToEntity(test, req)) {
1027
1057
  return res.status(404).json({ message: 'no test founded with this id' });
1028
1058
  }
1029
1059
  for (const questionId of test.questions) {
@@ -1135,7 +1165,7 @@ class ExamsRouter extends EnduranceRouter {
1135
1165
  if (!test) {
1136
1166
  return res.status(404).json({ message: 'no test founded with this id' });
1137
1167
  }
1138
- if (req.entity?._id && test.entityId && !test.entityId.equals(req.entity._id)) {
1168
+ if (!testBelongsToEntity(test, req)) {
1139
1169
  return res.status(404).json({ message: 'no test founded with this id' });
1140
1170
  }
1141
1171
  const question = new TestQuestion({
@@ -1196,7 +1226,7 @@ class ExamsRouter extends EnduranceRouter {
1196
1226
  if (!test) {
1197
1227
  return res.status(404).json({ message: 'no test founded with this id' });
1198
1228
  }
1199
- if (req.entity?._id && test.entityId && !test.entityId.equals(req.entity._id)) {
1229
+ if (!testBelongsToEntity(test, req)) {
1200
1230
  return res.status(404).json({ message: 'no test founded with this id' });
1201
1231
  }
1202
1232
  const otherQuestionsIds = test.questions.map(question => question.questionId);
@@ -1249,7 +1279,7 @@ class ExamsRouter extends EnduranceRouter {
1249
1279
  if (!test) {
1250
1280
  return res.status(404).json({ message: 'Test not found' });
1251
1281
  }
1252
- if (req.entity?._id && test.entityId && !test.entityId.equals(req.entity._id)) {
1282
+ if (!testBelongsToEntity(test, req)) {
1253
1283
  return res.status(404).json({ message: 'Test not found' });
1254
1284
  }
1255
1285
  for (let i = test.questions.length - 1; i > 0; i--) {
@@ -1302,7 +1332,7 @@ class ExamsRouter extends EnduranceRouter {
1302
1332
  if (!test) {
1303
1333
  return res.status(404).json({ message: 'no test founded with this id' });
1304
1334
  }
1305
- if (req.entity?._id && test.entityId && !test.entityId.equals(req.entity._id)) {
1335
+ if (!testBelongsToEntity(test, req)) {
1306
1336
  return res.status(404).json({ message: 'no test founded with this id' });
1307
1337
  }
1308
1338
  test.invitationText = invitationText;
@@ -1899,7 +1929,7 @@ class ExamsRouter extends EnduranceRouter {
1899
1929
  if (!test) {
1900
1930
  return res.status(404).json({ message: 'Test non trouvé' });
1901
1931
  }
1902
- if (req.entity?._id && test.entityId && !test.entityId.equals(req.entity._id)) {
1932
+ if (!testBelongsToEntity(test, req)) {
1903
1933
  return res.status(404).json({ message: 'Test non trouvé' });
1904
1934
  }
1905
1935
  let categoriesToUse = [];
@@ -2034,7 +2064,7 @@ class ExamsRouter extends EnduranceRouter {
2034
2064
  if (!test) {
2035
2065
  return res.status(404).json({ message: 'Test non trouvé' });
2036
2066
  }
2037
- if (req.entity?._id && test.entityId && !test.entityId.equals(req.entity._id)) {
2067
+ if (!testBelongsToEntity(test, req)) {
2038
2068
  return res.status(404).json({ message: 'Test non trouvé' });
2039
2069
  }
2040
2070
  // Construction de la requête
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "@programisto/edrm-exams",
4
- "version": "0.3.10",
4
+ "version": "0.3.11",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },