فهرست منبع

Merge pull request #28200 from overleaf/rh-trial-extension-fix

Fix calculation of extended trial renewal date

GitOrigin-RevId: 3a73b4bba37ebcccfc80d123a1a6e6c286320571
roo hutton 1 سال پیش
والد
کامیت
3a7a7c8281

+ 4 - 3
services/web/app/src/Features/Subscription/RecurlyWrapper.js

@@ -700,11 +700,12 @@ const promises = {
     if (trialEndsAt == null) {
       trialEndsAt = new Date()
     }
-    const nextRenewalDate = new Date()
-    nextRenewalDate.setDate(trialEndsAt.getDate() + daysUntilExpire)
+    const nextRenewalDate = new Date(
+      trialEndsAt.getTime() + daysUntilExpire * 24 * 60 * 60 * 1000
+    )
     logger.debug(
       { subscriptionId, daysUntilExpire },
-      'Exending Free trial for user'
+      'Extending Free trial for user'
     )
     try {
       await RecurlyWrapper.promises.apiRequest({

+ 53 - 0
services/web/test/unit/src/Subscription/RecurlyWrapperTests.js

@@ -1472,4 +1472,57 @@ describe('RecurlyWrapper', function () {
       })
     })
   })
+
+  describe('extendTrial', function () {
+    beforeEach(function () {
+      this.subscriptionId = 'subscription-id-123'
+    })
+
+    afterEach(function () {
+      this.RecurlyWrapper.promises.apiRequest.restore()
+      tk.reset()
+    })
+
+    describe('with default parameters (7 days)', function () {
+      beforeEach(async function () {
+        tk.freeze(new Date('2025-01-25T10:30:00Z'))
+
+        this.apiRequest = sinon
+          .stub(this.RecurlyWrapper.promises, 'apiRequest')
+          .resolves()
+        await this.RecurlyWrapper.promises.extendTrial(this.subscriptionId)
+      })
+
+      it('should extend trial by 7 days from current time', function () {
+        const options = this.apiRequest.lastCall.args[0]
+        expect(options.qs.next_bill_date).to.deep.equal(
+          new Date('2025-02-01T10:30:00Z')
+        )
+      })
+    })
+
+    describe('extending trial across year boundary', function () {
+      beforeEach(async function () {
+        // Trial ends on December 28, 2025, extend by 14 days to cross into 2026
+        this.trialEndsAt = new Date('2025-12-28T12:00:00Z')
+        this.daysUntilExpire = 14
+
+        this.apiRequest = sinon
+          .stub(this.RecurlyWrapper.promises, 'apiRequest')
+          .resolves()
+        await this.RecurlyWrapper.promises.extendTrial(
+          this.subscriptionId,
+          this.trialEndsAt,
+          this.daysUntilExpire
+        )
+      })
+
+      it('should correctly calculate date across year boundary', function () {
+        const options = this.apiRequest.lastCall.args[0]
+        expect(options.qs.next_bill_date).to.deep.equal(
+          new Date('2026-01-11T12:00:00Z')
+        )
+      })
+    })
+  })
 })