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

*  Improvements to #1957

*  Improvements

Co-authored-by: Dokime <jnau5@yahoo.com>
This commit is contained in:
Ricardo Espinoza 2021-08-15 05:52:44 -04:00 committed by GitHub
parent 56b82439cd
commit 02026fa29f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 481 additions and 18 deletions

View file

@ -102,12 +102,142 @@ export const companyFields = [
}, },
}, },
options: [ 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', displayName: 'Is Published',
name: 'isPublished', name: 'isPublished',
type: 'boolean', type: 'boolean',
default: false, default: false,
}, },
{
displayName: 'Number of Employees',
name: 'numberOfEmpoyees',
type: 'number',
default: 0,
},
{ {
displayName: 'Overwrite With Blank', displayName: 'Overwrite With Blank',
name: 'overwriteWithBlank', name: 'overwriteWithBlank',
@ -115,6 +245,18 @@ export const companyFields = [
default: false, default: false,
description: 'If true, then empty values are set to fields. Otherwise empty values are skipped', 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: [ 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', displayName: 'Company Name',
name: 'name', name: 'name',
type: 'string', type: 'string',
default: '', 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', displayName: 'Is Published',
@ -185,6 +449,13 @@ export const companyFields = [
type: 'boolean', type: 'boolean',
default: false, default: false,
}, },
{
displayName: 'Number of Employees',
name: 'numberOfEmpoyees',
type: 'number',
default: 0,
},
{ {
displayName: 'Overwrite With Blank', displayName: 'Overwrite With Blank',
name: 'overwriteWithBlank', name: 'overwriteWithBlank',
@ -192,6 +463,18 @@ export const companyFields = [
default: false, default: false,
description: 'If true, then empty values are set to fields. Otherwise empty values are skipped', 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', displayName: 'Search',
name: 'isPublished', name: 'search',
type: 'boolean', type: 'string',
default: '', default: '',
description: 'String or search command to filter entities by.', description: 'String or search command to filter entities by.',
}, },

View file

@ -13,13 +13,6 @@ import {
IDataObject, NodeApiError, IDataObject, NodeApiError,
} from 'n8n-workflow'; } 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<any> { // tslint:disable-line:no-any export async function mauticApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: any = {}, query?: IDataObject, uri?: string): Promise<any> { // tslint:disable-line:no-any
const authenticationMethod = this.getNodeParameter('authentication', 0, 'credentials') as string; const authenticationMethod = this.getNodeParameter('authentication', 0, 'credentials') as string;

View file

@ -193,6 +193,21 @@ export class Mautic implements INodeType {
} }
return returnData; return returnData;
}, },
async getIndustries(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
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 // Get all the available contact fields to display them to user so that he can
// select them easily // select them easily
async getContactFields(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> { async getContactFields(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
@ -235,16 +250,101 @@ export class Mautic implements INodeType {
for (let i = 0; i < length; i++) { for (let i = 0; i < length; i++) {
qs = {}; qs = {};
try { try {
if (resource === 'company') { if (resource === 'company') {
//https://developer.mautic.org/#create-company //https://developer.mautic.org/#create-company
if (operation === 'create') { if (operation === 'create') {
const name = this.getNodeParameter('name', i) as string;
const simple = this.getNodeParameter('simple', i) as boolean; 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 = { const body: IDataObject = {
companyname: name, 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 = await mauticApiRequest.call(this, 'POST', '/companies/new', body);
responseData = responseData.company; responseData = responseData.company;
if (simple === true) { if (simple === true) {
@ -255,13 +355,100 @@ export class Mautic implements INodeType {
if (operation === 'update') { if (operation === 'update') {
const companyId = this.getNodeParameter('companyId', i) as string; const companyId = this.getNodeParameter('companyId', i) as string;
const simple = this.getNodeParameter('simple', i) as boolean; const simple = this.getNodeParameter('simple', i) as boolean;
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
const body: IDataObject = {}; const body: IDataObject = {};
Object.assign(body, updateFields); const {
if (body.name) { addressUi,
body.companyname = body.name; customFieldsUi,
delete body.name; 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 = await mauticApiRequest.call(this, 'PATCH', `/companies/${companyId}/edit`, body);
responseData = responseData.company; responseData = responseData.company;
if (simple === true) { if (simple === true) {