mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-10 22:54:05 -08:00
d6239d5bfb
* Update Compression node * Update Crypto node * Update DateTime node * Update EditImage node * Update EmailSend node * Update ExecuteWorkflow node * Update FTP node * Update Function node * Update FunctionItem node * Update ExecuteCommand node * Update OpenWeatherMap node * Update ReadBinaryFile node * Update ReadPdf node * Update RssFeedRead node & add URL validation * Update SpreadsheetFile node * Update Switch node * Update WriteBinaryFile node * Update Xml node * Update ActiveCampaign node * Update Airtable node * Update ApiTemplateIo node * Update Asana node * Update AwsLambda node * Update AwsSns node * Update AwsComprehend node * Update AwsRekognition node * Update AwsS3 node * Fix Error item * Update AwsSes node * Update AwsSqs node * Update Amqp node * Update Bitly node * Update Box node * Update Brandfetch node * Update CircleCi node * Update Clearbit node * Update ClickUp node * Update Cockpit node * Update CoinGecko node * Update Contentful node * Update ConvertKit node * Update Cortex node * Update CustomerIo node * Update DeepL node * Update Demio node * Update Disqus node * Update Drift node * Update Dropbox node * Update GetResponse node * Refactor & Update Ghost node * Update Github node * Update Gitlab node * Update GoogleAnalytics node * Update GoogleBooks node * Update GoogleCalendar node * Update GoogleDrive node * Update Gmail node * Update GoogleSheets node * Update GoogleSlides node * Update GoogleTasks node * Update Gotify node * Update GraphQL node * Update HackerNews node * Update Harvest node * Update HtmlExtract node * Update Hubspot node * Update Hunter node * Update Intercom node * Update Kafka node * Refactor & update Line node * Update LinkedIn node * Update Mailchimp node * Update Mandrill node * Update Matrix node * Update Mautic node * Update Medium node * Update MessageBird node * Update Mindee node * Update Mocean node * Update MondayCom node * Update MicrosoftExcel node * Update MicrosoftOneDrive node * Update MicrosoftOutlook node * Update Affinity node * Update Chargebee node * Update Discourse node * Update Freshdesk node * Update YouTube node * Update InvoiceNinja node * Update MailerLite node * Update Mailgun node * Update Mailjet node * Update Mattermost node * Update Nasa node * Update NextCloud node * Update OpenThesaurus node * Update Orbit node * Update PagerDuty node * Update PayPal node * Update Peekalink node * Update Phantombuster node * Update PostHog node * Update ProfitWell node * Refactor & Update Pushbullet node * Update QuickBooks node * Update Raindrop node * Update Reddit node * Update Rocketchat node * Update S3 node * Update Salesforce node * Update SendGrid node * Update SentryIo node * Update Shopify node * Update Signl4 node * Update Slack node * Update Spontit node * Update Spotify node * Update Storyblok node * Refactor & Update Strapi node * Refactor & Update Strava node * Update Taiga node * Refactor & update Tapfiliate node * Update Telegram node * Update TheHive node * Update Todoist node * Update TravisCi node * Update Trello node * Update Twilio node * Update Twist node * Update Twitter node * Update Uplead node * Update UProc node * Update Vero node * Update Webflow node * Update Wekan node * Update Wordpress node * Update Xero node * Update Yourls node * Update Zendesk node * Update ZohoCrm node * Refactor & Update Zoom node * Update Zulip node * Update Clockify node * Update MongoDb node * Update MySql node * Update MicrosoftTeams node * Update Stackby node * Refactor Discourse node * Support corner-case in Github node update * Support corner-case in Gitlab node update * Refactor & Update GoogleContacts node * Refactor Mindee node * Update Coda node * Lint fixes * Update Beeminder node * Update Google Firebase RealtimeDatabase node * Update HelpScout node * Update Mailcheck node * Update Paddle node * Update Pipedrive node * Update Pushover node * Update Segment node * Refactor & Update Vonage node * Added new conditions to warnings on execute batch cmd * Added keep only properties flag * Fixed code for keep only props * Added dependencies for image editing Co-authored-by: dali <servfrdali@yahoo.fr>
630 lines
22 KiB
TypeScript
630 lines
22 KiB
TypeScript
import {
|
|
IExecuteFunctions,
|
|
} from 'n8n-core';
|
|
|
|
import {
|
|
IDataObject,
|
|
ILoadOptionsFunctions,
|
|
INodeExecutionData,
|
|
INodePropertyOptions,
|
|
INodeType,
|
|
INodeTypeDescription,
|
|
NodeApiError,
|
|
NodeOperationError,
|
|
} from 'n8n-workflow';
|
|
|
|
import {
|
|
mauticApiRequest,
|
|
mauticApiRequestAllItems,
|
|
validateJSON,
|
|
} from './GenericFunctions';
|
|
|
|
import {
|
|
contactFields,
|
|
contactOperations,
|
|
} from './ContactDescription';
|
|
|
|
import {
|
|
companyFields,
|
|
companyOperations,
|
|
} from './CompanyDescription';
|
|
|
|
import {
|
|
companyContactFields,
|
|
companyContactOperations,
|
|
} from './CompanyContactDescription';
|
|
|
|
import {
|
|
contactSegmentFields,
|
|
contactSegmentOperations,
|
|
} from './ContactSegmentDescription';
|
|
|
|
import {
|
|
snakeCase,
|
|
} from 'change-case';
|
|
|
|
export class Mautic implements INodeType {
|
|
description: INodeTypeDescription = {
|
|
displayName: 'Mautic',
|
|
name: 'mautic',
|
|
icon: 'file:mautic.svg',
|
|
group: ['output'],
|
|
version: 1,
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
|
description: 'Consume Mautic API',
|
|
defaults: {
|
|
name: 'Mautic',
|
|
color: '#52619b',
|
|
},
|
|
inputs: ['main'],
|
|
outputs: ['main'],
|
|
credentials: [
|
|
{
|
|
name: 'mauticApi',
|
|
required: true,
|
|
displayOptions: {
|
|
show: {
|
|
authentication: [
|
|
'credentials',
|
|
],
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: 'mauticOAuth2Api',
|
|
required: true,
|
|
displayOptions: {
|
|
show: {
|
|
authentication: [
|
|
'oAuth2',
|
|
],
|
|
},
|
|
},
|
|
},
|
|
],
|
|
properties: [
|
|
{
|
|
displayName: 'Authentication',
|
|
name: 'authentication',
|
|
type: 'options',
|
|
options: [
|
|
{
|
|
name: 'Credentials',
|
|
value: 'credentials',
|
|
},
|
|
{
|
|
name: 'OAuth2',
|
|
value: 'oAuth2',
|
|
},
|
|
],
|
|
default: 'credentials',
|
|
},
|
|
{
|
|
displayName: 'Resource',
|
|
name: 'resource',
|
|
type: 'options',
|
|
options: [
|
|
{
|
|
name: 'Company',
|
|
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 Segment',
|
|
value: 'contactSegment',
|
|
description: 'Add/remove contacts to/from a segment',
|
|
},
|
|
],
|
|
default: 'contact',
|
|
description: 'Resource to consume.',
|
|
},
|
|
...companyOperations,
|
|
...companyFields,
|
|
...contactOperations,
|
|
...contactFields,
|
|
...contactSegmentOperations,
|
|
...contactSegmentFields,
|
|
...companyContactOperations,
|
|
...companyContactFields,
|
|
],
|
|
};
|
|
|
|
methods = {
|
|
loadOptions: {
|
|
// Get all the available companies to display them to user so that he can
|
|
// select them easily
|
|
async getCompanies(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
const returnData: INodePropertyOptions[] = [];
|
|
const companies = await mauticApiRequestAllItems.call(this, 'companies', 'GET', '/companies');
|
|
for (const company of companies) {
|
|
returnData.push({
|
|
name: company.fields.all.companyname,
|
|
value: company.fields.all.companyname,
|
|
});
|
|
}
|
|
return returnData;
|
|
},
|
|
// Get all the available tags to display them to user so that he can
|
|
// select them easily
|
|
async getTags(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
const returnData: INodePropertyOptions[] = [];
|
|
const tags = await mauticApiRequestAllItems.call(this, 'tags', 'GET', '/tags');
|
|
for (const tag of tags) {
|
|
returnData.push({
|
|
name: tag.tag,
|
|
value: tag.tag,
|
|
});
|
|
}
|
|
return returnData;
|
|
},
|
|
// Get all the available stages to display them to user so that he can
|
|
// select them easily
|
|
async getStages(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
const returnData: INodePropertyOptions[] = [];
|
|
const stages = await mauticApiRequestAllItems.call(this, 'stages', 'GET', '/stages');
|
|
for (const stage of stages) {
|
|
returnData.push({
|
|
name: stage.name,
|
|
value: stage.id,
|
|
});
|
|
}
|
|
return returnData;
|
|
},
|
|
// Get all the available company fields to display them to user so that he can
|
|
// select them easily
|
|
async getCompanyFields(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
const returnData: INodePropertyOptions[] = [];
|
|
const fields = await mauticApiRequestAllItems.call(this, 'fields', 'GET', '/fields/company');
|
|
for (const field of fields) {
|
|
returnData.push({
|
|
name: field.label,
|
|
value: field.alias,
|
|
});
|
|
}
|
|
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[]> {
|
|
const returnData: INodePropertyOptions[] = [];
|
|
const fields = await mauticApiRequestAllItems.call(this, 'fields', 'GET', '/fields/contact');
|
|
for (const field of fields) {
|
|
returnData.push({
|
|
name: field.label,
|
|
value: field.alias,
|
|
});
|
|
}
|
|
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;
|
|
},
|
|
},
|
|
};
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
const items = this.getInputData();
|
|
const returnData: IDataObject[] = [];
|
|
const length = items.length as unknown as number;
|
|
let qs: IDataObject;
|
|
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++) {
|
|
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 body: IDataObject = {
|
|
companyname: name,
|
|
};
|
|
Object.assign(body, additionalFields);
|
|
responseData = await mauticApiRequest.call(this, 'POST', '/companies/new', body);
|
|
responseData = responseData.company;
|
|
if (simple === true) {
|
|
responseData = responseData.fields.all;
|
|
}
|
|
}
|
|
//https://developer.mautic.org/#edit-company
|
|
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;
|
|
}
|
|
responseData = await mauticApiRequest.call(this, 'PATCH', `/companies/${companyId}/edit`, body);
|
|
responseData = responseData.company;
|
|
if (simple === true) {
|
|
responseData = responseData.fields.all;
|
|
}
|
|
}
|
|
//https://developer.mautic.org/#get-company
|
|
if (operation === 'get') {
|
|
const companyId = this.getNodeParameter('companyId', i) as string;
|
|
const simple = this.getNodeParameter('simple', i) as boolean;
|
|
responseData = await mauticApiRequest.call(this, 'GET', `/companies/${companyId}`);
|
|
responseData = responseData.company;
|
|
if (simple === true) {
|
|
responseData = responseData.fields.all;
|
|
}
|
|
}
|
|
//https://developer.mautic.org/#list-contact-companies
|
|
if (operation === 'getAll') {
|
|
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
|
const simple = this.getNodeParameter('simple', i) as boolean;
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
|
qs = Object.assign(qs, additionalFields);
|
|
if (returnAll === true) {
|
|
responseData = await mauticApiRequestAllItems.call(this, 'companies', 'GET', '/companies', {}, qs);
|
|
} else {
|
|
qs.limit = this.getNodeParameter('limit', i) as number;
|
|
qs.start = 0;
|
|
responseData = await mauticApiRequest.call(this, 'GET', '/companies', {}, qs);
|
|
if (responseData.errors) {
|
|
throw new NodeApiError(this.getNode(), responseData);
|
|
}
|
|
responseData = responseData.companies;
|
|
responseData = Object.values(responseData);
|
|
}
|
|
if (simple === true) {
|
|
//@ts-ignore
|
|
responseData = responseData.map(item => item.fields.all);
|
|
}
|
|
}
|
|
//https://developer.mautic.org/#delete-company
|
|
if (operation === 'delete') {
|
|
const simple = this.getNodeParameter('simple', i) as boolean;
|
|
const companyId = this.getNodeParameter('companyId', i) as string;
|
|
responseData = await mauticApiRequest.call(this, 'DELETE', `/companies/${companyId}/delete`);
|
|
responseData = responseData.company;
|
|
if (simple === true) {
|
|
responseData = responseData.fields.all;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (resource === 'contact') {
|
|
//https://developer.mautic.org/?php#create-contact
|
|
if (operation === 'create') {
|
|
const options = this.getNodeParameter('options', i) as IDataObject;
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
|
const jsonActive = this.getNodeParameter('jsonParameters', i) as boolean;
|
|
let body: IDataObject = {};
|
|
if (!jsonActive) {
|
|
body.email = this.getNodeParameter('email', i) as string;
|
|
body.firstname = this.getNodeParameter('firstName', i) as string;
|
|
body.lastname = this.getNodeParameter('lastName', i) as string;
|
|
body.company = this.getNodeParameter('company', i) as string;
|
|
body.position = this.getNodeParameter('position', i) as string;
|
|
body.title = this.getNodeParameter('title', i) as string;
|
|
} else {
|
|
const json = validateJSON(this.getNodeParameter('bodyJson', i) as string);
|
|
if (json !== undefined) {
|
|
body = { ...json };
|
|
} else {
|
|
throw new NodeOperationError(this.getNode(), 'Invalid JSON');
|
|
}
|
|
}
|
|
if (additionalFields.ipAddress) {
|
|
body.ipAddress = additionalFields.ipAddress as string;
|
|
}
|
|
if (additionalFields.lastActive) {
|
|
body.lastActive = additionalFields.lastActive as string;
|
|
}
|
|
if (additionalFields.ownerId) {
|
|
body.ownerId = additionalFields.ownerId as string;
|
|
}
|
|
if (additionalFields.addressUi) {
|
|
const addressValues = (additionalFields.addressUi as IDataObject).addressValues as IDataObject;
|
|
if (addressValues) {
|
|
body.address1 = addressValues.address1 as string;
|
|
body.address2 = addressValues.address2 as string;
|
|
body.city = addressValues.city as string;
|
|
body.state = addressValues.state as string;
|
|
body.country = addressValues.country as string;
|
|
body.zipcode = addressValues.zipCode as string;
|
|
}
|
|
}
|
|
if (additionalFields.socialMediaUi) {
|
|
const socialMediaValues = (additionalFields.socialMediaUi as IDataObject).socialMediaValues as IDataObject;
|
|
if (socialMediaValues) {
|
|
body.facebook = socialMediaValues.facebook as string;
|
|
body.foursquare = socialMediaValues.foursquare as string;
|
|
body.instagram = socialMediaValues.instagram as string;
|
|
body.linkedin = socialMediaValues.linkedIn as string;
|
|
body.skype = socialMediaValues.skype as string;
|
|
body.twitter = socialMediaValues.twitter as string;
|
|
}
|
|
}
|
|
if (additionalFields.customFieldsUi) {
|
|
const customFields = (additionalFields.customFieldsUi as IDataObject).customFieldValues as IDataObject[];
|
|
if (customFields) {
|
|
const data = customFields.reduce((obj, value) => Object.assign(obj, { [`${value.fieldId}`]: value.fieldValue }), {});
|
|
Object.assign(body, data);
|
|
}
|
|
}
|
|
if (additionalFields.b2bOrb2c) {
|
|
body.b2b_or_b2c = additionalFields.b2bOrb2c as string;
|
|
}
|
|
if (additionalFields.crmId) {
|
|
body.crm_id = additionalFields.crmId as string;
|
|
}
|
|
if (additionalFields.fax) {
|
|
body.fax = additionalFields.fax as string;
|
|
}
|
|
if (additionalFields.hasPurchased) {
|
|
body.haspurchased = additionalFields.hasPurchased as boolean;
|
|
}
|
|
if (additionalFields.mobile) {
|
|
body.mobile = additionalFields.mobile as string;
|
|
}
|
|
if (additionalFields.phone) {
|
|
body.phone = additionalFields.phone as string;
|
|
}
|
|
if (additionalFields.prospectOrCustomer) {
|
|
body.prospect_or_customer = additionalFields.prospectOrCustomer as string;
|
|
}
|
|
if (additionalFields.sandbox) {
|
|
body.sandbox = additionalFields.sandbox as boolean;
|
|
}
|
|
if (additionalFields.stage) {
|
|
body.stage = additionalFields.stage as string;
|
|
}
|
|
if (additionalFields.tags) {
|
|
body.tags = additionalFields.tags as string;
|
|
}
|
|
if (additionalFields.website) {
|
|
body.website = additionalFields.website as string;
|
|
}
|
|
responseData = await mauticApiRequest.call(this, 'POST', '/contacts/new', body);
|
|
responseData = [responseData.contact];
|
|
if (options.rawData === false) {
|
|
responseData = responseData.map(item => item.fields.all);
|
|
}
|
|
}
|
|
//https://developer.mautic.org/?php#edit-contact
|
|
if (operation === 'update') {
|
|
const options = this.getNodeParameter('options', i) as IDataObject;
|
|
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
|
|
const contactId = this.getNodeParameter('contactId', i) as string;
|
|
let body: IDataObject = {};
|
|
if (updateFields.email) {
|
|
body.email = updateFields.email as string;
|
|
}
|
|
if (updateFields.firstName) {
|
|
body.firstname = updateFields.firstName as string;
|
|
}
|
|
if (updateFields.lastName) {
|
|
body.lastname = updateFields.lastName as string;
|
|
}
|
|
if (updateFields.company) {
|
|
body.company = updateFields.company as string;
|
|
}
|
|
if (updateFields.position) {
|
|
body.position = updateFields.position as string;
|
|
}
|
|
if (updateFields.title) {
|
|
body.title = updateFields.title as string;
|
|
}
|
|
if (updateFields.bodyJson) {
|
|
const json = validateJSON(updateFields.bodyJson as string);
|
|
if (json !== undefined) {
|
|
body = { ...json };
|
|
} else {
|
|
throw new NodeOperationError(this.getNode(), 'Invalid JSON');
|
|
}
|
|
}
|
|
if (updateFields.ipAddress) {
|
|
body.ipAddress = updateFields.ipAddress as string;
|
|
}
|
|
if (updateFields.lastActive) {
|
|
body.lastActive = updateFields.lastActive as string;
|
|
}
|
|
if (updateFields.ownerId) {
|
|
body.ownerId = updateFields.ownerId as string;
|
|
}
|
|
if (updateFields.addressUi) {
|
|
const addressValues = (updateFields.addressUi as IDataObject).addressValues as IDataObject;
|
|
if (addressValues) {
|
|
body.address1 = addressValues.address1 as string;
|
|
body.address2 = addressValues.address2 as string;
|
|
body.city = addressValues.city as string;
|
|
body.state = addressValues.state as string;
|
|
body.country = addressValues.country as string;
|
|
body.zipcode = addressValues.zipCode as string;
|
|
}
|
|
}
|
|
if (updateFields.socialMediaUi) {
|
|
const socialMediaValues = (updateFields.socialMediaUi as IDataObject).socialMediaValues as IDataObject;
|
|
if (socialMediaValues) {
|
|
body.facebook = socialMediaValues.facebook as string;
|
|
body.foursquare = socialMediaValues.foursquare as string;
|
|
body.instagram = socialMediaValues.instagram as string;
|
|
body.linkedin = socialMediaValues.linkedIn as string;
|
|
body.skype = socialMediaValues.skype as string;
|
|
body.twitter = socialMediaValues.twitter as string;
|
|
}
|
|
}
|
|
if (updateFields.customFieldsUi) {
|
|
const customFields = (updateFields.customFieldsUi as IDataObject).customFieldValues as IDataObject[];
|
|
if (customFields) {
|
|
const data = customFields.reduce((obj, value) => Object.assign(obj, { [`${value.fieldId}`]: value.fieldValue }), {});
|
|
Object.assign(body, data);
|
|
}
|
|
}
|
|
if (updateFields.b2bOrb2c) {
|
|
body.b2b_or_b2c = updateFields.b2bOrb2c as string;
|
|
}
|
|
if (updateFields.crmId) {
|
|
body.crm_id = updateFields.crmId as string;
|
|
}
|
|
if (updateFields.fax) {
|
|
body.fax = updateFields.fax as string;
|
|
}
|
|
if (updateFields.hasPurchased) {
|
|
body.haspurchased = updateFields.hasPurchased as boolean;
|
|
}
|
|
if (updateFields.mobile) {
|
|
body.mobile = updateFields.mobile as string;
|
|
}
|
|
if (updateFields.phone) {
|
|
body.phone = updateFields.phone as string;
|
|
}
|
|
if (updateFields.prospectOrCustomer) {
|
|
body.prospect_or_customer = updateFields.prospectOrCustomer as string;
|
|
}
|
|
if (updateFields.sandbox) {
|
|
body.sandbox = updateFields.sandbox as boolean;
|
|
}
|
|
if (updateFields.stage) {
|
|
body.stage = updateFields.stage as string;
|
|
}
|
|
if (updateFields.tags) {
|
|
body.tags = updateFields.tags as string;
|
|
}
|
|
if (updateFields.website) {
|
|
body.website = updateFields.website as string;
|
|
}
|
|
responseData = await mauticApiRequest.call(this, 'PATCH', `/contacts/${contactId}/edit`, body);
|
|
responseData = [responseData.contact];
|
|
if (options.rawData === false) {
|
|
responseData = responseData.map(item => item.fields.all);
|
|
}
|
|
}
|
|
//https://developer.mautic.org/?php#get-contact
|
|
if (operation === 'get') {
|
|
const options = this.getNodeParameter('options', i) as IDataObject;
|
|
const contactId = this.getNodeParameter('contactId', i) as string;
|
|
responseData = await mauticApiRequest.call(this, 'GET', `/contacts/${contactId}`);
|
|
responseData = [responseData.contact];
|
|
if (options.rawData === false) {
|
|
responseData = responseData.map(item => item.fields.all);
|
|
}
|
|
}
|
|
//https://developer.mautic.org/?php#list-contacts
|
|
if (operation === 'getAll') {
|
|
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
|
const options = this.getNodeParameter('options', i) as IDataObject;
|
|
qs = Object.assign(qs, options);
|
|
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);
|
|
}
|
|
|
|
if (returnAll === true) {
|
|
responseData = await mauticApiRequestAllItems.call(this, 'contacts', 'GET', '/contacts', {}, qs);
|
|
} else {
|
|
qs.limit = this.getNodeParameter('limit', i) as number;
|
|
qs.start = 0;
|
|
responseData = await mauticApiRequest.call(this, 'GET', '/contacts', {}, qs);
|
|
if (responseData.errors) {
|
|
throw new NodeApiError(this.getNode(), responseData);
|
|
}
|
|
responseData = responseData.contacts;
|
|
responseData = Object.values(responseData);
|
|
}
|
|
if (options.rawData === false) {
|
|
//@ts-ignore
|
|
responseData = responseData.map(item => item.fields.all);
|
|
}
|
|
}
|
|
//https://developer.mautic.org/?php#delete-contact
|
|
if (operation === 'delete') {
|
|
const options = this.getNodeParameter('options', i) as IDataObject;
|
|
const contactId = this.getNodeParameter('contactId', i) as string;
|
|
responseData = await mauticApiRequest.call(this, 'DELETE', `/contacts/${contactId}/delete`);
|
|
responseData = [responseData.contact];
|
|
if (options.rawData === false) {
|
|
responseData = responseData.map(item => item.fields.all);
|
|
}
|
|
}
|
|
}
|
|
|
|
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;
|
|
const companyId = this.getNodeParameter('companyId', i) as string;
|
|
responseData = await mauticApiRequest.call(this, 'POST', `/companies/${companyId}/contact/${contactId}/add`, {});
|
|
// responseData = responseData.company;
|
|
// if (simple === true) {
|
|
// responseData = responseData.fields.all;
|
|
// }
|
|
}
|
|
//https://developer.mautic.org/#remove-contact-from-a-company
|
|
if (operation === 'remove') {
|
|
const contactId = this.getNodeParameter('contactId', i) as string;
|
|
const companyId = this.getNodeParameter('companyId', i) as string;
|
|
responseData = await mauticApiRequest.call(this, 'POST', `/companies/${companyId}/contact/${contactId}/remove`, {});
|
|
// responseData = responseData.company;
|
|
// if (simple === true) {
|
|
// responseData = responseData.fields.all;
|
|
// }
|
|
}
|
|
}
|
|
|
|
if (Array.isArray(responseData)) {
|
|
returnData.push.apply(returnData, responseData as IDataObject[]);
|
|
} else {
|
|
returnData.push(responseData as IDataObject);
|
|
}
|
|
} catch (error) {
|
|
if (this.continueOnFail()) {
|
|
returnData.push({ error: error.message });
|
|
continue;
|
|
}
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
return [this.helpers.returnJsonArray(returnData)];
|
|
}
|
|
}
|