fetch reservations, find and update changed reservations
This commit is contained in:
24
cronServices/checkBookingChanges.js
Normal file
24
cronServices/checkBookingChanges.js
Normal 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();
|
||||
@@ -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',
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -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');
|
||||
}
|
||||
};
|
||||
@@ -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
|
||||
|
||||
@@ -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"
|
||||
|
||||
2
services/integration/bookingChangeCharges.js
Normal file
2
services/integration/bookingChangeCharges.js
Normal file
@@ -0,0 +1,2 @@
|
||||
'use strict';
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user