Skip to content

Commit 9709321

Browse files
committed
feat: transactions updates
feat: transactions updates
1 parent 9296822 commit 9709321

File tree

5 files changed

+31
-37
lines changed

5 files changed

+31
-37
lines changed

src/utils/middlewares.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const validateYupSchemaAgainstAnObject = async function (schema: any, obj
2121

2222
export const middlewareValidateYupSchemaAgainstReqBody = function (schema: any) {
2323
return async function (req: any, res: any, next: any) {
24-
let validate = await validateYupSchemaAgainstAnObject(schema, req.body);
24+
let validate = await validateYupSchemaAgainstAnObject(schema, req.body.input);
2525
if (validate.length > 0) {
2626
return res.status(400).json({
2727
message: "Validation Error",

src/v1/routes.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@ import {
1111
getTransactionsByUser,
1212
updateTransaction,
1313
} from "./transaction";
14-
import { transactionSchema } from "./transaction.validation";
14+
import { createTransactionSchema, updateTransactionSchema } from "./transaction.validation";
1515

1616
const v1Routes = express.Router();
1717

1818
v1Routes.post(
1919
"/createTransaction",
2020
validateUserExistsSentThroughReqBody("body.user_id"),
21-
middlewareValidateYupSchemaAgainstReqBody(transactionSchema),
21+
middlewareValidateYupSchemaAgainstReqBody(createTransactionSchema),
2222
createTransaction,
2323
);
2424

2525
v1Routes.put(
2626
"/updateTransaction/:id",
2727
validateUserExistsSentThroughReqBody("body.user_id"),
28-
middlewareValidateYupSchemaAgainstReqBody(transactionSchema),
28+
middlewareValidateYupSchemaAgainstReqBody(updateTransactionSchema),
2929
updateTransaction,
3030
);
3131

src/v1/transaction.ts

+11-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export const createTransaction = async (req: Request, res: Response) => {
66
try {
77
// Check if wallet_id exists
88
const wallet = await prisma.wallet.findUnique({
9-
where: { id: req.body.wallet_id },
9+
where: { id: req.body.input.wallet_id },
1010
});
1111
if (!wallet) {
1212
return res.status(400).json({ message: "Wallet not found" });
@@ -15,10 +15,10 @@ export const createTransaction = async (req: Request, res: Response) => {
1515
// Create transaction
1616
let response = await prisma.transaction.create({
1717
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,
18+
transaction_type: req.body.input.transaction_type,
19+
user_id: req.body.input.user_id,
20+
wallet_id: req.body.input.wallet_id,
21+
amount: req.body.input.amount,
2222
},
2323
});
2424

@@ -34,7 +34,7 @@ export const createTransaction = async (req: Request, res: Response) => {
3434
export const updateTransaction = async (req: Request, res: Response) => {
3535
try {
3636
const { id } = req.params;
37-
const updateData = req.body;
37+
const updateData = req.body.input;
3838

3939
// Check if transaction exists
4040
const existingTransaction = await prisma.transaction.findUnique({
@@ -46,7 +46,7 @@ export const updateTransaction = async (req: Request, res: Response) => {
4646

4747
if (updateData.wallet_id) {
4848
const wallet = await prisma.wallet.findUnique({
49-
where: { id: updateData.wallet_id },
49+
where: { id: existingTransaction.wallet_id },
5050
});
5151
if (!wallet) {
5252
return res.status(400).json({ message: "Wallet not found" });
@@ -140,7 +140,10 @@ export const getTransactionsByUser = async (req: Request, res: Response) => {
140140
},
141141
});
142142

143-
res.status(200).json(transactions);
143+
res.status(200).json({
144+
transactions: transactions,
145+
message: "Transactions retrieved successfully",
146+
});
144147
} catch (e) {
145148
generateError(res, e);
146149
}

src/v1/transaction.validation.ts

+15-24
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,33 @@
1-
import * as yup from "yup";
1+
import { object, string, number, InferType, date } from "yup";
22

3-
// Define the transaction validation schema
4-
export const transactionSchema = yup.object({
5-
transaction_type: yup
6-
.string()
3+
export const createTransactionSchema = object({
4+
transaction_type: string()
75
.required("Transaction type is required")
86
.oneOf(["credit", "debit"], "Transaction type must be either credit or debit"),
9-
user_id: yup
10-
.number()
7+
user_id: number()
118
.required("User ID is required")
129
.positive("User ID must be a positive integer")
1310
.integer("User ID must be an integer"),
14-
wallet_id: yup
15-
.number()
11+
wallet_id: number()
1612
.required("Wallet ID is required")
1713
.positive("Wallet ID must be a positive integer")
1814
.integer("Wallet ID must be an integer"),
19-
amount: yup
20-
.number()
15+
amount: number()
2116
.required("Amount is required")
2217
.positive("Amount must be a positive integer")
2318
.integer("Amount must be an integer"),
24-
created_at: yup
25-
.date()
19+
created_at: date()
2620
.default(() => new Date())
2721
.required("Creation date is required"),
28-
updated_at: yup
29-
.date()
22+
updated_at: date()
3023
.default(() => new Date())
3124
.required("Update date is required"),
3225
});
26+
export type CreateTransactionSchema = InferType<typeof createTransactionSchema>;
3327

34-
// Define the type for the expected shape of the transaction data
35-
export interface TransactionData {
36-
transaction_type: "credit" | "debit";
37-
user_id: number;
38-
wallet_id: number;
39-
amount: number;
40-
created_at: Date;
41-
updated_at: Date;
42-
}
28+
export const updateTransactionSchema = object({
29+
transaction_type: string().optional().oneOf(["credit", "debit"], "Transaction type must be either credit or debit"),
30+
user_id: number().optional().positive("User ID must be a positive integer").integer("User ID must be an integer"),
31+
amount: number().optional().positive("Amount must be a positive integer").integer("Amount must be an integer"),
32+
});
33+
export type UpdateTransactionSchema = InferType<typeof updateTransactionSchema>;

src/v1/user.validation.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { object, string, number, date, InferType } from "yup";
1+
import { object, string, number, InferType } from "yup";
22

33
export const createUserSchema = object({
44
username: string().required("Username is required"),

0 commit comments

Comments
 (0)