-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
85 lines (72 loc) · 2.7 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
const express = require('express');
const mongoose = require('mongoose');
const app = express();
const dotenv = require('dotenv').config();
const port = process.env.PORT || 3000;
const Joi = require('@hapi/joi');
const email = require('./email/sendmailtoall');
mongoose.connect('mongodb://localhost/appointment_app', {
useCreateIndex: true,
useNewUrlParser: true,
useUnifiedTopology: true
}).then(() => {
console.log('connected to mongoDb');
});
const onSuccess = "Appointment was successfully made! you will soon recieve an email on the confirmation of your appointment timing.";
const onFailure = "Appointment Wasn't successful. Please try again later.";
const schema = new mongoose.Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true
},
phone: {
type: String,
required: true
},
time: {
type: String,
required: true
},
appointmentTime: {
type: String,
required: true
}
});
const Model = mongoose.model('appointments', schema);
function validate(appointment) {
const schema = {
name: Joi.string().min(3).max(100).required(),
email: Joi.string().min(3).max(255).required().email(),
phone: Joi.string().min(3).max(100).required(),
time: Joi.string().min(1).max(16).required(),
}
return Joi.validate(appointment, schema);
}
app.set('view engine', 'ejs');
app.use(express.urlencoded({ extended: false }))
app.get('/', async (req, res) => {
res.render('index', { appointment: { status: '', message: '' } });
});
app.post('/', async (req, res) => {
const { error } = validate(req.body);
if(error) return res.render('index', { appointment: { status: 'notMade', message: error.details[0].message } });
const newAppointment = new Model({
name: req.body.name,
email: req.body.email,
phone: req.body.phone,
appointmentTime: req.body.time,
time: new Date(),
});
await newAppointment.save();
email(process.env.YOUR_EMAIL, 'The appointment', 'An appointment on your clinic',
`<b>${req.body.name}</b> has tried to take an appointment on ${req.body.time}, you can contact him via email on ${req.body.email} or call on ${req.body.phone}...`)
email(req.body.email, 'Info on the appointment', 'Your appointment', `You have tried to take out your time and have willing to have an appointment on <b>${req.body.time}</b> and soon will get a confirmation email on the real appointment time. have a grat day.`)
res.render('index', { appointment: { status: 'made', message: onSuccess } });
});
app.listen(port, () => {
console.log('listening on port %d....', port);
});