Vulcan/packages/vulcan-newsletter/lib/server/integrations/sendy.js

79 lines
1.7 KiB
JavaScript
Raw Normal View History

import Sendy from 'sendy-api'; // see https://github.com/igord/sendy-api
import { getSetting } from 'meteor/vulcan:core';
import Newsletters from '../../modules/collection.js';
/*
API
*/
const {server, apiKey, listId, fromName, fromEmail, replyTo } = getSetting('sendy');
const SendyAPI = new Sendy(server, apiKey);
const subscribeSync = options => {
2017-04-12 11:46:27 +09:00
try {
const wrapped = Meteor.wrapAsync( SendyAPI.subscribe, SendyAPI );
return wrapped( options );
} catch ( error ) {
console.log('// Sendy API error')
console.log(error)
if (error.message === 'Already subscribed.') {
return {result: 'already-subscribed'}
}
}
};
const unsubscribeSync = options => {
2017-04-12 11:46:27 +09:00
try {
const wrapped = Meteor.wrapAsync( SendyAPI.unsubscribe, SendyAPI );
return wrapped( options );
} catch ( error ) {
console.log('// Sendy API error')
console.log(error)
}
};
const createCampaignSync = options => {
2017-04-12 11:46:27 +09:00
try {
const wrapped = Meteor.wrapAsync( SendyAPI.createCampaign, SendyAPI );
return wrapped( options );
} catch ( error ) {
console.log('// Sendy API error')
console.log(error)
}
};
/*
Methods
*/
Newsletters.sendy = {
subscribe(email) {
2017-04-12 11:46:27 +09:00
return subscribeSync({email, list_id: listId});
},
unsubscribe(email) {
2017-04-12 11:46:27 +09:00
return unsubscribeSync({email, list_id: listId});
},
send({ title, subject, text, html, isTest = false }) {
const params = {
2017-04-12 11:46:27 +09:00
from_name: fromName,
from_email: fromEmail,
reply_to: replyTo,
title: subject,
subject: subject,
plain_text: text,
html_text: html,
send_campaign: !isTest,
list_ids: listId
};
2017-04-12 11:46:27 +09:00
return createCampaignSync(params);
}
}