Fix weekend rates

This commit is contained in:
Senad Uka
2019-10-28 20:35:34 +01:00
parent c83866860d
commit 81538600a6
3 changed files with 24 additions and 4 deletions

View File

@@ -24,14 +24,17 @@ const checkBookingChanges = () => {
const ratesMap = {};
rates.forEach(rate => {
const { rateId, price } = rate;
ratesMap[rateId] = price;
const { rateId, price, weekendPrice } = rate;
ratesMap[rateId] = {
price,
weekendPrice
};
});
const resourcesMap = {};
resources.forEach(resource => {
const { resourceId, rate } = resource;
resource.price = ratesMap[rate] || 0;
resource.price = ratesMap[rate] || {price: 0, weekendPrice: 0};
resourcesMap[resourceId] = resource;
});

View File

@@ -193,7 +193,16 @@ const bulkWriteReservationsWithChangesTracking = (reservations, resourcesMap) =>
if (parseFloat(instance.previous('hourlyRate') > 0)) {
instance.setDataValue('hourlyRate', instance.previous('hourlyRate'));
}else{
const hourlyRate = resourceId ? resourcesMap[resourceId].price : 0;
//Determine if we should apply weekend price or work day price
const newStartWeekDay = moment.utc(instance.start).isoWeekday();
const isWeekend = newStartWeekDay > 5; //6 - Saturday, 7 - Sunday
let hourlyRate;
if (isWeekend){
hourlyRate = resourceId ? resourcesMap[resourceId].price.weekendPrice : 0;
}else{
hourlyRate = resourceId ? resourcesMap[resourceId].price.price : 0;
}
console.log(hourlyRate);
instance.setDataValue('hourlyRate', hourlyRate);
}
}

View File

@@ -9,9 +9,17 @@ const fetchRates = () => {
const rates = result.data || [];
const cleanedRates = [];
rates.forEach(rate => {
const additionalRates = rate.rates;
let weekendRate = rate.price; //fallback price
additionalRates.forEach(additionalRate => {
if (additionalRate.isWeekendRate){
weekendRate = additionalRate.price;
}
});
cleanedRates.push({
rateId: rate['_id'],
price: rate.price,
weekendPrice: weekendRate
});
});
resolve(cleanedRates);