Skip to content

Commit

Permalink
feat(utils): add getTransactionAmountBasedOnCategory util (#314)
Browse files Browse the repository at this point in the history
  • Loading branch information
duongdev committed Sep 12, 2024
1 parent 2292f00 commit a7c536a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 19 deletions.
16 changes: 5 additions & 11 deletions apps/api/v1/routes/transactions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getTransactionAmountBasedOnCategory } from '@6pm/utilities'
import { zCreateTransaction, zUpdateTransaction } from '@6pm/validation'
import { zValidator } from '@hono/zod-validator'
import { Hono } from 'hono'
Expand Down Expand Up @@ -153,12 +154,7 @@ const router = new Hono()

const createTransactionData = {
...data,
amount:
(category &&
(category?.type === 'INCOME'
? Math.abs(data.amount)
: -Math.abs(data.amount))) ||
data.amount,
amount: getTransactionAmountBasedOnCategory(data.amount, category?.type),
}
logger.debug('Creating transaction with data %o', createTransactionData)

Expand Down Expand Up @@ -241,11 +237,9 @@ const router = new Hono()
}

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

const updatedTransaction = await updateTransaction({
transactionId,
Expand Down
17 changes: 9 additions & 8 deletions apps/mobile/stores/transaction/hooks.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { UNCATEGORIZED_ID } from '@/components/home/category-chart'
import { getHonoClient } from '@/lib/client'
import { useMeQuery } from '@/queries/auth'
import { dayjsExtended } from '@6pm/utilities'
import {
dayjsExtended,
getTransactionAmountBasedOnCategory,
} from '@6pm/utilities'
import {
type TransactionFormValues,
type TransactionPopulated,
Expand Down Expand Up @@ -180,9 +183,7 @@ export function useCreateTransaction() {
const categoryType = category?.type

const amount = category
? categoryType === 'INCOME'
? Math.abs(data.amount)
: -Math.abs(data.amount)
? getTransactionAmountBasedOnCategory(data.amount, categoryType)
: data.amount

const result = await hc.v1.transactions.$post({
Expand Down Expand Up @@ -309,10 +310,10 @@ export function useUpdateTransaction() {
const category = data.categoryId ? categoriesDict[data.categoryId] : null
const categoryType = category?.type

const amount =
categoryType === 'INCOME'
? Math.abs(data.amount)
: -Math.abs(data.amount)
const amount = getTransactionAmountBasedOnCategory(
data.amount,
category?.type,
)

const transaction: TransactionPopulated = {
id,
Expand Down
1 change: 1 addition & 0 deletions packages/utilities/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './date/dayjs'
export * from './date/period'
export * from './transactions'
10 changes: 10 additions & 0 deletions packages/utilities/src/transactions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function getTransactionAmountBasedOnCategory(
amount: number,
categoryType?: 'INCOME' | 'EXPENSE' | null,
): number {
if (!categoryType) {
return amount
}

return categoryType === 'INCOME' ? Math.abs(amount) : -Math.abs(amount)
}

0 comments on commit a7c536a

Please sign in to comment.