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

Assignment 14 Wealth Portfolio App #46

Open
wants to merge 2 commits into
base: master
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
2 changes: 2 additions & 0 deletions Week-14/Assignment 14.1/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DB_URL=mongodb+srv://rahulsawant:password%40123@cluster0.ycesmco.mongodb.net/?retryWrites=true&w=majority
PORT=3001
Binary file added Week-14/Assignment 14.1/Screenshot.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 41 additions & 0 deletions Week-14/Assignment 14.1/helper/model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const mongoose=require('mongoose')
const userSchema=new mongoose.Schema({
fname:{ required:true,type:String},
lname:{ required:true,type:String},
email:{ required:true,type:String},
username:{ required:true, type:String,unique: true},
password:{ required:true,type:String}
})
userSchema.path('username').validate(async (username)=>{
const count_username= await mongoose.models.User.countDocuments({username})

return !count_username
},'Username Already Exists')
userSchema.path('email').validate(async (email)=>{

const count_email= await mongoose.models.User.countDocuments({email})
return !count_email
},'Email Already Exists')
const userModel=mongoose.model('User',userSchema)



const holdingsSchema=new mongoose.Schema({
asset_type:{ required:true,type:String,enum: ['RD','FD','STOCKS','MUTUALFUNDS',"FIXEDINCOME","EXPENSES"]},
asset_id:{ required:true,type:String},
asset_value:{ required:true,type:Number},
intrest_rate:{ required:false,type:Number},
invested_date:{ required:true,type:Date},
invested_period_years:{ required:false,type:Number},
maturity_date:{ required:false,type:Date},
status:{type:String,enum:['ACTIVE','INACTIVE']},
expense_type:{type:String, required:true,enum:["CREDIT","DEBIT"]},
document:{type:Buffer,required:false}
})
holdingsSchema.path('asset_id').validate(async (asset_id)=>{
const count_asset_id= await mongoose.models.Holding.countDocuments({asset_id})

return !count_asset_id
},'Asset ID Already Exists')
const holdingModel=mongoose.model('Holding',holdingsSchema)
module.exports={userModel,holdingModel}
240 changes: 240 additions & 0 deletions Week-14/Assignment 14.1/helper/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
const express = require("express");
const mongoose = require("mongoose");
const route = express.Router();
const { userModel, holdingModel } = require("./model");
const Grid = require("gridfs-stream");
const fs = require("fs");
const database = require("../index");
Grid.mongo = mongoose.mongo;
//Login request
route.post("/login", async (req, res) => {
const { username, password } = req.body;

userModel.findOne({ username: username, password: password }, (err, user) => {
if (err) {
res.status(500).send({ error: err });
}
if (!user) {
res.status(404).send({ error: "user not found" });
}

res.status(200).send({ message: `User sucessfully logged in` });
});
});

//Register
route.post("/register", async (req, res) => {
const data = new userModel({
fname: req.body.fname,
lname: req.body.lname,
email: req.body.email,
username: req.body.username,
password: req.body.password,
});
try {
const datasave = await data.save();
res
.status(200)
.json({ message: "Registration Successful", user: req.body.username });
} catch (error) {
res.status(400).json({ message: error.message });
}
});

//Add assests
route.post("/holdings", async (req, res) => {
const data = new holdingModel({
asset_type: req.body.asset_type,
asset_id: req.body.asset_id,
asset_value: req.body.asset_value,
intrest_rate: req.body.intrest_rate ? req.body.intrest_rate : 0,
invested_date: req.body.invested_date,
invested_period_years: req.body.invested_period_years
? req.body.invested_period_years
: 0,
maturity_date: req.body.maturity_date ? req.body.maturity_date : null,
status: req.body.status ? req.body.status : "ACTIVE",
expense_type: req.body.expense_type,
});
try {
const datasave = await data.save();
res.status(200).json({ message: "Assests added sucessfully" });
} catch (error) {
res.status(400).json({ message: error.message });
}
});

//fetch all assets
route.get("/holdings", async (req, res) => {
try {
const allData = await holdingModel.find();
res.json(allData);
} catch (error) {
res.status(500).json({ message: error });
}
});

//get income ,expense and saving for current year
// route.get("/holdings/stats", async (req, res) => {
// var today = new Date();
// if (today.getMonth() + 1 <= 3) {
// start = new Date(today.getFullYear() - 1, 03, 01);
// end = new Date(today.getFullYear(), 02, 31);
// } else {
// start = new Date(today.getFullYear(), 03, 01);
// end = new Date(today.getFullYear() + 1, 02, 31);
// }
// try {
// const allData = await holdingModel.find({
// $and: [
// { invested_date: { $gt: start } },
// { invested_date: { $lt: end } },
// ],
// }).aggregate( [ { $group : { _id : "$expense_type" } } ] )
// ;
// res.json(allData);
// } catch (error) {
// res.status(500).json({ message: error });
// }
// });

