implement booking reservation change log

This commit is contained in:
Bilal Catic
2019-07-15 01:24:18 +02:00
parent 8a0e198690
commit e150ebc7e8
6 changed files with 150 additions and 28 deletions

View File

@@ -174,7 +174,7 @@ const bulkWriteReservationsWithChangesTracking = (reservations) => {
const changedKeys = instance.changed();
const previous = instance.previous();
const lookupKeys = ['start', 'end', 'resourceId'];
const lookupKeys = ['start', 'end', 'resourceId', 'canceled'];
let realChange = false;
lookupKeys.forEach((key) => {
@@ -193,25 +193,41 @@ const bulkWriteReservationsWithChangesTracking = (reservations) => {
});
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);
}
const asyncReservationUpdate = () => {
return new Promise((resolve, reject) => {
db.bookingReservation.update(reservation, {
where: {
reservationId: reservation.reservationId,
},
returning: true,
individualHooks: true,
})
.catch((error) => {
console.log('Error updating');
console.log(error);
reject(error);
})
);
.then(([updateCount, updatedInstances]) => {
if (updateCount === 0){
const oldReservation = {
start: null,
end: null,
resourceId: null,
};
changes.push({
oldReservation,
newReservation: reservation,
});
resolve(db.bookingReservation.upsert(reservation));
}else{
resolve();
}
})
.catch((error) => {
console.log('Error updating');
console.log(error);
reject(error);
})
});
};
asyncJobs.push(asyncReservationUpdate());
});
Promise.all(asyncJobs)