🔀 Merge branch 'RicardoE105-feature/mailchimp-extended'

This commit is contained in:
Jan Oberhauser 2020-07-10 12:31:07 +02:00
commit c08ff6cee3
2 changed files with 341 additions and 29 deletions

View file

@ -49,7 +49,6 @@ export async function mailchimpApiRequest(this: IHookFunctions | IExecuteFunctio
const datacenter = (credentials.apiKey as string).split('-').pop();
options.url = `https://${datacenter}.${host}${endpoint}`;
return await this.helpers.request!(options);
} else {
const credentials = this.getCredentials('mailchimpOAuth2Api') as IDataObject;

View file

@ -47,6 +47,7 @@ interface ICreateMemberBody {
timestamp_opt?: string;
tags?: string[];
merge_fields?: IDataObject;
interests?: IDataObject;
}
export class Mailchimp implements INodeType {
@ -112,6 +113,10 @@ export class Mailchimp implements INodeType {
name: 'resource',
type: 'options',
options: [
{
name: 'List Group',
value: 'listGroup',
},
{
name: 'Member',
value: 'member',
@ -194,6 +199,28 @@ export class Mailchimp implements INodeType {
default: 'create',
description: 'The operation to perform.',
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
required: true,
displayOptions: {
show: {
resource: [
'listGroup',
],
},
},
options: [
{
name: 'Get All',
value: 'getAll',
description: 'Get all groups',
},
],
default: 'getAll',
description: 'The operation to perform.',
},
/* -------------------------------------------------------------------------- */
/* member:create */
/* -------------------------------------------------------------------------- */
@ -256,27 +283,22 @@ export class Mailchimp implements INodeType {
{
name: 'Subscribed',
value: 'subscribed',
description: '',
},
{
name: 'Unsubscribed',
value: 'unsubscribed',
description: '',
},
{
name: 'Cleaned',
value: 'cleaned',
description: '',
},
{
name: 'Pending',
value: 'pending',
description: '',
},
{
name: 'Transactional',
value: 'transactional',
description: '',
},
],
default: '',
@ -287,7 +309,6 @@ export class Mailchimp implements INodeType {
name: 'jsonParameters',
type: 'boolean',
default: false,
description: '',
displayOptions: {
show: {
resource:[
@ -324,12 +345,10 @@ export class Mailchimp implements INodeType {
{
name: 'HTML',
value: 'html',
description: '',
},
{
name: 'Text',
value: 'text',
description: '',
},
],
default: '',
@ -496,7 +515,6 @@ export class Mailchimp implements INodeType {
alwaysOpenEditWindow: true,
},
default: '',
description: '',
displayOptions: {
show: {
resource:[
@ -519,7 +537,86 @@ export class Mailchimp implements INodeType {
alwaysOpenEditWindow: true,
},
default: '',
description: '',
displayOptions: {
show: {
resource:[
'member',
],
operation: [
'create',
],
jsonParameters: [
true,
],
},
},
},
{
displayName: 'Interest Groups',
name: 'groupsUi',
placeholder: 'Add Interest Group',
type: 'fixedCollection',
default: {},
typeOptions: {
multipleValues: true,
},
displayOptions: {
show: {
resource:[
'member'
],
operation: [
'create',
],
jsonParameters: [
false,
],
},
},
options: [
{
name: 'groupsValues',
displayName: 'Group',
typeOptions: {
multipleValueButtonText: 'Add Interest Group',
},
values: [
{
displayName: 'Category ID',
name: 'categoryId',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getGroupCategories',
loadOptionsDependsOn: [
'list',
],
},
default: '',
},
{
displayName: 'Category Field ID',
name: 'categoryFieldId',
type: 'string',
default: '',
},
{
displayName: 'Value',
name: 'value',
type: 'boolean',
default: false,
},
],
},
],
},
{
displayName: 'Interest Groups',
name: 'groupJson',
type: 'json',
typeOptions: {
alwaysOpenEditWindow: true,
},
default: '',
displayOptions: {
show: {
resource:[
@ -772,12 +869,10 @@ export class Mailchimp implements INodeType {
{
name: 'HTML',
value: 'html',
description: '',
},
{
name: 'Text',
value: 'text',
description: '',
},
],
default: '',
@ -791,27 +886,22 @@ export class Mailchimp implements INodeType {
{
name: 'Subscribed',
value: 'subscribed',
description: '',
},
{
name: 'Unsubscribed',
value: 'unsubscribed',
description: '',
},
{
name: 'Cleaned',
value: 'cleaned',
description: '',
},
{
name: 'Pending',
value: 'pending',
description: '',
},
{
name: 'Transactional',
value: 'transactional',
description: '',
},
],
default: '',
@ -874,7 +964,6 @@ export class Mailchimp implements INodeType {
name: 'jsonParameters',
type: 'boolean',
default: false,
description: '',
displayOptions: {
show: {
resource:[
@ -911,17 +1000,73 @@ export class Mailchimp implements INodeType {
{
name: 'HTML',
value: 'html',
description: '',
},
{
name: 'Text',
value: 'text',
description: '',
},
],
default: '',
description: 'Type of email this member asked to get',
},
{
displayName: 'Interest Groups',
name: 'groupsUi',
placeholder: 'Add Interest Group',
type: 'fixedCollection',
default: {},
typeOptions: {
multipleValues: true,
},
displayOptions: {
show: {
'/resource':[
'member'
],
'/operation':[
'update',
],
'/jsonParameters': [
false,
],
},
},
options: [
{
name: 'groupsValues',
displayName: 'Group',
typeOptions: {
multipleValueButtonText: 'Add Interest Group',
},
values: [
{
displayName: 'Category ID',
name: 'categoryId',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getGroupCategories',
loadOptionsDependsOn: [
'list',
],
},
default: '',
},
{
displayName: 'Category Field ID',
name: 'categoryFieldId',
type: 'string',
default: '',
},
{
displayName: 'Value',
name: 'value',
type: 'boolean',
default: false,
},
],
},
],
},
{
displayName: 'Language',
name: 'language',
@ -1024,27 +1169,22 @@ export class Mailchimp implements INodeType {
{
name: 'Subscribed',
value: 'subscribed',
description: '',
},
{
name: 'Unsubscribed',
value: 'unsubscribed',
description: '',
},
{
name: 'Cleaned',
value: 'cleaned',
description: '',
},
{
name: 'Pending',
value: 'pending',
description: '',
},
{
name: 'Transactional',
value: 'transactional',
description: '',
},
],
default: '',
@ -1119,7 +1259,6 @@ export class Mailchimp implements INodeType {
alwaysOpenEditWindow: true,
},
default: '',
description: '',
displayOptions: {
show: {
resource:[
@ -1142,7 +1281,28 @@ export class Mailchimp implements INodeType {
alwaysOpenEditWindow: true,
},
default: '',
description: '',
displayOptions: {
show: {
resource:[
'member',
],
operation: [
'update',
],
jsonParameters: [
true,
],
},
},
},
{
displayName: 'Interest Groups',
name: 'groupJson',
type: 'json',
typeOptions: {
alwaysOpenEditWindow: true,
},
default: '',
displayOptions: {
show: {
resource:[
@ -1250,6 +1410,96 @@ export class Mailchimp implements INodeType {
},
],
},
/* -------------------------------------------------------------------------- */
/* member:getAll */
/* -------------------------------------------------------------------------- */
{
displayName: 'List',
name: 'list',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getLists',
},
displayOptions: {
show: {
resource: [
'listGroup',
],
operation: [
'getAll',
],
},
},
default: '',
options: [],
required: true,
description: 'List of lists',
},
{
displayName: 'Group Category',
name: 'groupCategory',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getGroupCategories',
loadOptionsDependsOn: [
'list',
],
},
displayOptions: {
show: {
resource: [
'listGroup',
],
operation: [
'getAll',
],
},
},
default: '',
options: [],
required: true,
},
{
displayName: 'Return All',
name: 'returnAll',
type: 'boolean',
displayOptions: {
show: {
resource: [
'listGroup',
],
operation: [
'getAll',
],
},
},
default: false,
description: 'If all results should be returned or only up to a given limit.',
},
{
displayName: 'Limit',
name: 'limit',
type: 'number',
displayOptions: {
show: {
resource: [
'listGroup',
],
operation: [
'getAll',
],
returnAll: [
false,
],
},
},
typeOptions: {
minValue: 1,
maxValue: 1000,
},
default: 500,
description: 'How many results to return.',
},
],
};
@ -1261,7 +1511,7 @@ export class Mailchimp implements INodeType {
// select them easily
async getLists(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
const { lists } = await mailchimpApiRequest.call(this, '/lists', 'GET');
const lists = await mailchimpApiRequestAllItems.call(this, '/lists', 'GET', 'lists');
for (const list of lists) {
const listName = list.name;
const listId = list.id;
@ -1289,6 +1539,23 @@ export class Mailchimp implements INodeType {
}
return returnData;
},
// Get all the interest fields to display them to user so that he can
// select them easily
async getGroupCategories(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
const listId = this.getCurrentNodeParameter('list');
const { categories } = await mailchimpApiRequest.call(this, `/lists/${listId}/interest-categories`, 'GET');
for (const category of categories) {
const categoryName = category.title;
const categoryId = category.id;
returnData.push({
name: categoryName,
value: categoryId,
});
}
return returnData;
},
}
};
@ -1302,6 +1569,22 @@ export class Mailchimp implements INodeType {
const operation = this.getNodeParameter('operation', 0) as string;
for (let i = 0; i < length; i++) {
if (resource === 'listGroup') {
//https://mailchimp.com/developer/reference/lists/interest-categories/#get_/lists/-list_id-/interest-categories/-interest_category_id-
if (operation === 'getAll') {
const listId = this.getNodeParameter('list', i) as string;
const categoryId = this.getNodeParameter('groupCategory', i) as string;
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
if (returnAll === true) {
responseData = await mailchimpApiRequestAllItems.call(this, `/lists/${listId}/interest-categories/${categoryId}/interests`, 'GET', 'interests', {}, qs);
} else {
qs.count = this.getNodeParameter('limit', i) as number;
responseData = await mailchimpApiRequest.call(this, `/lists/${listId}/interest-categories/${categoryId}/interests`, 'GET', {}, qs);
responseData = responseData.interests;
}
}
}
if (resource === 'member') {
//https://mailchimp.com/developer/reference/lists/list-members/#post_/lists/-list_id-/members
if (operation === 'create') {
@ -1363,15 +1646,29 @@ export class Mailchimp implements INodeType {
}
body.merge_fields = mergeFields;
}
const groupsValues = (this.getNodeParameter('groupsUi', i) as IDataObject).groupsValues as IDataObject[];
if (groupsValues) {
const groups = {};
for (let i = 0; i < groupsValues.length; i++) {
// @ts-ignore
groups[groupsValues[i].categoryFieldId] = groupsValues[i].value;
}
body.interests = groups;
}
} else {
const locationJson = validateJSON(this.getNodeParameter('locationJson', i) as string);
const mergeFieldsJson = validateJSON(this.getNodeParameter('mergeFieldsJson', i) as string);
const groupJson = validateJSON(this.getNodeParameter('groupJson', i) as string);
if (locationJson) {
body.location = locationJson;
}
if (mergeFieldsJson) {
body.merge_fields = mergeFieldsJson;
}
if (groupJson) {
body.interests = groupJson;
}
}
responseData = await mailchimpApiRequest.call(this, `/lists/${listId}/members`, 'POST', body);
}
@ -1504,15 +1801,31 @@ export class Mailchimp implements INodeType {
body.merge_fields = mergeFields;
}
}
if (updateFields.groupsUi) {
const groupsValues = (updateFields.groupsUi as IDataObject).groupsValues as IDataObject[];
if (groupsValues) {
const groups = {};
for (let i = 0; i < groupsValues.length; i++) {
// @ts-ignore
groups[groupsValues[i].categoryFieldId] = groupsValues[i].value;
}
body.interests = groups;
}
}
} else {
const locationJson = validateJSON(this.getNodeParameter('locationJson', i) as string);
const mergeFieldsJson = validateJSON(this.getNodeParameter('mergeFieldsJson', i) as string);
const groupJson = validateJSON(this.getNodeParameter('groupJson', i) as string);
if (locationJson) {
body.location = locationJson;
}
if (mergeFieldsJson) {
body.merge_fields = mergeFieldsJson;
}
if (groupJson) {
body.interests = groupJson;
}
}
responseData = await mailchimpApiRequest.call(this, `/lists/${listId}/members/${email}`, 'PUT', body);
}