|
|
const BaseController = require('./BaseController');
|
|
|
const heytripService = require('../services/heytripService');
|
|
|
const { QuotedHotelsPrice } = require('../vendor/heytrip');
|
|
|
|
|
|
class Api extends BaseController {
|
|
|
|
|
|
hotelSearch = async(ctx) => {
|
|
|
const { keyword, checkin, checkout, adults, rooms } = ctx.query;
|
|
|
const page = ctx.query.page || 1;
|
|
|
const size = ctx.query.pagesize || 10;
|
|
|
try {
|
|
|
// if (!keyword || !checkin || !checkout) {
|
|
|
if (!keyword) {
|
|
|
ctx.throw(400, 'Missing required parameters`keyword`.');
|
|
|
}
|
|
|
|
|
|
const { limit, offset } = this.getPagination(page, size);
|
|
|
|
|
|
let keywordStr = keyword;
|
|
|
keywordStr = keyword.replace(/[,’'"\\;%()@&+=\-\.®–\!]/g, ''); // '[^a-zA-Z0-9 @@&&()\-\./®’"'',–\!]'
|
|
|
keywordStr = keywordStr.replace(/\s+/g, ' ');
|
|
|
keywordStr = keywordStr.trim();
|
|
|
|
|
|
const { rows, count } = await heytripService.hotelSearch(keywordStr, { limit, offset });
|
|
|
|
|
|
let quoteRes = [];
|
|
|
|
|
|
if (checkin && checkout) {
|
|
|
const allIds = rows.map((item) => Number(item.hotel_id));
|
|
|
const basePriceParam = {
|
|
|
hotelIds: allIds,
|
|
|
nationality: 'CN', // 默认取中国报价
|
|
|
CheckInDate: checkin,
|
|
|
CheckOutDate: checkout,
|
|
|
adultNum: adults || 1,
|
|
|
roomCount: rooms || 1,
|
|
|
};
|
|
|
quoteRes = await QuotedHotelsPrice(basePriceParam);
|
|
|
|
|
|
heytripService.writeHeytripRequestLog({
|
|
|
action: 'heytripBasePrice',
|
|
|
body: basePriceParam,
|
|
|
});
|
|
|
}
|
|
|
|
|
|
const quoteMapped = quoteRes.reduce((r, c) => ({ ...r, [c.Id]: c }), {});
|
|
|
const res = rows.map((item) => ({ ...item, base_price: quoteMapped[item.hotel_id] || {} }));
|
|
|
|
|
|
ctx.body = this.response(0, 'hotel_search', { rows: res, count }, page, size);
|
|
|
} catch (error) {
|
|
|
// console.error(error);
|
|
|
ctx.statusCode = error.statusCode;
|
|
|
ctx.body = this.response(1, error.message || 'An error occurred.');
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
module.exports = new Api();
|