From 26458d99578be0b04840c3ae1cfc555d7539c3c4 Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Thu, 25 Jul 2019 06:27:02 +0200 Subject: [PATCH 1/3] add pool option to the config --- config/config.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/config/config.js b/config/config.js index 3bb20d6..ea66105 100644 --- a/config/config.js +++ b/config/config.js @@ -1,3 +1,9 @@ +const pool = { + max: parseInt(process.env.DB_POOL_MAX_CONNECTIONS) || 5, + acquire: parseInt(process.env.DB_POOL_ACQUIRE) || 60000, + evict: parseInt(process.env.DB_POOL_EVICT) || 1000, +}; + module.exports = { development: { username: 'docker', @@ -5,14 +11,17 @@ module.exports = { database: 'CrmIntegration', port: '5431', dialect: 'postgres', - logging: parseInt(process.env.SEQUELIZE_LOGGING) ? console.log : false + logging: parseInt(process.env.SEQUELIZE_LOGGING) ? console.log : false, + pool }, test: { "use_env_variable": 'DATABASE_URL', - logging: parseInt(process.env.SEQUELIZE_LOGGING) ? console.log : false + logging: parseInt(process.env.SEQUELIZE_LOGGING) ? console.log : false, + pool }, production: { "use_env_variable": 'DATABASE_URL', - logging: parseInt(process.env.SEQUELIZE_LOGGING) ? console.log : false + logging: parseInt(process.env.SEQUELIZE_LOGGING) ? console.log : false, + pool } }; From 2b271196a0ab97fbd5a085f561b0ed1d456400b1 Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Thu, 25 Jul 2019 06:27:22 +0200 Subject: [PATCH 2/3] add pool option to the config --- environment.env | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/environment.env b/environment.env index b77df6a..74ad45d 100644 --- a/environment.env +++ b/environment.env @@ -24,3 +24,8 @@ BOOKING_CHANGE_PERCENTAGE_CHARGE=Percentage of hourly reate to apply for cancell ALLOWED_BOOKING_CANCELLATION_TIME=Time from creation (in minutes) in which cancellation is not charged SEQUELIZE_LOGGING=0 - false, 1 - true (console logging) + +#More about pool option : http://docs.sequelizejs.com/class/lib/sequelize.js~Sequelize.html +DB_POOL_MAX_CONNECTIONS=Maximum number of connection in pool (ex. 18) +DB_POOL_ACQUIRE=The maximum time, in milliseconds, that pool will try to get connection before throwing error (ex. 120000) +DB_POOL_EVICT=The time interval, in milliseconds, after which sequelize-pool will remove idle connections. (ex. 10000) From 54ae3e4ef52928bdb4885f8fd30d6431894c8c6a Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Thu, 25 Jul 2019 06:28:26 +0200 Subject: [PATCH 3/3] make dlock screen waiting for incident calculation to finish --- constants/constants.js | 1 + controllers/doorLock.js | 34 +++++++-- services/integration/doorLockCharges.js | 92 ++++++++++++++----------- 3 files changed, 81 insertions(+), 46 deletions(-) diff --git a/constants/constants.js b/constants/constants.js index 0388308..1d06ee2 100644 --- a/constants/constants.js +++ b/constants/constants.js @@ -62,6 +62,7 @@ const officeRnDAPIErrors = { FAILED_TO_ADD_FEES: 'Failed to add fees in ORD', }; const integrationServiceErrors = { + IMPORT_SUCCESSFUL_CALCULATION_FAILED: 'Import succeeded but fetching reservations and calculating charges failed', FAILED_TO_SAVE_BOOKINGS: 'Failed to save booking reservations', FAILED_TO_SAVE_DOOR_LOCK_ENTRIES: 'Failed to save door lock entries', FAILED_TO_SAVE_DATA_GENERIC: 'Failed to save data', diff --git a/controllers/doorLock.js b/controllers/doorLock.js index c34248a..a75cf2c 100644 --- a/controllers/doorLock.js +++ b/controllers/doorLock.js @@ -4,6 +4,7 @@ const { parseDoorLockDataFile, writeDoorLockEvent } = require('../services/doorL const { fetchAllBookings, writeBookingReservation } = require('../services/officeRnD/bookings'); const { calculateDoorLockCharges } = require('../services/integration/doorLockCharges'); const { integrationServiceErrors } = require('../constants/constants'); +const { checkBookingChanges } = require('../services/integration/checkBookingChange'); const IncomingForm = require('formidable').IncomingForm; @@ -42,11 +43,25 @@ const uploadDoorLockData = (req, res) => { Promise.all(asyncWriteJobs) .then(() => { - res.json({ - parsedData, - parserErrors, - unknownMembers - }); + checkBookingChanges() + .then(() => { + calculateDoorLockCharges() + .then(() => { + res.json({ + parsedData, + parserErrors, + unknownMembers + }); + }) + .catch((error) => { + console.log('Error : ', error); + res.status(500).send(integrationServiceErrors.IMPORT_SUCCESSFUL_CALCULATION_FAILED); + }); + }) + .catch((error) => { + console.log('Error : ', error); + res.status(500).send(integrationServiceErrors.IMPORT_SUCCESSFUL_CALCULATION_FAILED); + }); }) .catch((error) => { console.log(integrationServiceErrors.FAILED_TO_SAVE_DOOR_LOCK_ENTRIES); @@ -54,7 +69,7 @@ const uploadDoorLockData = (req, res) => { res.status(500).send(integrationServiceErrors.FAILED_TO_SAVE_DATA_GENERIC); }); - fetchAllBookings() + /*fetchAllBookings() .then((bookingEntries) => { const asyncJobs = []; bookingEntries.forEach((bookingEntry) => asyncJobs.push(writeBookingReservation(bookingEntry))); @@ -62,13 +77,18 @@ const uploadDoorLockData = (req, res) => { Promise.all(asyncJobs) .then(() => { calculateDoorLockCharges(); - }); + }) + .catch((error) => { + console.log('Error updating booking reservations : '); + console.log(error); + }) }) .catch((error) => { console.log(integrationServiceErrors.FAILED_TO_SAVE_BOOKINGS); console.log(error); return; }); + */ }) .catch((error) => { res.status(500).send(error); diff --git a/services/integration/doorLockCharges.js b/services/integration/doorLockCharges.js index 0aeb222..582ba28 100644 --- a/services/integration/doorLockCharges.js +++ b/services/integration/doorLockCharges.js @@ -510,52 +510,66 @@ const getIncidentData = (reservation) => { }; const calculateDoorLockCharges = () => { - getAllFinishedBookings() - .then((reservations) => { - const unlockedIncidents = []; - const unscheduledIncidents = []; + return new Promise((resolve, reject) => { + getAllFinishedBookings() + .then((reservations) => { + const unlockedIncidents = []; + const unscheduledIncidents = []; - const asyncCheckForIncidents = []; + const asyncCheckForIncidents = []; - reservations.forEach((reservation) => { - asyncCheckForIncidents.push(getIncidentData(reservation)); - }); + reservations.forEach((reservation) => { + asyncCheckForIncidents.push(getIncidentData(reservation)); + }); - Promise.all(asyncCheckForIncidents) - .then((allReservationsIncidents) => { - allReservationsIncidents.forEach((reservationIncidents) => { - reservationIncidents.forEach((incident) => { - if (incident.error) { - console.log('Error checking incident : ', incident.error); - } else if (incident.incidentType) { - switch (incident.incidentType) { - case incidentType.UNLOCKED_INCIDENT_RELATED_WITH_RESERVATION: - case incidentType.UNLOCKED_INCIDENT_STANDALONE: - unlockedIncidents.push(incident); - break; - case incidentType.UNSCHEDULED_INCIDENT_BEFORE_RESERVATION: - case incidentType.UNSCHEDULED_INCIDENT_AFTER_RESERVATION: - case incidentType.UNSCHEDULED_INCIDENT_STANDALONE: - unscheduledIncidents.push(incident); - break; + Promise.all(asyncCheckForIncidents) + .then((allReservationsIncidents) => { + allReservationsIncidents.forEach((reservationIncidents) => { + reservationIncidents.forEach((incident) => { + if (incident.error) { + console.log('Error checking incident : ', incident.error); + } else if (incident.incidentType) { + switch (incident.incidentType) { + case incidentType.UNLOCKED_INCIDENT_RELATED_WITH_RESERVATION: + case incidentType.UNLOCKED_INCIDENT_STANDALONE: + unlockedIncidents.push(incident); + break; + case incidentType.UNSCHEDULED_INCIDENT_BEFORE_RESERVATION: + case incidentType.UNSCHEDULED_INCIDENT_AFTER_RESERVATION: + case incidentType.UNSCHEDULED_INCIDENT_STANDALONE: + unscheduledIncidents.push(incident); + break; + } } - } + }); }); - }); - insertUnscheduledIncidents(unscheduledIncidents) - .catch((error) => console.log(error)); + setUnlockedIncidentsLevel(unlockedIncidents) + .then((completedUnlockedIncidents) => { + const insertIncidentsJobs = [insertUnscheduledIncidents(unscheduledIncidents), insertUnlockedIncidents(completedUnlockedIncidents)]; - setUnlockedIncidentsLevel(unlockedIncidents) - .then((completedUnlockedIncidents) => { - insertUnlockedIncidents(completedUnlockedIncidents) - .catch((error) => console.log(error)); - }) - .catch((error) => console.log(error)); - }) - .catch((error) => console.log(error)); - }) - .catch((error) => console.log(error)); + Promise.all(insertIncidentsJobs) + .then(() => { + resolve(true); + }) + .catch((error) => { + reject(error); + }); + + /* + insertUnscheduledIncidents(unscheduledIncidents) + .catch((error) => console.log(error)); + + insertUnlockedIncidents(completedUnlockedIncidents) + .catch((error) => console.log(error)); + */ + }) + .catch((error) => reject(error)); + }) + .catch((error) => reject(error)); + }) + .catch((error) => reject(error)); + }); }; module.exports = {