Fix bug with calendar

This commit is contained in:
Senad Uka
2019-08-26 05:03:16 +02:00
parent 2ac4074d94
commit 1447d800bf
9 changed files with 245 additions and 59 deletions

View File

@@ -0,0 +1,15 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.addColumn('bookingChangeIncidents', 'deleted', {
type: Sequelize.BOOLEAN,
defaultValue: false,
after: 'chargeFee',
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.removeColumn('bookingChangeIncidents', 'deleted');
}
};

View File

@@ -12,6 +12,7 @@ module.exports = (sequelize, DataTypes) => {
newBookingEnd: DataTypes.DATE, newBookingEnd: DataTypes.DATE,
incidentType: DataTypes.INTEGER, incidentType: DataTypes.INTEGER,
chargeFee: DataTypes.FLOAT, chargeFee: DataTypes.FLOAT,
deleted: DataTypes.BOOLEAN,
}, {}); }, {});
bookingChangeIncident.associate = function(models) { bookingChangeIncident.associate = function(models) {
// associations can be defined here // associations can be defined here

View File

@@ -5,6 +5,7 @@ module.exports = (sequelize, DataTypes) => {
reservationId: { reservationId: {
type: DataTypes.TEXT, type: DataTypes.TEXT,
primaryKey: true, primaryKey: true,
unique: true,
}, },
memberId: DataTypes.TEXT, memberId: DataTypes.TEXT,
officeId: DataTypes.TEXT, officeId: DataTypes.TEXT,

View File

@@ -12,25 +12,55 @@ const {
incidentType incidentType
} = require('../../constants/constants'); } = require('../../constants/constants');
const bulkWriteBookingChangeIncidents = (incidents) => { const { getBookingChangeLogsForReservationId } = require('./bookingChangeLog');
return new Promise((resolve, reject) => {
const asyncJobs = [];
incidents.forEach((incident) => {
asyncJobs.push(db.bookingChangeIncident.findOrCreate({where: incident, defaults: incident}));
});
Promise.all(asyncJobs) const getShorteningIncidentsForReservationId = (reservationId) => {
.then(() => { const filter = {
resolve(); reservationId,
incidentType: incidentType.BOOKING_SHORTENED,
deleted: false,
};
return db.bookingChangeIncident.findAll({where: filter});
};
const getReservationIncidentsAndLogs = (reservationId) => {
return new Promise((resolve, reject) => {
const asyncDataFetch = [getShorteningIncidentsForReservationId(reservationId), getBookingChangeLogsForReservationId(reservationId)];
Promise.all(asyncDataFetch)
.then((reservationData) => {
resolve({
incidents: reservationData[0],
changeLogs: reservationData[1],
});
}) })
.catch((error) => reject(error)); .catch((error) => reject(error));
}); });
}; };
const chargeBookingChanges = (changes) => { const bulkWriteBookingChangeIncidents = (incidents) => {
//TODO: Check if this complete method can be replaced with
// return db.bookingChangeIncident.bulkCreate(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())
.catch((error) => reject(error));
});
};
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 = [];
const reservationsForAdditionalCheck = [];
changes.forEach((change) => { changes.forEach((change) => {
const { oldReservation, newReservation } = change; const { oldReservation, newReservation } = change;
if (oldReservation && newReservation){ if (oldReservation && newReservation){
@@ -52,21 +82,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 +135,15 @@ const chargeBookingChanges = (changes) => {
}; };
incidents.push(incident); incidents.push(incident);
}else{
reservationsForAdditionalCheck.push(reservationId);
} }
} 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 +160,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,22 +186,70 @@ const chargeBookingChanges = (changes) => {
} }
} }
} }
else{
// console.log('\t\tNo');
}
} }
} }
}); });
resolve(bulkWriteBookingChangeIncidents(incidents));
resolve({incidents, reservationsForAdditionalCheck});
}else{ }else{
reject('Input argument is not an array !'); reject('Input argument is not an array !');
} }
}); });
}; };
const getReservationsIncidentsForRemoval = (reservations) => {
return new Promise((resolve, reject) => {
const asyncChecks = [];
reservations.forEach((reservationId) => {
asyncChecks.push(getReservationIncidentsAndLogs(reservationId));
});
Promise.all(asyncChecks)
.then((possibleRemovals) => {
const incidentsToRemove = [];
possibleRemovals.forEach((possibleRemoval) => {
const { incidents, changeLogs } = possibleRemoval;
if (incidents.length > 0){
const initialReservation = changeLogs && changeLogs[0] ? changeLogs[0] : undefined;
const reservationLastChange = changeLogs && changeLogs[changeLogs.length-1] ?
changeLogs[changeLogs.length-1] : undefined;
if (initialReservation && reservationLastChange){
const initialNewStartMoment = moment.utc(initialReservation.newStart);
const initialNewEndMoment = moment.utc(initialReservation.newEnd);
const lastNewStartMoment = moment.utc(reservationLastChange.newStart);
const lastNewEndMoment = moment.utc(reservationLastChange.newEnd);
if (initialNewStartMoment && initialNewEndMoment &&
lastNewStartMoment && lastNewEndMoment){
const initialDuration = initialNewEndMoment.diff(initialNewStartMoment, 'hours', true);
const lastDuration = lastNewEndMoment.diff(lastNewStartMoment, 'hours', true);
if (lastDuration > initialDuration){
incidentsToRemove.push(...incidents);
}
}
}
}
});
resolve(incidentsToRemove);
})
.catch((error) => reject(error));
});
};
const getChargedCanceledReservations = (reservationIds) => { const getChargedCanceledReservations = (reservationIds) => {
const filters = { const filters = {
reservationId: { reservationId: {
[Op.in]: reservationIds, [Op.in]: reservationIds,
}, },
incidentType: incidentType.BOOKING_CANCELED_LATE, incidentType: incidentType.BOOKING_CANCELED_LATE,
deleted: false,
}; };
const attributes = ['memberId', 'oldBookingStart', 'oldBookingEnd', 'chargeFee']; const attributes = ['memberId', 'oldBookingStart', 'oldBookingEnd', 'chargeFee'];
@@ -147,7 +257,21 @@ const getChargedCanceledReservations = (reservationIds) => {
return db.bookingChangeIncident.findAll({attributes, where: filters}); return db.bookingChangeIncident.findAll({attributes, where: filters});
}; };
module.exports = { const deleteBookingChangeIncidents = (incidents) => {
chargeBookingChanges, const asyncActions = [];
getChargedCanceledReservations, incidents.forEach((incident) => {
incident.set('deleted', true);
asyncActions.push(incident.save());
});
return Promise.all(asyncActions);
};
module.exports = {
getChargedCanceledReservations,
getIncidentsFromChanges,
bulkWriteBookingChangeIncidents,
getShorteningIncidentsForReservationId,
getReservationsIncidentsForRemoval,
deleteBookingChangeIncidents,
}; };

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,8 +48,19 @@ 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());
}); };
const getBookingChangeLogsForReservationId = (reservationId) => {
const filter = {
reservationId,
};
const order = [['createdAt', 'ASC']];
return db.bookingReservationChangeLog.findAll({where: filter, order});
};
module.exports = { module.exports = {
bulkWriteChanges, bulkWriteChanges,
getBookingChangeLogsForReservationId,
}; };

