-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp-country-index.js
705 lines (596 loc) · 24 KB
/
app-country-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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
//http://fabianosoriani.wordpress.com/2011/08/15/express-api-on-node-js-with-mysql-auth/
//rest: https://github.com/visionmedia/express-resource
var config = require('./config');
codelists = require('./codelists'),
express = require('express'),
domain = require('domain').create(),
mysql = require('mysql'),
async = require('async'),
_ = require('underscore'),
fs = require('fs'),
hbs = require('hbs'),
inspect = require('eyes').inspector(),
moment = require('moment'),
log4js = require('log4js'),
argv = require('optimist').argv;
/* #### INIT #### */
//logging init
log4js.configure({
appenders: [{
type: "console"
}],
replaceConsole: true
});
var logger = log4js.getLogger(),
startTimer,
timeMeasure = true,
app = express(),
dbConLocal = mysql.createConnection(config.mysqlConn_local),
dbConProd1 = mysql.createConnection(config.mysqlConn_1);
//##### ENVIRONMENT VARIABLES
var USELOCALDB = false;
if (USELOCALDB) dbConProd1 = dbConLocal;
/* #### APPS #### */
app.use(function(req, res, next) {
domain.on("error", function(err) {
console.log("error:" + err.message);
res.writeHeader(500, {
'Content-Type': "text/html"
});
res.write("<h1>" + err.name + "</h1>");
res.end("<p style='border:1px dotted red'>" + err.message + "</p>");
});
domain.enter();
next();
});
app.configure(function() {
app.set('view engine', 'hbs');
app.use(express.static('public'));
app.use(express.bodyParser());
});
var headerTmpl = '<div><a href="/">Home</a></div>';
var serverDest = _.keys(config.pathMap);
var MV = { //Mountvacation workspace
date: {
next: function(day) { //0-6 sunday - shows upcomming days
var plusWeek = (moment().day() >= day) ? 7 : 0;
return moment().day(plusWeek + day).format();
}
},
api: { //api routines
readImageDir: function(req, res) {
},
getForecast: function(req, res) {
var action = req.params.action;
var queryStr = 'SELECT IDResort, date, summit_cond FROM myweather_dnevni_napoved;';
dbConProd1.query(queryStr, function(err, rows, fields) {
if (err) throw err;
var filteredList = _.chain(rows)
.filter(function(dts) {
var dtsDay = moment(dts.date).day();
return dtsDay === 0 || dtsDay === 6;
})
.groupBy(function(obj) {
return obj.IDResort;
})
.value();
_.each(filteredList, function(obj, key) {
var objS = {
d6: '',
d0: ''
}; //V8 doesn't have insertation order
_.each(obj, function(val, i) {
var sumPerc = codelists.weatherSymbolsPercentage[val.summit_cond];
if (typeof(sumPerc) === 'undefined') {
console.log('undefined weather symbol:', val);
sumPerc = 0;
}
var day = moment(val.date).day();
objS['d' + day] = [val.summit_cond, sumPerc, moment(val.date).format('D.M.YYYY'), day];
});
filteredList[key] = objS;
});
if (action) {
action = action.split(',');
var idxJs = 'MV.data.forecast=' + JSON.stringify(filteredList) + ';';
_.each(action, function(server) {
var path = config.pathMap[server] || 'x'; //sym. error
outSftp(res, idxJs, 'data-forecast.js', path);
});
} else {
// outJS(res, filteredList, 'MV.data.info');
outJSON(res, filteredList, 'MV.data.forecast');
}
});
},
getResortsIndexNew: function(req, res) {
var action = req.params.action;
var diskImgList = fs.readdirSync(config.baseImageUrl);
var queryStr = 'SELECT resorts.ID as rid, ' +
' IDCountry as cid, ' +
' resorts.title as title, ' +
' indexes.index as idx, ' +
' resorts_values.field, ' +
' LEFT(resorts_values.`value`, 20) as valorig, ' +
' IF(LEFT(field,4) = "seg_" AND resorts_values.`value` > "", TRUE, resorts_values.`value`) as val ' +
'FROM resorts ' +
'INNER JOIN indexes ON resorts.ID = indexes.tableID ' +
'INNER JOIN resorts_values ON resorts.ID = resorts_values.IDResort ' +
'WHERE resorts.visible = "True" ' +
'AND indexes.table = "resorts" ' +
'AND indexes.`default` = TRUE ' +
'AND (resorts_values.lang = "en" OR resorts_values.lang = "") ' +
'AND resorts_values.`value` > "" ' +
'AND field IN("slopes_all","slopes_blue","slopes_red","slopes_black","slopes_green", ' +
' "slopes_number","slopes_cross","artificial_snow","airport_distance","gastronomy_restaurants", ' +
' "gastronomy_bars","sealevel_minheight","sealevel_maxheight","images","seg_family_ski", ' +
' "seg_free_style","seg_free_ride","seg_romantic","seg_cross_country","seg_ski_party","seg_ski_spa", ' +
' "publicw_thermal_spa","child_slope","child_lift","child_care","child_carpet_lift","child_park", ' +
' "snowboard_halfpipe","snowboard_funpark","snowboard_corner","snowboard_wave","snowboard_cross", ' +
' "snowboard_jumps","snowboard_slides","snowboard_boxen","snowboard_rides", "priority")';
dbConProd1.query(queryStr, function(err, rows, fields) {
if (err) throw err;
var resorts = _.groupBy(rows, function(obj) {
return obj.rid;
});
var resFiltered = _.map(resorts, function(obj, key, list) {
var resObj = {
// priority: obj.priority,
id: obj[0].rid,
cid: obj[0].cid,
title: obj[0].title,
idx: obj[0].idx,
seg: [],
s_park: [],
family: [],
other: []
};
_.each(obj, function(val) { //each field
var fld = codelists.resortFieldsShort[val.field]; //|| val.field;
if (fld === 'img') { //images
var strToArr = eval(val.val)[0] || '';
var exists = diskImgList.indexOf("resort_" + obj[0].rid + "_1_sm.jpg") !== -1; //check for maps
if (exists) {
resObj[fld] = 'map';
} else {
resObj[fld] = strToArr || 'no-image';
}
return;
}
if (_.isUndefined(fld)) {
_.each(codelists.resortsFieldsGroups, function(group, searchP) {
if (val.field.indexOf(searchP) === 0) resObj[group.groupName].push(group[val.field]);
});
} else {
var valN = parseFloat(val.val.replace('False', 0), 10); //pretvorba num bol string
if (_.isNaN(val.val)) valN = val.val;
if (!_.isNull(valN)) resObj[fld] = valN;
}
});
return resObj;
});
resFiltered = _.sortBy(resFiltered, function(objF) {
// console.log(objF, key, list);
return objF.pri;
});
if (action) {
action = action.split(',');
var idxJs = 'MV.country.data.resorts=' + JSON.stringify(resFiltered);
_.each(action, function(server) {
var path = config.pathMap[server] || 'x'; //sym. error
outSftp(res, idxJs, 'resorts-index.js', path);
});
} else {
// outJS(res, resFiltered, 'MV.country.data.resorts');
outJSON(res, resFiltered, 'MV.country.data.resorts');
}
});
},
getAccShortDesc: function(req, res) {
var action = req.params.action;
var queryStr = 'SELECT IDAccommodation as aid, lang, CONCAT(LEFT(`value`, LOCATE(" ", `value`, 120)), "...") as val ' +
'FROM accommodations ' +
'INNER JOIN accommodations_values ON accommodations.ID = accommodations_values.IDAccommodation ' +
'WHERE IDResort = 9436 ' +
'AND field = "description_short" ' +
'AND active = true ' +
'AND `value` > "" ';
dbConProd1.query(queryStr, function(err, rows, fields) {
if (err) throw err;
var filteredList = _.chain(rows)
// .filter(function(dts){ var dtsDay = moment(dts.date).day(); return dtsDay === 0 || dtsDay === 6; })
//.groupBy(function(obj){return obj.aid;})
.groupBy(function(obj) {
return obj.lang;
})
.value();
if (action) {
action = action.split(',');
var idxJs = 'MV.data.accShortDesc=' + JSON.stringify(filteredList) + ';';
_.each(action, function(server) {
var path = config.pathMap[server] || 'x'; //sym. error
outSftp(res, idxJs, 'data-accShortDesc.js', path);
});
} else {
// outJS(res, filteredList, 'MV.data.info');
outJSON(res, filteredList, 'MV.data.accShortDesc');
}
});
}
}
};
bootstrapRoutes();
function bootstrapRoutes() {
app.all('*', function(req, res, next) {
startTimer = new Date();
inspect(req.url);
next();
});
app.get('/', getIndex);
app.get('/countries', getCountries);
app.get('/countries/:action', getCountries);
app.get('/resorts', getResorts);
app.get('/resorts/:action', getResorts);
// app.get('/resortsIndex', getResortsIndex);
// app.get('/resortsIndex/:action', getResortsIndex);
app.get('/resortsIndexNew', MV.api.getResortsIndexNew);
app.get('/resortsIndexNew/:action', MV.api.getResortsIndexNew);
app.get('/info', getInfo);
app.get('/info/:action', getInfo);
app.get('/forecast', MV.api.getForecast);
app.get('/forecast/:action', MV.api.getForecast);
app.get('/accShortDesc', MV.api.getAccShortDesc);
app.get('/accShortDesc/:action', MV.api.getAccShortDesc);
}
//Handlebar helpers
hbs.registerHelper('link', function(object) {
return new hbs.SafeString(
'<a href="' + object.url + '">' + object.text + '</a>'
);
});
hbs.registerHelper('select', function(object) {
var html = '<select size="8" id="' + object.id + '" name="' + object.id + '" ' + object.attr + '>';
_.each(object.values, function(item) {
//var selected = (inputFieldDesc.value() === item) ? 'selected="selected"' : '';
html += '<option value="' + item.value + '">' + item.text + '</option>';
});
html += '</select>';
return new hbs.SafeString(html);
});
//METHODS
function getIndex(req, res) {
var data = {};
data.routes = _.map(app.routes.get, function(val, key) {
return {
url: val.path,
text: val.path
};
});
data.baseRoutes = {
id: 'baseRoutes',
multi: false,
attr: 'multiple',
values: _.filter(_.map(app.routes.get, function(val, key) {
return {
"value": val.path,
url: val.path,
text: val.path
};
}), function(val) {
return (val.value.indexOf('/:') === -1);
})
};
data.serverList = {
id: 'serverList',
multi: false,
attr: 'multiple',
values: _.map(serverDest, function(val, key) {
return {
"value": val,
url: val,
text: val
};
})
};
res.render('index.hbs', data);
}
function getInfo(req, res) {
//Countries - group by lang
var action = req.params.action;
var queryStr = 'SELECT 88000 + SUM(nights) as nights ' +
'FROM ( ' +
'SELECT ' +
' booking2_cart.ID, ' +
' SUM(booking2_cart_content.stay) as nights ' +
'FROM ' +
' booking2_cart ' +
'LEFT JOIN ' +
' booking2_cart_content ON booking2_cart_content.IDCart = booking2_cart.ID ' +
'LEFT JOIN ' +
' booking2_cart_content_persons ON booking2_cart_content_persons.IDContent=booking2_cart_content.ID ' +
'WHERE booking2_cart.status="payment" ' +
'AND booking2_cart.payment_status="completed" ' +
'GROUP BY booking2_cart.ID) as nights_per_booking; ';
dbConProd1.query(queryStr, function(err, rows, fields) {
if (err) throw err;
rows = rows[0];
if (action) {
action = action.split(',');
var idxJs = 'MV.data.info=' + JSON.stringify(rows) + ';';
_.each(action, function(server) {
var path = config.pathMap[server] || 'x'; //sym. error
outSftp(res, idxJs, 'data-info.js', path);
});
} else {
// outJS(res, rows, 'MV.data.info');
outJSON(res, rows, 'MV.data.countries');
}
});
}
function getCountries(req, res) {
//Countries - group by lang
var action = req.params.action;
var queryStr = 'SELECT ' +
'countries_values.IDCountry, countries_values.`value` AS title, countries_values.lang, indexes.`index` ' +
'FROM countries_values ' +
' INNER JOIN countries ' +
' ON countries_values.IDCountry = countries.ID AND countries.active = true ' +
' INNER JOIN indexes ' +
' ON countries_values.IDCountry = indexes.tableID AND countries_values.lang = indexes.lang ' +
'WHERE countries_values.field = "title" ' +
'AND countries_values.IDCountry NOT IN (21, 28, 22) ' +
'GROUP BY countries_values.ID ' +
'ORDER BY title ';
dbConProd1.query(queryStr, function(err, rows, fields) {
if (err) throw err;
var countryGroup = _.groupBy(rows, function(val) {
return val.IDCountry;
});
_.each(countryGroup, function(obj, key, list) {
var nObj = {};
_.each(obj, function(val, key) {
nObj[val.lang] = [val.title || '', val.index];
// Clone lang objects
if (val.lang === 'si') {
nObj.sl = nObj[val.lang];
}
if (val.lang === 'cz') {
nObj.cs = nObj[val.lang];
}
});
list[key] = nObj;
});
if (action) {
action = action.split(',');
var idxJs = 'MV.data.countries=' + JSON.stringify(countryGroup) + ';';
_.each(action, function(server) {
var path = config.pathMap[server] || 'x'; //sym. error
outSftp(res, idxJs, 'data-countries.js', path);
});
} else {
// outJS(res, countryGroup, 'MV.data.countries');
outJSON(res, countryGroup, 'MV.data.countries');
}
});
}
function getResorts(req, res) {
//Resorts - group by country
var action = req.params.action;
var queryStr = 'SELECT ' +
'IDCountry, ' +
'resorts.ID as id, ' +
'resorts.title, ' +
'`index` ' +
'FROM countries INNER JOIN resorts ON countries.ID = resorts.IDCountry ' +
' INNER JOIN indexes ON resorts.ID = indexes.tableID ' +
'WHERE resorts.visible = TRUE ' +
'AND IDCountry NOT IN (21, 28, 22) ' +
'AND `default` = TRUE ' +
'AND indexes.table = "resorts" ' +
'GROUP BY resorts.ID ' +
'ORDER BY title ';
dbConProd1.query(queryStr, function(err, rows, fields) {
if (err) throw err;
_.each(rows, function(resort){
if(resort.id === 76){
resort.title += ' (Vogel, Pokljuka)';
}
if(resort.id === 142){
resort.title += ' (Terme snovik)';
}
});
rows.push({
IDCountry: 19,
id: 76,
title: 'Vogel',
index: 'bohinj'
}); //fake insert
rows.push({
IDCountry: 19,
id: 76,
title: 'Pokljuka',
index: 'bohinj'
}); //fake insert
rows.push({
IDCountry: 19,
id: 142,
title: 'Terme snovik',
index: 'krvavec'
}); //fake insert
countryGroup = _.map(rows, function(val) {
return [val.title, val.id, val.index, val.IDCountry];
});
if (action) {
action = action.split(',');
var idxJs = 'MV.data.resorts=' + JSON.stringify(countryGroup) + ';';
_.each(action, function(server) {
var path = config.pathMap[server] || 'x'; //sym. error
outSftp(res, idxJs, 'data-resorts.js', path);
});
} else {
//outJS(res, countryGroup, 'MV.data.resorts');
outJSON(res, countryGroup, 'MV.data.countries');
}
});
}
function getResortsIndex(req, res) {
//JSON for country index page
var action = req.params.action;
//http://www.mysqltutorial.org/stored-procedures-parameters.aspx
//http://stackoverflow.com/questions/10546956/is-there-a-driver-for-mysql-on-nodejs-that-supports-stored-procedures
var queryStr = 'SELECT resorts.ID as id, IDCountry as cid, resorts.title as title, indexes.index as idx ' +
'FROM resorts INNER JOIN indexes ON resorts.ID = indexes.tableID ' +
'WHERE resorts.visible = "True" AND indexes.table = "resorts" GROUP BY resorts.ID';
dbConProd1.query(queryStr, function(err, resorts, fields) {
if (err) throw err;
//http://stackoverflow.com/questions/7653080/adding-to-an-array-asynchronously-in-node-js
async.forEach(resorts, function(resort, callback) {
//console.log(resort); // print the key
var resortIndexQuery = 'CALL resort_index(' + resort.id + ')';
dbConLocal.query(resortIndexQuery, function(err, rows, fields) {
if (err) throw err;
rows[0].forEach(function(val, idx) {
if (val.val === 'True') {
resort[val.field] = true;
} else if (val.field === 'family' || val.field === 's_park' || val.field === 'seg' || val.field === 'other') {
if (val.val !== null) {
var numArr = val.val.split(',');
for (var i in numArr) {
numArr[i] = parseInt(numArr[i], 10);
}
resort[val.field] = numArr;
} else {
resort[val.field] = [];
}
} else if (val.field === 'img') {
var strToArr = eval(val.val)[0] || '';
fs.exists(config.baseImageUrl + "resort_" + resort.id + "_1_sm.jpg", function(exists) { //check for maps
if (exists) {
resort[val.field] = 'map';
} else {
resort[val.field] = strToArr || 'no-image';
// resort[val.field] = _.first(strToArr.match(/[\d]+$/)) || 'no-image'; //just image id
}
});
} else {
var valN = val.val.replace(',', '.');
if (isNaN(valN)) {
resort[val.field] = valN;
} else {
resort[val.field] = parseFloat(valN, 10);
}
}
});
callback(); // tell async that the iterator has completed
});
}, function(err) {
//console.log(resorts);
//
resorts = _.sortBy(resorts, function(obj) {
return obj.title;
});
if (action) {
action = action.split(',');
var idxJs = 'MV.country.data.resorts=' + JSON.stringify(resorts);
_.each(action, function(server) {
var path = config.pathMap[server] || 'x'; //sym. error
outSftp(res, idxJs, 'resorts-index.js', path);
});
} else {
// outJS(res, resorts, 'MV.country.data.resorts');
outJSON(res, resorts, 'MV.country.data.resorts');
}
});
});
}
function printTime() {
if (timeMeasure) console.log('it took: ' + ((new Date() - startTimer) / 1000) + ' s from req startTimer');
}
//sends to SFTP
function outSftp(res, data, file, path) {
var sftp = require('sftp');
// var dataCp = iconv.convert(data);
var dataCp = data;
config.sftp_api1.home = path;
printTime();
sftp = new sftp(
config.sftp_api1,
function(err) {
//Error
if (err) throw err;
//Success
console.log("Connected to SFTP");
//Write sample file
sftp.writeFile(file, dataCp, "ascii", function(err) {
if (err) throw err;
var msg = "It's saved as: " + path + '/' + file + ' (' + (dataCp.length / 1024).toFixed(2) + ' kB - ' + dataCp.length + ')';
printTime();
console.log(msg);
res.send(msg);
});
}
);
}
//sends to express
function outJSON(res, data) {
var json = JSON.stringify(data);
printTime();
res.contentType('application/json');
res.send(json);
}
//sends to express
function outJS(res, data, varName) {
var js = varName + '=' + JSON.stringify(data);
printTime();
res.contentType('application/text');
res.send(js);
}
//END -> ctrl + c exit event
process.on('SIGINT', function() {
dbConLocal.end(); //end conn. to db
dbConProd1.end(); //end conn. to db
console.log("\ngracefully shutting down from SIGINT (Crtl-C)");
process.exit();
});
process.setMaxListeners(14);
//INIT
// command line mode
var cServer = argv.s,
cPath = argv.p;
if (cServer && cPath) {
timeMeasure = false;
if (timeMeasure) startTimer = new Date();
console.log('Command line mode...');
var req = req = {
params: {
action: cServer
}
},
res = {
send: function(data) {
process.exit(code = 0); //exit app
}
},
methodMap = {
countries: getCountries,
resorts: getResorts,
resortsIndexNew: MV.api.getResortsIndexNew,
info: getInfo,
forecast: MV.api.getForecast,
accShortDesc: MV.api.getAccShortDesc
};
var mCall = methodMap[cPath];
if (!mCall) {
console.error('Wrong API call');
process.exit(code = 0); //exit app
}
mCall(req, res);
// getInfo(req, res);
} else {
//start app
app.listen(3001);
console.log('Listening on port 3001');
console.log('demo at: http://localhost:3001/resortsIndex');
console.log("BE aware: images are checked at: " + config.baseImageUrl);
//getResortsIndex();
}