2020-12-19 05:08:31 -08:00
|
|
|
import {
|
|
|
|
OptionsWithUrl,
|
|
|
|
} from 'request';
|
|
|
|
|
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IExecuteSingleFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
|
|
|
import {
|
2021-04-16 09:33:36 -07:00
|
|
|
IDataObject, NodeApiError,
|
2020-12-19 05:08:31 -08:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
interface IContact {
|
2020-12-19 14:03:24 -08:00
|
|
|
tags: [];
|
|
|
|
base: IDataObject;
|
|
|
|
extra: IDataObject[];
|
2020-12-19 05:08:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
const fieldCache: {
|
|
|
|
[key: string]: IDataObject[];
|
|
|
|
} = {};
|
|
|
|
|
|
|
|
|
|
|
|
export async function getFields(this: IExecuteFunctions, listId: string) {
|
|
|
|
if (fieldCache[listId]) {
|
|
|
|
return fieldCache[listId];
|
|
|
|
}
|
|
|
|
fieldCache[listId] = await egoiApiRequest.call(this, 'GET', `/lists/${listId}/fields`);
|
2020-12-19 14:03:24 -08:00
|
|
|
return fieldCache[listId];
|
2020-12-19 05:08:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export async function egoiApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: any = {}, qs: IDataObject = {}, headers?: object): Promise<any> { // tslint:disable-line:no-any
|
|
|
|
|
|
|
|
const credentials = this.getCredentials('egoiApi') as IDataObject;
|
|
|
|
|
|
|
|
const options: OptionsWithUrl = {
|
|
|
|
headers: {
|
|
|
|
'accept': 'application/json',
|
|
|
|
'Apikey': `${credentials.apiKey}`,
|
|
|
|
},
|
|
|
|
method,
|
|
|
|
qs,
|
|
|
|
body,
|
|
|
|
url: `https://api.egoiapp.com${endpoint}`,
|
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (Object.keys(body).length === 0) {
|
|
|
|
delete options.body;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
return await this.helpers.request!(options);
|
|
|
|
|
|
|
|
} catch (error) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2020-12-19 05:08:31 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function egoiApiRequestAllItems(this: IExecuteFunctions | ILoadOptionsFunctions, propertyName: string, method: string, endpoint: string, body: any = {}, query: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
|
|
|
|
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
|
|
|
|
query.offset = 0;
|
|
|
|
query.count = 500;
|
|
|
|
|
|
|
|
do {
|
|
|
|
responseData = await egoiApiRequest.call(this, method, endpoint, body, query);
|
|
|
|
returnData.push.apply(returnData, responseData[propertyName]);
|
|
|
|
query.offset += query.count;
|
|
|
|
} while (
|
|
|
|
responseData[propertyName] && responseData[propertyName].length !== 0
|
|
|
|
);
|
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export async function simplify(this: IExecuteFunctions, contacts: IContact[], listId: string) {
|
|
|
|
let fields = await getFields.call(this, listId);
|
|
|
|
|
|
|
|
fields = fields.filter((element: IDataObject) => element.type === 'extra');
|
|
|
|
const fieldsKeyValue: IDataObject = {};
|
|
|
|
for (const field of fields) {
|
|
|
|
fieldsKeyValue[field.field_id as string] = field.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
const data: IDataObject[] = [];
|
|
|
|
|
|
|
|
for (const contact of contacts) {
|
|
|
|
const extras = contact.extra.reduce(
|
2020-12-19 14:03:24 -08:00
|
|
|
(acumulator: IDataObject, currentValue: IDataObject): any => { // tslint:disable-line:no-any
|
|
|
|
const key = fieldsKeyValue[currentValue.field_id as string] as string;
|
|
|
|
return { [key]: currentValue.value, ...acumulator };
|
2020-12-19 05:08:31 -08:00
|
|
|
},
|
2020-12-19 14:03:24 -08:00
|
|
|
{},
|
2020-12-19 05:08:31 -08:00
|
|
|
);
|
|
|
|
data.push({
|
|
|
|
...contact.base,
|
|
|
|
...extras,
|
|
|
|
tags: contact.tags,
|
2020-12-19 14:03:24 -08:00
|
|
|
});
|
2020-12-19 05:08:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|