pull:初次提交

This commit is contained in:
Yep_Q
2025-09-08 04:48:28 +08:00
parent 5c0619656d
commit f64f498365
11751 changed files with 1953723 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
{
"node": "n8n-nodes-base.agileCrm",
"nodeVersion": "1.0",
"codexVersion": "1.0",
"categories": ["Marketing", "Sales"],
"resources": {
"credentialDocumentation": [
{
"url": "https://docs.n8n.io/integrations/builtin/credentials/agileCrm/"
}
],
"primaryDocumentation": [
{
"url": "https://docs.n8n.io/integrations/builtin/app-nodes/n8n-nodes-base.agilecrm/"
}
]
}
}

View File

@@ -0,0 +1,642 @@
import type {
IExecuteFunctions,
IDataObject,
INodeExecutionData,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import { NodeConnectionTypes, jsonParse, NodeOperationError } from 'n8n-workflow';
import { companyFields, companyOperations } from './CompanyDescription';
import { contactFields, contactOperations } from './ContactDescription';
import type { IContact, IContactUpdate } from './ContactInterface';
import { dealFields, dealOperations } from './DealDescription';
import type { IDeal } from './DealInterface';
import type { IFilter, ISearchConditions } from './FilterInterface';
import {
agileCrmApiRequest,
agileCrmApiRequestAllItems,
agileCrmApiRequestUpdate,
getFilterRules,
simplifyResponse,
validateJSON,
} from './GenericFunctions';
export class AgileCrm implements INodeType {
description: INodeTypeDescription = {
displayName: 'Agile CRM',
name: 'agileCrm',
// eslint-disable-next-line n8n-nodes-base/node-class-description-icon-not-svg
icon: 'file:agilecrm.png',
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
group: ['transform'],
version: 1,
description: 'Consume Agile CRM API',
defaults: {
name: 'Agile CRM',
},
usableAsTool: true,
inputs: [NodeConnectionTypes.Main],
outputs: [NodeConnectionTypes.Main],
credentials: [
{
name: 'agileCrmApi',
required: true,
},
],
properties: [
// Node properties which the user gets displayed and
// can change on the node.
{
displayName: 'Resource',
name: 'resource',
type: 'options',
noDataExpression: true,
options: [
{
name: 'Company',
value: 'company',
},
{
name: 'Contact',
value: 'contact',
},
{
name: 'Deal',
value: 'deal',
},
],
default: 'contact',
},
// CONTACT
...contactOperations,
...contactFields,
// COMPANY
...companyOperations,
...companyFields,
// DEAL
...dealOperations,
...dealFields,
],
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const returnData: IDataObject[] = [];
let responseData;
const resource = this.getNodeParameter('resource', 0);
const operation = this.getNodeParameter('operation', 0);
for (let i = 0; i < items.length; i++) {
if (resource === 'contact' || resource === 'company') {
const idGetter = resource === 'contact' ? 'contactId' : 'companyId';
if (operation === 'get') {
const contactId = this.getNodeParameter(idGetter, i) as string;
const endpoint = `api/contacts/${contactId}`;
responseData = await agileCrmApiRequest.call(this, 'GET', endpoint, {});
} else if (operation === 'delete') {
const contactId = this.getNodeParameter(idGetter, i) as string;
const endpoint = `api/contacts/${contactId}`;
responseData = await agileCrmApiRequest.call(this, 'DELETE', endpoint, {});
} else if (operation === 'getAll') {
const simple = this.getNodeParameter('simple', 0) as boolean;
const returnAll = this.getNodeParameter('returnAll', 0);
const filterType = this.getNodeParameter('filterType', i) as string;
const sort = this.getNodeParameter('options.sort.sort', i, {}) as {
direction: string;
field: string;
};
const body: IDataObject = {};
const filterJson: IFilter = {};
let contactType = '';
if (resource === 'contact') {
contactType = 'PERSON';
} else {
contactType = 'COMPANY';
}
filterJson.contact_type = contactType;
if (filterType === 'manual') {
const conditions = this.getNodeParameter(
'filters.conditions',
i,
[],
) as ISearchConditions[];
const matchType = this.getNodeParameter('matchType', i) as string;
let rules;
if (conditions.length !== 0) {
rules = getFilterRules(conditions, matchType);
Object.assign(filterJson, rules);
} else {
throw new NodeOperationError(
this.getNode(),
'At least one condition must be added.',
{ itemIndex: i },
);
}
} else if (filterType === 'json') {
const filterJsonRules = this.getNodeParameter('filterJson', i) as string;
if (validateJSON(filterJsonRules) !== undefined) {
Object.assign(filterJson, jsonParse(filterJsonRules));
} else {
throw new NodeOperationError(this.getNode(), 'Filter (JSON) must be a valid json', {
itemIndex: i,
});
}
}
body.filterJson = JSON.stringify(filterJson);
if (sort) {
if (sort.direction === 'ASC') {
body.global_sort_key = sort.field;
} else if (sort.direction === 'DESC') {
body.global_sort_key = `-${sort.field}`;
}
}
if (returnAll) {
body.page_size = 100;
responseData = await agileCrmApiRequestAllItems.call(
this,
'POST',
'api/filters/filter/dynamic-filter',
body,
undefined,
undefined,
true,
);
} else {
body.page_size = this.getNodeParameter('limit', 0);
responseData = await agileCrmApiRequest.call(
this,
'POST',
'api/filters/filter/dynamic-filter',
body,
undefined,
undefined,
true,
);
}
if (simple) {
responseData = simplifyResponse(responseData);
}
} else if (operation === 'create') {
const jsonParameters = this.getNodeParameter('jsonParameters', i);
const body: IContact = {};
const properties: IDataObject[] = [];
if (jsonParameters) {
const additionalFieldsJson = this.getNodeParameter('additionalFieldsJson', i) as string;
if (additionalFieldsJson !== '') {
if (validateJSON(additionalFieldsJson) !== undefined) {
Object.assign(body, jsonParse(additionalFieldsJson));
} else {
throw new NodeOperationError(
this.getNode(),
'Additional fields must be a valid JSON',
{ itemIndex: i },
);
}
}
} else {
const additionalFields = this.getNodeParameter('additionalFields', i);
// if company, add 'company' as type. default is person
if (resource === 'company') {
body.type = 'COMPANY';
}
if (additionalFields.starValue) {
body.star_value = additionalFields.starValue as string;
}
if (additionalFields.tags) {
body.tags = additionalFields.tags as string[];
}
// Contact specific properties
if (resource === 'contact') {
if (additionalFields.firstName) {
properties.push({
type: 'SYSTEM',
name: 'first_name',
value: additionalFields.firstName as string,
} as IDataObject);
}
if (additionalFields.lastName) {
properties.push({
type: 'SYSTEM',
name: 'last_name',
value: additionalFields.lastName as string,
} as IDataObject);
}
if (additionalFields.company) {
properties.push({
type: 'SYSTEM',
name: 'company',
value: additionalFields.company as string,
} as IDataObject);
}
if (additionalFields.title) {
properties.push({
type: 'SYSTEM',
name: 'title',
value: additionalFields.title as string,
} as IDataObject);
}
if (additionalFields.emailOptions) {
//@ts-ignore
additionalFields.emailOptions.emailProperties.map((property) => {
properties.push({
type: 'SYSTEM',
subtype: property.subtype as string,
name: 'email',
value: property.email as string,
} as IDataObject);
});
}
if (additionalFields.addressOptions) {
//@ts-ignore
additionalFields.addressOptions.addressProperties.map((property) => {
properties.push({
type: 'SYSTEM',
subtype: property.subtype as string,
name: 'address',
value: property.address as string,
} as IDataObject);
});
}
if (additionalFields.phoneOptions) {
//@ts-ignore
additionalFields.phoneOptions.phoneProperties.map((property) => {
properties.push({
type: 'SYSTEM',
subtype: property.subtype as string,
name: 'phone',
value: property.number as string,
} as IDataObject);
});
}
} else if (resource === 'company') {
if (additionalFields.email) {
properties.push({
type: 'SYSTEM',
name: 'email',
value: additionalFields.email as string,
} as IDataObject);
}
if (additionalFields.addressOptions) {
//@ts-ignore
additionalFields.addressOptions.addressProperties.map((property) => {
properties.push({
type: 'SYSTEM',
subtype: property.subtype as string,
name: 'address',
value: property.address as string,
} as IDataObject);
});
}
if (additionalFields.phone) {
properties.push({
type: 'SYSTEM',
name: 'phone',
value: additionalFields.phone as string,
} as IDataObject);
}
if (additionalFields.name) {
properties.push({
type: 'SYSTEM',
name: 'name',
value: additionalFields.name as string,
} as IDataObject);
}
}
if (additionalFields.websiteOptions) {
//@ts-ignore
additionalFields.websiteOptions.websiteProperties.map((property) => {
properties.push({
type: 'SYSTEM',
subtype: property.subtype as string,
name: 'website',
value: property.url as string,
} as IDataObject);
});
}
if (additionalFields.customProperties) {
//@ts-ignore
additionalFields.customProperties.customProperty.map((property) => {
properties.push({
type: 'CUSTOM',
subtype: property.subtype as string,
name: property.name,
value: property.value as string,
} as IDataObject);
});
}
body.properties = properties;
}
const endpoint = 'api/contacts';
responseData = await agileCrmApiRequest.call(this, 'POST', endpoint, body);
} else if (operation === 'update') {
const contactId = this.getNodeParameter(idGetter, i) as string;
const contactUpdatePayload: IContactUpdate = { id: contactId };
const jsonParameters = this.getNodeParameter('jsonParameters', i);
const body: IContact = {};
const properties: IDataObject[] = [];
if (jsonParameters) {
const additionalFieldsJson = this.getNodeParameter('additionalFieldsJson', i) as string;
if (additionalFieldsJson !== '') {
if (validateJSON(additionalFieldsJson) !== undefined) {
Object.assign(body, jsonParse(additionalFieldsJson));
} else {
throw new NodeOperationError(
this.getNode(),
'Additional fields must be a valid JSON',
{ itemIndex: i },
);
}
}
} else {
const additionalFields = this.getNodeParameter('additionalFields', i);
if (additionalFields.starValue) {
body.star_value = additionalFields.starValue as string;
}
if (additionalFields.tags) {
body.tags = additionalFields.tags as string[];
}
// Contact specific properties
if (resource === 'contact') {
if (additionalFields.leadScore) {
body.lead_score = additionalFields.leadScore as string;
}
if (additionalFields.firstName) {
properties.push({
type: 'SYSTEM',
name: 'first_name',
value: additionalFields.firstName as string,
} as IDataObject);
}
if (additionalFields.lastName) {
properties.push({
type: 'SYSTEM',
name: 'last_name',
value: additionalFields.lastName as string,
} as IDataObject);
}
if (additionalFields.company) {
properties.push({
type: 'SYSTEM',
name: 'company',
value: additionalFields.company as string,
} as IDataObject);
}
if (additionalFields.title) {
properties.push({
type: 'SYSTEM',
name: 'title',
value: additionalFields.title as string,
} as IDataObject);
}
if (additionalFields.emailOptions) {
//@ts-ignore
additionalFields.emailOptions.emailProperties.map((property) => {
properties.push({
type: 'SYSTEM',
subtype: property.subtype as string,
name: 'email',
value: property.email as string,
} as IDataObject);
});
}
if (additionalFields.addressOptions) {
//@ts-ignore
additionalFields.addressOptions.addressProperties.map((property) => {
properties.push({
type: 'SYSTEM',
subtype: property.subtype as string,
name: 'address',
value: property.address as string,
} as IDataObject);
});
}
if (additionalFields.phoneOptions) {
//@ts-ignore
additionalFields.phoneOptions.phoneProperties.map((property) => {
properties.push({
type: 'SYSTEM',
subtype: property.subtype as string,
name: 'phone',
value: property.number as string,
} as IDataObject);
});
}
} else if (resource === 'company') {
if (additionalFields.email) {
properties.push({
type: 'SYSTEM',
name: 'email',
value: additionalFields.email as string,
} as IDataObject);
}
if (additionalFields.addressOptions) {
//@ts-ignore
additionalFields.addressOptions.addressProperties.map((property) => {
properties.push({
type: 'SYSTEM',
subtype: property.subtype as string,
name: 'address',
value: property.address as string,
} as IDataObject);
});
}
if (additionalFields.phone) {
properties.push({
type: 'SYSTEM',
name: 'phone',
value: additionalFields.phone as string,
} as IDataObject);
}
}
if (additionalFields.websiteOptions) {
//@ts-ignore
additionalFields.websiteOptions.websiteProperties.map((property) => {
properties.push({
type: 'SYSTEM',
subtype: property.subtype as string,
name: 'website',
value: property.url as string,
} as IDataObject);
});
}
if (additionalFields.name) {
properties.push({
type: 'SYSTEM',
name: 'name',
value: additionalFields.name as string,
} as IDataObject);
}
if (additionalFields.customProperties) {
//@ts-ignore
additionalFields.customProperties.customProperty.map((property) => {
properties.push({
type: 'CUSTOM',
subtype: property.subtype as string,
name: property.name,
value: property.value as string,
} as IDataObject);
});
}
body.properties = properties;
}
Object.assign(contactUpdatePayload, body);
responseData = await agileCrmApiRequestUpdate.call(this, 'PUT', '', contactUpdatePayload);
}
} else if (resource === 'deal') {
if (operation === 'get') {
const dealId = this.getNodeParameter('dealId', i) as string;
const endpoint = `api/opportunity/${dealId}`;
responseData = await agileCrmApiRequest.call(this, 'GET', endpoint, {});
} else if (operation === 'delete') {
const contactId = this.getNodeParameter('dealId', i) as string;
const endpoint = `api/opportunity/${contactId}`;
responseData = await agileCrmApiRequest.call(this, 'DELETE', endpoint, {});
} else if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', 0);
const endpoint = 'api/opportunity';
if (returnAll) {
const limit = 100;
responseData = await agileCrmApiRequestAllItems.call(this, 'GET', endpoint, undefined, {
page_size: limit,
});
} else {
const limit = this.getNodeParameter('limit', 0);
responseData = await agileCrmApiRequest.call(this, 'GET', endpoint, undefined, {
page_size: limit,
});
}
} else if (operation === 'create') {
const jsonParameters = this.getNodeParameter('jsonParameters', i);
const body: IDeal = {};
if (jsonParameters) {
const additionalFieldsJson = this.getNodeParameter('additionalFieldsJson', i) as string;
if (additionalFieldsJson !== '') {
if (validateJSON(additionalFieldsJson) !== undefined) {
Object.assign(body, jsonParse(additionalFieldsJson));
} else {
throw new NodeOperationError(
this.getNode(),
'Additional fields must be a valid JSON',
{ itemIndex: i },
);
}
}
} else {
const additionalFields = this.getNodeParameter('additionalFields', i);
body.close_date = new Date(this.getNodeParameter('closeDate', i) as string).getTime();
body.expected_value = this.getNodeParameter('expectedValue', i) as number;
body.milestone = this.getNodeParameter('milestone', i) as string;
body.probability = this.getNodeParameter('probability', i) as number;
body.name = this.getNodeParameter('name', i) as string;
if (additionalFields.contactIds) {
body.contactIds = additionalFields.contactIds as string[];
}
if (additionalFields.customData) {
// @ts-ignore
body.customData = additionalFields.customData.customProperty as IDealCustomProperty[];
}
}
const endpoint = 'api/opportunity';
responseData = await agileCrmApiRequest.call(this, 'POST', endpoint, body);
} else if (operation === 'update') {
const jsonParameters = this.getNodeParameter('jsonParameters', i);
const body: IDeal = {};
if (jsonParameters) {
const additionalFieldsJson = this.getNodeParameter('additionalFieldsJson', i) as string;
if (additionalFieldsJson !== '') {
if (validateJSON(additionalFieldsJson) !== undefined) {
Object.assign(body, jsonParse(additionalFieldsJson));
} else {
throw new NodeOperationError(
this.getNode(),
'Additional fields must be valid JSON',
{ itemIndex: i },
);
}
}
} else {
const additionalFields = this.getNodeParameter('additionalFields', i);
body.id = this.getNodeParameter('dealId', i) as number;
if (additionalFields.expectedValue) {
body.expected_value = additionalFields.expectedValue as number;
}
if (additionalFields.name) {
body.name = additionalFields.name as string;
}
if (additionalFields.probability) {
body.probability = additionalFields.probability as number;
}
if (additionalFields.contactIds) {
body.contactIds = additionalFields.contactIds as string[];
}
if (additionalFields.customData) {
// @ts-ignore
body.customData = additionalFields.customData.customProperty as IDealCustomProperty[];
}
}
const endpoint = 'api/opportunity/partial-update';
responseData = await agileCrmApiRequest.call(this, 'PUT', endpoint, body);
}
}
if (Array.isArray(responseData)) {
returnData.push.apply(returnData, responseData as IDataObject[]);
} else {
returnData.push(responseData as IDataObject);
}
}
return [this.helpers.returnJsonArray(returnData)];
}
}

