Skip to content

Commit

Permalink
Merge pull request #2 from yesahem/main
Browse files Browse the repository at this point in the history
Fixed #1 
yasahem fixed the balance not showing issue
  • Loading branch information
baldanaresh authored Aug 4, 2024
2 parents c2d2386 + e208b55 commit 6ac8e8c
Show file tree
Hide file tree
Showing 6 changed files with 248 additions and 215 deletions.
Binary file added .DS_Store
Binary file not shown.
74 changes: 37 additions & 37 deletions backend/db.js
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,
};
3 changes: 2 additions & 1 deletion backend/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const express =require( "express");
// import user from "./db";
const mainRouter =require("./routes/index");
const cors = require('cors')

const app=express();
app.use(cors());
Expand All @@ -10,6 +11,6 @@ app.use("/api/v1/",mainRouter);



app.listen(3000,()=>{
app.listen(3001,()=>{
console.log(" im working on port 3000");
})
55 changes: 31 additions & 24 deletions backend/middleware.js
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
}
97 changes: 53 additions & 44 deletions backend/routes/transaction.js
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;

Loading

0 comments on commit 6ac8e8c

Please sign in to comment.