diff --git a/ghost/admin/app/templates/settings/labs.hbs b/ghost/admin/app/templates/settings/labs.hbs index eb759d4a23..87bb253693 100644 --- a/ghost/admin/app/templates/settings/labs.hbs +++ b/ghost/admin/app/templates/settings/labs.hbs @@ -153,6 +153,19 @@ +
+
+
+

Milestones

+

+ Occasional summaries of your audience & revenue growth +

+
+
+ +
+
+
@@ -226,19 +239,6 @@ -
-
-
-

Milestone emails

-

- Send emails for reaching specific milestones. -

-
-
- -
-
-
diff --git a/ghost/core/core/server/services/milestones/service.js b/ghost/core/core/server/services/milestones/service.js index b1d606fa7a..c1cfc176c1 100644 --- a/ghost/core/core/server/services/milestones/service.js +++ b/ghost/core/core/server/services/milestones/service.js @@ -10,7 +10,8 @@ const getStripeLiveEnabled = () => { const stripeConnect = settingsCache.get('stripe_connect_publishable_key'); const stripeKey = settingsCache.get('stripe_publishable_key'); - const stripeLiveRegex = /pk_live_/; + // Allow Stripe test key when in development mode + const stripeLiveRegex = process.env.NODE_ENV === 'development' ? /pk_test_/ : /pk_live_/; if (stripeConnect && stripeConnect.match(stripeLiveRegex)) { return true; @@ -84,6 +85,11 @@ module.exports = { * @returns {Promise} */ async scheduleRun(customTimeout) { + if (process.env.NODE_ENV === 'development') { + // Run the job within 5sec after boot when in local development mode + customTimeout = 5000; + } + const timeOut = customTimeout || JOB_TIMEOUT; const today = new Date(); diff --git a/ghost/core/test/unit/server/services/staff/index.test.js b/ghost/core/test/unit/server/services/staff/index.test.js index 95b7b43ffa..2bfd5bc2a4 100644 --- a/ghost/core/test/unit/server/services/staff/index.test.js +++ b/ghost/core/test/unit/server/services/staff/index.test.js @@ -1,5 +1,4 @@ const sinon = require('sinon'); -const assert = require('assert'); const staffService = require('../../../../../core/server/services/staff'); const DomainEvents = require('@tryghost/domain-events'); @@ -10,8 +9,6 @@ const {SubscriptionCancelledEvent, MemberCreatedEvent, SubscriptionActivatedEven const {MilestoneCreatedEvent} = require('@tryghost/milestones'); describe('Staff Service:', function () { - let userModelStub; - before(function () { models.init(); }); @@ -19,7 +16,10 @@ describe('Staff Service:', function () { beforeEach(function () { mockManager.mockMail(); mockManager.mockSlack(); - userModelStub = sinon.stub(models.User, 'getEmailAlertUsers').resolves([{ + mockManager.mockSetting('title', 'The Weekly Roundup'); + mockManager.mockLabsEnabled('milestoneEmails'); + + sinon.stub(models.User, 'getEmailAlertUsers').resolves([{ email: 'owner@ghost.org', slug: 'ghost' }]); @@ -232,22 +232,13 @@ describe('Staff Service:', function () { }); describe('milestone created event:', function () { - beforeEach(function () { - mockManager.mockLabsEnabled('milestoneEmails'); - }); - - afterEach(async function () { - sinon.restore(); - mockManager.restore(); - }); - - it('logs when milestone event is handled', async function () { + it('sends email for achieved milestone', async function () { await staffService.init(); DomainEvents.dispatch(MilestoneCreatedEvent.create({ milestone: { type: 'arr', currency: 'usd', - value: 100, + value: 1000, createdAt: new Date(), emailSentAt: new Date() }, @@ -258,8 +249,50 @@ describe('Staff Service:', function () { // Wait for the dispatched events (because this happens async) await DomainEvents.allSettled(); - const [userCalls] = userModelStub.args[0]; - assert.equal(userCalls, ['milestone-received']); + + mockManager.assert.sentEmailCount(1); + + mockManager.assert.sentEmail({ + to: 'owner@ghost.org', + subject: /The Weekly Roundup hit \$1,000 ARR/ + }); + }); + + it('does not send email when no email created at provided or a reason is set', async function () { + DomainEvents.dispatch(MilestoneCreatedEvent.create({ + milestone: { + type: 'arr', + currency: 'usd', + value: 1000, + createdAt: new Date(), + emailSentAt: null + }, + meta: { + currentValue: 105 + } + })); + + // Wait for the dispatched events (because this happens async) + await DomainEvents.allSettled(); + + DomainEvents.dispatch(MilestoneCreatedEvent.create({ + milestone: { + type: 'arr', + currency: 'usd', + value: 1000, + createdAt: new Date(), + emailSentAt: new Date(), + meta: { + currentValue: 105, + reason: 'import' + } + } + })); + + // Wait for the dispatched events (because this happens async) + await DomainEvents.allSettled(); + + mockManager.assert.sentEmailCount(0); }); }); }); diff --git a/ghost/staff-service/lib/email-templates/new-milestone-received.hbs b/ghost/staff-service/lib/email-templates/new-milestone-received.hbs new file mode 100644 index 0000000000..7e25e8ba80 --- /dev/null +++ b/ghost/staff-service/lib/email-templates/new-milestone-received.hbs @@ -0,0 +1,142 @@ + + + + + + {{subject}} + + {{> styles}} + + + + + + + + + + +
  +
+ + + + {{#> preview}} + {{#*inline "content"}} + {{{heading}}} + {{/inline}} + {{/preview}} + + + + + + + +
+ + + + + + + + + +
+
+ + + + +
+
+
+ + + + + + + + + +
+
+
+
+ + + + + + + + + + + + + + + + + +
+ + + + + +

{{{heading}}}

+ + + + + {{#each content as |contentPart|}} +

{{{contentPart}}}

+ {{/each}} + + + + + + + + + +
+ + + + + + +
{{ctaText}}
+
+ +

+
+

This message was sent from {{siteDomain}} to {{toEmail}}

+
+

Don’t want to receive these emails? Manage your preferences here.

+
+ + +
+ + +
+
 
+ + + diff --git a/ghost/staff-service/lib/email-templates/new-milestone-received.txt.js b/ghost/staff-service/lib/email-templates/new-milestone-received.txt.js new file mode 100644 index 0000000000..6341f8ce72 --- /dev/null +++ b/ghost/staff-service/lib/email-templates/new-milestone-received.txt.js @@ -0,0 +1,13 @@ +module.exports = function (data) { + // Be careful when you indent the email, because whitespaces are visible in emails! + return ` +Congratulations! + +${data.subject} + +--- + +Sent to ${data.toEmail} from ${data.siteDomain}. +If you would no longer like to receive these notifications you can adjust your settings at ${data.staffUrl}. + `; +}; diff --git a/ghost/staff-service/lib/emails.js b/ghost/staff-service/lib/emails.js index a7109ac57a..afae6f0434 100644 --- a/ghost/staff-service/lib/emails.js +++ b/ghost/staff-service/lib/emails.js @@ -187,13 +187,57 @@ class StaffServiceEmails { * @returns {Promise} */ async notifyMilestoneReceived({milestone}) { + if (!milestone?.emailSentAt || milestone?.meta?.reason) { + // Do not send an email when no email was set to be sent or a reason + // not to send provided + return; + } + + const formattedValue = this.getFormattedAmount({currency: milestone?.currency, amount: milestone.value, maximumFractionDigits: 0}); + const milestoneEmailConfig = require('./milestone-email-config')(this.settingsCache.get('title'), formattedValue); + + const emailData = milestoneEmailConfig?.[milestone.type]?.[milestone.value]; + + if (!emailData || Object.keys(emailData).length === 0) { + // Do not attempt to send an email with invalid or missing data + this.logging.warn('No Milestone email sent. Invalid or missing data.'); + return; + } + + const emailPromises = []; const users = await this.models.User.getEmailAlertUsers('milestone-received'); - // TODO: send email with correct templates for (const user of users) { const to = user.email; - this.logging.info(`Will send email to ${to} for ${milestone.type} / ${milestone.value} milestone.`); + const templateData = { + siteTitle: this.settingsCache.get('title'), + siteUrl: this.urlUtils.getSiteUrl(), + siteDomain: this.siteDomain, + fromEmail: this.fromEmailAddress, + ...emailData, + partial: `milestones/${milestone.value}`, + toEmail: to, + adminUrl: this.urlUtils.urlFor('admin', true), + staffUrl: this.urlUtils.urlJoin(this.urlUtils.urlFor('admin', true), '#', `/settings/staff/${user.slug}`) + }; + + const {html, text} = await this.renderEmailTemplate('new-milestone-received', templateData); + + emailPromises.push(await this.sendMail({ + to, + subject: emailData.subject, + html, + text + })); + } + + const results = await Promise.allSettled(emailPromises); + + for (const result of results) { + if (result.status === 'rejected') { + this.logging.warn(result?.reason); + } } } @@ -228,15 +272,18 @@ class StaffServiceEmails { } /** @private */ - getFormattedAmount({amount = 0, currency}) { + getFormattedAmount({amount = 0, currency, maximumFractionDigits = 2}) { if (!currency) { - return amount > 0 ? Intl.NumberFormat().format(amount) : ''; + return amount > 0 ? Intl.NumberFormat('en', {maximumFractionDigits}).format(amount) : ''; } return Intl.NumberFormat('en', { style: 'currency', currency, - currencyDisplay: 'symbol' + currencyDisplay: 'symbol', + maximumFractionDigits, + // see https://github.com/andyearnshaw/Intl.js/issues/123 + minimumFractionDigits: maximumFractionDigits }).format(amount); } diff --git a/ghost/staff-service/lib/milestone-email-config.js b/ghost/staff-service/lib/milestone-email-config.js new file mode 100644 index 0000000000..bc75034592 --- /dev/null +++ b/ghost/staff-service/lib/milestone-email-config.js @@ -0,0 +1,207 @@ +/** + * + * @param {string} siteTitle + * @param {string} formattedValue + * + * @returns {Object.} + */ +const milestoneEmailConfig = (siteTitle, formattedValue) => { + const arrContent = { + subject: `${siteTitle} hit ${formattedValue} ARR`, + heading: `Congrats! You reached ${formattedValue} ARR`, + content: [ + `${siteTitle} is now generating ${formattedValue} in annual recurring revenue. Congratulations — this is a significant milestone.`, + 'Subscription revenue is predictable and sustainable, meaning you can keep focusing on delivering great content while watching your business grow. Keep up the great work. See you at the next milestone!' + ], + ctaText: 'Login to your dashboard' + }; + + return { + // For ARR we use the same content and only the image changes + // Should we start to support different currencies, we'll need + // to update the structure for ARR content to reflect that. + arr: { + 100: { + ...arrContent, + image: { + url: 'https://static.ghost.org/v5.0.0/images/milestone-email-usd-100.png', + height: 348 + } + }, + 1000: { + ...arrContent, + image: { + url: 'https://static.ghost.org/v5.0.0/images/milestone-email-usd-1000.png', + height: 348 + } + }, + 10000: { + ...arrContent, + image: { + url: 'https://static.ghost.org/v5.0.0/images/milestone-email-usd-10k.png', + height: 348 + } + }, + 50000: { + ...arrContent, + image: { + url: 'https://static.ghost.org/v5.0.0/images/milestone-email-usd-50k.png', + height: 348 + } + }, + 100000: { + ...arrContent, + heading: `Congrats! You reached $100k ARR`, + image: { + url: 'https://static.ghost.org/v5.0.0/images/milestone-email-usd-100k.png', + height: 348 + } + }, + 250000: { + ...arrContent, + heading: `Congrats! You reached $250k ARR`, + image: { + url: 'https://static.ghost.org/v5.0.0/images/milestone-email-usd-250k.png', + height: 348 + } + }, + 500000: { + ...arrContent, + heading: `Congrats! You reached $500k ARR`, + image: { + url: 'https://static.ghost.org/v5.0.0/images/milestone-email-usd-500k.png', + height: 348 + } + }, + 1000000: { + ...arrContent, + heading: `Congrats! You reached $1m ARR`, + image: { + url: 'https://static.ghost.org/v5.0.0/images/milestone-email-usd-1m.png', + height: 348 + } + } + }, + members: { + 100: { + subject: `${siteTitle} has ${formattedValue} members 🤗`, + heading: `Milestone achieved: ${formattedValue} signups`, + content: [ + 'All the hard work in getting your publication up and running paid off, and your work has since gone on to inspire more than 100 people to sign up. This is the first major milestone in growing an online audience, and you’ve made it here!', + 'So what’s next?', + 'If you keep up the great work you’ll be well on your way to growing an even bigger audience. In the meantime, here’s some actionable advice about how to reach the next major milestones.', + 'You got this!' + ], + ctaText: 'Login to your dashboard', + image: { + url: 'https://static.ghost.org/v5.0.0/images/milestone-email-members-100.png' + } + }, + 1000: { + subject: `${siteTitle} now has ${formattedValue} members`, + heading: `You have ${formattedValue} true fans`, + content: [ + `Congrats, ${siteTitle} has officially reached ${formattedValue} member signups.`, + 'This is such an impressive milestone and according to Kevin Kelly’s true fan theory, it means you now have a direct relationship with enough people to run a truly independent creator business online.', + `Imagine ${formattedValue} people all in one room at the same time. That's a lot of people. It's also how many people are happy that you show up to create your work. Very cool. Keep up the great work!` + ], + ctaText: 'See your member stats', + image: { + url: 'https://static.ghost.org/v5.0.0/images/milestone-email-members-1000.png' + } + }, + 10000: { + subject: `${siteTitle} now has 10k members`, + heading: 'Huge success: 10k members', + content: [ + `There are now 10k people who enjoy ${siteTitle} so much they decided to sign up as members.`, + 'Building an audience of any size as an independent creator requires dedication, and reaching this incredible milestone is an impressive feat worth celebrating. There’s no stopping you now, keep up the great work!' + ], + ctaText: 'Go to your dashboard', + image: { + url: 'https://static.ghost.org/v5.0.0/images/milestone-email-members-10k.png' + } + }, + 25000: { + subject: `${siteTitle} now has 25k members`, + heading: `Celebrating ${formattedValue} signups`, + content: [ + 'Congrats, 25k people have chosen to support and follow your work. That’s an audience big enough to sell out Madison Square Garden. What an incredible milestone!', + 'It takes a lot of work and dedication to build an audience as an independent creator, so here’s to recognizing what you’ve achieved.', + 'Keep up the great work!' + ], + ctaText: 'View your dashboard', + image: { + url: 'https://static.ghost.org/v5.0.0/images/milestone-email-members-25k.png' + } + }, + 50000: { + subject: `${siteTitle} now has 50k members`, + heading: `${formattedValue} people love your work`, + content: [ + `It's time to pop the champagne because ${siteTitle} has officially reached 50k members. At this rate of growth you can almost fill a Superbowl stadium 🏈`, + 'Building an audience of this size is an incredible achievement, so hats off to you. Keep up the amazing work.', + 'See you at the next milestone!' + ], + ctaText: 'Go to your Dashboard', + image: { + url: 'https://static.ghost.org/v5.0.0/images/milestone-email-members-50k.png' + } + }, + 100000: { + subject: `${siteTitle} just hit 100k members!`, + heading: `You just reached ${formattedValue} members`, + content: [ + 'Congratulations — your work has attracted an audience of 100k people from around the world. Fun fact: Your audience is now big enough to fill any of the largest stadiums in the United States.', + 'Whatever you’re doing, it’s working. The sky is the limit from here. Keep up the great work (but first, go and celebrate this impressive milestone, you earned it).' + ], + ctaText: 'Go to your dashboard', + image: { + url: 'https://static.ghost.org/v5.0.0/images/milestone-email-members-100k.png' + } + }, + 250000: { + subject: `${siteTitle} now has 250k members`, + heading: 'Celebrating 250k member signups', + content: [ + `One-quarter of a million people enjoy and support ${siteTitle}. That’s the same number of people who make up the crowds at the SXSW festival.`, + 'You’re officially in the top 5% of creators using Ghost 🚀', + 'Reaching this milestone is no easy feat, so make sure you take some time to recognize how far you’ve come.', + 'Keep up the amazing work!' + ], + ctaText: 'Go to your dashboard', + image: { + url: 'https://static.ghost.org/v5.0.0/images/milestone-email-members-250k.png' + } + }, + 500000: { + subject: `${siteTitle} has ${formattedValue} members`, + heading: `Half a million members!`, + content: [ + `Congrats, ${siteTitle} has officially attracted an audience of more than ${formattedValue} people, and counting.`, + 'You’re officially in the top 3% of creators using Ghost. ', + 'It takes a huge amount of hard work and dedication to build an audience of this size. It is a testament to how much value your work is providing to thousands of people all over the world. Keep up the great work, and make sure to take the time to celebrate this incredible milestone.' + ], + ctaText: 'Login to your dashboard', + image: { + url: 'https://static.ghost.org/v5.0.0/images/milestone-email-members-500k.png' + } + }, + 1000000: { + subject: `${siteTitle} has 1 million members`, + heading: `You did it. 1 million members 🏆`, + content: [ + `Start writing your acceptance speech! The ${siteTitle} audience is now officially big enough to headline an event at the Copacabana, with more than 1 million members. That puts you in the top 1% of creators using Ghost.`, + 'In all seriousness, this is an incredible achievement and something to be very proud of. You deserve all the credit as a truly independent creator.', + 'Keep it up, you’re creating amazing value in the world!' + ], + ctaText: 'Go to your dashboard', + image: { + url: 'https://static.ghost.org/v5.0.0/images/milestone-email-members-1m.png' + } + } + } + }; +}; + +module.exports = milestoneEmailConfig; diff --git a/ghost/staff-service/test/staff-service.test.js b/ghost/staff-service/test/staff-service.test.js index d76eb608a9..2ab22edb83 100644 --- a/ghost/staff-service/test/staff-service.test.js +++ b/ghost/staff-service/test/staff-service.test.js @@ -111,7 +111,7 @@ describe('StaffService', function () { describe('email notifications:', function () { let mailStub; - let loggingInfoStub; + let loggingWarningStub; let subscribeStub; let getEmailAlertUsersStub; let service; @@ -120,6 +120,14 @@ describe('StaffService', function () { forUpdate: true }; let stubs; + let labs = { + isSet: (flag) => { + if (flag === 'milestoneEmails') { + return true; + } + return false; + } + }; const settingsCache = { get: (setting) => { @@ -151,7 +159,7 @@ describe('StaffService', function () { }; beforeEach(function () { - loggingInfoStub = sinon.stub().resolves(); + loggingWarningStub = sinon.stub().resolves(); mailStub = sinon.stub().resolves(); subscribeStub = sinon.stub().resolves(); getEmailAlertUsersStub = sinon.stub().resolves([{ @@ -160,8 +168,7 @@ describe('StaffService', function () { }]); service = new StaffService({ logging: { - info: loggingInfoStub, - warn: () => {}, + warn: loggingWarningStub, error: () => {} }, models: { @@ -177,7 +184,8 @@ describe('StaffService', function () { }, settingsCache, urlUtils, - settingsHelpers + settingsHelpers, + labs }); stubs = {mailStub, getEmailAlertUsersStub}; }); @@ -198,7 +206,6 @@ describe('StaffService', function () { it('listens to events', async function () { service = new StaffService({ logging: { - info: loggingInfoStub, warn: () => {}, error: () => {} }, @@ -257,8 +264,6 @@ describe('StaffService', function () { describe('handleEvent', function () { beforeEach(function () { - loggingInfoStub = sinon.stub().resolves(); - const models = { User: { getEmailAlertUsers: sinon.stub().resolves([{ @@ -322,7 +327,6 @@ describe('StaffService', function () { service = new StaffService({ logging: { - info: loggingInfoStub, warn: () => {}, error: () => {} }, @@ -338,12 +342,6 @@ describe('StaffService', function () { settingsHelpers, labs: { isSet: (flag) => { - if (flag === 'webmentions') { - return true; - } - if (flag === 'webmentionEmails') { - return true; - } if (flag === 'milestoneEmails') { return true; } @@ -401,14 +399,15 @@ describe('StaffService', function () { data: { milestone: { type: 'arr', - value: '100', - currency: 'usd' + value: '1000', + currency: 'usd', + emailSentAt: Date.now() } } }); - mailStub.called.should.be.false(); - loggingInfoStub.calledOnce.should.be.true(); - loggingInfoStub.calledWith('Will send email to owner@ghost.org for arr / 100 milestone.').should.be.true(); + mailStub.calledWith( + sinon.match({subject: `Ghost Site hit $1,000 ARR`}) + ).should.be.true(); }); }); @@ -798,7 +797,78 @@ describe('StaffService', function () { }); describe('notifyMilestoneReceived', function () { - it('prepares to send email when user setting available', async function () { + it('send Members milestone email', async function () { + const milestone = { + type: 'members', + value: 25000, + emailSentAt: Date.now() + }; + + await service.emails.notifyMilestoneReceived({milestone}); + + getEmailAlertUsersStub.calledWith('milestone-received').should.be.true(); + + mailStub.calledOnce.should.be.true(); + + mailStub.calledWith( + sinon.match.has('html', sinon.match('Ghost Site now has 25k members')) + ).should.be.true(); + + mailStub.calledWith( + sinon.match.has('html', sinon.match('Celebrating 25,000 signups')) + ).should.be.true(); + + // Correct image and NO height for Members milestone + mailStub.calledWith( + sinon.match.has('html', sinon.match('src="https://static.ghost.org/v5.0.0/images/milestone-email-members-25k.png" width="580" align="center"')) + ).should.be.true(); + + mailStub.calledWith( + sinon.match.has('html', sinon.match('Congrats, 25k people have chosen to support and follow your work. That’s an audience big enough to sell out Madison Square Garden. What an incredible milestone!')) + ).should.be.true(); + + mailStub.calledWith( + sinon.match.has('html', sinon.match('View your dashboard')) + ).should.be.true(); + }); + + it('send ARR milestone email', async function () { + const milestone = { + type: 'arr', + value: 500000, + currency: 'usd', + emailSentAt: Date.now() + }; + + await service.emails.notifyMilestoneReceived({milestone}); + + getEmailAlertUsersStub.calledWith('milestone-received').should.be.true(); + + mailStub.calledOnce.should.be.true(); + + mailStub.calledWith( + sinon.match.has('html', sinon.match('Ghost Site hit $500,000 ARR')) + ).should.be.true(); + + mailStub.calledWith( + sinon.match.has('html', sinon.match('Congrats! You reached $500k ARR')) + ).should.be.true(); + + // Correct image and height for ARR milestone + mailStub.calledWith( + sinon.match.has('html', sinon.match('src="https://static.ghost.org/v5.0.0/images/milestone-email-usd-500k.png" width="580" height="348" align="center"')) + ).should.be.true(); + + mailStub.calledWith( + sinon.match.has('html', sinon.match('Ghost Site is now generating $500,000 in annual recurring revenue. Congratulations — this is a significant milestone.')) + ).should.be.true(); + + mailStub.calledWith( + sinon.match.has('html', sinon.match('Login to your dashboard')) + ).should.be.true(); + }); + + it('does not send email when no date provided', async function () { const milestone = { type: 'members', value: 25000 @@ -806,8 +876,42 @@ describe('StaffService', function () { await service.emails.notifyMilestoneReceived({milestone}); + getEmailAlertUsersStub.calledWith('milestone-received').should.be.false(); + + mailStub.called.should.be.false(); + }); + + it('does not send email when a reason not to send email was provided', async function () { + const milestone = { + type: 'members', + value: 25000, + emailSentAt: Date.now(), + meta: { + reason: 'no-email' + } + }; + + await service.emails.notifyMilestoneReceived({milestone}); + + getEmailAlertUsersStub.calledWith('milestone-received').should.be.false(); + + mailStub.called.should.be.false(); + }); + + it('does not send email for a milestone without correct content', async function () { + const milestone = { + type: 'members', + value: 5000, // milestone not configured + emailSentAt: Date.now() + }; + + await service.emails.notifyMilestoneReceived({milestone}); + + getEmailAlertUsersStub.calledWith('milestone-received').should.be.false(); + + loggingWarningStub.calledOnce.should.be.true(); + mailStub.called.should.be.false(); - getEmailAlertUsersStub.calledWith('milestone-received').should.be.true(); }); }); });