v1/models/order_items.js   A
last analyzed

Complexity

Total Complexity 12
Complexity/F 1.71

Size

Lines of Code 97
Function Count 7

Duplication

Duplicated Lines 97
Ratio 100 %

Test Coverage

Coverage 89.47%

Importance

Changes 0
Metric Value
wmc 12
eloc 69
mnd 5
bc 5
fnc 7
dl 97
loc 97
ccs 17
cts 19
cp 0.8947
rs 10
bpm 0.7141
cpm 1.7142
noi 6
c 0
b 0
f 0

3 Functions

Rating   Name   Duplication   Size   Complexity  
A order_items.js ➔ updateOrderItem 34 34 4
A order_items.js ➔ addOrderItem 20 20 4
A order_items.js ➔ deleteOrderItem 30 30 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1 1 View Code Duplication
const db = require("../db/database.js");
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2
3 1
module.exports = (function () {
4
    function addOrderItem(res, body) {
5 4
        db.run("INSERT INTO order_items (orderId, productId, amount, apiKey) VALUES (?, ?, ?, ?)",
6
            body.order_id,
7
            body.product_id,
8
            body.amount,
9
            body.api_key, (err) => {
10 4
                if (err) {
11 3
                    return res.status(500).json({
12
                        errors: {
13
                            status: 500,
14
                            source: "POST /order_item",
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 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...
21
                }
22
            });
23
    }
24
25
    function updateOrderItem(res, body) {
26 4
        if (Number.isInteger(parseInt(body.order_id)) &&
27
            Number.isInteger(parseInt(body.product_id))) {
28 1
            db.run("UPDATE order_items SET orderId = ?, productId = ?, amount = ?" +
29
                " WHERE apiKey = ? AND orderId = ? AND productId = ?",
30
            body.order_id,
31
            body.product_id,
32
            body.amount,
33
            body.api_key,
34
            body.order_id,
35
            body.product_id, (err) => {
36 2
                if (err) {
37
                    return res.status(500).json({
38
                        errors: {
39
                            status: 500,
40
                            source: "PUT /order_item",
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(204).send();
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
        } else {
50 2
            res.status(400).json({
51
                errors: {
52
                    status: 400,
53
                    detail: "Required attributes order id and product id" +
54
                        " was not included in the request."
55
                }
56
            });
57
        }
58
    }
59
60
    function deleteOrderItem(res, body) {
61 4
        if (Number.isInteger(parseInt(body.order_id)) &&
62
            Number.isInteger(parseInt(body.product_id))) {
63 1
            db.run("DELETE FROM order_items WHERE apiKey = ? AND orderId = ? AND productId = ?",
64
                body.api_key,
65
                body.order_id,
66
                body.product_id, (err) => {
67 2
                    if (err) {
68
                        return res.status(500).json({
69
                            errors: {
70
                                status: 500,
71
                                source: "DELETE /order_item",
72
                                title: "Database error",
73
                                detail: err.message
74
                            }
75
                        });
76
                    } 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...
77 1
                        res.status(204).send();
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...
78
                    }
79
                });
80
        } else {
81 2
            res.status(400).json({
82
                errors: {
83
                    status: 400,
84
                    detail: "Required attributes order id and product id" +
85
                        " was not included in the request."
86
                }
87
            });
88
        }
89
    }
90
91 1
    return {
92
93
        addOrderItem: addOrderItem,
94
        updateOrderItem: updateOrderItem,
95
        deleteOrderItem: deleteOrderItem,
96
    };
97
}());
98