Add resource contact segment and company contact to Mautic node (#1975)

* Add contact To Segment operation

*  Improvements to #1973

*  Rename resource

Co-authored-by: Rodrigo Correia <rodrigoscdc@gmail.com>
Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
This commit is contained in:
Ricardo Espinoza 2021-07-08 04:12:19 -04:00 committed by GitHub
parent a842e4bc0a
commit abc2f2a515
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 156 additions and 17 deletions

View file

@ -2,7 +2,7 @@ import {
INodeProperties,
} from 'n8n-workflow';
export const contactCompanyOperations = [
export const companyContactOperations = [
{
displayName: 'Operation',
name: 'operation',
@ -10,7 +10,7 @@ export const contactCompanyOperations = [
displayOptions: {
show: {
resource: [
'contactCompany',
'companyContact',
],
},
},
@ -31,10 +31,10 @@ export const contactCompanyOperations = [
},
] as INodeProperties[];
export const contactCompanyFields = [
export const companyContactFields = [
/* -------------------------------------------------------------------------- */
/* contactCompany:add */
/* companyContact:add */
/* -------------------------------------------------------------------------- */
{
displayName: 'Contact ID',
@ -43,7 +43,7 @@ export const contactCompanyFields = [
displayOptions: {
show: {
resource: [
'contactCompany',
'companyContact',
],
operation: [
'add',
@ -61,7 +61,7 @@ export const contactCompanyFields = [
displayOptions: {
show: {
resource: [
'contactCompany',
'companyContact',
],
operation: [
'add',

View file

@ -0,0 +1,82 @@
import {
INodeProperties,
} from 'n8n-workflow';
export const contactSegmentOperations = [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
displayOptions: {
show: {
resource: [
'contactSegment',
],
},
},
options: [
{
name: 'Add',
value: 'add',
description: 'Add contact to a segment',
},
{
name: 'Remove',
value: 'remove',
description: 'Remove contact from a segment',
},
],
default: 'add',
description: 'The operation to perform.',
},
] as INodeProperties[];
export const contactSegmentFields = [
/* -------------------------------------------------------------------------- */
/* contactSegment:add */
/* -------------------------------------------------------------------------- */
{
displayName: 'Contact ID',
name: 'contactId',
type: 'string',
required: true,
displayOptions: {
show: {
resource: [
'contactSegment',
],
operation: [
'add',
'remove',
],
},
},
default: '',
description: 'Contact ID',
},
{
displayName: 'Segment ID',
name: 'segmentId',
type: 'options',
required: true,
displayOptions: {
show: {
resource: [
'contactSegment',
],
operation: [
'add',
'remove',
],
},
},
typeOptions: {
loadOptionsMethod: 'getSegments',
},
default: '',
description: 'Segment ID',
},
] as INodeProperties[];

View file

@ -30,9 +30,14 @@ import {
} from './CompanyDescription';
import {
contactCompanyFields,
contactCompanyOperations,
} from './ContactCompanyDescription';
companyContactFields,
companyContactOperations,
} from './CompanyContactDescription';
import {
contactSegmentFields,
contactSegmentOperations,
} from './ContactSegmentDescription';
import {
snakeCase,
@ -42,7 +47,7 @@ export class Mautic implements INodeType {
description: INodeTypeDescription = {
displayName: 'Mautic',
name: 'mautic',
icon: 'file:mautic.png',
icon: 'file:mautic.svg',
group: ['output'],
version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
@ -104,15 +109,20 @@ export class Mautic implements INodeType {
value: 'company',
description: 'Create or modify a company',
},
{
name: 'Company Contact',
value: 'companyContact',
description: 'Add/remove contacts to/from a company',
},
{
name: 'Contact',
value: 'contact',
description: 'Create & modify contacts',
},
{
name: 'Contact <> Company',
value: 'contactCompany',
description: 'Add/ remove contacts from a company',
name: 'Contact Segment',
value: 'contactSegment',
description: 'Add/remove contacts to/from a segment',
},
],
default: 'contact',
@ -122,8 +132,10 @@ export class Mautic implements INodeType {
...companyFields,
...contactOperations,
...contactFields,
...contactCompanyOperations,
...contactCompanyFields,
...contactSegmentOperations,
...contactSegmentFields,
...companyContactOperations,
...companyContactFields,
],
};
@ -194,6 +206,19 @@ export class Mautic implements INodeType {
}
return returnData;
},
// Get all the available segments to display them to user so that he can
// select them easily
async getSegments(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
const segments = await mauticApiRequestAllItems.call(this, 'lists', 'GET', '/segments');
for (const segment of segments) {
returnData.push({
name: segment.name,
value: segment.id,
});
}
return returnData;
},
},
};
@ -547,7 +572,22 @@ export class Mautic implements INodeType {
}
}
if (resource === 'contactCompany') {
if (resource === 'contactSegment') {
//https://developer.mautic.org/?php#add-contact-to-a-segment
if (operation === 'add') {
const contactId = this.getNodeParameter('contactId', i) as string;
const segmentId = this.getNodeParameter('segmentId', i) as string;
responseData = await mauticApiRequest.call(this, 'POST', `/segments/${segmentId}/contact/${contactId}/add`);
}
//https://developer.mautic.org/#remove-contact-from-a-segment
if (operation === 'remove') {
const contactId = this.getNodeParameter('contactId', i) as string;
const segmentId = this.getNodeParameter('segmentId', i) as string;
responseData = await mauticApiRequest.call(this, 'POST', `/segments/${segmentId}/contact/${contactId}/remove`);
}
}
if (resource === 'companyContact') {
//https://developer.mautic.org/#add-contact-to-a-company
if (operation === 'add') {
const contactId = this.getNodeParameter('contactId', i) as string;

View file

@ -24,7 +24,7 @@ export class MauticTrigger implements INodeType {
description: INodeTypeDescription = {
displayName: 'Mautic Trigger',
name: 'mauticTrigger',
icon: 'file:mautic.png',
icon: 'file:mautic.svg',
group: ['trigger'],
version: 1,
description: 'Handle Mautic events via webhooks',

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2 KiB

View file

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 16.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
width="349.779px" height="349.779px" viewBox="0 0 349.779 349.779" enable-background="new 0 0 349.779 349.779"
xml:space="preserve">
<path fill="#FFFFFF" d="M174.89,349.779C78.612,349.779,0,271.462,0,174.89S78.612,0,174.89,0c23.26,0,45.931,4.417,67.129,13.543
c5.889,2.65,8.833,9.422,6.478,15.605c-2.649,5.888-9.421,8.833-15.604,6.477c-18.549-7.655-37.98-11.482-58.002-11.482
c-83.323,0-151.041,67.718-151.041,151.041S91.567,326.225,174.89,326.225c83.323,0,151.041-67.718,151.041-151.041
c0-17.96-2.944-35.332-9.127-51.819c-2.355-6.183,0.883-12.955,7.066-15.31c6.183-2.355,12.954,0.883,15.31,7.066
c7.066,19.138,10.6,39.453,10.6,60.063C349.779,271.167,271.462,349.779,174.89,349.779"/>
<g>
<polygon fill="#FDB933" points="251.44,156.93 224.354,185.194 239.369,248.496 273.522,248.496 "/>
</g>
<polygon fill="#FDB933" points="240.253,73.312 249.674,82.734 174.89,161.935 110.999,96.277 74.196,248.496 108.35,248.496
128.665,163.996 174.89,214.343 273.817,106.583 283.239,116.299 292.66,63.007 "/>
</svg>

After

Width:  |  Height:  |  Size: 1.4 KiB