2020-10-03 05:08:50 -07:00
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
|
|
|
import {
|
|
|
|
IBinaryData,
|
|
|
|
IBinaryKeyData,
|
|
|
|
IDataObject,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
2021-04-16 09:33:36 -07:00
|
|
|
NodeOperationError,
|
2020-10-03 05:08:50 -07:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
import {
|
|
|
|
cleanData,
|
|
|
|
mindeeApiRequest,
|
|
|
|
} from './GenericFunctions';
|
|
|
|
|
|
|
|
export class Mindee implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Mindee',
|
|
|
|
name: 'mindee',
|
2020-10-03 05:09:37 -07:00
|
|
|
icon: 'file:mindee.svg',
|
2020-10-03 05:08:50 -07:00
|
|
|
group: ['input'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
2021-07-03 05:40:16 -07:00
|
|
|
description: 'Consume Mindee API',
|
2020-10-03 05:08:50 -07:00
|
|
|
defaults: {
|
|
|
|
name: 'Mindee',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'mindeeReceiptApi',
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
resource: [
|
|
|
|
'receipt',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'mindeeInvoiceApi',
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
resource: [
|
|
|
|
'invoice',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Invoice',
|
|
|
|
value: 'invoice',
|
|
|
|
},
|
2020-10-03 05:09:37 -07:00
|
|
|
{
|
|
|
|
name: 'Receipt',
|
|
|
|
value: 'receipt',
|
|
|
|
},
|
2020-10-03 05:08:50 -07:00
|
|
|
],
|
|
|
|
default: 'receipt',
|
2020-10-22 06:46:03 -07:00
|
|
|
description: 'The resource to operate on.',
|
2020-10-03 05:08:50 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Operation',
|
|
|
|
name: 'operation',
|
|
|
|
type: 'options',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Predict',
|
|
|
|
value: 'predict',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'predict',
|
2020-10-22 06:46:03 -07:00
|
|
|
description: 'The resource to operate on.',
|
2020-10-03 05:08:50 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Binary Property',
|
|
|
|
name: 'binaryPropertyName',
|
|
|
|
type: 'string',
|
|
|
|
required: true,
|
|
|
|
default: 'data',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
operation: [
|
2020-10-22 06:46:03 -07:00
|
|
|
'predict',
|
2020-10-03 05:08:50 -07:00
|
|
|
],
|
|
|
|
resource: [
|
|
|
|
'receipt',
|
|
|
|
'invoice',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'Name of the binary property which containsthe data for the file to be uploaded',
|
2020-10-03 05:08:50 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'RAW Data',
|
|
|
|
name: 'rawData',
|
|
|
|
type: 'boolean',
|
|
|
|
default: false,
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'Returns the data exactly in the way it got received from the API',
|
2020-10-03 05:08:50 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
|
|
|
const returnData: IDataObject[] = [];
|
2022-04-22 09:29:51 -07:00
|
|
|
const length = items.length;
|
2020-10-03 05:08:50 -07:00
|
|
|
const qs: IDataObject = {};
|
|
|
|
let responseData;
|
|
|
|
const resource = this.getNodeParameter('resource', 0) as string;
|
|
|
|
const operation = this.getNodeParameter('operation', 0) as string;
|
|
|
|
for (let i = 0; i < length; i++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
if (resource === 'receipt') {
|
|
|
|
if (operation === 'predict') {
|
|
|
|
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i) as string;
|
|
|
|
|
|
|
|
const rawData = this.getNodeParameter('rawData', i) as boolean;
|
|
|
|
|
|
|
|
if (items[i].binary === undefined) {
|
|
|
|
throw new NodeOperationError(this.getNode(), 'No binary data exists on item!');
|
|
|
|
}
|
|
|
|
|
|
|
|
const item = items[i].binary as IBinaryKeyData;
|
|
|
|
|
|
|
|
const binaryData = item[binaryPropertyName] as IBinaryData;
|
2021-08-20 09:08:40 -07:00
|
|
|
const dataBuffer = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName);
|
2021-07-19 23:58:54 -07:00
|
|
|
|
|
|
|
if (binaryData === undefined) {
|
|
|
|
throw new NodeOperationError(this.getNode(), `No binary data property "${binaryPropertyName}" does not exists on item!`);
|
|
|
|
}
|
|
|
|
|
|
|
|
responseData = await mindeeApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
|
|
|
`/expense_receipts/v2/predict`,
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
formData: {
|
|
|
|
file: {
|
2021-08-20 09:08:40 -07:00
|
|
|
value: dataBuffer,
|
2021-07-19 23:58:54 -07:00
|
|
|
options: {
|
|
|
|
filename: binaryData.fileName,
|
|
|
|
},
|
2020-10-22 06:46:03 -07:00
|
|
|
},
|
2020-10-03 05:08:50 -07:00
|
|
|
},
|
|
|
|
},
|
2021-07-19 23:58:54 -07:00
|
|
|
);
|
2020-10-03 05:08:50 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (rawData === false) {
|
|
|
|
responseData = cleanData(responseData.predictions);
|
|
|
|
}
|
2020-10-03 05:08:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (resource === 'invoice') {
|
|
|
|
if (operation === 'predict') {
|
|
|
|
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i) as string;
|
|
|
|
|
|
|
|
const rawData = this.getNodeParameter('rawData', i) as boolean;
|
|
|
|
|
|
|
|
if (items[i].binary === undefined) {
|
|
|
|
throw new NodeOperationError(this.getNode(), 'No binary data exists on item!');
|
|
|
|
}
|
|
|
|
|
|
|
|
const item = items[i].binary as IBinaryKeyData;
|
|
|
|
|
|
|
|
const binaryData = item[binaryPropertyName] as IBinaryData;
|
2021-08-20 09:08:40 -07:00
|
|
|
const dataBuffer = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName);
|
2021-07-19 23:58:54 -07:00
|
|
|
|
|
|
|
if (binaryData === undefined) {
|
|
|
|
throw new NodeOperationError(this.getNode(), `No binary data property "${binaryPropertyName}" does not exists on item!`);
|
|
|
|
}
|
|
|
|
|
|
|
|
responseData = await mindeeApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
|
|
|
`/invoices/v1/predict`,
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
formData: {
|
|
|
|
file: {
|
2021-08-20 09:08:40 -07:00
|
|
|
value: dataBuffer,
|
2021-07-19 23:58:54 -07:00
|
|
|
options: {
|
|
|
|
filename: binaryData.fileName,
|
|
|
|
},
|
2020-10-22 06:46:03 -07:00
|
|
|
},
|
2020-10-03 05:08:50 -07:00
|
|
|
},
|
|
|
|
},
|
2021-07-19 23:58:54 -07:00
|
|
|
);
|
2020-10-03 05:08:50 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (rawData === false) {
|
|
|
|
responseData = cleanData(responseData.predictions);
|
|
|
|
}
|
2020-10-03 05:08:50 -07:00
|
|
|
}
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (Array.isArray(responseData)) {
|
|
|
|
returnData.push.apply(returnData, responseData as IDataObject[]);
|
|
|
|
} else if (responseData !== undefined) {
|
|
|
|
returnData.push(responseData as IDataObject);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
|
|
|
returnData.push({ error: error.message });
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
2020-10-03 05:08:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return [this.helpers.returnJsonArray(returnData)];
|
|
|
|
}
|
|
|
|
}
|