Completed
Push — master ( be3ba8...c43b12 )
by Emil
03:15
created

copier.copyAll   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 74
Code Lines 55

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 55
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 74
ccs 9
cts 12
cp 0.75
crap 2.0625
rs 8.4727

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, copier.copyApiKey, (err) => {
39 2
            if (err) {
40
                return res.status(500).json({
41
                    errors: {
42
                        status: 500,
43
                        source: "/copy_products",
44
                        title: "Database error",
45
                        detail: err.message
46
                    }
47
                });
48
            }
49
50 1
            let sql = "INSERT INTO orders" +
51
                " (customerName," +
52
                " customerAddress," +
53
                " customerZip," +
54
                " customerCity," +
55
                " customerCountry," +
56
                " statusId," +
57
                " apiKey)" +
58
                " SELECT customerName," +
59
                " customerAddress," +
60
                " customerZip," +
61
                " customerCity," +
62
                " customerCountry," +
63
                " statusId," +
64
                "'" + apiKey + "'" +
65
                " FROM orders" +
66
                " WHERE apiKey = ?";
67
68 1
            db.run(sql, copier.copyApiKey, (err) => {
69 2
                if (err) {
70
                    return res.status(500).json({
71
                        errors: {
72
                            status: 500,
73
                            source: "/copy_orders",
74
                            title: "Database error",
75
                            detail: err.message
76
                        }
77
                    });
78
                }
79
80 1
                let orderItemsSQL = "INSERT INTO order_items" +
81
                    " (orderId," +
82
                    " productId," +
83
                    " amount," +
84
                    " apiKey)" +
85
                    " SELECT orderId," +
86
                    " productId," +
87
                    " amount," +
88
                    "'" + apiKey + "'" +
89
                    " FROM order_items" +
90
                    " WHERE apiKey = ?";
91
92 1
                db.run(orderItemsSQL, copier.copyApiKey, (err) => {
93 2
                    if (err) {
94
                        return res.status(500).json({
95
                            errors: {
96
                                status: 500,
97
                                source: "/copy_orders",
98
                                title: "Database error in order_items",
99
                                detail: err.message
100
                            }
101
                        });
102
                    }
103
104 1
                    return res.status(201).json({
105
                        data: {
106
                            message: "Products and orders have been copied"
107
                        }
108
                    });
109
                });
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...
110
            });
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...
111
        });
112
    },
113
114
    copyProducts: function(res, apiKey) {
115 1
        let sql = "INSERT INTO products" +
116
            " (articleNumber," +
117
            " productName," +
118
            " productDescription," +
119
            " productSpecifiers," +
120
            " stock," +
121
            " location," +
122
            " price," +
123
            " apiKey)" +
124
            " SELECT articleNumber," +
125
            " productName," +
126
            " productDescription," +
127
            " productSpecifiers," +
128
            " stock," +
129
            " location, " +
130
            " price," +
131
            "'" + apiKey + "'" +
132
            " FROM products" +
133
            " WHERE apiKey = ?";
134
135 1
        db.run(sql, copier.copyApiKey, (err) => {
136 2
            if (err) {
137
                return res.status(500).json({
138
                    errors: {
139
                        status: 500,
140
                        source: "/copy_products",
141
                        title: "Database error",
142
                        detail: err.message
143
                    }
144
                });
145
            }
146
147 1
            return products.getAllProducts(res, apiKey, 201);
148
        });
149
    },
150
151
    copyOrders: function(res, apiKey) {
152 1
        let sql = "INSERT INTO orders" +
153
            " (customerName," +
154
            " customerAddress," +
155
            " customerZip," +
156
            " customerCity," +
157
            " customerCountry," +
158
            " statusId," +
159
            " apiKey)" +
160
            " SELECT customerName," +
161
            " customerAddress," +
162
            " customerZip," +
163
            " customerCity," +
164
            " customerCountry," +
165
            " statusId," +
166
            "'" + apiKey + "'" +
167
            " FROM orders" +
168
            " WHERE apiKey = ?";
169
170 1
        db.run(sql, copier.copyApiKey, (err) => {
171 2
            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
            }
181
182 1
            let orderItemsSQL = "INSERT INTO order_items" +
183
                " (orderId," +
184
                " productId," +
185
                " amount," +
186
                " apiKey)" +
187
                " SELECT orderId," +
188
                " productId," +
189
                " amount," +
190
                "'" + apiKey + "'" +
191
                " FROM order_items" +
192
                " WHERE apiKey = ?";
193
194 1
            db.run(orderItemsSQL, copier.copyApiKey, (err) => {
195 2
                if (err) {
196
                    return res.status(500).json({
197
                        errors: {
198
                            status: 500,
199
                            source: "/copy_orders",
200
                            title: "Database error in order_items",
201
                            detail: err.message
202
                        }
203
                    });
204
                }
205
206 1
                return orders.getAllOrders(res, apiKey, 201);
207
            });
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...
208
        });
209
    }
210
};
211
212
module.exports = copier;
213