Add cancelation charges
This commit is contained in:
@@ -15,6 +15,10 @@ const fetchAllBookings = () => {
|
||||
const bookingData = result && result.data ? result.data : [];
|
||||
|
||||
bookingData.forEach((fullBookingEntry) => {
|
||||
const fees = fullBookingEntry && fullBookingEntry.fees ? fullBookingEntry.fees : [];
|
||||
const firstFee = fees.length > 0 && fees[0].fee ? fees[0].fee : undefined;
|
||||
const hourlyRate = firstFee && firstFee.price ? firstFee.price : undefined;
|
||||
|
||||
cleanedBookingReservations.push({
|
||||
reservationId: fullBookingEntry['_id'],
|
||||
memberId: fullBookingEntry.member,
|
||||
@@ -24,6 +28,7 @@ const fetchAllBookings = () => {
|
||||
end: fullBookingEntry.end.dateTime,
|
||||
timezone: fullBookingEntry.timezone,
|
||||
canceled: fullBookingEntry.canceled || false,
|
||||
hourlyRate,
|
||||
});
|
||||
});
|
||||
resolve(cleanedBookingReservations);
|
||||
@@ -145,7 +150,77 @@ const getFirstReservationInBlock = (reservation) => {
|
||||
};
|
||||
|
||||
const writeBookingReservation = (bookingReservation) => {
|
||||
return db.bookingReservation.findOrCreate({where: {...bookingReservation}, defaults: {...bookingReservation}});
|
||||
const { reservationId, memberId, officeId, resourceId, start, end, timezone, canceled, hourlyRate } = bookingReservation;
|
||||
const bookingReservationForDB = {
|
||||
reservationId,
|
||||
memberId,
|
||||
officeId,
|
||||
resourceId,
|
||||
start,
|
||||
end,
|
||||
timezone,
|
||||
canceled,
|
||||
hourlyRate,
|
||||
};
|
||||
return db.bookingReservation.findOrCreate({where: {...bookingReservationForDB}, defaults: {...bookingReservationForDB}});
|
||||
};
|
||||
|
||||
const bulkWriteReservationsWithChangesTracking = (reservations) => {
|
||||
return new Promise ((resolve, reject) => {
|
||||
const changes = [];
|
||||
const asyncJobs = [];
|
||||
|
||||
db.bookingReservation.addHook('beforeUpdate', 'updateHook', (instance) => {
|
||||
const changedKeys = instance.changed();
|
||||
const previous = instance.previous();
|
||||
|
||||
const lookupKeys = ['start', 'end'];
|
||||
|
||||
let realChange = false;
|
||||
lookupKeys.forEach((key) => {
|
||||
if ((changedKeys.indexOf(key) !== -1) &&
|
||||
(JSON.stringify(previous[key]) !== JSON.stringify(instance[key]))){
|
||||
realChange = true;
|
||||
}
|
||||
});
|
||||
|
||||
if (realChange){
|
||||
changes.push({
|
||||
oldReservation: previous,
|
||||
newReservation: instance.get(),
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
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);
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
console.log('Error updating');
|
||||
console.log(error);
|
||||
reject(error);
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
Promise.all(asyncJobs)
|
||||
.then(() => {
|
||||
db.bookingReservation.removeHook('updateHook');
|
||||
resolve(changes);
|
||||
})
|
||||
.catch((error) => reject(error));
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
@@ -154,5 +229,6 @@ module.exports = {
|
||||
getAllFinishedBookings,
|
||||
getFirstNextBooking,
|
||||
getFirstPreviousBooking,
|
||||
getFirstReservationInBlock
|
||||
getFirstReservationInBlock,
|
||||
bulkWriteReservationsWithChangesTracking,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user