View File

@@ -0,0 +1,924 @@
import type { INodeProperties } from 'n8n-workflow';
export const companyOperations: INodeProperties[] = [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: {
show: {
resource: ['company'],
},
},
options: [
{
name: 'Create',
value: 'create',
description: 'Create a new company',
action: 'Create a company',
},
{
name: 'Delete',
value: 'delete',
description: 'Delete a company',
action: 'Delete a company',
},
{
name: 'Get',
value: 'get',
description: 'Get a company',
action: 'Get a company',
},
{
name: 'Get Many',
value: 'getAll',
description: 'Get many companies',
action: 'Get many companies',
},
{
name: 'Update',
value: 'update',
description: 'Update company properties',
action: 'Update a company',
},
],
default: 'get',
},
];
export const companyFields: INodeProperties[] = [
/* -------------------------------------------------------------------------- */
/* company:get */
/* -------------------------------------------------------------------------- */
{
displayName: 'Company ID',
name: 'companyId',
type: 'string',
required: true,
displayOptions: {
show: {
resource: ['company'],
operation: ['get'],
},
},
default: '',
description: 'Unique identifier for a particular company',
},
/* -------------------------------------------------------------------------- */
/* company:get all */
/* -------------------------------------------------------------------------- */
{
displayName: 'Return All',
name: 'returnAll',
type: 'boolean',
displayOptions: {
show: {
resource: ['company'],
operation: ['getAll'],
},
},
default: false,
description: 'Whether to return all results or only up to a given limit',
},
{
displayName: 'Limit',
name: 'limit',
type: 'number',
typeOptions: {
minValue: 1,
},
displayOptions: {
show: {
resource: ['company'],
operation: ['getAll'],
returnAll: [false],
},
},
default: 20,
description: 'Max number of results to return',
},
{
displayName: 'Filter',
name: 'filterType',
type: 'options',
options: [
{
name: 'None',
value: 'none',
},
{
name: 'Build Manually',
value: 'manual',
},
{
name: 'JSON',
value: 'json',
},
],
displayOptions: {
show: {
resource: ['company'],
operation: ['getAll'],
},
},
default: 'none',
},
{
displayName: 'Must Match',
name: 'matchType',
type: 'options',
options: [
{
name: 'Any Filter',
value: 'anyFilter',
},
{
name: 'All Filters',
value: 'allFilters',
},
],
displayOptions: {
show: {
resource: ['company'],
operation: ['getAll'],
filterType: ['manual'],
},
},
default: 'anyFilter',
},
{
displayName: 'Simplify',
name: 'simple',
type: 'boolean',
displayOptions: {
show: {
resource: ['company'],
operation: ['getAll'],
},
},
// eslint-disable-next-line n8n-nodes-base/node-param-default-wrong-for-simplify
default: false,
description: 'Whether to return a simplified version of the response instead of the raw data',
},
{
displayName: 'Filters',
name: 'filters',
type: 'fixedCollection',
typeOptions: {
multipleValues: true,
},
displayOptions: {
show: {
resource: ['company'],
operation: ['getAll'],
filterType: ['manual'],
},
},
default: {},
placeholder: 'Add Condition',
options: [
{
displayName: 'Conditions',
name: 'conditions',
values: [
{
displayName: 'Field',
name: 'field',
type: 'string',
default: '',
description: 'Any searchable field',
},
{
displayName: 'Condition Type',
name: 'condition_type',
type: 'options',
options: [
{
name: 'After',
value: 'AFTER',
},
{
name: 'Before',
value: 'BEFORE',
},
{
name: 'Between',
value: 'BETWEEN',
},
{
name: 'Equals',
value: 'EQUALS',
},
{
name: 'Last',
value: 'LAST',
},
{
name: 'Not Equal',
value: 'NOTEQUALS',
},
{
name: 'On',
value: 'ON',
},
],
default: 'EQUALS',
},
{
displayName: 'Value',
name: 'value',
type: 'string',
default: '',
},
{
displayName: 'Value 2',
name: 'value2',
type: 'string',
displayOptions: {
show: {
condition_type: ['BETWEEN'],
},
},
default: '',
},
],
},
],
},
{
displayName:
'See <a href="https://github.com/agilecrm/rest-api#121-get-contacts-by-dynamic-filter" target="_blank">Agile CRM guide</a> to creating filters',
name: 'jsonNotice',
type: 'notice',
displayOptions: {
show: {
resource: ['company'],
operation: ['getAll'],
filterType: ['json'],
},
},
default: '',
},
{
displayName: 'Filters (JSON)',
name: 'filterJson',
type: 'string',
displayOptions: {
show: {
resource: ['company'],
operation: ['getAll'],
filterType: ['json'],
},
},
default: '',
},
{
displayName: 'Options',
name: 'options',
type: 'collection',
placeholder: 'Add option',
default: {},
displayOptions: {
show: {
resource: ['company'],
operation: ['getAll'],
},
},
options: [
{
displayName: 'Sort',
name: 'sort',
type: 'fixedCollection',
placeholder: 'Add Sort',
default: [],
options: [
{
displayName: 'Sort',
name: 'sort',
values: [
{
displayName: 'Direction',
name: 'direction',
type: 'options',
options: [
{
name: 'Ascending',
value: 'ASC',
},
{
name: 'Descending',
value: 'DESC',
},
],
default: 'ASC',
description: 'The sorting direction',
},
{
displayName: 'Field',
name: 'field',
type: 'string',
default: '',
description: 'The sorting field',
},
],
},
],
},
],
},
/* -------------------------------------------------------------------------- */
/* company:create */
/* -------------------------------------------------------------------------- */
{
displayName: 'JSON Parameters',
name: 'jsonParameters',
type: 'boolean',
default: false,
displayOptions: {
show: {
resource: ['company'],
operation: ['create'],
},
},
},
{
displayName: 'Additional Fields',
name: 'additionalFieldsJson',
type: 'json',
typeOptions: {
alwaysOpenEditWindow: true,
},
default: '',
displayOptions: {
show: {
resource: ['company'],
operation: ['create'],
jsonParameters: [true],
},
},
description:
'Object of values to set as described <a href="https://github.com/agilecrm/rest-api#1-companys---companies-api">here</a>',
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
type: 'collection',
placeholder: 'Add Field',
default: {},
displayOptions: {
show: {
resource: ['company'],
operation: ['create'],
jsonParameters: [false],
},
},
options: [
{
displayName: 'Address',
name: 'addressOptions',
type: 'fixedCollection',
default: {},
description: 'Company address',
typeOptions: {
multipleValues: true,
},
options: [
{
displayName: 'Address Properties',
name: 'addressProperties',
values: [
{
displayName: 'Type',
name: 'subtype',
type: 'options',
required: true,
default: '',
description: 'Type of address',
options: [
{
name: 'Postal',
value: 'postal',
},
{
name: 'Office',
value: 'office',
},
],
},
{
displayName: 'Address',
name: 'address',
type: 'string',
required: true,
default: '',
description: 'Full address',
},
],
},
],
},
{
displayName: 'Email',
name: 'email',
type: 'string',
placeholder: 'name@email.com',
default: '',
description: 'Company email',
},
{
displayName: 'Name',
name: 'name',
type: 'string',
default: '',
description: 'Company name',
},
{
displayName: 'Phone',
name: 'phone',
type: 'string',
default: '',
description: 'Company phone',
},
{
displayName: 'Star Value',
name: 'starValue',
type: 'options',
default: '',
description: 'Rating of company (Max value 5). This is not applicable for companies.',
options: [
{
name: '0',
value: 0,
},
{
name: '1',
value: 1,
},
{
name: '2',
value: 2,
},
{
name: '3',
value: 3,
},
{
name: '4',
value: 4,
},
{
name: '5',
value: 5,
},
],
},
{
displayName: 'Tags',
name: 'tags',
type: 'string',
typeOptions: {
multipleValues: true,
multipleValueButtonText: 'Add Tag',
},
default: [],
description:
'Unique identifiers added to company, for easy management of companys. This is not applicable for companies.',
},
{
displayName: 'Website',
name: 'websiteOptions',
type: 'fixedCollection',
description: 'Companies websites',
default: {},
typeOptions: {
multipleValues: true,
},
options: [
{
displayName: 'Website Properties.',
name: 'websiteProperties',
values: [
{
displayName: 'Type',
name: 'subtype',
type: 'options',
required: true,
default: '',
description: 'Type of website',
options: [
{
name: 'Facebook',
value: 'facebook',
},
{
name: 'Feed',
value: 'feed',
},
{
name: 'Flickr',
value: 'flickr',
},
{
name: 'Github',
value: 'github',
},
{
name: 'Google Plus',
value: 'googlePlus',
},
{
name: 'LinkedIn',
value: 'linkedin',
},
{
name: 'Skype',
value: 'skype',
},
{
name: 'Twitter',
value: 'twitter',
},
{
name: 'URL',
value: 'url',
},
{
name: 'Xing',
value: 'xing',
},
{
name: 'YouTube',
value: 'youtube',
},
],
},
{
displayName: 'URL',
name: 'url',
type: 'string',
required: true,
default: '',
description: 'Website URL',
},
],
},
],
},
{
displayName: 'Custom Properties',
name: 'customProperties',
type: 'fixedCollection',
default: {},
typeOptions: {
multipleValues: true,
},
options: [
{
displayName: 'Property',
name: 'customProperty',
values: [
{
displayName: 'Name',
name: 'name',
type: 'string',
required: true,
default: '',
description: 'Property name',
},
{
displayName: 'Sub Type',
name: 'subtype',
type: 'string',
default: '',
description: 'Property sub type',
},
{
displayName: 'Value',
name: 'value',
type: 'string',
default: '',
description: 'Property value',
},
],
},
],
},
],
},
/* -------------------------------------------------------------------------- */
/* company:delete */
/* -------------------------------------------------------------------------- */
{
displayName: 'Company ID',
name: 'companyId',
type: 'string',
required: true,
displayOptions: {
show: {
resource: ['company'],
operation: ['delete'],
},
},
default: '',
description: 'ID of company to delete',
},
/* -------------------------------------------------------------------------- */
/* company:update */
/* -------------------------------------------------------------------------- */
{
displayName: 'Company ID',
name: 'companyId',
type: 'string',
required: true,
displayOptions: {
show: {
resource: ['company'],
operation: ['update'],
},
},
default: '',
description: 'Unique identifier for a particular company',
},
{
displayName: 'JSON Parameters',
name: 'jsonParameters',
type: 'boolean',
default: false,
displayOptions: {
show: {
resource: ['company'],
operation: ['update'],
},
},
},
{
displayName: 'Additional Fields',
name: 'additionalFieldsJson',
type: 'json',
typeOptions: {
alwaysOpenEditWindow: true,
},
default: '',
displayOptions: {
show: {
resource: ['company'],
operation: ['update'],
jsonParameters: [true],
},
},
description:
'Object of values to set as described <a href="https://github.com/agilecrm/rest-api#1-companys---companies-api">here</a>',
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
type: 'collection',
placeholder: 'Add Field',
default: {},
displayOptions: {
show: {
resource: ['company'],
operation: ['update'],
jsonParameters: [false],
},
},
options: [
{
displayName: 'Address',
name: 'addressOptions',
type: 'fixedCollection',
default: {},
description: 'Company address',
typeOptions: {
multipleValues: true,
},
options: [
{
displayName: 'Address Properties',
name: 'addressProperties',
values: [
{
displayName: 'Type',
name: 'subtype',
type: 'options',
required: true,
default: '',
description: 'Type of address',
options: [
{
name: 'Postal',
value: 'postal',
},
{
name: 'Office',
value: 'office',
},
],
},
{
displayName: 'Address',
name: 'address',
type: 'string',
required: true,
default: '',
description: 'Full address',
},
],
},
],
},
{
displayName: 'Email',
name: 'email',
type: 'string',
placeholder: 'name@email.com',
default: '',
description: 'Company email',
},
{
displayName: 'Star Value',
name: 'starValue',
type: 'options',
default: '',
description: 'Rating of company (Max value 5). This is not applicable for companies.',
options: [
{
name: '0',
value: 0,
},
{
name: '1',
value: 1,
},
{
name: '2',
value: 2,
},
{
name: '3',
value: 3,
},
{
name: '4',
value: 4,
},
{
name: '5',
value: 5,
},
],
},
{
displayName: 'Tags',
name: 'tags',
type: 'string',
typeOptions: {
multipleValues: true,
multipleValueButtonText: 'Add Tag',
},
default: [],
description:
'Unique identifiers added to company, for easy management of companys. This is not applicable for companies.',
},
{
displayName: 'Name',
name: 'name',
type: 'string',
default: '',
description: 'Company name',
},
{
displayName: 'Phone',
name: 'phone',
type: 'string',
default: '',
description: 'Company phone',
},
{
displayName: 'Website',
name: 'websiteOptions',
type: 'fixedCollection',
default: {},
description: 'Companys websites',
typeOptions: {
multipleValues: true,
},
options: [
{
displayName: 'Website Properties.',
name: 'websiteProperties',
values: [
{
displayName: 'Type',
name: 'subtype',
type: 'options',
required: true,
default: '',
description: 'Type of website',
options: [
{
name: 'Facebook',
value: 'facebook',
},
{
name: 'Feed',
value: 'feed',
},
{
name: 'Flickr',
value: 'flickr',
},
{
name: 'Github',
value: 'github',
},
{
name: 'Google Plus',
value: 'googlePlus',
},
{
name: 'LinkedIn',
value: 'linkedin',
},
{
name: 'Skype',
value: 'skype',
},
{
name: 'Twitter',
value: 'twitter',
},
{
name: 'URL',
value: 'url',
},
{
name: 'Xing',
value: 'xing',
},
{
name: 'YouTube',
value: 'youtube',
},
],
},
{
displayName: 'URL',
name: 'url',
type: 'string',
required: true,
default: '',
description: 'Website URL',
},
],
},
],
},
{
displayName: 'Custom Properties',
name: 'customProperties',
type: 'fixedCollection',
default: {},
typeOptions: {
multipleValues: true,
},
options: [
{
displayName: 'Property',
name: 'customProperty',
values: [
{
displayName: 'Name',
name: 'name',
type: 'string',
required: true,
default: '',
description: 'Property name',
},
{
displayName: 'Sub Type',
name: 'subtype',
type: 'string',
default: '',
description: 'Property sub type',
},
{
displayName: 'Value',
name: 'value',
type: 'string',
default: '',
description: 'Property value',
},
],
},
],
},
],
},
];

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,24 @@
import type { IDataObject } from 'n8n-workflow';
export interface IProperty {
type: string;
name: string;
subtype?: string;
value?: string;
}
export interface IContact {
type?: string;
star_value?: string;
lead_score?: string;
tags?: string[];
properties?: IDataObject[];
}
export interface IContactUpdate {
id: string;
properties?: IDataObject[];
star_value?: string;
lead_score?: string;
tags?: string[];
}

