-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
97 lines (89 loc) · 2.46 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
const express = require("express");
const {
MainMenu,
Register,
SendMoney,
WithdrawMoney,
CheckBalance,
unregisteredMenu,
} = require("./menu");
const User = require("./models/user");
const mongoose = require("mongoose");
const dotenv = require("dotenv");
const cors = require("cors");
const app = express();
const PORT = process.env.PORT || 5000;
//Configuring Express
dotenv.config();
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
const connectionString = process.env.DB_URI;
//Configure MongoDB Database
mongoose
.connect(connectionString, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then((res) => {
console.log("MongoDB Running Successfully");
})
.catch((err) => {
console.log({ err });
console.log("MongoDB not Connected ");
});
app.post("/", (req, res) => {
const { sessionId, serviceCode, phoneNumber, text } = req.body;
User.findOne({ number: phoneNumber })
.then( async (user) => {
// AUTHENTICATION PARAMETERS
let userName;
let userRegistered;
let response = "";
if (!user) {
userRegistered = false;
} else {
userRegistered = true;
userName = user.name;
}
// MAIN LOGIC
if (text == "" && userRegistered == true) {
response = MainMenu(userName);
} else if (text == "" && userRegistered == false) {
response = unregisteredMenu();
} else if (text != "" && userRegistered == false) {
const textArray = text.split("*");
switch (textArray[0]) {
case "1":
response = await Register(textArray, phoneNumber);
break;
default:
response = "END Invalid choice. Please try again";
}
} else {
const textArray = text.split("*");
switch (textArray[0]) {
case "1":
response = await SendMoney(textArray, phoneNumber);
break;
case "2":
response = await WithdrawMoney(textArray);
break;
case "3":
response = await CheckBalance(textArray);
break;
default:
response = "END Invalid choice. Please try again";
}
}
// Send the response back to the API
res.set("Content-Type: text/plain");
res.send(response);
})
.catch((err) => {
console.log({ err });
});
});
app.listen(PORT, () => {
console.log(`Server is running on ${PORT}... `);
});