mirror of
https://github.com/n8n-io/n8n.git
synced 2025-01-11 04:47:29 -08:00
⚡ 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:
parent
56b82439cd
commit
02026fa29f
|
@ -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.',
|
||||
},
|
||||
|
|
|
@ -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<any> { // tslint:disable-line:no-any
|
||||
const authenticationMethod = this.getNodeParameter('authentication', 0, 'credentials') as string;
|
||||
|
||||
|
|
|
@ -193,6 +193,21 @@ export class Mautic implements INodeType {
|
|||
}
|
||||
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
|
||||
// select them easily
|
||||
async getContactFields(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
||||
|
@ -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) {
|
||||
|
|
Loading…
Reference in a new issue