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';
|
|
|
|
|
2017-04-11 10:49:54 +09:00
|
|
|
/*
|
|
|
|
|
|
|
|
API
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2017-04-18 09:56:38 +09:00
|
|
|
const settings = getSetting('mailchimp');
|
2017-04-11 10:49:54 +09:00
|
|
|
|
2017-04-18 09:56:38 +09:00
|
|
|
if (settings) {
|
|
|
|
|
|
|
|
const { apiKey, listId, fromName, fromEmail } = settings;
|
|
|
|
const mailChimpAPI = new MailChimpNPM.MailChimpAPI(apiKey, { version : '2.0' });
|
2017-04-11 10:49:54 +09:00
|
|
|
|
2017-04-18 09:56:38 +09:00
|
|
|
const callSyncAPI = ( section, method, options, callback ) => {
|
2017-04-19 12:19:17 +09:00
|
|
|
const wrapped = Meteor.wrapAsync( mailChimpAPI.call, mailChimpAPI );
|
|
|
|
return wrapped( section, method, options );
|
2017-04-18 09:56:38 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
Methods
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
Newsletters.mailchimp = {
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
},
|
2017-04-11 10:27:35 +09:00
|
|
|
|
2017-04-18 09:56:38 +09:00
|
|
|
send({ title, subject, text, html, isTest = false }) {
|
2016-06-23 17:24:58 +09:00
|
|
|
|
2017-04-18 09:56:38 +09:00
|
|
|
try {
|
2016-06-23 17:24:58 +09:00
|
|
|
|
2017-04-18 09:56:38 +09:00
|
|
|
var campaignOptions = {
|
|
|
|
type: 'regular',
|
|
|
|
options: {
|
|
|
|
list_id: listId,
|
|
|
|
subject: subject,
|
|
|
|
from_email: fromEmail,
|
|
|
|
from_name: fromName
|
|
|
|
},
|
|
|
|
content: {
|
|
|
|
html: html,
|
|
|
|
text: text
|
|
|
|
}
|
|
|
|
};
|
2016-06-23 17:24:58 +09:00
|
|
|
|
2017-04-18 09:56:38 +09:00
|
|
|
// create campaign
|
|
|
|
const mailchimpNewsletter = callSyncAPI('campaigns', 'create', campaignOptions);
|
2016-06-23 17:24:58 +09:00
|
|
|
|
2017-04-18 09:56:38 +09:00
|
|
|
console.log('// Newsletter created');
|
|
|
|
// console.log(campaign)
|
2016-06-23 17:24:58 +09:00
|
|
|
|
2017-04-18 09:56:38 +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-18 09:56:38 +09:00
|
|
|
const scheduleOptions = {
|
|
|
|
cid: mailchimpNewsletter.id,
|
|
|
|
schedule_time: scheduledTime
|
|
|
|
};
|
2016-06-23 17:24:58 +09:00
|
|
|
|
2017-04-18 09:56:38 +09:00
|
|
|
// schedule campaign
|
|
|
|
const schedule = callSyncAPI('campaigns', 'schedule', scheduleOptions); // eslint-disable-line
|
2016-06-23 17:24:58 +09:00
|
|
|
|
2017-04-18 09:56:38 +09:00
|
|
|
console.log('// Newsletter scheduled for '+scheduledTime);
|
2017-04-02 21:11:09 +09:00
|
|
|
|
2017-04-18 09:56:38 +09:00
|
|
|
return mailchimpNewsletter;
|
2016-06-23 17:24:58 +09:00
|
|
|
|
2017-04-18 09:56:38 +09:00
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-23 17:24:58 +09:00
|
|
|
}
|
2017-04-18 09:56:38 +09:00
|
|
|
|
2016-06-23 17:24:58 +09:00
|
|
|
}
|
2017-04-11 10:27:35 +09:00
|
|
|
|
|
|
|
}
|