You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
2.4 KiB
JavaScript
88 lines
2.4 KiB
JavaScript
const crypto = require('crypto');
|
|
const axios = require('axios');
|
|
const { get, post } = axios;
|
|
|
|
const { HEYTRIP_API, HEYTRIP_API_PROD } = require('./../config/constants');
|
|
const { chunk, isEmpty } = require('../utils/commons');
|
|
|
|
const make_token = () => {
|
|
var apiKey = '18daad53d0ec4003a207c41ddaf63b78';
|
|
var secret = 'f76e547e55964812bf94cc0d31f74333';
|
|
var timestamp = Math.round(new Date().getTime() / 1000);
|
|
var hash = crypto
|
|
.createHash('sha512')
|
|
.update(apiKey + secret + timestamp)
|
|
.digest('hex');
|
|
var authHeaderValue = 'Bearer apikey=' + apiKey + ',signature=' + hash + ',timestamp=' + timestamp;
|
|
return authHeaderValue;
|
|
};
|
|
const AvailableAccommodationIds = async (pageIndex) => {
|
|
const response = await get(`${HEYTRIP_API_PROD}/AvailableAccommodationIds?pageIndex=${pageIndex}`, {
|
|
headers: {
|
|
Authorization: make_token(),
|
|
},
|
|
});
|
|
console.log('Call pageIndex', pageIndex, 'totalPage', response.data.TotalPage);
|
|
return response.data.Data || [];
|
|
};
|
|
|
|
const AccommodationsDetails = async (body) => {
|
|
if (isEmpty(body.AccommodationIds)) {
|
|
return [];
|
|
}
|
|
const response = await post(
|
|
`${HEYTRIP_API_PROD}/AccommodationsDetails`,
|
|
{ ...body },
|
|
{
|
|
headers: {
|
|
Authorization: make_token(),
|
|
},
|
|
}
|
|
);
|
|
return Object.values(response.data.Data || {});
|
|
};
|
|
|
|
const Availability = async (body) => {
|
|
// console.log('Call Heytrip');
|
|
const response = await post(
|
|
`${HEYTRIP_API_PROD}/Availability`,
|
|
{ ...body },
|
|
{
|
|
headers: {
|
|
Authorization: make_token(),
|
|
},
|
|
}
|
|
);
|
|
// console.log(response.config);
|
|
return response.data.Data || [];
|
|
};
|
|
|
|
const QuotedHotelsPrice = async (body) => {
|
|
const idsChunk = body.hotelIds.length > 10 ? chunk(body.hotelIds, 10) : [body.hotelIds];
|
|
let quoteRes = [];
|
|
for await (const piece of idsChunk) {
|
|
const response = await post(
|
|
`${HEYTRIP_API_PROD}/QuotedHotelsPrice`,
|
|
{ ...body, hotelIds: piece },
|
|
{
|
|
headers: {
|
|
Authorization: make_token(),
|
|
},
|
|
}
|
|
);
|
|
quoteRes = quoteRes.concat(response.data.Data || []);
|
|
}
|
|
return quoteRes;
|
|
};
|
|
|
|
const Country = async (param) => {};
|
|
|
|
const City = async (param) => {};
|
|
|
|
module.exports = {
|
|
AvailableAccommodationIds,
|
|
AccommodationsDetails,
|
|
Availability,
|
|
QuotedHotelsPrice,
|
|
};
|