//get income ,expense and saving
route.get("/holdings/stats", async (req, res) => {
try {
const allData = await holdingModel.aggregate([
{ $match: { expense_type: "DEBIT" } },
{ $group: { _id: "$expense_type", total: { $sum: "$asset_value" } } },
]);
const debit = allData[0].total ? allData[0].total : 0;

const allData1 = await holdingModel.aggregate([
{ $match: { asset_type: "FIXEDINCOME" } },
{ $group: { _id: "$asset_type", total: { $sum: "$asset_value" } } },
]);
const income = allData1[0].total ? allData1[0].total : 0;

const allData2 = await holdingModel.aggregate([
{ $match: { expense_type: "CREDIT" } },
{ $group: { _id: "$expense_type", total: { $sum: "$asset_value" } } },
]);
const credit = allData2[0].total ? allData2[0].total : 0;

res.send({ Income: income, Expenses: debit, Savings: income - debit });
} catch (error) {
res.status(500).json({ message: error });
}
});

//get holding by asset id
route.get("/holdings/:id", async (req, res) => {
const data = await holdingModel.find({ asset_id: req.params.id });
try {
res.json(data);
} catch (err) {
res.send(400).json({ message: err.message });
}
});

//update holding by asset id
route.put("/holdings/:id", async (req, res) => {
const asset_id = req.params.id;
const update_data = req.body;

try {
const data = await holdingModel.findOneAndUpdate(
{ asset_id: asset_id },
update_data,
{
new: true,
upsert: true,
}
);
res.send({ message: `Asset ${asset_id} sucessfully updated`, data: data });
} catch (err) {
res.send(400).json({ message: err.message });
}
});

//Activate holding by asset id
route.put("/holdings/activate/:id", async (req, res) => {
const asset_id = req.params.id;

holdingModel.findOne({ asset_id: asset_id }, async (err, asset) => {
if (err) {
res.status(500).send({ error: err });
}
if (asset.status == "ACTIVATE") {
res.send({ message: `Asset ${asset_id} Already Activated` });
}
try {
const data = await holdingModel.findOneAndUpdate(
{ asset_id: asset_id },
{ status: "ACTIVATE" },
{
new: true,
upsert: true,
}
);
res.send({ message: `Asset ${asset_id} sucessfully Activated` });
} catch (err) {
res.send(400).json({ message: err.message });
}
});
});

//Inctivate holding by asset id
route.put("/holdings/inactivate/:id", async (req, res) => {
const asset_id = req.params.id;

holdingModel.findOne({ asset_id: asset_id }, async (err, asset) => {
if (err) {
res.status(500).send({ error: err });
}
if (asset.status == "INACTIVATE") {
res.send({ message: `Asset ${asset_id} Already Inctivated` });
}
try {
const data = await holdingModel.findOneAndUpdate(
{ asset_id: asset_id },
{ status: "INACTIVATE" },
{
new: true,
upsert: true,
}
);
res.send({ message: `Asset ${asset_id} sucessfully Inctivated` });
} catch (err) {
res.sendStatus(400);
}
});
});

//add documnet by asset id
// route.post("/holdings/doc/:id", async (req, res) => {

// try {
// const asset_id = req.params.id;
// const {path} = req.body;
// const gfs=Grid(database.db)
// const writestream=gfs.createWriteStream({
// document:`documnet${asset_id}.pdf`
// })
// fs.createReadStream(path).pipe(writestream)
// writestream.on('close',(file)=>{
// console.log(`${file.filename} written to DB`)
// })
// // const data = await holdingModel.findOneAndUpdate(
// // { asset_id: asset_id },
// // update_data,
// // {
// // new: true,
// // upsert: true,
// // }
// // );
// res.send({ message: `Asset ${asset_id} sucessfully updated`, data: data });
// } catch (err) {
// res.send(400).json({ message: err.message });
// }
// });

module.exports = route;
23 changes: 23 additions & 0 deletions Week-14/Assignment 14.1/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const { application } = require('express')
const express=require('express')
const mongoose=require('mongoose')
const routes=require('./helper/route')
const app=express()
require("dotenv").config();
const PORT = process.env.PORT;
const DB_URL = process.env.DB_URL;
mongoose.connect(DB_URL)
const database=mongoose.connection
database.on('error',(error)=>{
console.log(error)
})

database.once('connected',()=>{
console.log('database connected')
})
app.use(express.json())
app.listen(PORT,()=>{
console.log(`Server started a 127.0.0.1:${PORT}`)
})
app.use('/api',routes)
module.exports = database;
Loading