Просмотр исходного кода

Merge pull request #28808 from overleaf/ii-await-user-handler

[web] Promisify UserHandler

GitOrigin-RevId: 2daa6f74ec566851d208bf1b3d12d89ecf183383
ilkin-overleaf 10 месяцев назад
Родитель
Сommit
22b38d02b0

+ 2 - 4
services/web/app/src/Features/Authentication/AuthenticationController.js

@@ -654,10 +654,8 @@ function _afterLoginSessionSetup(req, user, callback) {
 const _afterLoginSessionSetupAsync = promisify(_afterLoginSessionSetup)
 
 function _loginAsyncHandlers(req, user, anonymousAnalyticsId, isNewUser) {
-  UserHandler.populateTeamInvites(user, err => {
-    if (err != null) {
-      logger.warn({ err }, 'error setting up login data')
-    }
+  UserHandler.promises.populateTeamInvites(user).catch(err => {
+    logger.warn({ err }, 'error setting up login data')
   })
   LoginRateLimiter.recordSuccessfulLogin(user.email, () => {})
   AuthenticationController._recordSuccessfulLogin(user._id, () => {})

+ 9 - 10
services/web/app/src/Features/User/UserHandler.js

@@ -1,14 +1,13 @@
-const { callbackify, promisify } = require('@overleaf/promise-utils')
+const { callbackify } = require('util')
 const TeamInvitesHandler = require('../Subscription/TeamInvitesHandler')
 const {
   db,
   READ_PREFERENCE_SECONDARY,
 } = require('../../infrastructure/mongodb')
 
-function populateTeamInvites(user, callback) {
-  TeamInvitesHandler.createTeamInvitesForLegacyInvitedEmail(
-    user.email,
-    callback
+async function populateTeamInvites(user) {
+  return await TeamInvitesHandler.promises.createTeamInvitesForLegacyInvitedEmail(
+    user.email
   )
 }
 
@@ -22,10 +21,10 @@ async function countActiveUsers() {
 }
 
 module.exports = {
-  populateTeamInvites,
+  populateTeamInvites: callbackify(populateTeamInvites),
   countActiveUsers: callbackify(countActiveUsers),
-}
-module.exports.promises = {
-  populateTeamInvites: promisify(populateTeamInvites),
-  countActiveUsers,
+  promises: {
+    populateTeamInvites,
+    countActiveUsers,
+  },
 }

+ 6 - 4
services/web/test/unit/src/Authentication/AuthenticationControllerTests.js

@@ -94,7 +94,9 @@ describe('AuthenticationController', function () {
           },
         }),
         '../User/UserHandler': (this.UserHandler = {
-          populateTeamInvites: sinon.stub(),
+          promises: {
+            populateTeamInvites: sinon.stub().resolves(),
+          },
         }),
         '../Analytics/AnalyticsManager': (this.AnalyticsManager = {
           recordEventForUserInBackground: sinon.stub(),
@@ -556,7 +558,7 @@ describe('AuthenticationController', function () {
       })
 
       it('should not setup the user data in the background', function () {
-        this.UserHandler.populateTeamInvites.called.should.equal(false)
+        this.UserHandler.promises.populateTeamInvites.called.should.equal(false)
       })
 
       it('should record a failed login', function () {
@@ -1134,7 +1136,7 @@ describe('AuthenticationController', function () {
       this.AuthenticationController._clearRedirectFromSession = sinon.stub()
       this.AuthenticationController._redirectToReconfirmPage = sinon.stub()
       this.UserSessionsManager.trackSession = sinon.stub()
-      this.UserHandler.populateTeamInvites = sinon.stub()
+      this.UserHandler.promises.populateTeamInvites = sinon.stub().resolves()
       this.LoginRateLimiter.recordSuccessfulLogin = sinon.stub()
       this.AuthenticationController._recordSuccessfulLogin = sinon.stub()
       this.AnalyticsManager.recordEvent = sinon.stub()
@@ -1461,7 +1463,7 @@ describe('AuthenticationController', function () {
       })
 
       it('should setup the user data in the background', function () {
-        this.UserHandler.populateTeamInvites
+        this.UserHandler.promises.populateTeamInvites
           .calledWith(this.user)
           .should.equal(true)
       })

+ 6 - 4
services/web/test/unit/src/User/UserHandlerTests.js

@@ -12,7 +12,9 @@ describe('UserHandler', function () {
     }
 
     this.TeamInvitesHandler = {
-      createTeamInvitesForLegacyInvitedEmail: sinon.stub().yields(),
+      promises: {
+        createTeamInvitesForLegacyInvitedEmail: sinon.stub().resolves(),
+      },
     }
 
     this.db = {
@@ -30,12 +32,12 @@ describe('UserHandler', function () {
   })
 
   describe('populateTeamInvites', function () {
-    beforeEach(function (done) {
-      this.UserHandler.populateTeamInvites(this.user, done)
+    beforeEach(async function () {
+      await this.UserHandler.promises.populateTeamInvites(this.user)
     })
 
     it('notifies the user about legacy team invites', function () {
-      this.TeamInvitesHandler.createTeamInvitesForLegacyInvitedEmail
+      this.TeamInvitesHandler.promises.createTeamInvitesForLegacyInvitedEmail
         .calledWith(this.user.email)
         .should.eq(true)
     })