Total Complexity | 7 |
Complexity/F | 1.75 |
Lines of Code | 88 |
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 | " image_url," + |
|
22 | " category," + |
||
23 | 5 | " apiKey)" + |
|
24 | " SELECT articleNumber," + |
||
25 | " productName," + |
||
26 | " productDescription," + |
||
27 | " productSpecifiers," + |
||
28 | " stock," + |
||
29 | " location, " + |
||
30 | " price," + |
||
31 | " image_url," + |
||
32 | " category," + |
||
33 | " '" + apiKey + "'" + |
||
34 | " FROM products" + |
||
35 | " WHERE apiKey = ?"; |
||
36 | |||
37 | db.run(sql, |
||
38 | copier.copyApiKey, |
||
39 | function (err) { |
||
40 | if (err) { |
||
41 | return res.status(500).json({ |
||
42 | errors: { |
||
43 | 5 | status: 500, |
|
44 | source: "/copy_products", |
||
45 | title: "Database error", |
||
46 | 5 | detail: err.message |
|
47 | } |
||
48 | }); |
||
49 | } |
||
50 | |||
51 | db.all("SELECT * FROM " + |
||
52 | "(SELECT " + products.dataFields + |
||
53 | " FROM products WHERE apiKey = ?" + |
||
54 | " ORDER BY ROWID DESC LIMIT " + |
||
55 | parseInt(this.changes) + ")" + |
||
56 | " ORDER BY id ASC", |
||
57 | 5 | apiKey, |
|
58 | 5 | (err, rows) => { |
|
59 | if (err) { |
||
60 | 5 | return res.status(500).json({ |
|
61 | errors: { |
||
62 | status: 500, |
||
63 | source: "/copy_products", |
||
64 | title: "Database error", |
||
65 | detail: err.message |
||
66 | } |
||
67 | }); |
||
68 | 5 | } |
|
69 | |||
70 | res.status(201).json( { data: rows } ); |
||
71 | }); |
||
72 | }); |
||
73 | } |
||
74 | }, |
||
75 | |||
76 | errorResponse: function(res, path, err) { |
||
77 | return res.status(500).json({ |
||
78 | errors: { |
||
79 | 5 | status: 500, |
|
80 | source: path, |
||
81 | 5 | title: "Database error", |
|
82 | detail: err.message |
||
83 | } |
||
84 | }); |
||
85 | } |
||
86 | }; |
||
87 | |||
88 | module.exports = copier; |
||
89 |