Skip to content

Commit

Permalink
fix entity import
Browse files Browse the repository at this point in the history
  • Loading branch information
tcaiger committed Sep 20, 2024
1 parent 73863b7 commit 01313c6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export async function updateCountryEntities(
countryCode,
pushToDhis,
);

await transactingModels.entity.findOrCreate(
{ code: countryCode },
{
Expand All @@ -103,6 +104,7 @@ export async function updateCountryEntities(
},
);
const codes = []; // An array to hold all facility codes, allowing duplicate checking

for (let i = 0; i < entityObjects.length; i++) {
const entityObject = entityObjects[i];
const { entity_type: entityType } = entityObject;
Expand Down Expand Up @@ -189,7 +191,25 @@ export async function updateCountryEntities(
geojson.type === 'Polygon'
? { type: 'MultiPolygon', coordinates: [geojson.coordinates] }
: geojson;
await transactingModels.entity.updateRegionCoordinates(code, translatedGeojson);

try {
await transactingModels.entity.updateRegionCoordinates(code, translatedGeojson);
} catch (error) {
const largeGeoEntities = entityObjects.filter(entityObject => {
if (!entityObject?.geojson) return false;
const geoJsonString = JSON.stringify(entityObject.geojson);
// If the geo json is too large, we will hit the max payload size limit.
// Hard postgres max is 8000 characters, but we need to account for other data in the query payload
const maxGeoJsonPayload = 6500;
if (geoJsonString.length > maxGeoJsonPayload) {
return true;
}
});
const text = largeGeoEntities.map(entity => entity.code).join(', ');

error.message = `Error updating region coordinates for entities ${text}: ${error.message}`;
throw error;
}
}
}
return country;
Expand Down
9 changes: 8 additions & 1 deletion packages/database/src/TupaiaDatabase.js
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,8 @@ function addWhereClause(connection, baseQuery, where) {
return querySoFar; // Ignore undefined criteria
}
if (value === null) {
return querySoFar.whereNull(key);
const columnKey = getColSelector(connection, key);
return querySoFar.whereNull(columnKey);
}
const {
comparisonType = 'where',
Expand Down Expand Up @@ -748,5 +749,11 @@ function getColSelector(connection, inputColStr) {
return connection.raw(inputColStr);
}

const asGeoJsonPattern = /^ST_AsGeoJSON\((.+)\)$/;
if (asGeoJsonPattern.test(inputColStr)) {
const [, argsString] = inputColStr.match(asGeoJsonPattern);
return connection.raw(`ST_AsGeoJSON(${argsString})`);
}

return inputColStr;
}

0 comments on commit 01313c6

Please sign in to comment.