diff --git a/services/web/app/src/Features/Subscription/FeaturesUpdater.js b/services/web/app/src/Features/Subscription/FeaturesUpdater.js index d96da4ba17..c062efae52 100644 --- a/services/web/app/src/Features/Subscription/FeaturesUpdater.js +++ b/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() + }) + }) }) }, diff --git a/services/web/app/src/Features/Subscription/SubscriptionHandler.js b/services/web/app/src/Features/Subscription/SubscriptionHandler.js index 406bbf7614..5cf835d77e 100644 --- a/services/web/app/src/Features/Subscription/SubscriptionHandler.js +++ b/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() } diff --git a/services/web/app/src/infrastructure/Events.js b/services/web/app/src/infrastructure/Events.js deleted file mode 100644 index 25c2261c1a..0000000000 --- a/services/web/app/src/infrastructure/Events.js +++ /dev/null @@ -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() diff --git a/services/web/test/unit/src/Subscription/FeaturesUpdaterTests.js b/services/web/test/unit/src/Subscription/FeaturesUpdaterTests.js index 7a3ecd6738..2336eda827 100644 --- a/services/web/test/unit/src/Subscription/FeaturesUpdaterTests.js +++ b/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() { diff --git a/services/web/test/unit/src/Subscription/SubscriptionHandlerTests.js b/services/web/test/unit/src/Subscription/SubscriptionHandlerTests.js index 1e9c296b4c..bd086d954e 100644 --- a/services/web/test/unit/src/Subscription/SubscriptionHandlerTests.js +++ b/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) - }) }) })