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

Unverified Commit 1688f92f authored by Edward W's avatar Edward W Committed by GitHub
Browse files

Disable addition of blank CSV fields into the DB and add location based on CSV...

Disable addition of blank CSV fields into the DB and add location based on CSV longitude and latitude values (#91)
parent 2592b4fc
...@@ -280,6 +280,28 @@ const updateAssetProperty = async(client, assetId, propertyId, newValue) => { ...@@ -280,6 +280,28 @@ const updateAssetProperty = async(client, assetId, propertyId, newValue) => {
return client.query(query, values); return client.query(query, values);
}; };
/**
* Updates the location of an asset by encoding longitude and latitude coordinates
* @param {*} client Node Postgres client
* @param {Number} assetId ID of the asset whose location is to be added/updated
* @param {Number} longitude Longitude of the asset
* @param {Number} latitude Latitude of the asset
*/
const addLocation = async(client, assetId, longitude, latitude) => {
// Generate the SQL command
const query = `
UPDATE asset
SET location=ST_MakePoint($1, $2)
WHERE id=$3
`;
// Generate the values to subsitute into the SQL command
const values = [longitude, latitude, assetId];
// Execute the SQL command
return client.query(query, values);
};
/** /**
* Stores contents of CSV into the DB * Stores contents of CSV into the DB
* *
...@@ -349,7 +371,7 @@ const storeCSV = async(assetTypeId, csvJson) => { ...@@ -349,7 +371,7 @@ const storeCSV = async(assetTypeId, csvJson) => {
} }
for (const propertyName in asset) { for (const propertyName in asset) {
if (propertyName !== 'asset_id') { 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 // Throw an error if CSV contains a header that is not associated with the selected asset type
if (!(propertyName in properties)) { if (!(propertyName in properties)) {
throw 'The selected CSV file either contains an empty column, is missing a header, or contains a property that is not being tracked (' + propertyName + ')'; throw 'The selected CSV file either contains an empty column, is missing a header, or contains a property that is not being tracked (' + propertyName + ')';
...@@ -365,19 +387,29 @@ const storeCSV = async(assetTypeId, csvJson) => { ...@@ -365,19 +387,29 @@ const storeCSV = async(assetTypeId, csvJson) => {
// Throw an error if a row fails to contain a value for a property that is required // Throw an error if a row fails to contain a value for a property that is required
if (value === '' && propertyIsRequired) { 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 + ', ' + JSON.stringify(asset) + ')';
} } else if (value === '') {
else if (assetProperties.length > 0) { continue;
} else if (assetProperties.length > 0) {
if (assetProperties[0].value !== value) { if (assetProperties[0].value !== value) {
await addAssetPropertyToHistory(client, assetId, propertyId, value, date); await addAssetPropertyToHistory(client, assetId, propertyId, value, date);
await updateAssetProperty(client, assetId, propertyId, value); await updateAssetProperty(client, assetId, propertyId, value);
} }
} } else {
else {
await addAssetPropertyToHistory(client, assetId, propertyId, value, date); await addAssetPropertyToHistory(client, assetId, propertyId, value, date);
await createAssetProperty(client, assetId, propertyId, value); await createAssetProperty(client, assetId, propertyId, value);
} }
} }
} }
// 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);
}
} }
await utils.db.commitTransaction(client); 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