• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

GrottoCenter / grottocenter-api / 5123216243

pending completion
5123216243

push

github

vmarseguerra
feat(entities): list latest contributions

774 of 2004 branches covered (38.62%)

Branch coverage included in aggregate %.

123 of 123 new or added lines in 11 files covered. (100.0%)

2775 of 5089 relevant lines covered (54.53%)

14.54 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

7.46
/api/controllers/v1/document/multiple-validate.js
1
const DocumentService = require('../../../services/DocumentService');
2
const FileService = require('../../../services/FileService');
3
const {
4
  NOTIFICATION_TYPES,
5
  NOTIFICATION_ENTITIES,
6
} = require('../../../services/NotificationService');
7
const NotificationService = require('../../../services/NotificationService');
8

9
async function markDocumentValidated(
10
  documentId,
11
  validationComment,
12
  validationAuthor
13
) {
14
  await TDocument.updateOne(documentId).set({
15
    isValidated: true,
16
    modifiedDocJson: null,
17
    dateValidation: new Date(),
18
    validationComment,
19
    validator: validationAuthor,
20
  });
21
}
22

23
async function validateAndUpdateDocument(
24
  document,
25
  validationComment,
26
  validationAuthor
27
) {
28
  const {
29
    documentMainLanguage,
30
    author,
31
    description,
32
    titleAndDescriptionLanguage,
33
    title,
34
    modifiedFiles,
35
    deletedFiles,
36
    newFiles,
37
    ...cleanedData
38
  } = document.modifiedDocJson;
39

40
  await sails.getDatastore().transaction(async (db) => {
41
    // Update associated data not handled by TDocument manually
42
    // Updated before the TDocument update so the last_change_document DB trigger will fetch the last updated name
43
    await TDescription.updateOne({ document: document.id })
44
      .set({
45
        author,
46
        body: description,
47
        document: document.id,
48
        language: titleAndDescriptionLanguage.id,
49
        title,
50
      })
51
      .usingConnection(db);
52

53
    await TDocument.updateOne(document.id)
54
      .set({
55
        ...cleanedData,
56
        // Currently, only one language per document is allowed
57
        ...(documentMainLanguage && {
×
58
          languages: documentMainLanguage.id,
59
        }),
60
        modifiedDocJson: null,
61
        dateValidation: new Date(),
62
        isValidated: true,
63
        validationComment,
64
        validator: validationAuthor,
65
      })
66
      .usingConnection(db);
67

68
    const filePromises = [];
69
    // Files have already been created,
70
    // they just need to be linked to the document.
71
    if (newFiles) {
×
72
      filePromises.push(
73
        ...newFiles.map((f) =>
74
          FileService.updateOne(f.id).set({ isValidated: true })
75
        )
76
      );
77
    }
78
    if (modifiedFiles) {
×
79
      filePromises.push(...modifiedFiles.map((f) => FileService.update(f)));
80
    }
81

82
    if (deletedFiles) {
×
83
      filePromises.push(...deletedFiles.map((f) => FileService.delete(f)));
84
    }
85
    await Promise.all(filePromises);
86
  });
87
}
88

89
async function updateESAndNotify(req, documentId, hasChange, userId) {
90
  const found = await TDocument.findOne(documentId)
91
    .populate('author')
92
    .populate('authorizationDocument')
93
    .populate('authors')
94
    .populate('cave')
95
    .populate('descriptions')
96
    .populate('editor')
97
    .populate('entrance')
98
    .populate('identifierType')
99
    .populate('languages')
100
    .populate('library')
101
    .populate('license')
102
    .populate('massif')
103
    .populate('option')
104
    .populate('parent')
105
    .populate('regions')
106
    .populate('reviewer')
107
    .populate('subjects')
108
    .populate('type');
109
  await DocumentService.setNamesOfPopulatedDocument(found);
110

111
  if (hasChange) {
×
112
    await DocumentService.updateDocumentInElasticSearchIndexes(found);
113
  } else {
114
    await DocumentService.addDocumentToElasticSearchIndexes(found);
115
  }
116

117
  await NotificationService.notifySubscribers(
118
    req,
119
    found,
120
    userId,
121
    NOTIFICATION_TYPES.VALIDATE,
122
    NOTIFICATION_ENTITIES.DOCUMENT
123
  );
124
}
125

126
module.exports = async (req, res) => {
127
  const documentChanges = [];
128
  // Validate input
129
  for (const doc of req.param('documents') ?? []) {
×
130
    const isValidated = doc.isValidated
×
131
      ? !(doc.isValidated.toLowerCase() === 'false')
132
      : true;
133

134
    if (isValidated === false && !doc.validationComment) {
×
135
      return res.badRequest(
136
        `If the document with id ${doc.id} is refused, a comment must be provided.`
137
      );
138
    }
139

140
    documentChanges.push({
141
      id: doc.id,
142
      isValidated,
143
      validationComment: doc.validationComment,
144
    });
145
  }
146
  const documentIds = documentChanges.map((e) => e.id);
147
  const foundDocuments = await TDocument.find({ id: documentIds });
148

149
  for (const document of foundDocuments) {
150
    const change = documentChanges.find((d) => d.id === document.id);
151
    const isAModifiedDoc = !!document.modifiedDocJson;
152
    if (!change.isValidated) {
×
153
      // Validate it but do not update its fields
154
      // eslint-disable-next-line no-await-in-loop
155
      await markDocumentValidated(
156
        document.id,
157
        change.validationComment,
158
        req.token.id
159
      );
160
      continue; // eslint-disable-line no-continue
161
    }
162

163
    if (isAModifiedDoc) {
×
164
      // eslint-disable-next-line no-await-in-loop
165
      await validateAndUpdateDocument(
166
        document,
167
        change.validationComment,
168
        req.token.id
169
      );
170
    } else {
171
      // Likely a document creation
172
      // eslint-disable-next-line no-await-in-loop
173
      await markDocumentValidated(
174
        document.id,
175
        change.validationComment,
176
        req.token.id
177
      );
178
    }
179

180
    // eslint-disable-next-line no-await-in-loop
181
    await updateESAndNotify(
182
      res,
183
      document.id,
184
      isAModifiedDoc,
185
      req.token.id
186
    ).catch((err) =>
187
      sails.log.error(
188
        'Document multiple validate updateESAndNotify error',
189
        document,
190
        err
191
      )
192
    );
193
  }
194

195
  return res.ok();
196
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc