diff --git a/api/src/utils/batch/validation/spatial-validation.ts b/api/src/utils/batch/validation/spatial-validation.ts index 07304d82e..f6339aa02 100644 --- a/api/src/utils/batch/validation/spatial-validation.ts +++ b/api/src/utils/batch/validation/spatial-validation.ts @@ -74,13 +74,17 @@ export const getRecordFromShort = async (shortId: string): Promise 0) { + delete res.rows[0]['activity_payload']; + return res.rows[0]; + } } catch (e) { defaultLog.error({ message: '[getRecordFromShort]', error: e }); + } finally { + connection.release(); } }; /** @@ -105,7 +109,6 @@ export const getLongIDFromShort = async (shortId: string): Promise => { `, values: [shortId] }); - return res.rows[0]['activity_id']; } catch (e) { defaultLog.error({ @@ -113,6 +116,8 @@ export const getLongIDFromShort = async (shortId: string): Promise => { error: e }); throw new Error('Error validating geometry in the database' + e.message); + } finally { + connection.release(); } }; @@ -145,6 +150,8 @@ export const getRecordTypeFromShort = async (shortId: string): Promise = error: e }); throw new Error('Error validating geometry in the database' + e.message); + } finally { + connection.release(); } }; @@ -161,11 +168,12 @@ export const getGeometryAsGeoJSONFromShort = async (shortId: string): Promise ) => { try { + const isValidShortID = validateShortID(shortId, ActivityLetter.Activity_Treatment_ChemicalPlantAquatic); + if (!isValidShortID) { + result.validationMessages.push(invalidShortID); + return; + } const expectedRecordTypes = [ 'Activity_Treatment_ChemicalPlantAquatic', 'Activity_Treatment_ChemicalPlantTerrestrial' ]; const batchUploadInvasivePlantRow = 'Monitoring - Terrestrial Invasive Plant'; const batchUploadTerrestrialPlantRow = 'Monitoring - Aquatic Invasive Plant'; - const isValidShortID = validateShortID(shortId, ActivityLetter.Activity_Treatment_ChemicalPlantAquatic); const linkedRecord = await getRecordFromShort(shortId); + if (!linkedRecord) { + result.validationMessages.push(invalidLongID(shortId)); + return; + } const isItTheRightRecordType = expectedRecordTypes.includes(linkedRecord['activity_subtype']); const doTheSpeciesMatch = linkedRecord['species_treated']?.includes(row.data[batchUploadInvasivePlantRow]) || @@ -240,15 +248,9 @@ const _handleActivity_Monitoring_ChemicalTerrestrialAquaticPlant = async ( if (!isItTheRightRecordType) { result.validationMessages.push(invalidRecordType); } - if (!isValidShortID) { - result.validationMessages.push(invalidShortID); - } if (!linkedGeoJSON) { result.validationMessages.push(invalidLinkedGeoJSON); } - if (!linkedRecord) { - result.validationMessages.push(invalidLongID(shortId)); - } if (!isValidGeoJSON) { result.validationMessages.push(invalidWKT); } @@ -293,9 +295,12 @@ async function _validateCell( switch (templateColumn?.dataType) { case 'linked_id': + // linked_id is optional, skip this column if data not present + if (!data) { + break; + } const thisRecordType = template.subtype; switch (thisRecordType) { - // chem monitoring case 'Activity_Monitoring_ChemicalTerrestrialAquaticPlant': await _handleActivity_Monitoring_ChemicalTerrestrialAquaticPlant(data, result, row); break;