-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrest_api.js
238 lines (203 loc) · 5.71 KB
/
rest_api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
'use strict';
var Promise = require("bluebird");
const SerialWrapper = require('./serial_wrapper.js');
const API = require('./api.js');
const express = require('express')
const bodyParser = require('body-parser')
const Lock = require('./lock');
const app = express()
const host = '127.0.0.1';
const port = 3443
var debug = false;
var wrapper = new SerialWrapper('/dev/tty.usbserial-AL0517WZ', {}, debug);
var lock = new Lock();
var api = new API(wrapper, lock);
app.use(bodyParser.urlencoded({ extended: false }))
//this is used to catch all exceptions and return execution error and 503
//status code
const asyncMiddleware = fn => (req, res, next) => {
Promise.resolve(fn(req, res, next))
.catch(function(e) {
//release API lock if any!!
lock.release();
//console.log(e);
res.status(503);
res.send({status: 'execution_error'});
});
};
//app.post('/play', function(req, res) {
// api.playMusic().then(function() {
// res.send({status: 'ok'});
// });
//});
app.post('/play', asyncMiddleware(async (req, res) => {
var data = await api.playMusic();
res.send({'status': 'ok'});
}));
//app.post('/play', async function(req, res, next) {
// try {
// res.send({status: 'ok'});
// } catch(e) {
// res.send({status: 'execution_error'});
// }
//});
app.post('/createSale', asyncMiddleware(async (req, res) => {
if (!req.body.nSale || !req.body.amount) {
res.status(503);
res.send({msg: 'NSale or amount is empty!'});
return;
}
var lines = [];
if (req.body.lines) {
lines = req.body.lines.split(';');
}
var stornoData = {
'docType': null,
'docNumber': null,
'docDateTime': null,
'fmNumber': null,
};
if (req.body.stornoDataDocType) {
stornoData.docType = req.body.stornoDataDocType;
}
if (req.body.stornoDataDocNumber) {
stornoData.docNumber = req.body.stornoDataDocNumber;
}
if (req.body.stornoDataDocDateTime) {
stornoData.docDateTime = req.body.stornoDataDocDateTime;
}
if (req.body.stornoDataFmNumber) {
stornoData.fmNumber = req.body.stornoDataFmNumber;
}
var params = {
NSale: req.body.nSale,
Amount: req.body.amount,
lines: lines,
PaidMode: req.body.paidMode,
storno: req.body.storno,
stornoData: stornoData,
};
//console.log(params);
let saleData = await api.createSale(params);
res.send(saleData);
//api.createSale(params).then(function(data) {
// console.log(data);
// res.send(data);
//}).catch(function() {
// res.status(503);
// res.send({msg: 'Error'});
//});
}));
app.get('/diagnostic_info', asyncMiddleware(async (req, res) => {
let data = await api.getDiagnosticInfo();
//console.log(data);
res.send({
'status': 'ok',
'data': data,
});
//api.createSale(params).then(function(data) {
// console.log(data);
// res.send(data);
//}).catch(function() {
// res.status(503);
// res.send({msg: 'Error'});
//});
}));
app.get('/run_daily_financial_report', asyncMiddleware(async (req, res) => {
var withNulling = true;
if (req.body.withNulling == '0') {
withNulling = false;
}
console.log('Run daily financial report');
console.log('With nulling: ' + withNulling);
let data = await api.runDailyFinancialReport(withNulling);
res.send({
'status': 'ok',
'data': data,
});
}));
app.get('/run_period_financial_report', asyncMiddleware(async (req, res) => {
var start = req.body.start;
var end = null;
if (req.body.end) {
end = req.body.end;
}
console.log('Run periodic financial report');
console.log('Start: ' + start);
console.log('End: ' + end);
let data = await api.runFinancialReportByDate(start, end);
res.send({
'status': 'ok',
'data': data,
});
}));
//app.post('/old.createSale', function(req, res) {
// //var nSale = req.body.nSale;
// //var amount = req.body.amount;
//
// //var paidMode = null;
// //if (req.body.paidMode) {
// // paidMode = req.body.paidMode;
// //}
//
// if (!req.body.nSale || !req.body.amount) {
// res.status(503);
// res.send({msg: 'NSale or amount is empty!'});
// return;
// }
//
// var lines = [];
// if (req.body.lines) {
// lines = req.body.lines.split(';');
// }
//
// var stornoData = {
// 'docType': null,
// 'docNumber': null,
// 'docDateTime': null,
// 'fmNumber': null,
// };
//
// if (req.body.stornoDataDocType) {
// stornoData.docType = req.body.stornoDataDocType;
// }
//
// if (req.body.stornoDataDocNumber) {
// stornoData.docNumber = req.body.stornoDataDocNumber;
// }
//
// if (req.body.stornoDataDocDateTime) {
// stornoData.docDateTime = req.body.stornoDataDocDateTime;
// }
//
// if (req.body.stornoDataFmNumber) {
// stornoData.fmNumber = req.body.stornoDataFmNumber;
// }
//
// //var params = {
// // NSale: nSale,
// // Amount: amount,
// // lines: lines,
// // PaidMode: paidMode,
// //};
//
// var params = {
// NSale: req.body.nSale,
// Amount: req.body.amount,
// lines: lines,
// PaidMode: req.body.paidMode,
// storno: req.body.storno,
// stornoData: stornoData,
// };
//
// api.createSale(params).then(function(data) {
// console.log(data);
// res.send(data);
// }).catch(function() {
// res.status(503);
// res.send({msg: 'Error'});
// });
//});
app.listen(port, host, function() {
console.log(`Listening on port ${host}:${port}`);
});