-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
101 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const { mongoose } = require("mongoose"); | ||
|
||
const accountSchema = new mongoose.Schema({ | ||
userId: { | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: "User", | ||
required: true, | ||
}, | ||
balance: { | ||
type: Number, | ||
required: true, | ||
}, | ||
}); | ||
|
||
const Account = mongoose.model("Account", accountSchema); | ||
module.exports = Account; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
const express = require("express"); | ||
const router = express.Router(); | ||
const Account = require("../Models/accountModel"); | ||
const authMiddleware = require("../Middleware/authMiddleware"); | ||
router.get("/balance", authMiddleware, async (req, res) => { | ||
try { | ||
const account = await Account.findOne({ userId: req.userId }); | ||
|
||
if (!account) { | ||
return res.status(404).json({ message: "Account not found" }); | ||
} | ||
|
||
res.json({ balance: account.balance }); | ||
} catch (error) { | ||
console.error("Error fetching account balance:", error); | ||
res.status(500).json({ message: "Internal server error" }); | ||
} | ||
}); | ||
|
||
router.post("/transfer", authMiddleware, async (req, res) => { | ||
|
||
const { amount, to } = req.body; | ||
|
||
const account = await Account.findOne({ userId: req.userId }) | ||
|
||
if (!account || account.balance < amount) { | ||
return res.status(400).json({ | ||
message: "Insufficient balance", | ||
}); | ||
} | ||
|
||
const toAccount = await Account.findOne({ userId: to }) | ||
if (!toAccount) { | ||
return res.status(400).json({ | ||
message: "Invalid account", | ||
}); | ||
} | ||
|
||
await Account.updateOne( | ||
{ userId: req.userId }, | ||
{ $inc: { balance: -amount } } | ||
) | ||
await Account.updateOne( | ||
{ userId: to }, | ||
{ $inc: { balance: amount } } | ||
) | ||
|
||
|
||
res.json({ | ||
message: "Transfer successful", | ||
}); | ||
}); | ||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
const express = require("express"); | ||
const router = express.Router(); | ||
const authRoutes = require("./authRoutes"); | ||
const accountRoutes = require("./accountRoutes"); | ||
const authMiddleware = require("../Middleware/authMiddleware"); | ||
|
||
router.use("/user", authRoutes); | ||
router.use("/account", accountRoutes); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters