fetch reservations, find and update changed reservations
This commit is contained in:
2
services/integration/bookingChangeCharges.js
Normal file
2
services/integration/bookingChangeCharges.js
Normal file
@@ -0,0 +1,2 @@
|
||||
'use strict';
|
||||
|
||||
@@ -148,11 +148,77 @@ const writeBookingReservation = (bookingReservation) => {
|
||||
return db.bookingReservation.findOrCreate({where: {...bookingReservation}, defaults: {...bookingReservation}});
|
||||
};
|
||||
|
||||
const bulkWriteReservationsWithChangesTracking = (reservations) => {
|
||||
return new Promise ((resolve, reject) => {
|
||||
const changes = [];
|
||||
const asyncJobs = [];
|
||||
|
||||
db.bookingReservation.addHook('beforeUpdate', 'updateHook', (instance) => {
|
||||
const changedKeys = instance.changed();
|
||||
const previous = instance.previous();
|
||||
|
||||
const indexOfUpdatedAt = changedKeys.indexOf('updatedAt');
|
||||
|
||||
if (indexOfUpdatedAt !== -1){
|
||||
changedKeys.splice(indexOfUpdatedAt, 1);
|
||||
}
|
||||
|
||||
if (changedKeys.length > 0){
|
||||
//check if there is really difference, reservation start and end timestamps are reported as changed but they are not
|
||||
|
||||
let realChange = false;
|
||||
changedKeys.forEach((changedKey) => {
|
||||
if (JSON.stringify(previous[changedKey]) !== JSON.stringify(instance[changedKey])){
|
||||
realChange = true;
|
||||
}
|
||||
});
|
||||
|
||||
if (realChange){
|
||||
changes.push({
|
||||
oldReservation: previous,
|
||||
newReservation: instance.get(),
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
reservations.forEach((reservation) => {
|
||||
asyncJobs.push(
|
||||
db.bookingReservation.update(reservation, {
|
||||
where: {
|
||||
reservationId: reservation.reservationId,
|
||||
},
|
||||
returning: true,
|
||||
individualHooks: true,
|
||||
})
|
||||
.then(([updateCount, updatedInstances]) => {
|
||||
if (updateCount === 0){
|
||||
db.bookingReservation.upsert(reservation);
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
console.log('Error updating');
|
||||
console.log(error);
|
||||
reject(error);
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
Promise.all(asyncJobs)
|
||||
.then(() => {
|
||||
db.bookingReservation.removeHook('updateHook');
|
||||
resolve(changes);
|
||||
})
|
||||
.catch((error) => reject(error));
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
fetchAllBookings,
|
||||
writeBookingReservation,
|
||||
getAllFinishedBookings,
|
||||
getFirstNextBooking,
|
||||
getFirstPreviousBooking,
|
||||
getFirstReservationInBlock
|
||||
getFirstReservationInBlock,
|
||||
bulkWriteReservationsWithChangesTracking,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user