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 { getResourceMappings } = require('../officeRnD/resources');
const { fetchAllMembers } = require('../officeRnD/members'); const { fetchAllMembers } = require('../officeRnD/members');
const { fetchAllMembershipsAsMap } = require('../officeRnD/memberships'); const { fetchAllMembershipsAsMap } = require('../officeRnD/memberships');
const { getChargedCanceledReservations } = require('../integration/bookingChangeCharges');
const { discounts, DISCOUNT_PLANS } = require('../../constants/constants'); const { discounts, DISCOUNT_PLANS } = require('../../constants/constants');
const createFeeFromIncident = (incident) => { 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) => { 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 startMoment = moment.tz(start, timezone);
const endMoment = moment.tz(end, timezone); const endMoment = moment.tz(end, timezone);
@@ -318,44 +341,16 @@ const getMembersFeesForDateRange = (dateRange, memberIds) => {
membersMap[memberId].bookingData.totalBookedHours += bookingLength; membersMap[memberId].bookingData.totalBookedHours += bookingLength;
if (canceled) { if (!canceled){
reservationIdsForAdditionalData.push(reservationId);
} else {
allActiveBookings.push(booking);
membersMap[memberId].bookingData.totalChargedHours += bookingLength; membersMap[memberId].bookingData.totalChargedHours += bookingLength;
const bookingFee = bookingLength * hourlyRate; const bookingFee = bookingLength * hourlyRate;
membersMap[memberId].bookingData.totalBookingChargedFee += bookingFee; membersMap[memberId].bookingData.totalBookingChargedFee += bookingFee;
allFees.push(createFeeFromBooking(booking, resourceMappings));
} }
} }
}); });
getChargedCanceledReservations(reservationIdsForAdditionalData)
.then((incidents) => {
incidents.forEach((incident) => {
const {memberId, oldBookingStart, oldBookingEnd, chargeFee} = incident.get();
const startMoment = moment.tz(oldBookingStart, UI_TIMEZONE);
const endMoment = moment.tz(oldBookingEnd, UI_TIMEZONE);
if (startMoment.isValid() && endMoment.isValid()) {
const bookingLength = endMoment.diff(startMoment, 'hours', true);
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 //add discount
memberIds.forEach((memberId) => { memberIds.forEach((memberId) => {
const discountFee = createNegativeFeeForDiscount(membersMap[memberId], dateRange); const discountFee = createNegativeFeeForDiscount(membersMap[memberId], dateRange);
@@ -369,8 +364,7 @@ const getMembersFeesForDateRange = (dateRange, memberIds) => {
}); });
resolve(allFees); resolve(allFees);
})
.catch((error) => reject(error));
}) })
.catch((error) => { .catch((error) => {
console.log(error); console.log(error);