diff --git a/server/controllers/Heytrip.js b/server/controllers/Heytrip.js index 48c1807..3e5c618 100644 --- a/server/controllers/Heytrip.js +++ b/server/controllers/Heytrip.js @@ -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.'); diff --git a/server/helper/heytripDataHelper.js b/server/helper/heytripDataHelper.js index 413864c..6236a44 100644 --- a/server/helper/heytripDataHelper.js +++ b/server/helper/heytripDataHelper.js @@ -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; diff --git a/server/services/heytripService.js b/server/services/heytripService.js index a05e048..31d6d5c 100644 --- a/server/services/heytripService.js +++ b/server/services/heytripService.js @@ -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 }) => {