perf: 获取报价: 读取数据库中的房型信息

main
Lei OT 10 months ago
parent 69dcd27a87
commit 103f30263c

@ -21,10 +21,6 @@ class Heytrip extends BaseController {
getAvailability = async (ctx) => {
try {
const data = await heytripService.getHotelAvailability(ctx.query);
heytripService.writeHeytripRequestLog({
action: 'heytripAvailability',
body: ctx.query,
});
ctx.body = this.response(0, 'get_hotel_availability', data);
} catch (error) {
ctx.body = this.response(1, error.message || 'An error occurred.');

@ -344,7 +344,11 @@ const resolveRatePlans = (plans) => {
return OutputText;
}); // 限时取消
rp.Cancelable === true ? CancelRulesMapped.push('其他时间不可取消') : false;
return { ...rp, PriceUnit, PriceTotal: rp.Price, CancelRulesText: CancelRulesMapped, CancelableText };
return { ...rp,
PriceUnit, PriceTotal: rp.Price,
CancelRulesText: CancelRulesMapped, CancelableText,
PayTypeText: PayTypeMapped[rp.PayType]?.label(),
};
}),
}));
return res;

@ -1,7 +1,7 @@
const db = require('../config/db');
const initModels = require('./../models/init-models');
const { AvailableAccommodationIds, AccommodationsDetails, Availability } = require('../vendor/heytrip');
const { isEmpty } = require('../utils/commons');
const { isEmpty, groupBy } = require('../utils/commons');
const { resolveDetails, resolveRatePlans } = require('../helper/heytripDataHelper');
const { DEFAULT_LGC, LGC_MAPPED, HEYTRIP_API_PROD } = require('../config/constants');
@ -26,7 +26,7 @@ const Locations = models.locationsModel;
const Logs = models.requestLogsModel;
const foreignOption = { onDelete: 'NO ACTION', onUpdate: 'NO ACTION' };
// HeytripIds.hasMany(Hotelinfo, {
// foreignKey: 'hotel_id',
// onDelete: 'NO ACTION',
@ -35,11 +35,13 @@ const Logs = models.requestLogsModel;
// Hotelinfo.belongsTo(HeytripIds, { as: 'aid', foreignKey: 'hotel_id', onDelete: 'NO ACTION', onUpdate: 'NO ACTION' });
// Hotelinfo.hasMany(Rooms, { sourceKey: 'hotel_id', foreignKey: 'hotel_id', onDelete: 'NO ACTION', onUpdate: 'NO ACTION' });
// Rooms.belongsTo(Hotelinfo, { targetKey: 'hotel_id', foreignKey: 'hotel_id', onDelete: 'NO ACTION', onUpdate: 'NO ACTION', });
Hotelinfo.belongsTo(HeytripIds, { as: 'aid', foreignKey: 'hotel_id', onDelete: 'NO ACTION', onUpdate: 'NO ACTION' });
Hotelinfo.hasMany(Hotelinfo2, { as: 'locale_info', sourceKey: 'hotel_id', foreignKey: 'hotel_id', onDelete: 'NO ACTION', onUpdate: 'NO ACTION' });
// Hotelinfo2.belongsTo(Hotelinfo, { as: 'locale_info2', targetKey: 'hotel_id', foreignKey: 'hotel_id', onDelete: 'NO ACTION', onUpdate: 'NO ACTION', });
Hotelinfo.hasOne(City, { as: 'city', sourceKey: 'city_id', foreignKey: 'id', onDelete: 'NO ACTION', onUpdate: 'NO ACTION', }); // 多语种, 所以实际是 hasMany , 用 hasOne 要指定 lgc= 1 或者2
Hotelinfo.hasOne(Country, { as: 'country', sourceKey: 'country_code', foreignKey: 'id', onDelete: 'NO ACTION', onUpdate: 'NO ACTION', }); // 多语种, 所以实际是 hasMany , 用 hasOne 要指定 lgc= 1 或者2
Hotelinfo.belongsTo(HeytripIds, { as: 'aid', foreignKey: 'hotel_id', ...foreignOption });
Hotelinfo.hasMany(Hotelinfo2, { as: 'locale_info', sourceKey: 'hotel_id', foreignKey: 'hotel_id', ...foreignOption });
// Hotelinfo2.belongsTo(Hotelinfo, { as: 'locale_info2', targetKey: 'hotel_id', foreignKey: 'hotel_id', ...foreignOption, });
Hotelinfo.hasOne(City, { as: 'city', sourceKey: 'city_id', foreignKey: 'id', ...foreignOption, }); // 多语种, 所以实际是 hasMany , 用 hasOne 要指定 lgc= 1 或者2
Hotelinfo.hasOne(Country, { as: 'country', sourceKey: 'country_code', foreignKey: 'id', ...foreignOption, }); // 多语种, 所以实际是 hasMany , 用 hasOne 要指定 lgc= 1 或者2
Rooms.hasMany(Images, { sourceKey: 'room_id', foreignKey: 'info_source_id', ...foreignOption });
class Heytrip {
/**
@ -440,11 +442,32 @@ class Heytrip {
}
};
hotelRooms = async (aid) => {
const res = await Rooms.findAll({
where: { hotel_id: aid, },
attributes: [ 'room_id', 'lgc', 'locale',
['room_name', 'RoomName'],
// ['locale_name', 'LocaleName'],
['bed_type_desc', 'BedTypeDesc'],
['smoking', 'Smoking'],
['area', 'Area'],
],
// include: [
// { model: Images, where: { lgc: [0, 1] }, required: false, separate: true },
// ],
});
return res.map(row => row.toJSON());
};
/**
* 获取实时的报价
*/
getHotelAvailability = async (param) => {
const { hotel_id, checkin, checkout, adults, children_ages, rooms, nationality } = param;
const _hotelRooms = await this.hotelRooms(hotel_id);
const hotelRoomsMappedByLgc = groupBy(_hotelRooms, 'lgc');
const roomRes = hotelRoomsMappedByLgc['2'] || hotelRoomsMappedByLgc['1'];
const roomMappedByID = (roomRes || []).reduce((rr, c) => ({...rr, [c.room_id]: c}), {});
const paramBody = {
Language: 'en-US',
// Language: 'zh-CN',
@ -460,7 +483,14 @@ class Heytrip {
};
const _quoteRes = await Availability(paramBody);
const quoteRes = resolveRatePlans(_quoteRes);
return quoteRes;
const roomsQuote = quoteRes.map((row) => ({...row, ...roomMappedByID[row.RoomId]}));
this.writeHeytripRequestLog({
action: 'heytripAvailability',
body: paramBody,
});
return roomsQuote;
};
writeHeytripRequestLog = async ({ action, body }) => {

Loading…
Cancel
Save