Skip to content

Commit

Permalink
[TBB-194]: Refactor Flexible nights (#25)
Browse files Browse the repository at this point in the history
* Refactor flexi nights

Co-authored-by: moixons <[email protected]>
  • Loading branch information
moixons and moixons authored Jun 21, 2021
1 parent 85d5ca1 commit b2a54f9
Show file tree
Hide file tree
Showing 9 changed files with 2,285 additions and 323 deletions.
117 changes: 64 additions & 53 deletions compiled/offer/flexiNights.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,57 @@ var _extends = Object.assign || function (target) { for (var i = 1; i < argument

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

/**
* Calculates the duration based on number of nights/days and extra nights
*
* @param {*} offerPackageDuration, total number of nights/days for a given package
* @param {*} extraNights, number of extra nights
* @returns the duration of the package option
*/
var calculateDuration = function calculateDuration(offerPackageDuration, extraNights) {
return offerPackageDuration + extraNights;
};

/**
* Calculates the prices based on number of nights and the package prices list
*
* @param {*} offerPackagePrices, list of prices of the package
* @param {*} extraNights, number of extra nights
* @returns a list of prices based on prices in the package and number of nights
*/
var calculatePackagePrices = function calculatePackagePrices(offerPackagePrices, extraNights) {
return offerPackagePrices.map(function (price) {
var packagePrice = extraNights && extraNights > 0 ? price.price + extraNights * price.nightly_price : price.price;
var packageValue = extraNights && extraNights > 0 ? price.value + extraNights * price.nightly_value : price.value;
return _extends({}, price, {
price: packagePrice,
value: packageValue,
nightly_price: price.nightly_price,
nightly_value: price.nightly_value
});
});
};

/**
* Generates a package option
*
* @param {*} packageOption, a package of an offer
* @param {*} numberOfNights, number of nights
* @param {*} packageOption, a package option of a package in an offer
* @param {*} offerPackage, a package of an offer
* @param {*} extraNights, number of extra nights
* @returns Generates a package option
*/
var generatePackageOption = function generatePackageOption(packageOption, numberOfNights, extraNights) {
var generatePackageOption = function generatePackageOption(packageOption, offerPackage, extraNights) {
var offerPackageDuration = offerPackage.number_of_nights || offerPackage.number_of_days;
var offerPackagePrices = offerPackage.prices ? offerPackage.prices : [];

return {
packageId: packageOption.fk_room_rate_id,
packageId: packageOption.id || offerPackage.id,
extraNights: extraNights,
roomRateId: packageOption.fk_room_rate_id,
name: packageOption.name || undefined,
duration: numberOfNights + extraNights
roomTypeId: offerPackage.fk_room_type_id || undefined,
roomRateId: offerPackage.fk_room_rate_id || undefined,
name: packageOption.name || offerPackage.name,
duration: calculateDuration(offerPackageDuration, extraNights),
prices: calculatePackagePrices(offerPackagePrices, extraNights)
};
};

Expand All @@ -33,71 +69,46 @@ var getPackageOptions = function getPackageOptions(offerPackage) {
};

/**
* Generates a list of all the package options, adding new options for flexible nights
* Add the flexible nights package options
*
* @param {*} offerPackage, a package of an offer
* @returns a list of all the package options
* @param {*} packageOption, a package option of a package in an offer
* @returns a list of flexible nights package options
*/
var generateAllPackageOptions = function generateAllPackageOptions(offerPackage) {
var generateFlexiNightsPackageOptions = function generateFlexiNightsPackageOptions(offerPackage, packageOption) {
var result = [];
if (!offerPackage) return result;

var packageOptions = getPackageOptions(offerPackage);

var maxExtraNights = offerPackage.flexible_nights && offerPackage.max_extra_nights ? offerPackage.max_extra_nights : 0;

var _loop = function _loop(extraNights) {
packageOptions.forEach(function (packageOption) {
result.push(generatePackageOption(packageOption, offerPackage.number_of_nights, extraNights));
});
};

for (var extraNights = 0; extraNights <= maxExtraNights; extraNights++) {
_loop(extraNights);
for (var extraNights = 1; extraNights <= offerPackage.max_extra_nights; extraNights++) {
result.push(generatePackageOption(packageOption, offerPackage, extraNights));
}
return result;
};

/**
* Calculates the prices based on number of nights and the package prices list
*
* @param {*} offerPackagePrices, list of prices of the package
* @param {*} duration, number of nights
* @returns a list of prices based on prices in the package and number of nights
*/
var calculatePackagePrices = function calculatePackagePrices(offerPackagePrices, duration) {
return offerPackagePrices.map(function (price) {
return _extends({}, price, {
price: price.price + duration * price.nightly_price,
value: price.value + duration * price.nightly_value,
nightly_price: 0,
nightly_value: 0
});
});
};

/**
* Generates a list of all the package options with their prices,
* adding new options for flexible nights
* Generates a list of all the package options, adding new options for flexible nights
*
* @param {*} offerPackage, a package of an offer
* @returns a list of all the package options with their prices
* @returns a list of all the package options
*/
var generateAllPackageOptionsWithPrices = function generateAllPackageOptionsWithPrices(offerPackage) {
var offerPackagePrices = offerPackage.prices ? [].concat(_toConsumableArray(offerPackage.prices)) : undefined;
var generateAllPackageOptions = function generateAllPackageOptions(offerPackage) {
var result = [];
if (!offerPackage) return result;

var allPackageOptions = generateAllPackageOptions(offerPackage);
var result = allPackageOptions.map(function (packageOption) {
return _extends({}, packageOption, {
prices: offerPackagePrices ? calculatePackagePrices(offerPackagePrices, packageOption.duration) : undefined
});
var packageOptions = getPackageOptions(offerPackage);

packageOptions.forEach(function (packageOption) {
result.push(generatePackageOption(packageOption, offerPackage, 0));
if (offerPackage.flexible_nights && offerPackage.max_extra_nights) {
result.push.apply(result, _toConsumableArray(generateFlexiNightsPackageOptions(offerPackage, packageOption)));
}
});

return result;
};

module.exports = {
calculateDuration: calculateDuration,
generatePackageOption: generatePackageOption,
calculatePackagePrices: calculatePackagePrices,
generateAllPackageOptions: generateAllPackageOptions,
generateAllPackageOptionsWithPrices: generateAllPackageOptionsWithPrices
generateFlexiNightsPackageOptions: generateFlexiNightsPackageOptions
};
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.2.7",
"version": "2.2.8",
"description": "Lib for expanding functionality and deduplicating code between services",
"main": "compiled/index.js",
"homepage": "https://github.com/brandsExclusive/lib-global#readme",
Expand Down
120 changes: 65 additions & 55 deletions src/offer/flexiNights.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,55 @@
/**
* Calculates the duration based on number of nights/days and extra nights
*
* @param {*} offerPackageDuration, total number of nights/days for a given package
* @param {*} extraNights, number of extra nights
* @returns the duration of the package option
*/
const calculateDuration = (offerPackageDuration, extraNights) => {
return offerPackageDuration + extraNights
}

/**
* Calculates the prices based on number of nights and the package prices list
*
* @param {*} offerPackagePrices, list of prices of the package
* @param {*} extraNights, number of extra nights
* @returns a list of prices based on prices in the package and number of nights
*/
const calculatePackagePrices = (offerPackagePrices, extraNights) => {
return offerPackagePrices.map((price) => {
const packagePrice = (extraNights && extraNights > 0) ? price.price + extraNights * price.nightly_price : price.price
const packageValue = (extraNights && extraNights > 0) ? price.value + extraNights * price.nightly_value : price.value
return {
...price,
price: packagePrice,
value: packageValue,
nightly_price: price.nightly_price,
nightly_value: price.nightly_value,
}
})
}

/**
* Generates a package option
*
* @param {*} packageOption, a package of an offer
* @param {*} numberOfNights, number of nights
* @param {*} packageOption, a package option of a package in an offer
* @param {*} offerPackage, a package of an offer
* @param {*} extraNights, number of extra nights
* @returns Generates a package option
*/
const generatePackageOption = (packageOption, numberOfNights, extraNights) => {
const generatePackageOption = (packageOption, offerPackage, extraNights) => {
const offerPackageDuration = offerPackage.number_of_nights || offerPackage.number_of_days
const offerPackagePrices = (offerPackage.prices) ? offerPackage.prices : []

return {
packageId: packageOption.fk_room_rate_id,
packageId: packageOption.id || offerPackage.id,
extraNights: extraNights,
roomRateId: packageOption.fk_room_rate_id,
name: packageOption.name || undefined,
duration: numberOfNights + extraNights,
roomTypeId: offerPackage.fk_room_type_id || undefined,
roomRateId: offerPackage.fk_room_rate_id || undefined,
name: packageOption.name || offerPackage.name,
duration: calculateDuration(offerPackageDuration, extraNights),
prices: calculatePackagePrices(offerPackagePrices, extraNights),
}
}

Expand All @@ -28,77 +65,50 @@ const getPackageOptions = (offerPackage) => (
[{ fk_room_rate_id: offerPackage.fk_room_rate_id }])

/**
* Generates a list of all the package options, adding new options for flexible nights
* Add the flexible nights package options
*
* @param {*} offerPackage, a package of an offer
* @returns a list of all the package options
* @param {*} packageOption, a package option of a package in an offer
* @returns a list of flexible nights package options
*/
const generateAllPackageOptions = (offerPackage) => {
const generateFlexiNightsPackageOptions = (offerPackage, packageOption) => {
const result = []
if (!offerPackage) return result

const packageOptions = getPackageOptions(offerPackage)

const maxExtraNights =
(offerPackage.flexible_nights && offerPackage.max_extra_nights) ?
offerPackage.max_extra_nights : 0

for (
let extraNights = 0;
extraNights <= maxExtraNights;
let extraNights = 1;
extraNights <= offerPackage.max_extra_nights;
extraNights++
) {
packageOptions.forEach(packageOption => {
result.push(generatePackageOption(packageOption, offerPackage.number_of_nights, extraNights))
})
result.push(generatePackageOption(packageOption, offerPackage, extraNights))
}
return result
}

/**
* Calculates the prices based on number of nights and the package prices list
*
* @param {*} offerPackagePrices, list of prices of the package
* @param {*} duration, number of nights
* @returns a list of prices based on prices in the package and number of nights
*/
const calculatePackagePrices = (offerPackagePrices, duration) => {
return offerPackagePrices.map((price) => {
return {
...price,
price: price.price + duration * price.nightly_price,
value: price.value + duration * price.nightly_value,
nightly_price: 0,
nightly_value: 0,
}
})
}

/**
* Generates a list of all the package options with their prices,
* adding new options for flexible nights
* Generates a list of all the package options, adding new options for flexible nights
*
* @param {*} offerPackage, a package of an offer
* @returns a list of all the package options with their prices
* @returns a list of all the package options
*/
const generateAllPackageOptionsWithPrices = (offerPackage) => {
const offerPackagePrices = (offerPackage.prices) ? [...offerPackage.prices] : undefined
const generateAllPackageOptions = (offerPackage) => {
const result = []
if (!offerPackage) return result

const allPackageOptions = generateAllPackageOptions(offerPackage)
const result = allPackageOptions.map((packageOption) => {
return {
...packageOption,
prices: (offerPackagePrices) ?
calculatePackagePrices(offerPackagePrices, packageOption.duration) :
undefined,
const packageOptions = getPackageOptions(offerPackage)

packageOptions.forEach(packageOption => {
result.push(generatePackageOption(packageOption, offerPackage, 0))
if (offerPackage.flexible_nights && offerPackage.max_extra_nights) {
result.push(...generateFlexiNightsPackageOptions(offerPackage, packageOption))
}
})

return result
}

module.exports = {
calculateDuration,
generatePackageOption,
calculatePackagePrices,
generateAllPackageOptions,
generateAllPackageOptionsWithPrices,
generateFlexiNightsPackageOptions,
}
11 changes: 11 additions & 0 deletions test/factories/offers/factories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const { buildLEOffer } = require('./leOffer')
const { buildLMEOffer } = require('./lmeOffer')
const { buildTAOOffer } = require('./taoOffer')
const { buildTourOffer } = require('./tourOffer')

module.exports = {
buildLEOffer,
buildLMEOffer,
buildTAOOffer,
buildTourOffer,
}
Loading

0 comments on commit b2a54f9

Please sign in to comment.