🚧 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,15 +90,19 @@ 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;
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 (resource === 'lead') {
if (opeation === 'create') { if (opeation === 'create') {
const email = this.getNodeParameter('email') as string; const email = this.getNodeParameter('email', i) as string;
const options = this.getNodeParameter('options') as IDataObject; const options = this.getNodeParameter('options', i) as IDataObject;
const jsonActive = this.getNodeParameter('jsonParameters') as boolean; const jsonActive = this.getNodeParameter('jsonParameters', i) as boolean;
const body: ILead = { const body: ILead = {
email, email,
}; };
@ -125,7 +129,7 @@ export class Intercom implements INodeType {
body.companies = companies; body.companies = companies;
} }
if (!jsonActive) { if (!jsonActive) {
const customAttributesValues = (this.getNodeParameter('customAttributesUi') as IDataObject).customAttributesValues as IDataObject[]; const customAttributesValues = (this.getNodeParameter('customAttributesUi', i) as IDataObject).customAttributesValues as IDataObject[];
if (customAttributesValues) { if (customAttributesValues) {
const customAttributes = {}; const customAttributes = {};
for (let i = 0; i < customAttributesValues.length; i++) { for (let i = 0; i < customAttributesValues.length; i++) {
@ -135,20 +139,24 @@ export class Intercom implements INodeType {
body.custom_attributes = customAttributes; body.custom_attributes = customAttributes;
} }
} else { } else {
const customAttributesJson = validateJSON(this.getNodeParameter('customAttributesJson') as string); const customAttributesJson = validateJSON(this.getNodeParameter('customAttributesJson', i) as string);
if (customAttributesJson) { if (customAttributesJson) {
body.custom_attributes = customAttributesJson; body.custom_attributes = customAttributesJson;
} }
} }
try { try {
response = await intercomApiRequest.call(this, '/contacts', 'POST', body); responseData = await intercomApiRequest.call(this, '/contacts', 'POST', body);
} catch (err) { } catch (err) {
throw new Error(`Intercom Error: ${JSON.stringify(err)}`); throw new Error(`Intercom Error: ${JSON.stringify(err)}`);
} }
} }
} }
return { if (Array.isArray(responseData)) {
json: response, returnData.push.apply(returnData, responseData as IDataObject[]);
}; } else {
returnData.push(responseData as IDataObject);
}
}
return [this.helpers.returnJsonArray(returnData)];
} }
} }