Explorar o código

Merge pull request #2543 from overleaf/hb-remove-unused-v1-endpoints

Remove unused v1 endpoints

GitOrigin-RevId: b7d629b42f10074cf17dfb1d851316f970584a81
nate stemen %!s(int64=6) %!d(string=hai) anos
pai
achega
60554cb634

+ 22 - 0
services/web/app/src/Features/Subscription/FeaturesUpdater.js

@@ -278,6 +278,28 @@ const FeaturesUpdater = {
     }
 
     return mismatchReasons
+  },
+
+  doSyncFromV1(v1UserId, callback) {
+    logger.log({ v1UserId }, '[AccountSync] starting account sync')
+    return UserGetter.getUser({ 'overleaf.id': v1UserId }, { _id: 1 }, function(
+      err,
+      user
+    ) {
+      if (err != null) {
+        logger.warn({ v1UserId }, '[AccountSync] error getting user')
+        return callback(err)
+      }
+      if ((user != null ? user._id : undefined) == null) {
+        logger.warn({ v1UserId }, '[AccountSync] no user found for v1 id')
+        return callback(null)
+      }
+      logger.log(
+        { v1UserId, userId: user._id },
+        '[AccountSync] updating user subscription and features'
+      )
+      return FeaturesUpdater.refreshFeatures(user._id, callback)
+    })
   }
 }
 

+ 93 - 1
services/web/test/unit/src/Subscription/FeaturesUpdaterTests.js

@@ -31,7 +31,10 @@ describe('FeaturesUpdater', function() {
         './SubscriptionLocator': (this.SubscriptionLocator = {}),
         './PlansLocator': (this.PlansLocator = {}),
         'logger-sharelatex': {
-          log() {}
+          log() {},
+          warn(obj, text) {
+            console.log(text)
+          }
         },
         'settings-sharelatex': (this.Settings = {}),
         '../Referal/ReferalFeatures': (this.ReferalFeatures = {}),
@@ -318,4 +321,93 @@ describe('FeaturesUpdater', function() {
       })
     })
   })
+
+  describe('doSyncFromV1', function() {
+    beforeEach(function() {
+      this.v1UserId = 1
+      this.user = {
+        _id: this.user_id,
+        email: 'user@example.com',
+        overleaf: {
+          id: this.v1UserId
+        }
+      }
+
+      this.UserGetter.getUser = sinon.stub().callsArgWith(2, null, this.user)
+      this.FeaturesUpdater.refreshFeatures = sinon.stub().yields(null)
+      return (this.call = cb => {
+        return this.FeaturesUpdater.doSyncFromV1(this.v1UserId, cb)
+      })
+    })
+
+    describe('when all goes well', function() {
+      it('should call getUser', function(done) {
+        return this.call(() => {
+          expect(this.UserGetter.getUser.callCount).to.equal(1)
+          expect(
+            this.UserGetter.getUser.calledWith({ 'overleaf.id': this.v1UserId })
+          ).to.equal(true)
+          return done()
+        })
+      })
+
+      it('should call refreshFeatures', function(done) {
+        return this.call(() => {
+          expect(this.FeaturesUpdater.refreshFeatures.callCount).to.equal(1)
+          expect(
+            this.FeaturesUpdater.refreshFeatures.calledWith(this.user_id)
+          ).to.equal(true)
+          return done()
+        })
+      })
+
+      it('should not produce an error', function(done) {
+        return this.call(err => {
+          expect(err).to.not.exist
+          return done()
+        })
+      })
+    })
+
+    describe('when getUser produces an error', function() {
+      beforeEach(function() {
+        return (this.UserGetter.getUser = sinon
+          .stub()
+          .callsArgWith(2, new Error('woops')))
+      })
+
+      it('should not call refreshFeatures', function() {
+        expect(this.FeaturesUpdater.refreshFeatures.callCount).to.equal(0)
+      })
+
+      it('should produce an error', function(done) {
+        return this.call(err => {
+          expect(err).to.exist
+          return done()
+        })
+      })
+    })
+
+    describe('when getUser does not find a user', function() {
+      beforeEach(function() {
+        return (this.UserGetter.getUser = sinon
+          .stub()
+          .callsArgWith(2, null, null))
+      })
+
+      it('should not call refreshFeatures', function(done) {
+        return this.call(() => {
+          expect(this.FeaturesUpdater.refreshFeatures.callCount).to.equal(0)
+          return done()
+        })
+      })
+
+      it('should not produce an error', function(done) {
+        return this.call(err => {
+          expect(err).to.not.exist
+          return done()
+        })
+      })
+    })
+  })
 })