Skip to content

Commit f688c77

Browse files
authored
Bailed out of welcome email automation if member status changes (TryGhost#27537)
closes https://linear.app/ghost/issue/NY-1194
1 parent 4f6d484 commit f688c77

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

  • ghost/core
    • core/server/services/welcome-email-automations
    • test/integration/services/welcome-email-automations

ghost/core/core/server/services/welcome-email-automations/poll.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,11 @@ async function processRun({
199199
}
200200

201201
// TODO(NY-1193): Bail if member is unsubscribed
202-
// TODO(NY-1194): Bail if member's status has changed
202+
203+
if (member.get('status') !== memberStatus) {
204+
await markExited(run.id, 'member changed status');
205+
return;
206+
}
203207

204208
await memberWelcomeEmailService.api.send({
205209
member: {

ghost/core/test/integration/services/welcome-email-automations/poll.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,43 @@ describe('welcome email automations poll', function () {
429429
assert.deepEqual(await readTrackedRecipients(), []);
430430
});
431431

432+
it('bails out if member status has changed since the run was created', async function () {
433+
const automation = await createAutomation({
434+
slug: MEMBER_WELCOME_EMAIL_SLUGS.free
435+
});
436+
const automatedEmail = await createAutomatedEmail({
437+
welcome_email_automation_id: automation.id
438+
});
439+
const member = await createMember({
440+
status: 'free'
441+
});
442+
const run = await createRun({
443+
welcome_email_automation_id: automation.id,
444+
member_id: member.id,
445+
next_welcome_email_automated_email_id: automatedEmail.id,
446+
ready_at: new Date(Date.now() - 1000)
447+
});
448+
449+
await testUtils.knex('members').where({id: member.id}).update({
450+
status: 'paid',
451+
updated_at: new Date()
452+
});
453+
454+
await poll(options);
455+
456+
sinon.assert.notCalled(options.memberWelcomeEmailService.api.send);
457+
sinon.assert.notCalled(options.enqueueAnotherPollNow);
458+
sinon.assert.notCalled(options.enqueueAnotherPollAt);
459+
assert.deepEqual(await readTrackedRecipients(), []);
460+
461+
const updatedRun = await readRun(run.id);
462+
assert.equal(updatedRun.exit_reason, 'member changed status');
463+
assert.equal(updatedRun.next_welcome_email_automated_email_id, null);
464+
assert.equal(updatedRun.ready_at, null);
465+
assert.equal(updatedRun.step_started_at, null);
466+
assert.equal(updatedRun.step_attempts, 0);
467+
});
468+
432469
it('fails if run stored with more attempts than now supported', async function () {
433470
const automation = await createAutomation();
434471
const automatedEmail = await createAutomatedEmail({

0 commit comments

Comments
 (0)