Skip to content
Open
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
2 changes: 2 additions & 0 deletions server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ app.get("/is-up", (req, res) => {

// All the routes here
import userRoutes from "./routers/user.routers.js";
import transactionRoutes from "./routers/transaction.route.js";
app.use("/api/v1/users", userRoutes);
app.use("/api/transactions", transactionRoutes);

export default app;
40 changes: 40 additions & 0 deletions server/controllers/transaction.controllers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Transaction } from "../models/transaction.models.js";
import ApiError from "../utils/ApiError.js";
import ApiResponse from "../utils/ApiResponse.js";
import asyncHandler from "../utils/asyncHandler.js";

// Create a new transaction
export const createTransaction = asyncHandler(async (req, res) => {
const { amount, category, type, date, note, goalId } = req.body;

// If type is Expense, category is required
if (type === "Expense" && (!category || category.trim() === "")) {
throw new ApiError(400, "Category is required for Expense transactions.");
}

const transaction = await Transaction.create({
user: req.user._id,
amount,
category,
type,
date,
note,
goalId,
});

res.status(201).json(new ApiResponse(201, transaction, "Transaction created"));
});

// Get all transactions for the logged-in user
export const getTransactions = asyncHandler(async (req, res) => {
const transactions = await Transaction.find({ user: req.user._id }).sort({ date: -1 });
res.json(new ApiResponse(200, transactions));
});

// Delete a transaction by ID
export const deleteTransaction = asyncHandler(async (req, res) => {
const { id } = req.params;
const transaction = await Transaction.findOneAndDelete({ _id: id, user: req.user._id });
if (!transaction) throw new ApiError(404, "Transaction not found");
res.json(new ApiResponse(200, transaction, "Transaction deleted"));
});
29 changes: 29 additions & 0 deletions server/models/transaction.models.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import mongoose from "mongoose";

const transactionSchema = new mongoose.Schema(
{
user: { type: mongoose.Schema.Types.ObjectId, ref: "User", required: true },
amount: { type: Number, required: true },
category: {
type: String,
validate: {
validator: function (v) {
// If type is "Expense", category is required (not empty)
if (this.type === "Expense") {
return typeof v === "string" && v.trim().length > 0;
}
// If type is "Income", category can be empty or undefined
return true;
},
message: "Category is required for Expense transactions.",
},
},
type: { type: String, enum: ["Income", "Expense"], required: true },
date: { type: String, required: true }, // e.g. "DD/MM/YYYY"
note: { type: String, default: "" },
goalId: { type: mongoose.Schema.Types.ObjectId, ref: "Goal" }, // optional
},
{ timestamps: true }
);

export const Transaction = mongoose.model("Transaction", transactionSchema);
17 changes: 17 additions & 0 deletions server/routers/transaction.routers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import express from "express";
import { verifyUser } from "../middlewares/auth.middleware.js";
import {
createTransaction,
getTransactions,
deleteTransaction,
} from "../controllers/transaction.controller.js";

const router = express.Router();

router.use(verifyUser);

router.post("/", createTransaction);
router.get("/", getTransactions);
router.delete("/:id", deleteTransaction);

export default router;