UNPKG

3.48 kBJavaScriptView Raw
1const requestPromise = require('request-promise-native');
2const Global = require('./global');
3
4module.exports = function(config) {
5 const requester = {};
6
7 const AsksuiteService = require('./infochat.service')(config);
8
9 requester.search = async function(
10 type,
11 idHotel,
12 checkin,
13 checkout,
14 adultos,
15 criancas,
16 cupom,
17 childAges,
18 otherParams,
19 quotationIdentifier,
20 ) {
21 try {
22 //vai buscar no nosso banco de dados os motores registrados com o type X;
23 const BookingEngine = await AsksuiteService.findBookingEngineByType(type);
24
25 const options = {
26 uri: Global.CRAWLER_ENDPOINT + '/generic/' + BookingEngine.urlPath,
27 qs: {
28 idHotel: idHotel,
29 checkin: checkin,
30 checkout: checkout,
31 adultos: adultos,
32 criancas: criancas,
33 cupom: cupom,
34 childAges: childAges,
35 otherParams: otherParams,
36 quotationIdentifier: quotationIdentifier,
37 },
38 json: true, // Automatically parses the JSON string in the response
39 };
40
41 const response = await requestPromise.get(options);
42
43 return Promise.resolve(response);
44 } catch (err) {
45 console.log('caiu aqui no erro');
46 console.error(err.error);
47 return Promise.reject(err.error);
48 }
49 };
50
51 requester.searchByMultipleCompanies = async function(
52 checkin,
53 checkout,
54 adultos,
55 criancas,
56 childAges,
57 dataHotels,
58 ) {
59 return new Promise((resolve, reject) => {
60 Promise.all(
61 dataHotels.map(({ companyId, company, idHotel, type, code, otherParams, dialog }) => {
62 return requester
63 .search(
64 type,
65 idHotel,
66 checkin,
67 checkout,
68 adultos,
69 criancas,
70 code,
71 childAges,
72 otherParams,
73 )
74 .then(data => {
75 return {
76 companyId,
77 company,
78 data,
79 dialog,
80 };
81 })
82 .catch(error => {
83 return {
84 companyId,
85 company,
86 error,
87 dialog,
88 };
89 });
90 }),
91 )
92 .then(data => {
93 resolve(data);
94 })
95 .catch(error => {
96 console.log('error', error);
97 reject(error);
98 });
99 });
100 };
101
102 requester.getSuggestionDates = async function(
103 type,
104 idHotel,
105 checkin,
106 checkout,
107 adultos,
108 criancas,
109 cupom,
110 childAges,
111 otherParams,
112 ) {
113 try {
114 const BookingEngine = await AsksuiteService.findBookingEngineByType(type);
115
116 const options = {
117 uri: Global.CRAWLER_ENDPOINT + '/generic/generic/suggestion',
118 qs: {
119 idHotel: idHotel,
120 checkin: checkin,
121 checkout: checkout,
122 adultos: adultos,
123 criancas: criancas,
124 cupom: cupom,
125 childAges: childAges,
126 otherParams: otherParams,
127 crawlerName: BookingEngine.urlPath,
128 },
129 json: true, // Automatically parses the JSON string in the response
130 };
131
132 const response = await requestPromise.get(options);
133
134 return Promise.resolve(response);
135 } catch (err) {
136 console.log('caiu aqui no erro');
137 console.error(err.error);
138 return Promise.reject(err.error);
139 }
140 };
141
142 return requester;
143};