From f9db088d57ec334f72378cb2df8f21d2bb696be5 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Wed, 31 Jan 2024 10:47:04 +0000 Subject: [PATCH] Merge pull request #16835 from overleaf/bg-google-spam-temporarily-disable-emails Temporarily disable onboarding emails GitOrigin-RevId: a1967bc793fcd7b3b90f812fb8c9b9d83f704093 --- .../Subscription/SubscriptionEmailHandler.js | 15 +++-- .../User/UserOnboardingEmailManager.js | 3 +- services/web/config/settings.defaults.js | 1 + .../SubscriptionEmailHandlerTests.js | 52 +++++++++++------ .../User/UserOnboardingEmailManagerTests.js | 57 ++++++++++++------- 5 files changed, 85 insertions(+), 43 deletions(-) diff --git a/services/web/app/src/Features/Subscription/SubscriptionEmailHandler.js b/services/web/app/src/Features/Subscription/SubscriptionEmailHandler.js index 844ea9a6ed..c0b32d76bd 100644 --- a/services/web/app/src/Features/Subscription/SubscriptionEmailHandler.js +++ b/services/web/app/src/Features/Subscription/SubscriptionEmailHandler.js @@ -2,6 +2,7 @@ const EmailHandler = require('../Email/EmailHandler') const UserGetter = require('../User/UserGetter') require('./SubscriptionEmailBuilder') const PlansLocator = require('./PlansLocator') +const Settings = require('@overleaf/settings') const SubscriptionEmailHandler = { async sendTrialOnboardingEmail(userId, planCode) { @@ -13,13 +14,15 @@ const SubscriptionEmailHandler = { if (!plan) { throw new Error('unknown paid plan: ' + planCode) } - const emailOptions = { - to: user.email, - sendingUser_id: userId, - planName: plan.name, - features: plan.features, + if (Settings.enableOnboardingEmails) { + const emailOptions = { + to: user.email, + sendingUser_id: userId, + planName: plan.name, + features: plan.features, + } + await EmailHandler.promises.sendEmail('trialOnboarding', emailOptions) } - await EmailHandler.promises.sendEmail('trialOnboarding', emailOptions) }, } diff --git a/services/web/app/src/Features/User/UserOnboardingEmailManager.js b/services/web/app/src/Features/User/UserOnboardingEmailManager.js index 5adaae6a31..dd984c4bd2 100644 --- a/services/web/app/src/Features/User/UserOnboardingEmailManager.js +++ b/services/web/app/src/Features/User/UserOnboardingEmailManager.js @@ -2,6 +2,7 @@ const Queues = require('../../infrastructure/Queues') const EmailHandler = require('../Email/EmailHandler') const UserUpdater = require('./UserUpdater') const UserGetter = require('./UserGetter') +const Settings = require('@overleaf/settings') const ONE_DAY_MS = 24 * 60 * 60 * 1000 @@ -15,7 +16,7 @@ async function scheduleOnboardingEmail(user) { async function sendOnboardingEmail(userId) { const user = await UserGetter.promises.getUser({ _id: userId }, { email: 1 }) - if (user) { + if (Settings.enableOnboardingEmails && user) { await EmailHandler.promises.sendEmail('userOnboardingEmail', { to: user.email, }) diff --git a/services/web/config/settings.defaults.js b/services/web/config/settings.defaults.js index 15168f4ebb..923d76d12f 100644 --- a/services/web/config/settings.defaults.js +++ b/services/web/config/settings.defaults.js @@ -397,6 +397,7 @@ module.exports = { enableSubscriptions: false, restrictedCountries: [], + enableOnboardingEmails: process.env.ENABLE_ONBOARDING_EMAILS === 'true', enabledLinkedFileTypes: (process.env.ENABLED_LINKED_FILE_TYPES || '').split( ',' diff --git a/services/web/test/unit/src/Subscription/SubscriptionEmailHandlerTests.js b/services/web/test/unit/src/Subscription/SubscriptionEmailHandlerTests.js index 11a4abeff4..c3fca396a9 100644 --- a/services/web/test/unit/src/Subscription/SubscriptionEmailHandlerTests.js +++ b/services/web/test/unit/src/Subscription/SubscriptionEmailHandlerTests.js @@ -29,27 +29,45 @@ describe('SubscriptionEmailHandler', function () { features: { collaborators: 42 }, }), }), + '@overleaf/settings': (this.Settings = { + enableOnboardingEmails: true, + }), }, }) }) - it('sends trail onboarding email', async function () { - await this.SubscriptionEmailHandler.sendTrialOnboardingEmail( - this.userId, - 'foo-plan-code' - ) + describe('when onboarding emails are disabled', function () { + beforeEach(function () { + this.Settings.enableOnboardingEmails = false + }) + it('does not send a trial onboarding email', async function () { + await this.SubscriptionEmailHandler.sendTrialOnboardingEmail( + this.userId, + 'foo-plan-code' + ) + expect(this.EmailHandler.promises.sendEmail).to.not.have.been.called + }) + }) - expect(this.PlansLocator.findLocalPlanInSettings).to.have.been.calledWith( - 'foo-plan-code' - ) - expect(this.EmailHandler.promises.sendEmail.lastCall.args).to.deep.equal([ - 'trialOnboarding', - { - to: this.email, - sendingUser_id: this.userId, - planName: 'foo', - features: { collaborators: 42 }, - }, - ]) + describe('when onboarding emails are enabled', function () { + it('sends trial onboarding email', async function () { + await this.SubscriptionEmailHandler.sendTrialOnboardingEmail( + this.userId, + 'foo-plan-code' + ) + + expect(this.PlansLocator.findLocalPlanInSettings).to.have.been.calledWith( + 'foo-plan-code' + ) + expect(this.EmailHandler.promises.sendEmail.lastCall.args).to.deep.equal([ + 'trialOnboarding', + { + to: this.email, + sendingUser_id: this.userId, + planName: 'foo', + features: { collaborators: 42 }, + }, + ]) + }) }) }) diff --git a/services/web/test/unit/src/User/UserOnboardingEmailManagerTests.js b/services/web/test/unit/src/User/UserOnboardingEmailManagerTests.js index 2980c80a9e..3243695dde 100644 --- a/services/web/test/unit/src/User/UserOnboardingEmailManagerTests.js +++ b/services/web/test/unit/src/User/UserOnboardingEmailManagerTests.js @@ -49,6 +49,9 @@ describe('UserOnboardingEmailManager', function () { '../Email/EmailHandler': this.EmailHandler, './UserGetter': this.UserGetter, './UserUpdater': this.UserUpdater, + '@overleaf/settings': (this.Settings = { + enableOnboardingEmails: true, + }), }, }) }) @@ -68,26 +71,42 @@ describe('UserOnboardingEmailManager', function () { }) describe('sendOnboardingEmail', function () { - it('should send onboarding email and update user', async function () { - await this.UserOnboardingEmailManager.sendOnboardingEmail(this.fakeUserId) - expect(this.EmailHandler.promises.sendEmail).to.have.been.calledWith( - 'userOnboardingEmail', - { - to: this.fakeUserEmail, - } - ) - expect(this.UserUpdater.promises.updateUser).to.have.been.calledWith( - this.fakeUserId, - { $set: { onboardingEmailSentAt: sinon.match.date } } - ) - }) - - it('should stop if user is not found', async function () { - await this.UserOnboardingEmailManager.sendOnboardingEmail({ - data: { userId: 'deleted-user' }, + describe('when onboarding emails are disabled', function () { + beforeEach(function () { + this.Settings.enableOnboardingEmails = false + }) + it('should not send onboarding email', async function () { + await this.UserOnboardingEmailManager.sendOnboardingEmail( + this.fakeUserId + ) + expect(this.EmailHandler.promises.sendEmail).not.to.have.been.called + expect(this.UserUpdater.promises.updateUser).not.to.have.been.called + }) + }) + describe('when onboarding emails are enabled', function () { + it('should send onboarding email and update user', async function () { + await this.UserOnboardingEmailManager.sendOnboardingEmail( + this.fakeUserId + ) + expect(this.EmailHandler.promises.sendEmail).to.have.been.calledWith( + 'userOnboardingEmail', + { + to: this.fakeUserEmail, + } + ) + expect(this.UserUpdater.promises.updateUser).to.have.been.calledWith( + this.fakeUserId, + { $set: { onboardingEmailSentAt: sinon.match.date } } + ) + }) + + it('should stop if user is not found', async function () { + await this.UserOnboardingEmailManager.sendOnboardingEmail({ + data: { userId: 'deleted-user' }, + }) + expect(this.EmailHandler.promises.sendEmail).not.to.have.been.called + expect(this.UserUpdater.promises.updateUser).not.to.have.been.called }) - expect(this.EmailHandler.promises.sendEmail).not.to.have.been.called - expect(this.UserUpdater.promises.updateUser).not.to.have.been.called }) }) })