Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
14ab16b
commit b972241
Showing
2 changed files
with
73 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,74 @@ | ||
var Model = require("../models/index"); | ||
const { Appointment, Slot } = Model; | ||
const Nexmo = require("nexmo"); | ||
|
||
const appointmentController = { | ||
all(req, res) { | ||
// Returns all appointments | ||
Appointment.find({}).exec((err, appointments) => res.json(appointments)); | ||
}, | ||
create(req, res) { | ||
var requestBody = req.body; | ||
|
||
var newslot = new Slot({ | ||
slot_time: requestBody.slot_time, | ||
slot_date: requestBody.slot_date, | ||
created_at: Date.now() | ||
}); | ||
newslot.save(); | ||
// Creates a new record from a submitted form | ||
var newappointment = new Appointment({ | ||
name: requestBody.name, | ||
email: requestBody.email, | ||
phone: requestBody.phone, | ||
slots: newslot._id | ||
}); | ||
|
||
const nexmo = new Nexmo({ | ||
apiKey: "daf6eb4c", | ||
apiSecret: "50c1d40fd38c2bf3" | ||
}); | ||
|
||
let msg = | ||
requestBody.name + | ||
" " + | ||
"this message is to confirm your appointment at" + | ||
" " + | ||
requestBody.appointment; | ||
|
||
// and saves the record to | ||
// the data base | ||
newappointment.save((err, saved) => { | ||
// Returns the saved appointment | ||
// after a successful save | ||
Appointment.find({ _id: saved._id }) | ||
.populate("slots") | ||
.exec((err, appointment) => res.json(appointment)); | ||
|
||
const from = 7016468675; | ||
const to = 2347016468675; | ||
|
||
nexmo.message.sendSms(from, to, msg, (err, responseData) => { | ||
if (err) { | ||
console.log(err); | ||
} else { | ||
console.dir(responseData); | ||
} | ||
}); | ||
}); | ||
var path = require("path"); | ||
var favicon = require("serve-favicon"); | ||
var logger = require("morgan"); | ||
var cookieParser = require("cookie-parser"); | ||
var bodyParser = require("body-parser"); | ||
const mongoose = require("mongoose"); | ||
|
||
var index = require("./routes/index"); | ||
const api = require("./routes/api/index"); | ||
|
||
var app = express(); | ||
|
||
mongoose.Promise = global.Promise; | ||
|
||
//Adds connection to database using mongoose | ||
//for <dbuser>:replace with your username, <dbpassword>: replace with your password. | ||
//<DATABASE_URL>: replace with database url, example:ds234562.mlab.com:17283 | ||
mongoose.connect("<dbuser>:<dbpassword>@<DATABASE_URL>/appointments", { | ||
useMongoClient: true | ||
}); | ||
|
||
//This enabled CORS, Cross-origin resource sharing (CORS) is a mechanism that allows restricted resources (e.g. fonts) | ||
//on a web page to be requested from another domain outside the domain from which the first resource was served | ||
|
||
app.all("/*", function(req, res, next) { | ||
// CORS headers | ||
res.header("Access-Control-Allow-Origin", "*"); // restrict it to the required domain | ||
res.header("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,OPTIONS"); | ||
// Set custom headers for CORS | ||
res.header( | ||
"Access-Control-Allow-Headers", | ||
"Content-type,Accept,X-Access-Token,X-Key" | ||
); | ||
if (req.method == "OPTIONS") { | ||
res.status(200).end(); | ||
} else { | ||
next(); | ||
} | ||
}; | ||
}); | ||
// view engine setup | ||
app.set("views", path.join(__dirname, "views")); | ||
app.set("view engine", "ejs"); | ||
|
||
// uncomment after placing your favicon in /public | ||
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); | ||
app.use(logger("dev")); | ||
app.use(bodyParser.json()); | ||
app.use(bodyParser.urlencoded({ extended: false })); | ||
app.use(cookieParser()); | ||
app.use(express.static(path.join(__dirname, "public"))); | ||
|
||
app.use("/", index); | ||
app.use("/api", api); | ||
//app.use('/users', users); | ||
|
||
// catch 404 and forward to error handler | ||
app.use(function(req, res, next) { | ||
var err = new Error("Not Found"); | ||
err.status = 404; | ||
next(err); | ||
}); | ||
|
||
// error handler | ||
app.use(function(err, req, res, next) { | ||
// set locals, only providing error in development | ||
res.locals.message = err.message; | ||
res.locals.error = req.app.get("env") === "development" ? err : {}; | ||
|
||
// render the error page | ||
res.status(err.status || 500); | ||
res.render("error"); | ||
}); | ||
|
||
module.exports = appointmentController; | ||
module.exports = app; |