IEEE.org     |     IEEE Xplore Digital Library     |     IEEE Standards     |     IEEE Spectrum     |     More Sites

Unverified Commit 340806d7 authored by Edward W's avatar Edward W Committed by GitHub
Browse files

Fixed missing latitude/longitude errors and period in CSV header bug (#105)

parent 09c2470d
......@@ -87,7 +87,7 @@ const storeCSV = async(req, res, next) => {
const csvFile = req.file;
const csvPath = csvFile.path;
try {
const json = await csv().fromFile(csvPath);
const json = await csv({flatKeys: true}).fromFile(csvPath);
const result = await db.assetDefinitions.storeCSV(assetTypeId, json);
res.json({result: result});
} catch (error) {
......
......@@ -371,6 +371,11 @@ const storeCSV = async(assetTypeId, csvJson) => {
throw 'The selected CSV file is missing an asset ID column.';
}
// Check that latitude and longitude are specified in the CSV
if (!('latitude' in asset) || !('longitude' in asset)) {
throw 'The selected CSV file is missing a latitude and/or longitude column';
}
// Check that all headers associated with the selected asset type are contained in the CSV
for (const propertyName in properties) {
if (!(propertyName in asset)) {
......@@ -403,7 +408,7 @@ const storeCSV = async(assetTypeId, csvJson) => {
if (propertyName !== 'asset_id' && propertyName !== 'longitude' && propertyName !== 'latitude') {
// Throw an error if CSV contains a header that is not associated with the selected asset type
if (!(propertyName in properties)) {
throw 'The selected CSV file contains a property that is not affiliated with the selected asset type. (' + propertyName + ')';
throw 'The selected CSV file may contain a property that is not affiliated with the selected asset type. If this is not the case, make sure to check your CSV for irrelevant columns such as blank columns and columns with blank headers. (' + propertyName + ')';
}
property = properties[propertyName];
......@@ -415,7 +420,7 @@ const storeCSV = async(assetTypeId, csvJson) => {
let date = new Date();
// Throw an error if a row fails to contain a value for a property that is required
if (value === '' && propertyIsRequired) {
throw 'The selected CSV file is missing a required value (' + propertyName + ', ' + JSON.stringify(asset) + ')';
throw 'The selected CSV file is missing a required value (' + propertyName + ', Asset ID ' + assetId + ')';
} else if (value === '') {
continue;
} else if (assetProperties.length > 0) {
......@@ -431,14 +436,13 @@ const storeCSV = async(assetTypeId, csvJson) => {
}
// Add location of asset to the DB
if (!('latitude' in properties) || !('longitude' in properties)) {
throw 'The selected CSV file is missing a latitude and/or longitude column';
} else {
let longitude = parseFloat(properties['longitude']);
let latitude = parseFloat(properties['latitude']);
await addLocation(client, assetId, longitude, latitude);
let longitude = parseFloat(asset['longitude']);
let latitude = parseFloat(asset['latitude']);
if (isNaN(longitude) || isNaN(latitude)) {
throw 'The selected CSV file contains a row missing a longitude and/or latitude value (Asset ID ' + assetId + ')';
}
await addLocation(client, assetId, longitude, latitude);
}
await utils.db.commitTransaction(client);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment