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: {
type: DataTypes.TEXT,
primaryKey: true,
unique: true,
},
memberId: DataTypes.TEXT,
officeId: DataTypes.TEXT,

View File

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