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

Re-work unlinking dropbox when subscription ends

GitOrigin-RevId: 92cf5713d57e157cf544d0470d5fdb6e4010d5cd
This commit is contained in:
Shane Kilkelly 2020-11-04 09:53:06 +00:00 committed by Copybot
parent 654abb3b47
commit 405a221187
5 changed files with 48 additions and 21 deletions

View file

@ -13,12 +13,28 @@ const UserGetter = require('../User/UserGetter')
const FeaturesUpdater = {
refreshFeatures(userId, callback = () => {}) {
UserGetter.getUser(userId, { _id: 1, features: 1 }, (err, user) => {
if (err) {
return callback(err)
}
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, callback)
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()
})
})
})
},

View file

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

View file

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

View file

@ -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() {

View file

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