-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
76 lines (64 loc) · 2.25 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
const express = require('express');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const moment = require('moment');
const path = require('path');
const bodyParser = require('body-parser');
const jsSHA = require('jssha');
var hasher = new jsSHA('SHA-512', 'TEXT');
var id = 0;
app.use( bodyParser.urlencoded({ extended: true }) );
app.use(express.static('public'));
app.set('view engine', 'jade');
app.locals.title = "Bookmeet";
app.locals.allSchedules = {};
app.get('/', function (req, res) {
res.render('index');
});
app.post('/schedules', function (req, res) {
if (!req.body.name) { return response.sendStatus(400); }
hasher.update(req.body.name + String(Math.random()) + '-admin');
var manageId = hasher.getHash('HEX').slice(0,8);
hasher.update(req.body.name + String(Math.random()) + '-bookers');
var bookerId = hasher.getHash('HEX').slice(0,10);
app.locals.allSchedules[manageId] = {
name: req.body.name,
manageId: manageId,
bookerId: bookerId,
appointments: {}
}
console.log('Redirecting to ' + manageId);
res.redirect('/manage/' + manageId);
});
app.get('/manage/:id', function (req, res) {
res.render('manage', {schedule: app.locals.allSchedules[req.params.id]});
});
app.get('/book/:id', function (req, res) {
res.render('book', {schedule: app.locals.allSchedules[req.params.id.slice(0,8)]});
});
io.on('connection', function (socket) {
socket.on('message', function (channel, message) {
if(channel.includes('new-slot', 9)) {
var schedule = channel.slice(0,8);
var appointment = {
id: id,
taken: false,
date: moment.utc(message['date']),
startmoment: moment.utc(message['startmoment']),
endmoment: moment.utc(message['endmoment']),
notes: message['notes']
};
app.locals.allSchedules[schedule]['appointments'][id] = appointment;
id++;
io.sockets.emit(schedule, appointment);
} else if(channel.includes('booking', 9)) {
var schedule = channel.slice(0,8);
app.locals.allSchedules[schedule]['appointments'][message['id']].taken = true;
io.sockets.emit(schedule, app.locals.allSchedules[schedule]['appointments'][message['id']]);
}
});
});
http.listen(process.env.PORT || 3000, function(){
console.log('Server is up.');
});