Skip to content

Commit

Permalink
feat:transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
ubongedem78 committed Jan 24, 2024
1 parent 5f7dd14 commit 27f474f
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 9 deletions.
26 changes: 21 additions & 5 deletions src/controllers/transaction.controller.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
const { fetUserTransactions } = require("../utils/transaction.Util");
const {
fetchUserTransactions,
fetchSingleTransaction,
} = require("../utils/transaction.Util");

const getAllUserTransactions = async (req, res, next) => {
const { userId } = req.params;
try {
const transactions = await fetUserTransactions(userId);
const transactions = await fetchUserTransactions(userId);

return res.status(200).json({
msg: "Transactions fetched successfully",
transactions,
});
} catch (error) {
console.log(error);
next();
next(error);
}
};

module.exports = { getAllUserTransactions };
const getSingleTransaction = async (req, res, next) => {
const { userId, transactionId } = req.params;
try {
const transaction = await fetchSingleTransaction(transactionId);

return res.status(200).json({
msg: "Transaction fetched successfully",
transaction,
});
} catch (error) {
next(error);
}
};

module.exports = { getAllUserTransactions, getSingleTransaction };
2 changes: 2 additions & 0 deletions src/routes/transaction.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ const express = require("express");
const router = express.Router();
const {
getAllUserTransactions,
getSingleTransaction,
} = require("../controllers/transaction.controller");

router.get("/transactions/:userId", getAllUserTransactions);
router.get("/transactions/:userId/:transactionId", getSingleTransaction);

module.exports = router;
78 changes: 74 additions & 4 deletions src/utils/transaction.Util.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
const { User, Transaction, Wallet } = require("../model/model");
const { Op } = require("sequelize");
const { NotFoundError } = require("../errors");
async function fetUserTransactions(userId) {

/**
* Fetch all transactions for a specific user.
* @param {string} userId - The ID of the user for whom transactions should be fetched.
* @returns {Promise<Array>} A Promise that resolves to an array of formatted transactions.
* @throws {NotFoundError} Will throw a NotFoundError if the user is not found.
*/
async function fetchUserTransactions(userId) {
const user = await User.findByPk(userId, {
include: {
model: Wallet,
Expand All @@ -13,6 +20,7 @@ async function fetUserTransactions(userId) {
throw new NotFoundError("User not found");
}

// Find all transactions where the user's Wallet ID matches either creditWalletId or debitWalletId
const transactions = await Transaction.findAll({
where: {
[Op.or]: [
Expand All @@ -25,8 +33,70 @@ async function fetUserTransactions(userId) {
],
},
});
console.log(transactions);
return transactions;

// Format transactions to include specific details
const formattedTransactions = transactions.map((transaction) => {
const type =
transaction.creditWalletId === user.Wallet.id ? "credit" : "debit";
return {
id: transaction.id,
type,
amount: transaction.amount,
narration: transaction.narration,
createdAt: transaction.createdAt,
updatedAt: transaction.updatedAt,
};
});

return formattedTransactions;
}

/**
* Fetch details of a single transaction.
* @param {string} transactionId - The ID of the transaction to fetch.
* @returns {Promise<Object>} A Promise that resolves to details of the single transaction.
* @throws {NotFoundError} Will throw a NotFoundError if the transaction is not found.
*/
async function fetchSingleTransaction(transactionId) {
const transaction = await Transaction.findByPk(transactionId, {
include: [
{
model: Wallet,
as: "creditWallet",
attributes: ["id", "balance"],
},
{
model: Wallet,
as: "debitWallet",
attributes: ["id", "balance"],
},
],
});

if (!transaction) {
throw new NotFoundError("Transaction not found");
}

// Determine the type of transaction (credit or debit) and extract relevant details
let transactionDetails = {
id: transaction.id,
amount: transaction.amount,
status: transaction.status,
narration: transaction.narration,
createdAt: transaction.createdAt,
updatedAt: transaction.updatedAt,
type: transaction.creditWalletId ? "credit" : "debit",
wallet: {
id: transaction.creditWalletId
? transaction.creditWallet.id
: transaction.debitWallet.id,
balance: transaction.creditWalletId
? transaction.creditWallet.balance
: transaction.debitWallet.balance,
},
};

return transactionDetails;
}

module.exports = { fetUserTransactions };
module.exports = { fetchUserTransactions, fetchSingleTransaction };

0 comments on commit 27f474f

Please sign in to comment.