Small improvements and fixes to Mautic-Nodes

This commit is contained in:
Jan Oberhauser 2020-01-15 20:53:43 -06:00
parent 3c7b4458c4
commit 1bfa412a27
6 changed files with 173 additions and 91 deletions

View file

@ -12,6 +12,7 @@ export class MauticApi implements ICredentialType {
name: 'url', name: 'url',
type: 'string' as NodePropertyTypes, type: 'string' as NodePropertyTypes,
default: '', default: '',
placeholder: 'https://name.mautic.net',
}, },
{ {
displayName: 'Username', displayName: 'Username',
@ -22,7 +23,7 @@ export class MauticApi implements ICredentialType {
{ {
displayName: 'Password', displayName: 'Password',
name: 'password', name: 'password',
type: 'string' as NodePropertyTypes, type: 'password' as NodePropertyTypes,
default: '', default: '',
}, },
]; ];

View file

@ -1,4 +1,4 @@
import { INodeProperties } from "n8n-workflow"; import { INodeProperties } from 'n8n-workflow';
export const contactOperations = [ export const contactOperations = [
{ {
@ -104,7 +104,7 @@ export const contactFields = [
}, },
}, },
default: '', default: '',
description: 'LastName', description: 'Last Name',
}, },
{ {
displayName: 'Primary Company', displayName: 'Primary Company',
@ -394,6 +394,7 @@ export const contactFields = [
}, },
], ],
}, },
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* contact:get */ /* contact:get */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
@ -414,6 +415,7 @@ export const contactFields = [
default: '', default: '',
description: 'Contact ID', description: 'Contact ID',
}, },
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* contact:getAll */ /* contact:getAll */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
@ -458,74 +460,7 @@ export const contactFields = [
default: 30, default: 30,
description: 'How many results to return.', description: 'How many results to return.',
}, },
{
displayName: 'Filters',
name: 'filters',
type: 'collection',
placeholder: 'Add Filter',
default: {},
displayOptions: {
show: {
resource: [
'contact',
],
operation: [
'getAll',
],
},
},
options: [
{
displayName: 'Search',
name: 'search',
type: 'string',
default: '',
description: 'String or search command to filter entities by.',
},
{
displayName: 'Order By',
name: 'orderBy',
type: 'string',
default: '',
description: `Column to sort by. Can use any column listed in the response.<br/>
However, all properties in the response that are written in camelCase need to be<br/>
changed a bit. Before every capital add an underscore _ and then change the capital<br/>
letters to non-capital letters. So dateIdentified becomes date_identified, modifiedByUser
becomes modified_by_user etc.`,
},
{
displayName: 'Order By Dir',
name: 'orderByDir',
type: 'options',
default: '',
options: [
{
name: 'ASC',
valie: 'asc',
},
{
name: 'Desc',
valie: 'desc',
},
],
description: 'Sort direction: asc or desc.',
},
{
displayName: 'Published Only',
name: 'publishedOnly',
type: 'boolean',
default: false,
description: 'Only return currently published entities.',
},
{
displayName: 'Minimal',
name: 'minimal',
type: 'boolean',
default: false,
description: 'Return only array of entities without additional lists in it.',
},
]
},
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* contact:delete */ /* contact:delete */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
@ -546,4 +481,128 @@ export const contactFields = [
default: '', default: '',
description: 'Contact ID', description: 'Contact ID',
}, },
/* -------------------------------------------------------------------------- */
/* contact:all */
/* -------------------------------------------------------------------------- */
{
displayName: 'Options',
name: 'options',
type: 'collection',
displayOptions: {
show: {
'resource': [
'contact',
],
},
},
placeholder: 'Add Option',
default: {},
options: [
{
displayName: 'Search',
name: 'search',
type: 'string',
displayOptions: {
show: {
'/resource': [
'contact',
],
'/operation': [
'getAll',
],
},
},
default: '',
description: 'String or search command to filter entities by.',
},
{
displayName: 'Order By',
name: 'orderBy',
type: 'string',
displayOptions: {
show: {
'/resource': [
'contact',
],
'/operation': [
'getAll',
],
},
},
default: '',
description: 'Column to sort by. Can use any column listed in the response.',
},
{
displayName: 'Order By Dir',
name: 'orderByDir',
type: 'options',
displayOptions: {
show: {
'/resource': [
'contact',
],
'/operation': [
'getAll',
],
},
},
default: '',
options: [
{
name: 'ASC',
value: 'asc',
},
{
name: 'DESC',
value: 'desc',
},
],
description: 'Sort direction: ASC or DESC.',
},
{
displayName: 'Published Only',
name: 'publishedOnly',
type: 'boolean',
displayOptions: {
show: {
'/resource': [
'contact',
],
'/operation': [
'getAll',
],
},
},
default: false,
description: 'Only return currently published entities.',
},
{
displayName: 'Minimal',
name: 'minimal',
type: 'boolean',
displayOptions: {
show: {
'/resource': [
'contact',
],
'/operation': [
'getAll',
],
},
},
default: false,
description: 'Return only array of entities without additional lists in it.',
},
{
displayName: 'RAW Data',
name: 'rawData',
type: 'boolean',
default: false,
description: `By default only the data of the fields get returned. If this<br />
options gets set the RAW response with all data gets returned.`,
},
]
},
] as INodeProperties[]; ] as INodeProperties[];

