Add Contact List resource and deal:search operation (#1217)

*  Add Contact List resource and deal:search operation

*  Minior improvements to Hubspot-Node

Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
This commit is contained in:
Ricardo Espinoza 2020-12-02 07:25:13 -05:00 committed by GitHub
parent 00249c85ee
commit bea7469c61
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 1886 additions and 1374 deletions

View file

@ -0,0 +1,160 @@
import {
INodeProperties,
} from 'n8n-workflow';
export const contactListOperations = [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
displayOptions: {
show: {
resource: [
'contactList',
],
},
},
options: [
{
name: 'Add',
value: 'add',
description: 'Add contact to a list',
},
{
name: 'Remove',
value: 'remove',
description: 'Remove a contact from a list',
},
],
default: 'add',
description: 'The operation to perform.',
},
] as INodeProperties[];
export const contactListFields = [
/* -------------------------------------------------------------------------- */
/* contactList:add */
/* -------------------------------------------------------------------------- */
{
displayName: 'By',
name: 'by',
type: 'options',
options: [
{
name: 'Contact ID',
value: 'id',
},
{
name: 'Email',
value: 'email',
},
],
required: true,
displayOptions: {
show: {
resource: [
'contactList',
],
operation: [
'add',
],
},
},
default: 'email',
},
{
displayName: 'Email',
name: 'email',
type: 'string',
required: true,
displayOptions: {
show: {
resource: [
'contactList',
],
operation: [
'add',
],
by: [
'email',
],
},
},
default: '',
},
{
displayName: 'Contact ID',
name: 'id',
type: 'string',
required: true,
displayOptions: {
show: {
resource: [
'contactList',
],
operation: [
'add',
],
by: [
'id',
],
},
},
default: '',
},
{
displayName: 'List ID',
name: 'listId',
type: 'string',
required: true,
displayOptions: {
show: {
resource: [
'contactList',
],
operation: [
'add',
],
},
},
default: '',
},
/* -------------------------------------------------------------------------- */
/* contactList:remove */
/* -------------------------------------------------------------------------- */
{
displayName: 'Contact ID',
name: 'id',
type: 'string',
required: true,
displayOptions: {
show: {
resource: [
'contactList',
],
operation: [
'remove',
],
},
},
default: '',
},
{
displayName: 'List ID',
name: 'listId',
type: 'string',
required: true,
displayOptions: {
show: {
resource: [
'contactList',
],
operation: [
'remove',
],
},
},
default: '',
},
] as INodeProperties[];

View file

