2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IExecuteFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
2020-07-02 13:41:59 -07:00
|
|
|
IDataObject,
|
|
|
|
INodeExecutionData,
|
2020-08-25 14:16:38 -07:00
|
|
|
INodePropertyOptions,
|
2020-07-02 13:41:59 -07:00
|
|
|
INodeType,
|
2020-10-01 05:01:39 -07:00
|
|
|
INodeTypeDescription,
|
2020-07-02 13:41:59 -07:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { convertKitApiRequest } from './GenericFunctions';
|
2020-07-02 13:41:59 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { customFieldFields, customFieldOperations } from './CustomFieldDescription';
|
2020-07-02 13:41:59 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { formFields, formOperations } from './FormDescription';
|
2020-07-02 13:41:59 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { sequenceFields, sequenceOperations } from './SequenceDescription';
|
2020-07-02 13:41:59 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { tagFields, tagOperations } from './TagDescription';
|
2020-07-02 13:41:59 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { tagSubscriberFields, tagSubscriberOperations } from './TagSubscriberDescription';
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2020-07-02 13:41:59 -07:00
|
|
|
export class ConvertKit implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'ConvertKit',
|
|
|
|
name: 'convertKit',
|
2021-06-12 12:00:37 -07:00
|
|
|
icon: 'file:convertKit.svg',
|
2020-07-02 13:41:59 -07:00
|
|
|
group: ['input'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
2021-07-03 05:40:16 -07:00
|
|
|
description: 'Consume ConvertKit API',
|
2020-07-02 13:41:59 -07:00
|
|
|
defaults: {
|
|
|
|
name: 'ConvertKit',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'convertKitApi',
|
|
|
|
required: true,
|
2020-10-22 06:46:03 -07:00
|
|
|
},
|
2020-07-02 13:41:59 -07:00
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2020-07-02 13:41:59 -07:00
|
|
|
options: [
|
|
|
|
{
|
2020-08-05 18:55:53 -07:00
|
|
|
name: 'Custom Field',
|
|
|
|
value: 'customField',
|
2020-07-02 13:41:59 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Form',
|
|
|
|
value: 'form',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Sequence',
|
|
|
|
value: 'sequence',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Tag',
|
|
|
|
value: 'tag',
|
|
|
|
},
|
2020-08-05 18:55:53 -07:00
|
|
|
{
|
|
|
|
name: 'Tag Subscriber',
|
|
|
|
value: 'tagSubscriber',
|
|
|
|
},
|
2020-07-02 13:41:59 -07:00
|
|
|
],
|
2020-08-25 14:16:38 -07:00
|
|
|
default: 'form',
|
2020-07-02 13:41:59 -07:00
|
|
|
},
|
|
|
|
//--------------------
|
|
|
|
// Field Description
|
|
|
|
//--------------------
|
2020-08-05 18:55:53 -07:00
|
|
|
...customFieldOperations,
|
|
|
|
...customFieldFields,
|
2020-07-02 13:41:59 -07:00
|
|
|
//--------------------
|
|
|
|
// FormDescription
|
|
|
|
//--------------------
|
|
|
|
...formOperations,
|
|
|
|
...formFields,
|
|
|
|
//--------------------
|
|
|
|
// Sequence Description
|
|
|
|
//--------------------
|
|
|
|
...sequenceOperations,
|
|
|
|
...sequenceFields,
|
|
|
|
//--------------------
|
|
|
|
// Tag Description
|
|
|
|
//--------------------
|
|
|
|
...tagOperations,
|
|
|
|
...tagFields,
|
2020-08-05 18:55:53 -07:00
|
|
|
//--------------------
|
|
|
|
// Tag Subscriber Description
|
|
|
|
//--------------------
|
|
|
|
...tagSubscriberOperations,
|
|
|
|
...tagSubscriberFields,
|
2020-07-02 13:41:59 -07:00
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2020-08-05 18:55:53 -07:00
|
|
|
methods = {
|
|
|
|
loadOptions: {
|
|
|
|
// Get all the 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 convertKitApiRequest.call(this, 'GET', '/tags');
|
|
|
|
for (const tag of tags) {
|
|
|
|
const tagName = tag.name;
|
|
|
|
const tagId = tag.id;
|
|
|
|
returnData.push({
|
|
|
|
name: tagName,
|
|
|
|
value: tagId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
// Get all the forms to display them to user so that he can
|
|
|
|
// select them easily
|
|
|
|
async getForms(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
const { forms } = await convertKitApiRequest.call(this, 'GET', '/forms');
|
|
|
|
for (const form of forms) {
|
|
|
|
const formName = form.name;
|
|
|
|
const formId = form.id;
|
|
|
|
returnData.push({
|
|
|
|
name: formName,
|
|
|
|
value: formId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Get all the sequences to display them to user so that he can
|
|
|
|
// select them easily
|
|
|
|
async getSequences(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
const { courses } = await convertKitApiRequest.call(this, 'GET', '/sequences');
|
|
|
|
for (const course of courses) {
|
|
|
|
const courseName = course.name;
|
|
|
|
const courseId = course.id;
|
|
|
|
returnData.push({
|
|
|
|
name: courseName,
|
|
|
|
value: courseId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return returnData;
|
|
|
|
},
|
2020-10-22 06:46:03 -07:00
|
|
|
},
|
2020-08-05 18:55:53 -07:00
|
|
|
};
|
|
|
|
|
2020-07-02 13:41:59 -07:00
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
2022-08-30 08:55:33 -07:00
|
|
|
const returnData: INodeExecutionData[] = [];
|
2020-07-02 13:41:59 -07:00
|
|
|
|
|
|
|
const qs: IDataObject = {};
|
|
|
|
let responseData;
|
|
|
|
|
2022-12-02 03:53:59 -08:00
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2020-07-02 13:41:59 -07:00
|
|
|
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
if (resource === 'customField') {
|
|
|
|
if (operation === 'create') {
|
|
|
|
const label = this.getNodeParameter('label', i) as string;
|
2020-07-02 13:41:59 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
responseData = await convertKitApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
|
|
|
'/custom_fields',
|
|
|
|
{ label },
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
if (operation === 'delete') {
|
|
|
|
const id = this.getNodeParameter('id', i) as string;
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await convertKitApiRequest.call(this, 'DELETE', `/custom_fields/${id}`);
|
|
|
|
}
|
|
|
|
if (operation === 'get') {
|
|
|
|
const id = this.getNodeParameter('id', i) as string;
|
|
|
|
|
|
|
|
responseData = await convertKitApiRequest.call(this, 'GET', `/custom_fields/${id}`);
|
|
|
|
}
|
|
|
|
if (operation === 'getAll') {
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2022-12-29 03:20:43 -08:00
|
|
|
responseData = await convertKitApiRequest.call(this, 'GET', '/custom_fields');
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.custom_fields;
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (!returnAll) {
|
2022-11-18 06:26:22 -08:00
|
|
|
const limit = this.getNodeParameter('limit', i);
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.slice(0, limit);
|
|
|
|
}
|
2020-07-02 13:41:59 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'update') {
|
|
|
|
const id = this.getNodeParameter('id', i) as string;
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const label = this.getNodeParameter('label', i) as string;
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
responseData = await convertKitApiRequest.call(this, 'PUT', `/custom_fields/${id}`, {
|
|
|
|
label,
|
|
|
|
});
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = { success: true };
|
|
|
|
}
|
2020-08-05 18:55:53 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (resource === 'form') {
|
|
|
|
if (operation === 'addSubscriber') {
|
|
|
|
const email = this.getNodeParameter('email', i) as string;
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const formId = this.getNodeParameter('id', i) as string;
|
2020-07-02 13:41:59 -07:00
|
|
|
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2020-07-02 13:41:59 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const body: IDataObject = {
|
|
|
|
email,
|
|
|
|
};
|
2020-07-02 13:41:59 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.firstName) {
|
|
|
|
body.first_name = additionalFields.firstName as string;
|
|
|
|
}
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.tags) {
|
|
|
|
body.tags = additionalFields.tags as string[];
|
|
|
|
}
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.fieldsUi) {
|
2022-08-01 13:47:55 -07:00
|
|
|
const fieldValues = (additionalFields.fieldsUi as IDataObject)
|
|
|
|
.fieldsValues as IDataObject[];
|
2021-07-19 23:58:54 -07:00
|
|
|
if (fieldValues) {
|
|
|
|
body.fields = {};
|
|
|
|
for (const fieldValue of fieldValues) {
|
|
|
|
//@ts-ignore
|
|
|
|
body.fields[fieldValue.key] = fieldValue.value;
|
|
|
|
}
|
2020-08-05 18:55:53 -07:00
|
|
|
}
|
2020-07-02 13:41:59 -07:00
|
|
|
}
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
const { subscription } = await convertKitApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
|
|
|
`/forms/${formId}/subscribe`,
|
|
|
|
body,
|
|
|
|
);
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = subscription;
|
|
|
|
}
|
|
|
|
if (operation === 'getAll') {
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2022-12-29 03:20:43 -08:00
|
|
|
responseData = await convertKitApiRequest.call(this, 'GET', '/forms');
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.forms;
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (!returnAll) {
|
2022-11-18 06:26:22 -08:00
|
|
|
const limit = this.getNodeParameter('limit', i);
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.slice(0, limit);
|
|
|
|
}
|
2020-08-05 18:55:53 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'getSubscriptions') {
|
|
|
|
const formId = this.getNodeParameter('id', i) as string;
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.subscriberState) {
|
|
|
|
qs.subscriber_state = additionalFields.subscriberState as string;
|
|
|
|
}
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
responseData = await convertKitApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/forms/${formId}/subscriptions`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.subscriptions;
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (!returnAll) {
|
2022-11-18 06:26:22 -08:00
|
|
|
const limit = this.getNodeParameter('limit', i);
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.slice(0, limit);
|
|
|
|
}
|
2020-08-05 18:55:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (resource === 'sequence') {
|
|
|
|
if (operation === 'addSubscriber') {
|
|
|
|
const email = this.getNodeParameter('email', i) as string;
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const sequenceId = this.getNodeParameter('id', i) as string;
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const body: IDataObject = {
|
|
|
|
email,
|
|
|
|
};
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.firstName) {
|
|
|
|
body.first_name = additionalFields.firstName as string;
|
|
|
|
}
|
2020-07-02 13:41:59 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.tags) {
|
|
|
|
body.tags = additionalFields.tags as string[];
|
|
|
|
}
|
2020-07-02 13:41:59 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.fieldsUi) {
|
2022-08-01 13:47:55 -07:00
|
|
|
const fieldValues = (additionalFields.fieldsUi as IDataObject)
|
|
|
|
.fieldsValues as IDataObject[];
|
2021-07-19 23:58:54 -07:00
|
|
|
if (fieldValues) {
|
|
|
|
body.fields = {};
|
|
|
|
for (const fieldValue of fieldValues) {
|
|
|
|
//@ts-ignore
|
|
|
|
body.fields[fieldValue.key] = fieldValue.value;
|
|
|
|
}
|
2020-08-05 18:55:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
const { subscription } = await convertKitApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
|
|
|
`/sequences/${sequenceId}/subscribe`,
|
|
|
|
body,
|
|
|
|
);
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = subscription;
|
|
|
|
}
|
|
|
|
if (operation === 'getAll') {
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2022-12-29 03:20:43 -08:00
|
|
|
responseData = await convertKitApiRequest.call(this, 'GET', '/sequences');
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.courses;
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (!returnAll) {
|
2022-11-18 06:26:22 -08:00
|
|
|
const limit = this.getNodeParameter('limit', i);
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.slice(0, limit);
|
|
|
|
}
|
2020-07-02 13:41:59 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'getSubscriptions') {
|
|
|
|
const sequenceId = this.getNodeParameter('id', i) as string;
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.subscriberState) {
|
|
|
|
qs.subscriber_state = additionalFields.subscriberState as string;
|
|
|
|
}
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
responseData = await convertKitApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/sequences/${sequenceId}/subscriptions`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.subscriptions;
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (!returnAll) {
|
2022-11-18 06:26:22 -08:00
|
|
|
const limit = this.getNodeParameter('limit', i);
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.slice(0, limit);
|
|
|
|
}
|
2020-07-02 13:41:59 -07:00
|
|
|
}
|
2020-08-05 18:55:53 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (resource === 'tag') {
|
|
|
|
if (operation === 'create') {
|
2022-12-02 12:54:28 -08:00
|
|
|
const names = (this.getNodeParameter('name', i) as string)
|
|
|
|
.split(',')
|
|
|
|
.map((e) => ({ name: e }));
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const body: IDataObject = {
|
|
|
|
tag: names,
|
|
|
|
};
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await convertKitApiRequest.call(this, 'POST', '/tags', body);
|
|
|
|
}
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'getAll') {
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2022-12-29 03:20:43 -08:00
|
|
|
responseData = await convertKitApiRequest.call(this, 'GET', '/tags');
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.tags;
|
2020-07-02 13:41:59 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (!returnAll) {
|
2022-11-18 06:26:22 -08:00
|
|
|
const limit = this.getNodeParameter('limit', i);
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.slice(0, limit);
|
|
|
|
}
|
2020-07-02 13:41:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (resource === 'tagSubscriber') {
|
|
|
|
if (operation === 'add') {
|
|
|
|
const tagId = this.getNodeParameter('tagId', i) as string;
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const email = this.getNodeParameter('email', i) as string;
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const body: IDataObject = {
|
|
|
|
email,
|
|
|
|
};
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.firstName) {
|
|
|
|
body.first_name = additionalFields.firstName as string;
|
|
|
|
}
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.fieldsUi) {
|
2022-08-01 13:47:55 -07:00
|
|
|
const fieldValues = (additionalFields.fieldsUi as IDataObject)
|
|
|
|
.fieldsValues as IDataObject[];
|
2021-07-19 23:58:54 -07:00
|
|
|
if (fieldValues) {
|
|
|
|
body.fields = {};
|
|
|
|
for (const fieldValue of fieldValues) {
|
|
|
|
//@ts-ignore
|
|
|
|
body.fields[fieldValue.key] = fieldValue.value;
|
|
|
|
}
|
2020-08-05 18:55:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
const { subscription } = await convertKitApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
|
|
|
`/tags/${tagId}/subscribe`,
|
|
|
|
body,
|
|
|
|
);
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = subscription;
|
|
|
|
}
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'getAll') {
|
|
|
|
const tagId = this.getNodeParameter('tagId', i) as string;
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
responseData = await convertKitApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/tags/${tagId}/subscriptions`,
|
|
|
|
);
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.subscriptions;
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (!returnAll) {
|
2022-11-18 06:26:22 -08:00
|
|
|
const limit = this.getNodeParameter('limit', i);
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.slice(0, limit);
|
|
|
|
}
|
2020-08-05 18:55:53 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'delete') {
|
|
|
|
const tagId = this.getNodeParameter('tagId', i) as string;
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const email = this.getNodeParameter('email', i) as string;
|
2020-08-05 18:55:53 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
responseData = await convertKitApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
|
|
|
`/tags/${tagId}>/unsubscribe`,
|
|
|
|
{ email },
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2020-08-05 18:55:53 -07:00
|
|
|
}
|
2020-07-02 13:41:59 -07:00
|
|
|
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2023-02-27 19:39:43 -08:00
|
|
|
this.helpers.returnJsonArray(responseData as IDataObject[]),
|
2022-08-30 08:55:33 -07:00
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-08-30 08:55:33 -07:00
|
|
|
returnData.push({ error: error.message, json: {} });
|
2021-07-19 23:58:54 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
2020-07-02 13:41:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-30 08:55:33 -07:00
|
|
|
return this.prepareOutputData(returnData);
|
2020-07-02 13:41:59 -07:00
|
|
|
}
|
|
|
|
}
|