v2/models/invoices.js   A
last analyzed

Complexity

Total Complexity 21
Complexity/F 2.1

Size

Lines of Code 176
Function Count 10

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 85.37%

Importance

Changes 0
Metric Value
wmc 21
eloc 118
mnd 11
bc 11
fnc 10
dl 0
loc 176
ccs 35
cts 41
cp 0.8537
rs 10
bpm 1.1
cpm 2.1
noi 5
c 0
b 0
f 0
1 1
const db = require("../db/database.js");
2
3 1
const invoices = {
4
    allowedFields: {
5
        creation_date: "creationDate",
6
        due_date: "dueDate",
7
        total_price: "totalPrice",
8
        order_id: "orderId",
9
    },
10
11
    dataFields: "i.ROWID as id, i.creationDate as creation_date," +
12
        " i.dueDate as due_date, o.ROWID as order_id," +
13
        " o.customerName as name, o.customerAddress as address," +
14
        " o.customerZip as zip, o.customerCity as city," +
15
        " o.customerCountry as country, (i.totalPrice / 100) as total_price",
16
17
    getInvoices: function(res, apiKey) {
18 3
        db.all("SELECT " + invoices.dataFields +
19
            " FROM invoices i" +
20
            " INNER JOIN orders o ON o.ROWID = i.orderId AND o.apiKey = i.apiKey" +
21
            " WHERE i.apiKey = ?",
22
        apiKey, (err, rows) => {
23 3
            if (err) {
24
                return res.status(500).json({
25
                    errors: {
26
                        status: 500,
27
                        source: "/invoices",
28
                        title: "Database error",
29
                        detail: err.message
30
                    }
31
                });
32
            }
33
34 3
            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...
35
        });
36
    },
37
38
    getInvoice: function(res, apiKey, invoiceId, status=200) {
39 10
        if (Number.isInteger(parseInt(invoiceId))) {
40 9
            db.get("SELECT " + invoices.dataFields +
41
                    " FROM invoices i" +
42
                    " INNER JOIN orders o ON o.ROWID = i.orderId AND o.apiKey = i.apiKey" +
43
                    " WHERE i.apiKey = ? AND i.ROWID = ?",
44
            apiKey,
45
            invoiceId,
46
            function(err, row) {
47 9
                if (err) {
48
                    return res.status(500).json({
49
                        errors: {
50
                            status: 500,
51
                            source: "/invoice/" + invoiceId,
52
                            title: "Database error",
53
                            detail: err.message
54
                        }
55
                    });
56
                }
57
58 9
                res.status(status).json( { data: row } );
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...
59
            });
60
        } else {
61 1
            res.status(400).json({
62
                errors: {
63
                    status: 400,
64
                    detail: "Required attribute invoice id " +
65
                        " is not an integer."
66
                }
67
            });
68
        }
69
    },
70
71
    addInvoice: function(res, body) {
72 7
        db.run("INSERT INTO invoices (orderId, totalPrice, creationDate, dueDate, apiKey)" +
73
            " VALUES (?, ?, ?, ?, ?)",
74
        body.order_id,
75
        body.total_price * 100,
76
        body.creation_date,
77
        body.due_date,
78
        body.api_key,
79
        function (err) {
80 7
            if (err) {
81 2
                return res.status(500).json({
82
                    errors: {
83
                        status: 500,
84
                        source: "POST /invoice",
85
                        title: "Database error",
86
                        detail: err.message
87
                    }
88
                });
89
            }
90
91 5
            invoices.getInvoice(res, body.api_key, this.lastID, 201);
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...
92
        });
93
    },
94
95
    updateInvoice: function (res, body) {
96 2
        if (Number.isInteger(parseInt(body.id))) {
97 1
            let params = [];
98 1
            let sql = "UPDATE invoices SET ";
99 1
            let updateFields = [];
100
101 1
            for (const field in invoices.allowedFields) {
102 4
                if (body[field] !== undefined) {
103 3
                    updateFields.push(invoices.allowedFields[field] + " = ?");
104
105 3
                    if (field == "total_price") {
106 1
                        body[field] *= 100;
107
                    }
108
109 3
                    params.push(body[field]);
110
                }
111
            }
112
113 1
            sql += updateFields.join(", ");
114 1
            sql += " WHERE apiKey = ? AND ROWID = ?";
115
116 1
            params.push(body.api_key, body.id);
117
118 1
            db.run(
119
                sql,
120
                params,
121
                function(err) {
122 2
                    if (err) {
123
                        return res.status(500).json({
124
                            errors: {
125
                                status: 500,
126
                                source: "PUT /invoices",
127
                                title: "Database error",
128
                                detail: err.message
129
                            }
130
                        });
131
                    }
132
133 1
                    return res.status(204).send();
134
                });
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...
135
        } else {
136
            return res.status(400).json({
137
                errors: {
138
                    status: 400,
139
                    detail: "Required attribute invoice id (id) " +
140
                        " was not included in the request."
141
                }
142
            });
143
        }
144
    },
145
146
    deleteInvoice: function(res, body) {
147 2
        if (Number.isInteger(parseInt(body.id))) {
148 1
            db.run("DELETE FROM invoices WHERE apiKey = ? AND ROWID = ?",
149
                body.api_key,
150
                body.id, function (err) {
151 2
                    if (err) {
152
                        return res.status(500).json({
153
                            errors: {
154
                                status: 500,
155
                                source: "DELETE /invoices",
156
                                title: "Database error",
157
                                detail: err.message
158
                            }
159
                        });
160
                    }
161
162 1
                    return res.status(204).send();
163
                });
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...
164
        } else {
165
            return res.status(400).json({
166
                errors: {
167
                    status: 400,
168
                    detail: "Required attribute invoice id (id) " +
169
                        " was not included in the request."
170
                }
171
            });
172
        }
173
    },
174
};
175
176
module.exports = invoices;
177