View File

@@ -0,0 +1,432 @@
import type { INodeProperties } from 'n8n-workflow';
export const dealOperations: INodeProperties[] = [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: {
show: {
resource: ['deal'],
},
},
options: [
{
name: 'Create',
value: 'create',
description: 'Create a new deal',
action: 'Create a deal',
},
{
name: 'Delete',
value: 'delete',
description: 'Delete a deal',
action: 'Delete a deal',
},
{
name: 'Get',
value: 'get',
description: 'Get a deal',
action: 'Get a deal',
},
{
name: 'Get Many',
value: 'getAll',
description: 'Get many deals',
action: 'Get many deals',
},
{
name: 'Update',
value: 'update',
description: 'Update deal properties',
action: 'Update a deal',
},
],
default: 'get',
},
];
export const dealFields: INodeProperties[] = [
/* -------------------------------------------------------------------------- */
/* deal:get */
/* -------------------------------------------------------------------------- */
{
displayName: 'Deal ID',
name: 'dealId',
type: 'string',
required: true,
displayOptions: {
show: {
resource: ['deal'],
operation: ['get'],
},
},
default: '',
description: 'Unique identifier for a particular deal',
},
/* -------------------------------------------------------------------------- */
/* deal:get all */
/* -------------------------------------------------------------------------- */
{
displayName: 'Limit',
name: 'limit',
type: 'number',
typeOptions: {
minValue: 1,
},
description: 'Max number of results to return',
default: 20,
displayOptions: {
show: {
resource: ['deal'],
operation: ['getAll'],
returnAll: [false],
},
},
},
{
displayName: 'Return All',
name: 'returnAll',
type: 'boolean',
displayOptions: {
show: {
resource: ['deal'],
operation: ['getAll'],
},
},
default: false,
description: 'Whether to return all results or only up to a given limit',
},
/* -------------------------------------------------------------------------- */
/* deal:create */
/* -------------------------------------------------------------------------- */
{
displayName: 'Close Date',
name: 'closeDate',
type: 'dateTime',
required: true,
displayOptions: {
show: {
resource: ['deal'],
operation: ['create'],
jsonParameters: [false],
},
},
default: '',
description: 'Closing date of deal',
},
{
displayName: 'Expected Value',
name: 'expectedValue',
type: 'number',
required: true,
typeOptions: {
minValue: 0,
maxValue: 1000000000000,
},
displayOptions: {
show: {
resource: ['deal'],
operation: ['create'],
jsonParameters: [false],
},
},
default: 1,
description: 'Expected Value of deal',
},
{
displayName: 'Milestone',
name: 'milestone',
type: 'string',
required: true,
displayOptions: {
show: {
resource: ['deal'],
operation: ['create'],
jsonParameters: [false],
},
},
default: '',
description: 'Milestone of deal',
},
{
displayName: 'Name',
name: 'name',
type: 'string',
required: true,
displayOptions: {
show: {
resource: ['deal'],
operation: ['create'],
jsonParameters: [false],
},
},
default: '',
description: 'Name of deal',
},
{
displayName: 'Probability',
name: 'probability',
type: 'number',
required: true,
typeOptions: {
minValue: 0,
maxValue: 100,
},
displayOptions: {
show: {
resource: ['deal'],
operation: ['create'],
jsonParameters: [false],
},
},
default: 50,
description: 'Expected probability',
},
{
displayName: 'JSON Parameters',
name: 'jsonParameters',
type: 'boolean',
default: false,
displayOptions: {
show: {
resource: ['deal'],
operation: ['create'],
},
},
},
{
displayName: 'Additional Fields',
name: 'additionalFieldsJson',
type: 'json',
typeOptions: {
alwaysOpenEditWindow: true,
},
default: '',
displayOptions: {
show: {
resource: ['deal'],
operation: ['create'],
jsonParameters: [true],
},
},
description:
'Object of values to set as described <a href="https://github.com/agilecrm/rest-api#1-deals---companies-api">here</a>',
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
type: 'collection',
placeholder: 'Add Field',
default: {},
displayOptions: {
show: {
resource: ['deal'],
operation: ['create'],
jsonParameters: [false],
},
},
options: [
{
displayName: 'Contact IDs',
name: 'contactIds',
type: 'string',
typeOptions: {
multipleValues: true,
multipleValueButtonText: 'Add ID',
},
default: [],
description: 'Unique contact identifiers',
},
{
displayName: 'Custom Data',
name: 'customData',
type: 'fixedCollection',
default: {},
typeOptions: {
multipleValues: true,
},
options: [
{
displayName: 'Property',
name: 'customProperty',
values: [
{
displayName: 'Name',
name: 'name',
type: 'string',
required: true,
default: '',
description: 'Property name',
},
{
displayName: 'Value',
name: 'value',
type: 'string',
default: '',
description: 'Property value',
},
],
},
],
},
],
},
/* -------------------------------------------------------------------------- */
/* deal:delete */
/* -------------------------------------------------------------------------- */
{
displayName: 'Deal ID',
name: 'dealId',
type: 'string',
required: true,
displayOptions: {
show: {
resource: ['deal'],
operation: ['delete'],
},
},
default: '',
description: 'ID of deal to delete',
},
/* -------------------------------------------------------------------------- */
/* deal:update */
/* -------------------------------------------------------------------------- */
{
displayName: 'Deal ID',
name: 'dealId',
type: 'string',
required: true,
displayOptions: {
show: {
resource: ['deal'],
operation: ['update'],
},
},
default: '',
description: 'ID of deal to update',
},
{
displayName: 'JSON Parameters',
name: 'jsonParameters',
type: 'boolean',
default: false,
displayOptions: {
show: {
resource: ['deal'],
operation: ['update'],
},
},
},
{
displayName: 'Additional Fields',
name: 'additionalFieldsJson',
type: 'json',
typeOptions: {
alwaysOpenEditWindow: true,
},
default: '',
displayOptions: {
show: {
resource: ['deal'],
operation: ['update'],
jsonParameters: [true],
},
},
description:
'Object of values to set as described <a href="https://github.com/agilecrm/rest-api#1-deals---companies-api">here</a>',
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
type: 'collection',
placeholder: 'Add Field',
default: {},
displayOptions: {
show: {
resource: ['deal'],
operation: ['update'],
jsonParameters: [false],
},
},
options: [
{
displayName: 'Expected Value',
name: 'expectedValue',
type: 'number',
typeOptions: {
minValue: 0,
maxValue: 10000,
},
default: '',
description: 'Expected Value of deal',
},
{
displayName: 'Name',
name: 'name',
type: 'string',
default: '',
description: 'Name of deal',
},
{
displayName: 'Probability',
name: 'probability',
type: 'number',
typeOptions: {
minValue: 0,
maxValue: 100,
},
default: 50,
description: 'Expected Value of deal',
},
{
displayName: 'Contact IDs',
name: 'contactIds',
type: 'string',
typeOptions: {
multipleValues: true,
multipleValueButtonText: 'Add ID',
},
default: [],
description: 'Unique contact identifiers',
},
{
displayName: 'Custom Data',
name: 'customData',
type: 'fixedCollection',
default: {},
typeOptions: {
multipleValues: true,
},
options: [
{
displayName: 'Property',
name: 'customProperty',
values: [
{
displayName: 'Name',
name: 'name',
type: 'string',
required: true,
default: '',
description: 'Property name',
},
{
displayName: 'Value',
name: 'value',
type: 'string',
default: '',
description: 'Property value',
},
],
},
],
},
],
},
];

