From 26143d13d32c1818a10b96604206728c4a6a15c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Alby?= Date: Thu, 7 Jan 2021 15:21:54 +0100 Subject: [PATCH] Merge pull request #3493 from overleaf/jel-reconfirmedAt-query Simplify email confirmation method GitOrigin-RevId: 05cc41694d4edbb744de611d3a626b036d27e863 --- services/web/app/src/Features/User/UserUpdater.js | 12 +++++------- services/web/test/unit/src/User/UserUpdaterTests.js | 5 +++-- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/services/web/app/src/Features/User/UserUpdater.js b/services/web/app/src/Features/User/UserUpdater.js index f3b7945925..322f4948cc 100644 --- a/services/web/app/src/Features/User/UserUpdater.js +++ b/services/web/app/src/Features/User/UserUpdater.js @@ -175,18 +175,16 @@ async function confirmEmail(userId, email) { _id: userId, 'emails.email': email } + + // only update confirmedAt if it was not previously set const update = { $set: { 'emails.$.reconfirmedAt': confirmedAt + }, + $min: { + 'emails.$.confirmedAt': confirmedAt } } - const user = await UserGetter.promises.getUser(userId) - const emailUnconfirmed = user.emails.find(emailData => { - if (emailData.email === email && !emailData.confirmedAt) return true - }) - if (emailUnconfirmed) { - update.$set['emails.$.confirmedAt'] = confirmedAt - } if (Features.hasFeature('affiliations')) { update.$unset = { diff --git a/services/web/test/unit/src/User/UserUpdaterTests.js b/services/web/test/unit/src/User/UserUpdaterTests.js index 481d068702..38b85a14cd 100644 --- a/services/web/test/unit/src/User/UserUpdaterTests.js +++ b/services/web/test/unit/src/User/UserUpdaterTests.js @@ -718,7 +718,6 @@ describe('UserUpdater', function() { describe('confirmEmail', function() { beforeEach(function() { this.UserUpdater.promises.updateUser = sinon.stub().resolves({ n: 1 }) - this.UserGetter.promises.getUser.resolves(this.stubbedUser) }) it('should update the email record', function(done) { @@ -735,8 +734,10 @@ describe('UserUpdater', function() { }, { $set: { - 'emails.$.confirmedAt': new Date(), 'emails.$.reconfirmedAt': new Date() + }, + $min: { + 'emails.$.confirmedAt': new Date() } } )