Skip to content

Commit

Permalink
Include ts code and remove hotel rates merging logic (#127)
Browse files Browse the repository at this point in the history
* Remove hotel rates merger logic

* Add logging of rate count

* Revert rate merger changes

* Change ways of passing extra body params to metasearch

* Update build

* Remove merging of rate based on provider logic

* Renmove comments

* Include ts code in HotelDetailsClient.js

* Bump version

* Remove commented code
  • Loading branch information
XavierEr authored Nov 2, 2021
1 parent 247182f commit a46a047
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 378 deletions.
2 changes: 1 addition & 1 deletion dist/WegoSdk.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wego-sdk",
"version": "1.4.4",
"version": "1.4.6",
"description": "Wego SDK",
"main": "dist/WegoSdk.js",
"files": [
Expand Down
9 changes: 5 additions & 4 deletions src/Poller.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ Poller.prototype = {
},

preparePoll: function () {
var self = this;
if (this.pollCount < this.delays.length && !this.forceStop) {
this.timer = setTimeout(function () {
const self = this;

if (self.pollCount < self.delays.length && !self.forceStop && !self.isLastPolling()) {
self.timer = setTimeout(function () {
self.poll();
}, this.delays[this.pollCount]);
}, self.delays[self.pollCount]);
}
},

Expand Down
6 changes: 3 additions & 3 deletions src/hotel-details/HotelDetailsClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class HotelDetailsClient {
self.nearbyHotels = options.nearbyHotels || {};
self.hotelDetailsEndpointUrl = hotelDetailsEndpointUrl;
self.extraSearchRequestBody = options.extraSearchRequestBody;
self.bowOnly = options.bowOnly;
self.extraBodyParams = options.extraBodyParams;

self.poller = new Poller({
delays: DELAYS,
Expand Down Expand Up @@ -111,7 +111,7 @@ class HotelDetailsClient {
const currencyCode = currency.code;
const locale = self.locale;
const extraSearchRequestBody = self.extraSearchRequestBody || {};
const bowOnly = self.bowOnly;
const extraBodyParams = self.extraBodyParams;

const searchRequestBody = {
search: {
Expand All @@ -129,7 +129,7 @@ class HotelDetailsClient {
userLoggedIn: self.userLoggedIn
},
includeDirect: true,
...(!!bowOnly && { bowOnly })
...(!!extraBodyParams && extraBodyParams),
};

if (self.searchId !== undefined) {
Expand Down
12 changes: 6 additions & 6 deletions src/hotel-search/HotelSearchClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class HotelSearchClient {
self.shortlistedHotelIds = options.shortlistedHotelIds || [];
self.onGroupSearchChanged = options.onGroupSearchChanged || function () { };
self.extraSearchRequestBody = options.extraSearchRequestBody;
self.bowOnly = options.bowOnly;
self.extraBodyParams = options.extraBodyParams;

self.merger = new HotelSearchMerger();
self.poller = new Poller({
Expand Down Expand Up @@ -77,9 +77,9 @@ class HotelSearchClient {
}

mergeResponse(response) {
let self = this;
let isSearchEnd = response.done || self.poller.isLastPolling()
self.merger.mergeResponse(response, isSearchEnd);
const self = this;

self.merger.mergeResponse(response);
self.lastRatesCount = response.count;
self.responseSearch = response.search;
dataUtils.prepareResponseSearch(self.responseSearch, self.merger.getStaticData());
Expand Down Expand Up @@ -143,7 +143,7 @@ class HotelSearchClient {
const extraSearchRequestBody = self.extraSearchRequestBody || {};
const selectedHotelIds = dataUtils.trimArray(self.selectedHotelIds);
const shortlistedHotelIds = self.shortlistedHotelIds;
const bowOnly = self.bowOnly;
const extraBodyParams = self.extraBodyParams;

const searchParams = {
search: {
Expand All @@ -165,7 +165,7 @@ class HotelSearchClient {
userLoggedIn: self.userLoggedIn,
},
includeDirect: true, // to show Book on Wego rates
...(!!bowOnly && { bowOnly }),
...(!!extraBodyParams && extraBodyParams),
rateAmenityIds: self.rateAmenityIds,
offset: self.lastRatesCount
};
Expand Down
72 changes: 7 additions & 65 deletions src/hotel-search/Merger.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ HotelSearchClient.prototype = {
this.__filterOptionsMap = this._getEmptyFilterOptionsMap();
},

mergeResponse: function (response, isSearchEnd = false) {
mergeResponse: function (response) {
var hotelIds = this._getUpdatedHotelIds(response);

this._mergeStaticData(response);
this._mergeHotels(response.hotels);
this._mergeFilter(Object.assign({}, response.rentalFilter, response.filter), response.providers); // Has to be in this sequence because rentalFilter contains airbnb minPrice and maxPrice which is to be overiden by filter.
this._mergeRates(response.rates, isSearchEnd);
this._mergeRates(response.rates);
this._mergeSortedRatesByBasePrice();
this._mergeSortedRatesByTotalPrice();
this._mergeScores(response.scores);
Expand Down Expand Up @@ -99,36 +99,20 @@ HotelSearchClient.prototype = {
});
},

_mergeRates: function (newRates, isSearchEnd) {
_mergeRates: function (newRates) {
if (!newRates) return;
var self = this;

newRates.forEach(function (newRate) {
dataUtils.prepareRate(newRate, self.currency, self.__staticData);

var hotelId = newRate.hotelId;
var hotel = self.__hotelMap[hotelId];
if (!hotel) return;
var rates = hotel.rates;

var i;
for (i = 0; i < rates.length; i++) {
if (dataUtils.isBetterRate(newRate, rates[i])) break;
if (newRate.providerCode === rates[i].providerCode) return;
}
rates.splice(i, 0, newRate);

i++;
for (; i < rates.length; i++) {
if (newRate.providerCode === rates[i].providerCode) {
rates.splice(i, 1);
break;
}
}
if (!!hotel) {
hotel.rates = [...hotel.rates, newRate];
};
});

if (isSearchEnd) {
this._lastMergeRates(newRates);
}
},

_mergeSortedRatesByBasePrice: function () {
Expand Down Expand Up @@ -181,48 +165,6 @@ HotelSearchClient.prototype = {
}
},

_lastMergeRates: function (newRates) {
if (!newRates) return;
var self = this;

var singlePartnerHotels = {};
for (var hotelId in self.__hotelMap) {
if (self.__hotelMap[hotelId].rates && self.__hotelMap[hotelId].rates.length === 1) {
singlePartnerHotels[hotelId] = true;
}
}

var hotelIdToNewRatesMap = {};
for (var i in newRates) {
var rate = newRates[i];
if (singlePartnerHotels[rate.hotelId]) {
if (!hotelIdToNewRatesMap[rate.hotelId]) {
hotelIdToNewRatesMap[rate.hotelId] = [];
}
var hotelOrderedRates = hotelIdToNewRatesMap[rate.hotelId];
var index;
for (index = 0; index < hotelOrderedRates.length; index++) {
if (dataUtils.isBetterRate(rate, hotelOrderedRates[index])) {
break;
}
}
hotelOrderedRates.splice(index, 0, rate);
}
}

for (var hotelId in singlePartnerHotels) {
var currentBestRate = self.__hotelMap[hotelId].rates[0];
var hotelOrderedRates = hotelIdToNewRatesMap[hotelId];
if (!hotelOrderedRates) {
continue;
}
if (hotelOrderedRates[0].id !== currentBestRate.id) {
hotelOrderedRates.splice(0, 0, currentBestRate);
}
self.__hotelMap[hotelId].rates = hotelOrderedRates;
}
},

_mergeScores: function (scores) {
if (!scores) return;
var hotelMap = this.__hotelMap;
Expand Down
37 changes: 0 additions & 37 deletions src/hotel-search/dataUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,43 +108,6 @@ module.exports = {
return value;
},

isBetterRate: function (firstRate, secondRate) {
function processRateAmount(rate) {
var amount = Math.round(rate.price.amount);
if (amount > 99999) {
amount = (amount / 100) * 100;
}
return amount;
}

function getPriceTaxAmountInclusive(rate) {
return rate.price.taxAmountUsd < 0 ? -1 : 1;
}

var firstTax = getPriceTaxAmountInclusive(firstRate);
var secondTax = getPriceTaxAmountInclusive(secondRate);

if (firstTax != secondTax) return firstTax > secondTax;

var firstRateAmount = processRateAmount(firstRate);
var secondRateAmount = processRateAmount(secondRate);
if (firstRateAmount != secondRateAmount) return firstRateAmount < secondRateAmount;

if (firstRate.provider.directBooking && !secondRate.provider.directBooking) {
return true;
} else if (secondRate.provider.directBooking && !firstRate.provider.directBooking) {
return false;
}

if (firstRate.provider.type == 'DIRECT_PRIORITY' && secondRate.provider.type != 'DIRECT_PRIORITY') {
return true;
} else if (secondRate.provider.type == 'DIRECT_PRIORITY' && firstRate.provider.type != 'DIRECT_PRIORITY') {
return false;
}

return firstRate.price.ecpc > secondRate.price.ecpc;
},

__filterOptionTypeToStaticDataType: {
stars: 'stars',
brands: 'brands',
Expand Down
9 changes: 4 additions & 5 deletions test/hotel-search/HotelSearchClient.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ describe("HotelSearchClient", function () {
expect(client.merger.__staticData.brands[1]).to.equal(brand);
});

it("no hotel can have more than 1 rate from a provider when search's status is not'done'", () => {
it("rates should be assigned to respective hotel based on hotelId", () => {
var response = {
done: false,
hotels: [{ id: 1 }, { id: 2 }],
Expand All @@ -399,11 +399,10 @@ describe("HotelSearchClient", function () {
client.poller.pollCount = 1;
client.poller.pollLimit = 3;
client.handleSearchResponse(response);
expect(client.merger.__hotelMap[1].rates.length).to.equal(1);
expect(client.merger.__hotelMap[1].rates.length).to.equal(3);
expect(client.merger.__hotelMap[2].rates.length).to.equal(2);
expect(client.merger.__hotelMap[2].rates[0].providerCode).to.not.equal(
client.merger.__hotelMap[2].rates[1].providerCode);
});

it("some hotels may have more than 1 from a provider rate when search's status is not 'done' but reaching limit polling time", () => {
var response = {
done: true,
Expand Down Expand Up @@ -577,7 +576,7 @@ describe("HotelSearchClient", function () {
var params = client.fetchHotelsParams();
expect(params.currencyCode).to.equal(currencyCode);
});
it("returns moreRates", function() {
it("returns moreRates", function () {
var params = client.fetchHotelsParams();
expect(params.moreRates).to.equal(true);
});
Expand Down
95 changes: 1 addition & 94 deletions test/hotel-search/Merger.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,107 +301,14 @@ describe('Merger', function() {
hotels: [hotel],
});

var oldHotels = merger.getHotels();
var oldHotel = oldHotels[0];

merger.mergeResponse({
rates: [rate3]
});

var newHotels = merger.getHotels();
var newHotel = newHotels[0];

expect(getRateIds(newHotel.rates)).to.deep.equal([2, 3, 1]);
expect(newHotel).to.not.equal(oldHotel);
expect(newHotels).to.not.equal(oldHotels);
});

it('update with better rate', function() {
var rate1 = {
id: 1,
hotelId: 1,
providerCode: 'x',
price: {
amount: 100,
}
};

var rate2 = {
id: 2,
hotelId: 1,
providerCode: 'y',
price: {
amount: 70,
}
};

var rate3 = {
id: 3,
hotelId: 1,
providerCode: 'y',
price: {
amount: 60,
}
};

var hotel = {
id: 1,
};

merger.mergeResponse({
rates: [rate1, rate2],
hotels: [hotel],
});

merger.mergeResponse({
rates: [rate3]
});

expect(getRateIds(merger.__hotelMap[1].rates)).to.deep.equal([3, 1]);
});

it('not update with worse rate', function() {
var rate1 = {
id: 1,
hotelId: 1,
providerCode: 'x',
price: {
amount: 100,
}
};

var rate2 = {
id: 2,
hotelId: 1,
providerCode: 'y',
price: {
amount: 70,
}
};

var rate3 = {
id: 3,
hotelId: 1,
providerCode: 'y',
price: {
amount: 120,
}
};

var hotel = {
id: 1,
};

merger.mergeResponse({
rates: [rate1, rate2],
hotels: [hotel],
});

merger.mergeResponse({
rates: [rate3]
});

expect(getRateIds(merger.__hotelMap[1].rates)).to.deep.equal([2, 1]);
expect(getRateIds(newHotel.rates)).to.deep.equal([1, 2, 3]);
});

it("some hotel may have more than 1 from a provider rate when search's is end", () => {
Expand Down
Loading

0 comments on commit a46a047

Please sign in to comment.