-
Notifications
You must be signed in to change notification settings - Fork 38
/
index.js
112 lines (101 loc) · 3.27 KB
/
index.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
const express = require('express');
const app = express();
const host = process.env.IP || '0.0.0.0';
const port = process.env.PORT || 8080;
const mongo = require('mongodb').MongoClient;
const mongoUri = process.env.uri;
const mongoUsername = process.env.username || process.env.MONGODB_USER;
const mongoPassword = process.env.password || process.env.MONGODB_PASSWORD;
const dbName = process.env.database_name ||
process.env.MONGODB_DBNAME ||
process.env.MONGODB_DATABASE ||
'sampledb';
const dbServiceName = process.env.DATABASE_SERVICE_NAME || 'localhost';
var dbConnectionUrl;
// If the monogo secret has been attached, modify the provided URI to include
// authentication credentials
if (mongoUri) {
var auth = mongoUsername + ':' + mongoPassword + '@'
var pieces = mongoUri.split('//');
dbConnectionUrl = pieces[0] + '//' + auth + pieces[1] + '/' + dbName;
}
else if (process.env.MONGODB_URL){
dbConnectionUrl = process.env.MONGODB_URL || 'mongodb://localhost:27017/sampledb';
} else {
dbConnectionUrl = 'mongodb://' + mongoUsername + ':' +
mongoPassword + '@' +
dbServiceName + ':27017/'
+ dbName;
}
app.get('/ticketNumber', function(req, res, next) {
let newTicketNumber = 100;
mongo.connect(dbConnectionUrl, (err, client) => {
if (err) {
console.error(err);
res.send({success: false, result: 9999});
} else {
const db = client.db(dbName);
const collection = db.collection('orders');
collection.find({}).count().then((n) => {
if (n > 0) {
collection.find().sort({ticketNumber:-1}).limit(1).toArray((err, items) => {
let highestTicket = items[0].ticketNumber;
newTicketNumber = highestTicket + 1;
collection.insertOne({ticketNumber: newTicketNumber, order: req.query}, (err, result) => {
console.log('err:' + err, ' result: ' + result);
});
res.send({success: true, result: newTicketNumber, order: req.query});
});
} else {
collection.insertOne({ticketNumber: newTicketNumber, order: req.query}, (err, result) => {
console.log('err:' + err, ' result: ' + result);
});
res.send({success: true, result: newTicketNumber, order: req.query});
}
}).catch((err) => {
console.log(err);
res.send({success: false, result: 999});
});
}
});
});
/* for debugging purposes */
app.get('/allorders', function (req, res, next) {
var ordersList;
mongo.connect(dbConnectionUrl, (err, client) => {
if (err) {
console.error(err)
return
}
console.log(dbConnectionUrl);
const db = client.db(dbName);
const collection = db.collection('orders');
collection.find().toArray((err, items) => {
ordersList = items;
console.log(ordersList);
res.send({success: true, result: ordersList});
});
});
});
app.get('/debug', function(req, res, next) {
var details = {
"mongo_url": dbConnectionUrl,
"connected": false
};
mongo.connect(dbConnectionUrl, (err, client) => {
if (err) {
console.error(err)
} else {
console.log('Connected to Mongo')
details["connected"] = true;
console.log("Updated details")
}
res.send(details);
});
});
app.use(function(err, req, res, next) {
console.error(err.stack);
res.status(500).send('Something went wrong.')
});
app.listen(port, host);
console.log('Concession Kiosk Backend started on: ' + host + ':' + port);