Skip to content

Commit 60aacd9

Browse files
committed
created transaction api's
1 parent 528b963 commit 60aacd9

File tree

5 files changed

+230
-42
lines changed

5 files changed

+230
-42
lines changed

src/utils/middlewares.ts

Lines changed: 2 additions & 2 deletions
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.input);
24+
let validate = await validateYupSchemaAgainstAnObject(schema, req.body);
2525
if (validate.length > 0) {
2626
return res.status(400).json({
2727
message: "Validation Error",
@@ -36,7 +36,7 @@ export const validateUserExistsSentThroughReqBody = function (path: any) {
3636
return async function (req: any, res: any, next: any) {
3737
let user = await prisma.user.findUnique({
3838
where: {
39-
id: get(req, path, null),
39+
id: parseInt(get(req, path, null)),
4040
},
4141
});
4242
if (user === null) {

src/v1/routes.ts

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,43 @@ import { createUserSchema, updateUserSchema } from "./user.validation";
33
import { createUser, getUserBalance, updateUser } from "./user";
44

55
import express from "express";
6+
import {
7+
createTransaction,
8+
deleteTransaction,
9+
getTransaction,
10+
getTransactions,
11+
getTransactionsByUser,
12+
updateTransaction,
13+
} from "./transaction";
14+
import { transactionSchema } from "./transaction.validation";
615

716
const v1Routes = express.Router();
8-
v1Routes.get("/createTransaction", function (req, res) {
9-
res.status(200).json({
10-
message: "createTransaction",
11-
});
12-
});
13-
v1Routes.get("/updateTransaction", function (req, res) {
14-
res.status(200).json({
15-
message: "updateTransaction",
16-
});
17-
});
18-
v1Routes.delete("/deleteTransaction", function (req, res) {
19-
res.status(200).json({
20-
message: "deleteTransaction",
21-
});
22-
});
23-
v1Routes.get("/getTransaction", function (req, res) {
24-
res.status(200).json({
25-
message: "getTransaction",
26-
});
27-
});
28-
v1Routes.get("/getTransactions", function (req, res) {
29-
res.status(200).json({
30-
message: "getTransactions",
31-
});
32-
});
33-
v1Routes.get("/getTransactionsByUser", function (req, res) {
34-
res.status(200).json({
35-
message: "getTransactionsByUser",
36-
});
37-
});
17+
18+
v1Routes.post(
19+
"/createTransaction",
20+
validateUserExistsSentThroughReqBody("body.user_id"),
21+
middlewareValidateYupSchemaAgainstReqBody(transactionSchema),
22+
createTransaction,
23+
);
24+
25+
v1Routes.put(
26+
"/updateTransaction/:id",
27+
validateUserExistsSentThroughReqBody("body.user_id"),
28+
middlewareValidateYupSchemaAgainstReqBody(transactionSchema),
29+
updateTransaction,
30+
);
31+
32+
v1Routes.delete("/deleteTransaction/:id", deleteTransaction);
33+
34+
v1Routes.get("/getTransaction/:id", getTransaction);
35+
36+
v1Routes.get("/getTransactions", getTransactions);
37+
38+
v1Routes.get(
39+
"/getTransactionsByUser/:userId",
40+
validateUserExistsSentThroughReqBody("params.userId"),
41+
getTransactionsByUser,
42+
);
3843

3944
v1Routes.post("/createWallet", function (req, res) {
4045
res.status(200).json({
@@ -45,7 +50,7 @@ v1Routes.post("/createWallet", function (req, res) {
4550
v1Routes.post("/createUser", middlewareValidateYupSchemaAgainstReqBody(createUserSchema), createUser);
4651
v1Routes.put(
4752
"/updateUser",
48-
validateUserExistsSentThroughReqBody("body.input.id"),
53+
validateUserExistsSentThroughReqBody("body.id"),
4954
middlewareValidateYupSchemaAgainstReqBody(updateUserSchema),
5055
updateUser,
5156
);

src/v1/transaction.ts

Lines changed: 147 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,147 @@
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+
};

src/v1/transaction.validation.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import * as yup from "yup";
2+
3+
// Define the transaction validation schema
4+
export const transactionSchema = yup.object({
5+
transaction_type: yup
6+
.string()
7+
.required("Transaction type is required")
8+
.oneOf(["credit", "debit"], "Transaction type must be either credit or debit"),
9+
user_id: yup
10+
.number()
11+
.required("User ID is required")
12+
.positive("User ID must be a positive integer")
13+
.integer("User ID must be an integer"),
14+
wallet_id: yup
15+
.number()
16+
.required("Wallet ID is required")
17+
.positive("Wallet ID must be a positive integer")
18+
.integer("Wallet ID must be an integer"),
19+
amount: yup
20+
.number()
21+
.required("Amount is required")
22+
.positive("Amount must be a positive integer")
23+
.integer("Amount must be an integer"),
24+
created_at: yup
25+
.date()
26+
.default(() => new Date())
27+
.required("Creation date is required"),
28+
updated_at: yup
29+
.date()
30+
.default(() => new Date())
31+
.required("Update date is required"),
32+
});
33+
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+
}

src/v1/user.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ export const createUser = async (req: Request, res: Response) => {
66
try {
77
let response = await prisma.user.create({
88
data: {
9-
email: req.body.input.email,
10-
username: req.body.input.username,
11-
application_user_id: req.body.input.application_user_id,
9+
email: req.body.email,
10+
username: req.body.username,
11+
application_user_id: req.body.application_user_id,
1212
},
1313
});
1414
res.status(200).json({

0 commit comments

Comments
 (0)