Merge branch 'improve-error-handling-for-addFees' into 'master'

Improve error handling for add fees

See merge request saburly/psihologija!61
This commit was merged in pull request #61.
This commit is contained in:
Bilal Catic
2019-10-04 14:57:58 +00:00
8 changed files with 214 additions and 182 deletions

View File

@@ -57,12 +57,16 @@ const csvParserErrors = {
const officeRnDAPIErrors = { const officeRnDAPIErrors = {
FAILED_TO_FETCH_MEMBERS: 'Failed to fetch members', FAILED_TO_FETCH_MEMBERS: 'Failed to fetch members',
FAILED_TO_FETCH_BOOKINGS: 'Failed to fetch booking reservations', FAILED_TO_FETCH_BOOKINGS: 'Failed to fetch booking reservations',
FAILED_TO_FETCH_FEES: 'Failed to fetch existing fees', FAILED_TO_FETCH_FEES: 'Failed to fetch existing fees from ORD',
FAILED_TO_FETCH_PLANS: 'Failed to fetch plans', FAILED_TO_FETCH_PLANS: 'Failed to fetch plans from ORD',
FAILED_TO_DELETE_FEES: 'Failed to delete fees in ORD', FAILED_TO_DELETE_FEES: 'Failed to delete fees in ORD',
FAILED_TO_ADD_FEES: 'Failed to add fees in ORD', FAILED_TO_ADD_FEES: 'Failed to add fees in ORD',
MEMBERSHIPS_ARE_NOT_LOADED_CORRECTLY: 'Memberships are not loaded correctly', MEMBERSHIPS_ARE_NOT_LOADED_CORRECTLY: 'Memberships are not loaded correctly',
OAUTH_FAILED: 'Failed to fetch new OAUTH token', OAUTH_FAILED: 'Failed to fetch new OAUTH token',
FAILED_TO_FETCH_DATA: 'Failed to fetch data from ORD. Please try again in a few minutes',
FAILED_TO_FETCH_RESOURCES: 'Failed to fetch resources data from ORD',
FAILED_TO_FETCH_RATES: 'Failed to fetch rates data from ORD',
FAILED_TO_UPDATE_MEMBERSHIP: 'Failed to update membership in ORD'
}; };
const integrationServiceErrors = { const integrationServiceErrors = {
PROCESSING_TRY_AGAIN: 'Incident calculations are in progress. Please try again in a few minutes', PROCESSING_TRY_AGAIN: 'Incident calculations are in progress. Please try again in a few minutes',

View File

@@ -147,28 +147,22 @@ const addFees = (req, res) => {
}); });
}) })
.catch((error) => { .catch((error) => {
console.log('Error reformatting memberships name');
console.log(error);
res.status(500).send(error); res.status(500).send(error);
}) })
}) })
.catch((error) => { .catch((error) => {
console.log('Error adding fees');
res.status(500).send(error); res.status(500).send(error);
}) })
}) })
.catch((error) => { .catch((error) => {
console.log(error);
res.status(500).send(error); res.status(500).send(error);
}) })
}) })
.catch((error) => { .catch((error) => {
console.log(error);
res.status(500).send(error); res.status(500).send(error);
}); });
}) })
.catch((error) => { .catch((error) => {
console.log('Error with updating booking reservations');
res.status(500).send(error); res.status(500).send(error);
}) })
}else{ }else{

View File

@@ -3,7 +3,7 @@
const { fetchAllBookings, bulkWriteReservationsWithChangesTracking } = require('../officeRnD/bookings'); const { fetchAllBookings, bulkWriteReservationsWithChangesTracking } = require('../officeRnD/bookings');
const { fetchResources } = require('../officeRnD/resources'); const { fetchResources } = require('../officeRnD/resources');
const { fetchRates } = require('../officeRnD/rates'); const { fetchRates } = require('../officeRnD/rates');
const { officeRnDAPIErrors } = require('../../constants/constants');
const { const {
getIncidentsFromChanges, getIncidentsFromChanges,
bulkWriteBookingChangeIncidents, bulkWriteBookingChangeIncidents,
@@ -77,8 +77,8 @@ const checkBookingChanges = () => {
}); });
}) })
.catch((error) => { .catch((error) => {
console.log('Error fetching bookings, resources and rates from ORD ', error); console.log('\t> [Check Booking Changes] Error fetching bookings, resources and rates from ORD ', error);
reject(error); reject(officeRnDAPIErrors.FAILED_TO_FETCH_DATA);
}); });
}); });
}; };

View File

