forked from minamhere/southwest-price-drop-bot
-
Notifications
You must be signed in to change notification settings - Fork 41
/
alert.js
151 lines (129 loc) · 4.96 KB
/
alert.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
const PN = require('google-libphonenumber');
const cloneDeep = require('lodash.clonedeep');
const shortid = require('shortid');
const { getPriceForFlight } = require('./get-price.js');
const { ALERT_TYPES } = require('../constants.js');
class Alert {
constructor (data) {
if (data == null) { return; }
this.data = typeof data === 'string' ? JSON.parse(data) : cloneDeep(data);
this.data.id = this.data.id || shortid.generate();
this.data.from = this.data.from.toLocaleUpperCase();
this.data.to = this.data.to.toLocaleUpperCase();
if (this.data.isPointsBooking) {
this.data.isPointsBooking = this.data.isPointsBooking;
} else if (this.data.bookingType) {
if (this.data.bookingType === 'cash') {
this.data.isPointsBooking = false;
} else if (this.data.bookingType === 'points') {
this.data.isPointsBooking = true;
} else {
console.warn('Unexpected booking type, defaulting to cash: ' + this.data.bookingType);
this.data.isPointsBooking = false;
}
} else {
this.data.isPointsBooking = false;
}
try {
this.data.number = this.data.number != null && this.data.number !== 'NaN' ? this.data.number.split(',').map(n => n.trim()).filter(n => n.length).join(',') : 'NaN';
} catch (err) {
console.log(typeof (this.data.number) + this.data.number);
throw (err);
}
this.data.originalPrice = this.data.price ? parseInt(this.data.price, 10) : null;
this.data.phone = this.data.phone ? this.data.phone.split('').filter(d => /\d/.test(d)).join('') : null;
this.data.toEmail = this.data.toEmail ? this.data.toEmail.split('').filter(d => /\S/.test(d)).join('') : null;
this.data.priceHistory = Alert.compactPriceHistory(this.data.priceHistory || []);
this.data.alertType = this.data.alertType != null ? this.data.alertType : ALERT_TYPES.SINGLE; // If there's no alertType, assume Single
this.data.fetchingPrices = this.data.fetchingPrices || false;
}
get user () { return this.data.user; }
get id () { return this.data.id; }
get date () { return new Date(this.data.date); }
get from () { return this.data.from; }
get to () { return this.data.to; }
get isPointsBooking () { return this.data.isPointsBooking; }
get number () { return this.data.number; }
get price () { return this.data.originalPrice; }
get phone () { return this.data.phone; }
get toEmail () { return this.data.toEmail; }
get priceHistory () { return this.data.priceHistory; }
get alertType () { return this.data.alertType; }
get fetchingPrices () { return this.data.fetchingPrices; }
get formattedDate () {
return this.date.toLocaleDateString('en-US', { timeZone: 'UTC' });
}
get formattedNumber () {
return 'WN ' + this.number.split(',').join(', ');
}
get formattedPhone () {
if (this.data.phone == null) { return null; }
if (this.data.phone === '') { return ''; }
const phoneUtil = PN.PhoneNumberUtil.getInstance();
const number = phoneUtil.parse(this.data.phone, 'US');
return phoneUtil.format(number, PN.PhoneNumberFormat.INTERNATIONAL);
}
get formattedEmail () {
return this.data.toEmail;
}
get formattedPrice () {
if (this.data.isPointsBooking) {
return this.data.price + ' points';
} else {
return '$' + this.data.price;
}
}
get formattedLatestPrice () {
if (this.data.isPointsBooking) {
return this.latestPrice + ' points';
} else {
return '$' + this.latestPrice;
}
}
get formattedPriceDifference () {
const diff = this.data.price - this.latestPrice;
if (this.data.isPointsBooking) {
return diff + ' points';
} else {
return '$' + diff;
}
}
get latestPrice () {
const ph = this.data.priceHistory;
return ph.length ? (ph[ph.length - 1]).price : Infinity;
}
get priceHasDropped () {
return this.latestPrice < this.price;
}
get signature () {
return [ this.formattedDate, this.from, this.to, this.number === 'NaN' ? 'All' : this.number ].join('|');
}
toJSON () {
return JSON.stringify(this.data);
}
key (namespace = 'alert') {
return `${namespace}.${this.id}`;
}
async getLatestPrice (browser = undefined, lock = undefined) {
const time = Date.now();
const price = await getPriceForFlight(this, browser, lock);
const entry = { time, price };
console.log('Got price:', this.signature, entry);
if (price < Infinity) this.priceHistory.push(entry);
if (!this.originalPrice) this.originalPrice = this.price;
this.data.fetchingPrices = false;
return price;
}
static compactPriceHistory (history) {
const compact = [];
for (let i = 0; i < history.length; i++) {
const p = i - 1;
const n = i + 1;
const prevDifferent = !history[p] || history[i].price !== history[p].price;
const nextDifferent = !history[n] || history[i].price !== history[n].price;
if (prevDifferent || nextDifferent) compact.push(history[i]);
}
return compact;
}
}
module.exports = Alert;