Skip to content

Commit

Permalink
Don't update campaign if already sent + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcL committed May 10, 2020
1 parent ef4ad33 commit dd80c2f
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ Once the markdown is converted to HTML, it's injected into an MJML template. Thi

See an example [here](./examples/templates/testTemplate.mjml).

## Mailchimp Campaigns

This library will create a new campaign for you if no campaign already exists with the same title and subject. If it already exists then it updates with the new HTML, unless the campaign has either the `sent` (already delivered) or `sending` (for automations) status.

## TODO

* Ensure conversion allows all use of Mailchimp tags
Expand Down
6 changes: 5 additions & 1 deletion src/createMailchimpCampaign.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const mailchimp = require('./mailchimp');

const createMailchimpCampaign = async options => {
const createMailchimpCampaign = async (options) => {
const { apiKey, listId, frontmatter, html } = options;

if (!apiKey || !listId) {
Expand All @@ -20,6 +20,10 @@ const createMailchimpCampaign = async options => {
campaign = await mailchimp.createCampaign(campaignOptions);
}

if (mailchimp.isCampaignSent(campaign)) {
return campaign;
}

await mailchimp.updateCampaignHtml({
...campaignOptions,
id: campaign.id,
Expand Down
11 changes: 9 additions & 2 deletions src/mailchimp.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ const updateCampaignHtml = ({ apiKey, id, html }) => {

const findCampaignIdByMetaData = ({ apiKey, listId, title, subject }) => {
const mailchimp = new Mailchimp(apiKey);
return mailchimp.get('/campaigns', { count: 1000 }).then(response => {
return response.campaigns.find(campaign => {
return mailchimp.get('/campaigns', { count: 1000 }).then((response) => {
return response.campaigns.find((campaign) => {
return (
campaign.recipients.list_id === listId &&
campaign.settings.subject_line === subject &&
Expand All @@ -47,8 +47,15 @@ const findCampaignIdByMetaData = ({ apiKey, listId, title, subject }) => {
});
};

const isCampaignSent = (campaign) => {
const { status } = campaign;

return status === 'sent' || status === 'sending';
};

module.exports = {
createCampaign,
findCampaignIdByMetaData,
isCampaignSent,
updateCampaignHtml,
};
45 changes: 45 additions & 0 deletions tests/mailchimp.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const mailchimp = require('../src/mailchimp');

describe('Mailchimp', () => {
describe('isCampaignSent', () => {
it('should return true if campaign is sent', () => {
const campaign = {
status: 'sent',
};

expect(mailchimp.isCampaignSent(campaign)).toBe(true);
});

it('should return true if campaign is sending', () => {
const campaign = {
status: 'sending',
};

expect(mailchimp.isCampaignSent(campaign)).toBe(true);
});

it('should return false if campaign is save', () => {
const campaign = {
status: 'save',
};

expect(mailchimp.isCampaignSent(campaign)).toBe(false);
});

it('should return false if campaign is paused', () => {
const campaign = {
status: 'paused',
};

expect(mailchimp.isCampaignSent(campaign)).toBe(false);
});

it('should return false if campaign is schedule', () => {
const campaign = {
status: 'schedule',
};

expect(mailchimp.isCampaignSent(campaign)).toBe(false);
});
});
});

0 comments on commit dd80c2f

Please sign in to comment.