1
|
1 |
|
const db = require("../db/database.js"); |
2
|
|
|
|
3
|
1 |
|
const invoices = { |
4
|
|
|
allowedFields: { |
5
|
|
|
creation_date: "creationDate", |
6
|
|
|
due_date: "dueDate", |
7
|
|
|
total_price: "totalPrice", |
8
|
|
|
order_id: "orderId", |
9
|
|
|
}, |
10
|
|
|
|
11
|
|
|
dataFields: "i.ROWID as id, i.creationDate as creation_date," + |
12
|
|
|
" i.dueDate as due_date, o.ROWID as order_id," + |
13
|
|
|
" o.customerName as name, o.customerAddress as address," + |
14
|
|
|
" o.customerZip as zip, o.customerCity as city," + |
15
|
|
|
" o.customerCountry as country, (i.totalPrice / 100) as total_price", |
16
|
|
|
|
17
|
|
|
getInvoices: function(res, apiKey) { |
18
|
3 |
|
db.all("SELECT " + invoices.dataFields + |
19
|
|
|
" FROM invoices i" + |
20
|
|
|
" INNER JOIN orders o ON o.ROWID = i.orderId AND o.apiKey = i.apiKey" + |
21
|
|
|
" WHERE i.apiKey = ?", |
22
|
|
|
apiKey, (err, rows) => { |
23
|
3 |
|
if (err) { |
24
|
|
|
return res.status(500).json({ |
25
|
|
|
errors: { |
26
|
|
|
status: 500, |
27
|
|
|
source: "/invoices", |
28
|
|
|
title: "Database error", |
29
|
|
|
detail: err.message |
30
|
|
|
} |
31
|
|
|
}); |
32
|
|
|
} |
33
|
|
|
|
34
|
3 |
|
res.json( { data: rows } ); |
|
|
|
|
35
|
|
|
}); |
36
|
|
|
}, |
37
|
|
|
|
38
|
|
|
getInvoice: function(res, apiKey, invoiceId, status=200) { |
39
|
10 |
|
if (Number.isInteger(parseInt(invoiceId))) { |
40
|
9 |
|
db.get("SELECT " + invoices.dataFields + |
41
|
|
|
" FROM invoices i" + |
42
|
|
|
" INNER JOIN orders o ON o.ROWID = i.orderId AND o.apiKey = i.apiKey" + |
43
|
|
|
" WHERE i.apiKey = ? AND i.ROWID = ?", |
44
|
|
|
apiKey, |
45
|
|
|
invoiceId, |
46
|
|
|
function(err, row) { |
47
|
9 |
|
if (err) { |
48
|
|
|
return res.status(500).json({ |
49
|
|
|
errors: { |
50
|
|
|
status: 500, |
51
|
|
|
source: "/invoice/" + invoiceId, |
52
|
|
|
title: "Database error", |
53
|
|
|
detail: err.message |
54
|
|
|
} |
55
|
|
|
}); |
56
|
|
|
} |
57
|
|
|
|
58
|
9 |
|
res.status(status).json( { data: row } ); |
|
|
|
|
59
|
|
|
}); |
60
|
|
|
} else { |
61
|
1 |
|
res.status(400).json({ |
62
|
|
|
errors: { |
63
|
|
|
status: 400, |
64
|
|
|
detail: "Required attribute invoice id " + |
65
|
|
|
" is not an integer." |
66
|
|
|
} |
67
|
|
|
}); |
68
|
|
|
} |
69
|
|
|
}, |
70
|
|
|
|
71
|
|
|
addInvoice: function(res, body) { |
72
|
7 |
|
db.run("INSERT INTO invoices (orderId, totalPrice, creationDate, dueDate, apiKey)" + |
73
|
|
|
" VALUES (?, ?, ?, ?, ?)", |
74
|
|
|
body.order_id, |
75
|
|
|
body.total_price * 100, |
76
|
|
|
body.creation_date, |
77
|
|
|
body.due_date, |
78
|
|
|
body.api_key, |
79
|
|
|
function (err) { |
80
|
7 |
|
if (err) { |
81
|
2 |
|
return res.status(500).json({ |
82
|
|
|
errors: { |
83
|
|
|
status: 500, |
84
|
|
|
source: "POST /invoice", |
85
|
|
|
title: "Database error", |
86
|
|
|
detail: err.message |
87
|
|
|
} |
88
|
|
|
}); |
89
|
|
|
} |
90
|
|
|
|
91
|
5 |
|
invoices.getInvoice(res, body.api_key, this.lastID, 201); |
|
|
|
|
92
|
|
|
}); |
93
|
|
|
}, |
94
|
|
|
|
95
|
|
|
updateInvoice: function (res, body) { |
96
|
2 |
|
if (Number.isInteger(parseInt(body.id))) { |
97
|
1 |
|
let params = []; |
98
|
1 |
|
let sql = "UPDATE invoices SET "; |
99
|
1 |
|
let updateFields = []; |
100
|
|
|
|
101
|
1 |
|
for (const field in invoices.allowedFields) { |
102
|
4 |
|
if (body[field] !== undefined) { |
103
|
3 |
|
updateFields.push(invoices.allowedFields[field] + " = ?"); |
104
|
|
|
|
105
|
3 |
|
if (field == "total_price") { |
106
|
1 |
|
body[field] *= 100; |
107
|
|
|
} |
108
|
|
|
|
109
|
3 |
|
params.push(body[field]); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
1 |
|
sql += updateFields.join(", "); |
114
|
1 |
|
sql += " WHERE apiKey = ? AND ROWID = ?"; |
115
|
|
|
|
116
|
1 |
|
params.push(body.api_key, body.id); |
117
|
|
|
|
118
|
1 |
|
db.run( |
119
|
|
|
sql, |
120
|
|
|
params, |
121
|
|
|
function(err) { |
122
|
2 |
|
if (err) { |
123
|
|
|
return res.status(500).json({ |
124
|
|
|
errors: { |
125
|
|
|
status: 500, |
126
|
|
|
source: "PUT /invoices", |
127
|
|
|
title: "Database error", |
128
|
|
|
detail: err.message |
129
|
|
|
} |
130
|
|
|
}); |
131
|
|
|
} |
132
|
|
|
|
133
|
1 |
|
return res.status(204).send(); |
134
|
|
|
}); |
|
|
|
|
135
|
|
|
} else { |
136
|
|
|
return res.status(400).json({ |
137
|
|
|
errors: { |
138
|
|
|
status: 400, |
139
|
|
|
detail: "Required attribute invoice id (id) " + |
140
|
|
|
" was not included in the request." |
141
|
|
|
} |
142
|
|
|
}); |
143
|
|
|
} |
144
|
|
|
}, |
145
|
|
|
|
146
|
|
|
deleteInvoice: function(res, body) { |
147
|
2 |
|
if (Number.isInteger(parseInt(body.id))) { |
148
|
1 |
|
db.run("DELETE FROM invoices WHERE apiKey = ? AND ROWID = ?", |
149
|
|
|
body.api_key, |
150
|
|
|
body.id, function (err) { |
151
|
2 |
|
if (err) { |
152
|
|
|
return res.status(500).json({ |
153
|
|
|
errors: { |
154
|
|
|
status: 500, |
155
|
|
|
source: "DELETE /invoices", |
156
|
|
|
title: "Database error", |
157
|
|
|
detail: err.message |
158
|
|
|
} |
159
|
|
|
}); |
160
|
|
|
} |
161
|
|
|
|
162
|
1 |
|
return res.status(204).send(); |
163
|
|
|
}); |
|
|
|
|
164
|
|
|
} else { |
165
|
|
|
return res.status(400).json({ |
166
|
|
|
errors: { |
167
|
|
|
status: 400, |
168
|
|
|
detail: "Required attribute invoice id (id) " + |
169
|
|
|
" was not included in the request." |
170
|
|
|
} |
171
|
|
|
}); |
172
|
|
|
} |
173
|
|
|
}, |
174
|
|
|
}; |
175
|
|
|
|
176
|
|
|
module.exports = invoices; |
177
|
|
|
|