🚧 added execute function

This commit is contained in:
Ricardo Espinoza 2019-11-18 16:41:00 -05:00
parent 2e51e224d8
commit b281eb511a

View file

@ -1,5 +1,5 @@
import { import {
IExecuteSingleFunctions, IExecuteFunctions,
} from 'n8n-core'; } from 'n8n-core';
import { import {
IDataObject, IDataObject,
@ -90,65 +90,73 @@ export class Intercom implements INodeType {
}, },
}; };
async executeSingle(this: IExecuteSingleFunctions): Promise<INodeExecutionData> { async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const resource = this.getNodeParameter('resource') as string; const items = this.getInputData();
const opeation = this.getNodeParameter('operation') as string; const returnData: IDataObject[] = [];
let response; const length = items.length as unknown as number;
if (resource === 'lead') { let responseData;
if (opeation === 'create') { for (let i = 0; i < length; i++) {
const email = this.getNodeParameter('email') as string; const resource = this.getNodeParameter('resource', 0) as string;
const options = this.getNodeParameter('options') as IDataObject; const opeation = this.getNodeParameter('operation', 0) as string;
const jsonActive = this.getNodeParameter('jsonParameters') as boolean; if (resource === 'lead') {
const body: ILead = { if (opeation === 'create') {
email, const email = this.getNodeParameter('email', i) as string;
}; const options = this.getNodeParameter('options', i) as IDataObject;
if (options.phone) { const jsonActive = this.getNodeParameter('jsonParameters', i) as boolean;
body.phone = options.phone as string; const body: ILead = {
} email,
if (options.name) { };
body.name = options.name as string; if (options.phone) {
} body.phone = options.phone as string;
if (options.unsubscribedFromEmails) { }
body.unsubscribed_from_emails = options.unsubscribedFromEmails as boolean; if (options.name) {
} body.name = options.name as string;
if (options.updateLastRequestAt) { }
body.update_last_request_at = options.updateLastRequestAt as boolean; if (options.unsubscribedFromEmails) {
} body.unsubscribed_from_emails = options.unsubscribedFromEmails as boolean;
if (options.companies) { }
const companies: ILeadCompany[] = []; if (options.updateLastRequestAt) {
// @ts-ignore body.update_last_request_at = options.updateLastRequestAt as boolean;
options.companies.forEach( o => { }
const company: ILeadCompany = {}; if (options.companies) {
company.company_id = o; const companies: ILeadCompany[] = [];
companies.push(company); // @ts-ignore
}); options.companies.forEach( o => {
body.companies = companies; const company: ILeadCompany = {};
} company.company_id = o;
if (!jsonActive) { companies.push(company);
const customAttributesValues = (this.getNodeParameter('customAttributesUi') as IDataObject).customAttributesValues as IDataObject[]; });
if (customAttributesValues) { body.companies = companies;
const customAttributes = {}; }
for (let i = 0; i < customAttributesValues.length; i++) { if (!jsonActive) {
// @ts-ignore const customAttributesValues = (this.getNodeParameter('customAttributesUi', i) as IDataObject).customAttributesValues as IDataObject[];
customAttributes[customAttributesValues[i].name] = customAttributesValues[i].value; if (customAttributesValues) {
const customAttributes = {};
for (let i = 0; i < customAttributesValues.length; i++) {
// @ts-ignore
customAttributes[customAttributesValues[i].name] = customAttributesValues[i].value;
}
body.custom_attributes = customAttributes;
}
} else {
const customAttributesJson = validateJSON(this.getNodeParameter('customAttributesJson', i) as string);
if (customAttributesJson) {
body.custom_attributes = customAttributesJson;
} }
body.custom_attributes = customAttributes;
} }
} else { try {
const customAttributesJson = validateJSON(this.getNodeParameter('customAttributesJson') as string); responseData = await intercomApiRequest.call(this, '/contacts', 'POST', body);
if (customAttributesJson) { } catch (err) {
body.custom_attributes = customAttributesJson; throw new Error(`Intercom Error: ${JSON.stringify(err)}`);
} }
} }
try { }
response = await intercomApiRequest.call(this, '/contacts', 'POST', body); if (Array.isArray(responseData)) {
} catch (err) { returnData.push.apply(returnData, responseData as IDataObject[]);
throw new Error(`Intercom Error: ${JSON.stringify(err)}`); } else {
} returnData.push(responseData as IDataObject);
} }
} }
return { return [this.helpers.returnJsonArray(returnData)];
json: response,
};
} }
} }