2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IExecuteFunctions,
|
2019-11-15 12:10:51 -08:00
|
|
|
IDataObject,
|
|
|
|
ILoadOptionsFunctions,
|
2020-10-01 05:01:39 -07:00
|
|
|
INodeExecutionData,
|
2019-11-15 12:10:51 -08:00
|
|
|
INodePropertyOptions,
|
2020-10-01 05:01:39 -07:00
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
2023-02-27 19:39:43 -08:00
|
|
|
JsonObject,
|
2019-11-15 12:10:51 -08:00
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeApiError, NodeOperationError } from 'n8n-workflow';
|
2022-12-02 12:54:28 -08:00
|
|
|
import { leadFields, leadOperations } from './LeadDescription';
|
2022-08-17 08:50:24 -07:00
|
|
|
import { intercomApiRequest, intercomApiRequestAllItems, validateJSON } from './GenericFunctions';
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { IAvatar, ILead, ILeadCompany } from './LeadInterface';
|
2022-12-02 12:54:28 -08:00
|
|
|
import { userFields, userOperations } from './UserDescription';
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { IUser, IUserCompany } from './UserInterface';
|
2022-08-17 08:50:24 -07:00
|
|
|
import { companyFields, companyOperations } from './CompanyDescription';
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { ICompany } from './CompanyInteface';
|
2019-11-15 12:10:51 -08:00
|
|
|
|
|
|
|
export class Intercom implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Intercom',
|
|
|
|
name: 'intercom',
|
2024-06-24 03:13:18 -07:00
|
|
|
|
2024-06-06 04:34:30 -07:00
|
|
|
icon: 'file:intercom.svg',
|
2019-11-15 12:10:51 -08:00
|
|
|
group: ['output'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
2021-07-03 05:40:16 -07:00
|
|
|
description: 'Consume Intercom API',
|
2019-11-15 12:10:51 -08:00
|
|
|
defaults: {
|
|
|
|
name: 'Intercom',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'intercomApi',
|
|
|
|
required: true,
|
2020-10-22 06:46:03 -07:00
|
|
|
},
|
2019-11-15 12:10:51 -08:00
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2019-11-15 12:10:51 -08:00
|
|
|
options: [
|
2019-11-21 09:39:04 -08:00
|
|
|
{
|
2019-11-22 14:04:06 -08:00
|
|
|
name: 'Company',
|
|
|
|
value: 'company',
|
2022-08-17 08:50:24 -07:00
|
|
|
description:
|
|
|
|
'Companies allow you to represent commercial organizations using your product',
|
2019-11-21 09:39:04 -08:00
|
|
|
},
|
2019-11-15 12:10:51 -08:00
|
|
|
{
|
|
|
|
name: 'Lead',
|
|
|
|
value: 'lead',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'Leads are useful for representing logged-out users of your application',
|
2019-11-21 09:39:04 -08:00
|
|
|
},
|
|
|
|
{
|
2019-11-22 14:04:06 -08:00
|
|
|
name: 'User',
|
|
|
|
value: 'user',
|
|
|
|
description: 'The Users resource is the primary way of interacting with Intercom',
|
2019-11-15 12:10:51 -08:00
|
|
|
},
|
|
|
|
],
|
2019-11-22 14:04:06 -08:00
|
|
|
default: 'user',
|
2019-11-15 12:10:51 -08:00
|
|
|
},
|
2022-12-02 12:54:28 -08:00
|
|
|
...leadOperations,
|
|
|
|
...userOperations,
|
2019-11-22 14:04:06 -08:00
|
|
|
...companyOperations,
|
2019-11-21 09:39:04 -08:00
|
|
|
...userFields,
|
2019-11-16 14:16:11 -08:00
|
|
|
...leadFields,
|
2019-11-21 09:39:04 -08:00
|
|
|
...companyFields,
|
2019-11-15 12:10:51 -08:00
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2019-11-16 14:16:11 -08:00
|
|
|
methods = {
|
|
|
|
loadOptions: {
|
2023-04-19 07:00:49 -07:00
|
|
|
// Get all the available companies to display them to user so that they can
|
2019-11-16 14:16:11 -08:00
|
|
|
// select them easily
|
|
|
|
async getCompanies(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
2022-12-02 12:54:28 -08:00
|
|
|
let response;
|
2019-11-16 14:16:11 -08:00
|
|
|
try {
|
|
|
|
response = await intercomApiRequest.call(this, '/companies', 'GET');
|
2021-04-16 09:33:36 -07:00
|
|
|
} catch (error) {
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
2019-11-16 14:16:11 -08:00
|
|
|
}
|
2022-12-02 12:54:28 -08:00
|
|
|
const companies = response.companies;
|
2019-11-16 14:16:11 -08:00
|
|
|
for (const company of companies) {
|
|
|
|
const companyName = company.name;
|
2019-11-16 15:47:28 -08:00
|
|
|
const companyId = company.company_id;
|
2019-11-16 14:16:11 -08:00
|
|
|
returnData.push({
|
|
|
|
name: companyName,
|
|
|
|
value: companyId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
2019-11-21 09:39:04 -08:00
|
|
|
},
|
2019-11-16 14:16:11 -08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-11-18 13:41:00 -08:00
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
2022-08-30 08:55:33 -07:00
|
|
|
const returnData: INodeExecutionData[] = [];
|
2022-04-22 09:29:51 -07:00
|
|
|
const length = items.length;
|
2019-11-22 14:04:06 -08:00
|
|
|
let qs: IDataObject;
|
2019-11-18 13:41:00 -08:00
|
|
|
let responseData;
|
|
|
|
for (let i = 0; i < length; i++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
qs = {};
|
2022-12-02 03:53:59 -08:00
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://developers.intercom.com/intercom-api-reference/reference#leads
|
|
|
|
if (resource === 'lead') {
|
|
|
|
if (operation === 'create' || operation === 'update') {
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2022-11-18 05:31:38 -08:00
|
|
|
const jsonActive = this.getNodeParameter('jsonParameters', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
const body: ILead = {};
|
|
|
|
if (operation === 'create') {
|
|
|
|
body.email = this.getNodeParameter('email', i) as string;
|
2019-11-18 13:41:00 -08:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.email) {
|
|
|
|
body.email = additionalFields.email as string;
|
2019-11-16 15:47:28 -08:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.phone) {
|
|
|
|
body.phone = additionalFields.phone as string;
|
2019-11-20 06:45:58 -08:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.name) {
|
|
|
|
body.name = additionalFields.name as string;
|
2019-11-20 06:45:58 -08:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.unsubscribedFromEmails) {
|
|
|
|
body.unsubscribed_from_emails = additionalFields.unsubscribedFromEmails as boolean;
|
2019-11-22 14:04:06 -08:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.updateLastRequestAt) {
|
|
|
|
body.update_last_request_at = additionalFields.updateLastRequestAt as boolean;
|
2019-11-22 14:04:06 -08:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.utmSource) {
|
|
|
|
body.utm_source = additionalFields.utmSource as string;
|
2019-11-21 09:39:04 -08:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.utmMedium) {
|
|
|
|
body.utm_medium = additionalFields.utmMedium as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.utmCampaign) {
|
|
|
|
body.utm_campaign = additionalFields.utmCampaign as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.utmTerm) {
|
|
|
|
body.utm_term = additionalFields.utmTerm as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.utmContent) {
|
|
|
|
body.utm_content = additionalFields.utmContent as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.avatar) {
|
|
|
|
const avatar: IAvatar = {
|
|
|
|
type: 'avatar',
|
|
|
|
image_url: additionalFields.avatar as string,
|
|
|
|
};
|
|
|
|
body.avatar = avatar;
|
|
|
|
}
|
|
|
|
if (additionalFields.companies) {
|
|
|
|
const companies: ILeadCompany[] = [];
|
|
|
|
// @ts-ignore
|
2022-08-17 08:50:24 -07:00
|
|
|
additionalFields.companies.forEach((o) => {
|
2021-07-19 23:58:54 -07:00
|
|
|
const company: ILeadCompany = {};
|
|
|
|
company.company_id = o;
|
|
|
|
companies.push(company);
|
|
|
|
});
|
|
|
|
body.companies = companies;
|
|
|
|
}
|
|
|
|
if (!jsonActive) {
|
2022-08-17 08:50:24 -07:00
|
|
|
const customAttributesValues = (
|
|
|
|
this.getNodeParameter('customAttributesUi', i) as IDataObject
|
|
|
|
).customAttributesValues as IDataObject[];
|
2021-07-19 23:58:54 -07:00
|
|
|
if (customAttributesValues) {
|
|
|
|
const customAttributes = {};
|
2022-12-02 12:54:28 -08:00
|
|
|
for (let index = 0; index < customAttributesValues.length; index++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
// @ts-ignore
|
2022-12-02 12:54:28 -08:00
|
|
|
customAttributes[customAttributesValues[index].name] =
|
|
|
|
customAttributesValues[index].value;
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
body.custom_attributes = customAttributes;
|
|
|
|
}
|
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
const customAttributesJson = validateJSON(
|
|
|
|
this.getNodeParameter('customAttributesJson', i) as string,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
if (customAttributesJson) {
|
|
|
|
body.custom_attributes = customAttributesJson;
|
|
|
|
}
|
2019-11-22 14:04:06 -08:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'update') {
|
|
|
|
const updateBy = this.getNodeParameter('updateBy', 0) as string;
|
|
|
|
const value = this.getNodeParameter('value', i) as string;
|
|
|
|
if (updateBy === 'userId') {
|
|
|
|
body.user_id = value;
|
|
|
|
}
|
|
|
|
if (updateBy === 'id') {
|
|
|
|
body.id = value;
|
2019-11-21 09:39:04 -08:00
|
|
|
}
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
|
|
|
|
try {
|
|
|
|
responseData = await intercomApiRequest.call(this, '/contacts', 'POST', body);
|
|
|
|
} catch (error) {
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
2019-11-21 09:39:04 -08:00
|
|
|
}
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'get') {
|
|
|
|
const selectBy = this.getNodeParameter('selectBy', 0) as string;
|
2019-11-22 14:04:06 -08:00
|
|
|
const value = this.getNodeParameter('value', i) as string;
|
2021-07-19 23:58:54 -07:00
|
|
|
if (selectBy === 'email') {
|
|
|
|
qs.email = value;
|
2019-11-21 09:39:04 -08:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (selectBy === 'userId') {
|
|
|
|
qs.user_id = value;
|
2019-11-21 09:39:04 -08:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (selectBy === 'phone') {
|
|
|
|
qs.phone = value;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
if (selectBy === 'id') {
|
|
|
|
responseData = await intercomApiRequest.call(this, `/contacts/${value}`, 'GET');
|
|
|
|
} else {
|
|
|
|
responseData = await intercomApiRequest.call(this, '/contacts', 'GET', {}, qs);
|
|
|
|
responseData = responseData.contacts;
|
|
|
|
}
|
|
|
|
} catch (error) {
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
2019-11-21 09:39:04 -08:00
|
|
|
}
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'getAll') {
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2022-11-18 07:29:44 -08:00
|
|
|
const filters = this.getNodeParameter('filters', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
Object.assign(qs, filters);
|
2019-11-22 14:04:06 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
2022-12-02 12:54:28 -08:00
|
|
|
if (returnAll) {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await intercomApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'contacts',
|
|
|
|
'/contacts',
|
|
|
|
'GET',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
2022-11-18 06:26:22 -08:00
|
|
|
qs.per_page = this.getNodeParameter('limit', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await intercomApiRequest.call(this, '/contacts', 'GET', {}, qs);
|
|
|
|
responseData = responseData.contacts;
|
|
|
|
}
|
|
|
|
} catch (error) {
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2019-11-21 09:39:04 -08:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'delete') {
|
|
|
|
const deleteBy = this.getNodeParameter('deleteBy', 0) as string;
|
|
|
|
const value = this.getNodeParameter('value', i) as string;
|
|
|
|
try {
|
|
|
|
if (deleteBy === 'id') {
|
|
|
|
responseData = await intercomApiRequest.call(this, `/contacts/${value}`, 'DELETE');
|
|
|
|
} else {
|
|
|
|
qs.user_id = value;
|
|
|
|
responseData = await intercomApiRequest.call(this, '/contacts', 'DELETE', {}, qs);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
2019-11-22 14:04:06 -08:00
|
|
|
}
|
2019-11-21 09:39:04 -08:00
|
|
|
}
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://developers.intercom.com/intercom-api-reference/reference#users
|
|
|
|
if (resource === 'user') {
|
|
|
|
if (operation === 'create' || operation === 'update') {
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2022-11-18 05:31:38 -08:00
|
|
|
const jsonActive = this.getNodeParameter('jsonParameters', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
const body: IUser = {};
|
|
|
|
|
|
|
|
if (operation === 'create') {
|
|
|
|
const identifierType = this.getNodeParameter('identifierType', i) as string;
|
|
|
|
if (identifierType === 'email') {
|
|
|
|
body.email = this.getNodeParameter('idValue', i) as string;
|
|
|
|
} else if (identifierType === 'userId') {
|
|
|
|
body.user_id = this.getNodeParameter('idValue', i) as string;
|
|
|
|
}
|
|
|
|
}
|
2019-11-22 14:04:06 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.email) {
|
|
|
|
body.email = additionalFields.email as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.userId) {
|
|
|
|
body.user_id = additionalFields.userId as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.phone) {
|
|
|
|
body.phone = additionalFields.phone as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.name) {
|
|
|
|
body.name = additionalFields.name as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.unsubscribedFromEmails) {
|
|
|
|
body.unsubscribed_from_emails = additionalFields.unsubscribedFromEmails as boolean;
|
|
|
|
}
|
|
|
|
if (additionalFields.updateLastRequestAt) {
|
|
|
|
body.update_last_request_at = additionalFields.updateLastRequestAt as boolean;
|
|
|
|
}
|
|
|
|
if (additionalFields.sessionCount) {
|
|
|
|
body.session_count = additionalFields.sessionCount as number;
|
|
|
|
}
|
|
|
|
if (additionalFields.avatar) {
|
|
|
|
const avatar: IAvatar = {
|
|
|
|
type: 'avatar',
|
|
|
|
image_url: additionalFields.avatar as string,
|
|
|
|
};
|
|
|
|
body.avatar = avatar;
|
|
|
|
}
|
|
|
|
if (additionalFields.utmSource) {
|
|
|
|
body.utm_source = additionalFields.utmSource as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.utmMedium) {
|
|
|
|
body.utm_medium = additionalFields.utmMedium as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.utmCampaign) {
|
|
|
|
body.utm_campaign = additionalFields.utmCampaign as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.utmTerm) {
|
|
|
|
body.utm_term = additionalFields.utmTerm as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.utmContent) {
|
|
|
|
body.utm_content = additionalFields.utmContent as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.companies) {
|
|
|
|
const companies: IUserCompany[] = [];
|
|
|
|
// @ts-ignore
|
2022-08-17 08:50:24 -07:00
|
|
|
additionalFields.companies.forEach((o) => {
|
2021-07-19 23:58:54 -07:00
|
|
|
const company: IUserCompany = {};
|
|
|
|
company.company_id = o;
|
|
|
|
companies.push(company);
|
|
|
|
});
|
|
|
|
body.companies = companies;
|
|
|
|
}
|
|
|
|
if (additionalFields.sessionCount) {
|
|
|
|
body.session_count = additionalFields.sessionCount as number;
|
|
|
|
}
|
|
|
|
if (!jsonActive) {
|
2022-08-17 08:50:24 -07:00
|
|
|
const customAttributesValues = (
|
|
|
|
this.getNodeParameter('customAttributesUi', i) as IDataObject
|
|
|
|
).customAttributesValues as IDataObject[];
|
2021-07-19 23:58:54 -07:00
|
|
|
if (customAttributesValues) {
|
|
|
|
const customAttributes = {};
|
2022-12-02 12:54:28 -08:00
|
|
|
for (let index = 0; index < customAttributesValues.length; index++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
// @ts-ignore
|
2022-12-02 12:54:28 -08:00
|
|
|
customAttributes[customAttributesValues[index].name] =
|
|
|
|
customAttributesValues[index].value;
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
body.custom_attributes = customAttributes;
|
|
|
|
}
|
2019-11-22 14:04:06 -08:00
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
const customAttributesJson = validateJSON(
|
|
|
|
this.getNodeParameter('customAttributesJson', i) as string,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
if (customAttributesJson) {
|
|
|
|
body.custom_attributes = customAttributesJson;
|
|
|
|
}
|
2019-11-22 14:04:06 -08:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
|
|
|
|
if (operation === 'update') {
|
|
|
|
const updateBy = this.getNodeParameter('updateBy', 0) as string;
|
|
|
|
const value = this.getNodeParameter('value', i) as string;
|
|
|
|
if (updateBy === 'userId') {
|
|
|
|
body.user_id = value;
|
|
|
|
}
|
|
|
|
if (updateBy === 'id') {
|
|
|
|
body.id = value;
|
|
|
|
}
|
|
|
|
if (updateBy === 'email') {
|
|
|
|
body.email = value;
|
2019-11-21 09:39:04 -08:00
|
|
|
}
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
|
|
|
|
try {
|
|
|
|
responseData = await intercomApiRequest.call(this, '/users', 'POST', body, qs);
|
|
|
|
} catch (error) {
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
2019-11-21 09:39:04 -08:00
|
|
|
}
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'get') {
|
|
|
|
const selectBy = this.getNodeParameter('selectBy', 0) as string;
|
|
|
|
const value = this.getNodeParameter('value', i) as string;
|
|
|
|
if (selectBy === 'userId') {
|
|
|
|
qs.user_id = value;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
if (selectBy === 'id') {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await intercomApiRequest.call(
|
|
|
|
this,
|
|
|
|
`/users/${value}`,
|
|
|
|
'GET',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
|
|
|
responseData = await intercomApiRequest.call(this, '/users', 'GET', {}, qs);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2019-11-21 09:39:04 -08:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'getAll') {
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2022-11-18 07:29:44 -08:00
|
|
|
const filters = this.getNodeParameter('filters', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
Object.assign(qs, filters);
|
|
|
|
|
|
|
|
try {
|
2022-12-02 12:54:28 -08:00
|
|
|
if (returnAll) {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await intercomApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'users',
|
|
|
|
'/users',
|
|
|
|
'GET',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
2022-11-18 06:26:22 -08:00
|
|
|
qs.per_page = this.getNodeParameter('limit', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await intercomApiRequest.call(this, '/users', 'GET', {}, qs);
|
|
|
|
responseData = responseData.users;
|
|
|
|
}
|
|
|
|
} catch (error) {
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2019-11-21 09:39:04 -08:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'delete') {
|
|
|
|
const id = this.getNodeParameter('id', i) as string;
|
|
|
|
try {
|
|
|
|
responseData = await intercomApiRequest.call(this, `/users/${id}`, 'DELETE');
|
|
|
|
} catch (error) {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
`Intercom Error: ${JSON.stringify(error)}`,
|
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2019-11-22 14:04:06 -08:00
|
|
|
}
|
2019-11-21 09:39:04 -08:00
|
|
|
}
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://developers.intercom.com/intercom-api-reference/reference#companies
|
|
|
|
if (resource === 'company') {
|
|
|
|
if (operation === 'create' || operation === 'update') {
|
|
|
|
const id = this.getNodeParameter('companyId', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2022-11-18 05:31:38 -08:00
|
|
|
const jsonActive = this.getNodeParameter('jsonParameters', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
const body: ICompany = {
|
|
|
|
company_id: id,
|
|
|
|
};
|
|
|
|
if (additionalFields.monthlySpend) {
|
|
|
|
body.monthly_spend = additionalFields.monthlySpend as number;
|
|
|
|
}
|
|
|
|
if (additionalFields.name) {
|
|
|
|
body.name = additionalFields.name as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.plan) {
|
|
|
|
body.plan = additionalFields.plan as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.size) {
|
|
|
|
body.size = additionalFields.size as number;
|
|
|
|
}
|
|
|
|
if (additionalFields.website) {
|
|
|
|
body.website = additionalFields.website as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.industry) {
|
|
|
|
body.industry = additionalFields.industry as string;
|
|
|
|
}
|
|
|
|
if (!jsonActive) {
|
2022-08-17 08:50:24 -07:00
|
|
|
const customAttributesValues = (
|
|
|
|
this.getNodeParameter('customAttributesUi', i) as IDataObject
|
|
|
|
).customAttributesValues as IDataObject[];
|
2021-07-19 23:58:54 -07:00
|
|
|
if (customAttributesValues) {
|
|
|
|
const customAttributes = {};
|
2022-12-02 12:54:28 -08:00
|
|
|
for (let index = 0; index < customAttributesValues.length; index++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
// @ts-ignore
|
2022-12-02 12:54:28 -08:00
|
|
|
customAttributes[customAttributesValues[index].name] =
|
|
|
|
customAttributesValues[index].value;
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
body.custom_attributes = customAttributes;
|
|
|
|
}
|
2019-11-22 14:04:06 -08:00
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
const customAttributesJson = validateJSON(
|
|
|
|
this.getNodeParameter('customAttributesJson', i) as string,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
if (customAttributesJson) {
|
|
|
|
body.custom_attributes = customAttributesJson;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
responseData = await intercomApiRequest.call(this, '/companies', 'POST', body, qs);
|
|
|
|
} catch (error) {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
`Intercom Error: ${JSON.stringify(error)}`,
|
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2019-11-22 14:04:06 -08:00
|
|
|
}
|
2019-11-21 09:39:04 -08:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'get') {
|
|
|
|
const selectBy = this.getNodeParameter('selectBy', 0) as string;
|
|
|
|
const value = this.getNodeParameter('value', i) as string;
|
|
|
|
if (selectBy === 'companyId') {
|
|
|
|
qs.company_id = value;
|
|
|
|
}
|
|
|
|
if (selectBy === 'name') {
|
|
|
|
qs.name = value;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
if (selectBy === 'id') {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await intercomApiRequest.call(
|
|
|
|
this,
|
|
|
|
`/companies/${value}`,
|
|
|
|
'GET',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
|
|
|
responseData = await intercomApiRequest.call(this, '/companies', 'GET', {}, qs);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
`Intercom Error: ${JSON.stringify(error)}`,
|
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2019-11-21 09:39:04 -08:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'getAll') {
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2022-11-18 07:29:44 -08:00
|
|
|
const filters = this.getNodeParameter('filters', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
Object.assign(qs, filters);
|
2021-04-24 08:42:31 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
2022-12-02 12:54:28 -08:00
|
|
|
if (returnAll) {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await intercomApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'companies',
|
|
|
|
'/companies',
|
|
|
|
'GET',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-04-24 08:42:31 -07:00
|
|
|
} else {
|
2022-11-18 06:26:22 -08:00
|
|
|
qs.per_page = this.getNodeParameter('limit', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await intercomApiRequest.call(this, '/companies', 'GET', {}, qs);
|
|
|
|
responseData = responseData.companies;
|
2021-04-24 08:42:31 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
} catch (error) {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
`Intercom Error: ${JSON.stringify(error)}`,
|
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (operation === 'users') {
|
|
|
|
const listBy = this.getNodeParameter('listBy', 0) as string;
|
|
|
|
const value = this.getNodeParameter('value', i) as string;
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2021-04-24 08:42:31 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (listBy === 'companyId') {
|
|
|
|
qs.company_id = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (listBy === 'id') {
|
2022-12-02 12:54:28 -08:00
|
|
|
if (returnAll) {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await intercomApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'users',
|
|
|
|
`/companies/${value}/users`,
|
|
|
|
'GET',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
2022-11-18 06:26:22 -08:00
|
|
|
qs.per_page = this.getNodeParameter('limit', i);
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await intercomApiRequest.call(
|
|
|
|
this,
|
|
|
|
`/companies/${value}/users`,
|
|
|
|
'GET',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.users;
|
|
|
|
}
|
2021-04-24 08:42:31 -07:00
|
|
|
} else {
|
2021-07-19 23:58:54 -07:00
|
|
|
qs.type = 'users';
|
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
if (returnAll) {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await intercomApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'users',
|
|
|
|
'/companies',
|
|
|
|
'GET',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
2022-11-18 06:26:22 -08:00
|
|
|
qs.per_page = this.getNodeParameter('limit', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await intercomApiRequest.call(this, '/companies', 'GET', {}, qs);
|
|
|
|
responseData = responseData.users;
|
|
|
|
}
|
2021-04-24 08:42:31 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
} catch (error) {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
`Intercom Error: ${JSON.stringify(error)}`,
|
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2019-11-21 09:39:04 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
|
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2023-02-27 19:39:43 -08:00
|
|
|
this.helpers.returnJsonArray(responseData as IDataObject),
|
2022-08-30 08:55:33 -07:00
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
|
|
|
|
returnData.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
} catch (error) {
|
2024-06-19 22:45:00 -07:00
|
|
|
if (this.continueOnFail(error)) {
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionErrorData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray({ error: error.message }),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionErrorData);
|
2021-07-19 23:58:54 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
2019-11-16 15:47:28 -08:00
|
|
|
}
|
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
|
2023-09-05 03:59:02 -07:00
|
|
|
return [returnData];
|
2019-11-15 12:10:51 -08:00
|
|
|
}
|
|
|
|
}
|