From 7a1983783372f28813afee113349db4bee5319b0 Mon Sep 17 00:00:00 2001 From: Ricardo Espinoza Date: Sat, 16 Nov 2019 17:16:11 -0500 Subject: [PATCH] :lipstick: create lead UI done --- .../nodes/Intercom/GenericFunctions.ts | 45 +++++ .../nodes/Intercom/Intercom.node.ts | 35 ++++ .../nodes/Intercom/LeadDescription.ts | 172 ++++++++++++++++++ 3 files changed, 252 insertions(+) create mode 100644 packages/nodes-base/nodes/Intercom/GenericFunctions.ts create mode 100644 packages/nodes-base/nodes/Intercom/LeadDescription.ts diff --git a/packages/nodes-base/nodes/Intercom/GenericFunctions.ts b/packages/nodes-base/nodes/Intercom/GenericFunctions.ts new file mode 100644 index 0000000000..007aa700cb --- /dev/null +++ b/packages/nodes-base/nodes/Intercom/GenericFunctions.ts @@ -0,0 +1,45 @@ +import { OptionsWithUri } from 'request'; + +import { + IExecuteFunctions, + IHookFunctions, + ILoadOptionsFunctions, + IExecuteSingleFunctions +} from 'n8n-core'; + +export async function intercomApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, resource: string, method: string, body: any = {}, headers?: object): Promise { // tslint:disable-line:no-any + const credentials = this.getCredentials('intercomApi'); + + if (credentials === undefined) { + throw new Error('No credentials got returned!'); + } + + const headerWithAuthentication = Object.assign({}, headers, + { Authorization: `Bearer ${credentials.apiKey}`, Accept: 'application/json' }); + + const endpoint = 'api.intercom.io'; + + const options: OptionsWithUri = { + headers: headerWithAuthentication, + method, + uri: `https://${endpoint}${resource}`, + body, + json: true + }; + + console.log(options) + + + try { + return await this.helpers.request!(options); + } catch (error) { + console.error(error); + + const errorMessage = error.response.body.message || error.response.body.Message; + + if (errorMessage !== undefined) { + throw errorMessage; + } + throw error.response.body; + } +} diff --git a/packages/nodes-base/nodes/Intercom/Intercom.node.ts b/packages/nodes-base/nodes/Intercom/Intercom.node.ts index 55e61151b7..e746715fbb 100644 --- a/packages/nodes-base/nodes/Intercom/Intercom.node.ts +++ b/packages/nodes-base/nodes/Intercom/Intercom.node.ts @@ -9,6 +9,13 @@ import { ILoadOptionsFunctions, INodePropertyOptions, } from 'n8n-workflow'; +import { + leadOpeations, + leadFields, +} from './LeadDescription'; +import { + intercomApiRequest, +} from './GenericFunctions'; export class Intercom implements INodeType { @@ -47,9 +54,37 @@ export class Intercom implements INodeType { default: '', description: 'Resource to consume.', }, + ...leadOpeations, + ...leadFields, ], }; + methods = { + loadOptions: { + // Get all the available companies to display them to user so that he can + // select them easily + async getCompanies(this: ILoadOptionsFunctions): Promise { + const returnData: INodePropertyOptions[] = []; + let companies, response; + try { + response = await intercomApiRequest.call(this, '/companies', 'GET'); + } catch (err) { + throw new Error(`Intercom Error: ${err}`); + } + companies = response.companies; + for (const company of companies) { + const companyName = company.name; + const companyId = company.id; + returnData.push({ + name: companyName, + value: companyId, + }); + } + return returnData; + } + }, + }; + async executeSingle(this: IExecuteSingleFunctions): Promise { return { diff --git a/packages/nodes-base/nodes/Intercom/LeadDescription.ts b/packages/nodes-base/nodes/Intercom/LeadDescription.ts new file mode 100644 index 0000000000..ef4e8ea3ff --- /dev/null +++ b/packages/nodes-base/nodes/Intercom/LeadDescription.ts @@ -0,0 +1,172 @@ +import { INodeProperties } from "n8n-workflow"; + +export const leadOpeations = [ + { + displayName: 'Operation', + name: 'operation', + type: 'options', + displayOptions: { + show: { + resource: [ + 'lead', + ], + }, + }, + options: [ + { + name: 'Create', + value: 'create', + description: 'Create a new lead', + }, + ], + default: '', + description: 'The operation to perform.', + }, +] as INodeProperties[]; + +export const leadFields = [ + { + displayName: 'Email', + name: 'email', + type: 'string', + default: '', + required: true, + displayOptions: { + show: { + resource: [ + 'lead', + ], + operation: [ + 'create', + ], + }, + }, + description: 'The email of the user.', + }, + { + displayName: 'JSON Parameters', + name: 'jsonParameters', + type: 'boolean', + default: false, + description: '', + displayOptions: { + show: { + operation: [ + 'create', + ], + resource: [ + 'lead' + ], + }, + }, + }, + { + displayName: 'Options', + name: 'options', + type: 'collection', + placeholder: 'Add Option', + default: {}, + displayOptions: { + show: { + operation: [ + 'create', + ], + resource: [ + 'lead' + ], + }, + }, + options: [ + { + displayName: 'Phone', + name: 'phone', + type: 'string', + default: '', + required: false, + description: 'The phone number of the user', + }, + { + displayName: 'Name', + name: 'name', + type: 'string', + default: '', + placeholder: '', + description: 'Name of the user', + }, + { + displayName: 'Unsubscribed From Emails', + name: 'unsubscribedFromEmails', + type: 'boolean', + default: '', + placeholder: '', + description: 'Whether the Lead is unsubscribed from emails', + }, + { + displayName: 'Update Last Request At', + name: 'updateLastRequestAt', + type: 'boolean', + default: false, + options: [], + required: false, + description: `A boolean value, which if true, instructs Intercom to update the users' last_request_at value to the current API service time in UTC. default value if not sent is false.`, + }, + { + displayName: 'Companies', + name: 'companies', + type: 'multiOptions', + typeOptions: { + loadOptionsMethod: 'getCompanies', + }, + default: [], + required: false, + description: 'Identifies the companies this user belongs to.', + }, + ] + }, + { + displayName: 'Custom Attributes', + name: 'customAttributes', + type: 'fixedCollection', + default: '', + placeholder: 'Add Attribute', + typeOptions: { + multipleValues: true, + }, + required: false, + displayOptions: { + show: { + resource: [ + 'lead', + ], + operation: [ + 'create', + ], + jsonParameters: [ + false, + ], + }, + }, + options: [ + { + name: 'customAttributesValues', + displayName: 'Attributes', + values: [ + { + displayName: 'Name', + name: 'name', + type: 'string', + default: '', + }, + { + displayName: 'Value', + name: 'value', + type: 'string', + default: '', + }, + ], + } + ], + description: 'A hash of key/value pairs to represent custom data you want to attribute to a user.', + }, +] as INodeProperties[]; +