create or update an opt-in record with null grantedAt if exceeding max users
This commit is contained in:
@ -5,11 +5,13 @@ import {
|
||||
JoinColumn,
|
||||
ManyToOne,
|
||||
PrimaryGeneratedColumn,
|
||||
Unique,
|
||||
UpdateDateColumn,
|
||||
} from 'typeorm'
|
||||
import { User } from './user'
|
||||
|
||||
@Entity({ name: 'features' })
|
||||
@Unique(['user', 'name'])
|
||||
export class Feature {
|
||||
@PrimaryGeneratedColumn('uuid')
|
||||
id!: string
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
import { authorized } from '../../utils/helpers'
|
||||
import {
|
||||
MutationOptInFeatureArgs,
|
||||
OptInFeatureError,
|
||||
@ -10,6 +9,7 @@ import {
|
||||
optInFeature,
|
||||
signFeatureToken,
|
||||
} from '../../services/features'
|
||||
import { authorized } from '../../utils/helpers'
|
||||
|
||||
export const optInFeatureResolver = authorized<
|
||||
OptInFeatureSuccess,
|
||||
@ -33,19 +33,19 @@ export const optInFeatureResolver = authorized<
|
||||
}
|
||||
}
|
||||
|
||||
const optIn = await optInFeature(featureName, claims.uid)
|
||||
if (!optIn) {
|
||||
const optedInFeature = await optInFeature(featureName, claims.uid)
|
||||
if (!optedInFeature) {
|
||||
return {
|
||||
errorCodes: [OptInFeatureErrorCode.NotFound],
|
||||
}
|
||||
}
|
||||
log.info('Opted in to a feature', optIn)
|
||||
log.info('Opted in to a feature', optedInFeature)
|
||||
|
||||
const token = signFeatureToken(optIn, claims.uid)
|
||||
const token = signFeatureToken(optedInFeature, claims.uid)
|
||||
|
||||
return {
|
||||
feature: {
|
||||
...optIn,
|
||||
...optedInFeature,
|
||||
token,
|
||||
},
|
||||
}
|
||||
|
||||
@ -42,7 +42,7 @@ const optInUltraRealisticVoice = async (uid: string): Promise<Feature> => {
|
||||
|
||||
const MAX_USERS = 1500
|
||||
// opt in to feature for the first 1500 users
|
||||
const newFeatures = (await AppDataSource.query(
|
||||
const optedInFeatures = (await AppDataSource.query(
|
||||
`insert into omnivore.features (user_id, name, granted_at)
|
||||
select $1, $2, $3 from omnivore.features
|
||||
where name = $2 and granted_at is not null
|
||||
@ -54,19 +54,30 @@ const optInUltraRealisticVoice = async (uid: string): Promise<Feature> => {
|
||||
)) as Feature[]
|
||||
|
||||
// if no new features were created then user has exceeded max users
|
||||
if (newFeatures.length === 0) {
|
||||
if (optedInFeatures.length === 0) {
|
||||
logger.info('exceeded max users')
|
||||
|
||||
return getRepository(Feature).save({
|
||||
// create/update an opt-in record with null grantedAt
|
||||
const optInRecord = {
|
||||
user: { id: uid },
|
||||
name: FeatureName.UltraRealisticVoice,
|
||||
grantedAt: null,
|
||||
})
|
||||
}
|
||||
const result = await getRepository(Feature).upsert(optInRecord, [
|
||||
'user',
|
||||
'name',
|
||||
])
|
||||
if (result.generatedMaps.length === 0) {
|
||||
throw new Error('failed to update opt-in record')
|
||||
}
|
||||
|
||||
logger.info('opt-in record updated', result.generatedMaps)
|
||||
return { ...optInRecord, ...(result.generatedMaps[0] as Feature) }
|
||||
}
|
||||
|
||||
logger.info('opted in', { uid, feature: newFeatures[0] })
|
||||
logger.info('opted in', { uid, feature: optedInFeatures[0] })
|
||||
|
||||
return newFeatures[0]
|
||||
return optedInFeatures[0]
|
||||
}
|
||||
|
||||
export const signFeatureToken = (
|
||||
@ -76,7 +87,7 @@ export const signFeatureToken = (
|
||||
},
|
||||
userId: string
|
||||
): string => {
|
||||
logger.info('signing feature token', { grantedAt: feature.grantedAt })
|
||||
logger.info('signing feature token', feature)
|
||||
|
||||
return jwt.sign(
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user