diff --git a/services/web/app/src/Features/Subscription/RecurlyClient.mjs b/services/web/app/src/Features/Subscription/RecurlyClient.mjs index d983fcfa8c..b9c6fd1ae0 100644 --- a/services/web/app/src/Features/Subscription/RecurlyClient.mjs +++ b/services/web/app/src/Features/Subscription/RecurlyClient.mjs @@ -799,6 +799,24 @@ async function terminateSubscriptionByUuid(subscriptionUuid) { return subscription } +/** + * Get the Recurly admin dashboard url for a user + * + * @param {string} userId + * @returns {string} + */ +function getCustomerAdminUrlFromUserId(userId) { + const isStagOrDev = + Settings.siteUrl.includes('dev-overleaf') || + Settings.siteUrl.includes('stag-overleaf') + + if (isStagOrDev) { + return `https://sharelatex-sandbox.recurly.com/accounts/${userId}` + } + + return `https://sharelatex.recurly.com/accounts/${userId}` +} + export default { errors: recurly.errors, @@ -823,6 +841,7 @@ export default { getPastDueInvoices: callbackify(getPastDueInvoices), failInvoice: callbackify(failInvoice), terminateSubscriptionByUuid: callbackify(terminateSubscriptionByUuid), + getCustomerAdminUrlFromUserId, promises: { getSubscription, diff --git a/services/web/test/unit/src/Subscription/RecurlyClient.test.mjs b/services/web/test/unit/src/Subscription/RecurlyClient.test.mjs index ec538a8c0f..d01acc8a87 100644 --- a/services/web/test/unit/src/Subscription/RecurlyClient.test.mjs +++ b/services/web/test/unit/src/Subscription/RecurlyClient.test.mjs @@ -789,4 +789,31 @@ describe('RecurlyClient', function () { expect(invoices).to.deep.equal(pastDueInvoices) }) }) + + describe('getCustomerAdminUrlFromUserId', function () { + it('should return staging URL for dev-overleaf sites', async function (ctx) { + ctx.settings.siteUrl = 'https://dev-overleaf.example.com' + const userId = 'user-123' + const url = await ctx.RecurlyClient.getCustomerAdminUrlFromUserId(userId) + expect(url).to.equal( + 'https://sharelatex-sandbox.recurly.com/accounts/user-123' + ) + }) + + it('should return staging URL for stag-overleaf sites', async function (ctx) { + ctx.settings.siteUrl = 'https://stag-overleaf.example.com' + const userId = 'user-456' + const url = await ctx.RecurlyClient.getCustomerAdminUrlFromUserId(userId) + expect(url).to.equal( + 'https://sharelatex-sandbox.recurly.com/accounts/user-456' + ) + }) + + it('should return production URL for production sites', async function (ctx) { + ctx.settings.siteUrl = 'https://www.overleaf.com' + const userId = 'user-789' + const url = await ctx.RecurlyClient.getCustomerAdminUrlFromUserId(userId) + expect(url).to.equal('https://sharelatex.recurly.com/accounts/user-789') + }) + }) })