Closer/server/src/services/entitlement.ts

35 lines
907 B
TypeScript
Raw Normal View History

import { db } from '../config/firebase'
import { RevenueCatEvent } from '../types'
const PREMIUM_ACTIVE_TYPES = new Set([
'INITIAL_PURCHASE',
'RENEWAL',
'PRODUCT_CHANGE',
'TRANSFER',
'UNCANCELLATION',
])
const PREMIUM_REVOKED_TYPES = new Set([
'EXPIRATION',
'CANCELLATION',
'SUBSCRIBER_ALIAS',
])
export async function syncEntitlement(event: RevenueCatEvent): Promise<void> {
const { type, app_user_id: uid } = event.event
if (PREMIUM_ACTIVE_TYPES.has(type)) {
await db().collection('users').doc(uid).update({ hasPremium: true })
console.log(`[entitlement] hasPremium=true for ${uid} (${type})`)
return
}
if (PREMIUM_REVOKED_TYPES.has(type)) {
await db().collection('users').doc(uid).update({ hasPremium: false })
console.log(`[entitlement] hasPremium=false for ${uid} (${type})`)
return
}
console.log(`[entitlement] ignored event type: ${type}`)
}