-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathkiosk.js
More file actions
816 lines (729 loc) · 25.9 KB
/
kiosk.js
File metadata and controls
816 lines (729 loc) · 25.9 KB
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
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
var Storage = require('./storage');
var Url = require('./url');
var Promise = require('bluebird');
var request = require('request-promise');
var moment = require('moment');
var _ = require('lodash');
var debug = require('debug')('kiosk');
const polyglot = require('./polyglot')();
const cityAliases = {
mow: 'mow',
spb: 'spb'
};
const cities = {
[cityAliases.mow]: {
alias: cityAliases.mow,
name: 'МОСКВА',
formattedName: polyglot.t('moscowCapitalized'),
code: 2000000
},
[cityAliases.spb]: {
alias: cityAliases.spb,
name: 'САНКТ-ПЕТЕРБУРГ',
formattedName: polyglot.t('spbCapitalized'),
code: 2004000
}
};
const hourAliases = {
earlyMorning: 'earlyMorning',
morning: 'morning',
day: 'day',
evening: 'evening'
};
const hourNames = {
[hourAliases.earlyMorning]: 'рано утром',
[hourAliases.morning]: 'утром',
[hourAliases.day]: 'днём',
[hourAliases.evening]: 'вечером'
};
/**
* The hours that are passed to _.inRange function (which means a value should be between the start hour and up to, but not including, the end hour)
* @type {Object}
*/
const hours = {
[hourAliases.earlyMorning]: [5, 7],
[hourAliases.morning]: [7, 10],
[hourAliases.day]: [10, 17],
[hourAliases.evening]: [17, 20]
};
const weekdays = {
any: 'any',
weekend: 'weekend',
weekday: 'weekday'
};
const afterCredentialsDelay = 15000;
/**
* Timespan length in days. Rzd only allows searching for tickets within 60 days.
* @type {number}
*/
const timespan = 60;
/**
* Maximum number of attempts to fetch tickets from rzd site.
* @type {number}
*/
const maxAttempts = 15;
/**
* Tickets count is never expected to go beneath this threshold.
* @type {number}
*/
const ticketsCountThreshold = 1200;
const collectionName = 'roundtrips';
/**
* @param {Object} to
* @constructor
*/
var Route = function(to) {
var cityCodes = Object.keys(cities);
this.to = cityCodes.indexOf(to) === -1 ? cities.spb.alias : cities[to].alias;
this.from = this.to === cityAliases.spb ? cities.mow.alias : cities.spb.alias;
};
Route.prototype.getCityFrom = function() {
return cities[this.from] || {};
};
Route.prototype.getCityTo = function() {
return cities[this.to] || {};
};
Route.prototype.getSummary = function() {
var fromName = this.getCityFrom()['formattedName'];
var toName = this.getCityTo()['formattedName'];
return `${fromName} → ${toName}`;
};
Route.prototype.isToSpb = function() {
return this.to === cityAliases.spb;
};
Route.prototype.toObject = function() {
return {
to: this.to,
from: this.from
}
};
/**
* Returns a new reversed route
* @param {Route} route
* @returns {Route}
*/
Route.getReversed = function(route) {
return new Route(route.from);
};
/**
* @returns {Route}
*/
Route.toMoscow = function() {
return new Route(cityAliases.mow);
};
/**
* @returns {Route}
*/
Route.toSpb = function() {
return new Route(cityAliases.spb);
};
/**
* Creates a Route object if necessary
* @param {Object} route
* @returns {Route}
*/
Route.hydrate = function(route) {
if (route && route instanceof Route) {
return route;
} else if (route && route.to) {
return new Route(route.to);
} else if (route && route.from) {
return Route.getReversed(route);
} else {
return new Route();
}
};
const defaultRoute = Route.toMoscow();
/**
* Returns request url and parameters
* @param {Route} route
* @param {Moment} date1
* @param {Moment} [date2]
* @param {String} [rid]
* @returns {Object}
*/
var getRequestOptions = function(route, date1, date2, rid) {
date2 = date2 || date1.clone();
var dateFormat = 'DD.MM.YYYY';
var cityFrom = route.getCityFrom();
var cityTo = route.getCityTo();
var parameters = {
STRUCTURE_ID: '735',
layer_id: '5371',
dir: '1',
tfl: '3',
checkSeats: '1',
st0: cityFrom.name,
code0: cityFrom.code,
dt0: date1.format(dateFormat),
st1: cityTo.name,
code1: cityTo.code,
dt1: date2.format(dateFormat)
};
if (rid) {
parameters.rid = rid;
}
return {
url: 'https://pass.rzd.ru/timetable/public/ru',
parameters: parameters
};
};
var getSessionCookie = function(response) {
var cookieHeader = response.headers['set-cookie'];
var cookie = {};
/**
* @example JSESSIONID=00004ADS7pUenJiasDpQq4maKIR:17obq8rib; Path=/
*/
var sessionCookie = _.find(cookieHeader, function(item) {
return item.indexOf('JSESSIONID') !== -1;
});
if (sessionCookie) {
var pair = sessionCookie.split(';')[0].split('=');
cookie = {
name: pair[0],
value: pair[1]
};
}
return cookie;
};
/**
* Fetches rid
* @param {Object} requestOptions
* @returns {Promise}
*/
var getCredentials = function(requestOptions) {
return request({
method: 'GET',
url: requestOptions.url,
qs: requestOptions.parameters,
resolveWithFullResponse: true,
json: true
})
.then(function(response) {
return extractCredentials(response, response.body);
})
.catch(function() {
throw new Error('Failed to get credentials.');
});
};
/**
* @param {Object} response
* @param {String} body
* @returns {{rid: (*|String), sessionCookie}}
*/
var extractCredentials = function(response, body) {
var sessionCookie = getSessionCookie(response);
return {
rid: body.rid,
sessionCookie: sessionCookie
};
};
/**
* Fetches tickets for a given date
* @param {Date} date
* @param {Route} route
* @returns {Promise}
*/
var getTicketsForDate = function(date, route) {
date = date || new Date();
var momentDate = moment(date);
var credentialsOptions = getRequestOptions(route, momentDate);
var attemptsCount = 0;
// Using promise constructor because of setTimeout
var promise = new Promise(function(resolve, reject) {
var getTicketsWithCredentials = function(credentials) {
debug('credentials', credentials);
setTimeout(function() {
// Add rid query parameter
var ticketOptions = _.clone(credentialsOptions);
ticketOptions.parameters.rid = credentials.rid;
// Add session cookie
var cookie = `${credentials.sessionCookie.name}=${credentials.sessionCookie.value};`;
request({
method: 'GET',
url: ticketOptions.url,
qs: ticketOptions.parameters,
resolveWithFullResponse: true,
json: true,
// Setting cookie via jar does not work.
headers: {
Cookie: cookie
}
}).then(function(response) {
var body = response.body;
var allTickets = [];
if (body.tp) {
allTickets = allTickets.concat(body.tp[0].list);
allTickets = allTickets.concat(body.tp[1] ? body.tp[1].list : []);
}
debug('allTickets.length', allTickets.length);
attemptsCount++;
// Occasionally a new rid is returned instead of tickets.
// If so, make another attempt to fetch tickets.
if (!allTickets.length && attemptsCount <= maxAttempts) {
debug('unexpected response: ', body);
debug('attemptsCount', attemptsCount);
if (attemptsCount <= maxAttempts) {
getCredentials(credentialsOptions)
.then(getTicketsWithCredentials);
} else {
reject('Maximum attempts reached, no tickets in response.');
}
} else {
var relevantTickets = filterTickets(allTickets, {
highSpeed: true
});
if (!relevantTickets.length && allTickets.length) {
debug('All tickets filtered out');
}
resolve(relevantTickets);
}
})
.catch(function(e) {
throw new Error('Unable to get tickets');
});
}, afterCredentialsDelay);
};
getCredentials(credentialsOptions)
.then(getTicketsWithCredentials);
});
return promise;
};
/**
* Filters only relevant tickets
* @param {Array} allTickets
* @param {Object} options
* @returns {Array}
*/
var filterTickets = function(allTickets, options) {
var brand = options.highSpeed ? 'САПСАН' : null;
var station0;
if (options.route) {
station0 = _.isObject(options.route) ? Route.hydrate(options.route).getCityFrom().name : cities[options.route].name;
}
var date = options.date;
var hourAlias = options.hours;
var hourRange = hours[hourAlias];
return _.filter(allTickets, function(ticket) {
var hit = true;
var departureMoment = moment(getTicketDepartureDate(ticket));
if (brand && ticket.brand !== brand) {
hit = false;
}
if (station0 && ticket.station0 !== station0) {
hit = false;
}
if (date && !departureMoment.isSame(date, 'day')) {
hit = false;
}
if (hourRange && !_.inRange(departureMoment.get('hours'), hourRange[0], hourRange[1])) {
hit = false;
}
return hit;
});
};
var formatCity = function(name) {
return _.map(name.toLowerCase().split('-'), function(part) {
return _.upperFirst(part);
})
.join('-');
};
/**
* @param {Object} json
* @returns {String}
*/
var formatTicket = function(json) {
var cityFrom = formatCity(json.station0);
var cityTo = formatCity(json.station1);
var date = moment(getTicketDepartureDate(json));
var dayFormatted = date.format(polyglot.t('dateFormat4'));
var timeFormatted = date.format(polyglot.t('timeFormat1'));
//Санкт-Петербург → Москва,
//16 марта, среда, отправление в 5:30
//1290 ₽
return polyglot.t('ticketTemplate', {
cityFrom: cityFrom,
cityTo: cityTo,
dayFormatted: dayFormatted,
timeFormatted: timeFormatted,
rate: json.cars[0].tariff
});
};
/**
* Returns a link to rzd site with route and dates selected.
* @param {Object} roundtrip
* @returns {Promise}
*/
var rzdDateRouteUrl = function(roundtrip) {
var route = Route.hydrate(roundtrip.originatingTicket.route);
var momentDate1 = moment(roundtrip.originatingTicket.datetime);
var momentDate2 = moment(roundtrip.returnTicket.datetime);
var toHash = function(obj) {
return _.map(obj, function(v, k) {
return encodeURIComponent(k) + '=' + encodeURIComponent(v);
}).join('|');
};
var requestOptions = getRequestOptions(route, momentDate1, momentDate2);
var structureId = requestOptions.parameters.STRUCTURE_ID;
delete requestOptions.parameters.layer_id;
delete requestOptions.parameters.STRUCTURE_ID;
requestOptions.parameters.st0 = route.getCityFrom().formattedName;
requestOptions.parameters.st1 = route.getCityTo().formattedName;
var parametersHash = toHash(requestOptions.parameters);
var url = `${requestOptions.url}?STRUCTURE_ID=${structureId}#${parametersHash}`;
// Using url shortener because when a link in Telegram gets clicked, the '|' characters in rzd url are url-encoded into %7C, which breaks the url.
// There is no way to avoid the '|' character usage.
return Url.shorten(url);
};
// TODO Move to date utility
var formatWeekday = function(dateMoment) {
var localeData = moment.localeData();
var formatted;
if (localeData._weekdays.format) {
formatted = localeData._weekdays.format[dateMoment.day()];
formatted = (formatted.substring(0, 2) === 'вт' ? 'во ' : 'в ') + formatted;
} else {
formatted = dateMoment.format('dddd');
}
return formatted;
};
var formatNestedTicket = function(ticket) {
var datetimeMoment = moment(ticket.datetime);
var dayFormatted = datetimeMoment.format(polyglot.t('dateFormat4'));
var timeFormatted = datetimeMoment.format(polyglot.t('timeFormat1'));
var route = Route.hydrate(ticket.route);
//Санкт-Петербург → Москва,
//16 марта, среда, отправление в 5:30
//1290 ₽
return polyglot.t('ticketTemplate', {
cityFrom: route.getCityFrom().formattedName,
cityTo: route.getCityTo().formattedName,
dayFormatted: dayFormatted,
timeFormatted: timeFormatted,
rate: ticket.price
});
};
/**
* @param {Object} roundtrip
* @param {Boolean} [includeLink]
* @returns {Promise}
*/
var fullFormat = function(roundtrip, includeLink) {
var originatingTicketText = formatNestedTicket(roundtrip.originatingTicket);
var returnTicketText = formatNestedTicket(roundtrip.returnTicket);
var promise;
var text = `${originatingTicketText}\n\n${returnTicketText}\n----------\n${roundtrip.totalCost} ₽`;
if (includeLink) {
promise = rzdDateRouteUrl(roundtrip)
.then(function(url) {
return `${text}\n\n${url}`;
});
} else {
promise = Promise.resolve(text);
}
return promise;
};
/**
* @param {Object} roundtrip
* @param {Boolean} [includeLink]
* @returns {Promise}
*/
var shortFormat = function(roundtrip, includeLink) {
var localeData = moment.localeData();
var originatingTicket = roundtrip.originatingTicket;
var originatingMoment = moment(originatingTicket.datetime);
var formatWeekday = polyglot.t('formatWeekday');
var originatingWeekday = formatWeekday && formatWeekday(originatingMoment, localeData._weekdays.format) || originatingMoment.format('dddd');
var originatingTicketDateFormatted = originatingWeekday + ' ' + originatingMoment.format(polyglot.t('dateFormat5'));
var returnTicket = roundtrip.returnTicket;
var returnMoment = moment(returnTicket.datetime);
var returnWeekday = formatWeekday && formatWeekday(returnMoment, localeData._weekdays.format) || originatingMoment.format('dddd');
var returnTicketDateFormatted = returnWeekday + ' ' + returnMoment.format(polyglot.t('dateFormat5'));
var originationRoute = Route.hydrate(originatingTicket.route);
var promise;
// Санкт-Петербург → Москва и обратно за 3447 ₽
// Туда в среду 18 мая в 7:00, обратно в четверг 19 мая в 18:00
var routeText = polyglot.t('routeTemplate', {
routeFrom: originationRoute.getCityFrom().formattedName,
routeTo: originationRoute.getCityTo().formattedName
});
var text = polyglot.t('ticketShortTemplate', {
totalCost: roundtrip.totalCost,
originatingTicketDateFormatted: originatingTicketDateFormatted,
returnTicketDateFormatted: returnTicketDateFormatted
});
if (includeLink) {
promise = rzdDateRouteUrl(roundtrip)
.then(function(url) {
var link = `<a href="${url}">${routeText}</a>`;
return `${link} ${text}`;
});
} else {
promise = Promise.resolve(`${routeText} ${text}`);
}
return promise;
};
/**
* @param {Array} roundtrips
* @param {Boolean} [includeLink]
* @returns {String}
*/
var formatRoundtrip = function(roundtrips, includeLink) {
var promises = [];
roundtrips = roundtrips && !_.isArray(roundtrips) ? [roundtrips] : roundtrips;
var isShortFormat = roundtrips.length > 1;
_.forEach(roundtrips, function(roundtrip) {
promises.push(isShortFormat ? shortFormat(roundtrip, includeLink) : fullFormat(roundtrip, includeLink));
});
return Promise.all(promises)
.then(function(texts) {
return texts.join("\n\n");
});
};
var formatRoundtripTitle = function(roundtrip) {
var originatingTicket = roundtrip.originatingTicket;
var originatingMoment = moment(originatingTicket.datetime);
var originatingTicketDateFormatted = originatingMoment.format(polyglot.t('dateFormat6'));
var originatingRoute = Route.hydrate(originatingTicket.route);
return `${originatingRoute.getCityFrom().formattedName} ⇄ ${originatingRoute.getCityTo().formattedName}, ${roundtrip.totalCost} ₽, ${originatingTicketDateFormatted}`;
};
/**
* @param {Object} json Ticket json
* @returns {Date}
*/
var getTicketDepartureDate = function(json) {
// This date format is for parsing, do not localize
return moment(`${json.date0} ${json.time0}`, 'DD.MM.YYYY HH:mm').toDate();
};
/**
* @param {Object} json Ticket json
* @returns {Date}
*/
var getDayAfterTicket = function(json) {
var ticketDepartureDate = getTicketDepartureDate(json);
return moment(ticketDepartureDate).add(1, 'days').startOf('day').toDate();
};
/**
* @param {Object} json Ticket json
* @returns {Date}
*/
var getDayBeforeTicket = function(json) {
var ticketDepartureDate = getTicketDepartureDate(json);
return moment(ticketDepartureDate).subtract(1, 'days').startOf('day').toDate();
};
/**
* Makes up ticket summary
* @param {Object} json Ticket json
* @returns {String}
*/
var getSummary = function(json) {
return `${json.date0} ${json.station0} ${json.station1} ${json.cars ? json.cars[0].tariff : ''}`;
};
/**
* Returns an array of dates of all stored tickets
* @param {Array} tickets
* @returns {Array}
*/
var extractDates = function(tickets) {
var datesInStorage = _.map(_.uniqBy(tickets, 'date0'), function(json) {
// This date format is for parsing, do not attempt to localize
return moment(json.date0, 'DD.MM.YYYY').toDate();
});
return _.sortBy(datesInStorage);
};
/**
* Generates a roundtrip for each departure date, route and other options
* @param {Array} allTickets
* @returns {Promise}
*/
var generateIndex = function(allTickets) {
var cheapestTickets = findAllCheapestTicketsWithOptions(allTickets);
var roundtrips = extractRoundtrips(cheapestTickets);
// If no roundtrips found, do not overwrite existing roundtrips.
if (!roundtrips.length) {
throw new Error('No roundtrips found.');
}
return Promise.all([roundtrips, Storage.drop(collectionName)])
.then(function(result) {
var roundtrips = result[0];
// Converting dates to string and routes to string alias before persisting
return Storage.insert(collectionName, roundtrips);
});
};
/**
* Finds the cheapest ticket for every day, route and hours combination
* @param {Array} allTickets
* @returns {Array}
*/
var findAllCheapestTicketsWithOptions = function(allTickets) {
var allDates = extractDates(allTickets);
var entries = [];
var toMoscow = Route.toMoscow().toObject();
var toSpb = Route.toSpb().toObject();
var minCallback = function(ticket) {
return parseInt(ticket.cars[0].tariff);
};
_.forEach(allDates, function(date) {
var optionSet = [
{date: date, route: toMoscow, hours: hourAliases.earlyMorning},
{date: date, route: toMoscow, hours: hourAliases.morning},
{date: date, route: toMoscow, hours: hourAliases.day},
{date: date, route: toMoscow, hours: hourAliases.evening},
{date: date, route: toSpb, hours: hourAliases.earlyMorning},
{date: date, route: toSpb, hours: hourAliases.morning},
{date: date, route: toSpb, hours: hourAliases.day},
{date: date, route: toSpb, hours: hourAliases.evening}
];
_.forEach(optionSet, function(options) {
var filteredTickets = filterTickets(allTickets, options);
var cheapestTicket = _.minBy(filteredTickets, minCallback);
if (cheapestTicket) {
var time = cheapestTicket.time0.split(':');
entries.push(_.extend({
ticket: cheapestTicket.id,
datetime: moment(date).hours(parseInt(time[0])).minutes(parseInt(time[1])).toDate(),
price: parseInt(cheapestTicket.cars[0].tariff)
}, options));
}
});
});
return entries;
};
/**
* Finds the cheapest roundtrips for every day, route and hours combination
* @param {Array} cheapestTickets
* @returns {Array}
*/
var extractRoundtrips = function(cheapestTickets) {
var roundtrips = [];
// Morning and early morning and day tickets are assumed to be originating (outbound).
var originatingTickets = _.filter(cheapestTickets, function(ticket) {
return ticket.hours === hourAliases.earlyMorning || ticket.hours === hourAliases.morning || ticket.hours === hourAliases.day;
});
var lastAvailableDay = getLastAvailableDay();
// Find a return ticket for every originating ticket.
_.forEach(originatingTickets, function(originatingTicket, i) {
// Do not make a roundtrip if it is the last of available days (otherwise return ticket will not be available)
if (originatingTicket.date < lastAvailableDay) {
var originatingTicketMoment = moment(originatingTicket.date);
var roundtrip = {
originatingTicket: originatingTicket,
returnTicket: null,
totalCost: null,
route: originatingTicket.route,
// If originating ticket date is Saturday, mark roundtrip as weekend.
weekday: originatingTicketMoment.isoWeekday() === 6 ? weekdays.weekend : weekdays.weekday,
// Indicates if originating departure time is early in the morning.
originatingHours: originatingTicket.hours,
// Storing month to simplify filtering
month: originatingTicketMoment.month()
};
var returnOptions = {
date: moment(originatingTicket.date).add(1, 'days').toDate(),
hours: hourAliases.evening,
route: Route.getReversed(originatingTicket.route).toObject()
};
var returnTicket = _.find(cheapestTickets, returnOptions);
// Only store the roundtrip if return ticket is found
if (returnTicket) {
roundtrip.returnTicket = returnTicket;
roundtrip.totalCost = originatingTicket.price + returnTicket.price;
roundtrips.push(roundtrip);
} else {
debug('No ticket found with options', returnOptions);
}
}
});
return roundtrips;
};
var getAll = function() {
return Storage.find(collectionName)
.then(function(roundtrips) {
_.forEach(roundtrips, function(rountrip) {
rountrip.route = new Route(rountrip.route.to);
});
return roundtrips;
});
};
/**
* Returns the last of available days
* @see timespan
* @returns {Date}
*/
var getLastAvailableDay = function() {
return moment().add(timespan - 1, 'days').startOf('day').toDate();
};
/**
* Determines if the month is in timespan
* @param {Number} month Month number, starting from 0
* @returns {Boolean}
*/
var isMonthWithinTimespan = function(month) {
var now = moment();
// Current month, zero-based
var currentMonth = now.month();
var currentYear = now.year();
var nextYear = currentYear + 1;
// month + 1 because month is zero-based, but parsing requires regular-numbered months
var diffInDays = moment(month + 1, 'M').diff(now, 'days');
// See if timespan extends to the next year, add the number of days in current year to day difference (e.g., -362 + 366 = 4)
if (moment(nextYear, 'YYYY').diff(moment(), 'days') < timespan) {
diffInDays += moment(nextYear, 'YYYY').diff(moment(currentYear, 'YYYY'), 'days');
}
return month === currentMonth || (diffInDays <= timespan && diffInDays > 0);
};
/**
* Returns a list of months
* @param {Number} [excludeMonth]
* @returns {Number[]}
*/
var getMonthsWithinTimespan = function(excludeMonth) {
var currentMonth = moment().month();
// Making so that range would always start from the current month
var months = _.range(currentMonth, 12).concat(_.range(0, currentMonth));
return _.filter(months, function(month) {
var isExcluded = !_.isUndefined(excludeMonth) && excludeMonth === month;
return isMonthWithinTimespan(month) && !isExcluded;
});
};
/**
* @param {String|String[]} [excludeHours]
* @return {String[]} Hour names
*/
var getHourNames = function(excludeHours) {
excludeHours = excludeHours && !_.isArray(excludeHours) ? [excludeHours] : excludeHours;
var keys = _.difference(_.keys(hours), excludeHours);
return _.values(_.pick(hourNames, keys)) || [];
};
var remove = function() {
return Storage.remove(collectionName);
};
module.exports = {
cityAliases: cityAliases,
hourAliases: hourAliases,
hours: hours,
weekdays: weekdays,
timespan: timespan,
ticketsCountThreshold: ticketsCountThreshold,
defaultRoute: defaultRoute,
Route: Route,
getRequestOptions: getRequestOptions,
getTicketsForDate: getTicketsForDate,
formatTicket: formatTicket,
rzdDateRouteUrl: rzdDateRouteUrl,
formatRoundtrip: formatRoundtrip,
formatRoundtripTitle: formatRoundtripTitle,
filterTickets: filterTickets,
getTicketDepartureDate: getTicketDepartureDate,
getDayAfterTicket: getDayAfterTicket,
getDayBeforeTicket: getDayBeforeTicket,
getSummary: getSummary,
extractDates: extractDates,
generateIndex: generateIndex,
getAll: getAll,
getLastAvailableDay: getLastAvailableDay,
isMonthWithinTimespan: isMonthWithinTimespan,
getMonthsWithinTimespan: getMonthsWithinTimespan,
getHourNames: getHourNames,
remove: remove
};