2023-03-09 09:13:15 -08:00
|
|
|
import type {
|
|
|
|
IExecuteFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
IDataObject,
|
|
|
|
JsonObject,
|
2024-02-14 07:29:09 -08:00
|
|
|
IHttpRequestMethods,
|
|
|
|
IRequestOptions,
|
2023-03-09 09:13:15 -08:00
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeApiError } from 'n8n-workflow';
|
2022-07-20 04:45:25 -07:00
|
|
|
|
|
|
|
export async function mindeeApiRequest(
|
2023-08-16 06:52:41 -07:00
|
|
|
this: IExecuteFunctions | ILoadOptionsFunctions,
|
2024-02-14 07:29:09 -08:00
|
|
|
method: IHttpRequestMethods,
|
2022-07-20 04:45:25 -07:00
|
|
|
path: string,
|
2022-12-02 06:25:21 -08:00
|
|
|
body: any = {},
|
2022-07-20 04:45:25 -07:00
|
|
|
qs: IDataObject = {},
|
|
|
|
option = {},
|
2022-08-17 08:50:24 -07:00
|
|
|
): Promise<any> {
|
2022-12-02 03:53:59 -08:00
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
2020-10-03 05:08:50 -07:00
|
|
|
|
2022-07-20 04:45:25 -07:00
|
|
|
let service;
|
2020-10-03 05:08:50 -07:00
|
|
|
|
|
|
|
if (resource === 'receipt') {
|
2022-07-20 04:45:25 -07:00
|
|
|
service = 'mindeeReceiptApi';
|
2020-10-03 05:08:50 -07:00
|
|
|
} else {
|
2022-07-20 04:45:25 -07:00
|
|
|
service = 'mindeeInvoiceApi';
|
2020-10-03 05:08:50 -07:00
|
|
|
}
|
|
|
|
|
2022-07-20 04:45:25 -07:00
|
|
|
const version = this.getNodeParameter('apiVersion', 0) as number;
|
|
|
|
// V1 of mindee is deprecated, we are keeping it for now but now V3 is active
|
|
|
|
const url =
|
|
|
|
version === 1
|
|
|
|
? `https://api.mindee.net/products${path}`
|
|
|
|
: `https://api.mindee.net/v1/products/mindee${path}`;
|
|
|
|
|
2024-02-14 07:29:09 -08:00
|
|
|
const options: IRequestOptions = {
|
2022-07-20 04:45:25 -07:00
|
|
|
headers: {},
|
2020-10-03 05:08:50 -07:00
|
|
|
method,
|
|
|
|
body,
|
|
|
|
qs,
|
2022-07-20 04:45:25 -07:00
|
|
|
uri: url,
|
2020-10-03 05:08:50 -07:00
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
try {
|
2023-02-27 19:39:43 -08:00
|
|
|
if (Object.keys(body as IDataObject).length === 0) {
|
2020-10-03 05:08:50 -07:00
|
|
|
delete options.body;
|
|
|
|
}
|
|
|
|
if (Object.keys(qs).length === 0) {
|
|
|
|
delete options.qs;
|
|
|
|
}
|
|
|
|
if (Object.keys(option).length !== 0) {
|
|
|
|
Object.assign(options, option);
|
|
|
|
}
|
2022-07-20 04:45:25 -07:00
|
|
|
|
|
|
|
return await this.helpers.requestWithAuthentication.call(this, service, options);
|
2020-10-03 05:08:50 -07:00
|
|
|
} catch (error) {
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
2020-10-03 05:08:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-20 04:45:25 -07:00
|
|
|
export function cleanDataPreviousApiVersions(predictions: IDataObject[]) {
|
2020-10-03 05:08:50 -07:00
|
|
|
const newData: IDataObject = {};
|
|
|
|
|
|
|
|
for (const key of Object.keys(predictions[0])) {
|
|
|
|
const data = predictions[0][key] as IDataObject | IDataObject[];
|
|
|
|
|
|
|
|
if (key === 'taxes' && data.length) {
|
|
|
|
newData[key] = {
|
|
|
|
amount: (data as IDataObject[])[0].amount,
|
|
|
|
rate: (data as IDataObject[])[0].rate,
|
|
|
|
};
|
2020-10-10 09:41:23 -07:00
|
|
|
} else if (key === 'locale') {
|
2022-07-20 04:45:25 -07:00
|
|
|
//@ts-ignore
|
2022-12-02 12:54:28 -08:00
|
|
|
newData.currency = data.currency;
|
2022-07-20 04:45:25 -07:00
|
|
|
//@ts-ignore
|
2022-12-02 12:54:28 -08:00
|
|
|
newData.locale = data.value;
|
2020-10-03 05:08:50 -07:00
|
|
|
} else {
|
2022-07-20 04:45:25 -07:00
|
|
|
newData[key] =
|
|
|
|
//@ts-ignore
|
|
|
|
data.value || data.name || data.raw || data.degrees || data.amount || data.iban;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newData;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function cleanData(document: IDataObject) {
|
|
|
|
// @ts-ignore
|
|
|
|
const prediction = document.inference.prediction as IDataObject;
|
|
|
|
const newData: IDataObject = {};
|
2022-12-02 12:54:28 -08:00
|
|
|
newData.id = document.id;
|
|
|
|
newData.name = document.name;
|
|
|
|
newData.number_of_pages = document.n_pages;
|
2022-07-20 04:45:25 -07:00
|
|
|
for (const key of Object.keys(prediction)) {
|
|
|
|
const data = prediction[key] as IDataObject | IDataObject[];
|
|
|
|
|
|
|
|
if (key === 'taxes' && data.length) {
|
|
|
|
newData[key] = {
|
|
|
|
amount: (data as IDataObject[])[0].amount,
|
|
|
|
rate: (data as IDataObject[])[0].rate,
|
|
|
|
};
|
|
|
|
} else if (key === 'locale') {
|
|
|
|
//@ts-ignore
|
2022-12-02 12:54:28 -08:00
|
|
|
newData.currency = data.currency;
|
2020-10-03 05:08:50 -07:00
|
|
|
//@ts-ignore
|
2022-12-02 12:54:28 -08:00
|
|
|
newData.locale = data.value;
|
2023-03-09 02:06:03 -08:00
|
|
|
} else if (key === 'line_items') {
|
|
|
|
const lineItems: IDataObject[] = [];
|
|
|
|
for (const lineItem of data as IDataObject[]) {
|
|
|
|
lineItems.push({
|
|
|
|
description: lineItem.description,
|
|
|
|
product_code: lineItem.product_code,
|
|
|
|
quantity: lineItem.quantity,
|
|
|
|
tax_amount: lineItem.tax_amount,
|
|
|
|
tax_rate: lineItem.tax_rate,
|
|
|
|
total_amount: lineItem.total_amount,
|
|
|
|
unit_price: lineItem.unit_price,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
newData[key] = lineItems;
|
2022-07-20 04:45:25 -07:00
|
|
|
} else {
|
|
|
|
newData[key] =
|
|
|
|
//@ts-ignore
|
|
|
|
data.value || data.name || data.raw || data.degrees || data.amount || data.iban;
|
2020-10-03 05:08:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newData;
|
|
|
|
}
|