2022-08-01 13:47:55 -07:00
|
|
|
import { IExecuteFunctions, IExecuteSingleFunctions, ILoadOptionsFunctions } from 'n8n-core';
|
2019-11-09 16:54:25 -08:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { IDataObject, INodePropertyOptions, NodeApiError, NodeOperationError } from 'n8n-workflow';
|
2019-11-09 16:54:25 -08:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { OptionsWithUri } from 'request';
|
2019-11-12 15:00:58 -08:00
|
|
|
|
|
|
|
interface ScriptsOptions {
|
|
|
|
script?: any; //tslint:disable-line:no-any
|
|
|
|
'script.param'?: any; //tslint:disable-line:no-any
|
|
|
|
'script.prerequest'?: any; //tslint:disable-line:no-any
|
|
|
|
'script.prerequest.param'?: any; //tslint:disable-line:no-any
|
|
|
|
'script.presort'?: any; //tslint:disable-line:no-any
|
|
|
|
'script.presort.param'?: any; //tslint:disable-line:no-any
|
|
|
|
}
|
2019-12-01 04:00:21 -08:00
|
|
|
interface LayoutObject {
|
|
|
|
name: string;
|
|
|
|
isFolder?: boolean;
|
2022-08-01 13:47:55 -07:00
|
|
|
folderLayoutNames?: LayoutObject[];
|
2019-12-01 04:00:21 -08:00
|
|
|
}
|
2019-11-14 04:02:57 -08:00
|
|
|
|
2021-02-06 07:42:07 -08:00
|
|
|
interface ScriptObject {
|
|
|
|
name: string;
|
|
|
|
isFolder?: boolean;
|
2022-08-01 13:47:55 -07:00
|
|
|
folderScriptNames?: LayoutObject[];
|
2021-02-06 07:42:07 -08:00
|
|
|
}
|
|
|
|
|
2019-11-09 16:54:25 -08:00
|
|
|
/**
|
|
|
|
* Make an API request to ActiveCampaign
|
|
|
|
*
|
|
|
|
* @param {IHookFunctions} this
|
|
|
|
* @param {string} method
|
|
|
|
* @returns {Promise<any>}
|
|
|
|
*/
|
2022-08-01 13:47:55 -07:00
|
|
|
export async function layoutsApiRequest(
|
|
|
|
this: ILoadOptionsFunctions | IExecuteFunctions | IExecuteSingleFunctions,
|
|
|
|
): Promise<INodePropertyOptions[]> {
|
|
|
|
// tslint:disable-line:no-any
|
2019-11-09 16:54:25 -08:00
|
|
|
const token = await getToken.call(this);
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('fileMaker');
|
2019-11-09 16:54:25 -08:00
|
|
|
|
|
|
|
const host = credentials.host as string;
|
|
|
|
const db = credentials.db as string;
|
|
|
|
|
|
|
|
const url = `https://${host}/fmi/data/v1/databases/${db}/layouts`;
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: {
|
2022-08-01 13:47:55 -07:00
|
|
|
Authorization: `Bearer ${token}`,
|
2019-11-09 16:54:25 -08:00
|
|
|
},
|
|
|
|
method: 'GET',
|
|
|
|
uri: url,
|
2020-10-22 06:46:03 -07:00
|
|
|
json: true,
|
2019-11-09 16:54:25 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
const responseData = await this.helpers.request!(options);
|
2019-12-01 04:00:21 -08:00
|
|
|
const items = parseLayouts(responseData.response.layouts);
|
2022-08-01 13:47:55 -07:00
|
|
|
items.sort((a, b) => (a.name > b.name ? 0 : 1));
|
2019-12-01 04:00:21 -08:00
|
|
|
return items;
|
2019-11-09 16:54:25 -08:00
|
|
|
} catch (error) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2019-11-09 16:54:25 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-01 04:00:21 -08:00
|
|
|
function parseLayouts(layouts: LayoutObject[]): INodePropertyOptions[] {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
for (const layout of layouts) {
|
2022-08-01 13:47:55 -07:00
|
|
|
if (layout.isFolder!) {
|
2019-12-01 04:00:21 -08:00
|
|
|
returnData.push(...parseLayouts(layout.folderLayoutNames!));
|
|
|
|
} else {
|
|
|
|
returnData.push({
|
|
|
|
name: layout.name,
|
|
|
|
value: layout.name,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
}
|
|
|
|
|
2019-11-09 16:54:25 -08:00
|
|
|
/**
|
|
|
|
* Make an API request to ActiveCampaign
|
|
|
|
*
|
|
|
|
* @returns {Promise<any>}
|
|
|
|
*/
|
2022-08-01 13:47:55 -07:00
|
|
|
export async function getFields(
|
|
|
|
this: ILoadOptionsFunctions,
|
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
): Promise<any> {
|
2019-11-09 16:54:25 -08:00
|
|
|
const token = await getToken.call(this);
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('fileMaker');
|
2019-11-09 16:54:25 -08:00
|
|
|
const layout = this.getCurrentNodeParameter('layout') as string;
|
|
|
|
|
|
|
|
const host = credentials.host as string;
|
|
|
|
const db = credentials.db as string;
|
|
|
|
|
|
|
|
const url = `https://${host}/fmi/data/v1/databases/${db}/layouts/${layout}`;
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: {
|
2022-08-01 13:47:55 -07:00
|
|
|
Authorization: `Bearer ${token}`,
|
2019-11-09 16:54:25 -08:00
|
|
|
},
|
|
|
|
method: 'GET',
|
|
|
|
uri: url,
|
2020-10-22 06:46:03 -07:00
|
|
|
json: true,
|
2019-11-09 16:54:25 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
const responseData = await this.helpers.request!(options);
|
|
|
|
return responseData.response.fieldMetaData;
|
|
|
|
} catch (error) {
|
|
|
|
// If that data does not exist for some reason return the actual error
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-09 18:14:10 -08:00
|
|
|
/**
|
|
|
|
* Make an API request to ActiveCampaign
|
|
|
|
*
|
|
|
|
* @returns {Promise<any>}
|
|
|
|
*/
|
2022-08-01 13:47:55 -07:00
|
|
|
export async function getPortals(
|
|
|
|
this: ILoadOptionsFunctions,
|
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
): Promise<any> {
|
2019-11-09 18:14:10 -08:00
|
|
|
const token = await getToken.call(this);
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('fileMaker');
|
2019-11-09 18:14:10 -08:00
|
|
|
const layout = this.getCurrentNodeParameter('layout') as string;
|
|
|
|
|
|
|
|
const host = credentials.host as string;
|
|
|
|
const db = credentials.db as string;
|
|
|
|
|
|
|
|
const url = `https://${host}/fmi/data/v1/databases/${db}/layouts/${layout}`;
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: {
|
2022-08-01 13:47:55 -07:00
|
|
|
Authorization: `Bearer ${token}`,
|
2019-11-09 18:14:10 -08:00
|
|
|
},
|
|
|
|
method: 'GET',
|
|
|
|
uri: url,
|
2020-10-22 06:46:03 -07:00
|
|
|
json: true,
|
2019-11-09 18:14:10 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
const responseData = await this.helpers.request!(options);
|
|
|
|
return responseData.response.portalMetaData;
|
|
|
|
} catch (error) {
|
|
|
|
// If that data does not exist for some reason return the actual error
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-11 17:01:43 -08:00
|
|
|
/**
|
|
|
|
* Make an API request to ActiveCampaign
|
|
|
|
*
|
|
|
|
* @returns {Promise<any>}
|
|
|
|
*/
|
2022-08-01 13:47:55 -07:00
|
|
|
export async function getScripts(
|
|
|
|
this: ILoadOptionsFunctions,
|
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
): Promise<any> {
|
2019-11-11 17:01:43 -08:00
|
|
|
const token = await getToken.call(this);
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('fileMaker');
|
2019-11-11 17:01:43 -08:00
|
|
|
|
|
|
|
const host = credentials.host as string;
|
|
|
|
const db = credentials.db as string;
|
|
|
|
|
|
|
|
const url = `https://${host}/fmi/data/v1/databases/${db}/scripts`;
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: {
|
2022-08-01 13:47:55 -07:00
|
|
|
Authorization: `Bearer ${token}`,
|
2019-11-11 17:01:43 -08:00
|
|
|
},
|
|
|
|
method: 'GET',
|
|
|
|
uri: url,
|
2020-10-22 06:46:03 -07:00
|
|
|
json: true,
|
2019-11-11 17:01:43 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
const responseData = await this.helpers.request!(options);
|
2021-02-06 07:42:07 -08:00
|
|
|
const items = parseScriptsList(responseData.response.scripts);
|
2022-08-01 13:47:55 -07:00
|
|
|
items.sort((a, b) => (a.name > b.name ? 0 : 1));
|
2021-02-06 07:42:07 -08:00
|
|
|
return items;
|
2019-11-11 17:01:43 -08:00
|
|
|
} catch (error) {
|
|
|
|
// If that data does not exist for some reason return the actual error
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-06 07:42:07 -08:00
|
|
|
function parseScriptsList(scripts: ScriptObject[]): INodePropertyOptions[] {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
for (const script of scripts) {
|
2022-08-01 13:47:55 -07:00
|
|
|
if (script.isFolder!) {
|
2021-02-06 07:42:07 -08:00
|
|
|
returnData.push(...parseScriptsList(script.folderScriptNames!));
|
|
|
|
} else if (script.name !== '-') {
|
|
|
|
returnData.push({
|
|
|
|
name: script.name,
|
|
|
|
value: script.name,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
}
|
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
export async function getToken(
|
|
|
|
this: ILoadOptionsFunctions | IExecuteFunctions | IExecuteSingleFunctions,
|
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
): Promise<any> {
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('fileMaker');
|
2019-11-09 16:54:25 -08:00
|
|
|
|
|
|
|
const host = credentials.host as string;
|
|
|
|
const db = credentials.db as string;
|
|
|
|
const login = credentials.login as string;
|
|
|
|
const password = credentials.password as string;
|
|
|
|
|
|
|
|
const url = `https://${host}/fmi/data/v1/databases/${db}/sessions`;
|
|
|
|
|
|
|
|
let requestOptions: OptionsWithUri;
|
|
|
|
// Reset all values
|
|
|
|
requestOptions = {
|
|
|
|
uri: url,
|
|
|
|
headers: {},
|
|
|
|
method: 'POST',
|
2020-10-22 06:46:03 -07:00
|
|
|
json: true,
|
2019-11-09 16:54:25 -08:00
|
|
|
//rejectUnauthorized: !this.getNodeParameter('allowUnauthorizedCerts', itemIndex, false) as boolean,
|
|
|
|
};
|
|
|
|
requestOptions.auth = {
|
|
|
|
user: login as string,
|
|
|
|
pass: password as string,
|
|
|
|
};
|
|
|
|
requestOptions.body = {
|
2022-08-01 13:47:55 -07:00
|
|
|
fmDataSource: [
|
2019-11-09 16:54:25 -08:00
|
|
|
{
|
2022-08-01 13:47:55 -07:00
|
|
|
database: host,
|
|
|
|
username: login as string,
|
|
|
|
password: password as string,
|
2020-10-22 06:46:03 -07:00
|
|
|
},
|
|
|
|
],
|
2019-11-09 16:54:25 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await this.helpers.request!(requestOptions);
|
|
|
|
|
|
|
|
if (typeof response === 'string') {
|
2022-08-01 13:47:55 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
'Response body is not valid JSON. Change "Response Format" to "String"',
|
|
|
|
);
|
2019-11-09 16:54:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return response.response.token;
|
|
|
|
} catch (error) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2019-11-09 16:54:25 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
export async function logout(
|
|
|
|
this: ILoadOptionsFunctions | IExecuteFunctions | IExecuteSingleFunctions,
|
|
|
|
token: string,
|
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
): Promise<any> {
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('fileMaker');
|
2019-11-09 16:54:25 -08:00
|
|
|
|
|
|
|
const host = credentials.host as string;
|
|
|
|
const db = credentials.db as string;
|
|
|
|
|
|
|
|
const url = `https://${host}/fmi/data/v1/databases/${db}/sessions/${token}`;
|
|
|
|
|
|
|
|
let requestOptions: OptionsWithUri;
|
|
|
|
// Reset all values
|
|
|
|
requestOptions = {
|
|
|
|
uri: url,
|
|
|
|
headers: {},
|
|
|
|
method: 'DELETE',
|
2020-10-22 06:46:03 -07:00
|
|
|
json: true,
|
2019-11-09 16:54:25 -08:00
|
|
|
//rejectUnauthorized: !this.getNodeParameter('allowUnauthorizedCerts', itemIndex, false) as boolean,
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await this.helpers.request!(requestOptions);
|
|
|
|
|
|
|
|
if (typeof response === 'string') {
|
2022-08-01 13:47:55 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
'Response body is not valid JSON. Change "Response Format" to "String"',
|
|
|
|
);
|
2019-11-09 16:54:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
|
|
|
} catch (error) {
|
2022-08-01 13:47:55 -07:00
|
|
|
const errorMessage =
|
|
|
|
error.response.body.messages[0].message + '(' + error.response.body.messages[0].message + ')';
|
2019-11-09 16:54:25 -08:00
|
|
|
|
|
|
|
if (errorMessage !== undefined) {
|
|
|
|
throw errorMessage;
|
|
|
|
}
|
|
|
|
throw error.response.body;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-13 11:51:45 -07:00
|
|
|
export function parseSort(this: IExecuteFunctions, i: number): object | null {
|
2019-11-11 17:01:43 -08:00
|
|
|
let sort;
|
2020-03-13 11:51:45 -07:00
|
|
|
const setSort = this.getNodeParameter('setSort', i, false);
|
2019-11-11 17:01:43 -08:00
|
|
|
if (!setSort) {
|
|
|
|
sort = null;
|
|
|
|
} else {
|
|
|
|
sort = [];
|
2020-03-13 11:51:45 -07:00
|
|
|
const sortParametersUi = this.getNodeParameter('sortParametersUi', i, {}) as IDataObject;
|
2019-11-11 17:01:43 -08:00
|
|
|
if (sortParametersUi.rules !== undefined) {
|
|
|
|
// @ts-ignore
|
|
|
|
for (const parameterData of sortParametersUi!.rules as IDataObject[]) {
|
|
|
|
// @ts-ignore
|
|
|
|
sort.push({
|
2022-08-01 13:47:55 -07:00
|
|
|
fieldName: parameterData!.name as string,
|
|
|
|
sortOrder: parameterData!.value,
|
2019-11-11 17:01:43 -08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sort;
|
|
|
|
}
|
|
|
|
|
2020-03-13 11:51:45 -07:00
|
|
|
export function parseScripts(this: IExecuteFunctions, i: number): object | null {
|
|
|
|
const setScriptAfter = this.getNodeParameter('setScriptAfter', i, false);
|
|
|
|
const setScriptBefore = this.getNodeParameter('setScriptBefore', i, false);
|
|
|
|
const setScriptSort = this.getNodeParameter('setScriptSort', i, false);
|
2019-11-11 17:01:43 -08:00
|
|
|
|
|
|
|
if (!setScriptAfter && setScriptBefore && setScriptSort) {
|
|
|
|
return {};
|
|
|
|
} else {
|
2019-11-12 15:00:58 -08:00
|
|
|
const scripts = {} as ScriptsOptions;
|
2019-11-11 17:01:43 -08:00
|
|
|
if (setScriptAfter) {
|
2020-03-13 11:51:45 -07:00
|
|
|
scripts.script = this.getNodeParameter('scriptAfter', i);
|
|
|
|
scripts!['script.param'] = this.getNodeParameter('scriptAfter', i);
|
2019-11-11 17:01:43 -08:00
|
|
|
}
|
|
|
|
if (setScriptBefore) {
|
2020-03-13 11:51:45 -07:00
|
|
|
scripts['script.prerequest'] = this.getNodeParameter('scriptBefore', i);
|
|
|
|
scripts['script.prerequest.param'] = this.getNodeParameter('scriptBeforeParam', i);
|
2019-11-11 17:01:43 -08:00
|
|
|
}
|
|
|
|
if (setScriptSort) {
|
2020-03-13 11:51:45 -07:00
|
|
|
scripts['script.presort'] = this.getNodeParameter('scriptSort', i);
|
|
|
|
scripts['script.presort.param'] = this.getNodeParameter('scriptSortParam', i);
|
2019-11-11 17:01:43 -08:00
|
|
|
}
|
|
|
|
return scripts;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-13 11:51:45 -07:00
|
|
|
export function parsePortals(this: IExecuteFunctions, i: number): object | null {
|
2019-11-11 17:01:43 -08:00
|
|
|
let portals;
|
2020-03-13 11:51:45 -07:00
|
|
|
const getPortals = this.getNodeParameter('getPortals', i);
|
2019-11-11 17:01:43 -08:00
|
|
|
if (!getPortals) {
|
|
|
|
portals = [];
|
|
|
|
} else {
|
2020-03-13 11:51:45 -07:00
|
|
|
portals = this.getNodeParameter('portals', i);
|
2019-11-11 17:01:43 -08:00
|
|
|
}
|
|
|
|
// @ts-ignore
|
|
|
|
return portals;
|
|
|
|
}
|
|
|
|
|
2020-03-13 11:51:45 -07:00
|
|
|
export function parseQuery(this: IExecuteFunctions, i: number): object | null {
|
2019-11-11 17:01:43 -08:00
|
|
|
let queries;
|
2020-03-13 11:51:45 -07:00
|
|
|
const queriesParamUi = this.getNodeParameter('queries', i, {}) as IDataObject;
|
2019-11-11 17:01:43 -08:00
|
|
|
if (queriesParamUi.query !== undefined) {
|
|
|
|
// @ts-ignore
|
|
|
|
queries = [];
|
|
|
|
for (const queryParam of queriesParamUi!.query as IDataObject[]) {
|
|
|
|
const query = {
|
2022-08-01 13:47:55 -07:00
|
|
|
omit: queryParam.omit ? 'true' : 'false',
|
2019-11-11 17:01:43 -08:00
|
|
|
};
|
|
|
|
// @ts-ignore
|
|
|
|
for (const field of queryParam!.fields!.field as IDataObject[]) {
|
|
|
|
// @ts-ignore
|
2019-11-14 04:02:57 -08:00
|
|
|
query[field.name] = field!.value;
|
2019-11-11 17:01:43 -08:00
|
|
|
}
|
|
|
|
queries.push(query);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
queries = null;
|
|
|
|
}
|
|
|
|
// @ts-ignore
|
|
|
|
return queries;
|
|
|
|
}
|
|
|
|
|
2020-03-13 11:51:45 -07:00
|
|
|
export function parseFields(this: IExecuteFunctions, i: number): object | null {
|
2019-11-11 17:01:43 -08:00
|
|
|
let fieldData;
|
2020-03-13 11:51:45 -07:00
|
|
|
const fieldsParametersUi = this.getNodeParameter('fieldsParametersUi', i, {}) as IDataObject;
|
2019-11-11 17:01:43 -08:00
|
|
|
if (fieldsParametersUi.fields !== undefined) {
|
|
|
|
// @ts-ignore
|
|
|
|
fieldData = {};
|
|
|
|
for (const field of fieldsParametersUi!.fields as IDataObject[]) {
|
|
|
|
// @ts-ignore
|
2019-11-14 04:02:57 -08:00
|
|
|
fieldData[field.name] = field!.value;
|
2019-11-11 17:01:43 -08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fieldData = null;
|
|
|
|
}
|
|
|
|
// @ts-ignore
|
|
|
|
return fieldData;
|
|
|
|
}
|