-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from yesahem/main
Fixed #1 yasahem fixed the balance not showing issue
- Loading branch information
Showing
6 changed files
with
248 additions
and
215 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 |
---|---|---|
@@ -1,46 +1,46 @@ | ||
const mongoose=require("mongoose"); | ||
const mongoose = require("mongoose"); | ||
|
||
mongoose.connect("https://localhost:27017/paytm"); | ||
|
||
const userSchema= new mongoose.Schema({ | ||
username:{ | ||
type:String, | ||
required:true, | ||
minLength:8, | ||
maxLength:30 | ||
}, | ||
password:{ | ||
type:String, | ||
required:true, | ||
minLength:8, | ||
maxLength:30 | ||
}, | ||
firstname:{ | ||
type:String, | ||
required:true, | ||
}, | ||
lastname:{ | ||
type:String, | ||
required:true, | ||
} | ||
const userSchema = new mongoose.Schema({ | ||
username: { | ||
type: String, | ||
required: true, | ||
minLength: 8, | ||
maxLength: 30, | ||
}, | ||
password: { | ||
type: String, | ||
required: true, | ||
minLength: 8, | ||
maxLength: 30, | ||
}, | ||
firstname: { | ||
type: String, | ||
required: true, | ||
}, | ||
lastname: { | ||
type: String, | ||
required: true, | ||
}, | ||
}); | ||
const accountSchema = new mongoose.Schema({ | ||
userId: { | ||
type: mongoose.Schema.Types.ObjectId, // Reference to User model | ||
ref: 'User', | ||
required: true | ||
}, | ||
balance: { | ||
type: Number, | ||
required: true | ||
} | ||
userId: { | ||
type: mongoose.Schema.Types.ObjectId, // Reference to User model | ||
ref: "User", | ||
required: true, | ||
}, | ||
balance: { | ||
type: Number, | ||
required: true, | ||
}, | ||
}); | ||
|
||
const user=mongoose.model("user",userSchema); | ||
const User = mongoose.model("User", userSchema); | ||
|
||
const Account = mongoose.model('Account', accountSchema); | ||
const Account = mongoose.model("Account", accountSchema); | ||
|
||
module.exports={ | ||
user, | ||
Account | ||
} | ||
module.exports = { | ||
User, | ||
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
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,28 +1,35 @@ | ||
const {JWT_SEC}=require("./config"); | ||
const jwt=require("jsonwebtoken"); | ||
const JWT_SEC = require("./config"); | ||
const jwt = require("jsonwebtoken"); | ||
|
||
const authMiddleware=(req,res,next)=>{ | ||
const authHeader=req.header.authorization; | ||
if(!authHeader || !authHeader.startWith('Bearer')){ | ||
return res.status(403).json({}); | ||
} | ||
const token=authHeader.split(' ')[1]; | ||
|
||
try{ | ||
const decoded=jwt.verify(token,JWT_SEC); | ||
if(decoded.userId){ | ||
res.userId=decoded.userId; | ||
next(); | ||
const authMiddleware = (req, res, next) => { | ||
const authHeader = req.headers.authorization; | ||
console.log(typeof authHeader); | ||
if (!authHeader || !authHeader.startsWith("Bearer ")) { | ||
return res.status(403).json({ | ||
msg: "Error in middleware", | ||
}); | ||
} | ||
const token = authHeader.split(" ")[1]; | ||
|
||
} | ||
else{ | ||
return res.status(403).json({}); | ||
} | ||
}catch(err){ | ||
return res.status(403).json({}); | ||
try { | ||
console.log(JWT_SEC); | ||
const decoded = jwt.verify(token, JWT_SEC); | ||
if (decoded.userId) { | ||
req.userId = decoded.userId; | ||
next(); | ||
} else { | ||
return res.status(403).json({ | ||
msg: "Something went wrong ", | ||
}); | ||
} | ||
} | ||
} catch (err) { | ||
return res.status(403).json({ | ||
msg: "Error thrown", | ||
}); | ||
} | ||
}; | ||
|
||
module.exports = { | ||
authMiddleware, | ||
}; | ||
|
||
module.exports={ | ||
authMiddleware | ||
} |
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,55 +1,64 @@ | ||
const express = require('express'); | ||
const { authMiddleware } = require('../middleware'); | ||
const { Account } = require('../db'); | ||
const { default: mongoose } = require('mongoose'); | ||
const express = require("express"); | ||
const { authMiddleware } = require("../middleware"); | ||
const { Account } = require("../db"); | ||
const { default: mongoose } = require("mongoose"); | ||
|
||
const router = express.Router(); | ||
|
||
router.get("/balance", authMiddleware, async (req, res) => { | ||
const account = await Account.findOne({ | ||
userId: req.userId | ||
}); | ||
const account = await Account.findOne({ | ||
userId: req.userId, | ||
}); | ||
console.log(account); | ||
|
||
res.json({ | ||
balance: account.balance | ||
}) | ||
res.json({ | ||
balance: account.balance, | ||
}); | ||
}); | ||
|
||
|
||
router.post("/transfer", authMiddleware, async (req, res) => { | ||
const session = await mongoose.startSession(); | ||
|
||
session.startTransaction(); | ||
const { amount, to } = req.body; | ||
|
||
// Fetch the accounts within the transaction | ||
const account = await Account.findOne({ userId: req.userId }).session(session); | ||
|
||
if (!account || account.balance < amount) { | ||
await session.abortTransaction(); | ||
return res.status(400).json({ | ||
message: "Insufficient balance" | ||
}); | ||
} | ||
|
||
const toAccount = await Account.findOne({ userId: to }).session(session); | ||
|
||
if (!toAccount) { | ||
await session.abortTransaction(); | ||
return res.status(400).json({ | ||
message: "Invalid account" | ||
}); | ||
} | ||
|
||
// Perform the transfer | ||
await Account.updateOne({ userId: req.userId }, { $inc: { balance: -amount } }).session(session); | ||
await Account.updateOne({ userId: to }, { $inc: { balance: amount } }).session(session); | ||
|
||
// Commit the transaction | ||
await session.commitTransaction(); | ||
res.json({ | ||
message: "Transfer successful" | ||
const session = await mongoose.startSession(); | ||
|
||
session.startTransaction(); | ||
const { amount, to } = req.body; | ||
|
||
// Fetch the accounts within the transaction | ||
const account = await Account.findOne({ userId: req.userId }).session( | ||
session, | ||
); | ||
|
||
if (!account || account.balance < amount) { | ||
await session.abortTransaction(); | ||
return res.status(400).json({ | ||
message: "Insufficient balance", | ||
}); | ||
} | ||
|
||
const toAccount = await Account.findOne({ userId: to }).session(session); | ||
|
||
if (!toAccount) { | ||
await session.abortTransaction(); | ||
return res.status(400).json({ | ||
message: "Invalid account", | ||
}); | ||
} | ||
|
||
// Perform the transfer | ||
await Account.updateOne( | ||
{ userId: req.userId }, | ||
{ $inc: { balance: -amount } }, | ||
).session(session); | ||
await Account.updateOne( | ||
{ userId: to }, | ||
{ $inc: { balance: amount } }, | ||
).session(session); | ||
|
||
// Commit the transaction | ||
await session.commitTransaction(); | ||
res.json({ | ||
message: "Transfer successful", | ||
}); | ||
}); | ||
|
||
module.exports = router; | ||
module.exports = router; | ||
|
Oops, something went wrong.