fetch and save booking reservations
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
const { parseDoorLockDataFile, writeDoorLockEvent } = require("../services/doorLock");
|
||||
const { fetchAllBookings, writeBookingReservation } = require('../services/officeRnD/bookings');
|
||||
const { officeRnDAPIErrors } = require('../constants/constants');
|
||||
|
||||
const IncomingForm = require('formidable').IncomingForm;
|
||||
@@ -34,6 +35,15 @@ const uploadDoorLockData = (req, res) => {
|
||||
unknownMembers
|
||||
});
|
||||
|
||||
fetchAllBookings()
|
||||
.then((bookingEntries) => {
|
||||
bookingEntries.forEach((bookingEntry) => writeBookingReservation(bookingEntry));
|
||||
})
|
||||
.catch((error) => {
|
||||
console.log('===> ERROR');
|
||||
console.log(error);
|
||||
});
|
||||
|
||||
parsedData.forEach((entry) => {
|
||||
writeDoorLockEvent(entry);
|
||||
});
|
||||
|
||||
30
migrations/20190530140559-create-booking-reservations.js
Normal file
30
migrations/20190530140559-create-booking-reservations.js
Normal file
@@ -0,0 +1,30 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = {
|
||||
up: (queryInterface, Sequelize) => {
|
||||
return queryInterface.createTable('bookingReservations', {
|
||||
id: {
|
||||
allowNull: false,
|
||||
autoIncrement: true,
|
||||
primaryKey: true,
|
||||
type: Sequelize.INTEGER
|
||||
},
|
||||
reservationId: Sequelize.TEXT,
|
||||
memberId: Sequelize.TEXT,
|
||||
resource: Sequelize.TEXT,
|
||||
start: Sequelize.DATE,
|
||||
end: Sequelize.DATE,
|
||||
createdAt: {
|
||||
allowNull: false,
|
||||
type: Sequelize.DATE
|
||||
},
|
||||
updatedAt: {
|
||||
allowNull: false,
|
||||
type: Sequelize.DATE
|
||||
}
|
||||
});
|
||||
},
|
||||
down: (queryInterface, Sequelize) => {
|
||||
return queryInterface.dropTable('bookingReservations');
|
||||
}
|
||||
};
|
||||
15
models/bookingReservation.js
Normal file
15
models/bookingReservation.js
Normal file
@@ -0,0 +1,15 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
const bookingReservation = sequelize.define('bookingReservation', {
|
||||
reservationId: DataTypes.TEXT,
|
||||
memberId: DataTypes.TEXT,
|
||||
resource: DataTypes.TEXT,
|
||||
start: DataTypes.DATE,
|
||||
end: DataTypes.DATE,
|
||||
}, {});
|
||||
bookingReservation.associate = function(models) {
|
||||
// associations can be defined here
|
||||
};
|
||||
return bookingReservation;
|
||||
};
|
||||
40
services/officeRnD/bookings.js
Normal file
40
services/officeRnD/bookings.js
Normal file
@@ -0,0 +1,40 @@
|
||||
'use strict';
|
||||
|
||||
const db = require('../../models/index');
|
||||
|
||||
const { API } = require('../../helpers/api');
|
||||
|
||||
const fetchAllBookings = () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
API.get('/bookings')
|
||||
.then((result) => {
|
||||
const cleanedBookingReservations = [];
|
||||
const bookingData = result && result.data ? result.data : [];
|
||||
|
||||
bookingData.forEach((fullBookingEntry) => {
|
||||
cleanedBookingReservations.push({
|
||||
reservationId: fullBookingEntry['_id'],
|
||||
memberId: fullBookingEntry.member,
|
||||
resource: fullBookingEntry.resourceId,
|
||||
start: fullBookingEntry.start.dateTime,
|
||||
end: fullBookingEntry.end.dateTime,
|
||||
});
|
||||
});
|
||||
resolve(cleanedBookingReservations);
|
||||
})
|
||||
.catch((error) => {
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const writeBookingReservation = (bookingReservation) => {
|
||||
db.bookingReservation.findOrCreate({where: {...bookingReservation}, defaults: {...bookingReservation}})
|
||||
.then()
|
||||
.catch();
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
fetchAllBookings,
|
||||
writeBookingReservation,
|
||||
};
|
||||
Reference in New Issue
Block a user