@ -1,6 +1,6 @@
import {
INodeProperties,
} from 'n8n-workflow';
} from 'n8n-workflow';
export const dealOperations = [
{
@ -45,6 +45,11 @@ export const dealOperations = [
value: 'getRecentlyModified',
description: 'Get recently modified deals',
},
{
name: 'Search',
value: 'search',
description: 'Search deals',
},
{
name: 'Update',
value: 'update',
@ -58,9 +63,9 @@ export const dealOperations = [
export const dealFields = [
/* -------------------------------------------------------------------------- */
/* deal:create */
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
/* deal:create */
/* -------------------------------------------------------------------------- */
{
displayName: 'Deal Stage',
name: 'stage',
@ -111,7 +116,7 @@ export const dealFields = [
name: 'associatedCompany',
type: 'multiOptions',
typeOptions: {
loadOptionsMethod:'getCompanies' ,
loadOptionsMethod: 'getCompanies',
},
default: [],
},
@ -120,7 +125,7 @@ export const dealFields = [
name: 'associatedVids',
type: 'multiOptions',
typeOptions: {
loadOptionsMethod:'getContacts' ,
loadOptionsMethod: 'getContacts',
},
default: [],
},
@ -188,9 +193,10 @@ export const dealFields = [
},
],
},
/* -------------------------------------------------------------------------- */
/* deal:update */
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
/* deal:update */
/* -------------------------------------------------------------------------- */
{
displayName: 'Deal ID',
name: 'dealId',
@ -307,9 +313,10 @@ export const dealFields = [
},
],
},
/* -------------------------------------------------------------------------- */
/* deal:get */
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
/* deal:get */
/* -------------------------------------------------------------------------- */
{
displayName: 'Deal ID',
name: 'dealId',
@ -355,9 +362,10 @@ export const dealFields = [
},
],
},
/* -------------------------------------------------------------------------- */
/* deal:getAll */
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
/* deal:getAll */
/* -------------------------------------------------------------------------- */
{
displayName: 'Return All',
name: 'returnAll',
@ -444,9 +452,10 @@ export const dealFields = [
},
],
},
/* -------------------------------------------------------------------------- */
/* deal:delete */
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
/* deal:delete */
/* -------------------------------------------------------------------------- */
{
displayName: 'Deal ID',
name: 'dealId',
@ -465,9 +474,10 @@ export const dealFields = [
default: '',
description: 'Unique identifier for a particular deal',
},
/* -------------------------------------------------------------------------- */
/* deal:getRecentlyCreated deal:getRecentlyModified */
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
/* deal:getRecentlyCreated deal:getRecentlyModified */
/* -------------------------------------------------------------------------- */
{
displayName: 'Return All',
name: 'returnAll',
@ -546,4 +556,238 @@ export const dealFields = [
},
],
},
/*--------------------------------------------------------------------------- */
/* deal:search */
/* -------------------------------------------------------------------------- */
{
displayName: 'Return All',
name: 'returnAll',
type: 'boolean',
displayOptions: {
show: {
resource: [
'deal',
],
operation: [
'search',
],
},
},
default: false,
description: 'If all results should be returned or only up to a given limit.',
},
{
displayName: 'Limit',
name: 'limit',
type: 'number',
displayOptions: {
show: {
resource: [
'deal',
],
operation: [
'search',
],
returnAll: [
false,
],
},
},
typeOptions: {
minValue: 1,
maxValue: 250,
},
default: 100,
description: 'How many results to return.',
},
{
displayName: 'Filter Groups',
name: 'filterGroupsUi',
type: 'fixedCollection',
default: '',
placeholder: 'Add Filter Group',
typeOptions: {
multipleValues: true,
},
required: false,
displayOptions: {
show: {
resource: [
'deal',
],
operation: [
'search',
],
},
},
options: [
{
name: 'filterGroupsValues',
displayName: 'Filter Group',
values: [
{
displayName: 'Filters',
name: 'filtersUi',
type: 'fixedCollection',
default: '',
placeholder: 'Add Filter',
typeOptions: {
multipleValues: true,
},
required: false,
options: [
{
name: 'filterValues',
displayName: 'Filter',
values: [
{
displayName: 'Property Name',
name: 'propertyName',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getDealProperties',
},
default: '',
},
{
displayName: 'Operator',
name: 'operator',
type: 'options',
options: [
{
name: 'Equal',
value: 'EQ',
},
{
name: 'Not Equal',
value: 'NEQ',
},
{
name: 'Less Than',
value: 'LT',
},
{
name: 'Less Than Or Equal',
value: 'LTE',
},
{
name: 'Greater Than',
value: 'GT',
},
{
name: 'Greater Than Or Equal',
value: 'GTE',
},
{
name: 'Is Known',
value: 'HAS_PROPERTY',
},
{
name: 'Is Unknown',
value: 'NOT_HAS_PROPERTY',
},
{
name: 'Contains Exactly',
value: 'CONSTAIN_TOKEN',
},
{
name: `Doesn't Contain Exactly`,
value: 'NOT_CONSTAIN_TOKEN',
},
],
default: 'EQ',
},
{
displayName: 'Value',
name: 'value',
displayOptions: {
hide: {
operator: [
'HAS_PROPERTY',
'NOT_HAS_PROPERTY',
],
},
},
type: 'string',
default: '',
},
],
},
],
description: 'Use filters to limit the results to only CRM objects with matching property values. More info <a href="https://developers.hubspot.com/docs/api/crm/search">here</a>',
},
],
},
],
description: `When multiple filters are provided within a filterGroup, they will be combined using a logical AND operator.<br>
When multiple filterGroups are provided, they will be combined using a logical OR operator.<br>
The system supports a maximum of three filterGroups with up to three filters each.<br>
More info <a href="https://developers.hubspot.com/docs/api/crm/search">here</a>`,
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
type: 'collection',
placeholder: 'Add Field',
default: {},
displayOptions: {
show: {
resource: [
'deal',
],
operation: [
'search',
],
},
},
options: [
{
displayName: 'Direction',
name: 'direction',
type: 'options',
options: [
{
name: 'ASC',
value: 'ASCENDING',
},
{
name: 'DESC',
value: 'DESCENDING',
},
],
default: 'DESCENDING',
description: 'Defines the direction in which search results are ordered. Default value is DESC.',
},
{
displayName: 'Fields',
name: 'properties',
type: 'multiOptions',
typeOptions: {
loadOptionsMethod: 'getDealProperties',
},
default: [],
description: `Used to include specific deal properties in the results.<br/>
By default, the results will only include Deal ID and will not include the values for any properties for your companys.<br/>
Including this parameter will include the data for the specified property in the results.<br/>
You can include this parameter multiple times to request multiple properties separed by ,.`,
},
{
displayName: 'Query',
name: 'query',
type: 'string',
default: '',
description: 'Perform a text search against all property values for an object type',
},
{
displayName: 'Sort By',
name: 'sortBy',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getDealProperties',
},
default: 'createdate',
},
],
},
] as INodeProperties[];

