Improvements to #1015

This commit is contained in:
ricardo 2020-10-05 18:44:52 -04:00
parent de4c0546cd
commit 0f2e4d55a4
2 changed files with 156 additions and 36 deletions

View file

@ -49,6 +49,7 @@ 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;
@ -110,3 +111,63 @@ function getMetadata(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFu
};
return this.helpers.request!(options);
}
export const campaignFieldsMetadata = [
'*',
'campaigns.id',
'campaigns.web_id',
'campaigns.type',
'campaigns.create_time',
'campaigns.archive_url',
'campaigns.long_archive_url',
'campaigns.status',
'campaigns.emails_sent',
'campaigns.send_time',
'campaigns.content_type',
'campaigns.needs_block_refresh',
'campaigns.resendable',
'campaigns.recipients',
'campaigns.recipients.list_id',
'campaigns.recipients.list_is_active',
'campaigns.recipients.list_name',
'campaigns.recipients.segment_text',
'campaigns.recipients.recipient_count',
'campaigns.settings',
'campaigns.settings.subject_line',
'campaigns.settings.preview_text',
'campaigns.settings.title',
'campaigns.settings.from_name',
'campaigns.settings.reply_to',
'campaigns.settings.use_conversation',
'campaigns.settings.to_name',
'campaigns.settings.folder_id',
'campaigns.settings.authenticate',
'campaigns.settings.auto_footer',
'campaigns.settings.inline_css',
'campaigns.settings.auto_tweet',
'campaigns.settings.fb_comments',
'campaigns.settings.timewarp',
'campaigns.settings.template_id',
'campaigns.settings.drag_and_drop',
'campaigns.tracking',
'campaigns.tracking.opens',
'campaigns.tracking.html_clicks',
'campaigns.tracking.text_clicks',
'campaigns.tracking.goal_tracking',
'campaigns.tracking.ecomm360',
'campaigns.tracking.google_analytics',
'campaigns.tracking.clicktale',
'campaigns.report_summary',
'campaigns.report_summary.opens',
'campaigns.report_summary.unique_opens',
'campaigns.report_summary.open_rate',
'campaigns.report_summary.clicks',
'campaigns.report_summary.subscriber_clicks',
'campaigns.report_summary.click_rate',
'campaigns.report_summary.click_rate.ecommerce',
'campaigns.report_summary.click_rate.ecommerce.total_orders',
'campaigns.report_summary.click_rate.ecommerce.total_spent',
'campaigns.report_summary.click_rate.ecommerce.total_revenue',
'campaigns.report_summary.delivery_status.enabled',
'campaigns._links',
];

View file

