fix unhandled promise warning when new booking is detected

This commit is contained in:
Bilal Catic
2019-08-22 02:28:11 +02:00
parent 69ebf36bb1
commit 1bb87cb476
2 changed files with 43 additions and 43 deletions

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

@@ -194,55 +194,54 @@ 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]) => {
try { try {
if (updateCount === 0) { if (updateCount === 0) {
const oldReservation = { const oldReservation = {
start: null, start: null,
end: null, end: null,
resourceId: null, resourceId: null,
}; };
changes.push({ changes.push({
oldReservation, oldReservation,
newReservation: reservation, newReservation: reservation,
}); });
db.bookingReservation.upsert(reservation)
.then(() => resolve()) newReservations.push(reservation);
.catch((error) => reject(error));
} else {
resolve();
}
}catch (e) {
console.log('CATCH E : ', e);
reject(e);
} }
}) resolve();
.catch((error) => { }catch (e) {
console.log('Error updating'); console.log('CATCH E : ', e);
console.log(error); reject(e);
reject(error); }
}); })
}); .catch((error) => {
}; console.log('Error updating');
console.log(error);
asyncJobs.push(asyncReservationUpdate()); 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));
}); });