fix unhandled promise warning when new booking is detected
This commit is contained in:
@@ -5,6 +5,7 @@ module.exports = (sequelize, DataTypes) => {
|
||||
reservationId: {
|
||||
type: DataTypes.TEXT,
|
||||
primaryKey: true,
|
||||
unique: true,
|
||||
},
|
||||
memberId: DataTypes.TEXT,
|
||||
officeId: DataTypes.TEXT,
|
||||
|
||||
@@ -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));
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user