Skip to content

Commit

Permalink
feat(api): restrict budget creation based on entitlement
Browse files Browse the repository at this point in the history
  • Loading branch information
duongdev committed Sep 22, 2024
1 parent 7b19a3c commit 3342e94
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
17 changes: 11 additions & 6 deletions apps/api/v1/services/budget.service.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand All @@ -19,12 +23,13 @@ type BudgetPopulated = Budget & {
}

export async function canUserCreateBudget({
// biome-ignore lint/correctness/noUnusedVariables: <explanation>
user,
}: {
user: User
}): Promise<boolean> {
return true
}: { user: User }): Promise<boolean> {
const userBudgets = await findBudgetsOfUser({ user })
const userPlan = getUserPlan(user)
const maxBudgets = getPlanConfig(userPlan, 'maxBudgets')

return maxBudgets !== null && userBudgets.length < maxBudgets
}

export async function canUserReadBudget({
Expand Down
8 changes: 8 additions & 0 deletions apps/api/v1/services/user.service.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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

0 comments on commit 3342e94

Please sign in to comment.