UNPKG

n8n-nodes-base

Version:

Base nodes of n8n

258 lines 12.4 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.GetResponse = void 0; const moment_timezone_1 = __importDefault(require("moment-timezone")); const n8n_workflow_1 = require("n8n-workflow"); const ContactDescription_1 = require("./ContactDescription"); const GenericFunctions_1 = require("./GenericFunctions"); class GetResponse { description = { displayName: 'GetResponse', name: 'getResponse', // eslint-disable-next-line n8n-nodes-base/node-class-description-icon-not-svg icon: 'file:getResponse.png', group: ['input'], version: 1, subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}', description: 'Consume GetResponse API', defaults: { name: 'GetResponse', }, usableAsTool: true, inputs: [n8n_workflow_1.NodeConnectionTypes.Main], outputs: [n8n_workflow_1.NodeConnectionTypes.Main], credentials: [ { name: 'getResponseApi', required: true, displayOptions: { show: { authentication: ['apiKey'], }, }, }, { name: 'getResponseOAuth2Api', required: true, displayOptions: { show: { authentication: ['oAuth2'], }, }, }, ], properties: [ { displayName: 'Authentication', name: 'authentication', type: 'options', options: [ { name: 'API Key', value: 'apiKey', }, { name: 'OAuth2', value: 'oAuth2', }, ], default: 'apiKey', }, { displayName: 'Resource', name: 'resource', type: 'options', noDataExpression: true, options: [ { name: 'Contact', value: 'contact', }, ], default: 'contact', }, ...ContactDescription_1.contactOperations, ...ContactDescription_1.contactFields, ], }; methods = { loadOptions: { // Get all the campaigns to display them to user so that they can // select them easily async getCampaigns() { const returnData = []; const campaigns = await GenericFunctions_1.getresponseApiRequest.call(this, 'GET', '/campaigns'); for (const campaign of campaigns) { returnData.push({ name: campaign.name, value: campaign.campaignId, }); } return returnData; }, // Get all the tagd to display them to user so that they can // select them easily async getTags() { const returnData = []; const tags = await GenericFunctions_1.getresponseApiRequest.call(this, 'GET', '/tags'); for (const tag of tags) { returnData.push({ name: tag.name, value: tag.tagId, }); } return returnData; }, // Get all the custom fields to display them to user so that they can // select them easily async getCustomFields() { const returnData = []; const customFields = await GenericFunctions_1.getresponseApiRequest.call(this, 'GET', '/custom-fields'); for (const customField of customFields) { returnData.push({ name: customField.name, value: customField.customFieldId, }); } return returnData; }, }, }; async execute() { const items = this.getInputData(); const returnData = []; const length = items.length; const qs = {}; let responseData; const resource = this.getNodeParameter('resource', 0); const operation = this.getNodeParameter('operation', 0); for (let i = 0; i < length; i++) { try { if (resource === 'contact') { //https://apireference.getresponse.com/#operation/createContact if (operation === 'create') { const email = this.getNodeParameter('email', i); const campaignId = this.getNodeParameter('campaignId', i); const additionalFields = this.getNodeParameter('additionalFields', i); const body = { email, campaign: { campaignId, }, }; Object.assign(body, additionalFields); if (additionalFields.customFieldsUi) { const customFieldValues = additionalFields.customFieldsUi .customFieldValues; if (customFieldValues) { body.customFieldValues = customFieldValues; for (let index = 0; index < customFieldValues.length; index++) { if (!Array.isArray(customFieldValues[index].value)) { customFieldValues[index].value = [customFieldValues[index].value]; } } delete body.customFieldsUi; } } responseData = await GenericFunctions_1.getresponseApiRequest.call(this, 'POST', '/contacts', body); responseData = { success: true }; } //https://apireference.getresponse.com/?_ga=2.160836350.2102802044.1604719933-1897033509.1604598019#operation/deleteContact if (operation === 'delete') { const contactId = this.getNodeParameter('contactId', i); const options = this.getNodeParameter('options', i); Object.assign(qs, options); responseData = await GenericFunctions_1.getresponseApiRequest.call(this, 'DELETE', `/contacts/${contactId}`, {}, qs); responseData = { success: true }; } //https://apireference.getresponse.com/?_ga=2.160836350.2102802044.1604719933-1897033509.1604598019#operation/getContactById if (operation === 'get') { const contactId = this.getNodeParameter('contactId', i); const options = this.getNodeParameter('options', i); Object.assign(qs, options); responseData = await GenericFunctions_1.getresponseApiRequest.call(this, 'GET', `/contacts/${contactId}`, {}, qs); } //https://apireference.getresponse.com/?_ga=2.160836350.2102802044.1604719933-1897033509.1604598019#operation/getContactList if (operation === 'getAll') { const returnAll = this.getNodeParameter('returnAll', i); const options = this.getNodeParameter('options', i); const timezone = this.getTimezone(); Object.assign(qs, options); const isNotQuery = ['sortBy', 'sortOrder', 'additionalFlags', 'fields', 'exactMatch']; const isDate = ['createdOnFrom', 'createdOnTo', 'changeOnFrom', 'changeOnTo']; const dateMapToKey = { createdOnFrom: '[createdOn][from]', createdOnTo: '[createdOn][to]', changeOnFrom: '[changeOn][from]', changeOnTo: '[changeOn][to]', }; for (const key of Object.keys(qs)) { if (!isNotQuery.includes(key)) { if (isDate.includes(key)) { qs[`query${dateMapToKey[key]}`] = moment_timezone_1.default .tz(qs[key], timezone) .format('YYYY-MM-DDTHH:mm:ssZZ'); } else { qs[`query[${key}]`] = qs[key]; } delete qs[key]; } } if (qs.sortBy) { qs[`sort[${qs.sortBy}]`] = qs.sortOrder || 'ASC'; } if (qs.exactMatch === true) { qs.additionalFlags = 'exactMatch'; delete qs.exactMatch; } if (returnAll) { responseData = await GenericFunctions_1.getResponseApiRequestAllItems.call(this, 'GET', '/contacts', {}, qs); } else { qs.perPage = this.getNodeParameter('limit', i); responseData = await GenericFunctions_1.getresponseApiRequest.call(this, 'GET', '/contacts', {}, qs); } } //https://apireference.getresponse.com/?_ga=2.160836350.2102802044.1604719933-1897033509.1604598019#operation/updateContact if (operation === 'update') { const contactId = this.getNodeParameter('contactId', i); const updateFields = this.getNodeParameter('updateFields', i); const body = {}; Object.assign(body, updateFields); if (updateFields.customFieldsUi) { const customFieldValues = updateFields.customFieldsUi .customFieldValues; customFieldValues.forEach((entry) => { if (typeof entry.value === 'string') { entry.value = entry.value.split(',').map((value) => value.trim()); } }); if (customFieldValues) { body.customFieldValues = customFieldValues; delete body.customFieldsUi; } } responseData = await GenericFunctions_1.getresponseApiRequest.call(this, 'POST', `/contacts/${contactId}`, body); } } const executionData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray(responseData), { itemData: { item: i } }); returnData.push(...executionData); } catch (error) { if (this.continueOnFail()) { const executionErrorData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray({ error: error.message }), { itemData: { item: i } }); returnData.push(...executionErrorData); continue; } throw error; } } return [returnData]; } } exports.GetResponse = GetResponse; //# sourceMappingURL=GetResponse.node.js.map