check if feature is granted

This commit is contained in:
Hongbo Wu 2024-04-17 12:53:29 +08:00
parent 259b778aba
commit b2a55a78d3
2 changed files with 32 additions and 0 deletions

View file

@ -7,6 +7,7 @@ import {
} from '../jobs/ai/create_digest'
import { createJobId, getJob, jobStateToTaskState } from '../queue-processor'
import { getDigest } from '../services/digest'
import { FeatureName, findGrantedFeatureByName } from '../services/features'
import { findActiveUser } from '../services/user'
import { analytics } from '../utils/analytics'
import { getClaimsByToken, getTokenByRequest } from '../utils/auth'
@ -72,6 +73,15 @@ export function digestRouter() {
return res.sendStatus(401)
}
const feature = await findGrantedFeatureByName(
FeatureName.AIDigest,
userId
)
if (!feature) {
logger.info(`${FeatureName.AIDigest} not granted: ${userId}`)
return res.sendStatus(403)
}
// check if job is already in queue
// if yes then return 202 accepted
// else enqueue job
@ -128,6 +138,15 @@ export function digestRouter() {
return res.sendStatus(401)
}
const feature = await findGrantedFeatureByName(
FeatureName.AIDigest,
userId
)
if (!feature) {
logger.info(`${FeatureName.AIDigest} not granted: ${userId}`)
return res.sendStatus(403)
}
// get job by user id
const jobId = createJobId(CREATE_DIGEST_JOB, userId)
const job = await getJob(jobId)
@ -185,6 +204,15 @@ export function digestRouter() {
return res.sendStatus(401)
}
const feature = await findGrantedFeatureByName(
FeatureName.AIDigest,
userId
)
if (!feature) {
logger.info(`${FeatureName.AIDigest} not granted: ${userId}`)
return res.sendStatus(403)
}
// get feedback from request body
if (!isFeedback(req.body)) {
logger.info('Invalid feedback format')

View file

@ -9,12 +9,14 @@ import { logger } from '../utils/logger'
const MAX_ULTRA_REALISTIC_USERS = 1500
const MAX_YOUTUBE_TRANSCRIPT_USERS = 500
const MAX_NOTION_USERS = 1000
const MAX_AIDIGEST_USERS = 5
export enum FeatureName {
AISummaries = 'ai-summaries',
YouTubeTranscripts = 'youtube-transcripts',
UltraRealisticVoice = 'ultra-realistic-voice',
Notion = 'notion',
AIDigest = 'ai-digest',
}
export const getFeatureName = (name: string): FeatureName | undefined => {
@ -40,6 +42,8 @@ export const optInFeature = async (
)
case FeatureName.Notion:
return optInLimitedFeature(FeatureName.Notion, uid, MAX_NOTION_USERS)
case FeatureName.AIDigest:
return optInLimitedFeature(FeatureName.AIDigest, uid, MAX_AIDIGEST_USERS)
default:
return undefined
}