Test Failed
Push — master ( 853a48...344ee9 )
by Emil
02:45
created

v1/models/copier.js   A

Complexity

Total Complexity 16
Complexity/F 1.6

Size

Lines of Code 218
Function Count 10

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 0
eloc 175
nc 1
dl 0
loc 218
rs 10
c 0
b 0
f 0
wmc 16
mnd 3
bc 22
fnc 10
bpm 2.2
cpm 1.6
noi 11

4 Functions

Rating   Name   Duplication   Size   Complexity  
B copier.js ➔ copyAll 0 100 1
A copier.js ➔ copyProducts 0 38 1
B copier.js ➔ copyOrders 0 61 1
B copier.constructor 0 211 1
1
const db = require("../db/database.js");
2
const products = require("./products.js");
3
const orders = require("./orders.js");
4
const config = require('../../config/config.json');
5
6
const copier = (function () {
7
    const copyApiKey = config.copyApiKey;
8
9
    function copyAll(res, apiKey) {
10
        let sql = "INSERT INTO products" +
11
            " (productId," +
12
            " articleNumber," +
13
            " productName," +
14
            " productDescription," +
15
            " productSpecifiers," +
16
            " stock," +
17
            " location," +
18
            " price," +
19
            " apiKey)" +
20
            " SELECT productId," +
21
            " articleNumber," +
22
            " productName," +
23
            " productDescription," +
24
            " productSpecifiers," +
25
            " stock," +
26
            " location, " +
27
            " price," +
28
            "'" + apiKey + "'" +
29
            " FROM products" +
30
            " WHERE apiKey = ?";
31
32
        db.run(sql, copyApiKey, (err) => {
33
            if (err) {
34
                return res.status(500).json({
35
                    errors: {
36
                        status: 500,
37
                        source: "/copy_products",
38
                        title: "Database error",
39
                        detail: err.message
40
                    }
41
                });
42
            } 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...
43
                let sql = "INSERT INTO orders" +
44
                    " (orderId," +
45
                    " customerName," +
46
                    " customerAddress," +
47
                    " customerZip," +
48
                    " customerCity," +
49
                    " customerCountry," +
50
                    " statusId," +
51
                    " apiKey)" +
52
                    " SELECT orderId," +
53
                    " customerName," +
54
                    " customerAddress," +
55
                    " customerZip," +
56
                    " customerCity," +
57
                    " customerCountry," +
58
                    " statusId," +
59
                    "'" + apiKey + "'" +
60
                    " FROM orders" +
61
                    " WHERE apiKey = ?";
62
63
                db.run(sql, copyApiKey, (err) => {
64
                    if (err) {
65
                        return res.status(500).json({
66
                            errors: {
67
                                status: 500,
68
                                source: "/copy_orders",
69
                                title: "Database error",
70
                                detail: err.message
71
                            }
72
                        });
73
                    } 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...
74
                        let orderItemsSQL = "INSERT INTO order_items" +
75
                            " (orderId," +
76
                            " productId," +
77
                            " amount," +
78
                            " apiKey)" +
79
                            " SELECT orderId," +
80
                            " productId," +
81
                            " amount," +
82
                            "'" + apiKey + "'" +
83
                            " FROM order_items" +
84
                            " WHERE apiKey = ?";
85
86
                        db.run(orderItemsSQL, copyApiKey, (err) => {
87
                            if (err) {
88
                                return res.status(500).json({
89
                                    errors: {
90
                                        status: 500,
91
                                        source: "/copy_orders",
92
                                        title: "Database error in order_items",
93
                                        detail: err.message
94
                                    }
95
                                });
96
                            } 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...
97
                                return res.status(201).json({
98
                                    data: {
99
                                        message: "Products and orders have been copied"
100
                                    }
101
                                });
102
                            }
103
                        });
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...
104
                    }
105
                });
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...
106
            }
107
        });
108
    }
109
110
    function copyProducts(res, apiKey) {
111
        let sql = "INSERT INTO products" +
112
            " (productId," +
113
            " articleNumber," +
114
            " productName," +
115
            " productDescription," +
116
            " productSpecifiers," +
117
            " stock," +
118
            " location," +
119
            " price," +
120
            " apiKey)" +
121
            " SELECT productId," +
122
            " articleNumber," +
123
            " productName," +
124
            " productDescription," +
125
            " productSpecifiers," +
126
            " stock," +
127
            " location, " +
128
            " price," +
129
            "'" + apiKey + "'" +
130
            " FROM products" +
131
            " WHERE apiKey = ?";
132
133
        db.run(sql, copyApiKey, (err) => {
134
            if (err) {
135
                return res.status(500).json({
136
                    errors: {
137
                        status: 500,
138
                        source: "/copy_products",
139
                        title: "Database error",
140
                        detail: err.message
141
                    }
142
                });
143
            } 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...
144
                products.getAllProducts(res, apiKey, 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...
145
            }
146
        });
147
    }
148
149
    function copyOrders(res, apiKey) {
150
        let sql = "INSERT INTO orders" +
151
            " (orderId," +
152
            " customerName," +
153
            " customerAddress," +
154
            " customerZip," +
155
            " customerCity," +
156
            " customerCountry," +
157
            " statusId," +
158
            " apiKey)" +
159
            " SELECT orderId," +
160
            " customerName," +
161
            " customerAddress," +
162
            " customerZip," +
163
            " customerCity," +
164
            " customerCountry," +
165
            " statusId," +
166
            "'" + apiKey + "'" +
167
            " FROM orders" +
168
            " WHERE apiKey = ?";
169
170
        db.run(sql, copyApiKey, (err) => {
171
            if (err) {
172
                return res.status(500).json({
173
                    errors: {
174
                        status: 500,
175
                        source: "/copy_orders",
176
                        title: "Database error",
177
                        detail: err.message
178
                    }
179
                });
180
            } 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...
181
                let orderItemsSQL = "INSERT INTO order_items" +
182
                    " (orderId," +
183
                    " productId," +
184
                    " amount," +
185
                    " apiKey)" +
186
                    " SELECT orderId," +
187
                    " productId," +
188
                    " amount," +
189
                    "'" + apiKey + "'" +
190
                    " FROM order_items" +
191
                    " WHERE apiKey = ?";
192
193
                db.run(orderItemsSQL, copyApiKey, (err) => {
194
                    if (err) {
195
                        return res.status(500).json({
196
                            errors: {
197
                                status: 500,
198
                                source: "/copy_orders",
199
                                title: "Database error in order_items",
200
                                detail: err.message
201
                            }
202
                        });
203
                    } 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...
204
                        orders.getAllOrders(res, apiKey, 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...
205
                    }
206
                });
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...
207
            }
208
        });
209
    }
210
211
    return {
212
        copyAll: copyAll,
213
        copyProducts: copyProducts,
214
        copyOrders: copyOrders,
215
    };
216
}());
217
218
module.exports = copier;
219