Total Complexity | 7 |
Complexity/F | 1.75 |
Lines of Code | 84 |
Function Count | 4 |
Duplicated Lines | 0 |
Ratio | 0 % |
Coverage | 90.48% |
Changes | 0 |
1 | 1 | const db = require("../db/database.js"); |
|
2 | 1 | const products = require("./products.js"); |
|
3 | 1 | ||
4 | const copier = { |
||
5 | copyApiKey: process.env.COPY_API_KEY, |
||
6 | |||
7 | 1 | copyProducts: function(res, apiKey) { |
|
8 | 1 | if (apiKey === copier.copyApiKey) { |
|
9 | var err = { message: "Cannot copy that API-key." }; |
||
10 | 1 | ||
11 | return copier.errorResponse(res, "/v2/copier/reset", err); |
||
12 | } else { |
||
|
|||
13 | let sql = "INSERT INTO products" + |
||
14 | 1 | " (articleNumber," + |
|
15 | " productName," + |
||
16 | " productDescription," + |
||
17 | " productSpecifiers," + |
||
18 | 6 | " stock," + |
|
19 | 1 | " location," + |
|
20 | " price," + |
||
21 | 1 | " apiKey)" + |
|
22 | " SELECT articleNumber," + |
||
23 | 5 | " productName," + |
|
24 | " productDescription," + |
||
25 | " productSpecifiers," + |
||
26 | " stock," + |
||
27 | " location, " + |
||
28 | " price," + |
||
29 | " '" + apiKey + "'" + |
||
30 | " FROM products" + |
||
31 | " WHERE apiKey = ?"; |
||
32 | |||
33 | db.run(sql, |
||
34 | copier.copyApiKey, |
||
35 | function (err) { |
||
36 | if (err) { |
||
37 | return res.status(500).json({ |
||
38 | errors: { |
||
39 | status: 500, |
||
40 | source: "/copy_products", |
||
41 | title: "Database error", |
||
42 | detail: err.message |
||
43 | 5 | } |
|
44 | }); |
||
45 | } |
||
46 | 5 | ||
47 | db.all("SELECT * FROM " + |
||
48 | "(SELECT " + products.dataFields + |
||
49 | " FROM products WHERE apiKey = ?" + |
||
50 | " ORDER BY ROWID DESC LIMIT " + |
||
51 | parseInt(this.changes) + ")" + |
||
52 | " ORDER BY id ASC", |
||
53 | apiKey, |
||
54 | (err, rows) => { |
||
55 | if (err) { |
||
56 | return res.status(500).json({ |
||
57 | 5 | errors: { |
|
58 | 5 | status: 500, |
|
59 | source: "/copy_products", |
||
60 | 5 | title: "Database error", |
|
61 | detail: err.message |
||
62 | } |
||
63 | }); |
||
64 | } |
||
65 | |||
66 | res.status(201).json( { data: rows } ); |
||
67 | }); |
||
68 | 5 | }); |
|
69 | } |
||
70 | }, |
||
71 | |||
72 | errorResponse: function(res, path, err) { |
||
73 | return res.status(500).json({ |
||
74 | errors: { |
||
75 | status: 500, |
||
76 | source: path, |
||
77 | title: "Database error", |
||
78 | detail: err.message |
||
79 | 5 | } |
|
80 | }); |
||
81 | 5 | } |
|
82 | }; |
||
83 | |||
84 | module.exports = copier; |
||
85 |