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

View File

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

View File

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

View File

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