Skip to content

Commit

Permalink
Merge pull request #42 from lux-group/calendar
Browse files Browse the repository at this point in the history
feat(calendar): add methods for calendar
  • Loading branch information
reb2020 authored Mar 15, 2022
2 parents b858592 + af6e2ff commit 45c3545
Show file tree
Hide file tree
Showing 9 changed files with 389 additions and 2 deletions.
54 changes: 54 additions & 0 deletions compiled/calendar/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"use strict";

var _OFFERS;

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var _require = require('../offer/constants'),
OFFER_TYPE_HOTEL = _require.OFFER_TYPE_HOTEL,
OFFER_TYPE_TOUR = _require.OFFER_TYPE_TOUR,
OFFER_TYPE_LAST_MINUTE_HOTEL = _require.OFFER_TYPE_LAST_MINUTE_HOTEL,
OFFER_TYPE_TACTICAL_AO_HOTEL = _require.OFFER_TYPE_TACTICAL_AO_HOTEL;

var OFFERS = (_OFFERS = {}, _defineProperty(_OFFERS, OFFER_TYPE_TOUR, {
type: OFFER_TYPE_TOUR,
parentType: OFFER_TYPE_TOUR,
dateFloorOffset: 2,
hourOfDayThreshold: 0,
useDynamicRate: false,
useTimezoneOffset: false
}), _defineProperty(_OFFERS, OFFER_TYPE_HOTEL, {
type: OFFER_TYPE_HOTEL,
parentType: OFFER_TYPE_HOTEL,
dateFloorOffset: 2,
hourOfDayThreshold: 0,
useDynamicRate: false,
useTimezoneOffset: false
}), _defineProperty(_OFFERS, OFFER_TYPE_LAST_MINUTE_HOTEL, {
type: OFFER_TYPE_LAST_MINUTE_HOTEL,
parentType: OFFER_TYPE_HOTEL,
dateFloorOffset: 0,
hourOfDayThreshold: 15,
useDynamicRate: true,
useTimezoneOffset: true
}), _defineProperty(_OFFERS, OFFER_TYPE_TACTICAL_AO_HOTEL, {
type: OFFER_TYPE_TACTICAL_AO_HOTEL,
parentType: OFFER_TYPE_HOTEL,
dateFloorOffset: 0,
hourOfDayThreshold: 15,
useDynamicRate: true,
useTimezoneOffset: true
}), _OFFERS);
var CEILING_YEARS = 3;
var DEFAULT_MONTHS_FALLBACK = 18;
var DATE_FORMAT = 'YYYY-MM-DD';
module.exports = {
CEILING_YEARS: CEILING_YEARS,
DEFAULT_MONTHS_FALLBACK: DEFAULT_MONTHS_FALLBACK,
DATE_FORMAT: DATE_FORMAT,
OFFER_TYPE_HOTEL: OFFER_TYPE_HOTEL,
OFFER_TYPE_TOUR: OFFER_TYPE_TOUR,
OFFER_TYPE_LAST_MINUTE_HOTEL: OFFER_TYPE_LAST_MINUTE_HOTEL,
OFFER_TYPE_TACTICAL_AO_HOTEL: OFFER_TYPE_TACTICAL_AO_HOTEL,
OFFERS: OFFERS
};
67 changes: 67 additions & 0 deletions compiled/calendar/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"use strict";

var moment = require('moment');

var constants = require('./constants');

var getTimezoneOffset = function getTimezoneOffset(offerPackageTimezoneOffset, offerType, requestTimezoneOffset) {
if (!requestTimezoneOffset && constants.OFFERS[offerType].useTimezoneOffset) {
return parseInt(offerPackageTimezoneOffset || 0);
}

return requestTimezoneOffset;
};

var getMonthsToRequest = function getMonthsToRequest(timezoneOffset, maxDate) {
var now = moment().utcOffset(timezoneOffset);
return moment(maxDate).diff(now, 'months') + 2;
};