@ -13,6 +13,7 @@ import {
} from 'n8n-workflow';
import {
campaignFieldsMetadata,
mailchimpApiRequest,
mailchimpApiRequestAllItems,
validateJSON,
@ -1630,15 +1631,23 @@ export class Mailchimp implements INodeType {
{
displayName: 'Exclude Fields',
name: 'excludeFields',
type: 'string',
default: '',
type: 'multiOptions',
typeOptions: {
loadOptionsMethod: 'getCampaignsFields',
},
default: [],
description: 'A comma-separated list of fields to exclude.',
},
{
displayName: 'Fields',
name: 'fields',
type: 'string',
default: '',
type: 'multiOptions',
typeOptions: {
loadOptionsMethod: 'getCampaignsFields',
},
default: [
'campaigns.id',
],
description: 'A comma-separated list of fields to return.',
},
{
@ -1665,6 +1674,40 @@ export class Mailchimp implements INodeType {
default: '',
description: 'Restrict the response to campaigns sent after the set time.',
},
{
displayName: 'Sort Direction',
name: 'sortDirection',
type: 'options',
options: [
{
name: 'ASC',
value: 'ASC',
},
{
name: 'DESC',
value: 'DESC',
},
],
default: '',
description: 'Determines the order direction for sorted results.',
},
{
displayName: 'Sort Field',
name: 'sortField',
type: 'options',
options: [
{
name: 'Create Time',
value: 'create_time',
},
{
name: 'Send Time',
value: 'send_time',
},
],
default: '',
description: 'Returns files sorted by the specified field.',
},
{
displayName: 'Status',
name: 'status',
@ -1698,10 +1741,7 @@ export class Mailchimp implements INodeType {
{
displayName: 'Campaign ID',
name: 'campaignId',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getCampaigns',
},
type: 'string',
displayOptions: {
show: {
resource: [
@ -1790,9 +1830,21 @@ export class Mailchimp implements INodeType {
value: campaignId,
});
}
return returnData
return returnData;
},
// Get all the available fields to display them to users so that they can select them easily
async getCampaignsFields(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
for (const campaignFields of campaignFieldsMetadata) {
returnData.push({
name: campaignFields,
value: campaignFields,
});
}
return returnData;
}
}
},
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
@ -2129,65 +2181,72 @@ export class Mailchimp implements INodeType {
qs.status = options.status as string;
}
if (options.beforeCreateTime) {
qs.beforeCreateTime = options.beforeCreateTime as string;
qs.before_create_time = options.beforeCreateTime as string;
}
if (options.beforeSendTime) {
qs.beforeSendTime = options.beforeSendTime as string;
qs.before_send_time = options.beforeSendTime as string;
}
// TODO
// Figure out how to make excludeFields and fileds work
// if (options.excludeFields) {
// qs.excludeFields = options.excludeFields as string;
// }
// if (options.fields) {
// qs.fields = options.fields as string;
// }
if (options.excludeFields) {
qs.exclude_fields = (options.exclude_fields as string[]).join(',');
}
if (options.fields) {
qs.fields = (options.fields as string[]).join(',');
if ((options.fields as string[]).includes('*')) {
qs.fields = campaignFieldsMetadata.join(',');
}
} else {
qs.fields = 'campaigns.id';
}
if (options.listId) {
qs.listId = options.listId as string;
qs.list_id = options.listId as string;
}
if (options.sinceCreateTime) {
qs.sinceCreateTime = options.sinceCreateTime as string;
qs.since_create_time = options.sinceCreateTime as string;
}
if (options.sinceSendTime) {
qs.sinceSendTime = options.sinceSendTime as string;
qs.since_send_time = options.sinceSendTime as string;
}
if (options.sortDirection) {
qs.sort_dir = options.sortDirection as string;
}
if (options.sortField) {
qs.sort_field = options.sortField as string;
}
// TODO
// Make the options work when returnAll is false
if (returnAll === true) {
responseData = await mailchimpApiRequestAllItems.call(this, `/campaigns`, 'GET', 'campaigns', {}, qs)
responseData = await mailchimpApiRequestAllItems.call(this, `/campaigns`, 'GET', 'campaigns', {}, qs);
} else {
qs.count = this.getNodeParameter('limit', i) as number;
responseData = await mailchimpApiRequest.call(this, `/campaigns`, 'GET', 'campaigns', {}, qs)
responseData = responseData.campaigns
responseData = await mailchimpApiRequest.call(this, `/campaigns`, 'GET', {}, qs);
responseData = responseData.campaigns;
}
}
//https://mailchimp.com/developer/api/marketing/campaigns/send-campaign/
if (operation === 'send') {
const campaignId = this.getNodeParameter('campaignId', i) as string;
responseData = await mailchimpApiRequest.call(this, `/campaigns/${campaignId}/actions/send`, 'POST', {})
responseData = await mailchimpApiRequest.call(this, `/campaigns/${campaignId}/actions/send`, 'POST', {});
responseData = { success: true };
}
//https://mailchimp.com/developer/api/marketing/campaigns/get-campaign-info/
if (operation === 'get') {
const campaignId = this.getNodeParameter('campaignId', i) as string;
if(!campaignId){
throw new Error("Campaign ID is required");
}
responseData = await mailchimpApiRequest.call(this, `/campaigns/${campaignId}`, 'GET', {})
responseData = await mailchimpApiRequest.call(this, `/campaigns/${campaignId}`, 'GET', {});
}
//https://mailchimp.com/developer/api/marketing/campaigns/delete-campaign/
if (operation === 'delete') {
const campaignId = this.getNodeParameter('campaignId', i) as string;
responseData = await mailchimpApiRequest.call(this, `/campaigns/${campaignId}`, 'DELETE', {})
responseData = await mailchimpApiRequest.call(this, `/campaigns/${campaignId}`, 'DELETE', {});
responseData = { success: true };
}
//https://mailchimp.com/developer/api/marketing/campaigns/replicate-campaign/
if (operation === 'replicate') {
const campaignId = this.getNodeParameter('campaignId', i) as string;
responseData = await mailchimpApiRequest.call(this, `/campaigns/${campaignId}/actions/replicate`, 'POST', {})
responseData = await mailchimpApiRequest.call(this, `/campaigns/${campaignId}/actions/replicate`, 'POST', {});
}
//https://mailchimp.com/developer/api/marketing/campaigns/resend-campaign/
if (operation === 'resend') {
const campaignId = this.getNodeParameter('campaignId', i) as string;
responseData = await mailchimpApiRequest.call(this, `/campaigns/${campaignId}/actions/create-resend`, 'POST', {})
responseData = await mailchimpApiRequest.call(this, `/campaigns/${campaignId}/actions/create-resend`, 'POST', {});
}
}