Merge pull request #20937 from overleaf/jdt-ai-addon-grants-feat

Grant feature access to users purchasing the ai add-on

GitOrigin-RevId: a4183beab957e4d0c362c8f0978be395b86ce398
This commit is contained in:
Jimmy Domagala-Tang 2024-10-10 10:44:38 -04:00 committed by Copybot
parent aa9a246346
commit 27fb9aadf3
4 changed files with 37 additions and 1 deletions

View file

@ -15,6 +15,8 @@ const AnalyticsManager = require('../Analytics/AnalyticsManager')
const Queues = require('../../infrastructure/Queues') const Queues = require('../../infrastructure/Queues')
const Modules = require('../../infrastructure/Modules') const Modules = require('../../infrastructure/Modules')
const AI_ADDON_CODE = 'assistant'
/** /**
* Enqueue a job for refreshing features for the given user * Enqueue a job for refreshing features for the given user
*/ */
@ -147,7 +149,13 @@ async function _getV1Features(user) {
} }
function _subscriptionToFeatures(subscription) { function _subscriptionToFeatures(subscription) {
return _planCodeToFeatures(subscription && subscription.planCode) const addonFeatures = _subscriptionAddonsToFeatures(
subscription && subscription.addOns
)
const planFeatures = _planCodeToFeatures(
subscription && subscription.planCode
)
return FeaturesHelper.mergeFeatures(addonFeatures, planFeatures)
} }
function _planCodeToFeatures(planCode) { function _planCodeToFeatures(planCode) {
@ -162,6 +170,18 @@ function _planCodeToFeatures(planCode) {
} }
} }
function _subscriptionAddonsToFeatures(addOns) {
if (!addOns) {
return {}
}
const hasAiAddon = addOns.some(addOn => addOn.addOnCode === AI_ADDON_CODE)
if (hasAiAddon) {
return { aiErrorAssistant: true }
} else {
return {}
}
}
async function doSyncFromV1(v1UserId) { async function doSyncFromV1(v1UserId) {
logger.debug({ v1UserId }, '[AccountSync] starting account sync') logger.debug({ v1UserId }, '[AccountSync] starting account sync')
const user = await UserGetter.promises.getUser( const user = await UserGetter.promises.getUser(

View file

@ -44,6 +44,10 @@ async function getSubscription(subscriptionId) {
return await client.getSubscription(subscriptionId) return await client.getSubscription(subscriptionId)
} }
async function getSubscriptionByUuid(subscriptionUuid) {
return await client.getSubscription('uuid-' + subscriptionUuid)
}
async function changeSubscription(subscriptionId, body) { async function changeSubscription(subscriptionId, body) {
const change = await client.createSubscriptionChange(subscriptionId, body) const change = await client.createSubscriptionChange(subscriptionId, body)
logger.debug( logger.debug(
@ -101,6 +105,7 @@ module.exports = {
getAccountForUserId: callbackify(getAccountForUserId), getAccountForUserId: callbackify(getAccountForUserId),
createAccountForUserId: callbackify(createAccountForUserId), createAccountForUserId: callbackify(createAccountForUserId),
getSubscription: callbackify(getSubscription), getSubscription: callbackify(getSubscription),
getSubscriptionByUuid: callbackify(getSubscriptionByUuid),
changeSubscription: callbackify(changeSubscription), changeSubscription: callbackify(changeSubscription),
changeSubscriptionByUuid: callbackify(changeSubscriptionByUuid), changeSubscriptionByUuid: callbackify(changeSubscriptionByUuid),
removeSubscriptionChange: callbackify(removeSubscriptionChange), removeSubscriptionChange: callbackify(removeSubscriptionChange),
@ -111,6 +116,7 @@ module.exports = {
promises: { promises: {
getSubscription, getSubscription,
getSubscriptionByUuid,
getAccountForUserId, getAccountForUserId,
createAccountForUserId, createAccountForUserId,
changeSubscription, changeSubscription,

View file

@ -305,8 +305,17 @@ async function updateSubscriptionFromRecurly(
return return
} }
const addOns = recurlySubscription?.subscription_add_ons?.map(addOn => {
return {
addOnCode: addOn.add_on_code,
quantity: addOn.quantity,
unitAmountInCents: addOn.unit_amount_in_cents,
}
})
subscription.recurlySubscription_id = recurlySubscription.uuid subscription.recurlySubscription_id = recurlySubscription.uuid
subscription.planCode = updatedPlanCode subscription.planCode = updatedPlanCode
subscription.addOns = addOns || []
subscription.recurlyStatus = { subscription.recurlyStatus = {
state: recurlySubscription.state, state: recurlySubscription.state,
trialStartedAt: recurlySubscription.trial_started_at, trialStartedAt: recurlySubscription.trial_started_at,

View file

@ -36,6 +36,7 @@ const SubscriptionSchema = new Schema(
managedUsers: { type: Boolean, default: null }, managedUsers: { type: Boolean, default: null },
groupSSO: { type: Boolean, default: null }, groupSSO: { type: Boolean, default: null },
}, },
addOns: Schema.Types.Mixed,
overleaf: { overleaf: {
id: { id: {
type: Number, type: Number,