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

@@ -194,18 +194,19 @@ const bulkWriteReservationsWithChangesTracking = (reservations) => {
}
});
reservations.forEach((reservation) => {
const asyncReservationUpdate = () => {
return new Promise((resolve, reject) => {
db.bookingReservation.update(reservation, {
where: {
reservationId: reservation.reservationId,
},
returning: true,
individualHooks: true,
})
.then(([updateCount, updatedInstances]) => {
if (updateCount === 0){
const newReservations = [];
const asyncReservationUpdate = (reservation) => {
return new Promise((resolve, reject) => {
db.bookingReservation.update(reservation, {
where: {
reservationId: reservation.reservationId,
},
returning: true,
individualHooks: true,
})
.then(([updateCount, updatedInstances]) => {
try {
if (updateCount === 0) {
const oldReservation = {
start: null,
end: null,
@@ -216,26 +217,31 @@ const bulkWriteReservationsWithChangesTracking = (reservations) => {
oldReservation,
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)
.then(() => {
db.bookingReservation.removeHook('updateHook');
resolve(changes);
db.bookingReservation.bulkCreate(newReservations)
.then(() => resolve(changes))
.catch((error) => reject(error));
})
.catch((error) => reject(error));
});