Merge pull request #11445 from overleaf/revert-11432-revert-11154-tm-lg-subscription-recurly-cache-name

Re-instate change property name for recurly status cache to recurlyStatus

GitOrigin-RevId: 3b3415d818629676ee44dbc558a6b87469fd1af0
This commit is contained in:
Thomas 2023-01-26 12:42:34 +01:00 committed by Copybot
parent 28c9c32e1b
commit aa9b07d323
5 changed files with 62 additions and 8 deletions

View file

@ -249,7 +249,7 @@ async function updateSubscriptionFromRecurly(
subscription.recurlySubscription_id = recurlySubscription.uuid
subscription.planCode = updatedPlanCode
subscription.recurly = {
subscription.recurlyStatus = {
state: recurlySubscription.state,
trialStartedAt: recurlySubscription.trial_started_at,
trialEndsAt: recurlySubscription.trial_ends_at,

View file

@ -193,6 +193,7 @@ function buildUsersSubscriptionViewModel(user, callback) {
// Subscription DB object contains a recurly property, used to cache trial info
// on the project-list. However, this can cause the wrong template to render,
// if we do not have any subscription data from Recurly (recurlySubscription)
// TODO: Delete this workaround once recurly cache property name migration rolled out.
if (personalSubscription) {
delete personalSubscription.recurly
}
@ -354,7 +355,7 @@ async function getBestSubscription(user) {
individualSubscription &&
!individualSubscription.customAccount &&
individualSubscription.recurlySubscription_id &&
!individualSubscription.recurly?.state
!individualSubscription.recurlyStatus?.state
) {
const recurlySubscription = await RecurlyWrapper.promises.getSubscription(
individualSubscription.recurlySubscription_id,
@ -480,7 +481,7 @@ function _isPlanEqualOrBetter(planA, planB) {
function _getRemainingTrialDays(subscription) {
const now = new Date()
const trialEndDate = subscription.recurly?.trialEndsAt
const trialEndDate = subscription.recurlyStatus?.trialEndsAt
return trialEndDate && trialEndDate > now
? Math.ceil(
(trialEndDate.getTime() - now.getTime()) / (24 * 60 * 60 * 1000)

View file

@ -38,7 +38,7 @@ const SubscriptionSchema = new Schema({
},
},
},
recurly: {
recurlyStatus: {
state: {
type: String,
},

View file

@ -0,0 +1,53 @@
/* eslint-disable no-unused-vars */
const Helpers = require('./lib/helpers')
exports.tags = ['saas']
exports.migrate = async client => {
const { db } = client
// 'recurly' -> 'recurlyStatus'
await db.subscriptions.updateMany(
{
$and: [
{ recurlyStatus: { $exists: false } },
{ recurly: { $exists: true } },
],
},
{ $rename: { recurly: 'recurlyStatus' } }
)
// some records may have already recached the recurly status, discard old cache
await db.subscriptions.updateMany(
{
$and: [
{ recurlyStatus: { $exists: true } },
{ recurly: { $exists: true } },
],
},
{ $unset: { recurly: 1 } }
)
}
exports.rollback = async client => {
const { db } = client
// 'recurlyStatus' -> 'recurly'
await db.subscriptions.updateMany(
{
$and: [
{ recurly: { $exists: false } },
{ recurlyStatus: { $exists: true } },
],
},
{ $rename: { recurlyStatus: 'recurly' } }
)
// some records may have already recached the recurly status, discard old cache
await db.subscriptions.updateMany(
{
$and: [
{ recurlyStatus: { $exists: true } },
{ recurly: { $exists: true } },
],
},
{ $unset: { recurlyStatus: 1 } }
)
}

View file

@ -22,7 +22,7 @@ describe('SubscriptionViewModelBuilder', function () {
planCode: this.planCode,
plan: this.plan,
recurlySubscription_id: this.recurlySubscription_id,
recurly: {
recurlyStatus: {
state: 'active',
},
}
@ -46,7 +46,7 @@ describe('SubscriptionViewModelBuilder', function () {
this.groupSubscription = {
planCode: this.groupPlanCode,
plan: this.plan,
recurly: {
recurlyStatus: {
state: 'active',
},
}
@ -168,7 +168,7 @@ describe('SubscriptionViewModelBuilder', function () {
it('should return a individual subscription with remaining free trial days', async function () {
const threeDaysLater = new Date()
threeDaysLater.setDate(threeDaysLater.getDate() + 3)
this.individualSubscription.recurly.trialEndsAt = threeDaysLater
this.individualSubscription.recurlyStatus.trialEndsAt = threeDaysLater
this.SubscriptionLocator.promises.getUsersSubscription
.withArgs(this.user)
.resolves(this.individualSubscription)
@ -189,7 +189,7 @@ describe('SubscriptionViewModelBuilder', function () {
it('should return a individual subscription with free trial on last day', async function () {
const threeHoursLater = new Date()
threeHoursLater.setTime(threeHoursLater.getTime() + 3 * 60 * 60 * 1000)
this.individualSubscription.recurly.trialEndsAt = threeHoursLater
this.individualSubscription.recurlyStatus.trialEndsAt = threeHoursLater
this.SubscriptionLocator.promises.getUsersSubscription
.withArgs(this.user)
.resolves(this.individualSubscription)