133 lines
6.8 KiB
JavaScript
133 lines
6.8 KiB
JavaScript
'use strict';
|
|
|
|
const { parseDoorLockDataFile, writeDoorLockEvent } = require('../services/doorLock/doorLock');
|
|
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 { checkIfProcessing, setStartProcessing, setDoneProcessing } = require('../services/integration/processingStatus');
|
|
|
|
const IncomingForm = require('formidable').IncomingForm;
|
|
|
|
const uploadDoorLockData = (req, res) => {
|
|
checkIfProcessing()
|
|
.then((processing) => {
|
|
if (processing){
|
|
res.status(500).send(integrationServiceErrors.PROCESSING_TRY_AGAIN);
|
|
}else{
|
|
const form = new IncomingForm();
|
|
const fileParsers = [];
|
|
|
|
form.on('file', (field, file) => {
|
|
if (file && file.type === 'text/csv') {
|
|
fileParsers.push(parseDoorLockDataFile(file));
|
|
}
|
|
});
|
|
|
|
form.on('end', () => {
|
|
Promise.all(fileParsers)
|
|
.then((parserResults) => {
|
|
const parsedData = [];
|
|
const parserErrors = [];
|
|
const unknownMembers = [];
|
|
|
|
parserResults.forEach((parserResult) => {
|
|
parsedData.push(...parserResult.parsedData);
|
|
parserErrors.push(...parserResult.errors);
|
|
|
|
parserResult.unknownMembers.forEach((newUnknownMember) => {
|
|
// Check if member is already labeled as unknown in different file
|
|
if (!unknownMembers.find((unknownMember) => unknownMember.details === newUnknownMember.details)){
|
|
unknownMembers.push(newUnknownMember);
|
|
}
|
|
});
|
|
});
|
|
|
|
const asyncWriteJobs = [];
|
|
|
|
parsedData.forEach((entry) => asyncWriteJobs.push(writeDoorLockEvent(entry)));
|
|
|
|
Promise.all(asyncWriteJobs)
|
|
.then(() => {
|
|
res.json({
|
|
parsedData,
|
|
parserErrors,
|
|
unknownMembers
|
|
});
|
|
|
|
setStartProcessing()
|
|
.then(() => {
|
|
checkBookingChanges()
|
|
.then(() => {
|
|
calculateDoorLockCharges()
|
|
.then(() => {
|
|
setDoneProcessing()
|
|
.catch((error) => {
|
|
console.log('Error in process done indication');
|
|
console.log(error);
|
|
})
|
|
})
|
|
.catch((error) => {
|
|
console.log('Error : ', error);
|
|
setDoneProcessing();
|
|
// res.status(500).send(integrationServiceErrors.IMPORT_SUCCESSFUL_CALCULATION_FAILED);
|
|
});
|
|
|
|
})
|
|
.catch((error) => {
|
|
console.log('Error : ', error);
|
|
setDoneProcessing();
|
|
// res.status(500).send(integrationServiceErrors.IMPORT_SUCCESSFUL_CALCULATION_FAILED);
|
|
});
|
|
})
|
|
.catch((error) => {
|
|
console.log('Error in processing indicator');
|
|
console.log(error);
|
|
setDoneProcessing();
|
|
});
|
|
})
|
|
.catch((error) => {
|
|
console.log(integrationServiceErrors.FAILED_TO_SAVE_DOOR_LOCK_ENTRIES);
|
|
console.log(error);
|
|
res.status(500).send(integrationServiceErrors.FAILED_TO_SAVE_DATA_GENERIC);
|
|
});
|
|
|
|
/*fetchAllBookings()
|
|
.then((bookingEntries) => {
|
|
const asyncJobs = [];
|
|
bookingEntries.forEach((bookingEntry) => asyncJobs.push(writeBookingReservation(bookingEntry)));
|
|
|
|
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);
|
|
});
|
|
});
|
|
form.parse(req);
|
|
}
|
|
})
|
|
.catch((error) => {
|
|
console.log('Error while checking processing : ');
|
|
console.log(error);
|
|
res.status(500).send('')
|
|
});
|
|
};
|
|
|
|
module.exports = {
|
|
uploadDoorLockData,
|
|
};
|