-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
331 lines (286 loc) · 10.8 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
require('dotenv').config();
const express = require('express');
const app = express();
const gallery = require('./engine/galleryCreator');
const participation = require('./engine/participation');
const twitter = require('./engine/twitter_bot');
const sendMail = require('./engine/sendMail');
const tableify = require('tableify');
const i18nextXHRBackend = require('i18next-xhr-backend');
const fs = require('fs');
const Papa = require('papaparse');
// const i18n = require('./engine/i18n')
// const cookieParser = require('cookie-parser');
// const i18n = require('i18n-2');
const i18n = require('i18next');
const i18nFsBackend = require('i18next-node-fs-backend');
const i18nMiddleware = require('i18next-express-middleware');
var bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(bodyParser.json());
app.set('view engine', 'ejs');
app.use(express.static('public'));
// app.use(i18n);
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
// var options = {
// // order and from where user language should be detected
// order: [/*'path', 'session', */ 'querystring', 'cookie', 'header'],
// // keys or params to lookup language from
// lookupQuerystring: 'lng',
// lookupCookie: 'i18next',
// lookupSession: 'lng',
// lookupPath: 'lng',
// lookupFromPathIndex: 0,
// // cache user language
// caches: false, // ['cookie']
// // optional expire and domain for set cookie
// cookieExpirationDate: new Date(),
// cookieDomain: 'myDomain',
// cookieSecure: true // if need secure cookie
// }
i18n
.use(i18nextXHRBackend) //Allows to use i18next on client through JQuery
.use(i18nFsBackend) //Used to interface i18next with express
.use(i18nMiddleware.LanguageDetector) //Used to detect the language
.init({
backend: {
loadPath: __dirname + '/locales/{{lng}}.json',
// addPath: __dirname + '/locales/{{lng}}.missing.json'
addPath: __dirname + '/locales/{{lng}}.json'
},
fallbackLng: 'en',
lowerCaseLng: true,
preload: ['en', 'es', 'it'],
saveMissing: true,
keySeparator: false,
// keySeparator: '>',
nsSeparator: false
// detection: options
});
app.use(i18nMiddleware.handle(i18n, {
removeLngFromUrl: false
}));
// function requireHTTPS(req, res, next) {
// // The 'x-forwarded-proto' check is for Heroku
// // if (!req.secure && req.get('x-forwarded-proto') !== 'https' && process.env.NODE_ENV !== "development") {
// if (!req.secure && req.get('x-forwarded-proto') !== 'https' && process.env.NODE_ENV !== "development") {
// return res.redirect('https://' + process.env.HOST + req.url);
// }
// next();
// }
// app.use(requireHTTPS);
// app.get('/locales/:lang', (req, res) => {
// var lang = req.params.lang;
// var file = fs.readFileSync(`locales/${lang}.json`);
// res.send(file);
// });
// app.get('/changelang/:lang/:redirect', (req, res) => {
// var lang = req.params.lang;
// var redirect = req.params.redirect
// // var file = fs.readFileSync(`locales/${lang}.json`);
// i18n
// .changeLanguage(lang)
// .then((t) => {
// console.log(t('Wedding')); // -> same as i18next.t
// });
// // console.log(i18n);
// console.log(lang);
// console.log(redirect);
// res.redirect(redirect);
// });
// i18next
// .changeLanguage('en')
// .then((t) => {
// t('key'); // -> same as i18next.t
// });
app.get('/minutes', (req, res) => {
res.send({
days: req.t('day'),
hours: req.t('hour'),
minutes: req.t('minute'),
seconds: req.t('second'),
pluralLetter: req.t('pluralLetter')
});
});
// app.get('/test', (req, res) => {
// twitter.getTweets().then( (result) =>{
// res.render('test',{tweets: result});
// });
// // res.render('test');
// // res.send("Hello Test!")
// // res.send(req.t('Wedding'));
// });
// app.get('/index.php', function (req, res,next) { //to run attached to the apache server
app.get('/', function (req, res, next) {
// console.log("ROOT: " + req.t('Wedding'));
// console.log(req.acceptsLanguages());
twitter.getTweets().then((tweets) => {
res.render('index', {
gallery: gallery.galleryCreator('public/images/gallery', req.t),
tweets
});
}).catch((err) => {
// Handle any error that occurred in any of the previous
// promises in the chain.
// console.log(error);
res.render('index', {
gallery: gallery.galleryCreator('public/images/gallery', req.t),
tweets: ""
});
});
});
app.get('/gallery', function (req, res, next) {
res.render('gallery', {
gallery: gallery.galleryCreator('public/images/gallery', req.t)
});
});
app.get('/about', function (req, res, next) {
res.render('about');
});
app.get('/locations', function (req, res, next) {
res.render('locations', {
MAPS_API: process.env.MAPS_API
});
});
app.get('/contact', function (req, res, next) {
res.render('contact', {
MAPS_API: process.env.MAPS_API
});
});
app.get('/services', function (req, res, next) {
res.render('services');
});
app.get('/where', function (req, res, next) {
res.render('where', {
MAPS_API: process.env.MAPS_API
});
});
app.post('/addAttendant', function (req, res) {
var {
name,
numberAdults,
numberChildren,
email,
overwrite,
participating,
evLocation
} = req.body;
// console.log("User name = "+req.body.name +", mail is "+req.body.email +" number is "+req.body.number+"and overwrite is "+ req.body.overwrite);
try {
var foundDuplicates = participation.isParticipating(email,evLocation);
console.log("DUPLICATES FOUND: "+foundDuplicates+" OVERWRITE: "+overwrite);
// var foundDuplicates = participation.addParticipation(name, numberAdults, numberChildren, email, overwrite);
if (participating === 'true') {
if (foundDuplicates === true && overwrite === 'false') {
console.log("SENDING DUPLICATES");
res.send({
status: "duplicates",
text: req.t("Do you want to update it?"),
title: req.t("Participation Already Available"),
confirmButton: req.t("YES"),
cancelButton: req.t("NO")
});
} else {
console.log("SENDING DONE");
participation.addParticipation(req.t,name, numberAdults, numberChildren, email ,evLocation);
res.send({
status: "done",
text: req.t("Thank you for notifying us"),
title: req.t("Participation Saved")
});
}
} else { //telling us they won't participate
if (foundDuplicates === true && overwrite === 'false') {
res.send({
status: "duplicates",
text: req.t("Are you sure you don't want to participate anymore?"),
title: req.t("Participation Already Available"),
confirmButton: req.t("YES"),
cancelButton: req.t("NO")
});
} else {
participation.addParticipation(req.t,name, numberAdults, numberChildren, email ,evLocation);
res.send({
status: "done",
text: req.t("We are sorry you can't make it. If you change your mind, come update your registration through your email!"),
title: req.t("Participation Saved")
});
}
}
} catch (err) {
res.send({
status: "error",
title: req.t("Participation Not Saved"),
text: req.t("Please try again in few minutes!")
});
}
});
app.post('/sendMail', function (req, res) {
// var {fname,lname,subject,message,email} = req.body;
// console.log("User name = "+req.body.name +", mail is "+req.body.email +" number is "+req.body.number+"and overwrite is "+ req.body.overwrite);
// console.log(JSON.stringify(req.body));
// foo(req.body, function(res){
// alert(location); // this is where you get the return value
// });
// const start = async function(r) {
// const result = await sendMail.send(r.body);;
// res.send({status:"ok",text:r.t("Message Sent Successfully!")})
// console.log("Message Sent Successfully!");
// }
// start(req);
var sendPromise = sendMail.send(req.body);
sendPromise.then((result) => {
// console.log("Mail sent successfully");
// console.log(result);
res.send({
status: "ok",
title: req.t("Message Sent Successfully!"),
text: req.t("We will reply to your question as soon as possible.")
});
}).catch((err) => {
res.send({
status: "error",
title: req.t("Unable to send the message"),
text: req.t('Please try again in a few minutes or send us an email at [email protected].')
});
// console.log(err);
});
// await sendMail.send(req.body);
// res.send({status:"ok",text:req.t("Message Sent Successfully!")})
});
app.get('/participationlist', function (req, res) {
res.render('participationlist', {
colombiaTable: tableify(participation.getAllByLocation("Colombia")),
colombiaTotal: tableify(participation.totalParticipantsByLocation("Colombia")),
italyTable: tableify(participation.getAllByLocation("Italy")),
italyTotal: tableify(participation.totalParticipantsByLocation("Italy"))
});
});
app.get('/downloadlist/:loc', function (req, res) {
var loc = req.params.loc;
console.log("LOC: "+loc);
participations = participation.getAllByLocation(loc);
// var csv = Papa.unparse(participations);
// fs.writeFileSync('./data/participations-data.csv', csv);
res.setHeader('Content-Type', 'text/csv');
res.setHeader('Content-Disposition', 'attachment; filename=\"' + 'participations-'+ loc+ "-"+ Date.now() + '.csv\"');
res.send(Papa.unparse(participations))
// res.download('./data/participations-data.csv', 'participations-data.csv');
});
app.get('/sitemap', function (req, res) {
res.download('./data/sitemap.xml', 'sitemap.xml');
});
// Handle 404 - Keep this as a last route
app.use(function (req, res, next) {
res.status(404);
res.render('404');
});
app.listen(process.env.PORT, function () { //for the configuration on the website
console.log(`Wedding website listening on port ${process.env.PORT}!`);
});