| 1 | import { and, eq } from 'drizzle-orm' |
| 2 | import { memoize } |
| 3 | import { polarClient } |
| 4 | import { db } |
| 5 | import { members } |
| 6 | import { BillingCacheKeys } |
| 7 | |
| 8 | const ACTIVE_STATUSES = new |
| 9 | |
| 10 | export const getOrganizationBillingStatus = |
| 11 | async (organizationId: |
| 12 | const [owner] |
| 13 | .select() |
| 14 | .from(members) |
| 15 | .where( |
| 16 | and( |
| 17 | eq(members.role |
| 18 | eq(members.organizationId |
| 19 | ) |
| 20 | ) |
| 21 | if (!owner) |
| 22 | return null |
| 23 | } |
| 24 | const data = |
| 25 | metadata: { referenceId: owner |
| 26 | }) |
| 27 | const hasActiveSubscription = |
| 28 | ACTIVE_STATUSES.has(sub |
| 29 | ) |
| 30 | return hasActiveSubscription |
| 31 | }, |
| 32 | { |
| 33 | duration: 3600, |
| 34 | revalidateTags: (orgId) |
| 35 | BillingCacheKeys.getOrganizationBillingStatus(orgId |
| 36 | log: ['verbose'], |
| 37 | logid: 'Organization Billing Status', |
| 38 | } |
| 39 | ) |
| 40 | |
| 41 | export const getUserBillingStatus = |
| 42 | async (userId: |
| 43 | const data = |
| 44 | metadata: { referenceId: userId |
| 45 | }) |
| 46 | const hasActiveSubscription = |
| 47 | data?.result?.items |
| 48 | false |
| 49 | return hasActiveSubscription |
| 50 | }, |
| 51 | { |
| 52 | duration: 3600, |
| 53 | revalidateTags: (orgId) |
| 54 | log: ['verbose'], |
| 55 | logid: 'User Billing Status', |
| 56 | } |
| 57 | ) |