From 02026fa29f6ee1a6e984b4f8b33ba3b3e6875ca2 Mon Sep 17 00:00:00 2001 From: Ricardo Espinoza Date: Sun, 15 Aug 2021 05:52:44 -0400 Subject: [PATCH] :zap: Add more parameters to company resource (Mautic) (#2087) * Add Json params on company * Revert and add only JSON params on Company * Fix Company search field * :zap: Improvements to #1957 * :zap: Improvements Co-authored-by: Dokime --- .../nodes/Mautic/CompanyDescription.ts | 289 +++++++++++++++++- .../nodes/Mautic/GenericFunctions.ts | 7 - .../nodes-base/nodes/Mautic/Mautic.node.ts | 203 +++++++++++- 3 files changed, 481 insertions(+), 18 deletions(-) diff --git a/packages/nodes-base/nodes/Mautic/CompanyDescription.ts b/packages/nodes-base/nodes/Mautic/CompanyDescription.ts index 36bd1ee5cf..cd6b447667 100644 --- a/packages/nodes-base/nodes/Mautic/CompanyDescription.ts +++ b/packages/nodes-base/nodes/Mautic/CompanyDescription.ts @@ -102,12 +102,142 @@ export const companyFields = [ }, }, options: [ + { + displayName: 'Address', + name: 'addressUi', + placeholder: 'Address', + type: 'fixedCollection', + typeOptions: { + multipleValues: false, + }, + default: {}, + options: [ + { + name: 'addressValues', + displayName: 'Address', + values: [ + { + displayName: 'Address Line 1', + name: 'address1', + type: 'string', + default: '', + }, + { + displayName: 'Address Line 2', + name: 'address2', + type: 'string', + default: '', + }, + { + displayName: 'City', + name: 'city', + type: 'string', + default: '', + }, + { + displayName: 'State', + name: 'state', + type: 'string', + default: '', + }, + { + displayName: 'Country', + name: 'country', + type: 'string', + default: '', + }, + { + displayName: 'Zip Code', + name: 'zipCode', + type: 'string', + default: '', + }, + ], + }, + ], + }, + { + displayName: 'Annual Revenue', + name: 'annualRevenue', + type: 'number', + default: 0, + }, + { + displayName: 'Company Email', + name: 'companyEmail', + type: 'string', + default: '', + }, + { + displayName: 'Custom Fields', + name: 'customFieldsUi', + placeholder: 'Add Custom Fields', + description: 'Adds a custom fields to set also values which have not been predefined.', + type: 'fixedCollection', + typeOptions: { + multipleValues: true, + }, + default: {}, + options: [ + { + name: 'customFieldValues', + displayName: 'Field', + values: [ + { + displayName: 'Field ID', + name: 'fieldId', + type: 'options', + typeOptions: { + loadOptionsMethod: 'getCompanyFields', + }, + default: '', + description: 'ID of the field to set.', + }, + { + displayName: 'Field Value', + name: 'fieldValue', + type: 'string', + default: '', + description: 'Value of the field to set.', + }, + ], + }, + ], + }, + { + displayName: 'Description', + name: 'description', + type: 'string', + default: '', + }, + { + displayName: 'Fax', + name: 'fax', + type: 'string', + default: '', + }, + { + displayName: 'Industry', + name: 'industry', + type: 'options', + typeOptions: { + loadOptionsMethod: 'getIndustries', + }, + default: '', + }, { displayName: 'Is Published', name: 'isPublished', type: 'boolean', default: false, }, + { + displayName: 'Number of Employees', + name: 'numberOfEmpoyees', + type: 'number', + default: 0, + }, + { displayName: 'Overwrite With Blank', name: 'overwriteWithBlank', @@ -115,6 +245,18 @@ export const companyFields = [ default: false, description: 'If true, then empty values are set to fields. Otherwise empty values are skipped', }, + { + displayName: 'Phone', + name: 'phone', + type: 'string', + default: '', + }, + { + displayName: 'Website', + name: 'website', + type: 'string', + default: '', + }, ], }, @@ -172,12 +314,134 @@ export const companyFields = [ }, }, options: [ + { + displayName: 'Address', + name: 'addressUi', + placeholder: 'Address', + type: 'fixedCollection', + typeOptions: { + multipleValues: false, + }, + default: {}, + options: [ + { + name: 'addressValues', + displayName: 'Address', + values: [ + { + displayName: 'Address Line 1', + name: 'address1', + type: 'string', + default: '', + }, + { + displayName: 'Address Line 2', + name: 'address2', + type: 'string', + default: '', + }, + { + displayName: 'City', + name: 'city', + type: 'string', + default: '', + }, + { + displayName: 'State', + name: 'state', + type: 'string', + default: '', + }, + { + displayName: 'Country', + name: 'country', + type: 'string', + default: '', + }, + { + displayName: 'Zip Code', + name: 'zipCode', + type: 'string', + default: '', + }, + ], + }, + ], + }, + { + displayName: 'Annual Revenue', + name: 'annualRevenue', + type: 'number', + default: 0, + }, + { + displayName: 'Company Email', + name: 'companyEmail', + type: 'string', + default: '', + }, { displayName: 'Company Name', name: 'name', type: 'string', default: '', - description: 'Company name', + }, + { + displayName: 'Custom Fields', + name: 'customFieldsUi', + placeholder: 'Add Custom Fields', + description: 'Adds a custom fields to set also values which have not been predefined.', + type: 'fixedCollection', + typeOptions: { + multipleValues: true, + }, + default: {}, + options: [ + { + name: 'customFieldValues', + displayName: 'Field', + values: [ + { + displayName: 'Field ID', + name: 'fieldId', + type: 'options', + typeOptions: { + loadOptionsMethod: 'getCompanyFields', + }, + default: '', + description: 'ID of the field to set.', + }, + { + displayName: 'Field Value', + name: 'fieldValue', + type: 'string', + default: '', + description: 'Value of the field to set.', + }, + ], + }, + ], + }, + { + displayName: 'Description', + name: 'description', + type: 'string', + default: '', + }, + { + displayName: 'Fax', + name: 'fax', + type: 'string', + default: '', + }, + { + displayName: 'Industry', + name: 'industry', + type: 'options', + typeOptions: { + loadOptionsMethod: 'getIndustries', + }, + default: '', }, { displayName: 'Is Published', @@ -185,6 +449,13 @@ export const companyFields = [ type: 'boolean', default: false, }, + { + displayName: 'Number of Employees', + name: 'numberOfEmpoyees', + type: 'number', + default: 0, + }, + { displayName: 'Overwrite With Blank', name: 'overwriteWithBlank', @@ -192,6 +463,18 @@ export const companyFields = [ default: false, description: 'If true, then empty values are set to fields. Otherwise empty values are skipped', }, + { + displayName: 'Phone', + name: 'phone', + type: 'string', + default: '', + }, + { + displayName: 'Website', + name: 'website', + type: 'string', + default: '', + }, ], }, @@ -340,8 +623,8 @@ export const companyFields = [ }, { displayName: 'Search', - name: 'isPublished', - type: 'boolean', + name: 'search', + type: 'string', default: '', description: 'String or search command to filter entities by.', }, diff --git a/packages/nodes-base/nodes/Mautic/GenericFunctions.ts b/packages/nodes-base/nodes/Mautic/GenericFunctions.ts index 4136fc5956..0ec2d9b2cb 100644 --- a/packages/nodes-base/nodes/Mautic/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Mautic/GenericFunctions.ts @@ -13,13 +13,6 @@ import { IDataObject, NodeApiError, } from 'n8n-workflow'; -interface OMauticErrorResponse { - errors: Array<{ - conde: number; - message: string; - }>; -} - export async function mauticApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: any = {}, query?: IDataObject, uri?: string): Promise { // tslint:disable-line:no-any const authenticationMethod = this.getNodeParameter('authentication', 0, 'credentials') as string; diff --git a/packages/nodes-base/nodes/Mautic/Mautic.node.ts b/packages/nodes-base/nodes/Mautic/Mautic.node.ts index fb79add534..e035f4205b 100644 --- a/packages/nodes-base/nodes/Mautic/Mautic.node.ts +++ b/packages/nodes-base/nodes/Mautic/Mautic.node.ts @@ -193,6 +193,21 @@ export class Mautic implements INodeType { } return returnData; }, + async getIndustries(this: ILoadOptionsFunctions): Promise { + const returnData: INodePropertyOptions[] = []; + const fields = await mauticApiRequestAllItems.call(this, 'fields', 'GET', '/fields/company'); + for (const field of fields) { + if (field.alias === 'companyindustry') { + for (const { label, value } of field.properties.list) { + returnData.push({ + name: label, + value, + }); + } + } + } + return returnData; + }, // Get all the available contact fields to display them to user so that he can // select them easily async getContactFields(this: ILoadOptionsFunctions): Promise { @@ -235,16 +250,101 @@ export class Mautic implements INodeType { for (let i = 0; i < length; i++) { qs = {}; try { + if (resource === 'company') { //https://developer.mautic.org/#create-company if (operation === 'create') { - const name = this.getNodeParameter('name', i) as string; const simple = this.getNodeParameter('simple', i) as boolean; - const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject; + const name = this.getNodeParameter('name', i) as string; const body: IDataObject = { companyname: name, }; - Object.assign(body, additionalFields); + const { + addressUi, + customFieldsUi, + companyEmail, + fax, + industry, + numberOfEmpoyees, + phone, + website, + annualRevenue, + description, + ...rest + } = this.getNodeParameter('additionalFields', i) as { + addressUi: { + addressValues: IDataObject, + }, + customFieldsUi: { + customFieldValues: [ + { + fieldId: string, + fieldValue: string, + }, + ], + } + companyEmail: string, + fax: string, + industry: string, + numberOfEmpoyees: number, + phone: string, + website: string, + annualRevenue: number, + description: string, + }; + if (addressUi?.addressValues) { + const { addressValues } = addressUi; + body.companyaddress1 = addressValues.address1 as string; + body.companyaddress2 = addressValues.address2 as string; + body.companycity = addressValues.city as string; + body.companystate = addressValues.state as string; + body.companycountry = addressValues.country as string; + body.companyzipcode = addressValues.zipCode as string; + } + + if (companyEmail) { + body.companyemail = companyEmail; + } + + if (fax) { + body.companyfax = fax; + } + + if (industry) { + body.companyindustry = industry; + } + + if (industry) { + body.companyindustry = industry; + } + + if (numberOfEmpoyees) { + body.companynumber_of_employees = numberOfEmpoyees; + } + + if (phone) { + body.companyphone = phone; + } + + if (website) { + body.companywebsite = website; + } + + if (annualRevenue) { + body.companyannual_revenue = annualRevenue; + } + + if (description) { + body.companydescription = description; + } + + if (customFieldsUi?.customFieldValues) { + const { customFieldValues } = customFieldsUi; + const data = customFieldValues.reduce((obj, value) => Object.assign(obj, { [`${value.fieldId}`]: value.fieldValue }), {}); + Object.assign(body, data); + } + + Object.assign(body, rest); responseData = await mauticApiRequest.call(this, 'POST', '/companies/new', body); responseData = responseData.company; if (simple === true) { @@ -255,13 +355,100 @@ export class Mautic implements INodeType { if (operation === 'update') { const companyId = this.getNodeParameter('companyId', i) as string; const simple = this.getNodeParameter('simple', i) as boolean; - const updateFields = this.getNodeParameter('updateFields', i) as IDataObject; const body: IDataObject = {}; - Object.assign(body, updateFields); - if (body.name) { - body.companyname = body.name; - delete body.name; + const { + addressUi, + customFieldsUi, + companyEmail, + name, + fax, + industry, + numberOfEmpoyees, + phone, + website, + annualRevenue, + description, + ...rest + } = this.getNodeParameter('updateFields', i) as { + addressUi: { + addressValues: IDataObject, + }, + customFieldsUi: { + customFieldValues: [ + { + fieldId: string, + fieldValue: string, + }, + ], + } + companyEmail: string, + name: string, + fax: string, + industry: string, + numberOfEmpoyees: number, + phone: string, + website: string, + annualRevenue: number, + description: string, + }; + if (addressUi?.addressValues) { + const { addressValues } = addressUi; + body.companyaddress1 = addressValues.address1 as string; + body.companyaddress2 = addressValues.address2 as string; + body.companycity = addressValues.city as string; + body.companystate = addressValues.state as string; + body.companycountry = addressValues.country as string; + body.companyzipcode = addressValues.zipCode as string; } + + if (companyEmail) { + body.companyemail = companyEmail; + } + + if (name) { + body.companyname = name; + } + + if (fax) { + body.companyfax = fax; + } + + if (industry) { + body.companyindustry = industry; + } + + if (industry) { + body.companyindustry = industry; + } + + if (numberOfEmpoyees) { + body.companynumber_of_employees = numberOfEmpoyees; + } + + if (phone) { + body.companyphone = phone; + } + + if (website) { + body.companywebsite = website; + } + + if (annualRevenue) { + body.companyannual_revenue = annualRevenue; + } + + if (description) { + body.companydescription = description; + } + + if (customFieldsUi?.customFieldValues) { + const { customFieldValues } = customFieldsUi; + const data = customFieldValues.reduce((obj, value) => Object.assign(obj, { [`${value.fieldId}`]: value.fieldValue }), {}); + Object.assign(body, data); + } + + Object.assign(body, rest); + responseData = await mauticApiRequest.call(this, 'PATCH', `/companies/${companyId}/edit`, body); responseData = responseData.company; if (simple === true) {