Charges/invoices update

This commit is contained in:
Senad Uka
2019-07-19 09:46:15 +02:00
parent e2dcb246f0
commit 4389aaa8da
16 changed files with 253 additions and 62 deletions

View File

@@ -174,7 +174,7 @@ const bulkWriteReservationsWithChangesTracking = (reservations) => {
const changedKeys = instance.changed();
const previous = instance.previous();
const lookupKeys = ['start', 'end'];
const lookupKeys = ['start', 'end', 'resourceId', 'canceled'];
let realChange = false;
lookupKeys.forEach((key) => {
@@ -184,6 +184,8 @@ const bulkWriteReservationsWithChangesTracking = (reservations) => {
}
});
instance.setDataValue('hourlyRate', previous.hourlyRate);
if (realChange){
changes.push({
oldReservation: previous,
@@ -193,25 +195,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)