fix non-returning promise warning

This commit is contained in:
Bilal Catic
2019-08-21 06:45:43 +02:00
parent 5a0426cc12
commit 27cbe1f51c
4 changed files with 74 additions and 28 deletions

View File

@@ -12,22 +12,22 @@ const {
incidentType
} = require('../../constants/constants');
const bulkWriteBookingChangeIncidents = (incidents) => {
return new Promise((resolve, reject) => {
const asyncJobs = [];
incidents.forEach((incident) => {
asyncJobs.push(db.bookingChangeIncident.findOrCreate({where: incident, defaults: incident}));
});
Promise.all(asyncJobs)
.then(() => {
resolve();
})
.then(() => resolve())
.catch((error) => reject(error));
});
};
const chargeBookingChanges = (changes) => {
const getIncidentsFromChanges = (changes) => {
return new Promise((resolve, reject) => {
if (Array.isArray(changes)){
const incidents = [];
@@ -52,21 +52,45 @@ const chargeBookingChanges = (changes) => {
const differenceFromNow = oldStart.diff(moment.utc(), 'minutes');
console.log('Change detected : ');
console.log('\tOld reservation :');
console.log('\t\tResource : ', oldResourceId);
console.log('\t\tStart : ', oldStart.format());
console.log('\t\tEnd : ', oldEnd.format());
console.log('\t\tLength : ', oldReservationLength);
console.log('\tNew Reservation :');
console.log('\t\tResource : ', newReservation.resourceId);
console.log('\t\tStart : ', newStart.format());
console.log('\t\tEnd : ', newEnd.format());
console.log('\t\tLength : ', newReservationLength);
console.log('\t\tCanceled : ', canceled);
console.log('\t---------------------------------');
console.log('\tDifference : ', differenceFromNow, 'minutes');
console.log('');
console.log('\tIs booking changed too late ? ', differenceFromNow, ' < ', CHARGE_BOOKING_CHANGE_UNDER_TIME);
if (differenceFromNow < CHARGE_BOOKING_CHANGE_UNDER_TIME){
const { reservationId, memberId, resourceId } = newReservation;
console.log('\t\tYes');
console.log('\tIs booking canceled ?');
if (!canceled) {
console.log('\t\tNo');
// Check if new reservation is on same day
const sameDay = oldStart.tz(reservationTimezone).isSame(newStart.tz(reservationTimezone), 'day');
console.log('\tIs new reservation on the same day ?');
if (sameDay) {
console.log('\t\tYes');
// Reservation moved in same day
// Check if member shortened the reservation
console.log('\tIs reservation shortened ? ', newReservationLength, ' < ', oldReservationLength);
if (newReservationLength < oldReservationLength) {
console.log('\t\tYes');
const differenceInLength = oldReservationLength - newReservationLength;
const chargeFee = differenceInLength * reservationHourlyRate * BOOKING_CHANGE_PERCENTAGE_CHARGE / 100;
console.log('\t\t\tThis is [shortened] incident ! Charge fee : ', chargeFee);
const incident = {
reservationId,
memberId,
@@ -81,12 +105,15 @@ const chargeBookingChanges = (changes) => {
};
incidents.push(incident);
}else{
console.log('\t\tNo');
}
} else {
console.log('\t\tNo');
// Reservation moved to another day
// Add cancellation charge
const chargeFee = oldReservationLength * reservationHourlyRate * BOOKING_CHANGE_PERCENTAGE_CHARGE / 100;
console.log('\t\t\tThis is incident ! Charge fee : ', chargeFee);
const incident = {
reservationId,
memberId,
@@ -103,10 +130,15 @@ const chargeBookingChanges = (changes) => {
incidents.push(incident);
}
}else{
console.log('\t\tYes');
const differenceFromCreation = moment.utc().diff(reservationCreationTimestamp, 'minutes');
console.log('\tIs booking created in past ', ALLOWED_BOOKING_CANCELLATION_TIME, ' minutes ?', differenceFromCreation, ' > ', ALLOWED_BOOKING_CANCELLATION_TIME);
if (differenceFromCreation > ALLOWED_BOOKING_CANCELLATION_TIME){
console.log('\t\tYes');
const chargeFee = reservationHourlyRate * oldReservationLength * BOOKING_CHANGE_PERCENTAGE_CHARGE / 100;
console.log('\t\t\tThis is [cancellation] incident ! charge fee : ', chargeFee);
const incident = {
reservationId,
memberId,
@@ -124,10 +156,14 @@ const chargeBookingChanges = (changes) => {
}
}
}
else{
console.log('\t\tNo');
}
}
}
});
resolve(bulkWriteBookingChangeIncidents(incidents));
resolve(incidents);
}else{
reject('Input argument is not an array !');
}
@@ -148,6 +184,7 @@ const getChargedCanceledReservations = (reservationIds) => {
};
module.exports = {
chargeBookingChanges,
getChargedCanceledReservations,
getIncidentsFromChanges,
bulkWriteBookingChangeIncidents,
};

View File

@@ -2,7 +2,7 @@
const db = require('../../models/index');
const bulkWriteChanges = ((changes) => {
const bulkWriteChanges = (changes) => {
const changeLogsForDB = [];
changes.forEach((change) => {
@@ -48,7 +48,7 @@ const bulkWriteChanges = ((changes) => {
return db.bookingReservationChangeLog.bulkCreate(changeLogsForDB);
// console.log(changeLogsForDB);
// return new Promise((resolve) => resolve());
});
};
module.exports = {
bulkWriteChanges,

View File

@@ -2,7 +2,7 @@
const { fetchAllBookings, bulkWriteReservationsWithChangesTracking } = require('../officeRnD/bookings');
const { chargeBookingChanges } = require('./bookingChangeCharges');
const { getIncidentsFromChanges, bulkWriteBookingChangeIncidents } = require('./bookingChangeCharges');
const { bulkWriteChanges } = require('./bookingChangeLog');
const checkBookingChanges = () => {
@@ -13,9 +13,11 @@ const checkBookingChanges = () => {
.then((changes) => {
bulkWriteChanges(changes)
.then(() => {
chargeBookingChanges(changes)
.then(() => {
resolve(true);
getIncidentsFromChanges(changes)
.then((incidents) => {
bulkWriteBookingChangeIncidents(incidents)
.then(() => resolve(true))
.catch((error) => reject(error));
})
.catch((error) => {
console.log('Error creating charges ', error);

View File

@@ -205,27 +205,34 @@ const bulkWriteReservationsWithChangesTracking = (reservations) => {
individualHooks: true,
})
.then(([updateCount, updatedInstances]) => {
if (updateCount === 0){
const oldReservation = {
start: null,
end: null,
resourceId: null,
};
try {
if (updateCount === 0) {
const oldReservation = {
start: null,
end: null,
resourceId: null,
};
changes.push({
oldReservation,
newReservation: reservation,
});
resolve(db.bookingReservation.upsert(reservation));
}else{
resolve();
changes.push({
oldReservation,
newReservation: reservation,
});
db.bookingReservation.upsert(reservation)
.then(() => resolve())
.catch((error) => reject(error));
} else {
resolve();
}
}catch (e) {
console.log('CATCH E : ', e);
reject(e);
}
})
.catch((error) => {
console.log('Error updating');
console.log(error);
reject(error);
})
});
});
};