Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): [Transaction] add delete transaction api #46

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion apps/api/v1/routes/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import { getAuthUserStrict } from '../middlewares/auth'
import { canUserReadBudget, findBudget } from '../services/budget.service'
import {
canUserCreateTransaction,
canUserDeleteTransaction,
canUserReadTransaction,
canUserUpdateTransaction,
createTransaction,
deleteTransaction,
findTransaction,
updateTransaction,
} from '../services/transaction.service'
Expand Down Expand Up @@ -130,7 +132,24 @@ router.delete(
}),
),
async (c) => {
return c.json({ message: 'not implemented' })
const { transactionId } = c.req.valid('param')
const user = getAuthUserStrict(c)

const transaction = await findTransaction({ transactionId })

if (
!(transaction && (await canUserReadTransaction({ user, transaction })))
) {
return c.json({ message: 'transaction not found' }, 404)
}

if (!(await canUserDeleteTransaction({ user, transaction }))) {
return c.json({ message: 'user cannot delete transaction' }, 403)
}

await deleteTransaction({ transactionId })

return c.json(transaction)
},
)

Expand Down
22 changes: 22 additions & 0 deletions apps/api/v1/services/transaction.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ export async function canUserUpdateTransaction({
return false
}

export async function canUserDeleteTransaction({
user,
transaction,
}: {
user: User
transaction: Transaction
}) {
return canUserUpdateTransaction({ user, transaction, walletAccount: null })
}

export async function findTransaction({
transactionId,
}: {
Expand Down Expand Up @@ -131,3 +141,15 @@ export async function updateTransaction({

return transaction
}

export async function deleteTransaction({
transactionId,
}: {
transactionId: string
}) {
await prisma.transaction.delete({
where: {
id: transactionId,
},
})
}