reuse incidents data to calculate discount

This commit is contained in:
Bilal Catic
2019-08-14 21:58:32 +02:00
parent 7ce3f8c398
commit 930915cb95

View File

@@ -9,7 +9,6 @@ const { DEFAULT_DATE_FORMAT, UI_TIMEZONE, incidentTypeExplanations, incidentType
const { getResourceMappings } = require('../officeRnD/resources');
const { fetchAllMembers } = require('../officeRnD/members');
const { fetchAllMembershipsAsMap } = require('../officeRnD/memberships');
const { getChargedCanceledReservations } = require('../integration/bookingChangeCharges');
const { discounts, DISCOUNT_PLANS } = require('../../constants/constants');
const createFeeFromIncident = (incident) => {
@@ -301,11 +300,35 @@ const getMembersFeesForDateRange = (dateRange, memberIds) => {
};
});
const reservationIdsForAdditionalData = [];
const memberIdTeamMappings = {};
membersList.forEach((member) => {
memberIdTeamMappings[member.memberId] = member.teamId;
});
const allActiveBookings = [];
const allFees = [];
allIncidents.forEach((incident) => {
const { memberId, oldBookingStartRaw, oldBookingEndRaw, totalChargeFee } = incident;
const incidentTypeNumber = incident.incidentType;
if ( incidentTypeNumber === incidentType.BOOKING_CANCELED_LATE){
const startMoment = moment.utc(oldBookingStartRaw);
const endMoment = moment.utc(oldBookingEndRaw);
if (startMoment.isValid() && endMoment.isValid()) {
const bookingLength = endMoment.diff(startMoment, 'hours', true);
// membersMap[memberId].bookingData.totalBookedHours += bookingLength;
// "booked hours" is counted in canceled booking section
membersMap[memberId].bookingData.totalChargedHours += bookingLength;
membersMap[memberId].bookingData.totalBookingChargedFee += totalChargeFee;
}
}
allFees.push(createFeeFromIncident(incident));
});
allBookings.forEach((booking) => {
const {reservationId, memberId, start, end, timezone, canceled, hourlyRate} = booking.get();
const {memberId, start, end, timezone, hourlyRate, canceled } = booking.get();
const startMoment = moment.tz(start, timezone);
const endMoment = moment.tz(end, timezone);
@@ -318,59 +341,30 @@ const getMembersFeesForDateRange = (dateRange, memberIds) => {
membersMap[memberId].bookingData.totalBookedHours += bookingLength;
if (canceled) {
reservationIdsForAdditionalData.push(reservationId);
} else {
allActiveBookings.push(booking);
if (!canceled){
membersMap[memberId].bookingData.totalChargedHours += bookingLength;
const bookingFee = bookingLength * hourlyRate;
membersMap[memberId].bookingData.totalBookingChargedFee += bookingFee;
allFees.push(createFeeFromBooking(booking, resourceMappings));
}
}
});
getChargedCanceledReservations(reservationIdsForAdditionalData)
.then((incidents) => {
incidents.forEach((incident) => {
const {memberId, oldBookingStart, oldBookingEnd, chargeFee} = incident.get();
//add discount
memberIds.forEach((memberId) => {
const discountFee = createNegativeFeeForDiscount(membersMap[memberId], dateRange);
if (discountFee){
allFees.push(discountFee);
}
});
const startMoment = moment.tz(oldBookingStart, UI_TIMEZONE);
const endMoment = moment.tz(oldBookingEnd, UI_TIMEZONE);
allFees.forEach((fee) => {
fee.team = memberIdTeamMappings[fee.member] || null;
});
if (startMoment.isValid() && endMoment.isValid()) {
const bookingLength = endMoment.diff(startMoment, 'hours', true);
resolve(allFees);
membersMap[memberId].bookingData.totalChargedHours += bookingLength;
membersMap[memberId].bookingData.totalBookingChargedFee += chargeFee;
}
});
const memberIdTeamMappings = {};
membersList.forEach((member) => {
memberIdTeamMappings[member.memberId] = member.teamId;
});
const allFees = [];
allIncidents.forEach((incident) => allFees.push(createFeeFromIncident(incident)));
allActiveBookings.forEach((booking) => allFees.push(createFeeFromBooking(booking, resourceMappings)));
//add discount
memberIds.forEach((memberId) => {
const discountFee = createNegativeFeeForDiscount(membersMap[memberId], dateRange);
if (discountFee){
allFees.push(discountFee);
}
});
allFees.forEach((fee) => {
fee.team = memberIdTeamMappings[fee.member] || null;
});
resolve(allFees);
})
.catch((error) => reject(error));
})
.catch((error) => {
console.log(error);