v1/models/deliveries.js   A
last analyzed

Complexity

Total Complexity 7
Complexity/F 1.4

Size

Lines of Code 55
Function Count 5

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
wmc 7
eloc 38
mnd 2
bc 2
fnc 5
dl 0
loc 55
ccs 12
cts 13
cp 0.9231
rs 10
bpm 0.4
cpm 1.4
noi 4
c 0
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A deliveries.js ➔ addDelivery 0 25 3
A deliveries.js ➔ getDeliveries 0 20 4
1 1
const db = require("../db/database.js");
2
3 1
module.exports = (function () {
4
    function getDeliveries(res, apiKey) {
5 2
        const sql = "SELECT deliveryId as id, productId as product_id, amount," +
6
                        " deliveryDate as delivery_date, comment" +
7
                        " FROM deliveries WHERE apiKey = ?";
8
9 2
        db.all(sql, apiKey, (err, rows) => {
10 2
            if (err) {
11
                return res.status(500).json({
12
                    errors: {
13
                        status: 500,
14
                        source: "/deliveries",
15
                        title: "Database error",
16
                        detail: err.message
17
                    }
18
                });
19
            } 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...
20 2
                res.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...
21
            }
22
        });
23
    }
24
25
    function addDelivery(res, body) {
26 6
        const sql = "INSERT INTO deliveries (deliveryId, productId, amount, deliveryDate," +
27
                        " comment, apiKey) VALUES (?, ?, ?, ?, ?, ?)";
28
29 6
        db.run(sql,
30
            body.id,
31
            body.product_id,
32
            body.amount,
33
            body.delivery_date,
34
            body.comment,
35
            body.api_key, (err) => {
36 6
                if (err) {
37 5
                    return res.status(500).json({
38
                        errors: {
39
                            status: 500,
40
                            source: "/delivery",
41
                            title: "Database error",
42
                            detail: err.message
43
                        }
44
                    });
45
                } 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...
46 1
                    res.status(201).json({ data: body });
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...
47
                }
48
            });
49
    }
50
51 1
    return {
52
        getDeliveries: getDeliveries,
53
        addDelivery: addDelivery,
54
    };
55
}());
56