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.

47 lines
1.6 KiB
JavaScript

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 } = 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);
const { rows, count } = await heytripService.hotelSearch(keyword, { limit, offset });
let quoteRes = [];
if (checkin && checkout) {
const allIds = rows.map((item) => item.hotel_id);
quoteRes = await QuotedHotelsPrice({
hotelIds: allIds,
nationality: 'CN', // 默认取中国报价
CheckInDate: checkin,
CheckOutDate: checkout,
adultNum: 1,
roomCount: 1,
});
}
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();