Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

completed backend for paytm #39

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions backend/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
JWT_SECRETS: "paytm@prince"
}
49 changes: 49 additions & 0 deletions backend/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const mongoose = require("mongoose");

mongoose.connect("mongodb+srv://prince981620:Hello%[email protected]/paytm");

const userSchema = new mongoose.Schema({
username: {
type: String,
required: true,
unique: true,
trim: true, // trim spaces from begininig and ends
lowercase: true,
minLength: 3,
maxLength: 30
},
password: {
type: String,
required: true,
minLength: 6
},
firstName: {
type: String,
required: true,
trim: true,
maxLength: 50
},
lastName: {
type: String,
required: true,
maxLength: 50
}
})
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);
const User = mongoose.model("User",userSchema);

module.exports = {
User,
Account
}
11 changes: 11 additions & 0 deletions backend/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
const express = require("express");
const mainRouter = require("./routes/index");
const cors = require("cors");

const app = express();

app.use(cors());
app.use(express.json());
// all the req for /api/v1 goes to mainRouter i.e /routes/index.js
app.use("/api/v1",mainRouter);

app.listen(3000,()=>{
console.log("listening on port 3000");
})
26 changes: 26 additions & 0 deletions backend/middileware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const jwt = require("jsonwebtoken");
const { JWT_SECRETS } = require("./config");


const authMiddleware = (req,res,next)=>{
const authHeader = req.headers.authorization;
if(!authHeader || !authHeader.startsWith('Bearer ')){
return res.status(411).json({});
}
const token = authHeader.split(" ")[1];

try{
const decoded = jwt.verify(token,JWT_SECRETS);
if(decoded.userId){
req.userId = decoded.userId;
next();
}else{
return res.status(403).json({});
}
}catch(err){
return res.status(403),json({});
}
}
module.exports = {
authMiddleware
}
126 changes: 126 additions & 0 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
"author": "",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.18.2",
"jsonwebtoken": "^9.0.2",
"mongoose": "^8.1.0",
"zod": "^3.22.4"
}
Expand Down
56 changes: 56 additions & 0 deletions backend/routes/account.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const express = require("express");
const { authMiddleware } = require("../middileware");
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
})
if(account){
res.status(200).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 account details initiating trtansaction
const account = await Account.findOne({userId: req.userId}).session(session);

if(!account || account.balance < amount){
await session.abortTransaction();
return res.status(400).json({
msg: "Insufficient balance"
})
}
// fetch the account details of the user receivnig money

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);

// now commit the transaction
await session.commitTransaction();
res.json({
msg: "transfer Successfull"
})

})

module.exports = router;
12 changes: 12 additions & 0 deletions backend/routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const express = require("express");
const userRouter = require("./user");
const accountRouter = require("./account");
const router = express.Router();
// all the req for /api/v1 comes to this router i.e /routes/index.js

// and from here all the req goes to /api/v1/user goes to
router.use("/user",userRouter);
// and from here all the req goes to /api/v1/account goes to
router.use("/account",accountRouter);

module.exports = router;
Loading