2016-11-26 02:46:55 +08:00
|
|
|
/* eslint-disable no-console */
|
|
|
|
|
2016-06-23 17:24:58 +09:00
|
|
|
// newsletter scheduling with MailChimp
|
|
|
|
|
2016-06-28 17:33:30 +09:00
|
|
|
import moment from 'moment';
|
2017-04-11 10:27:35 +09:00
|
|
|
import { getSetting } from 'meteor/vulcan:core';
|
|
|
|
import Newsletters from '../../modules/collection.js';
|
|
|
|
import MailChimpNPM from 'mailchimp';
|
|
|
|
|
|
|
|
const { apiKey, listId, fromName, fromEmail } = getSetting('mailchimp');
|
|
|
|
|
|
|
|
const mailChimpAPI = new MailChimpNPM.MailChimpAPI(apiKey, { version : '2.0' });
|
|
|
|
|
|
|
|
const callSyncAPI = function ( section, method, options, callback ) {
|
|
|
|
if ( callback && typeof callback === 'function' ) {
|
|
|
|
// If anyone still wants to use old-fashioned callback method
|
|
|
|
mailChimpAPI.call( section, method, options, callback );
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
var wrapped = Meteor.wrapAsync( mailChimpAPI.call, mailChimpAPI );
|
|
|
|
return wrapped( section, method, options );
|
|
|
|
} catch ( error ) {
|
|
|
|
// A workaround for:
|
|
|
|
// https://github.com/meteor/meteor/issues/2774
|
|
|
|
console.log('// MailChimp API error')
|
|
|
|
console.log(error)
|
|
|
|
// if ( !error.error ) {
|
|
|
|
// throw new Error( error.code, error.message );
|
|
|
|
// } else {
|
|
|
|
// throw new Error( error );
|
|
|
|
// }
|
|
|
|
}
|
2016-06-23 17:24:58 +09:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-04-11 10:27:35 +09:00
|
|
|
Newsletters.mailchimp = {
|
2016-06-23 17:24:58 +09:00
|
|
|
|
2017-04-11 10:27:35 +09:00
|
|
|
// add a user to a MailChimp list.
|
|
|
|
// called when a new user is created, or when an existing user fills in their email
|
|
|
|
subscribe(email, confirm = false) {
|
|
|
|
try {
|
|
|
|
const subscribeOptions = {
|
|
|
|
id: listId,
|
|
|
|
email: {email: email},
|
|
|
|
double_optin: confirm
|
|
|
|
};
|
|
|
|
// subscribe user
|
|
|
|
const subscribe = callSyncAPI('lists', 'subscribe', subscribeOptions);
|
|
|
|
return {result: 'subscribed', ...subscribe};
|
|
|
|
} catch (error) {
|
|
|
|
// if the email is already in the Mailchimp list, no need to throw an error
|
|
|
|
if (error.message === "214") {
|
|
|
|
return {result: 'already-subscribed'};
|
|
|
|
}
|
|
|
|
throw new Error("subscription-failed", error.message);
|
|
|
|
}
|
|
|
|
},
|
2016-06-23 17:24:58 +09:00
|
|
|
|
2017-04-11 10:27:35 +09:00
|
|
|
// remove a user to a MailChimp list.
|
|
|
|
// called from the user's account
|
|
|
|
unsubscribe(email) {
|
|
|
|
try {
|
|
|
|
const subscribeOptions = {
|
|
|
|
id: listId,
|
|
|
|
email: {email: email},
|
|
|
|
delete_member: true // delete the member from the list to make it possible for him to *resubscribe* via API (mailchimp's spam prevention policy)
|
|
|
|
};
|
|
|
|
// unsubscribe user
|
|
|
|
const subscribe = callSyncAPI('lists', 'unsubscribe', subscribeOptions);
|
|
|
|
return {result: 'unsubscribed', ...subscribe};
|
|
|
|
} catch (error) {
|
|
|
|
throw new Error("unsubscribe-failed", error.message);
|
2016-06-23 17:24:58 +09:00
|
|
|
}
|
2017-04-11 10:27:35 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
send({ title, subject, text, html, isTest = false }) {
|
2016-06-23 17:24:58 +09:00
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
var campaignOptions = {
|
|
|
|
type: 'regular',
|
|
|
|
options: {
|
|
|
|
list_id: listId,
|
|
|
|
subject: subject,
|
2017-04-11 10:27:35 +09:00
|
|
|
from_email: fromEmail,
|
|
|
|
from_name: fromName
|
2016-06-23 17:24:58 +09:00
|
|
|
},
|
|
|
|
content: {
|
2017-04-11 10:27:35 +09:00
|
|
|
html: html,
|
2016-06-23 17:24:58 +09:00
|
|
|
text: text
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// create campaign
|
2017-04-11 10:27:35 +09:00
|
|
|
const mailchimpNewsletter = callSyncAPI('campaigns', 'create', campaignOptions);
|
2016-06-23 17:24:58 +09:00
|
|
|
|
2016-06-28 17:33:30 +09:00
|
|
|
console.log('// Newsletter created');
|
2016-06-23 17:24:58 +09:00
|
|
|
// console.log(campaign)
|
|
|
|
|
2017-04-11 10:27:35 +09:00
|
|
|
const scheduledMoment = moment().utcOffset(0).add(1, 'hours');
|
|
|
|
const scheduledTime = scheduledMoment.format("YYYY-MM-DD HH:mm:ss");
|
2016-06-23 17:24:58 +09:00
|
|
|
|
2017-04-11 10:27:35 +09:00
|
|
|
const scheduleOptions = {
|
2016-06-23 17:24:58 +09:00
|
|
|
cid: mailchimpNewsletter.id,
|
|
|
|
schedule_time: scheduledTime
|
|
|
|
};
|
|
|
|
|
|
|
|
// schedule campaign
|
2017-04-11 10:27:35 +09:00
|
|
|
const schedule = callSyncAPI('campaigns', 'schedule', scheduleOptions); // eslint-disable-line
|
2016-06-23 17:24:58 +09:00
|
|
|
|
|
|
|
console.log('// Newsletter scheduled for '+scheduledTime);
|
2017-04-02 21:11:09 +09:00
|
|
|
|
2017-04-11 10:27:35 +09:00
|
|
|
return mailchimpNewsletter;
|
2016-06-23 17:24:58 +09:00
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
2017-04-11 10:27:35 +09:00
|
|
|
return false;
|
2016-06-23 17:24:58 +09:00
|
|
|
}
|
|
|
|
}
|
2017-04-11 10:27:35 +09:00
|
|
|
|
|
|
|
}
|