Skip to content

Commit

Permalink
feat(api): add get user subscription services' (#358)
Browse files Browse the repository at this point in the history
  • Loading branch information
duongdev authored Sep 22, 2024
1 parent f8da71e commit 4fa30e6
Show file tree
Hide file tree
Showing 4 changed files with 267 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- AlterTable
ALTER TABLE "User" ADD COLUMN "entitlement" TEXT,
ADD COLUMN "entitlementExpiresAt" TIMESTAMP(3),
ADD COLUMN "entitlementProductIdentifier" TEXT;
9 changes: 7 additions & 2 deletions apps/api/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@ model User {
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
email String @unique
name String?
email String @unique
name String?
entitlement String?
entitlementExpiresAt DateTime?
entitlementProductIdentifier String?
walletAccounts UserWalletAccount[]
budgetUsers BudgetUser[]
transactions Transaction[]
Expand Down
62 changes: 62 additions & 0 deletions apps/api/v1/services/user.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import type { CreateUser } from '@6pm/validation'
import type { User } from '@prisma/client'
import { getLogger } from '../../lib/log'
import prisma from '../../lib/prisma'
import {
getCustomerActiveSubscription,
getOrCreateCustomer,
} from './revenue-cat.service'

export async function findUserById(id: string) {
return await prisma.user.findUnique({
Expand Down Expand Up @@ -37,3 +43,59 @@ export async function deleteUser(userId: string) {
},
})
}

export async function syncUserSubscription(user: User) {
const logger = getLogger(`${syncAllUsersSubscription.name}:${user.id}`)

logger.info('Current user %o', user)

const customer = await getOrCreateCustomer({
userId: user.id,
})

const activeSubscription = getCustomerActiveSubscription(customer)

const updatedUser = await prisma.user.update({
where: { id: user.id },
data: {
entitlement: activeSubscription?.entitlement ?? null,
entitlementExpiresAt: activeSubscription?.subscription.expires_date
? new Date(activeSubscription.subscription.expires_date)
: null,
entitlementProductIdentifier:
activeSubscription?.subscription.product_identifier ?? null,
},
})

logger.info('Updated user %o', updatedUser)

return updatedUser
}

export async function syncAllUsersSubscription() {
const logger = getLogger(syncAllUsersSubscription.name)
const users = await prisma.user.findMany()
const failed = []

logger.info('Syncing %d users', users.length)

for (const user of users) {
try {
await syncUserSubscription(user)
logger.info('Synced user %s', user.email)
} catch (error) {
failed.push(user.email)

logger.warn('Failed to sync user %s', user.email)
logger.error(error)
}
}

logger.info('Synced %d users. Failed %d', users.length, failed.length)

return {
total: users.length,
success: users.length - failed.length,
failed,
}
}
Loading

0 comments on commit 4fa30e6

Please sign in to comment.