n8n-nodes-base
Version:
Base nodes of n8n
1,048 lines • 156 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.HubspotV2 = void 0;
const change_case_1 = require("change-case");
const set_1 = __importDefault(require("lodash/set"));
const n8n_workflow_1 = require("n8n-workflow");
const CompanyDescription_1 = require("./CompanyDescription");
const ContactDescription_1 = require("./ContactDescription");
const ContactListDescription_1 = require("./ContactListDescription");
const DealDescription_1 = require("./DealDescription");
const EngagementDescription_1 = require("./EngagementDescription");
const GenericFunctions_1 = require("./GenericFunctions");
const TicketDescription_1 = require("./TicketDescription");
const utilities_1 = require("../../../utils/utilities");
const parseToTimestamp_1 = require("./utils/parseToTimestamp");
class HubspotV2 {
description;
constructor(baseDescription) {
this.description = {
...baseDescription,
group: ['output'],
version: [2, 2.1, 2.2],
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
defaults: {
name: 'HubSpot',
},
usableAsTool: true,
inputs: [n8n_workflow_1.NodeConnectionTypes.Main],
outputs: [n8n_workflow_1.NodeConnectionTypes.Main],
credentials: [
{
name: 'hubspotApi',
required: true,
testedBy: 'hubspotApiTest',
displayOptions: {
show: {
authentication: ['apiKey'],
},
},
},
{
name: 'hubspotAppToken',
required: true,
testedBy: 'hubspotApiTest',
displayOptions: {
show: {
authentication: ['appToken'],
},
},
},
{
name: 'hubspotOAuth2Api',
required: true,
displayOptions: {
show: {
authentication: ['oAuth2'],
},
},
},
],
properties: [
{
displayName: 'Authentication',
name: 'authentication',
type: 'options',
options: [
{
name: 'API Key',
value: 'apiKey',
},
{
name: 'APP Token',
value: 'appToken',
},
{
name: 'OAuth2',
value: 'oAuth2',
},
],
default: 'apiKey',
},
{
displayName: 'Resource',
name: 'resource',
type: 'options',
noDataExpression: true,
options: [
{
name: 'Company',
value: 'company',
},
{
name: 'Contact',
value: 'contact',
},
{
name: 'Contact List',
value: 'contactList',
},
{
name: 'Deal',
value: 'deal',
},
{
name: 'Engagement',
value: 'engagement',
},
// {
// name: 'Form',
// value: 'form',
// },
{
name: 'Ticket',
value: 'ticket',
},
],
default: 'contact',
},
// CONTACT
...ContactDescription_1.contactOperations,
...ContactDescription_1.contactFields,
// CONTACT LIST
...ContactListDescription_1.contactListOperations,
...ContactListDescription_1.contactListFields,
// COMPANY
...CompanyDescription_1.companyOperations,
...CompanyDescription_1.companyFields,
// DEAL
...DealDescription_1.dealOperations,
...DealDescription_1.dealFields,
// ENGAGEMENT
...EngagementDescription_1.engagementOperations,
...EngagementDescription_1.engagementFields,
//! FORM Deprecated
//...formOperations,
//...formFields,
// TICKET
...TicketDescription_1.ticketOperations,
...TicketDescription_1.ticketFields,
],
};
}
methods = {
credentialTest: {
async hubspotApiTest(credential) {
try {
await GenericFunctions_1.validateCredentials.call(this, credential.data);
}
catch (error) {
const err = error;
if (err.statusCode === 401) {
return {
status: 'Error',
message: 'Invalid credentials',
};
}
}
return {
status: 'OK',
message: 'Authentication successful',
};
},
},
loadOptions: {
/* -------------------------------------------------------------------------- */
/* CONTACT */
/* -------------------------------------------------------------------------- */
// Get all the contact lead statuses to display them to user so that they can
// select them easily
async getContactLeadStatuses() {
const returnData = [];
const endpoint = '/properties/v2/contacts/properties';
const properties = await GenericFunctions_1.hubspotApiRequest.call(this, 'GET', endpoint, {});
for (const property of properties) {
if (property.name === 'hs_lead_status') {
for (const option of property.options) {
const statusName = option.label;
const statusId = option.value;
returnData.push({
name: statusName,
value: statusId,
});
}
}
}
return returnData;
},
// Get all the contact legal basics to display them to user so that they can
// select them easily
async getContactLealBasics() {
const returnData = [];
const endpoint = '/properties/v2/contacts/properties';
const properties = await GenericFunctions_1.hubspotApiRequest.call(this, 'GET', endpoint, {});
for (const property of properties) {
if (property.name === 'hs_legal_basis') {
for (const option of property.options) {
const statusName = option.label;
const statusId = option.value;
returnData.push({
name: statusName,
value: statusId,
});
}
}
}
return returnData;
},
// Get all the contact lifecycle stages to display them to user so that they can
// select them easily
async getContactLifeCycleStages() {
const returnData = [];
const endpoint = '/properties/v2/contacts/properties';
const properties = await GenericFunctions_1.hubspotApiRequest.call(this, 'GET', endpoint, {});
for (const property of properties) {
if (property.name === 'lifecyclestage') {
for (const option of property.options) {
const stageName = option.label;
const stageId = option.value;
returnData.push({
name: stageName,
value: stageId,
});
}
}
}
return returnData;
},
// Get all the contact lifecycle stages to display them to user so that they can
// select them easily
async getContactOriginalSources() {
const returnData = [];
const endpoint = '/properties/v2/contacts/properties';
const properties = await GenericFunctions_1.hubspotApiRequest.call(this, 'GET', endpoint, {});
for (const property of properties) {
if (property.name === 'hs_analytics_source') {
for (const option of property.options) {
const sourceName = option.label;
const sourceId = option.value;
returnData.push({
name: sourceName,
value: sourceId,
});
}
}
}
return returnData;
},
// Get all the contact preffered languages to display them to user so that they can
// select them easily
async getContactPrefferedLanguages() {
const returnData = [];
const endpoint = '/properties/v2/contacts/properties';
const properties = await GenericFunctions_1.hubspotApiRequest.call(this, 'GET', endpoint, {});
for (const property of properties) {
if (property.name === 'hs_language') {
for (const option of property.options) {
const languageName = option.label;
const languageId = option.value;
returnData.push({
name: languageName,
value: languageId,
});
}
}
}
return returnData;
},
// Get all the contact preffered languages to display them to user so that they can
// select them easily
async getContactStatuses() {
const returnData = [];
const endpoint = '/properties/v2/contacts/properties';
const properties = await GenericFunctions_1.hubspotApiRequest.call(this, 'GET', endpoint, {});
for (const property of properties) {
if (property.name === 'hs_content_membership_status') {
for (const option of property.options) {
const languageName = option.label;
const languageId = option.value;
returnData.push({
name: languageName,
value: languageId,
});
}
}
}
return returnData;
},
// Get all the contact properties to display them to user so that they can
// select them easily
async getContactProperties() {
const returnData = [];
const endpoint = '/properties/v2/contacts/properties';
let properties = (await GenericFunctions_1.hubspotApiRequest.call(this, 'GET', endpoint, {}));
properties = properties.sort((a, b) => {
if (a.label < b.label)
return -1;
if (a.label > b.label)
return 1;
return 0;
});
for (const property of properties) {
const propertyName = property.label;
const propertyId = property.name;
returnData.push({
name: propertyName,
value: propertyId,
});
}
return returnData;
},
// Get all the contact properties to display them to user so that they can
// select them easily
async getContactPropertiesWithType() {
const returnData = [];
const endpoint = '/properties/v2/contacts/properties';
const properties = await GenericFunctions_1.hubspotApiRequest.call(this, 'GET', endpoint, {});
for (const property of properties) {
const propertyName = property.label;
const propertyId = property.name;
const propertyType = property.type;
returnData.push({
name: propertyName,
// Hacky way to get the property type need to be parsed to be use in the api
value: `${propertyId}|${propertyType}`,
});
}
return returnData;
},
// Get all the contact properties to display them to user so that they can
// select them easily
async getContactCustomProperties() {
const returnData = [];
const endpoint = '/properties/v2/contacts/properties';
const properties = await GenericFunctions_1.hubspotApiRequest.call(this, 'GET', endpoint, {});
for (const property of properties) {
if (property.hubspotDefined === null) {
const propertyName = property.label;
const propertyId = property.name;
returnData.push({
name: propertyName,
value: propertyId,
});
}
}
return returnData;
},
// Get all the contact number of employees options to display them to user so that they can
// select them easily
async getContactNumberOfEmployees() {
const returnData = [];
const endpoint = '/properties/v2/contacts/properties';
const properties = await GenericFunctions_1.hubspotApiRequest.call(this, 'GET', endpoint, {});
for (const property of properties) {
if (property.name === 'numemployees') {
for (const option of property.options) {
const optionName = option.label;
const optionId = option.value;
returnData.push({
name: optionName,
value: optionId,
});
}
}
}
return returnData;
},
/* -------------------------------------------------------------------------- */
/* COMPANY */
/* -------------------------------------------------------------------------- */
// Get all the company industries to display them to user so that they can
// select them easily
async getCompanyIndustries() {
const returnData = [];
const endpoint = '/properties/v2/companies/properties';
const properties = await GenericFunctions_1.hubspotApiRequest.call(this, 'GET', endpoint, {});
for (const property of properties) {
if (property.name === 'industry') {
for (const option of property.options) {
const industryName = option.label;
const industryId = option.value;
returnData.push({
name: industryName,
value: industryId,
});
}
}
}
return returnData;
},
// Get all the company lead statuses to display them to user so that they can
// select them easily
async getCompanyleadStatuses() {
const returnData = [];
const endpoint = '/properties/v2/companies/properties';
const properties = await GenericFunctions_1.hubspotApiRequest.call(this, 'GET', endpoint, {});
for (const property of properties) {
if (property.name === 'hs_lead_status') {
for (const option of property.options) {
const statusName = option.label;
const statusId = option.value;
returnData.push({
name: statusName,
value: statusId,
});
}
}
}
return returnData;
},
// Get all the company lifecycle stages to display them to user so that they can
// select them easily
async getCompanylifecycleStages() {
const returnData = [];
const endpoint = '/properties/v2/companies/properties';
const properties = await GenericFunctions_1.hubspotApiRequest.call(this, 'GET', endpoint, {});
for (const property of properties) {
if (property.name === 'lifecyclestage') {
for (const option of property.options) {
const stageName = option.label;
const stageId = option.value;
returnData.push({
name: stageName,
value: stageId,
});
}
}
}
return returnData;
},
// Get all the company types stages to display them to user so that they can
// select them easily
async getCompanyTypes() {
const returnData = [];
const endpoint = '/properties/v2/companies/properties';
const properties = await GenericFunctions_1.hubspotApiRequest.call(this, 'GET', endpoint, {});
for (const property of properties) {
if (property.name === 'type') {
for (const option of property.options) {
const typeName = option.label;
const typeId = option.value;
returnData.push({
name: typeName,
value: typeId,
});
}
}
}
return returnData;
},
// Get all the company types stages to display them to user so that they can
// select them easily
async getCompanyTargetAccounts() {
const returnData = [];
const endpoint = '/properties/v2/companies/properties';
const properties = await GenericFunctions_1.hubspotApiRequest.call(this, 'GET', endpoint, {});
for (const property of properties) {
if (property.name === 'hs_target_account') {
for (const option of property.options) {
const targetName = option.label;
const targetId = option.value;
returnData.push({
name: targetName,
value: targetId,
});
}
}
}
return returnData;
},
// Get all the company source types stages to display them to user so that they can
// select them easily
async getCompanySourceTypes() {
const returnData = [];
const endpoint = '/properties/v2/companies/properties';
const properties = await GenericFunctions_1.hubspotApiRequest.call(this, 'GET', endpoint, {});
for (const property of properties) {
if (property.name === 'hs_analytics_source') {
for (const option of property.options) {
const typeName = option.label;
const typeId = option.value;
returnData.push({
name: typeName,
value: typeId,
});
}
}
}
return returnData;
},
// Get all the company web technologies stages to display them to user so that they can
// select them easily
async getCompanyWebTechnologies() {
const returnData = [];
const endpoint = '/properties/v2/companies/properties';
const properties = await GenericFunctions_1.hubspotApiRequest.call(this, 'GET', endpoint, {});
for (const property of properties) {
if (property.name === 'web_technologies') {
for (const option of property.options) {
const technologyName = option.label;
const technologyId = option.value;
returnData.push({
name: technologyName,
value: technologyId,
});
}
}
}
return returnData;
},
// Get all the company properties to display them to user so that they can
// select them easily
async getCompanyProperties() {
const returnData = [];
const endpoint = '/properties/v2/companies/properties';
const properties = await GenericFunctions_1.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;
},
// Get all the company custom properties to display them to user so that they can
// select them easily
async getCompanyCustomProperties() {
const returnData = [];
const endpoint = '/properties/v2/companies/properties';
const properties = await GenericFunctions_1.hubspotApiRequest.call(this, 'GET', endpoint, {});
for (const property of properties) {
if (property.hubspotDefined === null) {
const propertyName = property.label;
const propertyId = property.name;
returnData.push({
name: propertyName,
value: propertyId,
});
}
}
return returnData;
},
/* -------------------------------------------------------------------------- */
/* DEAL */
/* -------------------------------------------------------------------------- */
// Get all the groups to display them to user so that they can
// select them easily
async getDealStages() {
const returnData = [];
const endpoint = '/crm-pipelines/v1/pipelines/deals';
let stages = await GenericFunctions_1.hubspotApiRequest.call(this, 'GET', endpoint, {});
stages = stages.results[0].stages;
for (const stage of stages) {
const stageName = stage.label;
const stageId = stage.stageId;
returnData.push({
name: stageName,
value: stageId,
});
}
return returnData;
},
// Get all the deal types to display them to user so that they can
// select them easily
async getDealTypes() {
const returnData = [];
const endpoint = '/properties/v1/deals/properties/named/dealtype';
const dealTypes = await GenericFunctions_1.hubspotApiRequest.call(this, 'GET', endpoint);
for (const dealType of dealTypes.options) {
const dealTypeName = dealType.label;
const dealTypeId = dealType.value;
returnData.push({
name: dealTypeName,
value: dealTypeId,
});
}
return returnData;
},
// Get all the deal properties to display them to user so that they can
// select them easily
async getDealCustomProperties() {
const returnData = [];
const endpoint = '/properties/v2/deals/properties';
const properties = await GenericFunctions_1.hubspotApiRequest.call(this, 'GET', endpoint, {});
for (const property of properties) {
if (property.hubspotDefined === null) {
const propertyName = property.label;
const propertyId = property.name;
returnData.push({
name: propertyName,
value: propertyId,
});
}
}
return returnData;
},
// Get all the deal properties to display them to user so that they can
// select them easily
async getDealProperties() {
const returnData = [];
const endpoint = '/properties/v2/deals/properties';
let properties = (await GenericFunctions_1.hubspotApiRequest.call(this, 'GET', endpoint, {}));
properties = properties.sort((a, b) => {
if (a.label < b.label)
return -1;
if (a.label > b.label)
return 1;
return 0;
});
for (const property of properties) {
const propertyName = property.label;
const propertyId = property.name;
returnData.push({
name: propertyName,
value: propertyId,
});
}
return returnData;
},
// Get all the deal properties to display them to user so that they can
// select them easily
async getDealPropertiesWithType() {
const returnData = [];
const endpoint = '/properties/v2/deals/properties';
const properties = await GenericFunctions_1.hubspotApiRequest.call(this, 'GET', endpoint, {});
for (const property of properties) {
const propertyName = property.label;
const propertyId = property.name;
const propertyType = property.type;
returnData.push({
name: propertyName,
// Hacky way to get the property type need to be parsed to be use in the api
// this is no longer working, properties does not returned in the response
value: `${propertyId}|${propertyType}`,
});
}
return returnData;
},
// Get all the deal pipelines to display them to user so that they can
// select them easily
async getDealPipelines() {
const returnData = [];
const endpoint = '/crm/v3/pipelines/deals';
const data = await GenericFunctions_1.hubspotApiRequest.call(this, 'GET', endpoint, {});
for (const pipeline of data.results) {
returnData.push({
name: pipeline.label,
value: pipeline.id,
});
}
return returnData;
},
/* -------------------------------------------------------------------------- */
/* FORM */
/* -------------------------------------------------------------------------- */
// Get all the forms to display them to user so that they can
// select them easily
async getForms() {
const returnData = [];
const endpoint = '/forms/v2/forms';
const forms = await GenericFunctions_1.hubspotApiRequest.call(this, 'GET', endpoint, {}, { formTypes: 'ALL' });
for (const form of forms) {
const formName = form.name;
const formId = form.guid;
returnData.push({
name: formName,
value: formId,
});
}
return returnData;
},
// Get all the subscription types to display them to user so that they can
// select them easily
async getSubscriptionTypes() {
const returnData = [];
const endpoint = '/email/public/v1/subscriptions';
const subscriptions = await GenericFunctions_1.hubspotApiRequestAllItems.call(this, 'subscriptionDefinitions', 'GET', endpoint, {});
for (const subscription of subscriptions) {
const subscriptionName = subscription.name;
const subscriptionId = subscription.id;
returnData.push({
name: subscriptionName,
value: subscriptionId,
});
}
return returnData;
},
/* -------------------------------------------------------------------------- */
/* TICKET */
/* -------------------------------------------------------------------------- */
// Get all the ticket categories to display them to user so that they can
// select them easily
async getTicketCategories() {
const returnData = [];
const endpoint = '/properties/v2/tickets/properties';
const properties = await GenericFunctions_1.hubspotApiRequest.call(this, 'GET', endpoint, {});
for (const property of properties) {
if (property.name === 'hs_ticket_category') {
for (const option of property.options) {
const categoryName = option.label;
const categoryId = option.value;
returnData.push({
name: categoryName,
value: categoryId,
});
}
}
}
return returnData.sort((a, b) => (a.name < b.name ? 0 : 1));
},
// Get all the ticket pipelines to display them to user so that they can
// select them easily
async getTicketPipelines() {
const returnData = [];
const endpoint = '/crm-pipelines/v1/pipelines/tickets';
const { results } = await GenericFunctions_1.hubspotApiRequest.call(this, 'GET', endpoint, {});
for (const pipeline of results) {
const pipelineName = pipeline.label;
const pipelineId = pipeline.pipelineId;
returnData.push({
name: pipelineName,
value: pipelineId,
});
}
return returnData;
},
// Get all the ticket resolutions to display them to user so that they can
// select them easily
async getTicketPriorities() {
const returnData = [];
const endpoint = '/properties/v2/tickets/properties';
const properties = await GenericFunctions_1.hubspotApiRequest.call(this, 'GET', endpoint, {});
for (const property of properties) {
if (property.name === 'hs_ticket_priority') {
for (const option of property.options) {
const priorityName = option.label;
const priorityId = option.value;
returnData.push({
name: priorityName,
value: priorityId,
});
}
}
}
return returnData;
},
// Get all the ticket properties to display them to user so that they can
// select them easily
async getTicketProperties() {
const returnData = [];
const endpoint = '/properties/v2/tickets/properties';
const properties = await GenericFunctions_1.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;
},
// Get all the ticket resolutions to display them to user so that they can
// select them easily
async getTicketResolutions() {
const returnData = [];
const endpoint = '/properties/v2/tickets/properties';
const properties = await GenericFunctions_1.hubspotApiRequest.call(this, 'GET', endpoint, {});
for (const property of properties) {
if (property.name === 'hs_resolution') {
for (const option of property.options) {
const resolutionName = option.label;
const resolutionId = option.value;
returnData.push({
name: resolutionName,
value: resolutionId,
});
}
}
}
return returnData.sort((a, b) => (a.name < b.name ? 0 : 1));
},
// Get all the ticket sources to display them to user so that they can
// select them easily
async getTicketSources() {
const returnData = [];
const endpoint = '/properties/v2/tickets/properties';
const properties = await GenericFunctions_1.hubspotApiRequest.call(this, 'GET', endpoint, {});
for (const property of properties) {
if (property.name === 'source_type') {
for (const option of property.options) {
const sourceName = option.label;
const sourceId = option.value;
returnData.push({
name: sourceName,
value: sourceId,
});
}
}
}
return returnData.sort((a, b) => (a.name < b.name ? 0 : 1));
},
// Get all the ticket stages to display them to user so that they can
// select them easily
async getTicketStages() {
let currentPipelineId = this.getCurrentNodeParameter('pipelineId');
if (currentPipelineId === undefined) {
currentPipelineId = this.getNodeParameter('updateFields.pipelineId', '');
}
const returnData = [];
const endpoint = '/crm-pipelines/v1/pipelines/tickets';
const { results } = await GenericFunctions_1.hubspotApiRequest.call(this, 'GET', endpoint, {});
for (const pipeline of results) {
if (currentPipelineId === pipeline.pipelineId) {
for (const stage of pipeline.stages) {
const stageName = stage.label;
const stageId = stage.stageId;
returnData.push({
name: stageName,
value: stageId,
});
}
}
}
return returnData;
},
/* -------------------------------------------------------------------------- */
/* COMMON */
/* -------------------------------------------------------------------------- */
// Get all the owners to display them to user so that they can
// select them easily
async getOwners() {
const returnData = [];
const endpoint = '/crm/v3/owners';
const { results } = await GenericFunctions_1.hubspotApiRequest.call(this, 'GET', endpoint);
for (const owner of results) {
const ownerName = owner.email;
const ownerId = isNaN(parseInt(owner.id)) ? owner.id : parseInt(owner.id);
returnData.push({
name: ownerName,
value: ownerId,
});
}
return returnData;
},
// Get all the companies to display them to user so that they can
// select them easily
async getCompanies() {
const returnData = [];
const qs = {
properties: ['name'],
};
const endpoint = '/companies/v2/companies/paged';
const companies = await GenericFunctions_1.hubspotApiRequestAllItems.call(this, 'companies', 'GET', endpoint, {}, qs);
for (const company of companies) {
const companyName = company.properties.name
? company.properties.name.value
: company.companyId;
const companyId = company.companyId;
returnData.push({
name: companyName,
value: companyId,
});
}
return returnData.sort((a, b) => (a.name < b.name ? 0 : 1));
},
// Get all the companies to display them to user so that they can
// select them easily
async getContacts() {
const returnData = [];
const endpoint = '/contacts/v1/lists/all/contacts/all';
const contacts = await GenericFunctions_1.hubspotApiRequestAllItems.call(this, 'contacts', 'GET', endpoint);
for (const contact of contacts) {
const firstName = contact.properties?.firstname?.value || '';
const lastName = contact.properties?.lastname?.value || '';
const contactName = `${firstName} ${lastName}`;
const contactId = contact.vid;
returnData.push({
name: contactName,
value: contactId,
description: `Contact VID: ${contactId}`,
});
}
return returnData.sort((a, b) => (a.name < b.name ? 0 : 1));
},
},
listSearch: {
async searchCompanies() {
const qs = {
properties: ['name'],
};
const endpoint = '/companies/v2/companies/paged';
const searchResults = await GenericFunctions_1.hubspotApiRequestAllItems.call(this, 'companies', 'GET', endpoint, {}, qs);
return {
// tslint:disable-next-line: no-any
results: searchResults.map((b) => ({
name: b.properties?.name?.value || b.companyId,
value: b.companyId,
})),
};
},
async searchContacts(filter) {
const endpoint = '/contacts/v1/lists/all/contacts/all';
const qs = {
property: ['email'],
};
const contacts = (await GenericFunctions_1.hubspotApiRequestAllItems.call(this, 'contacts', 'GET', endpoint, {}, qs));
const results = contacts
.map((c) => ({
name: c.properties.email.value || c.vid,
value: c.vid,
}))
.filter((c) => !filter ||
c.name.toLowerCase().includes(filter.toLowerCase()) ||
c.value?.toString() === filter)
.sort((a, b) => {
return a.name.toLowerCase() < b.name.toLowerCase() ? -1 : 1;
});
return { results };
},
async searchDeals(filter) {
const endpoint = '/deals/v1/deal/paged';
const qs = {
properties: ['dealname'],
};
const deals = (await GenericFunctions_1.hubspotApiRequestAllItems.call(this, 'deals', 'GET', endpoint, {}, qs));
const results = deals
.map((c) => ({
name: c.properties?.dealname?.value || c.dealId,
value: c.dealId,
}))
.filter((c) => !filter ||
c.name.toString().toLowerCase().includes(filter.toString().toLowerCase()) ||
c.value?.toString() === filter)
.sort((a, b) => {
return a.name.toString().toLowerCase() < b.name.toString().toLowerCase() ? -1 : 1;
});
return {
results,
};
},
async searchEngagements() {
const endpoint = '/engagements/v1/engagements/paged';
const qs = {
properties: ['name'],
};
const engagements = await GenericFunctions_1.hubspotApiRequest.call(this, 'GET', endpoint, {}, qs);
return {
// tslint:disable-next-line: no-any
results: engagements.results.map((b) => ({
name: b.properties?.name?.value || b.engagement?.type
? `${b.engagement?.type}: ${b.engagement.id}`
: b.engagement.id,
value: b.engagement.id,
})),
};
},
async searchTickets() {
const endpoint = '/crm-objects/v1/objects/tickets/paged';
const qs = {
properties: ['ticket_name'],
};
const tickets = await GenericFunctions_1.hubspotApiRequestAllItems.call(this, 'objects', 'GET', endpoint, {}, qs);
return {
// tslint:disable-next-line: no-any
results: tickets.map((b) => ({
name: b.objectId,
value: b.objectId,
})),
};
},
async searchOwners() {
const endpoint = '/crm/v3/owners';
const { results } = await GenericFunctions_1.hubspotApiRequest.call(this, 'GET', endpoint, {});
return {
// tslint:disable-next-line: no-any
results: results.map((b) => ({
name: b.email,
value: isNaN(parseInt(b.id)) ? b.id : parseInt(b.id),
})),
};
},
},
};
async execute() {
const items = this.getInputData();
const returnData = [];
const length = items.length;
let responseData;
const qs = {};
const resource = this.getNodeParameter('resource', 0);
const operation = this.getNodeParameter('operation', 0);
const version = this.getNode().typeVersion;
//https://legacydocs.hubspot.com/docs/methods/lists/contact-lists-overview
if (resource === 'contactList') {
try {
//https://legacydocs.hubspot.com/docs/methods/lists/add_contact_to_list
if (operation === 'add') {
const listId = this.getNodeParameter('listId', 0);
const by = this.getNodeParameter('by', 0);
const body = { emails: [], vids: [] };
for (let i = 0; i < length; i++) {
if (by === 'id') {
const id = this.getNodeParameter('id', i);
body.vids.push(parseInt(id, 10));
}
else {
const email = this.getNodeParameter('email', i);
body.emails.push(email);
}
}
responseData = await GenericFunctions_1.hubspotApiRequest.call(this, 'POST', `/contacts/v1/lists/${listId}/add`, body);
}
//https://legacydocs.hubspot.com/docs/methods/lists/remove_contact_from_list
if (operation === 'remove') {
const listId = this.getNodeParameter('listId', 0);
const body = { vids: [] };
for (let i = 0; i < length; i++) {
const id = this.getNodeParameter('id', i);
body.vids.push(parseInt(id, 10));
}
responseData = await GenericFunctions_1.hubspotApiRequest.call(this, 'POST', `/contacts/v1/lists/${listId}/remove`, body);
}
const itemData = (0, utilities_1.generatePairedItemData)(items.length);
const executionData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray(responseData), { itemData });
returnData.push(...executionData);
}
catch (error) {
if (this.continueOnFail()) {
returnData.push({ json: { error: error.message } });
}
else {
throw error;
}
}
}
else {
for (let i = 0; i < length; i++) {
try {
//https://developers.hubspot.com/docs/methods/contacts/create_or_update
if (resource === 'contact') {
//https://developers.hubspot.com/docs/methods/companies/create_company
if (operation === 'upsert') {
const email = this.getNodePar