2022-07-20 04:45:25 -07:00
|
|
|
import { OptionsWithUri } from 'request';
|
2020-10-03 05:08:50 -07:00
|
|
|
|
2022-07-20 04:45:25 -07:00
|
|
|
import { IExecuteFunctions, IExecuteSingleFunctions, ILoadOptionsFunctions } from 'n8n-core';
|
2020-10-03 05:08:50 -07:00
|
|
|
|
2022-07-20 04:45:25 -07:00
|
|
|
import { IDataObject, NodeApiError } from 'n8n-workflow';
|
|
|
|
|
|
|
|
export async function mindeeApiRequest(
|
|
|
|
this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
|
|
|
|
method: string,
|
|
|
|
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}`;
|
|
|
|
|
2020-10-03 05:08:50 -07:00
|
|
|
const options: OptionsWithUri = {
|
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 {
|
|
|
|
if (Object.keys(body).length === 0) {
|
|
|
|
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) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
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;
|
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;
|
|
|
|
}
|