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

GrottoCenter / grottocenter-api / 5631056675

pending completion
5631056675

push

github

vmarseguerra
refactor(right): replaces rights with a static groups system

751 of 1944 branches covered (38.63%)

Branch coverage included in aggregate %.

59 of 59 new or added lines in 35 files covered. (100.0%)

2702 of 5007 relevant lines covered (53.96%)

14.64 hits per line

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

13.95
/api/controllers/v1/document/import-rows.js
1
const DocumentService = require('../../../services/DocumentService');
1✔
2
const DocumentCSVImportService = require('../../../services/DocumentCSVImportService');
1✔
3
const DocumentDuplicateService = require('../../../services/DocumentDuplicateService');
1✔
4
const RightService = require('../../../services/RightService');
1✔
5

6
const { doubleCheck } = sails.helpers.csvhelpers;
1✔
7

8
module.exports = async (req, res) => {
1✔
9
  const hasRight = RightService.hasGroup(
×
10
    req.token.groups,
11
    RightService.G.ADMINISTRATOR
12
  );
13
  if (!hasRight) {
×
14
    return res.forbidden('You are not authorized to import documents via CSV.');
×
15
  }
16

17
  const requestResponse = {
×
18
    type: 'document',
19
    total: {
20
      success: 0,
21
      failure: 0,
22
    },
23
    successfulImport: [],
24
    successfulImportAsDuplicates: [],
25
    failureImport: [],
26
  };
27

28
  for (const [index, data] of req.body.data.entries()) {
×
29
    // eslint-disable-next-line no-await-in-loop
30
    const missingColumns = await sails.helpers.csvhelpers.checkColumns.with({
×
31
      data,
32
    });
33
    // Stop if missing columnes
34
    if (missingColumns.length > 0) {
×
35
      requestResponse.failureImport.push({
×
36
        line: index + 2,
37
        message: `Columns missing : ${missingColumns.toString()}`,
38
      });
39
      continue; // eslint-disable-line no-continue
×
40
    }
41

42
    // Check for duplicates
43
    const idDb = doubleCheck.with({
×
44
      data,
45
      key: 'id',
46
    });
47
    const nameDb = doubleCheck.with({
×
48
      data,
49
      key: 'dct:rights/cc:attributionName',
50
    });
51

52
    // eslint-disable-next-line no-await-in-loop
53
    const result = await TDocument.find({
×
54
      idDbImport: idDb,
55
      nameDbImport: nameDb,
56
      isDeleted: false,
57
    });
58

59
    // Data formatting
60
    // Author retrieval : create one if not present in db
61
    // eslint-disable-next-line no-await-in-loop
62
    const authorId = await sails.helpers.csvhelpers.getAuthor.with({
×
63
      data,
64
    });
65
    try {
×
66
      const dataDocument =
67
        // eslint-disable-next-line no-await-in-loop
68
        await DocumentCSVImportService.getConvertedDocumentFromCsv(
×
69
          req,
70
          data,
71
          authorId
72
        );
73
      const dataLangDesc =
74
        DocumentCSVImportService.getConvertedLangDescDocumentFromCsv(
×
75
          data,
76
          authorId
77
        );
78

79
      if (result.length !== 0) {
×
80
        // Create a duplicate in DB
81
        const duplicateContent = {
×
82
          document: dataDocument,
83
          description: dataLangDesc,
84
        };
85
        // eslint-disable-next-line no-await-in-loop
86
        await DocumentDuplicateService.create(
×
87
          req.token.id,
88
          duplicateContent,
89
          result[0].id
90
        );
91
        requestResponse.successfulImportAsDuplicates.push({
×
92
          line: index + 2,
93
          message: `Document with id ${idDb} has been created as a document duplicate.`,
94
        });
95
        continue; // eslint-disable-line no-continue
×
96
      }
97

98
      // eslint-disable-next-line no-await-in-loop
99
      const createdDocument = await DocumentService.createDocument(
×
100
        req,
101
        dataDocument,
102
        dataLangDesc,
103
        true
104
      );
105
      // eslint-disable-next-line no-await-in-loop
106
      const docFiles = await TFile.find({ document: createdDocument.id });
×
107
      requestResponse.successfulImport.push({
×
108
        documentId: createdDocument.id,
109
        title: dataLangDesc.title,
110
        filesImported: docFiles.map((f) => f.fileName).join(','),
×
111
      });
112
    } catch (err) {
113
      sails.log.error(err);
×
114
      requestResponse.failureImport.push({
×
115
        line: index + 2,
116
        message: err.toString(),
117
      });
118
    }
119
  }
120

121
  requestResponse.total.success = requestResponse.successfulImport.length;
×
122
  requestResponse.total.successfulImportAsDuplicates =
×
123
    requestResponse.successfulImportAsDuplicates.length;
124
  requestResponse.total.failure = requestResponse.failureImport.length;
×
125
  return res.ok(requestResponse);
×
126
};
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