Completed
Push — master ( a26a04...a5e25e )
by Emil
03:35
created

copier.copyAll   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 64
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2.0811

Importance

Changes 0
Metric Value
cc 2
eloc 46
nc 2
nop 1
dl 0
loc 64
ccs 8
cts 11
cp 0.7272
crap 2.0811
rs 8.7672
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1 1
const db = require("../db/database.js");
2 1
const products = require("./products.js");
3 1
const orders = require("./orders.js");
4
5
let config;
6
7 1
try {
8 1
    config = require('../../config/config.json');
9
} catch (error) {
10 1
    console.error(error);
11
}
12
13
14 1
const copier = {
15
    copyApiKey: process.env.COPY_API_KEY || config.copyApiKey,
16
17
    copyAll: function(res, apiKey) {
18 1
        let sql = "INSERT INTO products" +
19
            " (articleNumber," +
20
            " productName," +
21
            " productDescription," +
22
            " productSpecifiers," +
23
            " stock," +
24
            " location," +
25
            " price," +
26
            " apiKey)" +
27
            " SELECT articleNumber," +
28
            " productName," +
29
            " productDescription," +
30
            " productSpecifiers," +
31
            " stock," +
32
            " location, " +
33
            " price," +
34
            "'" + apiKey + "'" +
35
            " FROM products" +
36
            " WHERE apiKey = ?";
37
38 1
        db.run(sql,
39
            copier.copyApiKey,
40
            function (err) {
41 2
                if (err) {
42
                    return res.status(500).json({
43
                        errors: {
44
                            status: 500,
45
                            source: "/copy_products",
46
                            title: "Database error",
47
                            detail: err.message
48
                        }
49
                    });
50
                }
51
52 1
                db.all("SELECT * FROM " +
53
                    "(SELECT " + products.dataFields +
54
                    " FROM products WHERE apiKey = ?" +
55
                    " ORDER BY ROWID DESC LIMIT " +
56
                    parseInt(this.changes) + ")" +
57
                    " ORDER BY id ASC",
58
                apiKey,
59
                (err, rows) => {
60 2
                    if (err) {
61
                        return res.status(500).json({
62
                            errors: {
63
                                status: 500,
64
                                source: "/copy_products",
65
                                title: "Database error",
66
                                detail: err.message
67
                            }
68
                        });
69
                    }
70
71 1
                    let copiedProducts = rows;
72
73 1
                    let sql = "INSERT INTO orders" +
74
                        " (customerName," +
75
                        " customerAddress," +
76
                        " customerZip," +
77
                        " customerCity," +
78
                        " customerCountry," +
79
                        " statusId," +
80
                        " apiKey)" +
81
                        " SELECT customerName," +
82
                        " customerAddress," +
83
                        " customerZip," +
84
                        " customerCity," +
85
                        " customerCountry," +
86
                        " statusId," +
87
                        "'" + apiKey + "'" +
88
                        " FROM orders" +
89
                        " WHERE apiKey = ?";
90
91 1
                    db.run(sql,
92
                        copier.copyApiKey,
93
                        function (err) {
94 2
                            if (err) {
95
                                return res.status(500).json({
96
                                    errors: {
97
                                        status: 500,
98
                                        source: "/copy_orders",
99
                                        title: "Database error",
100
                                        detail: err.message
101
                                    }
102
                                });
103
                            }
104
105 1
                            db.all("SELECT * FROM " +
106
                                "(SELECT " + orders.dataFields +
107
                                " FROM orders o" +
108
                                " INNER JOIN status s ON s.id = o.statusId" +
109
                                " WHERE o.apiKey = ?" +
110
                                " ORDER BY o.ROWID DESC LIMIT " +
111
                                parseInt(this.changes) + ")" +
112
                                " ORDER BY id ASC",
113
                            apiKey,
114
                            (err, orderRows) => {
115 2
                                if (err) {
116
                                    return res.status(500).json({
117
                                        errors: {
118
                                            status: 500,
119
                                            source: "/orders",
120
                                            title: "Database error",
121
                                            detail: err.message
122
                                        }
123
                                    });
124
                                }
125
126 1
                                let copiedOrders = orderRows;
127
128 1
                                let oisql = " SELECT orderId," +
129
                                    " productId," +
130
                                    " amount " +
131
                                    " FROM order_items" +
132
                                    " WHERE apiKey = ?";
133
134 1
                                db.all(oisql,
135
                                    copier.copyApiKey,
136
                                    function (err, oiRows) {
137 2
                                        if (err) {
138
                                            return res.status(500).json({
139
                                                errors: {
140
                                                    status: 500,
141
                                                    source: "/copy_orders",
142
                                                    title: "Database error in order_items",
143
                                                    detail: err.message
144
                                                }
145
                                            });
146
                                        }
147
148 1
                                        return copier.organizeOrderItems(
149
                                            res,
150
                                            copiedProducts,
151
                                            copiedOrders,
152
                                            oiRows
153
                                        );
154
                                    });
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...
155
                            });
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...
156
                        });
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...
157
                });
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...
158
            });
159
    },
160
161
    organizeOrderItems: function(res, products, orders, orderItems) {
162 1
        orderItems.forEach(function(orderItem) {
163 5
            let originalOrderId = orderItem.orderId;
164
165 5
            orderItem.orderId = orders[orderItem.orderId - 1].id;
166 5
            orderItem.productId = products[orderItem.productId - 1].id;
167
168 5
            if (!orders[originalOrderId - 1].order_items) {
169 4
                orders[originalOrderId - 1].order_items = [];
170
            }
171
172 5
            orders[originalOrderId - 1].order_items.push(orderItem);
173
        });
174
175 1
        let copyResponse = {
176
            data: {
177
                products: products,
178
                orders: orders
179
            }
180
        };
181
182 1
        return res.status(201).json(copyResponse);
183
    },
184
185
    copyProducts: function(res, apiKey) {
186 2
        let sql = "INSERT INTO products" +
187
            " (articleNumber," +
188
            " productName," +
189
            " productDescription," +
190
            " productSpecifiers," +
191
            " stock," +
192
            " location," +
193
            " price," +
194
            " apiKey)" +
195
            " SELECT articleNumber," +
196
            " productName," +
197
            " productDescription," +
198
            " productSpecifiers," +
199
            " stock," +
200
            " location, " +
201
            " price," +
202
            " '" + apiKey + "'" +
203
            " FROM products" +
204
            " WHERE apiKey = ?";
205
206 2
        db.run(sql,
207
            copier.copyApiKey,
208
            function (err) {
209 2
                if (err) {
210
                    return res.status(500).json({
211
                        errors: {
212
                            status: 500,
213
                            source: "/copy_products",
214
                            title: "Database error",
215
                            detail: err.message
216
                        }
217
                    });
218
                }
219
220 2
                db.all("SELECT * FROM " +
221
                    "(SELECT " + products.dataFields +
222
                    " FROM products WHERE apiKey = ?" +
223
                    " ORDER BY ROWID DESC LIMIT " +
224
                    parseInt(this.changes) + ")" +
225
                    " ORDER BY id ASC",
226
                apiKey,
227
                (err, rows) => {
228 2
                    if (err) {
229
                        return res.status(500).json({
230
                            errors: {
231
                                status: 500,
232
                                source: "/copy_products",
233
                                title: "Database error",
234
                                detail: err.message
235
                            }
236
                        });
237
                    }
238
239 2
                    res.status(201).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...
240
                });
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...
241
            });
242
    }
243
};
244
245
module.exports = copier;
246