diff --git a/apps/api/v1/services/budget.service.ts b/apps/api/v1/services/budget.service.ts index c7f81d9..6ba37ea 100644 --- a/apps/api/v1/services/budget.service.ts +++ b/apps/api/v1/services/budget.service.ts @@ -1,4 +1,7 @@ -import { calculateBudgetPeriodStartEndDates } from '@6pm/utilities' +import { + calculateBudgetPeriodStartEndDates, + getPlanConfig, +} from '@6pm/utilities' import type { CreateBudget, UpdateBudget } from '@6pm/validation' import { type Budget, @@ -9,6 +12,7 @@ import { } from '@prisma/client' import prisma from '../../lib/prisma' import { inviteUserToBudget } from './budget-invitation.service' +import { getUserPlan } from './user.service' const BUDGET_INCLUDE: Prisma.BudgetInclude = { periodConfigs: true, @@ -19,12 +23,13 @@ type BudgetPopulated = Budget & { } export async function canUserCreateBudget({ - // biome-ignore lint/correctness/noUnusedVariables: user, -}: { - user: User -}): Promise { - return true +}: { user: User }): Promise { + const userBudgets = await findBudgetsOfUser({ user }) + const userPlan = getUserPlan(user) + const maxBudgets = getPlanConfig(userPlan, 'maxBudgets') + + return maxBudgets !== null && userBudgets.length < maxBudgets } export async function canUserReadBudget({ diff --git a/apps/api/v1/services/user.service.ts b/apps/api/v1/services/user.service.ts index 09a4dd0..9b068a0 100644 --- a/apps/api/v1/services/user.service.ts +++ b/apps/api/v1/services/user.service.ts @@ -1,3 +1,4 @@ +import type { UserEntitlement } from '@6pm/utilities' import type { CreateUser } from '@6pm/validation' import type { User } from '@prisma/client' import { getLogger } from '../../lib/log' @@ -99,3 +100,10 @@ export async function syncAllUsersSubscription() { failed, } } + +/** @alias getUserEntitlement */ +export const getUserPlan = (user: User): UserEntitlement => { + return (user.entitlement as UserEntitlement) ?? 'saver' +} +/** @alias getUserPlan */ +export const getUserEntitlement = getUserPlan