-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver_BASE_24125.js
153 lines (122 loc) · 4.44 KB
/
server_BASE_24125.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
"use strict";
require('dotenv').config();
const PORT = process.env.PORT || 8080;
const ENV = process.env.ENV || "development";
const express = require("express");
const cookieParser= require("cookie-parser");
// const cookies = require("js-cookie")
const bodyParser = require("body-parser");
const sass = require("node-sass-middleware");
const app = express();
const knexConfig = require("./knexfile");
const knex = require("knex")(knexConfig[ENV]);
const morgan = require('morgan');
const knexLogger = require('knex-logger');
// Needed for twilio connection
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const client = require('twilio')(accountSid, authToken);
// Seperated Routes for each Resource
const usersRoutes = require("./routes/dbroute");
// const businessFunction= require("./public/scripts/business-page");
// console.log(userRoute)
// Load the logger first so all (static) HTTP requests are logged to STDOUT
// 'dev' = Concise output colored by response status for development use.
// The :status token will be colored red for server error codes, yellow for client error codes, cyan for redirection codes, and uncolored for all other codes.
app.use(morgan('dev'));
// Log knex SQL queries to STDOUT as well
app.use(knexLogger(knex));
app.use(cookieParser('The dog barks loudly when no one is listening'));
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({ extended: true }));
app.use("/styles", sass({
src: __dirname + "/styles",
dest: __dirname + "/public/styles",
debug: true,
outputStyle: 'expanded'
}));
app.use(express.static("public"));
// Mount all resource routes
app.use("/api", usersRoutes(knex));
// Render home page
app.get("/", (req, res) => {
res.render("index.ejs");
})
// Getting checkout without ordering returns an error message
app.get("/checkout", (req, res) => {
res.render("checkout.ejs");
});
app.get("/confirm", (req, res) => {
res.render("confirm.ejs");
})
// Selecting next button on homepage redirects user to checkout page
app.post("/next", (req, res) => {
res.redirect("/checkout");
})
// Selecting confirm button on the checkout page redirects the user to the homepage with a confirmed pop-up
app.post("/checkout/confirm", async (req, res) => {
console.log(`name: ${req.body.name}, phone_number: ${req.body.phone_number}`)
let name = req.body.name;
let phoneNumber = req.body.phone_number
//inserting order information to the database
await knex.insert({
id: 4,
name: name,
phone_number: phoneNumber,
status: "outstanding"
}).into("orders")
// .then(function(results){
// for (drink in drinks){
// if (drinks[drink] = 8) {
// knex.insert({
// preset_drink_id: null
// }).into('orders_lines').then(function(results){
// for (ingredient in ingredients){
// knex.insert({
// ingredient_id: ingredient
// }).into('customized_drink_id')
// }
// })
// } else {
// knex.insert({
// preset_drink_id: drinks[drink]
// }).into(orders_lines)
// }
// }
// })
await apiReload.loadPresets()
client.messages
.create({
body: 'A new order has been placed! See </business> for details',
from: phoneNumber,
to: process.env.MY_PHONE_NUMBER,
})
.then(message => console.log(message.sid));
res.redirect("/confirm")
})
// Getting business page renders business page html with only outstanding orders being displayed
app.get("/business", (req, res) => {
res.render("business.ejs")
})
// Selecting the "send" button beside the "time" field on the business page should trigger Twilio to send a message to the customer about pickup time
app.post("/business/time-entered", async (req, res) => {
await knex.update({
status: "picked up"
}).where({
id: 2
}).into('orders')
await apiReload.loadPresets()
let minutes = req.body.minutes;
client.messages
.create({
body: 'Your order has been processed and will be ready in ' + minutes + ' minutes. See you soon :)',
from: phoneNumber, //'+16477244390',
to: process.env.MY_PHONE_NUMBER,
// we need to alter the "to" so it retrieves the phone number of the customer who ordered the drink
})
.then(message => console.log(message.sid));
res.redirect("/business")
})
app.listen(PORT, () => {
console.log("Example app listening on port " + PORT);
});