Completed
Push — master ( 5dafee...6f490e )
by Emil
03:07
created

v2/models/invoices.js   A

Complexity

Total Complexity 17
Complexity/F 2.13

Size

Lines of Code 147
Function Count 8

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 88.57%

Importance

Changes 0
Metric Value
wmc 17
eloc 99
mnd 9
bc 9
fnc 8
dl 0
loc 147
ccs 31
cts 35
cp 0.8857
rs 10
bpm 1.125
cpm 2.125
noi 4
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 2
        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 2
            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 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...
35
        });
36
    },
37
38
    getInvoice: function(res, apiKey, invoiceId, status=200) {
39 9
        if (Number.isInteger(parseInt(invoiceId))) {
40 8
            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 8
                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 8
                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 4
                    updateFields.push(invoices.allowedFields[field] + " = ?");
104
105 4
                    if (field == "total_price") {
106 1
                        body[field] *= 100;
107
                    }
108
109 4
                    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
147
module.exports = invoices;
148