From c001d2d9ad2d56bc3d20e3599a63af585c3afddb Mon Sep 17 00:00:00 2001 From: Dustin Do Date: Sun, 22 Sep 2024 10:53:06 +0700 Subject: [PATCH] feat(api): restrict wallet creation based on entitlement --- apps/api/v1/services/wallet.service.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/apps/api/v1/services/wallet.service.ts b/apps/api/v1/services/wallet.service.ts index 3ee90012..004bab8d 100644 --- a/apps/api/v1/services/wallet.service.ts +++ b/apps/api/v1/services/wallet.service.ts @@ -1,7 +1,9 @@ +import { getPlanConfig } from '@6pm/utilities' import type { CreateWallet, UpdateWallet } from '@6pm/validation' import type { User, UserWalletAccount } from '@prisma/client' import prisma from '../../lib/prisma' import { DEFAULT_WALLETS } from '../constants/wallet.const' +import { getUserPlan } from './user.service' export async function findUserWallet({ user, @@ -75,9 +77,12 @@ export async function getWalletBalance({ return balance._sum.amountInVnd || 0 } -// biome-ignore lint/correctness/noUnusedVariables: export async function canUserCreateWallet({ user }: { user: User }) { - return true + const wallets = await findUserWallets({ user }) + const userPlan = getUserPlan(user) + const maxWallets = getPlanConfig(userPlan, 'maxWallets') + + return maxWallets !== null && wallets.length < maxWallets } export async function canUserUpdateWallet({