var getMaxCheckInCloseDate = function getMaxCheckInCloseDate(checkInCloses) {
var defaultMonths = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : constants.DEFAULT_MONTHS_FALLBACK;
return checkInCloses ? moment.min(moment(checkInCloses), moment().add(constants.CEILING_YEARS, 'years')).format(constants.DATE_FORMAT) : moment().add(defaultMonths, 'months').format(constants.DATE_FORMAT);
};

var getStartDate = function getStartDate(minDate, travelFromDate) {
if (minDate && travelFromDate) {
if (moment(travelFromDate).isAfter(moment(minDate))) {
return moment(travelFromDate).format(constants.DATE_FORMAT);
} else {
return moment(minDate).format(constants.DATE_FORMAT);
}
}

if (travelFromDate) {
return moment(travelFromDate).format(constants.DATE_FORMAT);
}

return moment(minDate).format(constants.DATE_FORMAT);
};

var getDateFloorOffset = function getDateFloorOffset(_ref) {
var timezoneOffset = _ref.timezoneOffset,
dateFloorOffset = _ref.dateFloorOffset,
hourOfDayThreshold = _ref.hourOfDayThreshold,
_ref$enquiryType = _ref.enquiryType,
enquiryType = _ref$enquiryType === void 0 ? 'customer' : _ref$enquiryType;
if (enquiryType === 'admin') return 0;

if (dateFloorOffset === 0) {
var nowHours = parseInt(moment().utcOffset(timezoneOffset).format('HH'));

if (nowHours >= hourOfDayThreshold) {
return 1;
}
}

return dateFloorOffset;
};

module.exports = {
constants: constants,
getTimezoneOffset: getTimezoneOffset,
getMonthsToRequest: getMonthsToRequest,
getMaxCheckInCloseDate: getMaxCheckInCloseDate,
getStartDate: getStartDate,
getDateFloorOffset: getDateFloorOffset
};
3 changes: 2 additions & 1 deletion compiled/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ module.exports = {
offer: require('./offer'),
currency: require('./currency'),
occupancy: require('./occupancy'),
date: require('./date')
date: require('./date'),
calendar: require('./calendar')
};
45 changes: 45 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,49 @@ declare module "@luxuryescapes/lib-global" {
including?: boolean
) => Date[];
};
const calendar: {
constants: {
CEILING_YEARS: number;
DEFAULT_MONTHS_FALLBACK: number;
DATE_FORMAT: string;
OFFERS: {
[offerType: string]: {
type: string;
parentType: string;
dateFloorOffset: number;
hourOfDayThreshold: number;
useDynamicRate: boolean;
useTimezoneOffset: boolean;
};
};
};
getTimezoneOffset: (
offerPackageTimezoneOffset: number,
offerType: string,
requestTimezoneOffset: number
) => number;
getMonthsToRequest: (
timezoneOffset: number,
maxDate: string
) => number;
getMaxCheckInCloseDate: (
checkInCloses?: string,
defaultMonths?: number
) => string;
getStartDate: (
minDate?: string,
travelFromDate?: string
) => string;
getDateFloorOffset: ({
timezoneOffset,
dateFloorOffset,
hourOfDayThreshold,
enquiryType
}: {
timezoneOffset: number,
dateFloorOffset: number,
hourOfDayThreshold: number,
enquiryType: 'customer' | 'admin'
}) => string;
};
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@luxuryescapes/lib-global",
"version": "2.6.7",
"version": "2.6.8",
"description": "Lib for expanding functionality and deduplicating code between services",
"main": "compiled/index.js",
"types": "index.d.ts",
Expand Down
51 changes: 51 additions & 0 deletions src/calendar/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const { OFFER_TYPE_HOTEL, OFFER_TYPE_TOUR, OFFER_TYPE_LAST_MINUTE_HOTEL, OFFER_TYPE_TACTICAL_AO_HOTEL } = require('../offer/constants')

