Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1420,6 +1420,30 @@ module.exports = class MemberRepository {
to_status: updatedMember.get('status'),
...eventData
}, options);

const context = options?.context || {};
const source = this._resolveContextSource(context);
const shouldSendPaidWelcomeEmail = config.get('memberWelcomeEmailTestInbox') && WELCOME_EMAIL_SOURCES.includes(source);
let isPaidWelcomeEmailActive = false;
if (shouldSendPaidWelcomeEmail && this._AutomatedEmail) {
const paidWelcomeEmail = await this._AutomatedEmail.findOne({slug: MEMBER_WELCOME_EMAIL_SLUGS.paid}, options);
isPaidWelcomeEmailActive = paidWelcomeEmail && paidWelcomeEmail.get('lexical') && paidWelcomeEmail.get('status') === 'active';
}
if (updatedMember.get('status') === 'paid' && isPaidWelcomeEmailActive) {
await this._Outbox.add({
id: ObjectId().toHexString(),
event_type: MemberCreatedEvent.name,
payload: JSON.stringify({
memberId: memberModel.id,
email: memberModel.get('email'),
name: memberModel.get('name'),
source,
timestamp: new Date(),
status: 'paid'
})
}, options);
this.dispatchEvent(StartOutboxProcessingEvent.create({memberId: memberModel.id}), options);
}
}
}

Expand Down
59 changes: 59 additions & 0 deletions ghost/core/test/integration/services/member-welcome-emails.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,23 @@ describe('Member Welcome Emails Integration', function () {
lexical,
created_at: new Date()
});

await db.knex('automated_emails').insert({
id: ObjectId().toHexString(),
status: 'active',
name: 'Paid Member Welcome Email',
slug: MEMBER_WELCOME_EMAIL_SLUGS.paid,
subject: 'Welcome paid member to {{site.title}}',
lexical,
created_at: new Date()
});
});

afterEach(async function () {
await db.knex('outbox').del();
await db.knex('members').del();
await db.knex('automated_emails').where('slug', MEMBER_WELCOME_EMAIL_SLUGS.free).del();
await db.knex('automated_emails').where('slug', MEMBER_WELCOME_EMAIL_SLUGS.paid).del();
await configUtils.restore();
});

Expand Down Expand Up @@ -235,6 +246,54 @@ describe('Member Welcome Emails Integration', function () {
assert.equal(entriesAfterJob.length, 1);
assert.ok(entriesAfterJob.models[0].get('message'));
});

it('does not send email when paid template is inactive but entry has status paid', async function () {
await db.knex('automated_emails')
.where('slug', MEMBER_WELCOME_EMAIL_SLUGS.paid)
.update({status: 'inactive'});

await models.Outbox.add({
event_type: 'MemberCreatedEvent',
payload: JSON.stringify({
memberId: 'paid_member_1',
email: 'paid-inactive@example.com',
name: 'Paid Inactive Template Member',
status: 'paid'
}),
status: OUTBOX_STATUSES.PENDING
});

await scheduleInlineJob();

assert.equal(mailService.GhostMailer.prototype.send.callCount, 0);

const entriesAfterJob = await models.Outbox.findAll();
assert.equal(entriesAfterJob.length, 1);
assert.ok(entriesAfterJob.models[0].get('message').includes('inactive'));
});

it('does not send email when no paid template exists but entry has status paid', async function () {
await db.knex('automated_emails').where('slug', MEMBER_WELCOME_EMAIL_SLUGS.paid).del();

await models.Outbox.add({
event_type: 'MemberCreatedEvent',
payload: JSON.stringify({
memberId: 'paid_member_2',
email: 'paid-notemplate@example.com',
name: 'Paid No Template Member',
status: 'paid'
}),
status: OUTBOX_STATUSES.PENDING
});

await scheduleInlineJob();

assert.equal(mailService.GhostMailer.prototype.send.callCount, 0);

const entriesAfterJob = await models.Outbox.findAll();
assert.equal(entriesAfterJob.length, 1);
assert.ok(entriesAfterJob.models[0].get('message'));
});
});
});

Loading
Loading