Add processing for upload dlock
This commit is contained in:
@@ -5,96 +5,126 @@ const { fetchAllBookings, writeBookingReservation } = require('../services/offic
|
||||
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) => {
|
||||
const form = new IncomingForm();
|
||||
const fileParsers = [];
|
||||
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);
|
||||
}
|
||||
});
|
||||
form.on('file', (field, file) => {
|
||||
if (file && file.type === 'text/csv') {
|
||||
fileParsers.push(parseDoorLockDataFile(file));
|
||||
}
|
||||
});
|
||||
|
||||
const asyncWriteJobs = [];
|
||||
form.on('end', () => {
|
||||
Promise.all(fileParsers)
|
||||
.then((parserResults) => {
|
||||
const parsedData = [];
|
||||
const parserErrors = [];
|
||||
const unknownMembers = [];
|
||||
|
||||
parsedData.forEach((entry) => asyncWriteJobs.push(writeDoorLockEvent(entry)));
|
||||
parserResults.forEach((parserResult) => {
|
||||
parsedData.push(...parserResult.parsedData);
|
||||
parserErrors.push(...parserResult.errors);
|
||||
|
||||
Promise.all(asyncWriteJobs)
|
||||
.then(() => {
|
||||
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);
|
||||
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);
|
||||
}
|
||||
});
|
||||
});
|
||||
})
|
||||
.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)));
|
||||
const asyncWriteJobs = [];
|
||||
|
||||
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);
|
||||
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 = {
|
||||
|
||||
@@ -5,6 +5,7 @@ const { getAllIncidents } = require('../services/integration/reports');
|
||||
const { getMembersFeesForDateRange } = require('../services/integration/invoiceIntegration');
|
||||
const { deleteFeesFromORD, addFeesToORD } = require('../services/officeRnD/fees');
|
||||
const { checkBookingChanges } = require('../services/integration/checkBookingChange');
|
||||
const { checkIfProcessing } = require('../services/integration/processingStatus');
|
||||
|
||||
const getKnownOfficeResourceMappings = (req, res) => {
|
||||
const dataToFetch = [getMappingsFromDatabase(), fetchOffices(), fetchResources() ];
|
||||
@@ -111,10 +112,25 @@ const addFees = (req, res) => {
|
||||
}
|
||||
};
|
||||
|
||||
const checkProcessingStatus = (req, res) => {
|
||||
checkIfProcessing()
|
||||
.then((processing) => {
|
||||
res.send({
|
||||
processing
|
||||
})
|
||||
})
|
||||
.catch((error) => {
|
||||
console.log('Error checking if processing ');
|
||||
console.log(error);
|
||||
res.status(500).send(error);
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
getKnownOfficeResourceMappings,
|
||||
addNewMapping,
|
||||
getAllIncidentsController,
|
||||
getMemberIncidents,
|
||||
addFees,
|
||||
checkProcessingStatus,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user