View File

@@ -2,7 +2,12 @@
const { fetchAllBookings, bulkWriteReservationsWithChangesTracking } = require('../officeRnD/bookings'); const { fetchAllBookings, bulkWriteReservationsWithChangesTracking } = require('../officeRnD/bookings');
const { chargeBookingChanges } = require('./bookingChangeCharges'); const {
getIncidentsFromChanges,
bulkWriteBookingChangeIncidents,
getReservationsIncidentsForRemoval,
deleteBookingChangeIncidents,
} = require('./bookingChangeCharges');
const { bulkWriteChanges } = require('./bookingChangeLog'); const { bulkWriteChanges } = require('./bookingChangeLog');
const checkBookingChanges = () => { const checkBookingChanges = () => {
@@ -13,9 +18,28 @@ const checkBookingChanges = () => {
.then((changes) => { .then((changes) => {
bulkWriteChanges(changes) bulkWriteChanges(changes)
.then(() => { .then(() => {
chargeBookingChanges(changes) getIncidentsFromChanges(changes)
.then(() => { .then(({incidents, reservationsForAdditionalCheck}) => {
resolve(true); bulkWriteBookingChangeIncidents(incidents)
.then(() => {
getReservationsIncidentsForRemoval(reservationsForAdditionalCheck)
.then((incidentsToRemove) => {
deleteBookingChangeIncidents(incidentsToRemove)
.then(() => resolve(true))
.catch((error) => {
console.log('Error deleting incidents : ', error);
reject(error);
});
})
.catch((error) => {
console.log('Failed to fetch reservations possible incidents for removal', error);
reject(error);
});
})
.catch((error) => {
console.log('error', error);
reject(error);
});
}) })
.catch((error) => { .catch((error) => {
console.log('Error creating charges ', error); console.log('Error creating charges ', error);

View File

@@ -65,7 +65,7 @@ const createFeeFromIncident = (incident) => {
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('ddd, MMM DD');
bookingTimeExplanation = `${bookingStartMoment.clone().format('HH:mm a')} - ${bookingEndMoment.clone().format('HH:mm a')}`; bookingTimeExplanation = `${bookingStartMoment.clone().format('HH:mm a')} - ${bookingEndMoment.clone().format('HH:mm a')}`;
incidentTimeExplanation = ''; // `UNLOCK : ${unlockMoment.clone().format('HH:mm a')}`; incidentTimeExplanation = ''; // `UNLOCK : ${unlockMoment.clone().format('HH:mm a')}`;
additionalIncidentExplanation = ` ${unlockedIncidentLevelsPrices[incidentLevel].description},`; additionalIncidentExplanation = ` ${unlockedIncidentLevelsPrices[incidentLevel].description},`;
@@ -79,7 +79,7 @@ const createFeeFromIncident = (incident) => {
break; break;
case incidentType.UNSCHEDULED_INCIDENT_BEFORE_RESERVATION: case incidentType.UNSCHEDULED_INCIDENT_BEFORE_RESERVATION:
roomExplanation = resourceName; roomExplanation = resourceName;
dateExplanation = bookingStartMoment.clone().startOf('day').format('MMM DD'); dateExplanation = bookingStartMoment.clone().startOf('day').format('ddd, MMM DD');
bookingTimeExplanation = `${bookingStartMoment.clone().format('HH:mm a')} - ${bookingEndMoment.clone().format('HH:mm a')}`; bookingTimeExplanation = `${bookingStartMoment.clone().format('HH:mm a')} - ${bookingEndMoment.clone().format('HH:mm a')}`;
incidentTimeExplanation = ` Unlock : ${unlockMoment.clone().format('HH:mm a')},`; incidentTimeExplanation = ` Unlock : ${unlockMoment.clone().format('HH:mm a')},`;
incidentTypeExplanation = ''; incidentTypeExplanation = '';
@@ -92,7 +92,7 @@ const createFeeFromIncident = (incident) => {
break; break;
case incidentType.UNSCHEDULED_INCIDENT_AFTER_RESERVATION: case incidentType.UNSCHEDULED_INCIDENT_AFTER_RESERVATION:
roomExplanation = resourceName; roomExplanation = resourceName;
dateExplanation = bookingStartMoment.clone().startOf('day').format('MMM DD'); dateExplanation = bookingStartMoment.clone().startOf('day').format('ddd, MMM DD');
bookingTimeExplanation = `${bookingStartMoment.clone().format('HH:mm a')} - ${bookingEndMoment.clone().format('HH:mm a')}`; bookingTimeExplanation = `${bookingStartMoment.clone().format('HH:mm a')} - ${bookingEndMoment.clone().format('HH:mm a')}`;
incidentTimeExplanation = ` Lock : ${lockMoment.clone().format('HH:mm a')},`; incidentTimeExplanation = ` Lock : ${lockMoment.clone().format('HH:mm a')},`;
incidentTypeExplanation = ''; incidentTypeExplanation = '';
@@ -105,7 +105,7 @@ const createFeeFromIncident = (incident) => {
break; break;
case incidentType.UNLOCKED_INCIDENT_STANDALONE: case incidentType.UNLOCKED_INCIDENT_STANDALONE:
roomExplanation = resourceName; roomExplanation = resourceName;
dateExplanation = unlockMoment.clone().startOf('day').format('MMM DD'); dateExplanation = unlockMoment.clone().startOf('day').format('ddd, MMM DD');
bookingTimeExplanation = `No reservation`; bookingTimeExplanation = `No reservation`;
incidentTimeExplanation = ''; // `UNLOCK : ${unlockMoment.clone().format('HH:mm a')}`; incidentTimeExplanation = ''; // `UNLOCK : ${unlockMoment.clone().format('HH:mm a')}`;
additionalIncidentExplanation = ` ${unlockedIncidentLevelsPrices[incidentLevel].description},`; additionalIncidentExplanation = ` ${unlockedIncidentLevelsPrices[incidentLevel].description},`;
@@ -119,7 +119,7 @@ const createFeeFromIncident = (incident) => {
break; break;
case incidentType.UNSCHEDULED_INCIDENT_STANDALONE: case incidentType.UNSCHEDULED_INCIDENT_STANDALONE:
roomExplanation = resourceName; roomExplanation = resourceName;
dateExplanation = unlockMoment.clone().startOf('day').format('MMM DD'); dateExplanation = unlockMoment.clone().startOf('day').format('ddd, MMM DD');
bookingTimeExplanation = `No reservation`; bookingTimeExplanation = `No reservation`;
incidentTimeExplanation = ` Unlock : ${unlockMoment.clone().format('HH:mm a')} Lock : ${lockMoment.clone().format('HH:mm a')},`; incidentTimeExplanation = ` Unlock : ${unlockMoment.clone().format('HH:mm a')} Lock : ${lockMoment.clone().format('HH:mm a')},`;
incidentTypeExplanation = ''; incidentTypeExplanation = '';
@@ -137,7 +137,7 @@ const createFeeFromIncident = (incident) => {
roomExplanation = oldResourceName; roomExplanation = oldResourceName;
} }
dateExplanation = `${oldBookingStartMoment.clone().format('MMM DD')} -> ${newBookingStartMoment.clone().format('MMM DD')}`; dateExplanation = `${oldBookingStartMoment.clone().format('ddd, MMM DD')} -> ${newBookingStartMoment.clone().format('ddd, MMM DD')}`;
bookingTimeExplanation = `(${oldBookingStartMoment.clone().format('HH:mm a')} - ${oldBookingEndMoment.clone().format('HH:mm a')}) -> (${newBookingStartMoment.clone().format('HH:mm a')} - ${newBookingEndMoment.clone().format('HH:mm a')})`; bookingTimeExplanation = `(${oldBookingStartMoment.clone().format('HH:mm a')} - ${oldBookingEndMoment.clone().format('HH:mm a')}) -> (${newBookingStartMoment.clone().format('HH:mm a')} - ${newBookingEndMoment.clone().format('HH:mm a')})`;
incidentTimeExplanation = ` Moved on : ${incidentTimestampMoment.clone().format('MMM DD, HH:mm a')},`; incidentTimeExplanation = ` Moved on : ${incidentTimestampMoment.clone().format('MMM DD, HH:mm a')},`;
incidentTypeExplanation = '[Cancellation]'; incidentTypeExplanation = '[Cancellation]';
@@ -155,7 +155,7 @@ const createFeeFromIncident = (incident) => {
roomExplanation = oldResourceName; roomExplanation = oldResourceName;
} }
dateExplanation = `${oldBookingStartMoment.clone().format('MMM DD')}`; dateExplanation = `${oldBookingStartMoment.clone().format('ddd, MMM DD')}`;
bookingTimeExplanation = `(${oldBookingStartMoment.clone().format('HH:mm a')} - ${oldBookingEndMoment.clone().format('HH:mm a')}) -> (${newBookingStartMoment.clone().format('HH:mm a')} - ${newBookingEndMoment.clone().format('HH:mm a')})`; bookingTimeExplanation = `(${oldBookingStartMoment.clone().format('HH:mm a')} - ${oldBookingEndMoment.clone().format('HH:mm a')}) -> (${newBookingStartMoment.clone().format('HH:mm a')} - ${newBookingEndMoment.clone().format('HH:mm a')})`;
incidentTimeExplanation = ` Shortened on : ${incidentTimestampMoment.clone().format('MMM DD, HH:mm a')},`; incidentTimeExplanation = ` Shortened on : ${incidentTimestampMoment.clone().format('MMM DD, HH:mm a')},`;
incidentTypeExplanation = '[Cancellation]'; incidentTypeExplanation = '[Cancellation]';
@@ -168,7 +168,7 @@ const createFeeFromIncident = (incident) => {
break; break;
case incidentType.BOOKING_CANCELED_LATE: case incidentType.BOOKING_CANCELED_LATE:
roomExplanation = oldResourceName; roomExplanation = oldResourceName;
dateExplanation = `${oldBookingStartMoment.clone().format('MMM DD')}`; dateExplanation = `${oldBookingStartMoment.clone().format('ddd, MMM DD')}`;
bookingTimeExplanation = `${oldBookingStartMoment.clone().format('HH:mm a')} - ${oldBookingEndMoment.clone().format('HH:mm a')}`; bookingTimeExplanation = `${oldBookingStartMoment.clone().format('HH:mm a')} - ${oldBookingEndMoment.clone().format('HH:mm a')}`;
incidentTimeExplanation = ` Canceled on : ${incidentTimestampMoment.clone().format('MMM DD, HH:mm a')},`; incidentTimeExplanation = ` Canceled on : ${incidentTimestampMoment.clone().format('MMM DD, HH:mm a')},`;
incidentTypeExplanation = '[Cancellation]'; incidentTypeExplanation = '[Cancellation]';
@@ -181,7 +181,7 @@ const createFeeFromIncident = (incident) => {
break; break;
} }
const formattedName = `${incidentTypeExplanation} ${incidentExplanation},${additionalIncidentExplanation}${incidentTimeExplanation} ${officeName}, ${roomExplanation}, ${dateExplanation}, ${bookingTimeExplanation}`; const formattedName = `${dateExplanation}, ${bookingTimeExplanation}, ${roomExplanation}, ${incidentExplanation}, ${officeName}`;
return { return {
name: formattedName, name: formattedName,

View File

@@ -136,7 +136,9 @@ const getBookingChangeIncidents = (startDate, endDate, memberIds) => {
'createdAt' 'createdAt'
]; ];
const filters = {}; const filters = {
deleted: false,
};
if (startDate && endDate) { if (startDate && endDate) {
filters.createdAt = { filters.createdAt = {
@@ -280,6 +282,7 @@ const getAllIncidents = (dateRange, memberIds) => {
newBookingEnd, newBookingEnd,
incidentType, incidentType,
chargeFee, chargeFee,
deleted,
createdAt, createdAt,
} = bookingChangeIncident; } = bookingChangeIncident;
const memberName = membersMap[memberId].name; const memberName = membersMap[memberId].name;
@@ -307,6 +310,7 @@ const getAllIncidents = (dateRange, memberIds) => {
newBookingEndRaw: newBookingEnd, newBookingEndRaw: newBookingEnd,
incidentType, incidentType,
totalChargeFee: chargeFee, totalChargeFee: chargeFee,
deleted,
incidentTimestamp: formatTime(createdAt), incidentTimestamp: formatTime(createdAt),
incidentTimestampRaw: createdAt, incidentTimestampRaw: createdAt,
}); });

View File

@@ -194,18 +194,19 @@ const bulkWriteReservationsWithChangesTracking = (reservations) => {
} }
}); });
reservations.forEach((reservation) => { const newReservations = [];
const asyncReservationUpdate = () => { const asyncReservationUpdate = (reservation) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
db.bookingReservation.update(reservation, { db.bookingReservation.update(reservation, {
where: { where: {
reservationId: reservation.reservationId, reservationId: reservation.reservationId,
}, },
returning: true, returning: true,
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,26 +217,31 @@ const bulkWriteReservationsWithChangesTracking = (reservations) => {
oldReservation, oldReservation,
newReservation: reservation, newReservation: reservation,
}); });
resolve(db.bookingReservation.upsert(reservation));
}else{
resolve();
}
})
.catch((error) => {
console.log('Error updating');
console.log(error);
reject(error);
})
});
};
asyncJobs.push(asyncReservationUpdate()); newReservations.push(reservation);
}); }
resolve();
}catch (e) {
console.log('CATCH E : ', e);
reject(e);
}
})
.catch((error) => {
console.log('Error updating');
console.log(error);
reject(error);
});
});
};
reservations.forEach((reservation) => asyncJobs.push(asyncReservationUpdate(reservation)));
Promise.all(asyncJobs) Promise.all(asyncJobs)
.then(() => { .then(() => {
db.bookingReservation.removeHook('updateHook'); db.bookingReservation.removeHook('updateHook');
resolve(changes);
db.bookingReservation.bulkCreate(newReservations)
.then(() => resolve(changes))
.catch((error) => reject(error));
}) })
.catch((error) => reject(error)); .catch((error) => reject(error));
}); });