View file

@ -14,14 +14,19 @@ import {
import {
hubspotApiRequest,
hubspotApiRequestAllItems,
} from './GenericFunctions';
} from './GenericFunctions';
import {
import {
contactFields,
contactOperations,
} from './ContactDescription';
import {
import {
contactListFields,
contactListOperations,
} from './ContactListDescription';
import {
companyFields,
companyOperations,
} from './CompanyDescription';
@ -34,9 +39,9 @@ import {
import {
formFields,
formOperations,
} from './FormDescription';
} from './FormDescription';
import {
import {
ticketFields,
ticketOperations,
} from './TicketDescription';
@ -52,7 +57,7 @@ import {
import {
snakeCase,
} from 'change-case';
} from 'change-case';
export class Hubspot implements INodeType {
description: INodeTypeDescription = {
@ -120,6 +125,10 @@ export class Hubspot implements INodeType {
name: 'Contact',
value: 'contact',
},
{
name: 'Contact List',
value: 'contactList',
},
{
name: 'Company',
value: 'company',
@ -143,6 +152,9 @@ export class Hubspot implements INodeType {
// CONTACT
...contactOperations,
...contactFields,
// CONTACT LIST
...contactListOperations,
...contactListFields,
// COMPANY
...companyOperations,
...companyFields,
@ -543,7 +555,7 @@ export class Hubspot implements INodeType {
const endpoint = '/properties/v1/deals/properties/named/dealtype';
const dealTypes = await hubspotApiRequest.call(this, 'GET', endpoint);
for (const dealType of dealTypes.options) {
const dealTypeName = dealType.label ;
const dealTypeName = dealType.label;
const dealTypeId = dealType.value;
returnData.push({
name: dealTypeName,
@ -571,6 +583,22 @@ export class Hubspot implements INodeType {
}
return returnData;
},
// Get all the deal properties to display them to user so that he can
// select them easily
async getDealProperties(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
const endpoint = '/properties/v2/deals/properties';
const properties = await hubspotApiRequest.call(this, 'GET', endpoint, {});
for (const property of properties) {
const propertyName = property.label;
const propertyId = property.name;
returnData.push({
name: propertyName,
value: propertyId,
});
}
return returnData;
},
/* -------------------------------------------------------------------------- */
/* FORM */
@ -802,7 +830,7 @@ export class Hubspot implements INodeType {
const endpoint = '/contacts/v1/lists/all/contacts/all';
const contacts = await hubspotApiRequestAllItems.call(this, 'contacts', 'GET', endpoint);
for (const contact of contacts) {
const contactName = `${contact.properties.firstname.value} ${contact.properties.lastname.value}` ;
const contactName = `${contact.properties.firstname.value} ${contact.properties.lastname.value}`;
const contactId = contact.vid;
returnData.push({
name: contactName,
@ -823,6 +851,38 @@ export class Hubspot implements INodeType {
const qs: IDataObject = {};
const resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string;
//https://legacydocs.hubspot.com/docs/methods/lists/contact-lists-overview
if (resource === 'contactList') {
//https://legacydocs.hubspot.com/docs/methods/lists/add_contact_to_list
if (operation === 'add') {
const listId = this.getNodeParameter('listId', 0) as string;
const by = this.getNodeParameter('by', 0) as string;
const body: { [key: string]: [] } = { emails: [], vids: [] };
for (let i = 0; i < length; i++) {
if (by === 'id') {
const id = this.getNodeParameter('id', i) as string;
body.vids.push(parseInt(id, 10) as never);
} else {
const email = this.getNodeParameter('email', i) as string;
body.emails.push(email as never);
}
}
responseData = await hubspotApiRequest.call(this, 'POST', `/contacts/v1/lists/${listId}/add`, body);
returnData.push(responseData);
}
//https://legacydocs.hubspot.com/docs/methods/lists/remove_contact_from_list
if (operation === 'remove') {
const listId = this.getNodeParameter('listId', 0) as string;
const body: { [key: string]: [] } = { vids: [] };
for (let i = 0; i < length; i++) {
const id = this.getNodeParameter('id', i) as string;
body.vids.push(parseInt(id, 10) as never);
}
responseData = await hubspotApiRequest.call(this, 'POST', `/contacts/v1/lists/${listId}/remove`, body);
returnData.push(responseData);
}
} else {
for (let i = 0; i < length; i++) {
//https://developers.hubspot.com/docs/methods/contacts/create_or_update
if (resource === 'contact') {
@ -1948,6 +2008,53 @@ export class Hubspot implements INodeType {
const endpoint = `/deals/v1/deal/${dealId}`;
responseData = await hubspotApiRequest.call(this, 'DELETE', endpoint);
}
//https://developers.hubspot.com/docs/api/crm/search
if (operation === 'search') {
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
const returnAll = this.getNodeParameter('returnAll', 0) as boolean;
const filtersGroupsUi = this.getNodeParameter('filterGroupsUi', i) as IDataObject;
const sortBy = additionalFields.sortBy || 'createdate';
const direction = additionalFields.direction || 'DESCENDING';
const body: IDataObject = {
sorts: [
{
propertyName: sortBy,
direction,
},
],
};
if (filtersGroupsUi) {
const filterGroupValues = (filtersGroupsUi as IDataObject).filterGroupsValues as IDataObject[];
if (filterGroupValues) {
body.filterGroups = [];
for (const filterGroupValue of filterGroupValues) {
if (filterGroupValue.filtersUi) {
const filterValues = (filterGroupValue.filtersUi as IDataObject).filterValues as IDataObject[];
if (filterValues) {
//@ts-ignore
body.filterGroups.push({ filters: filterValues });
}
}
}
}
}
Object.assign(body, additionalFields);
const endpoint = '/crm/v3/objects/deals/search';
if (returnAll) {
responseData = await hubspotApiRequestAllItems.call(this, 'results', 'POST', endpoint, body, qs);
} else {
qs.count = this.getNodeParameter('limit', 0) as number;
responseData = await hubspotApiRequest.call(this, 'POST', endpoint, body, qs);
responseData = responseData.results;
}
}
}
//https://developers.hubspot.com/docs/methods/forms/forms_overview
if (resource === 'form') {
@ -2245,6 +2352,7 @@ export class Hubspot implements INodeType {
returnData.push(responseData as IDataObject);
}
}
}
return [this.helpers.returnJsonArray(returnData)];
}
}