@@ -9,9 +9,10 @@ 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 { fetchAllMembershipsForMemberIds } = require('../officeRnD/memberships'); const { fetchAllMembershipsForMemberIds } = require('../officeRnD/memberships');
const { discounts, DISCOUNT_PLANS } = require('../../constants/constants'); const { discounts, DISCOUNT_PLANS, officeRnDAPIErrors } = require('../../constants/constants');
const createFeeFromIncident = (incident) => { const createFeeFromIncident = (incident) => {
try {
const { const {
memberId, memberId,
officeId, officeId,
@@ -59,7 +60,7 @@ const createFeeFromIncident = (incident) => {
const incidentTimestampMoment = moment.tz(incidentTimestampRaw, UI_TIMEZONE); const incidentTimestampMoment = moment.tz(incidentTimestampRaw, UI_TIMEZONE);
switch (incidentTypeNumber){ switch (incidentTypeNumber) {
case incidentType.UNLOCKED_INCIDENT_RELATED_WITH_RESERVATION: case incidentType.UNLOCKED_INCIDENT_RELATED_WITH_RESERVATION:
roomExplanation = resourceName; roomExplanation = resourceName;
dateExplanation = bookingStartMoment.clone().startOf('day').format('MMM DD'); dateExplanation = bookingStartMoment.clone().startOf('day').format('MMM DD');
@@ -184,6 +185,13 @@ const createFeeFromIncident = (incident) => {
office: officeId, office: officeId,
isPersonal: false, isPersonal: false,
} }
}
catch (e) {
console.log("[Create Fee From Incident] Incident is incomplete, it will not be added");
console.log(e);
console.log(">> INCIDENT : ", incident);
return null;
}
}; };
const createFeeFromBooking = (booking, resourceMappings) => { const createFeeFromBooking = (booking, resourceMappings) => {
@@ -320,7 +328,10 @@ const getMembersFeesForDateRange = (dateRange, memberIds) => {
const allFees = []; const allFees = [];
allIncidents.forEach((incident) => { allIncidents.forEach((incident) => {
const feeFromIncident = createFeeFromIncident(incident);
if (feeFromIncident){
allFees.push(createFeeFromIncident(incident)); allFees.push(createFeeFromIncident(incident));
}
const incidentsValuableForDiscountCalculation = [ const incidentsValuableForDiscountCalculation = [
incidentType.UNSCHEDULED_INCIDENT_BEFORE_RESERVATION, incidentType.UNSCHEDULED_INCIDENT_BEFORE_RESERVATION,
@@ -399,18 +410,22 @@ const getMembersFeesForDateRange = (dateRange, memberIds) => {
break; break;
} }
if (membersMap[memberId] && membersMap[memberId].bookingData){
membersMap[memberId].bookingData.totalChargedHours += chargedBookingLength; membersMap[memberId].bookingData.totalChargedHours += chargedBookingLength;
membersMap[memberId].bookingData.totalBookingChargedFee += totalChargeFee; membersMap[memberId].bookingData.totalBookingChargedFee += totalChargeFee;
}
}); });
allBookings.forEach((booking) => { allBookings.forEach((booking) => {
const {memberId, start, end, timezone, hourlyRate, canceled } = 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);
if (startMoment.isValid() && endMoment.isValid()) { const memberMappingExists = !!membersMap[memberId];
if (startMoment.isValid() && endMoment.isValid() && memberMappingExists) {
const bookingLength = endMoment.diff(startMoment, 'hours', true); const bookingLength = endMoment.diff(startMoment, 'hours', true);
if (!membersMap[memberId] || !membersMap[memberId].bookingData) { if (!membersMap[memberId].bookingData) {
membersMap[memberId].bookingData = Object.assign({}, oneMemberObject); membersMap[memberId].bookingData = Object.assign({}, oneMemberObject);
} }
@@ -423,34 +438,43 @@ const getMembersFeesForDateRange = (dateRange, memberIds) => {
allFees.push(createFeeFromBooking(booking, resourceMappings)); allFees.push(createFeeFromBooking(booking, resourceMappings));
} }
}else{
console.log('[Get Members Fees For Date Range] Start Moment / End Moment / Member Mapping is invalid for member : ', memberId);
} }
}); });
//add discount //add discount
memberIdsToUse.forEach((memberId) => { memberIdsToUse.forEach((memberId) => {
if (membersMap[memberId]){
const discountFee = createNegativeFeeForDiscount(membersMap[memberId], dateRange); const discountFee = createNegativeFeeForDiscount(membersMap[memberId], dateRange);
if (discountFee){ if (discountFee){
allFees.push(discountFee); allFees.push(discountFee);
} }
}else{
console.log('[Get Members Fees For Date Range] Member mapping unknown for member : ', memberId);
}
}); });
allFees.forEach((fee) => { allFees.forEach((fee) => {
const { member } = fee; const { member } = fee;
const { teamId, name: memberName} = membersMap[member].member; const memberFromMapping = membersMap[member];
if (memberFromMapping && memberFromMapping.member){
const { teamId, name: memberName} = memberFromMapping.member;
fee.team = memberIdTeamMappings[member] || null; fee.team = memberIdTeamMappings[member] || null;
if (teamId){ if (teamId){
//if member is part of the company, add name to the fee description/name //if member is part of the company, add name to the fee description/name
fee.name += `, ${memberName}`; fee.name += `, ${memberName}`;
} }
}
}); });
resolve({allFees, memberships}); resolve({allFees, memberships});
}) })
.catch((error) => { .catch((error) => {
console.log(error); console.log("[Get Members Fees For Date Range] Error fetching data : ",error);
reject(error); reject(officeRnDAPIErrors.FAILED_TO_FETCH_DATA);
}); });
}); });
}; };

