Improve AgileCrm-Node

This commit is contained in:
Jan Oberhauser 2020-05-11 20:22:31 +02:00
parent f66ed93b0a
commit 7b4980690f
9 changed files with 1173 additions and 1263 deletions

View file

@ -16,10 +16,5 @@
"lerna": "^3.13.1", "lerna": "^3.13.1",
"run-script-os": "^1.0.7" "run-script-os": "^1.0.7"
}, },
"postcss": {}, "postcss": {}
"dependencies": {
"@typescript-eslint/parser": "^2.31.0",
"tslint-eslint-rules": "^5.4.0",
"typescript-tslint-plugin": "^0.5.5"
}
} }

View file

@ -22,7 +22,7 @@ import {
} from './DealDescription'; } from './DealDescription';
import { IContact, IContactUpdate } from './ContactInterface'; import { IContact, IContactUpdate } from './ContactInterface';
import { agileCrmApiRequest, agileCrmApiRequestUpdate, validateJSON} from './GenericFunctions'; import { agileCrmApiRequest, agileCrmApiRequestUpdate, validateJSON } from './GenericFunctions';
import { IDeal } from './DealInterface'; import { IDeal } from './DealInterface';
@ -90,36 +90,31 @@ export class AgileCrm implements INodeType {
const items = this.getInputData(); const items = this.getInputData();
const returnData: IDataObject[] = []; const returnData: IDataObject[] = [];
const length = items.length as unknown as number;
let responseData; let responseData;
const resource = this.getNodeParameter('resource', 0) as string; const resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string; const operation = this.getNodeParameter('operation', 0) as string;
for (let i = 0; i < length; i++) { for (let i = 0; i < items.length; i++) {
if(resource === 'contact' || resource === 'company'){ if (resource === 'contact' || resource === 'company') {
const idGetter = resource === 'contact' ? 'contactId' : 'companyId'; const idGetter = resource === 'contact' ? 'contactId' : 'companyId';
if(operation === 'get'){ if (operation === 'get') {
const contactId = this.getNodeParameter(idGetter, i) as string; const contactId = this.getNodeParameter(idGetter, i) as string;
const endpoint = `api/contacts/${contactId}`; const endpoint = `api/contacts/${contactId}`;
responseData = await agileCrmApiRequest.call(this, 'GET', endpoint, {}); responseData = await agileCrmApiRequest.call(this, 'GET', endpoint, {});
}
if(operation === 'delete'){ } else if (operation === 'delete') {
const contactId = this.getNodeParameter(idGetter, i) as string; const contactId = this.getNodeParameter(idGetter, i) as string;
const endpoint = `api/contacts/${contactId}`; const endpoint = `api/contacts/${contactId}`;
responseData = await agileCrmApiRequest.call(this, 'DELETE', endpoint, {}); responseData = await agileCrmApiRequest.call(this, 'DELETE', endpoint, {});
}
if(operation === 'getAll'){ } else if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', i) as boolean; const returnAll = this.getNodeParameter('returnAll', i) as boolean;
if(resource === 'contact'){ if (resource === 'contact') {
if (returnAll) { if (returnAll) {
const endpoint = 'api/contacts'; const endpoint = 'api/contacts';
responseData = await agileCrmApiRequest.call(this, 'GET', endpoint, {}); responseData = await agileCrmApiRequest.call(this, 'GET', endpoint, {});
@ -139,17 +134,15 @@ export class AgileCrm implements INodeType {
} }
} }
} } else if (operation === 'create') {
if(operation === 'create'){
const jsonParameters = this.getNodeParameter('jsonParameters', i) as boolean; const jsonParameters = this.getNodeParameter('jsonParameters', i) as boolean;
const body: IContact = {}; const body: IContact = {};
const properties : IDataObject[] = []; const properties: IDataObject[] = [];
if (jsonParameters) { if (jsonParameters) {
const additionalFieldsJson = this.getNodeParameter('additionalFieldsJson', i) as string; const additionalFieldsJson = this.getNodeParameter('additionalFieldsJson', i) as string;
if (additionalFieldsJson !== '' ) { if (additionalFieldsJson !== '') {
if (validateJSON(additionalFieldsJson) !== undefined) { if (validateJSON(additionalFieldsJson) !== undefined) {
@ -165,7 +158,7 @@ export class AgileCrm implements INodeType {
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject; const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
// if company, add 'company' as type. default is person // if company, add 'company' as type. default is person
if(resource === 'company'){ if (resource === 'company') {
body.type = 'COMPANY'; body.type = 'COMPANY';
} }
if (additionalFields.starValue) { if (additionalFields.starValue) {
@ -176,36 +169,36 @@ export class AgileCrm implements INodeType {
} }
// Contact specific properties // Contact specific properties
if(resource === 'contact'){ if (resource === 'contact') {
if(additionalFields.firstName){ if (additionalFields.firstName) {
properties.push({ properties.push({
type: 'SYSTEM', type: 'SYSTEM',
name: 'first_name', name: 'first_name',
value: additionalFields.firstName as string value: additionalFields.firstName as string
} as IDataObject); } as IDataObject);
} }
if(additionalFields.lastName){ if (additionalFields.lastName) {
properties.push({ properties.push({
type: 'SYSTEM', type: 'SYSTEM',
name: 'last_name', name: 'last_name',
value: additionalFields.lastName as string value: additionalFields.lastName as string
} as IDataObject); } as IDataObject);
} }
if(additionalFields.company){ if (additionalFields.company) {
properties.push({ properties.push({
type: 'SYSTEM', type: 'SYSTEM',
name: 'company', name: 'company',
value: additionalFields.company as string value: additionalFields.company as string
} as IDataObject); } as IDataObject);
} }
if(additionalFields.title){ if (additionalFields.title) {
properties.push({ properties.push({
type: 'SYSTEM', type: 'SYSTEM',
name: 'title', name: 'title',
value: additionalFields.title as string value: additionalFields.title as string
} as IDataObject); } as IDataObject);
} }
if(additionalFields.emailOptions){ if (additionalFields.emailOptions) {
//@ts-ignore //@ts-ignore
additionalFields.emailOptions.emailProperties.map(property => { additionalFields.emailOptions.emailProperties.map(property => {
properties.push({ properties.push({
@ -216,7 +209,7 @@ export class AgileCrm implements INodeType {
} as IDataObject); } as IDataObject);
}); });
} }
if(additionalFields.addressOptions){ if (additionalFields.addressOptions) {
//@ts-ignore //@ts-ignore
additionalFields.addressOptions.addressProperties.map(property => { additionalFields.addressOptions.addressProperties.map(property => {
properties.push({ properties.push({
@ -228,7 +221,7 @@ export class AgileCrm implements INodeType {
}); });
} }
if(additionalFields.phoneOptions){ if (additionalFields.phoneOptions) {
//@ts-ignore //@ts-ignore
additionalFields.phoneOptions.phoneProperties.map(property => { additionalFields.phoneOptions.phoneProperties.map(property => {
properties.push({ properties.push({
@ -240,7 +233,7 @@ export class AgileCrm implements INodeType {
}); });
} }
} else if (resource === 'company') { } else if (resource === 'company') {
if(additionalFields.email){ if (additionalFields.email) {
properties.push({ properties.push({
type: 'SYSTEM', type: 'SYSTEM',
name: 'email', name: 'email',
@ -248,7 +241,7 @@ export class AgileCrm implements INodeType {
} as IDataObject); } as IDataObject);
} }
if(additionalFields.address){ if (additionalFields.address) {
properties.push({ properties.push({
type: 'SYSTEM', type: 'SYSTEM',
name: 'address', name: 'address',
@ -256,7 +249,7 @@ export class AgileCrm implements INodeType {
} as IDataObject); } as IDataObject);
} }
if(additionalFields.phone){ if (additionalFields.phone) {
properties.push({ properties.push({
type: 'SYSTEM', type: 'SYSTEM',
name: 'phone', name: 'phone',
@ -266,7 +259,7 @@ export class AgileCrm implements INodeType {
} }
if(additionalFields.websiteOptions){ if (additionalFields.websiteOptions) {
//@ts-ignore //@ts-ignore
additionalFields.websiteOptions.websiteProperties.map(property => { additionalFields.websiteOptions.websiteProperties.map(property => {
properties.push({ properties.push({
@ -278,7 +271,7 @@ export class AgileCrm implements INodeType {
}); });
} }
if(additionalFields.customProperties){ if (additionalFields.customProperties) {
//@ts-ignore //@ts-ignore
additionalFields.customProperties.customProperty.map(property => { additionalFields.customProperties.customProperty.map(property => {
properties.push({ properties.push({
@ -294,19 +287,18 @@ export class AgileCrm implements INodeType {
} }
const endpoint = 'api/contacts'; const endpoint = 'api/contacts';
responseData = await agileCrmApiRequest.call(this, 'POST', endpoint, body); responseData = await agileCrmApiRequest.call(this, 'POST', endpoint, body);
}
if(operation === 'update') { } else if (operation === 'update') {
const contactId = this.getNodeParameter(idGetter, i) as string; const contactId = this.getNodeParameter(idGetter, i) as string;
const contactUpdatePayload : IContactUpdate = {id: contactId}; const contactUpdatePayload: IContactUpdate = { id: contactId };
const jsonParameters = this.getNodeParameter('jsonParameters', i) as boolean; const jsonParameters = this.getNodeParameter('jsonParameters', i) as boolean;
const body: IContact = {}; const body: IContact = {};
const properties : IDataObject[] = []; const properties: IDataObject[] = [];
if (jsonParameters) { if (jsonParameters) {
const additionalFieldsJson = this.getNodeParameter('additionalFieldsJson', i) as string; const additionalFieldsJson = this.getNodeParameter('additionalFieldsJson', i) as string;
if (additionalFieldsJson !== '' ) { if (additionalFieldsJson !== '') {
if (validateJSON(additionalFieldsJson) !== undefined) { if (validateJSON(additionalFieldsJson) !== undefined) {
@ -327,41 +319,41 @@ export class AgileCrm implements INodeType {
} }
// Contact specific properties // Contact specific properties
if(resource === 'contact'){ if (resource === 'contact') {
if (additionalFields.leadScore) { if (additionalFields.leadScore) {
body.lead_score = additionalFields.leadScore as string; body.lead_score = additionalFields.leadScore as string;
} }
if(additionalFields.firstName){ if (additionalFields.firstName) {
properties.push({ properties.push({
type: 'SYSTEM', type: 'SYSTEM',
name: 'first_name', name: 'first_name',
value: additionalFields.firstName as string value: additionalFields.firstName as string
} as IDataObject); } as IDataObject);
} }
if(additionalFields.lastName){ if (additionalFields.lastName) {
properties.push({ properties.push({
type: 'SYSTEM', type: 'SYSTEM',
name: 'last_name', name: 'last_name',
value: additionalFields.lastName as string value: additionalFields.lastName as string
} as IDataObject); } as IDataObject);
} }
if(additionalFields.company){ if (additionalFields.company) {
properties.push({ properties.push({
type: 'SYSTEM', type: 'SYSTEM',
name: 'company', name: 'company',
value: additionalFields.company as string value: additionalFields.company as string
} as IDataObject); } as IDataObject);
} }
if(additionalFields.title){ if (additionalFields.title) {
properties.push({ properties.push({
type: 'SYSTEM', type: 'SYSTEM',
name: 'title', name: 'title',
value: additionalFields.title as string value: additionalFields.title as string
} as IDataObject); } as IDataObject);
} }
if(additionalFields.emailOptions){ if (additionalFields.emailOptions) {
//@ts-ignore //@ts-ignore
additionalFields.emailOptions.emailProperties.map(property => { additionalFields.emailOptions.emailProperties.map(property => {
properties.push({ properties.push({
@ -372,7 +364,7 @@ export class AgileCrm implements INodeType {
} as IDataObject); } as IDataObject);
}); });
} }
if(additionalFields.addressOptions){ if (additionalFields.addressOptions) {
//@ts-ignore //@ts-ignore
additionalFields.addressOptions.addressProperties.map(property => { additionalFields.addressOptions.addressProperties.map(property => {
properties.push({ properties.push({
@ -384,7 +376,7 @@ export class AgileCrm implements INodeType {
}); });
} }
if(additionalFields.phoneOptions){ if (additionalFields.phoneOptions) {
//@ts-ignore //@ts-ignore
additionalFields.phoneOptions.phoneProperties.map(property => { additionalFields.phoneOptions.phoneProperties.map(property => {
properties.push({ properties.push({
@ -396,7 +388,7 @@ export class AgileCrm implements INodeType {
}); });
} }
} else if (resource === 'company') { } else if (resource === 'company') {
if(additionalFields.email){ if (additionalFields.email) {
properties.push({ properties.push({
type: 'SYSTEM', type: 'SYSTEM',
name: 'email', name: 'email',
@ -404,7 +396,7 @@ export class AgileCrm implements INodeType {
} as IDataObject); } as IDataObject);
} }
if(additionalFields.address){ if (additionalFields.address) {
properties.push({ properties.push({
type: 'SYSTEM', type: 'SYSTEM',
name: 'address', name: 'address',
@ -412,7 +404,7 @@ export class AgileCrm implements INodeType {
} as IDataObject); } as IDataObject);
} }
if(additionalFields.phone){ if (additionalFields.phone) {
properties.push({ properties.push({
type: 'SYSTEM', type: 'SYSTEM',
name: 'phone', name: 'phone',
@ -422,7 +414,7 @@ export class AgileCrm implements INodeType {
} }
if(additionalFields.websiteOptions){ if (additionalFields.websiteOptions) {
//@ts-ignore //@ts-ignore
additionalFields.websiteOptions.websiteProperties.map(property => { additionalFields.websiteOptions.websiteProperties.map(property => {
properties.push({ properties.push({
@ -433,7 +425,7 @@ export class AgileCrm implements INodeType {
} as IDataObject); } as IDataObject);
}); });
} }
if(additionalFields.customProperties){ if (additionalFields.customProperties) {
//@ts-ignore //@ts-ignore
additionalFields.customProperties.customProperty.map(property => { additionalFields.customProperties.customProperty.map(property => {
properties.push({ properties.push({
@ -453,26 +445,23 @@ export class AgileCrm implements INodeType {
} }
} } else if (resource === 'deal') {
if(resource === 'deal'){
if(operation === 'get'){ if (operation === 'get') {
const dealId = this.getNodeParameter('dealId', i) as string; const dealId = this.getNodeParameter('dealId', i) as string;
const endpoint = `api/opportunity/${dealId}`; const endpoint = `api/opportunity/${dealId}`;
responseData = await agileCrmApiRequest.call(this, 'GET', endpoint, {}); responseData = await agileCrmApiRequest.call(this, 'GET', endpoint, {});
}
if(operation === 'delete'){ } else if (operation === 'delete') {
const contactId = this.getNodeParameter('dealId', i) as string; const contactId = this.getNodeParameter('dealId', i) as string;
const endpoint = `api/opportunity/${contactId}`; const endpoint = `api/opportunity/${contactId}`;
responseData = await agileCrmApiRequest.call(this, 'DELETE', endpoint, {}); responseData = await agileCrmApiRequest.call(this, 'DELETE', endpoint, {});
}
if(operation === 'getAll'){ } else if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', i) as boolean; const returnAll = this.getNodeParameter('returnAll', i) as boolean;
if (returnAll) { if (returnAll) {
const endpoint = 'api/opportunity'; const endpoint = 'api/opportunity';
responseData = await agileCrmApiRequest.call(this, 'GET', endpoint, {}); responseData = await agileCrmApiRequest.call(this, 'GET', endpoint, {});
@ -481,9 +470,8 @@ export class AgileCrm implements INodeType {
const endpoint = `api/opportunity?page_size=${limit}`; const endpoint = `api/opportunity?page_size=${limit}`;
responseData = await agileCrmApiRequest.call(this, 'GET', endpoint, {}); responseData = await agileCrmApiRequest.call(this, 'GET', endpoint, {});
} }
}
if(operation === 'create'){ } else if (operation === 'create') {
const jsonParameters = this.getNodeParameter('jsonParameters', i) as boolean; const jsonParameters = this.getNodeParameter('jsonParameters', i) as boolean;
const body: IDeal = {}; const body: IDeal = {};
@ -491,12 +479,9 @@ export class AgileCrm implements INodeType {
if (jsonParameters) { if (jsonParameters) {
const additionalFieldsJson = this.getNodeParameter('additionalFieldsJson', i) as string; const additionalFieldsJson = this.getNodeParameter('additionalFieldsJson', i) as string;
if (additionalFieldsJson !== '' ) { if (additionalFieldsJson !== '') {
if (validateJSON(additionalFieldsJson) !== undefined) { if (validateJSON(additionalFieldsJson) !== undefined) {
Object.assign(body, JSON.parse(additionalFieldsJson)); Object.assign(body, JSON.parse(additionalFieldsJson));
} else { } else {
throw new Error('Additional fields must be a valid JSON'); throw new Error('Additional fields must be a valid JSON');
} }
@ -504,18 +489,18 @@ export class AgileCrm implements INodeType {
} else { } else {
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject; const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
body.close_date = new Date(this.getNodeParameter('closeDate', i) as string).getTime(); body.close_date = new Date(this.getNodeParameter('closeDate', i) as string).getTime();
body.expected_value = this.getNodeParameter('expectedValue', i) as number; body.expected_value = this.getNodeParameter('expectedValue', i) as number;
body.milestone = this.getNodeParameter('milestone', i) as string; body.milestone = this.getNodeParameter('milestone', i) as string;
body.probability = this.getNodeParameter('probability', i) as number; body.probability = this.getNodeParameter('probability', i) as number;
body.name = this.getNodeParameter('name', i) as string; body.name = this.getNodeParameter('name', i) as string;
if(additionalFields.contactIds){ if (additionalFields.contactIds) {
body.contactIds = additionalFields.contactIds as string[]; body.contactIds = additionalFields.contactIds as string[];
} }
if(additionalFields.customData){ if (additionalFields.customData) {
// @ts-ignore // @ts-ignore
body.customData = additionalFields.customData.customProperty as IDealCustomProperty[]; body.customData = additionalFields.customData.customProperty as IDealCustomProperty[];
} }
@ -524,9 +509,8 @@ export class AgileCrm implements INodeType {
const endpoint = 'api/opportunity'; const endpoint = 'api/opportunity';
responseData = await agileCrmApiRequest.call(this, 'POST', endpoint, body); responseData = await agileCrmApiRequest.call(this, 'POST', endpoint, body);
}
if(operation === 'update'){ } else if (operation === 'update') {
const jsonParameters = this.getNodeParameter('jsonParameters', i) as boolean; const jsonParameters = this.getNodeParameter('jsonParameters', i) as boolean;
const body: IDeal = {}; const body: IDeal = {};
@ -534,14 +518,14 @@ export class AgileCrm implements INodeType {
if (jsonParameters) { if (jsonParameters) {
const additionalFieldsJson = this.getNodeParameter('additionalFieldsJson', i) as string; const additionalFieldsJson = this.getNodeParameter('additionalFieldsJson', i) as string;
if (additionalFieldsJson !== '' ) { if (additionalFieldsJson !== '') {
if (validateJSON(additionalFieldsJson) !== undefined) { if (validateJSON(additionalFieldsJson) !== undefined) {
Object.assign(body, JSON.parse(additionalFieldsJson)); Object.assign(body, JSON.parse(additionalFieldsJson));
} else { } else {
throw new Error('Additional fields must be a valid JSON'); throw new Error('Additional fields must be valid JSON');
} }
} }
@ -549,23 +533,23 @@ export class AgileCrm implements INodeType {
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject; const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
body.id = this.getNodeParameter('dealId', i) as number; body.id = this.getNodeParameter('dealId', i) as number;
if(additionalFields.expectedValue){ if (additionalFields.expectedValue) {
body.expected_value = additionalFields.expectedValue as number; body.expected_value = additionalFields.expectedValue as number;
} }
if(additionalFields.name){ if (additionalFields.name) {
body.name = additionalFields.name as string; body.name = additionalFields.name as string;
} }
if(additionalFields.probability){ if (additionalFields.probability) {
body.probability = additionalFields.probability as number; body.probability = additionalFields.probability as number;
} }
if(additionalFields.contactIds){ if (additionalFields.contactIds) {
body.contactIds = additionalFields.contactIds as string[]; body.contactIds = additionalFields.contactIds as string[];
} }
if(additionalFields.customData){ if (additionalFields.customData) {
// @ts-ignore // @ts-ignore
body.customData = additionalFields.customData.customProperty as IDealCustomProperty[]; body.customData = additionalFields.customData.customProperty as IDealCustomProperty[];
} }
@ -577,7 +561,6 @@ export class AgileCrm implements INodeType {
} }
} }
if (Array.isArray(responseData)) { if (Array.isArray(responseData)) {
returnData.push.apply(returnData, responseData as IDataObject[]); returnData.push.apply(returnData, responseData as IDataObject[]);
} else { } else {
@ -588,5 +571,5 @@ export class AgileCrm implements INodeType {
return [this.helpers.returnJsonArray(returnData)]; return [this.helpers.returnJsonArray(returnData)];
} }
} }

View file

@ -45,11 +45,11 @@ export const companyOperations = [
}, },
] as INodeProperties[]; ] as INodeProperties[];
export const companyFields = [ export const companyFields = [
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* company:get */ /* company:get */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
{ {
displayName: 'company ID', displayName: 'Company ID',
name: 'companyId', name: 'companyId',
type: 'string', type: 'string',
required: true, required: true,
@ -66,7 +66,7 @@ export const companyFields = [
default: '', default: '',
description: 'Unique identifier for a particular company', description: 'Unique identifier for a particular company',
}, },
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* company:get all */ /* company:get all */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
@ -147,7 +147,6 @@ export const companyFields = [
], ],
}, },
}, },
description: 'Object of values to set as described <a href="https://github.com/agilecrm/rest-api#1-companys---companies-api" target="_blank">here</a>.', description: 'Object of values to set as described <a href="https://github.com/agilecrm/rest-api#1-companys---companies-api" target="_blank">here</a>.',
}, },
{ {
@ -174,18 +173,14 @@ export const companyFields = [
displayName: 'Address', displayName: 'Address',
name: 'email', name: 'email',
type: 'string', type: 'string',
default: '', default: '',
placeholder: 'Company address',
description: 'Company address.', description: 'Company address.',
}, },
{ {
displayName: 'Email', displayName: 'Email',
name: 'email', name: 'email',
type: 'string', type: 'string',
default: '', default: '',
placeholder: 'Company email',
description: 'Company email.', description: 'Company email.',
}, },
{ {
@ -193,16 +188,13 @@ export const companyFields = [
name: 'name', name: 'name',
type: 'string', type: 'string',
default: '', default: '',
placeholder: 'Company name',
description: 'Company name.', description: 'Company name.',
}, },
{ {
displayName: 'Phone', displayName: 'Phone',
name: 'phone', name: 'phone',
type: 'string', type: 'string',
default: '', default: '',
placeholder: 'Company phone',
description: 'Company phone.', description: 'Company phone.',
}, },
{ {
@ -247,14 +239,12 @@ export const companyFields = [
multipleValueButtonText: 'Add Tag', multipleValueButtonText: 'Add Tag',
}, },
default: [], default: [],
placeholder: 'Tag',
description: 'Unique identifiers added to company, for easy management of companys. This is not applicable for companies.', description: 'Unique identifiers added to company, for easy management of companys. This is not applicable for companies.',
}, },
{ {
displayName: 'Website', displayName: 'Website',
name: 'websiteOptions', name: 'websiteOptions',
type: 'fixedCollection', type: 'fixedCollection',
description: 'Companies websites.', description: 'Companies websites.',
typeOptions: { typeOptions: {
multipleValues: true, multipleValues: true,
@ -273,35 +263,35 @@ export const companyFields = [
description: 'Type of website.', description: 'Type of website.',
options: [ options: [
{ {
name: 'FACEBOOK', name: 'Facebook',
value: 'facebook', value: 'facebook',
}, },
{ {
name: 'FEED', name: 'Feed',
value: 'feed', value: 'feed',
}, },
{ {
name: 'FLICKR', name: 'Flickr',
value: 'flickr', value: 'flickr',
}, },
{ {
name: 'LINKEDIN', name: 'Github',
value: 'linkedin',
},
{
name: 'GITHUB',
value: 'github', value: 'github',
}, },
{ {
name: 'GOOGLE_PLUS', name: 'Google Plus',
value: 'googlePlus', value: 'googlePlus',
}, },
{ {
name: 'SKYPE', name: 'LinkedIn',
value: 'linkedin',
},
{
name: 'Skype',
value: 'skype', value: 'skype',
}, },
{ {
name: 'TWITTER', name: 'Twitter',
value: 'twitter', value: 'twitter',
}, },
{ {
@ -309,14 +299,14 @@ export const companyFields = [
value: 'url', value: 'url',
}, },
{ {
name: 'XING', name: 'Xing',
value: 'xing', value: 'xing',
}, },
{ {
name: 'YOUTUBE', name: 'YouTube',
value: 'youtube', value: 'youtube',
}, },
] ],
}, },
{ {
displayName: 'URL', displayName: 'URL',
@ -324,19 +314,16 @@ export const companyFields = [
type: 'string', type: 'string',
required: true, required: true,
default: '', default: '',
description: 'Website URL', description: 'Website URL',
} },
] ],
}, },
],
]
}, },
{ {
displayName: 'Custom Properties', displayName: 'Custom Properties',
name: 'customProperties', name: 'customProperties',
type: 'fixedCollection', type: 'fixedCollection',
description: 'Custom Properties', description: 'Custom Properties',
typeOptions: { typeOptions: {
multipleValues: true, multipleValues: true,
@ -352,33 +339,29 @@ export const companyFields = [
type: 'string', type: 'string',
required: true, required: true,
default: '', default: '',
description: 'Property name.' description: 'Property name.'
}, },
{ {
displayName: 'Sub Type', displayName: 'Sub Type',
name: 'subtype', name: 'subtype',
type: 'string', type: 'string',
default: '', default: '',
description: 'Property sub type.', description: 'Property sub type.',
}, },
{ {
displayName: 'Value', displayName: 'Value',
name: 'value', name: 'value',
type: 'string', type: 'string',
default: '', default: '',
description: 'Property value.', description: 'Property value.',
} },
] ],
}, },
] ],
}, },
], ],
}, },
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* company:delete */ /* company:delete */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
@ -400,11 +383,12 @@ export const companyFields = [
default: '', default: '',
description: 'ID of company to delete', description: 'ID of company to delete',
}, },
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* company:update */ /* company:update */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
{ {
displayName: 'company ID', displayName: 'Company ID',
name: 'companyId', name: 'companyId',
type: 'string', type: 'string',
required: true, required: true,
@ -459,7 +443,6 @@ export const companyFields = [
], ],
}, },
}, },
description: 'Object of values to set as described <a href="https://github.com/agilecrm/rest-api#1-companys---companies-api" target="_blank">here</a>.', description: 'Object of values to set as described <a href="https://github.com/agilecrm/rest-api#1-companys---companies-api" target="_blank">here</a>.',
}, },
{ {
@ -487,7 +470,6 @@ export const companyFields = [
name: 'email', name: 'email',
type: 'string', type: 'string',
default: '', default: '',
placeholder: 'Company address',
description: 'Company address.', description: 'Company address.',
}, },
{ {
@ -495,7 +477,6 @@ export const companyFields = [
name: 'email', name: 'email',
type: 'string', type: 'string',
default: '', default: '',
placeholder: 'Company email',
description: 'Company email.', description: 'Company email.',
}, },
{ {
@ -507,29 +488,29 @@ export const companyFields = [
options: [ options: [
{ {
name: '0', name: '0',
value: 0 value: 0,
}, },
{ {
name: '1', name: '1',
value: 1 value: 1,
}, },
{ {
name: '2', name: '2',
value: 2 value: 2,
}, },
{ {
name: '3', name: '3',
value: 3 value: 3,
}, },
{ {
name: '4', name: '4',
value: 4 value: 4,
}, },
{ {
name: '5', name: '5',
value: 5 value: 5,
}, },
] ],
}, },
{ {
displayName: 'Tags', displayName: 'Tags',
@ -540,7 +521,6 @@ export const companyFields = [
multipleValueButtonText: 'Add Tag', multipleValueButtonText: 'Add Tag',
}, },
default: [], default: [],
placeholder: 'Tag',
description: 'Unique identifiers added to company, for easy management of companys. This is not applicable for companies.', description: 'Unique identifiers added to company, for easy management of companys. This is not applicable for companies.',
}, },
{ {
@ -548,7 +528,6 @@ export const companyFields = [
name: 'name', name: 'name',
type: 'string', type: 'string',
default: '', default: '',
placeholder: 'Company name',
description: 'Company name.', description: 'Company name.',
}, },
{ {
@ -556,14 +535,13 @@ export const companyFields = [
name: 'phone', name: 'phone',
type: 'string', type: 'string',
default: '', default: '',
placeholder: 'Company phone',
description: 'Company phone.', description: 'Company phone.',
}, },
{ {
displayName: 'Website', displayName: 'Website',
name: 'websiteOptions', name: 'websiteOptions',
type: 'fixedCollection', type: 'fixedCollection',
description: 'companys websites.', description: 'Companys websites.',
typeOptions: { typeOptions: {
multipleValues: true, multipleValues: true,
}, },
@ -581,35 +559,35 @@ export const companyFields = [
description: 'Type of website.', description: 'Type of website.',
options: [ options: [
{ {
name: 'FACEBOOK', name: 'Facebook',
value: 'facebook', value: 'facebook',
}, },
{ {
name: 'FEED', name: 'Feed',
value: 'feed', value: 'feed',
}, },
{ {
name: 'FLICKR', name: 'Flickr',
value: 'flickr', value: 'flickr',
}, },
{ {
name: 'LINKEDIN', name: 'Github',
value: 'linkedin',
},
{
name: 'GITHUB',
value: 'github', value: 'github',
}, },
{ {
name: 'GOOGLE_PLUS', name: 'Google Plus',
value: 'googlePlus', value: 'googlePlus',
}, },
{ {
name: 'SKYPE', name: 'LinkedIn',
value: 'linkedin',
},
{
name: 'Skype',
value: 'skype', value: 'skype',
}, },
{ {
name: 'TWITTER', name: 'Twitter',
value: 'twitter', value: 'twitter',
}, },
{ {
@ -617,14 +595,14 @@ export const companyFields = [
value: 'url', value: 'url',
}, },
{ {
name: 'XING', name: 'Xing',
value: 'xing', value: 'xing',
}, },
{ {
name: 'YOUTUBE', name: 'YouTube',
value: 'youtube', value: 'youtube',
}, },
] ],
}, },
{ {
displayName: 'URL', displayName: 'URL',
@ -633,10 +611,10 @@ export const companyFields = [
required: true, required: true,
default: '', default: '',
description: 'Website URL', description: 'Website URL',
} },
] ],
}, },
] ],
}, },
{ {
displayName: 'Custom Properties', displayName: 'Custom Properties',
@ -657,7 +635,7 @@ export const companyFields = [
type: 'string', type: 'string',
required: true, required: true,
default: '', default: '',
description: 'Property name.' description: 'Property name.',
}, },
{ {
displayName: 'Sub Type', displayName: 'Sub Type',
@ -672,10 +650,9 @@ export const companyFields = [
type: 'string', type: 'string',
default: '', default: '',
description: 'Property value.', description: 'Property value.',
} },
] ],
}, },
] ]
}, },
], ],

File diff suppressed because it is too large Load diff

View file

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

View file

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

View file

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

View file

@ -1,6 +1,6 @@
import { import {
OptionsWithUri OptionsWithUri
} from 'request'; } from 'request';
import { import {
IExecuteFunctions, IExecuteFunctions,
@ -12,81 +12,74 @@ import {
import { import {
IDataObject, IDataObject,
} from 'n8n-workflow'; } from 'n8n-workflow';
import { IContactUpdate, IProperty } from './ContactInterface'; import { IContactUpdate } from './ContactInterface';
export async function agileCrmApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: any = {}, query: IDataObject = {}, uri?: string): Promise<any> { export async function agileCrmApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: any = {}, query: IDataObject = {}, uri?: string): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('agileCrmApi'); const credentials = this.getCredentials('agileCrmApi');
const options: OptionsWithUri = { const options: OptionsWithUri = {
method, method,
headers: { headers: {
'Accept': 'application/json', 'Accept': 'application/json',
}, },
auth: { auth: {
username: credentials!.email as string, username: credentials!.email as string,
password: credentials!.apiKey as string password: credentials!.apiKey as string
}, },
uri: uri || `https://n8nio.agilecrm.com/dev/${endpoint}`, uri: uri || `https://n8nio.agilecrm.com/dev/${endpoint}`,
json: true json: true,
}; };
// Only add Body property if method not GET or DELETE to avoid 400 response // Only add Body property if method not GET or DELETE to avoid 400 response
if(method !== "GET" && method !== "DELETE"){ if (method !== 'GET' && method !== 'DELETE') {
options.body = body; options.body = body;
} }
try { try {
return await this.helpers.request!(options); return await this.helpers.request!(options);
} catch (error) { } catch (error) {
throw new Error(`AgileCRM error response: ${error.message}`);
if (error.response && error.response.body && error.response.body.errors) {
const errorMessages = error.response.body.errors.map((e: IDataObject) => e.message);
throw new Error(`AgileCRM error response [${error.statusCode}]: ${errorMessages.join(' | ')}`);
}
throw error;
} }
} }
export async function agileCrmApiRequestUpdate(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method = 'PUT', endpoint?: string, body: any = {}, query: IDataObject = {}, uri?: string): Promise<any> { export async function agileCrmApiRequestUpdate(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method = 'PUT', endpoint?: string, body: any = {}, query: IDataObject = {}, uri?: string): Promise<any> { // tslint:disable-line:no-any
const baseUri = 'https://n8nio.agilecrm.com/dev/'; const baseUri = 'https://n8nio.agilecrm.com/dev/';
const credentials = this.getCredentials('agileCrmApi'); const credentials = this.getCredentials('agileCrmApi');
const options: OptionsWithUri = { const options: OptionsWithUri = {
method, method,
headers: { headers: {
'Accept': 'application/json', 'Accept': 'application/json',
}, },
body: {id: body.id}, body: { id: body.id },
auth: { auth: {
username: credentials!.email as string, username: credentials!.email as string,
password: credentials!.apiKey as string password: credentials!.apiKey as string,
}, },
uri: uri || baseUri, uri: uri || baseUri,
json: true json: true,
}; };
const successfulUpdates = []; const successfulUpdates = [];
let lastSuccesfulUpdateReturn : any; let lastSuccesfulUpdateReturn: any; // tslint:disable-line:no-any
const payload : IContactUpdate = body; const payload: IContactUpdate = body;
try { try {
// Due to API, we must update each property separately. For user it looks like one seamless update // Due to API, we must update each property separately. For user it looks like one seamless update
if(payload.properties){ if (payload.properties) {
options.body.properties = payload.properties; options.body.properties = payload.properties;
options.uri = baseUri + 'api/contacts/edit-properties'; options.uri = baseUri + 'api/contacts/edit-properties';
lastSuccesfulUpdateReturn = await this.helpers.request!(options); lastSuccesfulUpdateReturn = await this.helpers.request!(options);
// Iterate trough properties and show them as individial updates instead of only vague "properties" // Iterate trough properties and show them as individial updates instead of only vague "properties"
payload.properties?.map((property : any) => { payload.properties?.map((property: any) => { // tslint:disable-line:no-any
successfulUpdates.push(`${property.name} `); successfulUpdates.push(`${property.name}`);
}); });
delete options.body.properties; delete options.body.properties;
} }
if(payload.lead_score){ if (payload.lead_score) {
options.body.lead_score = payload.lead_score; options.body.lead_score = payload.lead_score;
options.uri = baseUri + 'api/contacts/edit/lead-score'; options.uri = baseUri + 'api/contacts/edit/lead-score';
lastSuccesfulUpdateReturn = await this.helpers.request!(options); lastSuccesfulUpdateReturn = await this.helpers.request!(options);
@ -95,18 +88,18 @@ export async function agileCrmApiRequestUpdate(this: IHookFunctions | IExecuteFu
delete options.body.lead_score; delete options.body.lead_score;
} }
if(body.tags){ if (body.tags) {
options.body.tags = payload.tags; options.body.tags = payload.tags;
options.uri = baseUri + 'api/contacts/edit/tags'; options.uri = baseUri + 'api/contacts/edit/tags';
lastSuccesfulUpdateReturn = await this.helpers.request!(options); lastSuccesfulUpdateReturn = await this.helpers.request!(options);
payload.tags?.map((tag : string) => { payload.tags?.map((tag: string) => {
successfulUpdates.push(`(Tag) ${tag} `); successfulUpdates.push(`(Tag) ${tag}`);
}); });
delete options.body.tags; delete options.body.tags;
} }
if(body.star_value){ if (body.star_value) {
options.body.star_value = payload.star_value; options.body.star_value = payload.star_value;
options.uri = baseUri + 'api/contacts/edit/add-star'; options.uri = baseUri + 'api/contacts/edit/add-star';
lastSuccesfulUpdateReturn = await this.helpers.request!(options); lastSuccesfulUpdateReturn = await this.helpers.request!(options);
@ -119,13 +112,11 @@ export async function agileCrmApiRequestUpdate(this: IHookFunctions | IExecuteFu
return lastSuccesfulUpdateReturn; return lastSuccesfulUpdateReturn;
} catch (error) { } catch (error) {
if (successfulUpdates.length === 0) {
if (error.response && error.response.body && error.response.body.errors) { throw new Error(`AgileCRM error response: ${error.message}`);
const errorMessages = error.response.body.errors.map((e: IDataObject) => e.message); } else {
throw new Error(`AgileCRM error response [${error.statusCode}]: ${errorMessages.join(' | ')}`); throw new Error(`Not all properties updated. Updated properties: ${successfulUpdates.join(', ')} \n \nAgileCRM error response: ${error.message}`);
} }
throw new Error(`Not all items updated. Updated items: ${successfulUpdates.join(' , ')} \n \n` + error);
} }
} }
@ -139,4 +130,3 @@ export function validateJSON(json: string | undefined): any { // tslint:disable-
} }
return result; return result;
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 1.2 KiB