-
Notifications
You must be signed in to change notification settings - Fork 8
/
hotelAvailabilityModel.js
105 lines (95 loc) · 2.73 KB
/
hotelAvailabilityModel.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
const axios = require('axios');
const fileHelper = require('./fileHelper');
function ComputeRequestPayload(searchCriteria) {
const requestPayload = {
GetHotelAvailRQ: {
SearchCriteria: {
OffSet: 1,
SortBy: 'TotalRate',
SortOrder: 'DESC',
PageSize: 20,
TierLabels: false,
GeoSearch: {
GeoRef: {
Radius: 50,
UOM: 'MI',
RefPoint: {
Value: searchCriteria.location.airportCode,
ValueContext: 'CODE',
RefPointType: '6',
},
},
},
RateInfoRef: {
ConvertedRateInfoOnly: false,
CurrencyCode: 'USD',
BestOnly: '2',
PrepaidQualifier: 'IncludePrepaid',
StayDateRange: {
StartDate: searchCriteria.date.checkIn,
EndDate: searchCriteria.date.checkOut,
},
Rooms: {
Room: [
{
Index: 1,
Adults: 1,
Children: 0,
},
],
},
InfoSource: '100,110,112,113',
},
HotelPref: {
SabreRating: {
Min: '3',
Max: '5',
},
},
ImageRef: {
Type: 'MEDIUM',
LanguageCode: 'EN',
},
},
},
};
return JSON.stringify(requestPayload);
}
class HotelAvailabilityModel {
constructor(params) {
this.searchCriteria = params.searchCriteria;
this.apiAccessToken = params.apiAccessToken;
this.appId = params.appId;
this.apiEndPoint = params.apiEndPoint;
}
get results() {
let rc = this.searchResponse.GetHotelAvailRS.HotelAvailInfos;
if (!rc) {
rc = this.searchResponse.GetHotelAvailRS;
}
return rc;
}
async read() {
try {
const response = await axios({
method: 'post',
url: `${this.apiEndPoint}/v2.1.0/get/hotelavail`,
data: ComputeRequestPayload(this.searchCriteria),
headers: {
'content-type': 'application/json',
accept: 'application/json',
authorization: `Bearer ${this.apiAccessToken}`,
'Application-ID': this.appId,
},
});
this.searchResponse = response.data;
fileHelper.writeData(JSON.stringify(this.searchResponse), './cachedResponse.json');
} catch (error) {
console.log('\nUnexpected error calling hotel get availability.');
console.log(`[${error.response.status}] ... [${error.response.statusText}]`);
console.log(`[${error.response.data.errorCode}] ... [${error.response.data.message}]`);
fileHelper.writeData(JSON.stringify(error.response.data), './cachedResponse.json');
}
}
}
module.exports = HotelAvailabilityModel;