View File

@@ -68,13 +68,13 @@ const deleteFeesFromORD = (dateRange, memberIds) => {
resolve(feesToSkip); resolve(feesToSkip);
}) })
.catch((error) => { .catch((error) => {
console.log('error : ', error); console.log('[Delete Fees From ORD] Error deleting fees from ORD : ', error);
reject(error); reject(officeRnDAPIErrors.FAILED_TO_DELETE_FEES);
}); });
}) })
.catch((error) => { .catch((error) => {
console.log(error); console.log("[Delete Fees From ORD] Error fetching fees and plans from ORD : ", error);
reject(`${officeRnDAPIErrors.FAILED_TO_FETCH_FEES} or ${officeRnDAPIErrors.FAILED_TO_FETCH_PLANS}`); reject(`${officeRnDAPIErrors.FAILED_TO_FETCH_FEES} and ${officeRnDAPIErrors.FAILED_TO_FETCH_PLANS}`);
}); });
}); });
}; };
@@ -96,11 +96,13 @@ const addFeesToORD = (allFees, invoicedFees) => {
const nonInvoicedFees = allFees.filter(isFeeNonInvoiced); const nonInvoicedFees = allFees.filter(isFeeNonInvoiced);
API.post('/fees', nonInvoicedFees) API.post('/fees', nonInvoicedFees)
.catch((error) => { .then(() => {
console.log('==== ERROR ====');
console.log(error);
});
resolve(nonInvoicedFees.length); resolve(nonInvoicedFees.length);
})
.catch((error) => {
console.log('[Add Fees To ORD] Failed to send fees to ORD', error);
reject(officeRnDAPIErrors.FAILED_TO_ADD_FEES);
});
}); });
}; };

View File

@@ -23,6 +23,7 @@ const fetchAllMembershipsForMemberIds = (memberIds) => {
resolve(memberships); resolve(memberships);
} }
}else{ }else{
console.log('[Fetch All Memberships For Member Ids] Error fetching memberships - expected member IDs array');
reject(integrationServiceErrors.EXPECTED_MEMBER_IDS_ARRAY); reject(integrationServiceErrors.EXPECTED_MEMBER_IDS_ARRAY);
} }
}) })
@@ -48,7 +49,10 @@ const reformatMembershipsName = (memberships) => {
.then(() => { .then(() => {
resolve(true); resolve(true);
}) })
.catch((error) => reject(error)); .catch((error) => {
console.log('[Reformat Memberships Name] Failed to update memberships : ', error);
reject(officeRnDAPIErrors.FAILED_TO_UPDATE_MEMBERSHIP);
});
}else{ }else{
reject(officeRnDAPIErrors.MEMBERSHIPS_ARE_NOT_LOADED_CORRECTLY); reject(officeRnDAPIErrors.MEMBERSHIPS_ARE_NOT_LOADED_CORRECTLY);
} }

View File

@@ -1,5 +1,6 @@
'use strict'; 'use strict';
const { API } = require('../../helpers/api'); const { API } = require('../../helpers/api');
const { officeRnDAPIErrors } = require('../../constants/constants');
const fetchRates = () => { const fetchRates = () => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@@ -16,7 +17,8 @@ const fetchRates = () => {
resolve(cleanedRates); resolve(cleanedRates);
}) })
.catch((error) => { .catch((error) => {
reject(error); console.log("[Fetch Rates] Failed to fetch rates : ", error);
reject(officeRnDAPIErrors.FAILED_TO_FETCH_RATES);
}); });
}); });
}; };

View File

@@ -3,6 +3,7 @@
const db = require('../../models/index'); const db = require('../../models/index');
const { API } = require('../../helpers/api'); const { API } = require('../../helpers/api');
const { officeRnDAPIErrors } = require('../../constants/constants');
const fetchOffices = () => { const fetchOffices = () => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@@ -41,7 +42,8 @@ const fetchResources = () => {
resolve(cleanedResources); resolve(cleanedResources);
}) })
.catch((error) => { .catch((error) => {
reject(error); console.log("[Fetch resources] Failed to fetch resources : ", error);
reject(officeRnDAPIErrors.FAILED_TO_FETCH_RESOURCES);
}); });
}); });
}; };