Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional checks in batch uploader #3398

Merged
merged 3 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions api/src/utils/batch/validation/spatial-validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,17 @@ export const getRecordFromShort = async (shortId: string): Promise<Record<string
`,
values: [shortId]
});
delete res.rows[0]['activity_payload'];
return res.rows[0];
if (res.rowCount > 0) {
delete res.rows[0]['activity_payload'];
return res.rows[0];
}
} catch (e) {
defaultLog.error({
message: '[getRecordFromShort]',
error: e
});
} finally {
connection.release();
}
};
/**
Expand All @@ -105,14 +109,15 @@ export const getLongIDFromShort = async (shortId: string): Promise<string> => {
`,
values: [shortId]
});

return res.rows[0]['activity_id'];
} catch (e) {
defaultLog.error({
message: '[getLongIDFromShort]',
error: e
});
throw new Error('Error validating geometry in the database' + e.message);
} finally {
connection.release();
}
};

Expand Down Expand Up @@ -145,6 +150,8 @@ export const getRecordTypeFromShort = async (shortId: string): Promise<string> =
error: e
});
throw new Error('Error validating geometry in the database' + e.message);
} finally {
connection.release();
}
};

Expand All @@ -161,11 +168,12 @@ export const getGeometryAsGeoJSONFromShort = async (shortId: string): Promise<st
where short_id = $1`,
values: [shortId]
});

return res.rows[0]['geog'];
} catch (e) {
console.log('error in getGeometryAsGeoJSONFromShort', e);
throw new Error('Error validating geometry in the database' + e.message);
} finally {
connection.release();
}
};

Expand Down
25 changes: 15 additions & 10 deletions api/src/utils/batch/validation/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ const DATE_TIME_FORMAT = 'YYYY-MM-DDThh:mm';

const invalidShortID: BatchCellValidationMessage = {
severity: 'error',
messageTitle: 'ShortID is not the correct format',
messageDetail: 'ShortID is not the correct format []'
messageTitle: 'ShortID is invalid',
messageDetail: 'ShortID is invalid'
};

const invalidRecordType: BatchCellValidationMessage = {
Expand Down Expand Up @@ -214,14 +214,22 @@ const _handleActivity_Monitoring_ChemicalTerrestrialAquaticPlant = async (
row: Record<string, any>
) => {
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]) ||
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down
Loading