Skip to content

Commit

Permalink
feat(api): add check transaction category for amount
Browse files Browse the repository at this point in the history
  • Loading branch information
duongdev committed Jul 14, 2024
1 parent fa04ee1 commit 0759b36
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 8 deletions.
51 changes: 45 additions & 6 deletions apps/api/v1/routes/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { z } from 'zod'
import { getAuthUserStrict } from '../middlewares/auth'
import { generateTransactionDataFromFile } from '../services/ai.service'
import { canUserReadBudget, findBudget } from '../services/budget.service'
import { canUserReadCategory, findCategory } from '../services/category.service'
import {
canUserCreateTransaction,
canUserDeleteTransaction,
Expand All @@ -16,7 +17,7 @@ import {
listTransactions,
updateTransaction,
} from '../services/transaction.service'
import { findUserWallet } from '../services/wallet.service'
import { canUserReadWallet, findUserWallet } from '../services/wallet.service'

const router = new Hono()

Expand Down Expand Up @@ -86,18 +87,27 @@ const router = new Hono()
.post('/', zValidator('json', zCreateTransaction), async (c) => {
const user = getAuthUserStrict(c)
const data = c.req.valid('json')
const { budgetId, walletAccountId: walletId } = data
const { budgetId, walletAccountId: walletId, categoryId } = data

const budget = budgetId ? await findBudget({ budgetId }) : null
if (budgetId && (!budget || !(await canUserReadBudget({ user, budget })))) {
return c.json({ message: 'budget not found' }, 404)
}

const wallet = await findUserWallet({ user, walletId })
if (!wallet) {
if (!wallet || !(await canUserReadWallet({ user, walletId }))) {
return c.json({ message: 'wallet not found' }, 404)
}

const category =
(categoryId && (await findCategory({ id: categoryId }))) || null
if (
categoryId &&
(!category || !(await canUserReadCategory({ user, category })))
) {
return c.json({ message: 'category not found' }, 404)
}

if (
!(await canUserCreateTransaction({ user, budget, walletAccount: wallet }))
) {
Expand All @@ -106,7 +116,13 @@ const router = new Hono()

const transaction = await createTransaction({
user,
data,
data: {
...data,
amount:
category?.type === 'INCOME'
? Math.abs(data.amount)
: -Math.abs(data.amount),
},
})

return c.json(transaction, 201)
Expand Down Expand Up @@ -136,7 +152,10 @@ const router = new Hono()
}

const wallet = walletId ? await findUserWallet({ user, walletId }) : null
if (walletId && !wallet) {
if (
walletId &&
(!wallet || !(await canUserReadWallet({ user, walletId })))
) {
return c.json({ message: 'wallet not found' }, 404)
}

Expand All @@ -158,9 +177,29 @@ const router = new Hono()
return c.json({ message: 'budget not found' }, 404)
}

const categoryId = data.categoryId || transaction.categoryId

const category =
(categoryId && (await findCategory({ id: categoryId }))) || null
if (
categoryId &&
(!category || !(await canUserReadCategory({ user, category })))
) {
return c.json({ message: 'category not found' }, 404)
}

const transactionAmount = data.amount
? category?.type === 'INCOME'
? Math.abs(data.amount)
: -Math.abs(data.amount)
: transaction.amount

const updatedTransaction = await updateTransaction({
transactionId,
data,
data: {
...data,
amount: transactionAmount as number,
},
})

return c.json(updatedTransaction)
Expand Down
7 changes: 5 additions & 2 deletions apps/api/v1/services/category.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,12 @@ export async function deleteCategory({ categoryId }: { categoryId: string }) {
})
}

export async function findCategory({ id }: { id: string }) {
export async function findCategory({
id,
userId,
}: { id: string; userId?: string }) {
return prisma.category.findUnique({
where: { id },
where: { id, userId },
})
}

Expand Down
10 changes: 10 additions & 0 deletions apps/api/v1/services/wallet.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ export async function canUserDeleteWallet({
return await canUserUpdateWallet({ user, walletId })
}

export async function canUserReadWallet({
user,
walletId,
}: {
user: User
walletId: string
}) {
return await canUserUpdateWallet({ user, walletId })
}

export async function walletWithBalance(wallet: UserWalletAccount) {
const balance = await getWalletBalance({ wallet })
return { ...wallet, balance }
Expand Down

0 comments on commit 0759b36

Please sign in to comment.