-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb.js
More file actions
213 lines (193 loc) · 6.09 KB
/
web.js
File metadata and controls
213 lines (193 loc) · 6.09 KB
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
var express = require("express");
var logfmt = require("logfmt");
var mongoose = require('mongoose');
var config = require("./config");
var User = require('./models/user');
var bodyParser = require('body-parser');
var app = express();
mongoose.connect(config.mongoUri);
app.use(bodyParser());
app.use(logfmt.requestLogger());
app.get('/', function(req, res) {
res.json("Welcome to the api!");
});
app.post('/user', function(req, res) {
if(req.body.id) {
// update of existing user
User.findById(req.body.id, function(err, user) {
if (err) {
res.send(err);
} else {
// only rewrite new values
if (req.body.name) {
user.name = req.body.name;
}
if (req.body.password){
user.password = req.body.password;
}
if (req.body.creditCardNumber) {
user.creditCardNumber = req.body.creditCardNumber;
// strip spaces or any non number characters
user.expMonth = req.body.expMonth;
user.expYear = req.body.expYear;
user.cardVeriCode = req.body.cardVeriCode;
}
// req.body.removalPlate boolean - true or false
// true if u want to remove the plate, false if u want to add a new plate
if (req.body.removalPlate && req.body.numberPlate) {
var numberPlateGlobalised = req.body.numberPlate.toUpperCase();
if (eval(req.body.removalPlate)) {
// check if that number plate exists
user.numberPlates.remove(numberPlateGlobalised);
return res.json({code: "200", message: "Number plate removed", plates: user.numberPlates});
} else {
User.find({ numberPlates: { $elemMatch: { number: numberPlateGlobalised}}}, function(err, users) { // Check users in the DB for the same number plate
if (err){
res.json({code:"3003", message:"upppsss error looking for plate"});
} else {
if (users.length > 0) {
res.json({code: "3001", message: "Number plate already exists in DB"}); // and throws an error
} else {
var numberPlateObj = {number: numberPlateGlobalised, transaction: []}
user.numberPlates.push(numberPlateObj);
user.save(function (err) {
if (err) {
res.send(err);
}
});
res.json(code:"200", user: user);
}
}
});
}
} else if (req.body.numberPlate && !req.body.removalPlate) {
res.json({code: "3011", message: "Remove or add Plate?"});
} else if (req.body.removalPlate && !req.body.numberPlate) {
res.json({code: "3012", message: "What plate?"});
} else {
user.save(function (err) {
if (err) {
res.send(err);
}
});
res.json(user);
}
}
});
} else {
// creation of user
var user = new User();
if (!req.body.email || !req.body.password || !req.body.name) {
var error_message = {code: '2002', message: 'Not enough data for creation of account'};
res.send(error_message);
} else {
var user_exists = false;
User.find({email: req.body.email}, function(err, users) { // Check users in the DB for the same email
if (users.length > 0) {
res.json({code: '2003', message: 'E-mail already exists!'});
} else {
user.name = req.body.name;
user.password = req.body.password;
user.email = req.body.email;
// add other non required fields
user.save(function(err) {
if (err) {
res.send(err);
}
res.json({ code: "200", message: 'User created!' });
});
}
});
}
}
});
app.get('/user', function(req, res) {
User.find(function(err, users) {
if (err) {
res.send(err);
}
res.json(users);
});
});
app.post('/update', function(req, res) {
User.findById(req.body.id, function(err, user) {
if (err) {
res.send(err);
}
user.name = req.body.name;
user.save(function (err){
if (err) {
res.send(err);
}
});
res.json(user);
});
});
app.post('/payment', function(req, res) {
// numberPlate > the number plate of the car to make the payment
// amount > amount of the payment IN CENTS
var numberPlateGlobalised = req.body.numberPlate.toUpperCase();
// make sure there's a handle for the no users found and also that there can no be the same number plate in the system twice
User.find({numberPlates: numberPlateGlobalised}, function(err, users) {
// check that there is only one user returned from the DB
var user = null;
if (users.length == 1) {
user = users[0];
config.SimplifyPay.payment.create({
amount : req.body.amount,
description : "Test payment",
card :
{
expMonth : user.expMonth,
expYear : user.expYear,
cvc : user.cardVeriCode,
number : user.creditCardNumber
},
currency : "USD"
},
function(errData, data) {
if(errData) {
res.send({code: "3002", message: 'Payment failed', error: errData});
} else {
res.send({code: "200", message: 'Payment Approved', amount: req.body.amount, plate: numberPlateGlobalised});
// save transaction to history of user
var transactionObj = {dateTime: Date.now(), location:"MasterCard Office"};
user.numberPlates.foreach(function(plate, index, array) {
if (plate.number == numberPlateGlobalised) {
return;
}
});
}
});
} else {
res.json({code: "3001", message: "Number Plate not in the system!", plate: numberPlateGlobalised, })
}
});
});
// POST @ /login
// > password | password of the user to login
// > email | email of user to login
// << id | return id of the logged in user if successfull (code: 200)
app.post('/login', function(req, res) {
if (!req.body.password || !req.body.email) {
res.json({code:"400", message:"not enough data!"});
} else {
User.find({email:req.body.email}, function(err, users) {
if (err) {
res.json({code:"4000", message:"DB conn failed!"});
} else {
if (users.length == 1) {
if (req.body.password == users[0].password) {
res.json({code:"200", id:users[0]._id});
} else {
res.json({code:"401", message:"Not allowed!"});
}
}
}
});
}
});
var port = Number(process.env.PORT || 5000);
app.listen(port, function() {
console.log("Listening on " + port);
});