const OFFERS = {
[OFFER_TYPE_TOUR]: {
type: OFFER_TYPE_TOUR,
parentType: OFFER_TYPE_TOUR,
dateFloorOffset: 2,
hourOfDayThreshold: 0,
useDynamicRate: false,
useTimezoneOffset: false,
},
[OFFER_TYPE_HOTEL]: {
type: OFFER_TYPE_HOTEL,
parentType: OFFER_TYPE_HOTEL,
dateFloorOffset: 2,
hourOfDayThreshold: 0,
useDynamicRate: false,
useTimezoneOffset: false,
},
[OFFER_TYPE_LAST_MINUTE_HOTEL]: {
type: OFFER_TYPE_LAST_MINUTE_HOTEL,
parentType: OFFER_TYPE_HOTEL,
dateFloorOffset: 0,
hourOfDayThreshold: 15,
useDynamicRate: true,
useTimezoneOffset: true,
},
[OFFER_TYPE_TACTICAL_AO_HOTEL]: {
type: OFFER_TYPE_TACTICAL_AO_HOTEL,
parentType: OFFER_TYPE_HOTEL,
dateFloorOffset: 0,
hourOfDayThreshold: 15,
useDynamicRate: true,
useTimezoneOffset: true,
},
}

const CEILING_YEARS = 3
const DEFAULT_MONTHS_FALLBACK = 18
const DATE_FORMAT = 'YYYY-MM-DD'

module.exports = {
CEILING_YEARS,
DEFAULT_MONTHS_FALLBACK,
DATE_FORMAT,
OFFER_TYPE_HOTEL,
OFFER_TYPE_TOUR,
OFFER_TYPE_LAST_MINUTE_HOTEL,
OFFER_TYPE_TACTICAL_AO_HOTEL,
OFFERS,
}
68 changes: 68 additions & 0 deletions src/calendar/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const moment = require('moment')

const constants = require('./constants')

const getTimezoneOffset = (offerPackageTimezoneOffset, offerType, requestTimezoneOffset) => {
if (!requestTimezoneOffset && constants.OFFERS[offerType].useTimezoneOffset) {
return parseInt(offerPackageTimezoneOffset || 0)
}

return requestTimezoneOffset
}

const getMonthsToRequest = (timezoneOffset, maxDate) => {
const now = moment().utcOffset(timezoneOffset)
return moment(maxDate).diff(now, 'months') + 2
}

const getMaxCheckInCloseDate = (
checkInCloses,
defaultMonths = constants.DEFAULT_MONTHS_FALLBACK,
) => {
return checkInCloses ?
moment
.min(moment(checkInCloses), moment().add(constants.CEILING_YEARS, 'years'))
.format(constants.DATE_FORMAT) :
moment().add(defaultMonths, 'months').format(constants.DATE_FORMAT)
}

const getStartDate = (minDate, travelFromDate) => {
if (minDate && travelFromDate) {
if (moment(travelFromDate).isAfter(moment(minDate))) {
return moment(travelFromDate).format(constants.DATE_FORMAT)
} else {
return moment(minDate).format(constants.DATE_FORMAT)
}
}
if (travelFromDate) {
return moment(travelFromDate).format(constants.DATE_FORMAT)
}
return moment(minDate).format(constants.DATE_FORMAT)
}

const getDateFloorOffset = ({
timezoneOffset,
dateFloorOffset,
hourOfDayThreshold,
enquiryType = 'customer',
}) => {
if (enquiryType === 'admin') return 0

if (dateFloorOffset === 0) {
const nowHours = parseInt(moment().utcOffset(timezoneOffset).format('HH'))
if (nowHours >= hourOfDayThreshold) {
return 1
}
}

return dateFloorOffset
}

module.exports = {
constants,
getTimezoneOffset,
getMonthsToRequest,
getMaxCheckInCloseDate,
getStartDate,
getDateFloorOffset,
}
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ module.exports = {
currency: require('./currency'),
occupancy: require('./occupancy'),
date: require('./date'),
calendar: require('./calendar'),
}
Loading

0 comments on commit 45c3545

Please sign in to comment.