2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IDataObject,
|
2020-12-19 05:08:31 -08:00
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
2023-03-09 09:13:15 -08:00
|
|
|
JsonObject,
|
2024-02-14 07:29:09 -08:00
|
|
|
IRequestOptions,
|
|
|
|
IHttpRequestMethods,
|
2023-03-09 09:13:15 -08:00
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeApiError } from 'n8n-workflow';
|
2020-12-19 05:08:31 -08:00
|
|
|
|
|
|
|
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[];
|
|
|
|
} = {};
|
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
export async function egoiApiRequest(
|
2023-08-16 06:52:41 -07:00
|
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
2024-02-14 07:29:09 -08:00
|
|
|
method: IHttpRequestMethods,
|
2022-08-01 13:47:55 -07:00
|
|
|
endpoint: string,
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
body: any = {},
|
|
|
|
qs: IDataObject = {},
|
2022-11-08 06:28:21 -08:00
|
|
|
_headers?: object,
|
2022-08-01 13:47:55 -07:00
|
|
|
): Promise<any> {
|
2022-04-14 23:00:47 -07:00
|
|
|
const credentials = await this.getCredentials('egoiApi');
|
2020-12-19 05:08:31 -08:00
|
|
|
|
2024-02-14 07:29:09 -08:00
|
|
|
const options: IRequestOptions = {
|
2020-12-19 05:08:31 -08:00
|
|
|
headers: {
|
2022-08-01 13:47:55 -07:00
|
|
|
accept: 'application/json',
|
|
|
|
Apikey: `${credentials.apiKey}`,
|
2020-12-19 05:08:31 -08:00
|
|
|
},
|
|
|
|
method,
|
|
|
|
qs,
|
|
|
|
body,
|
|
|
|
url: `https://api.egoiapp.com${endpoint}`,
|
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
2023-02-27 19:39:43 -08:00
|
|
|
if (Object.keys(body as IDataObject).length === 0) {
|
2020-12-19 05:08:31 -08:00
|
|
|
delete options.body;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2022-12-23 10:09:52 -08:00
|
|
|
return await this.helpers.request(options);
|
2020-12-19 05:08:31 -08:00
|
|
|
} catch (error) {
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
2020-12-19 05:08:31 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-13 09:11:56 -08:00
|
|
|
export async function getFields(this: IExecuteFunctions, listId: string) {
|
|
|
|
if (fieldCache[listId]) {
|
|
|
|
return fieldCache[listId];
|
|
|
|
}
|
|
|
|
fieldCache[listId] = await egoiApiRequest.call(this, 'GET', `/lists/${listId}/fields`);
|
|
|
|
return fieldCache[listId];
|
|
|
|
}
|
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
export async function egoiApiRequestAllItems(
|
|
|
|
this: IExecuteFunctions | ILoadOptionsFunctions,
|
|
|
|
propertyName: string,
|
2024-02-14 07:29:09 -08:00
|
|
|
method: IHttpRequestMethods,
|
2022-08-01 13:47:55 -07:00
|
|
|
endpoint: string,
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
body: any = {},
|
|
|
|
query: IDataObject = {},
|
|
|
|
): Promise<any> {
|
2020-12-19 05:08:31 -08:00
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
|
|
|
|
query.offset = 0;
|
|
|
|
query.count = 500;
|
|
|
|
|
|
|
|
do {
|
|
|
|
responseData = await egoiApiRequest.call(this, method, endpoint, body, query);
|
2023-02-27 19:39:43 -08:00
|
|
|
returnData.push.apply(returnData, responseData[propertyName] as IDataObject[]);
|
2020-12-19 05:08:31 -08:00
|
|
|
query.offset += query.count;
|
2022-08-01 13:47:55 -07:00
|
|
|
} while (responseData[propertyName] && responseData[propertyName].length !== 0);
|
2020-12-19 05:08:31 -08:00
|
|
|
|
|
|
|
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(
|
2023-03-03 09:49:19 -08:00
|
|
|
(accumulator: IDataObject, currentValue: IDataObject): any => {
|
2020-12-19 14:03:24 -08:00
|
|
|
const key = fieldsKeyValue[currentValue.field_id as string] as string;
|
2023-03-03 09:49:19 -08:00
|
|
|
return { [key]: currentValue.value, ...accumulator };
|
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;
|
|
|
|
}
|