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

GrottoCenter / grottocenter-api / 10996310862

23 Sep 2024 02:22PM UTC coverage: 46.158% (-2.8%) from 48.952%
10996310862

push

github

vmarseguerra
feat(entities): adds delete / restore for document

740 of 2203 branches covered (33.59%)

Branch coverage included in aggregate %.

24 of 153 new or added lines in 17 files covered. (15.69%)

484 existing lines in 49 files now uncovered.

2462 of 4734 relevant lines covered (52.01%)

4.5 hits per line

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

7.04
/api/controllers/v1/document/multiple-validate.js
1
const DocumentService = require('../../../services/DocumentService');
1✔
2
const FileService = require('../../../services/FileService');
1✔
3
const RightService = require('../../../services/RightService');
1✔
4
const NotificationService = require('../../../services/NotificationService');
1✔
5

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

20
async function validateAndUpdateDocument(
21
  document,
22
  validationComment,
23
  validationAuthor
24
) {
25
  const {
26
    reviewerId,
27
    documentData,
28
    descriptionData,
29
    modifiedFiles,
30
    deletedFiles,
31
    newFiles,
32
  } = document.modifiedDocJson;
×
33

34
  await sails.getDatastore().transaction(async (db) => {
×
35
    // Update associated data not handled by TDocument manually
36
    // Updated before the TDocument update so the last_change_document DB trigger will fetch the last updated name
37
    await TDescription.updateOne({ document: document.id })
×
38
      .set(descriptionData)
39
      .usingConnection(db);
40

41
    await TDocument.updateOne(document.id)
×
42
      .set({
43
        ...documentData,
44
        modifiedDocJson: null,
45
        dateReviewed: new Date(),
46
        reviewer: reviewerId,
47
        dateValidation: new Date(),
48
        isValidated: true,
49
        validationComment,
50
        validator: validationAuthor,
51
      })
52
      .usingConnection(db);
53

54
    const filePromises = [];
×
55
    // Files have already been created,
56
    // they just need to be linked to the document.
57
    if (newFiles) {
×
58
      filePromises.push(
×
59
        ...newFiles.map((f) => TFile.updateOne(f.id).set({ isValidated: true }))
×
60
      );
61
    }
62
    if (modifiedFiles) {
×
63
      filePromises.push(
×
64
        ...modifiedFiles.map((f) => FileService.document.update(f))
×
65
      );
66
    }
67

68
    if (deletedFiles) {
×
69
      filePromises.push(
×
70
        ...deletedFiles.map((f) => FileService.document.delete(f))
×
71
      );
72
    }
73
    await Promise.all(filePromises);
×
74
  });
75
}
76

77
async function updateESAndNotify(req, documentId, hasChange, userId) {
NEW
78
  const document = await DocumentService.getPopulatedDocument(documentId);
×
79

80
  if (hasChange) {
×
NEW
81
    await DocumentService.updateESDocument(document);
×
82
  } else {
NEW
83
    await DocumentService.createESDocument(document);
×
84
  }
85

86
  await NotificationService.notifySubscribers(
×
87
    req,
88
    document,
89
    userId,
90
    NotificationService.NOTIFICATION_TYPES.VALIDATE,
91
    NotificationService.NOTIFICATION_ENTITIES.DOCUMENT
92
  );
93
}
94

95
module.exports = async (req, res) => {
1✔
96
  const hasRight = RightService.hasGroup(
×
97
    req.token.groups,
98
    RightService.G.MODERATOR
99
  );
100
  if (!hasRight) {
×
101
    return res.forbidden(
×
102
      'You are not authorized to validate multiple documents.'
103
    );
104
  }
105

106
  const documentChanges = [];
×
107
  // Validate input
108
  for (const doc of req.param('documents') ?? []) {
×
109
    // Whether or not the pending changes are accepted or not
110
    const isValidated = doc.isValidated
×
111
      ? doc.isValidated.toLowerCase() !== 'false'
112
      : true;
113

114
    if (isValidated === false && !doc.validationComment) {
×
115
      return res.badRequest(
×
116
        `If the document with id ${doc.id} is refused, a comment must be provided.`
117
      );
118
    }
119

120
    documentChanges.push({
×
121
      id: doc.id,
122
      isValidated,
123
      validationComment: doc.validationComment,
124
    });
125
  }
126
  const documentIds = documentChanges.map((e) => e.id);
×
127
  const foundDocuments = await TDocument.find({ id: documentIds });
×
128

129
  for (const document of foundDocuments) {
×
130
    const change = documentChanges.find((d) => d.id === document.id);
×
131
    const isAModifiedDoc = !!document.modifiedDocJson;
×
132
    if (!change.isValidated) {
×
133
      // Validate it but do not update its fields (reject change)
134
      // eslint-disable-next-line no-await-in-loop
135
      await markDocumentValidated(
×
136
        document.id,
137
        change.validationComment,
138
        req.token.id
139
      );
140
      continue; // eslint-disable-line no-continue
×
141
    }
142

143
    if (isAModifiedDoc) {
×
144
      // eslint-disable-next-line no-await-in-loop
145
      await validateAndUpdateDocument(
×
146
        document,
147
        change.validationComment,
148
        req.token.id
149
      );
150
    } else {
151
      // Likely a document creation
152
      // eslint-disable-next-line no-await-in-loop
153
      await markDocumentValidated(
×
154
        document.id,
155
        change.validationComment,
156
        req.token.id
157
      );
158
    }
159

160
    // eslint-disable-next-line no-await-in-loop
161
    await updateESAndNotify(
×
162
      res,
163
      document.id,
164
      isAModifiedDoc,
165
      req.token.id
166
    ).catch((err) =>
167
      sails.log.error(
×
168
        'Document multiple validate updateESAndNotify error',
169
        document,
170
        err
171
      )
172
    );
173
  }
174

175
  return res.ok();
×
176
};
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