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

GrottoCenter / grottocenter-api / 10996210778

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

Pull #1306

github

vmarseguerra
feat(entities): adds delete / restore for document
Pull Request #1306: Adds delete / restore for entrance sub entities

740 of 2203 branches covered (33.59%)

Branch coverage included in aggregate %.

526 of 1293 new or added lines in 114 files covered. (40.68%)

23 existing lines in 18 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

64.41
/api/controllers/v1/document/update.js
1
const ControllerService = require('../../../services/ControllerService');
1✔
2
const DocumentService = require('../../../services/DocumentService');
1✔
3
const NotificationService = require('../../../services/NotificationService');
1✔
4
const FileService = require('../../../services/FileService');
1✔
5
const RightService = require('../../../services/RightService');
1✔
6
const { toDocument } = require('../../../services/mapping/converters');
1✔
7

8
const { INVALID_FORMAT, INVALID_NAME, ERROR_DURING_UPLOAD_TO_AZURE } =
9
  FileService;
1✔
10

11
module.exports = async (req, res) => {
1✔
12
  const document = await TDocument.findOne({ id: req.param('id') });
2✔
13
  if (!document || document.isDeleted)
2!
NEW
14
    return res.notFound(`Document not found`);
×
15

16
  if (document.modifiedDocJson) {
2✔
17
    const hasRight = RightService.hasGroup(
1✔
18
      req.token.groups,
19
      RightService.G.MODERATOR
20
    );
21

22
    if (!hasRight) {
1!
23
      return res.forbidden(
×
24
        `You are not authorized to update a document with modifications waiting a moderator approval.`
25
      );
26
    }
27
  }
28

29
  // Add new files
30
  const newFiles = [];
2✔
31
  if (req.files && req.files.files) {
2!
32
    const { files } = req.files;
×
33
    try {
×
34
      await Promise.all(
×
35
        files.map(async (file) => {
36
          const createdFile = await FileService.document.create(
×
37
            file,
38
            req.param('id'),
39
            true,
40
            false
41
          );
42
          newFiles.push(createdFile);
×
43
        })
44
      );
45
    } catch (err) {
46
      const { message, fileName } = err;
×
47
      switch (message) {
×
48
        case INVALID_FORMAT:
49
          return res.badRequest(
×
50
            `The format of the file "${fileName}" is invalid.`
51
          );
52
        case INVALID_NAME:
53
          return res.badRequest(
×
54
            `The name of the file "${fileName}" is invalid.`
55
          );
56
        case ERROR_DURING_UPLOAD_TO_AZURE:
57
        default:
58
          return res.serverError(
×
59
            'An error occured when trying to upload the file to Azure.'
60
          );
61
      }
62
    }
63
  }
64

65
  const authorId = req.token.id;
2✔
66

67
  // Update json data (upcoming modifications which need to be validated)
68
  const documentData = await DocumentService.getConvertedDataFromClient(
2✔
69
    req.body
70
  );
71
  const descriptionData = DocumentService.getDescriptionDataFromClient(
2✔
72
    req.body,
73
    authorId
74
  );
75

76
  const modifiedFiles = DocumentService.getChangedFileFromClient(
2✔
77
    req.body.modifiedFiles ?? []
4✔
78
  );
79

80
  const deletedFiles = DocumentService.getChangedFileFromClient(
2✔
81
    req.body.deletedFiles ?? []
4✔
82
  );
83

84
  const updatedDocument = await DocumentService.updateDocument({
2✔
85
    documentId: req.param('id'),
86
    reviewerId: authorId,
87
    documentData,
88
    descriptionData,
89
    newFiles,
90
    modifiedFiles,
91
    deletedFiles,
92
  });
93
  if (!updatedDocument) return res.notFound();
2!
94

95
  // The returned document does not include the modifications as they have to be validated before being applied
96
  const doc = await DocumentService.getPopulatedDocument(updatedDocument.id);
2✔
97

98
  await NotificationService.notifySubscribers(
2✔
99
    req,
100
    doc,
101
    authorId,
102
    NotificationService.NOTIFICATION_TYPES.UPDATE,
103
    NotificationService.NOTIFICATION_ENTITIES.DOCUMENT
104
  );
105

106
  return ControllerService.treatAndConvert(
2✔
107
    req,
108
    null,
109
    doc,
110
    { controllerMethod: 'DocumentController.update' },
111
    res,
112
    toDocument
113
  );
114
};
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