forked from sevenval/odl-thek
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.js
127 lines (105 loc) · 3.28 KB
/
helper.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
var github = {};
var aws = {};
try {
github = require('./github.json');
} catch (e) {
github = {
GITHUB_CLIENT_ID : "",
GITHUB_CLIENT_SECRET : ""
}
}
try {
aws = require('./aws.json');
} catch (e) {
aws = {
"ID" : "",
"Secret" : ""
}
}
var config = {
"github" : {
"GITHUB_CLIENT_ID" : process.env.GITHUB_CLIENT_ID ? process.env.GITHUB_CLIENT_ID : github.GITHUB_CLIENT_ID,
"GITHUB_CLIENT_SECRET" : process.env.GITHUB_CLIENT_SECRET ? process.env.GITHUB_CLIENT_SECRET : github.GITHUB_CLIENT_SECRET,
"callbackURL" : process.env.callbackURL ? process.env.callbackURL : "http://localhost:3000/users/auth/github/callback"
},
"aws" : {
"ID" : process.env.AWS_ID ? process.env.AWS_ID : aws.ID,
"Secret" : process.env.AWS_Secret ? process.env.AWS_Secret : aws.Secret
},
"mongourl" : process.env.mongodburl ? process.env.mongodburl : "mongodb://localhost:27017/odlthek"
}
var db = require('mongoskin').db(config.mongourl);
var bookings = db.collection('bookings');
/* test if the user ist authorized */
var ensureAuthenticated = function (req, res, next) {
if (req.isAuthenticated()) { return next(); }
res.redirect('/')
}
/* test if the user ist authorized */
var ensureAdmin = function (req, res, next) {
if (req.session.user&&req.session.user.role == 'admin') { return next(); }
res.redirect('/')
}
var prettyDate = function(_date) {
return getDate(_date)+' '+getTime(_date);
}
var getDate = function(_date) {
return _date.getFullYear()+'-'+fillZero(_date.getMonth()+1,2)+'-'+fillZero(_date.getDate(),2);
}
var getTime = function(_date) {
return fillZero(_date.getHours(),2)+':'+fillZero(_date.getMinutes(),2);
}
var fillZero = function(_int,_num) {
var string = _int.toString();
var length = string.length;
for(var i = length ; i < _num ; i++) {
string = '0'+string;
}
return string;
}
var now = function() {
return fillZero(new Date().getHours(),2)+":"+fillZero(new Date().getMinutes(),2);
}
var checkGadgetBooking = function(_gid,_start,_end,_callback) {
var find = { gadget : _gid.toString() };
find.start = {$lt: _end};
find.end = {$gte: _end};
bookings.find(find).toArray( function(_err,_bookings1){
if(_bookings1.length > 0) {
_callback(false, '1');
} else {
find.start = {$lte: _start};
find.end = {$gt: _start};
bookings.find(find).toArray( function(_err,_bookings2){
console.log('2',_bookings2);
if(_bookings2.length > 0) {
_callback(false, '2');
} else {
find.start = {$gte: _start}
find.end = {$lte: _end}
bookings.find(find).toArray( function(_err,_bookings3){
console.log('3',_bookings3);
if(_bookings3.length > 0) {
_callback(false, '3');
} else {
_callback(true, 'OK');
}
});
}
});
}
});
};
var helper = {
github : config.github,
aws : config.aws,
db : db,
ensureAuthenticated : ensureAuthenticated,
ensureAdmin : ensureAdmin,
prettyDate : prettyDate,
now : now,
getTime : getTime,
getDate : getDate,
checkGadgetBooking : checkGadgetBooking
}
module.exports = helper;