diff --git a/JS/1234.xlsx b/JS/1234.xlsx index 41708a4..60e483c 100644 Binary files a/JS/1234.xlsx and b/JS/1234.xlsx differ diff --git a/JS/excelreader.js b/JS/excelreader.js index a64c251..a99896f 100644 --- a/JS/excelreader.js +++ b/JS/excelreader.js @@ -10,18 +10,40 @@ const { all } = require('express/lib/application'); //const myfilepath = Path.join(__dirname, 'Inventory', 'samplepc1234.xlsx'); function readInventoryFile(filepath) { + //Checking for correct file type + const fileinfo = Path.parse(filepath); + if (fileinfo.ext !== ".xlsx") { + return {error: "Given file is not xlsx format"} + } + let workbook = xlsx.readFileSync(filepath); let sheetNames = workbook.SheetNames; let sheetData = workbook.Sheets[sheetNames[0]]; + let system; + //Here is where we use known cell locations of the specs needed to build the systems file - let serialnum = sheetData.D1.v; - let model = sheetData.D2.v; - let OS = sheetData.D3.v; - let price = sheetData.D4.v; - let specs = [sheetData.D5.v, sheetData.D6.v, sheetData.D7.v, sheetData.D8.v]; + try { + system = new System( + sheetData.D1.v, + sheetData.D2.v, + sheetData.D3.v, + sheetData.D4.v, + [sheetData.D5.v, sheetData.D6.v, sheetData.D7.v, sheetData.D8.v]); + } + catch (error) { + console.error(error); + } + + //Checking if file is formatted correctly + if (!system) { + return {error: "One or more necessary values are not defined"} + } + if (parseInt(fileinfo.name) !== system.serialnum) { + return {error: "filename does not match serial number"}; + } - return new System(serialnum, model, OS, price, specs); + return system; } @@ -29,18 +51,37 @@ var inventoryWatcher = chokidar.watch(Path.join(__dirname, "Inventory"), { ignored: /(^|[\/\\])\../, persistent: true, awaitWriteFinish: true, + ignoreInitial: true }); console.log(`Watching directory for changes...` ) inventoryWatcher .on('add', function(filepath) { - dbtools.insertSystem(readInventoryFile(filepath)); + const system = readInventoryFile(filepath); + if (system.error) { + console.error(system.error); + } + else { + dbtools.insertSystem(system); + } + }) .on('change', function(filepath) { - dbtools.updateSystem(readInventoryFile(filepath)); + const system = readInventoryFile(filepath); + if (system.error) { + console.error(system.error); + } + else { + dbtools.updateSystem(system); + } }) .on('unlink', function(filepath) { - dbtools.deleteSystem(parseInt(Path.basename(filepath, '.xlsx'))); + if (Path.extname(filepath) !== ".xlsx") { + console.log("inventoryWatcher: a non xlsx file has been deleted"); + } + else { + dbtools.deleteSystem(parseInt(Path.basename(filepath, '.xlsx'))); + } }) .on('error', function(error) { console.error('Error happened', error);