|
1 | | -export const createTransaction = () => {}; |
2 | | -export const updateTransaction = () => {}; |
3 | | -export const deleteTransaction = () => {}; |
4 | | -export const getTransaction = () => {}; |
5 | | -export const getTransactions = () => {}; |
6 | | -export const getTransactionsByUser = () => {}; |
| 1 | +import prisma from "./../database/prisma"; |
| 2 | +import { Response, Request } from "express"; |
| 3 | +import { generateError } from "../utils/errors"; |
| 4 | + |
| 5 | +export const createTransaction = async (req: Request, res: Response) => { |
| 6 | + try { |
| 7 | + // Check if wallet_id exists |
| 8 | + const wallet = await prisma.wallet.findUnique({ |
| 9 | + where: { id: req.body.wallet_id }, |
| 10 | + }); |
| 11 | + if (!wallet) { |
| 12 | + return res.status(400).json({ message: "Wallet not found" }); |
| 13 | + } |
| 14 | + |
| 15 | + // Create transaction |
| 16 | + let response = await prisma.transaction.create({ |
| 17 | + data: { |
| 18 | + transaction_type: req.body.transaction_type, |
| 19 | + user_id: req.body.user_id, |
| 20 | + wallet_id: req.body.wallet_id, |
| 21 | + amount: req.body.amount, |
| 22 | + }, |
| 23 | + }); |
| 24 | + |
| 25 | + res.status(200).json({ |
| 26 | + message: "Transaction created successfully", |
| 27 | + user: response, |
| 28 | + }); |
| 29 | + } catch (e) { |
| 30 | + generateError(res, e); |
| 31 | + } |
| 32 | +}; |
| 33 | + |
| 34 | +export const updateTransaction = async (req: Request, res: Response) => { |
| 35 | + try { |
| 36 | + const { id } = req.params; |
| 37 | + const updateData = req.body; |
| 38 | + |
| 39 | + // Check if transaction exists |
| 40 | + const existingTransaction = await prisma.transaction.findUnique({ |
| 41 | + where: { id: parseInt(id) }, |
| 42 | + }); |
| 43 | + if (!existingTransaction) { |
| 44 | + return res.status(404).json({ message: "Transaction not found" }); |
| 45 | + } |
| 46 | + |
| 47 | + if (updateData.wallet_id) { |
| 48 | + const wallet = await prisma.wallet.findUnique({ |
| 49 | + where: { id: updateData.wallet_id }, |
| 50 | + }); |
| 51 | + if (!wallet) { |
| 52 | + return res.status(400).json({ message: "Wallet not found" }); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // Update the transaction |
| 57 | + const updatedTransaction = await prisma.transaction.update({ |
| 58 | + where: { id: parseInt(id) }, |
| 59 | + data: updateData, |
| 60 | + }); |
| 61 | + |
| 62 | + res.status(200).json({ |
| 63 | + message: "Transaction updated successfully", |
| 64 | + transaction: updatedTransaction, |
| 65 | + }); |
| 66 | + } catch (e) { |
| 67 | + generateError(res, e); |
| 68 | + } |
| 69 | +}; |
| 70 | + |
| 71 | +export const deleteTransaction = async (req: Request, res: Response) => { |
| 72 | + try { |
| 73 | + const { id } = req.params; |
| 74 | + |
| 75 | + // Check if the transaction exists |
| 76 | + const existingTransaction = await prisma.transaction.findUnique({ |
| 77 | + where: { id: parseInt(id) }, |
| 78 | + }); |
| 79 | + if (!existingTransaction) { |
| 80 | + return res.status(404).json({ message: "Transaction not found" }); |
| 81 | + } |
| 82 | + |
| 83 | + // Delete the transaction |
| 84 | + await prisma.transaction.delete({ |
| 85 | + where: { id: parseInt(id) }, |
| 86 | + }); |
| 87 | + |
| 88 | + res.status(200).json({ message: "Transaction deleted successfully" }); |
| 89 | + } catch (e) { |
| 90 | + generateError(res, e); |
| 91 | + } |
| 92 | +}; |
| 93 | + |
| 94 | +export const getTransaction = async (req: Request, res: Response) => { |
| 95 | + try { |
| 96 | + const { id } = req.params; |
| 97 | + |
| 98 | + // Retrieve the transaction |
| 99 | + const transaction = await prisma.transaction.findUnique({ |
| 100 | + where: { id: parseInt(id) }, |
| 101 | + }); |
| 102 | + |
| 103 | + if (!transaction) { |
| 104 | + return res.status(404).json({ message: "Transaction not found" }); |
| 105 | + } |
| 106 | + |
| 107 | + res.status(200).json(transaction); |
| 108 | + } catch (e) { |
| 109 | + generateError(res, e); |
| 110 | + } |
| 111 | +}; |
| 112 | + |
| 113 | +export const getTransactions = async (req: Request, res: Response) => { |
| 114 | + try { |
| 115 | + const transactions = await prisma.transaction.findMany(); |
| 116 | + |
| 117 | + if (!transactions) { |
| 118 | + return res.status(404).json({ message: "Transactions not found" }); |
| 119 | + } |
| 120 | + |
| 121 | + res.status(200).json(transactions); |
| 122 | + } catch (e) { |
| 123 | + generateError(res, e); |
| 124 | + } |
| 125 | +}; |
| 126 | + |
| 127 | +export const getTransactionsByUser = async (req: Request, res: Response) => { |
| 128 | + try { |
| 129 | + const { userId } = req.params; |
| 130 | + |
| 131 | + // Check if user exists |
| 132 | + const user = await prisma.user.findUnique({ |
| 133 | + where: { id: parseInt(userId) }, |
| 134 | + }); |
| 135 | + |
| 136 | + // Retrieve transactions for the user |
| 137 | + const transactions = await prisma.transaction.findMany({ |
| 138 | + where: { |
| 139 | + user_id: user?.id, |
| 140 | + }, |
| 141 | + }); |
| 142 | + |
| 143 | + res.status(200).json(transactions); |
| 144 | + } catch (e) { |
| 145 | + generateError(res, e); |
| 146 | + } |
| 147 | +}; |
0 commit comments