💄 create lead UI done

This commit is contained in:
Ricardo Espinoza 2019-11-16 17:16:11 -05:00
parent 8d886e3766
commit 7a19837833
3 changed files with 252 additions and 0 deletions

View file

@ -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<any> { // 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;
}
}

View file

@ -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<INodePropertyOptions[]> {
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<INodeExecutionData> {
return {

View file

@ -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[];