Test Failed
Push — master ( 59db98...3c8e23 )
by Emil
05:37 queued 01:35
created

v2/models/copier.js   A

Complexity

Total Complexity 7
Complexity/F 1.75

Size

Lines of Code 84
Function Count 4

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 90.48%

Importance

Changes 0
Metric Value
wmc 7
eloc 62
mnd 3
bc 3
fnc 4
dl 0
loc 84
ccs 19
cts 21
cp 0.9048
rs 10
bpm 0.75
cpm 1.75
noi 4
c 0
b 0
f 0
1 1
const db = require("../db/database.js");
2 1
const products = require("./products.js");
3 1
4
const copier = {
5
    copyApiKey: process.env.COPY_API_KEY,
6
7 1
    copyProducts: function(res, apiKey) {
8 1
        if (apiKey === copier.copyApiKey) {
9
            var err = { message: "Cannot copy that API-key." };
10 1
11
            return copier.errorResponse(res, "/v2/copier/reset", err);
12
        } else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
13
            let sql = "INSERT INTO products" +
14 1
                " (articleNumber," +
15
                " productName," +
16
                " productDescription," +
17
                " productSpecifiers," +
18 6
                " stock," +
19 1
                " location," +
20
                " price," +
21 1
                " apiKey)" +
22
                " SELECT articleNumber," +
23 5
                " productName," +
24
                " productDescription," +
25
                " productSpecifiers," +
26
                " stock," +
27
                " location, " +
28
                " price," +
29
                " '" + apiKey + "'" +
30
                " FROM products" +
31
                " WHERE apiKey = ?";
32
33
            db.run(sql,
34
                copier.copyApiKey,
35
                function (err) {
36
                    if (err) {
37
                        return res.status(500).json({
38
                            errors: {
39
                                status: 500,
40
                                source: "/copy_products",
41
                                title: "Database error",
42
                                detail: err.message
43 5
                            }
44
                        });
45
                    }
46 5
47
                    db.all("SELECT * FROM " +
48
                        "(SELECT " + products.dataFields +
49
                        " FROM products WHERE apiKey = ?" +
50
                        " ORDER BY ROWID DESC LIMIT " +
51
                        parseInt(this.changes) + ")" +
52
                        " ORDER BY id ASC",
53
                    apiKey,
54
                    (err, rows) => {
55
                        if (err) {
56
                            return res.status(500).json({
57 5
                                errors: {
58 5
                                    status: 500,
59
                                    source: "/copy_products",
60 5
                                    title: "Database error",
61
                                    detail: err.message
62
                                }
63
                            });
64
                        }
65
66
                        res.status(201).json( { data: rows } );
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
67
                    });
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
68 5
                });
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
69
        }
70
    },
71
72
    errorResponse: function(res, path, err) {
73
        return res.status(500).json({
74
            errors: {
75
                status: 500,
76
                source: path,
77
                title: "Database error",
78
                detail: err.message
79 5
            }
80
        });
81 5
    }
82
};
83
84
module.exports = copier;
85