You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
2.0 KiB
JavaScript

10 months ago
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;
10 months ago
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 });
10 months ago
let quoteRes = [];
if (checkin && checkout) {
const allIds = rows.map((item) => Number(item.hotel_id));
const basePriceParam = {
10 months ago
hotelIds: allIds,
nationality: 'CN', // 默认取中国报价
CheckInDate: checkin,
CheckOutDate: checkout,
adultNum: adults || 1,
roomCount: rooms || 1,
};
quoteRes = await QuotedHotelsPrice(basePriceParam);
heytripService.writeHeytripRequestLog({
action: 'heytripBasePrice',
body: basePriceParam,
10 months ago
});
}
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();