From 45e6abdba4b4329c4ad3911fe68f4a618798d389 Mon Sep 17 00:00:00 2001 From: Senad Uka Date: Tue, 27 Aug 2019 13:55:01 +0200 Subject: [PATCH] Not deleting the ORD created fees --- constants/constants.js | 1 + services/officeRnD/fees.js | 27 +++++++++++++++++++++------ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/constants/constants.js b/constants/constants.js index bdc29da..84f5a32 100644 --- a/constants/constants.js +++ b/constants/constants.js @@ -58,6 +58,7 @@ const officeRnDAPIErrors = { FAILED_TO_FETCH_MEMBERS: 'Failed to fetch members', FAILED_TO_FETCH_BOOKINGS: 'Failed to fetch booking reservations', FAILED_TO_FETCH_FEES: 'Failed to fetch existing fees', + FAILED_TO_FETCH_PLANS: 'Failed to fetch plans', FAILED_TO_DELETE_FEES: 'Failed to delete fees in ORD', FAILED_TO_ADD_FEES: 'Failed to add fees in ORD', MEMBERSHIPS_ARE_NOT_LOADED_CORRECTLY: 'Memberships are not loaded correctly', diff --git a/services/officeRnD/fees.js b/services/officeRnD/fees.js index bf08c11..287baf1 100644 --- a/services/officeRnD/fees.js +++ b/services/officeRnD/fees.js @@ -10,9 +10,23 @@ const deleteFeesFromORD = (dateRange, memberIds) => { const startDate = moment.utc(dateRange.startDate, DEFAULT_DATE_FORMAT).startOf('day'); const endDate = moment.utc(dateRange.endDate, DEFAULT_DATE_FORMAT).endOf('day'); - API.get('fees') - .then((response) => { - const fetchedFees = response.data ? response.data : []; + const asyncDataFetch = [API.get('fees'), API.get('plans')]; + + Promise.all(asyncDataFetch) + .then((responses) => { + const feesResponse = responses[0]; + const plansResponse = responses[1]; + + const fetchedFees = feesResponse.data ? feesResponse.data : []; + const fetchedPlans = plansResponse.data ? plansResponse.data : []; + + const manualFeeNames = []; + fetchedPlans.forEach((plan) => { + const { name } = plan; + if (name && name.length > 0){ + manualFeeNames.push(name); + } + }); const memberIdsMap = {}; memberIds.forEach((memberId) => { @@ -22,12 +36,13 @@ const deleteFeesFromORD = (dateRange, memberIds) => { const feeIdsToRemove = []; fetchedFees.forEach((fee) => { - const { member, date, invoice } = fee; + const { member, date, invoice, name } = fee; const { status } = invoice; const feeId = fee['_id']; const isDateInDateRange = startDate.isSameOrBefore(date) && endDate.isSameOrAfter(date); - if (memberIdsMap[member] && isDateInDateRange && (status === UNPAID_FEE_STATUS)) { + const manuallyAddedFee = manualFeeNames.indexOf(name) !== -1; + if (memberIdsMap[member] && isDateInDateRange && (status === UNPAID_FEE_STATUS) && !manuallyAddedFee) { feeIdsToRemove.push(feeId); } }); @@ -43,7 +58,7 @@ const deleteFeesFromORD = (dateRange, memberIds) => { }) .catch((error) => { console.log(error); - reject(officeRnDAPIErrors.FAILED_TO_FETCH_FEES); + reject(`${officeRnDAPIErrors.FAILED_TO_FETCH_FEES} or ${officeRnDAPIErrors.FAILED_TO_FETCH_PLANS}`); }); }); };