2022-08-17 08:50:24 -07:00
|
|
|
import { IExecuteFunctions } from 'n8n-core';
|
2020-10-28 15:03:29 -07:00
|
|
|
|
|
|
|
import {
|
|
|
|
IDataObject,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodePropertyOptions,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { mailerliteApiRequest, mailerliteApiRequestAllItems } from './GenericFunctions';
|
2020-10-28 15:03:29 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { subscriberFields, subscriberOperations } from './SubscriberDescription';
|
2020-10-28 15:03:29 -07:00
|
|
|
|
|
|
|
export class MailerLite implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'MailerLite',
|
2020-10-28 15:04:12 -07:00
|
|
|
name: 'mailerLite',
|
2022-06-20 07:54:01 -07:00
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-class-description-icon-not-svg
|
2020-10-28 15:03:29 -07:00
|
|
|
icon: 'file:mailerLite.png',
|
|
|
|
group: ['input'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
2021-07-03 05:40:16 -07:00
|
|
|
description: 'Consume Mailer Lite API',
|
2020-10-28 15:03:29 -07:00
|
|
|
defaults: {
|
|
|
|
name: 'MailerLite',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'mailerLiteApi',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2020-10-28 15:03:29 -07:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Subscriber',
|
|
|
|
value: 'subscriber',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'subscriber',
|
|
|
|
},
|
|
|
|
...subscriberOperations,
|
|
|
|
...subscriberFields,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
methods = {
|
|
|
|
loadOptions: {
|
|
|
|
// Get all the available custom fields to display them to user so that he can
|
|
|
|
// select them easily
|
|
|
|
async getCustomFields(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
const fields = await mailerliteApiRequest.call(this, 'GET', '/fields');
|
|
|
|
for (const field of fields) {
|
|
|
|
returnData.push({
|
|
|
|
name: field.key,
|
|
|
|
value: field.key,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
|
|
|
const returnData: IDataObject[] = [];
|
2022-04-22 09:29:51 -07:00
|
|
|
const length = items.length;
|
2020-10-28 15:03:29 -07:00
|
|
|
const 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++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
if (resource === 'subscriber') {
|
|
|
|
//https://developers.mailerlite.com/reference#create-a-subscriber
|
|
|
|
if (operation === 'create') {
|
|
|
|
const email = this.getNodeParameter('email', i) as string;
|
2020-10-28 15:03:29 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
2020-10-28 15:03:29 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const body: IDataObject = {
|
|
|
|
email,
|
|
|
|
fields: [],
|
|
|
|
};
|
2020-10-28 15:03:29 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
Object.assign(body, additionalFields);
|
2020-10-28 15:03:29 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.customFieldsUi) {
|
2022-08-17 08:50:24 -07:00
|
|
|
const customFieldsValues = (additionalFields.customFieldsUi as IDataObject)
|
|
|
|
.customFieldsValues as IDataObject[];
|
2020-10-28 15:03:29 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (customFieldsValues) {
|
|
|
|
const fields = {};
|
2020-10-28 15:03:29 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
for (const customFieldValue of customFieldsValues) {
|
|
|
|
//@ts-ignore
|
|
|
|
fields[customFieldValue.fieldId] = customFieldValue.value;
|
|
|
|
}
|
2020-10-28 15:03:29 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
body.fields = fields;
|
|
|
|
delete body.customFieldsUi;
|
2020-10-28 15:03:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await mailerliteApiRequest.call(this, 'POST', '/subscribers', body);
|
|
|
|
}
|
|
|
|
//https://developers.mailerlite.com/reference#single-subscriber
|
|
|
|
if (operation === 'get') {
|
|
|
|
const subscriberId = this.getNodeParameter('subscriberId', i) as string;
|
2020-10-28 15:03:29 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await mailerliteApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/subscribers/${subscriberId}`,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
//https://developers.mailerlite.com/reference#subscribers
|
|
|
|
if (operation === 'getAll') {
|
|
|
|
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
2020-10-28 15:03:29 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const filters = this.getNodeParameter('filters', i) as IDataObject;
|
2020-10-28 15:03:29 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
Object.assign(qs, filters);
|
2020-10-28 15:03:29 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (returnAll) {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await mailerliteApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/subscribers`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
|
|
|
qs.limit = this.getNodeParameter('limit', i) as number;
|
2020-10-28 15:03:29 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await mailerliteApiRequest.call(this, 'GET', `/subscribers`, {}, qs);
|
|
|
|
}
|
2020-10-28 15:03:29 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://developers.mailerlite.com/reference#update-subscriber
|
|
|
|
if (operation === 'update') {
|
|
|
|
const subscriberId = this.getNodeParameter('subscriberId', i) as string;
|
2020-10-28 15:03:29 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
|
2020-10-28 15:03:29 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const body: IDataObject = {};
|
2020-10-28 15:03:29 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
Object.assign(body, updateFields);
|
2020-10-28 15:03:29 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (updateFields.customFieldsUi) {
|
2022-08-17 08:50:24 -07:00
|
|
|
const customFieldsValues = (updateFields.customFieldsUi as IDataObject)
|
|
|
|
.customFieldsValues as IDataObject[];
|
2020-10-28 15:03:29 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (customFieldsValues) {
|
|
|
|
const fields = {};
|
2020-10-28 15:03:29 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
for (const customFieldValue of customFieldsValues) {
|
|
|
|
//@ts-ignore
|
|
|
|
fields[customFieldValue.fieldId] = customFieldValue.value;
|
|
|
|
}
|
2020-10-28 15:03:29 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
body.fields = fields;
|
|
|
|
delete body.customFieldsUi;
|
|
|
|
}
|
2020-10-28 15:03:29 -07:00
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await mailerliteApiRequest.call(
|
|
|
|
this,
|
|
|
|
'PUT',
|
|
|
|
`/subscribers/${subscriberId}`,
|
|
|
|
body,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
|
|
|
returnData.push({ error: error.message });
|
|
|
|
continue;
|
2020-10-28 15:03:29 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
throw error;
|
2020-10-28 15:03:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Array.isArray(responseData)) {
|
|
|
|
returnData.push.apply(returnData, responseData as IDataObject[]);
|
|
|
|
} else if (responseData !== undefined) {
|
|
|
|
returnData.push(responseData as IDataObject);
|
|
|
|
}
|
|
|
|
return [this.helpers.returnJsonArray(returnData)];
|
|
|
|
}
|
|
|
|
}
|