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

Merge pull request #3318 from overleaf/sk-remove-events-module

Re-work unlinking dropbox when subscription ends

GitOrigin-RevId: 92cf5713d57e157cf544d0470d5fdb6e4010d5cd
Shane Kilkelly 5 лет назад
Родитель
Сommit
405a221187

+ 21 - 5
services/web/app/src/Features/Subscription/FeaturesUpdater.js

@@ -13,12 +13,28 @@ const UserGetter = require('../User/UserGetter')
 
 const FeaturesUpdater = {
   refreshFeatures(userId, callback = () => {}) {
-    FeaturesUpdater._computeFeatures(userId, (error, features) => {
-      if (error) {
-        return callback(error)
+    UserGetter.getUser(userId, { _id: 1, features: 1 }, (err, user) => {
+      if (err) {
+        return callback(err)
       }
-      logger.log({ userId, features }, 'updating user features')
-      UserFeaturesUpdater.updateFeatures(userId, features, callback)
+      const oldFeatures = _.clone(user.features)
+      FeaturesUpdater._computeFeatures(userId, (error, features) => {
+        if (error) {
+          return callback(error)
+        }
+        logger.log({ userId, features }, 'updating user features')
+        UserFeaturesUpdater.updateFeatures(userId, features, err => {
+          if (err) {
+            return callback(err)
+          }
+          if (oldFeatures.dropbox === true && features.dropbox === false) {
+            logger.log({ userId }, '[FeaturesUpdater] must unlink dropbox')
+            const Modules = require('../../infrastructure/Modules')
+            Modules.hooks.fire('removeDropbox', userId, () => {})
+          }
+          return callback()
+        })
+      })
     })
   },
 

+ 0 - 2
services/web/app/src/Features/Subscription/SubscriptionHandler.js

@@ -6,7 +6,6 @@ const logger = require('logger-sharelatex')
 const SubscriptionUpdater = require('./SubscriptionUpdater')
 const LimitationsManager = require('./LimitationsManager')
 const EmailHandler = require('../Email/EmailHandler')
-const Events = require('../../infrastructure/Events')
 const Analytics = require('../Analytics/AnalyticsManager')
 
 const SubscriptionHandler = {
@@ -171,7 +170,6 @@ const SubscriptionHandler = {
                 ),
               ONE_HOUR_IN_MS
             )
-            Events.emit('cancelSubscription', user._id)
             Analytics.recordEvent(user._id, 'subscription-canceled')
             callback()
           }

+ 0 - 4
services/web/app/src/infrastructure/Events.js

@@ -1,4 +0,0 @@
-// TODO: This file was created by bulk-decaffeinate.
-// Sanity-check the conversion and remove this comment.
-const events = require('events')
-module.exports = new events.EventEmitter()

+ 27 - 3
services/web/test/unit/src/Subscription/FeaturesUpdaterTests.js

@@ -38,13 +38,20 @@ describe('FeaturesUpdater', function() {
         '../Referal/ReferalFeatures': (this.ReferalFeatures = {}),
         './V1SubscriptionManager': (this.V1SubscriptionManager = {}),
         '../Institutions/InstitutionsFeatures': (this.InstitutionsFeatures = {}),
-        '../User/UserGetter': (this.UserGetter = {})
+        '../User/UserGetter': (this.UserGetter = {}),
+        '../../infrastructure/Modules': (this.Modules = {
+          hooks: { fire: sinon.stub() }
+        })
       }
     }))
   })
 
   describe('refreshFeatures', function() {
     beforeEach(function() {
+      this.user = {
+        _id: this.user_id,
+        features: {}
+      }
       this.UserFeaturesUpdater.updateFeatures = sinon.stub().yields()
       this.FeaturesUpdater._getIndividualFeatures = sinon
         .stub()
@@ -64,10 +71,9 @@ describe('FeaturesUpdater', function() {
       this.FeaturesUpdater._mergeFeatures = sinon
         .stub()
         .returns({ merged: 'features' })
-      this.UserGetter.getUser = sinon.stub().yields(null, {})
+      this.UserGetter.getUser = sinon.stub().yields(null, this.user)
       return (this.callback = sinon.stub())
     })
-
     describe('normally', function() {
       beforeEach(function() {
         return this.FeaturesUpdater.refreshFeatures(this.user_id, this.callback)
@@ -148,6 +154,24 @@ describe('FeaturesUpdater', function() {
           .should.equal(true)
       })
     })
+    describe('when losing dropbox feature', function() {
+      beforeEach(function() {
+        this.user = {
+          _id: this.user_id,
+          features: { dropbox: true }
+        }
+        this.UserGetter.getUser = sinon.stub().yields(null, this.user)
+        this.FeaturesUpdater._mergeFeatures = sinon
+          .stub()
+          .returns({ dropbox: false })
+        return this.FeaturesUpdater.refreshFeatures(this.user_id, this.callback)
+      })
+      it('should fire module hook to unlink dropbox', function() {
+        this.Modules.hooks.fire
+          .calledWith('removeDropbox', this.user._id)
+          .should.equal(true)
+      })
+    })
   })
 
   describe('_mergeFeatures', function() {

+ 0 - 7
services/web/test/unit/src/Subscription/SubscriptionHandlerTests.js

@@ -91,7 +91,6 @@ describe('SubscriptionHandler', function() {
         './LimitationsManager': this.LimitationsManager,
         '../Email/EmailHandler': this.EmailHandler,
         '../Dropbox/DropboxHandler': this.DropboxHandler,
-        '../../infrastructure/Events': (this.Events = { emit: sinon.stub() }),
         '../Analytics/AnalyticsManager': this.AnalyticsManager
       }
     })
@@ -311,12 +310,6 @@ describe('SubscriptionHandler', function() {
           .calledWith(this.subscription.recurlySubscription_id)
           .should.equal(true)
       })
-
-      it('should trigger the cancel subscription event', function() {
-        this.Events.emit
-          .calledWith('cancelSubscription', this.user._id)
-          .should.equal(true)
-      })
     })
   })