UNPKG

asksuite-core

Version:
108 lines (88 loc) 2.85 kB
const requestPromise = require('request-promise-native'); module.exports = (config) => { const requester = {}; requester.getBookingRooms = async ( idHotel, checkin, checkout, adultos, criancas, cupom, childAges, otherParams, quotationIdentifier, ) => { try { const options = { uri: config.CRAWLER_ENDPOINT + '/generic/booking-pricing', qs: { idHotel, checkin, checkout, adultos, criancas, cupom, childAges, otherParams, quotationIdentifier, }, json: true, // Automatically parses the JSON string in the response }; const response = await requestPromise.get(options); return Promise.resolve(response); } catch (err) { console.log('caiu aqui no erro'); console.error(err.error); return Promise.reject(err.error); } }; const transformSearchEngineValueToNumbers = (item) => { const value = item.total; if (typeof value === 'number') { return value; } let strValue = String(value) .replace(/[^\d,.]/g, '') .replace('.', ',') .replace(/([.,])(\d{1,2})$/, (match, p1, p2) => '.' + p2) .replace(',', ''); const splitted = strValue.split(/[,.]/); if (splitted.length > 2) { strValue = splitted.reduce((c, it, index, array) => { const separator = index === splitted.length - 1 ? '.' : ''; return c + separator + it; }, ''); } return Number(strValue); }; const calculatePercentage = (minPriceBooking, minPriceEngine) => { const difference = minPriceBooking - minPriceEngine; const division = difference / minPriceBooking; return division * 100; }; requester.calculateDiscountAgainstBooking = (bookingPrices, searchEnginePrices) => { const bookingPricesFilteredPrices = bookingPrices.map((item) => { return item.total; }).filter((total) => total > 0); const searchEngineFilteredPrices = searchEnginePrices .map(transformSearchEngineValueToNumbers) .filter((total) => total > 0); if (!bookingPricesFilteredPrices.length || !searchEngineFilteredPrices.length) { return false; } const minPriceBooking = Math.min(...bookingPricesFilteredPrices); // Simulates higher price to show card at chat please comment the other minPriceBooking // const minPriceBooking = 1300; const minPriceEngine = Math.min(...searchEngineFilteredPrices); if (minPriceBooking <= minPriceEngine) { return false; } else { if ((minPriceEngine - minPriceBooking) * -1 < 1) { return false; } const resultPercentage = calculatePercentage(minPriceBooking, minPriceEngine); return resultPercentage; } }; return requester; };