fetch reservations, find and update changed reservations

This commit is contained in:
Bilal Catic
2019-06-27 05:38:45 +02:00
parent aedb09d1be
commit 7aab1e538e
7 changed files with 127 additions and 4 deletions

View File

@@ -0,0 +1,24 @@
'use strict';
require('dotenv').config();
const { fetchAllBookings, bulkWriteReservationsWithChangesTracking } = require('../services/officeRnD/bookings');
const checkBookingChanges = () => {
fetchAllBookings()
.then((reservations) => {
bulkWriteReservationsWithChangesTracking(reservations)
.then((changes) => {
console.log('== CHANGES == ');
console.log(changes);
})
.catch((error) => {
console.log('Error bulk write booking reservations :', error);
});
})
.catch((error) => {
console.log('Error fetching bookings from ORD ', error);
});
};
checkBookingChanges();

View File

@@ -0,0 +1,14 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.removeConstraint('bookingReservations', 'bookingReservations_pkey');
},
down: (queryInterface, Sequelize) => {
return queryInterface.addConstraint('bookingReservations', ['id'], {
name: 'bookingReservations_pkey',
type: 'PRIMARY KEY',
});
}
};

View File

@@ -0,0 +1,14 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.addConstraint('bookingReservations', ['reservationId'], {
name: 'bookingReservations_pkey',
type: 'PRIMARY KEY',
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.removeConstraint('bookingReservations', 'bookingReservations_pkey');
}
};

View File

@@ -2,7 +2,10 @@
module.exports = (sequelize, DataTypes) => {
const bookingReservation = sequelize.define('bookingReservation', {
reservationId: DataTypes.TEXT,
reservationId: {
type: DataTypes.TEXT,
primaryKey: true,
},
memberId: DataTypes.TEXT,
officeId: DataTypes.TEXT,
resourceId: DataTypes.TEXT,
@@ -10,7 +13,6 @@ module.exports = (sequelize, DataTypes) => {
end: DataTypes.DATE,
timezone: DataTypes.TEXT,
canceled: DataTypes.BOOLEAN,
}, {});
bookingReservation.associate = function(models) {
// associations can be defined here

View File

@@ -15,7 +15,8 @@
"start-server": "nodemon server.js",
"start-client": "cd client && yarn start",
"start": "node server.js",
"heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"
"heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client",
"check-booking-changes": "node ./cronServices/checkBookingChanges.js"
},
"engines": {
"node": "11.12.x"

View File

@@ -0,0 +1,2 @@
'use strict';

View File

@@ -148,11 +148,77 @@ const writeBookingReservation = (bookingReservation) => {
return db.bookingReservation.findOrCreate({where: {...bookingReservation}, defaults: {...bookingReservation}});
};
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 indexOfUpdatedAt = changedKeys.indexOf('updatedAt');
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])){
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 = {
fetchAllBookings,
writeBookingReservation,
getAllFinishedBookings,
getFirstNextBooking,
getFirstPreviousBooking,
getFirstReservationInBlock
getFirstReservationInBlock,
bulkWriteReservationsWithChangesTracking,
};