fix(KoboToolbox Node): Improve attachment matching logic and GeoJSON Polygon format (#3394)

* Fix query,sort + use question name in attachments

* Change Menu structure

* kobo: Clearer webhook name

* [kobo]: fix when no json filter

* Fix ambiguous attachment matching + GeoJSON Polygon format

* Fix kobo function

* Fix extra descriptions
This commit is contained in:
Yann Jouanique 2022-07-04 22:01:04 +02:00 committed by GitHub
parent 3c659682e9
commit 725d122da7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -49,7 +49,6 @@ export async function koBoToolboxApiRequest(this: IExecuteFunctions | IWebhookFu
results = response.results ? _.concat(results || [], response.results) : response;
if (returnAll && response.next) {
options.url = response.next;
continue;
}
else {
keepLooking = false;
@ -61,13 +60,14 @@ export async function koBoToolboxApiRequest(this: IExecuteFunctions | IWebhookFu
function parseGeoPoint(geoPoint: string): null | number[] {
// Check if it looks like a "lat lon z precision" flat string e.g. "-1.931161 30.079811 0 0" (lat, lon, elevation, precision)
// NOTE: we are discarding the elevation and precision values since they're not (well) supported in GeoJSON
const coordinates = _.split(geoPoint, ' ');
if (coordinates.length >= 2 && _.every(coordinates, coord => coord && /^-?\d+(?:\.\d+)?$/.test(_.toString(coord)))) {
// NOTE: GeoJSON uses lon, lat, while most common systems use lat, lon order!
return _.concat([
return [
_.toNumber(coordinates[1]),
_.toNumber(coordinates[0]),
], _.toNumber(coordinates[2]) ? _.toNumber(coordinates[2]) : []);
];
}
return null;
}
@ -102,10 +102,16 @@ const formatValue = (value: any, format: string): any => { //tslint:disable-line
const coordinates = _.compact(points.map(parseGeoPoint));
// Only return if all values are properly parsed
if (coordinates.length === points.length) {
return {
type: _.first(points) === _.last(points) ? 'Polygon' : 'LineString', // check if shape is closed or open
coordinates,
};
// If the shape is closed, declare it as Polygon, otherwise as LineString
return _.first(points) === _.last(points)
? {
type: 'Polygon',
coordinates: [coordinates]
}
: {
type: 'LineString',
coordinates,
};
}
}
@ -172,14 +178,22 @@ export async function downloadAttachments(this: IExecuteFunctions | IWebhookFunc
if (attachmentList && attachmentList.length) {
for (const [index, attachment] of attachmentList.entries()) {
// look for the question name linked to this attachment
const filename = attachment.filename;
const fileName = attachment.filename;
const sanitizedFileName = _.toString(fileName).replace(/_[^_]+(?=\.\w+)/,''); // strip suffix
let relatedQuestion = null;
if('question' === options.binaryNamingScheme) {
Object.keys(submission).forEach(question => {
if (filename.endsWith('/' + _.toString(submission[question]).replace(/\s/g, '_'))) {
for(let question of Object.keys(submission)) {
// The logic to map attachments to question is sometimes ambiguous:
// - If the attachment is linked to a question, the question's value is the same as the attachment's filename (with spaces replaced by underscores)
// - BUT sometimes the attachment's filename has an extra suffix, e.g. "My_Picture_0OdlaKJ.jpg", would map to the question "picture": "My Picture.jpg"
const sanitizedQuestionValue = _.toString(submission[question]).replace(/\s/g, '_'); // replace spaces with underscores
if (sanitizedFileName == sanitizedQuestionValue) {
relatedQuestion = question;
// Just use the first match...
break;
}
});
}
}
// Download attachment
@ -222,7 +236,6 @@ export async function downloadAttachments(this: IExecuteFunctions | IWebhookFunc
else {
binaryName = `${options.dataPropertyAttachmentsPrefixName || 'attachment_'}${index}`;
}
const fileName = filename.split('/').pop();
binaryItem.binary![binaryName] = await this.helpers.prepareBinaryData(response.body, fileName);
}