From b281eb511a102ea863019233dccd81fb3b22c52c Mon Sep 17 00:00:00 2001 From: Ricardo Espinoza Date: Mon, 18 Nov 2019 16:41:00 -0500 Subject: [PATCH] :construction: added execute function --- .../nodes/Intercom/Intercom.node.ts | 118 ++++++++++-------- 1 file changed, 63 insertions(+), 55 deletions(-) diff --git a/packages/nodes-base/nodes/Intercom/Intercom.node.ts b/packages/nodes-base/nodes/Intercom/Intercom.node.ts index f7a1024acd..ea635e22b7 100644 --- a/packages/nodes-base/nodes/Intercom/Intercom.node.ts +++ b/packages/nodes-base/nodes/Intercom/Intercom.node.ts @@ -1,5 +1,5 @@ import { - IExecuteSingleFunctions, + IExecuteFunctions, } from 'n8n-core'; import { IDataObject, @@ -90,65 +90,73 @@ export class Intercom implements INodeType { }, }; - async executeSingle(this: IExecuteSingleFunctions): Promise { - const resource = this.getNodeParameter('resource') as string; - const opeation = this.getNodeParameter('operation') as string; - let response; - if (resource === 'lead') { - if (opeation === 'create') { - const email = this.getNodeParameter('email') as string; - const options = this.getNodeParameter('options') as IDataObject; - const jsonActive = this.getNodeParameter('jsonParameters') as boolean; - const body: ILead = { - email, - }; - if (options.phone) { - body.phone = options.phone as string; - } - if (options.name) { - body.name = options.name as string; - } - if (options.unsubscribedFromEmails) { - body.unsubscribed_from_emails = options.unsubscribedFromEmails as boolean; - } - if (options.updateLastRequestAt) { - body.update_last_request_at = options.updateLastRequestAt as boolean; - } - if (options.companies) { - const companies: ILeadCompany[] = []; - // @ts-ignore - options.companies.forEach( o => { - const company: ILeadCompany = {}; - company.company_id = o; - companies.push(company); - }); - body.companies = companies; - } - if (!jsonActive) { - const customAttributesValues = (this.getNodeParameter('customAttributesUi') as IDataObject).customAttributesValues as IDataObject[]; - if (customAttributesValues) { - const customAttributes = {}; - for (let i = 0; i < customAttributesValues.length; i++) { - // @ts-ignore - customAttributes[customAttributesValues[i].name] = customAttributesValues[i].value; + async execute(this: IExecuteFunctions): Promise { + const items = this.getInputData(); + const returnData: IDataObject[] = []; + const length = items.length as unknown as number; + let responseData; + for (let i = 0; i < length; i++) { + const resource = this.getNodeParameter('resource', 0) as string; + const opeation = this.getNodeParameter('operation', 0) as string; + if (resource === 'lead') { + if (opeation === 'create') { + const email = this.getNodeParameter('email', i) as string; + const options = this.getNodeParameter('options', i) as IDataObject; + const jsonActive = this.getNodeParameter('jsonParameters', i) as boolean; + const body: ILead = { + email, + }; + if (options.phone) { + body.phone = options.phone as string; + } + if (options.name) { + body.name = options.name as string; + } + if (options.unsubscribedFromEmails) { + body.unsubscribed_from_emails = options.unsubscribedFromEmails as boolean; + } + if (options.updateLastRequestAt) { + body.update_last_request_at = options.updateLastRequestAt as boolean; + } + if (options.companies) { + const companies: ILeadCompany[] = []; + // @ts-ignore + options.companies.forEach( o => { + const company: ILeadCompany = {}; + company.company_id = o; + companies.push(company); + }); + body.companies = companies; + } + if (!jsonActive) { + const customAttributesValues = (this.getNodeParameter('customAttributesUi', i) as IDataObject).customAttributesValues as IDataObject[]; + 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 { - const customAttributesJson = validateJSON(this.getNodeParameter('customAttributesJson') as string); - if (customAttributesJson) { - body.custom_attributes = customAttributesJson; + try { + responseData = await intercomApiRequest.call(this, '/contacts', 'POST', body); + } catch (err) { + throw new Error(`Intercom Error: ${JSON.stringify(err)}`); } } - try { - response = await intercomApiRequest.call(this, '/contacts', 'POST', body); - } catch (err) { - throw new Error(`Intercom Error: ${JSON.stringify(err)}`); - } + } + if (Array.isArray(responseData)) { + returnData.push.apply(returnData, responseData as IDataObject[]); + } else { + returnData.push(responseData as IDataObject); } } - return { - json: response, - }; + return [this.helpers.returnJsonArray(returnData)]; } }