Completed
Push — master ( 81f0bb...084cce )
by Emil
06:15 queued 01:10
created

v2/models/copier.js   A

Complexity

Total Complexity 19
Complexity/F 1.73

Size

Lines of Code 245
Function Count 11

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 84.78%

Importance

Changes 0
Metric Value
cc 0
eloc 181
nc 2
dl 0
loc 245
ccs 39
cts 46
cp 0.8478
crap 0
rs 10
c 0
b 0
f 0
wmc 19
mnd 1
bc 21
fnc 11
bpm 1.909
cpm 1.7272
noi 6
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 2
        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 2
        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 2
                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 2
                    let copiedProducts = rows;
72
73 2
                    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 2
                    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 2
                            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 2
                                let copiedOrders = orderRows;
127
128 2
                                let oisql = " SELECT orderId," +
129
                                    " productId," +
130
                                    " amount " +
131
                                    " FROM order_items" +
132
                                    " WHERE apiKey = ?";
133
134 2
                                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 2
                                        return copier.organizeOrderItems(
149
                                            res,
150
                                            apiKey,
151
                                            copiedProducts,
152
                                            copiedOrders,
153
                                            oiRows
154
                                        );
155
                                    });
156
                            });
157
                        });
158
                });
159
            });
160
    },
161
162
    organizeOrderItems: function(
163
        res,
164
        apiKey,
165
        copiedProducts,
166
        copiedOrders,
167
        orderItems
168
    ) {
169 2
        let sqlReadyOrderItems = [];
170
171 2
        orderItems.forEach(function(orderItem) {
172 10
            orderItem.orderId = copiedOrders[orderItem.orderId - 1].id;
173 10
            orderItem.productId = copiedProducts[orderItem.productId - 1].id;
174
175 10
            sqlReadyOrderItems.push(
176
                `(${orderItem.orderId}, ${orderItem.productId}, ${orderItem.amount}, '${apiKey}')`
177
            );
178
        });
179
180 2
        let sql = "INSERT INTO order_items" +
181
            " (orderId, productId, amount, apiKey)" +
182
            " VALUES " + sqlReadyOrderItems.join(", ");
183
184 2
        db.run(sql, (err) => {
185 2
            if (err) {
186
                return res.status(500).json({
187
                    errors: {
188
                        status: 500,
189
                        source: "/copy_all",
190
                        title: "Database error in insert order_items",
191
                        detail: err.message
192
                    }
193
                });
194
            }
195
196 2
            let copyResponse = {
197
                data: {
198
                    products: copiedProducts,
199
                    orders: []
200
                }
201
            };
202
203 2
            return orders.getAllOrders(res, apiKey, 201, copyResponse);
204
        });
205
    },
206
207
    copyProducts: function(res, apiKey) {
208 2
        let sql = "INSERT INTO products" +
209
            " (articleNumber," +
210
            " productName," +
211
            " productDescription," +
212
            " productSpecifiers," +
213
            " stock," +
214
            " location," +
215
            " price," +
216
            " apiKey)" +
217
            " SELECT articleNumber," +
218
            " productName," +
219
            " productDescription," +
220
            " productSpecifiers," +
221
            " stock," +
222
            " location, " +
223
            " price," +
224
            " '" + apiKey + "'" +
225
            " FROM products" +
226
            " WHERE apiKey = ?";
227
228 2
        db.run(sql,
229
            copier.copyApiKey,
230
            function (err) {
231 2
                if (err) {
232
                    return res.status(500).json({
233
                        errors: {
234
                            status: 500,
235
                            source: "/copy_products",
236
                            title: "Database error",
237
                            detail: err.message
238
                        }
239
                    });
240
                }
241
242 2
                db.all("SELECT * FROM " +
243
                    "(SELECT " + products.dataFields +
244
                    " FROM products WHERE apiKey = ?" +
245
                    " ORDER BY ROWID DESC LIMIT " +
246
                    parseInt(this.changes) + ")" +
247
                    " ORDER BY id ASC",
248
                apiKey,
249
                (err, rows) => {
250 2
                    if (err) {
251
                        return res.status(500).json({
252
                            errors: {
253
                                status: 500,
254
                                source: "/copy_products",
255
                                title: "Database error",
256
                                detail: err.message
257
                            }
258
                        });
259
                    }
260
261 2
                    res.status(201).json( { data: rows } );
262
                });
263
            });
264
    }
265
};
266
267
module.exports = copier;
268