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() } } )