View File

@@ -0,0 +1,15 @@
export interface IDealCustomProperty {
name: string;
value: string;
}
export interface IDeal {
id?: number;
expected_value?: number;
probability?: number;
name?: string;
close_date?: number;
milestone?: string;
contactIds?: string[];
customData?: IDealCustomProperty[];
}

View File

@@ -0,0 +1,19 @@
export interface ISearchConditions {
field?: string;
condition_type?: string;
value?: string;
value2?: string;
}
export interface IFilterRules {
LHS?: string;
CONDITION?: string;
RHS?: string;
RHS_NEW?: string;
}
export interface IFilter {
or_rules?: IFilterRules;
rules?: IFilterRules;
contact_type?: string;
}

View File

@@ -0,0 +1,232 @@
import type {
IDataObject,
IExecuteFunctions,
IHookFunctions,
ILoadOptionsFunctions,
JsonObject,
IRequestOptions,
IHttpRequestMethods,
} from 'n8n-workflow';
import { NodeApiError } from 'n8n-workflow';
import type { IContactUpdate } from './ContactInterface';
import type { IFilterRules, ISearchConditions } from './FilterInterface';
export async function agileCrmApiRequest(
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
method: IHttpRequestMethods,
endpoint: string,
body: any = {},
query: IDataObject = {},
uri?: string,
sendAsForm?: boolean,
): Promise<any> {
const credentials = await this.getCredentials('agileCrmApi');
const options: IRequestOptions = {
method,
headers: {
Accept: 'application/json',
},
auth: {
username: credentials.email as string,
password: credentials.apiKey as string,
},
qs: query,
uri: uri || `https://${credentials.subdomain}.agilecrm.com/dev/${endpoint}`,
json: true,
};
// To send the request as 'content-type': 'application/x-www-form-urlencoded' add form to options instead of body
if (sendAsForm) {
options.form = body;
}
// Only add Body property if method not GET or DELETE to avoid 400 response
// And when not sending a form
else if (method !== 'GET' && method !== 'DELETE') {
options.body = body;
}
try {
return await this.helpers.request(options);
} catch (error) {
throw new NodeApiError(this.getNode(), error as JsonObject);
}
}
export async function agileCrmApiRequestAllItems(
this: IHookFunctions | ILoadOptionsFunctions | IExecuteFunctions,
method: IHttpRequestMethods,
resource: string,
body: any = {},
query: IDataObject = {},
uri?: string,
sendAsForm?: boolean,
): Promise<any> {
// https://github.com/agilecrm/rest-api#11-listing-contacts-
const returnData: IDataObject[] = [];
let responseData;
do {
responseData = await agileCrmApiRequest.call(
this,
method,
resource,
body,
query,
uri,
sendAsForm,
);
if (responseData.length !== 0) {
returnData.push.apply(returnData, responseData as IDataObject[]);
if (sendAsForm) {
body.cursor = responseData[responseData.length - 1].cursor;
} else {
query.cursor = responseData[responseData.length - 1].cursor;
}
}
} while (
responseData.length !== 0 &&
responseData[responseData.length - 1].hasOwnProperty('cursor')
);
return returnData;
}
export async function agileCrmApiRequestUpdate(
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
method: IHttpRequestMethods = 'PUT',
_endpoint?: string,
body: any = {},
_query: IDataObject = {},
uri?: string,
): Promise<any> {
const credentials = await this.getCredentials('agileCrmApi');
const baseUri = `https://${credentials.subdomain}.agilecrm.com/dev/`;
const options: IRequestOptions = {
method,
headers: {
Accept: 'application/json',
},
body: { id: body.id },
auth: {
username: credentials.email as string,
password: credentials.apiKey as string,
},
uri: uri || baseUri,
json: true,
};
const successfulUpdates = [];
let lastSuccesfulUpdateReturn: any;
const payload: IContactUpdate = body;
try {
// Due to API, we must update each property separately. For user it looks like one seamless update
if (payload.properties) {
options.body.properties = payload.properties;
options.uri = baseUri + 'api/contacts/edit-properties';
lastSuccesfulUpdateReturn = await this.helpers.request(options);
// Iterate trough properties and show them as individial updates instead of only vague "properties"
payload.properties?.map((property: any) => {
successfulUpdates.push(`${property.name}`);
});
delete options.body.properties;
}
if (payload.lead_score) {
options.body.lead_score = payload.lead_score;
options.uri = baseUri + 'api/contacts/edit/lead-score';
lastSuccesfulUpdateReturn = await this.helpers.request(options);
successfulUpdates.push('lead_score');
delete options.body.lead_score;
}
if (body.tags) {
options.body.tags = payload.tags;
options.uri = baseUri + 'api/contacts/edit/tags';
lastSuccesfulUpdateReturn = await this.helpers.request(options);
payload.tags?.map((tag: string) => {
successfulUpdates.push(`(Tag) ${tag}`);
});
delete options.body.tags;
}
if (body.star_value) {
options.body.star_value = payload.star_value;
options.uri = baseUri + 'api/contacts/edit/add-star';
lastSuccesfulUpdateReturn = await this.helpers.request(options);
successfulUpdates.push('star_value');
delete options.body.star_value;
}
return lastSuccesfulUpdateReturn;
} catch (error) {
if (successfulUpdates.length === 0) {
throw new NodeApiError(this.getNode(), error as JsonObject);
} else {
throw new NodeApiError(this.getNode(), error as JsonObject, {
message: `Not all properties updated. Updated properties: ${successfulUpdates.join(', ')}`,
description: error.message,
httpCode: error.statusCode,
});
}
}
}
export function validateJSON(json: string | undefined): any {
let result;
try {
result = JSON.parse(json!);
} catch (exception) {
result = undefined;
}
return result;
}
export function getFilterRules(conditions: ISearchConditions[], matchType: string): IDataObject {
const rules = [];
// eslint-disable-next-line @typescript-eslint/no-for-in-array
for (const key in conditions) {
if (conditions.hasOwnProperty(key)) {
const searchConditions: ISearchConditions = conditions[key];
const rule: IFilterRules = {
LHS: searchConditions.field,
CONDITION: searchConditions.condition_type,
RHS: searchConditions.value as string,
RHS_NEW: searchConditions.value2 as string,
};
rules.push(rule);
}
}
if (matchType === 'anyFilter') {
return {
or_rules: rules,
};
} else {
return {
rules,
};
}
}
export function simplifyResponse(
records: [{ id: string; properties: [{ name: string; value: string }] }],
) {
const results = [];
for (const record of records) {
results.push(
record.properties.reduce(
(obj, value) => Object.assign(obj, { [`${value.name}`]: value.value }),
{ id: record.id },
),
);
}
return results;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB