2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2020-10-03 05:08:50 -07:00
|
|
|
IDataObject,
|
2023-03-06 08:33:32 -08:00
|
|
|
IExecuteFunctions,
|
2020-10-03 05:08:50 -07:00
|
|
|
INodeExecutionData,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeOperationError } from 'n8n-workflow';
|
2020-10-03 05:08:50 -07:00
|
|
|
|
2022-07-20 04:45:25 -07:00
|
|
|
import { cleanData, cleanDataPreviousApiVersions, mindeeApiRequest } from './GenericFunctions';
|
2020-10-03 05:08:50 -07:00
|
|
|
|
|
|
|
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'],
|
2023-03-09 02:06:03 -08:00
|
|
|
version: [1, 2, 3],
|
2020-10-03 05:08:50 -07:00
|
|
|
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: {
|
2022-07-20 04:45:25 -07:00
|
|
|
resource: ['receipt'],
|
2020-10-03 05:08:50 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'mindeeInvoiceApi',
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-07-20 04:45:25 -07:00
|
|
|
resource: ['invoice'],
|
2020-10-03 05:08:50 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
2022-07-20 04:45:25 -07:00
|
|
|
{
|
|
|
|
displayName: 'API Version',
|
|
|
|
name: 'apiVersion',
|
|
|
|
type: 'options',
|
|
|
|
isNodeSetting: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
'@version': [1],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: '1',
|
|
|
|
value: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: '3',
|
|
|
|
value: 3,
|
|
|
|
},
|
2023-03-09 02:06:03 -08:00
|
|
|
{
|
|
|
|
name: '4',
|
|
|
|
value: 4,
|
|
|
|
},
|
2022-07-20 04:45:25 -07:00
|
|
|
],
|
|
|
|
default: 1,
|
2023-03-09 02:06:03 -08:00
|
|
|
description: 'Which Mindee API Version to use',
|
2022-07-20 04:45:25 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'API Version',
|
|
|
|
name: 'apiVersion',
|
|
|
|
type: 'options',
|
|
|
|
isNodeSetting: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
'@version': [2],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: '1',
|
|
|
|
value: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: '3',
|
|
|
|
value: 3,
|
|
|
|
},
|
2023-03-09 02:06:03 -08:00
|
|
|
{
|
|
|
|
name: '4',
|
|
|
|
value: 4,
|
|
|
|
},
|
2022-07-20 04:45:25 -07:00
|
|
|
],
|
|
|
|
default: 3,
|
2023-03-09 02:06:03 -08:00
|
|
|
description: 'Which Mindee API Version to use',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'API Version',
|
|
|
|
name: 'apiVersion',
|
|
|
|
type: 'options',
|
|
|
|
isNodeSetting: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
'@version': [3],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: '1',
|
|
|
|
value: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: '3',
|
|
|
|
value: 3,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: '4',
|
|
|
|
value: 4,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 4,
|
|
|
|
description: 'Which Mindee API Version to use',
|
2022-07-20 04:45:25 -07:00
|
|
|
},
|
2020-10-03 05:08:50 -07:00
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2020-10-03 05:08:50 -07:00
|
|
|
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',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Operation',
|
|
|
|
name: 'operation',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2020-10-03 05:08:50 -07:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Predict',
|
|
|
|
value: 'predict',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'predict',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Binary Property',
|
|
|
|
name: 'binaryPropertyName',
|
|
|
|
type: 'string',
|
|
|
|
required: true,
|
|
|
|
default: 'data',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-07-20 04:45:25 -07:00
|
|
|
operation: ['predict'],
|
|
|
|
resource: ['receipt', 'invoice'],
|
2020-10-03 05:08:50 -07:00
|
|
|
},
|
|
|
|
},
|
2022-07-20 04:45: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-06-20 07:54:01 -07:00
|
|
|
description: 'Whether to return 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
|
|
|
let responseData;
|
2022-07-20 04:45:25 -07:00
|
|
|
const version = this.getNodeParameter('apiVersion', 0) as number;
|
2022-12-02 03:53:59 -08:00
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2022-07-20 04:45:25 -07:00
|
|
|
let endpoint;
|
2020-10-03 05:08:50 -07:00
|
|
|
for (let i = 0; i < length; i++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
if (resource === 'receipt') {
|
|
|
|
if (operation === 'predict') {
|
2022-11-18 05:31:38 -08:00
|
|
|
const rawData = this.getNodeParameter('rawData', i);
|
2023-03-06 08:33:32 -08:00
|
|
|
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
|
2023-03-06 08:33:32 -08:00
|
|
|
const binaryData = this.helpers.assertBinaryData(i, binaryPropertyName);
|
2021-08-20 09:08:40 -07:00
|
|
|
const dataBuffer = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName);
|
2021-07-19 23:58:54 -07:00
|
|
|
|
2022-07-20 04:45:25 -07:00
|
|
|
if (version === 1) {
|
|
|
|
responseData = await mindeeApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
2022-12-29 03:20:43 -08:00
|
|
|
'/expense_receipts/v2/predict',
|
2022-07-20 04:45:25 -07:00
|
|
|
{},
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
formData: {
|
|
|
|
file: {
|
|
|
|
value: dataBuffer,
|
|
|
|
options: {
|
|
|
|
filename: binaryData.fileName,
|
|
|
|
},
|
2021-07-19 23:58:54 -07:00
|
|
|
},
|
2020-10-22 06:46:03 -07:00
|
|
|
},
|
2020-10-03 05:08:50 -07:00
|
|
|
},
|
2022-07-20 04:45:25 -07:00
|
|
|
);
|
|
|
|
} else if (version === 3) {
|
|
|
|
endpoint = '/expense_receipts/v3/predict';
|
|
|
|
responseData = await mindeeApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
|
|
|
endpoint,
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
formData: {
|
|
|
|
document: {
|
|
|
|
value: dataBuffer,
|
|
|
|
options: {
|
|
|
|
filename: binaryData.fileName,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
2023-03-09 02:06:03 -08:00
|
|
|
} else if (version === 4) {
|
|
|
|
endpoint = '/expense_receipts/v4/predict';
|
|
|
|
responseData = await mindeeApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
|
|
|
endpoint,
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
formData: {
|
|
|
|
document: {
|
|
|
|
value: dataBuffer,
|
|
|
|
options: {
|
|
|
|
filename: binaryData.fileName,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
2022-07-20 04:45:25 -07:00
|
|
|
}
|
2022-12-02 12:54:28 -08:00
|
|
|
if (!rawData) {
|
2022-07-20 04:45:25 -07:00
|
|
|
if (version === 1) {
|
2023-02-27 19:39:43 -08:00
|
|
|
responseData = cleanDataPreviousApiVersions(
|
|
|
|
responseData.predictions as IDataObject[],
|
|
|
|
);
|
2023-03-09 02:06:03 -08:00
|
|
|
} else if (version === 3 || version === 4) {
|
2023-02-27 19:39:43 -08:00
|
|
|
responseData = cleanData(responseData.document as IDataObject);
|
2022-07-20 04:45:25 -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 (resource === 'invoice') {
|
|
|
|
if (operation === 'predict') {
|
2022-11-18 05:31:38 -08:00
|
|
|
const rawData = this.getNodeParameter('rawData', i);
|
2023-03-06 08:33:32 -08:00
|
|
|
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
|
2023-03-06 08:33:32 -08:00
|
|
|
const binaryData = this.helpers.assertBinaryData(i, binaryPropertyName);
|
2021-08-20 09:08:40 -07:00
|
|
|
const dataBuffer = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName);
|
2021-07-19 23:58:54 -07:00
|
|
|
|
2022-07-20 04:45:25 -07:00
|
|
|
if (version === 1) {
|
|
|
|
endpoint = '/invoices/v1/predict';
|
|
|
|
responseData = await mindeeApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
|
|
|
endpoint,
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
formData: {
|
|
|
|
file: {
|
|
|
|
value: dataBuffer,
|
|
|
|
options: {
|
|
|
|
filename: binaryData.fileName,
|
|
|
|
},
|
2021-07-19 23:58:54 -07:00
|
|
|
},
|
2020-10-22 06:46:03 -07:00
|
|
|
},
|
2020-10-03 05:08:50 -07:00
|
|
|
},
|
2022-07-20 04:45:25 -07:00
|
|
|
);
|
|
|
|
} else if (version === 3) {
|
|
|
|
endpoint = '/invoices/v3/predict';
|
|
|
|
responseData = await mindeeApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
|
|
|
endpoint,
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
formData: {
|
|
|
|
document: {
|
|
|
|
value: dataBuffer,
|
|
|
|
options: {
|
|
|
|
filename: binaryData.fileName,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
2023-03-09 02:06:03 -08:00
|
|
|
} else if (version === 4) {
|
|
|
|
endpoint = '/invoices/v4/predict';
|
|
|
|
responseData = await mindeeApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
|
|
|
endpoint,
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
formData: {
|
|
|
|
document: {
|
|
|
|
value: dataBuffer,
|
|
|
|
options: {
|
|
|
|
filename: binaryData.fileName,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
2022-07-20 04:45:25 -07:00
|
|
|
} else {
|
|
|
|
throw new NodeOperationError(this.getNode(), 'Invalid API version');
|
|
|
|
}
|
2022-12-02 12:54:28 -08:00
|
|
|
if (!rawData) {
|
2022-07-20 04:45:25 -07:00
|
|
|
if (version === 1) {
|
2023-02-27 19:39:43 -08:00
|
|
|
responseData = cleanDataPreviousApiVersions(
|
|
|
|
responseData.predictions as IDataObject[],
|
|
|
|
);
|
2023-03-09 02:06:03 -08:00
|
|
|
} else if (version === 3 || version === 4) {
|
2023-02-27 19:39:43 -08:00
|
|
|
responseData = cleanData(responseData.document as IDataObject);
|
2022-07-20 04:45:25 -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 (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)];
|
|
|
|
}
|
|
|
|
}
|