create incidents for booking changes

This commit is contained in:
Bilal Catic
2019-07-08 18:23:32 +02:00
parent 7aab1e538e
commit 6bed9aecc0
9 changed files with 230 additions and 23 deletions

View File

@@ -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,19 @@ 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) => {
@@ -157,28 +174,21 @@ const bulkWriteReservationsWithChangesTracking = (reservations) => {
const changedKeys = instance.changed();
const previous = instance.previous();
const indexOfUpdatedAt = changedKeys.indexOf('updatedAt');
const lookupKeys = ['start', 'end'];
if (indexOfUpdatedAt !== -1){
changedKeys.splice(indexOfUpdatedAt, 1);
}
if (changedKeys.length > 0){
//check if there is really difference, reservation start and end timestamps are reported as changed but they are not
let realChange = false;
changedKeys.forEach((changedKey) => {
if (JSON.stringify(previous[changedKey]) !== JSON.stringify(instance[changedKey])){
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(),
});
}
});
if (realChange){
changes.push({
oldReservation: previous,
newReservation: instance.get(),
});
}
});