Skip to content

Commit

Permalink
transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
ubongedem78 committed Feb 27, 2024
1 parent 27f474f commit b516820
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/controllers/transaction.controller.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const {
fetchUserTransactions,
fetchSingleTransaction,
generateTransaction,
} = require("../utils/transaction.Util");

const getAllUserTransactions = async (req, res, next) => {
Expand Down Expand Up @@ -31,4 +32,20 @@ const getSingleTransaction = async (req, res, next) => {
}
};

// generate payment request
const generatePaymentRequest = async (req, res, next) => {
const { userId } = req.params;
const { amount, narration } = req.body;
try {
const transaction = await generateTransaction(userId, amount, narration);

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

module.exports = { getAllUserTransactions, getSingleTransaction };
23 changes: 22 additions & 1 deletion src/utils/transaction.Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,25 @@ async function fetchSingleTransaction(transactionId) {
return transactionDetails;
}

module.exports = { fetchUserTransactions, fetchSingleTransaction };
async function generateTransaction(userId, amount, narration) {
const user = await User.findByPk(userId);

if (!user) {
throw new NotFoundError("User not found");
}

// Create a new transaction
const transaction = await Transaction.create({
amount,
narration,
debitWalletId: user.Wallet.id,
});

return transaction;
}

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

0 comments on commit b516820

Please sign in to comment.