-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
236 lines (204 loc) · 7.56 KB
/
server.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
const express = require('express');
const app = express();
const bodyParser = require("body-parser");
const sql = require("mssql");
const config = {
user: 'sa',
password: 'svcntt',
server: 'localhost',
database: 'DB_QLHD',
trustServerCertificate: true,
};
app.use(express.static('dist'));
//Here we are configuring express to use body-parser as middle-ware.
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.get('/', function (req, res) {
res.sendFile(__dirname + "/index.html");
})
Array.prototype.pushValues = function(arr) {
this.push.apply(this, arr);
};
function NoneEmpty(arr) {
for(var i = 0; i < arr.length; i++) {
if(arr[i].product_id === '' || arr[i].product_price === '' || arr[i].product_number === '') return false;
}
return true;
}
function checkCustomerID(customer_id) {
return new Promise((resolve, reject) => {
var sqlQuery = `SELECT * FROM KhachHang KH WHERE KH.MaKH = '${customer_id}'`;
const connection = new sql.Request();
connection.query(sqlQuery, (err, result) => {
if (err) return reject(err);
if (result.recordset.length === 0) {
return reject("Cannot insert! Wrong customer's ID!");
}
return resolve(true);
}
);
}).catch(err => {
});
}
function checkProductID(product) {
return new Promise((resolve, reject) => {
var sqlQuery = `SELECT * FROM SanPham SP WHERE SP.MaSP = '${product.product_id}'`
const request = new sql.Request();
request.query(sqlQuery, (err, result) => {
if (err) return reject(err);
if (result.recordset.length === 0) {
return reject("Cannot insert! Wrong product's ID!");
}
return resolve(true);
}
);
}).catch(err => {
});
}
function generateReceiptID(new_receipt_id) {
return new Promise((resolve, reject) => {
// Select the last row in table HoaDon
var sqlQuery = `SELECT TOP 1 MaHD FROM HoaDon ORDER BY MaHD DESC`
const request = new sql.Request();
request.query(sqlQuery, (err, result) => {
if (err) res.status(500).send(err);
// Create a new receipt ID
new_receipt_id.value = (parseInt(Object.values(result.recordset[0]), 10) + 1).toString();
// Check if the database is full
if (new_receipt_id.length > 6) {
return reject("Cannot insert! Database is full");
}
return resolve(true);
});
}).catch(err => {
});
}
function insertIntoHoaDon(new_receipt_id, customer_id, date) {
return new Promise((resolve, reject) => {
var sqlQuery = `INSERT INTO HoaDon VALUES ('${new_receipt_id.value}','${customer_id}', '${date}', NULL)`
const request = new sql.Request();
request.query(sqlQuery, (err, result) => {
if (err) return reject("Connection failed!");
//console.log(sqlQuery);
});
return resolve(true);
}).catch (err => {
});
}
function insertIntoCT_HoaDon(new_receipt_id, product_detail_list) {
return new Promise((resolve, reject) => {
var sqlQueries = ``;
for (let i = 0; i < product_detail_list.length; i++) {
sqlQueries += `INSERT INTO CT_HoaDon VALUES ('${new_receipt_id.value}', '${product_detail_list[i].product_id}',
${product_detail_list[i].product_number}, ${product_detail_list[i].product_price}, 0, NULL);`
}
const request = new sql.Request();
request.query(sqlQueries, (err, result) => {
if (err) return reject("Connection failed!");
//console.log(sqlQueries);
});
return resolve(true);
}).catch(err => {
});
}
app.post('/insert-receipt-post', async function (req, res) {
// Prepare output in JSON format
let response = {
customer_id:req.body.customer_id,
date: req.body.date, //yyyy-mm-dd
product_detail_list: JSON.parse(req.body.product_detail_list)
};
//TODO: validate response
//let isValid = true;
// Get customer ID and date
var customer_id = response.customer_id;
var date = response.date;
// Get all products
var product_detail_list = response.product_detail_list;
// Case 1: Insufficient information
if (customer_id === '' || date === '' || !NoneEmpty(product_detail_list)) {
res.send("Cannot insert! Some information is missing!");
return;
}
// Case 2: Wrong date (inputDate > today)
// Note: Wrong date format has already been checked via the front-end
var inputDate = new Date(date);
var today = new Date();
if (inputDate.setHours(0,0,0,0) > today.setHours(0,0,0,0)) {
res.send("Cannot insert! Invalid date!");
return;
}
// Case 3: Wrong customer_id
let isValid = await checkCustomerID(customer_id);
if (isValid !== true) {
res.send(`Error from checking customer id: ${customer_id}`);
return;
}
// Case 4: Wrong product_id
for (let i = 0; i < product_detail_list.length; i++) {
isValid = await checkProductID(product_detail_list[i]);
if (isValid !== true) {
res.send(`Error from checking product id: ${product_detail_list[i].product_id}`);
return;
}
}
// Case 5: Duplicate product_id
for (let i = 0; i < product_detail_list.length - 1; i++) {
for (let j = i+1; j < product_detail_list.length; j++) {
if (product_detail_list[i].product_id === product_detail_list[j].product_id) {
res.send(`Duplicate product id: ${product_detail_list[i].product_id}`);
return;
}
}
}
//TODO: link to DB and insert new row into table HoaDon and CT_HoaDon
new_receipt_id = {}
isValid = await generateReceiptID(new_receipt_id);
if (isValid !== true) {
res.send("Database is full!");
return;
}
new_receipt_id.value = ('0'.repeat(6 - new_receipt_id.value.length)) + new_receipt_id.value;
isValid = await insertIntoHoaDon(new_receipt_id, customer_id, date);
if (isValid !== true) {
res.send("Connection failed when inserting your receipt!");
return;
}
isValid = await insertIntoCT_HoaDon(new_receipt_id, product_detail_list);
if (isValid !== true) {
res.send(`Connection failed when inserting into CT_HoaDon!`);
return;
}
res.send("Insert successfully!");
})
app.get('/api/receipt-list', function (req, res) {
// If there is no parameters then get all HoaDon
var sqlQuery = 'SELECT * FROM HoaDon'
// TODO: handle resquest with parameters
if (Object.keys(req.query).length > 1) {
var sqlQuery = `SELECT * FROM HoaDon
WHERE YEAR(NgayLap) = ${req.query.year} AND MONTH(NgayLap) = ${req.query.month}`
}
// TODO: get receipt_list in DB
const request = new sql.Request();
request.query(sqlQuery, (err, result) => {
if (err) res.status(500).send(err);
var totalItems = result.recordset.length;
var page = req.query.page;
const total_receipt_list = result.recordset.map(elm => ({ id: elm.MaHD, customer_id: elm.MaKH, date: elm.NgayLap.toLocaleDateString(), total: elm.TongTien}));
const receipt_list = total_receipt_list.slice(10*(page-1), 10*page-1);
// Send to res
res.json({totalItems: totalItems,receipt_list: receipt_list});
});
})
sql.connect(config, err => {
if (err) {
console.log('Failed to open a SQL Database connection.', err.stack);
process.exit(1);
}
var server = app.listen(8080, function () {
var host = server.address().address;
var port = server.address().port;
console.log("Example app listening at http://%s:%s", host, port);
});
});