View file

@ -31,9 +31,9 @@ export async function mauticApiRequest(this: IHookFunctions | IExecuteFunctions
const errorMessage = err.error || err.error.message; const errorMessage = err.error || err.error.message;
if (errorMessage !== undefined) { if (errorMessage !== undefined) {
throw errorMessage; throw new Error(errorMessage);
} }
throw err throw err;
} }
} }
@ -50,20 +50,18 @@ export async function mauticApiRequestAllItems(this: IHookFunctions | IExecuteFu
query.limit = 30; query.limit = 30;
query.start = 0; query.start = 0;
let uri: string | undefined;
do { do {
responseData = await mauticApiRequest.call(this, method, endpoint, body, query, uri); responseData = await mauticApiRequest.call(this, method, endpoint, body, query);
const values = Object.values(responseData[propertyName]) const values = Object.values(responseData[propertyName]);
for (const value of values) { for (const value of values) {
data.push(value as IDataObject) data.push(value as IDataObject);
} }
returnData.push.apply(returnData, data); returnData.push.apply(returnData, data);
query.start++; query.start++;
data = []; data = [];
} while ( } while (
responseData.total !== undefined && responseData.total !== undefined &&
((query.limit * query.start) - parseInt(responseData.total)) < 0 ((query.limit * query.start) - parseInt(responseData.total, 10)) < 0
); );
return returnData; return returnData;

View file

@ -1,3 +1,5 @@
import { snakeCase } from 'change-case';
import { import {
IExecuteFunctions, IExecuteFunctions,
} from 'n8n-core'; } from 'n8n-core';
@ -89,10 +91,14 @@ export class Mautic implements INodeType {
const length = items.length as unknown as number; const length = items.length as unknown as number;
let qs: IDataObject; let qs: IDataObject;
let responseData; let responseData;
const resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string;
for (let i = 0; i < length; i++) { for (let i = 0; i < length; i++) {
qs = {}; qs = {};
const resource = this.getNodeParameter('resource', 0) as string; const options = this.getNodeParameter('options', i) as IDataObject;
const operation = this.getNodeParameter('operation', 0) as string;
if (resource === 'contact') { if (resource === 'contact') {
//https://developer.mautic.org/?php#create-contact //https://developer.mautic.org/?php#create-contact
if (operation === 'create') { if (operation === 'create') {
@ -115,7 +121,7 @@ export class Mautic implements INodeType {
if (json !== undefined) { if (json !== undefined) {
body = { ...json }; body = { ...json };
} else { } else {
throw new Error('Invalid JSON') throw new Error('Invalid JSON');
} }
} }
if (additionalFields.ipAddress) { if (additionalFields.ipAddress) {
@ -159,7 +165,7 @@ export class Mautic implements INodeType {
if (json !== undefined) { if (json !== undefined) {
body = { ...json }; body = { ...json };
} else { } else {
throw new Error('Invalid JSON') throw new Error('Invalid JSON');
} }
} }
if (updateFields.ipAddress) { if (updateFields.ipAddress) {
@ -191,8 +197,14 @@ export class Mautic implements INodeType {
//https://developer.mautic.org/?php#list-contacts //https://developer.mautic.org/?php#list-contacts
if (operation === 'getAll') { if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', i) as boolean; const returnAll = this.getNodeParameter('returnAll', i) as boolean;
const filters = this.getNodeParameter('filters', i) as IDataObject; qs = Object.assign(qs, options);
qs = Object.assign(qs, filters); if (qs.orderBy) {
// For some reason does camelCase get used in the returned data
// but snake_case here. So convert it automatically to not confuse
// the users.
qs.orderBy = snakeCase(qs.orderBy as string);
}
try { try {
if (returnAll === true) { if (returnAll === true) {
responseData = await mauticApiRequestAllItems.call(this, 'contacts', 'GET', '/contacts', {}, qs); responseData = await mauticApiRequestAllItems.call(this, 'contacts', 'GET', '/contacts', {}, qs);
@ -203,6 +215,7 @@ export class Mautic implements INodeType {
responseData = responseData.contacts; responseData = responseData.contacts;
responseData = Object.values(responseData); responseData = Object.values(responseData);
} }
} catch (err) { } catch (err) {
throw new Error(`Mautic Error: ${JSON.stringify(err)}`); throw new Error(`Mautic Error: ${JSON.stringify(err)}`);
} }
@ -218,12 +231,22 @@ export class Mautic implements INodeType {
} }
} }
} }
if (Array.isArray(responseData)) { if (Array.isArray(responseData)) {
if (options.rawData !== true) {
// @ts-ignore
responseData = responseData.map(item => item.fields.all);
}
returnData.push.apply(returnData, responseData as IDataObject[]); returnData.push.apply(returnData, responseData as IDataObject[]);
} else { } else {
if (options.rawData !== true) {
// @ts-ignore
responseData = responseData.fields.all;
}
returnData.push(responseData as IDataObject); returnData.push(responseData as IDataObject);
} }
} }
return [this.helpers.returnJsonArray(returnData)]; return [this.helpers.returnJsonArray(returnData)];
} }
} }

View file

@ -62,14 +62,14 @@ export class MauticTrigger implements INodeType {
displayName: 'Events Order', displayName: 'Events Order',
name: 'eventsOrder', name: 'eventsOrder',
type: 'options', type: 'options',
default: '', default: 'ASC',
options: [ options: [
{ {
name: 'Asc', name: 'ASC',
value: 'ASC', value: 'ASC',
}, },
{ {
name: 'Desc', name: 'DESC',
value: 'DESC', value: 'DESC',
}, },
], ],
@ -122,12 +122,12 @@ export class MauticTrigger implements INodeType {
const urlParts = urlParse(webhookUrl); const urlParts = urlParse(webhookUrl);
const body: IDataObject = { const body: IDataObject = {
name: `n8n-webhook:${urlParts.path}`, name: `n8n-webhook:${urlParts.path}`,
description: `n8n webhook`, description: 'n8n webhook',
webhookUrl: webhookUrl, webhookUrl,
triggers: events, triggers: events,
eventsOrderbyDir: eventsOrder, eventsOrderbyDir: eventsOrder,
isPublished: true, isPublished: true,
} };
const { hook } = await mauticApiRequest.call(this, 'POST', '/hooks/new', body); const { hook } = await mauticApiRequest.call(this, 'POST', '/hooks/new', body);
webhookData.webhookId = hook.id; webhookData.webhookId = hook.id;
return true; return true;

View file

@ -208,6 +208,7 @@
"dependencies": { "dependencies": {
"aws4": "^1.8.0", "aws4": "^1.8.0",
"basic-auth": "^2.0.1", "basic-auth": "^2.0.1",
"camel-case": "^4.1.1",
"cheerio": "^1.0.0-rc.3", "cheerio": "^1.0.0-rc.3",
"cron": "^1.7.2", "cron": "^1.7.2",
"eventsource": "^1.0.7", "eventsource": "^1.0.7",