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

* 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

* Add credentials injection and testing

* Fix credential injection and lint issues

Co-authored-by: Yann Jouanique <yann.jouanique@oneacrefund.org>
Co-authored-by: Yann Jouanique <yann.jouanique@gmail.com>
Co-authored-by: Omar Ajoue <krynble@gmail.com>
Co-authored-by: Jan Oberhauser <janober@users.noreply.github.com>
This commit is contained in:
agobrech
2022-07-04 22:06:38 +02:00
committed by GitHub
parent 725d122da7
commit 637e81552f
3 changed files with 22 additions and 41 deletions

View File

@@ -30,7 +30,6 @@ export async function koBoToolboxApiRequest(this: IExecuteFunctions | IWebhookFu
url: '',
headers: {
'Accept': 'application/json',
'Authorization': `Token ${credentials.token}`,
},
json: true,
};
@@ -44,7 +43,7 @@ export async function koBoToolboxApiRequest(this: IExecuteFunctions | IWebhookFu
let results = null;
let keepLooking = true;
while (keepLooking) {
const response = await this.helpers.httpRequest(options);
const response = await this.helpers.httpRequestWithAuthentication.call(this, 'koBoToolboxApi', options);
// Append or set results
results = response.results ? _.concat(results || [], response.results) : response;
if (returnAll && response.next) {
@@ -106,7 +105,7 @@ const formatValue = (value: any, format: string): any => { //tslint:disable-line
return _.first(points) === _.last(points)
? {
type: 'Polygon',
coordinates: [coordinates]
coordinates: [coordinates],
}
: {
type: 'LineString',
@@ -183,12 +182,12 @@ export async function downloadAttachments(this: IExecuteFunctions | IWebhookFunc
let relatedQuestion = null;
if('question' === options.binaryNamingScheme) {
for(let question of Object.keys(submission)) {
for(const 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) {
if (sanitizedFileName === sanitizedQuestionValue) {
relatedQuestion = question;
// Just use the first match...
break;