UNPKG

8.95 kBJavaScriptView Raw
1const Util = require('./util');
2const request = require('request');
3const mapCacheBookingEngine = new Map();
4
5module.exports = function(config) {
6 const bookingEngineCacheWrapper = Util.fallbackCacheFactory(mapCacheBookingEngine);
7
8 const requester = {};
9
10 requester.fetchAndCacheBookingEngines = function() {
11 console.log('listing booking engines');
12 return requester.listBookingEngines().then(bookingengines => {
13 bookingengines.forEach(bookingEngine => {
14 mapCacheBookingEngine.set(bookingEngine.type, bookingEngine);
15 });
16
17 console.log('cached booking engines');
18 });
19 };
20
21 function checkStatus(res, body, resolve, reject) {
22 if (res.statusCode >= 200 && res.statusCode < 300) {
23 resolve(body);
24 } else {
25 reject(body);
26 }
27 }
28
29 requester.list = function() {
30 const promise = new Promise(function(resolve, reject) {
31 request.get(
32 {
33 url: config.INFOCHAT_DATA_SERVER + '/chattrees',
34 json: true,
35 headers: {
36 Authorization: config.USER_TOKEN,
37 },
38 },
39 function(err, httpResponse, body) {
40 if (!err) {
41 console.log('######################');
42 //console.log(body);
43
44 checkStatus(httpResponse, body, resolve, reject);
45 } else {
46 console.log('erro ao listar dados');
47 reject(err);
48 }
49 },
50 );
51 });
52
53 return promise;
54 };
55
56 requester.listCompanies = function() {
57 const promise = new Promise(function(resolve, reject) {
58 request.get(
59 {
60 url: config.INFOCHAT_DATA_SERVER + '/companies',
61 json: true,
62 headers: {
63 Authorization: config.USER_TOKEN,
64 },
65 },
66 function(err, httpResponse, body) {
67 if (!err) {
68 checkStatus(httpResponse, body, resolve, reject);
69 } else {
70 console.log('erro ao listar dados');
71 reject(err);
72 }
73 },
74 );
75 });
76
77 return promise;
78 };
79
80 requester.listBookingEngines = function() {
81 const promise = new Promise(function(resolve, reject) {
82 request.get(
83 {
84 url: config.INFOCHAT_DATA_SERVER + '/bookingengines',
85 json: true,
86 headers: {
87 Authorization: config.USER_TOKEN,
88 },
89 },
90 function(err, httpResponse, body) {
91 if (!err) {
92 checkStatus(httpResponse, body, resolve, reject);
93 } else {
94 console.log('erro ao listar motores');
95 reject(err);
96 }
97 },
98 );
99 });
100
101 return promise;
102 };
103
104 requester.listLanguages = function() {
105 return new Promise(function(resolve, reject) {
106 request.get(
107 {
108 url: config.INFOCHAT_DATA_SERVER + '/languages',
109 json: true,
110 headers: {
111 Authorization: config.USER_TOKEN,
112 },
113 },
114 function(err, httpResponse, body) {
115 if (!err) {
116 checkStatus(httpResponse, body, resolve, reject);
117 } else {
118 console.log('erro ao listar languages');
119 reject(err);
120 }
121 },
122 );
123 });
124 };
125
126 requester.findCompany = function(id) {
127 const promise = new Promise(function(resolve, reject) {
128 request.get(
129 {
130 url: config.INFOCHAT_DATA_SERVER + '/companies/' + id,
131 json: true,
132 },
133 function(err, httpResponse, body) {
134 if (!err) {
135 checkStatus(httpResponse, body, resolve, reject);
136 } else {
137 console.log('erro ao listar dados');
138 reject(err);
139 }
140 },
141 );
142 });
143
144 return promise;
145 };
146
147 requester.findTree = function(id) {
148 const promise = new Promise(function(resolve, reject) {
149 request.get(
150 {
151 url: config.INFOCHAT_DATA_SERVER + '/chattrees/' + id,
152 json: true,
153 headers: {
154 Authorization: config.USER_TOKEN,
155 },
156 },
157 function(err, httpResponse, body) {
158 if (!err) {
159 checkStatus(httpResponse, body, resolve, reject);
160 } else {
161 console.log('erro ao listar dados');
162 reject(err);
163 }
164 },
165 );
166 });
167
168 return promise;
169 };
170
171 requester.findTreeByCompanyId = function(treeId) {
172 const promise = new Promise(function(resolve, reject) {
173 request.get(
174 {
175 url: config.INFOCHAT_DATA_SERVER + '/chattrees/' + treeId + '/findByCompanyId',
176 json: true,
177 headers: {
178 Authorization: config.USER_TOKEN,
179 },
180 },
181 function(err, httpResponse, body) {
182 if (!err) {
183 checkStatus(httpResponse, body, resolve, reject);
184 } else {
185 console.log('erro ao listar dados');
186 reject(err);
187 }
188 },
189 );
190 });
191
192 return promise;
193 };
194
195 requester.listKeywordsExpressions = function() {
196 return new Promise(function(resolve, reject) {
197 request.get(
198 {
199 url: config.INFOCHAT_DATA_SERVER + '/keywordintents',
200 json: true,
201 },
202 function(err, httpResponse, body) {
203 if (!err) {
204 checkStatus(httpResponse, body, resolve, reject);
205 } else {
206 console.log('erro ao listar dados');
207 reject(err);
208 }
209 },
210 );
211 });
212 };
213
214 requester.findBookingEngineByType = bookingEngineCacheWrapper(function(type) {
215 return new Promise(function(resolve, reject) {
216 request.get(
217 {
218 url: config.INFOCHAT_DATA_SERVER + '/bookingengines/findByType/' + type,
219 json: true,
220 headers: {
221 Authorization: config.USER_TOKEN,
222 },
223 },
224 function(err, httpResponse, body) {
225 if (!err) {
226 checkStatus(httpResponse, body, resolve, reject);
227 } else {
228 console.log('erro ao listar dados');
229 reject(err);
230 }
231 },
232 );
233 });
234 });
235
236 requester.finishConversation = function(conversationId) {
237 return Util.executeRequestAndSaveOnError(
238 {
239 request: {
240 url: config.INFOCHAT_DATA_SERVER + '/conversation/finishConversation/' + conversationId,
241 headers: {
242 Authorization: config.USER_TOKEN,
243 },
244 },
245 type: 'conversations',
246 },
247 config,
248 )
249 .then(function(body) {
250 console.log('finishedconversation');
251 return body;
252 })
253 .catch(err => {
254 console.log('erro ao realizar o finish');
255 throw err;
256 });
257 };
258
259 requester.markRequestPrice = function(conversationId, isTest = false, companyId = null) {
260 if (isTest) {
261 return Promise.resolve();
262 }
263
264 return Util.executeRequestAndSaveOnError(
265 {
266 request: {
267 url: `${config.INFOCHAT_DATA_SERVER}/conversation/setRequestedPrice/${conversationId}/${companyId}`,
268 method: 'POST',
269 headers: {
270 Authorization: config.USER_TOKEN,
271 },
272 },
273 type: 'conversations',
274 },
275 config,
276 )
277 .then(function(body) {
278 console.log('mark conversation request price', conversationId);
279 return body;
280 })
281 .catch(err => {
282 console.log('erro ao realizar o mark conversation request price');
283 throw err;
284 });
285 };
286
287 requester.registerQuote = function(searchData, isTest = false) {
288 return new Promise((resolve, reject) => {
289 const splittedCompanyId = searchData.companyId.split('.');
290 searchData.companyId =
291 splittedCompanyId.length > 1 ? splittedCompanyId[1] : splittedCompanyId[0];
292
293 if (isTest) {
294 resolve('test chat');
295 } else {
296 const [arrivalDate, arrivalTime] = Util.getDateAndTimeFromDateISO(searchData.arrivalDate);
297 const [departureDate, departureTime] = Util.getDateAndTimeFromDateISO(
298 searchData.departureDate,
299 );
300
301 searchData.arrivalTime = arrivalTime;
302 searchData.arrivalDate = Util.appendTimeISOStringToDate(arrivalDate, '06');
303
304 searchData.departureTime = departureTime;
305 searchData.departureDate = Util.appendTimeISOStringToDate(departureDate, '06');
306
307 if (searchData.searchResult) {
308 searchData = Object.assign(searchData, Util.getPriceExtremes(searchData.searchResult));
309 }
310
311 Util.executeRequestAndSaveOnError(
312 {
313 request: {
314 url: config.INFOCHAT_DATA_SERVER + '/conversation/registerQuote',
315 method: 'POST',
316 json: searchData,
317 headers: {
318 Authorization: config.USER_TOKEN,
319 },
320 },
321 type: 'quotes',
322 },
323 config,
324 )
325 .then(body => resolve(body))
326 .catch(reject);
327 }
328 });
329 };
330
331 return requester;
332};