Skip to content
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 Finance Management/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
package-lock.json
224 changes: 224 additions & 0 deletions Finance Management/controllers/FinanceManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
import fs from "fs"

class FinanceManager{
constructor(path,users){
this.path=path
this.users=users
}
async readFile(){
try {
const usersFinances =await fs.promises.readFile(this.path,"utf-8")
return JSON.parse(usersFinances)
} catch (error) {
if(error.code==="ENOENT"){
return []
}else{
return error
}
}
}
async readUsers(){
try {
const users=await fs.promises.readFile(this.users,"utf-8")
return JSON.parse(users)
} catch (error) {
if(error.code==="ENOENT"){
return []
}else{
return error
}
}
}
async updateFinance(uid,monthlyIncomes,expenses){
console.log(uid)
try {
const users=await this.readUsers()
console.log(users)
const user=users.find(user=>user.id===uid)
const usersFinances=await this.readFile();
const newFinance={
balance:null,
status:null,
id:uid,
monthlyIncomes:[
{description:monthlyIncomes.description,
amount:monthlyIncomes.amount}
],
bills:{
medical:[{
description:expenses.medicalDescription,
amount:expenses.medical
}],
homespends:[{
description:expenses.homeExpensesDescription,
amount:expenses.homeExpenses
}],
leisure:[{
description:expenses.entertainmentDescription,
amount:expenses.entertainment
}],
saving:[{
description:expenses.savingsDescription,
amount:expenses.savings
}],
education:[{
description:expenses.educationDescription,
amount:expenses.education
}]
}
}
const totalExpenses=expenses.medical+expenses.homeExpenses+expenses.entertainment+expenses.savings+expenses.education
const amountMax=Math.max(expenses.medical,expenses.homeExpenses,expenses.entertainment,expenses.savings,expenses.education
)
console.log(amountMax)
const from=Object.keys(newFinance.bills).find(key => {
const bill = newFinance.bills[key][0];
return bill.amount === amountMax;
})
const biggestDescription=newFinance.bills[from][0].description
newFinance.balance={
amount:monthlyIncomes.amount-totalExpenses,
biggestExpense:{
amount:amountMax,
from:Object.keys(newFinance.bills).find(key => {
const bill = newFinance.bills[key][0];
return bill.amount === amountMax;
}),
description:biggestDescription
}
}
if(newFinance.balance<totalExpenses)newFinance.status=false;
if(newFinance.balance>totalExpenses)newFinance.status=true;
if(newFinance.balance===totalExpenses)newFinance.status=null;
usersFinances.push(newFinance);
if(usersFinances.length>1) fs.unlinkSync(this.path)
await fs.promises.writeFile(this.path,JSON.stringify(usersFinances,null,"\t"),"utf-8");

const data={
message:"Succesfully registration",
status:true
}
user.monthlyIncome=true;
await fs.promises.writeFile(this.users,JSON.stringify(users,null,"\t"),"utf-8");
return data
} catch (error) {
return error
}

}
async getTotalByCategory(uid,category){
try {
const users=await this.readUsers();
const usersFinances=await this.readFile();
const userFinance=usersFinances.find(finance=>finance.id===uid);
const financeCategory=userFinance.bills[category];
let total=0
financeCategory.forEach(element => {
total=total+element.amount
});
const data={
message:`The total of the amount of ${category} expenses is : ${total}`,
amount:total
}
return data
} catch (error) {
return error
}
}
async getTotalExpenses (uid){
try {
const users=await this.readUsers()
const user=users.find(user=>user.id===uid);
const finances=await this.readFile()
const finance=finances.find(finance=>finance.id===uid);
const bills=finance.bills
let total=0
for (const category in bills) {
if (bills.hasOwnProperty(category)) {
const categoryExpenses = bills[category];
categoryExpenses.forEach(expense => {
total += expense.amount;
});
}
}
const data={
message:`The total of all expenses is : ${total}`,
total:total
}
return data

} catch (error) {
return error
}
}
async getTotalIncomes(uid){
try {
const finances=await this.readFile()
const finance=finances.find(finance=>finance.id===uid);
const incomes=finance.monthlyIncomes
let total=0
incomes.forEach(element=>{
total=total+element.amount
})
const data={
message:`The total of all incomes is : ${total}`,
total:total
}
return data
} catch (error) {
return error
}
}
async addExpense(uid,amount,category,description){
try {
const usersFinances=await this.readFile();
const userFinance=usersFinances.find(finance=>finance.id===uid)
const newExpense={
description:description,
amount:amount
}
userFinance.bills[category].push(newExpense)
userFinance.balance.amount=userFinance.balance.amount-amount
if(newExpense.amount>userFinance.balance.biggestExpense.amount){
userFinance.balance.biggestExpense.amount=amount;
userFinance.balance.biggestExpense.from=category;
userFinance.balance.biggestExpense.description=description;
}
fs.unlinkSync(this.path);
await fs.promises.writeFile(this.path,JSON.stringify(usersFinances,null,"\t"),"utf-8")
const data={
message:`NEW EXPENSE HAS BEEN ADDED,YOUR BALANCE IS : ${userFinance.balance.amount}`,
status:true
}
return data

} catch (error) {
return error
}
}
async addIncome(uid,amount,description){
try {
const finances=await this.readFile();
const finance=finances.find(finance=>finance.id===uid);
const incomes=finance.monthlyIncomes
const newIncome={
description:description,
amount:amount
}
incomes.push(newIncome);
finance.balance.amount=finance.balance.amount+amount
fs.unlinkSync(this.path);
await fs.promises.writeFile(this.path,JSON.stringify(finances,null,"\t"),"utf-8");
const data={
message:`a new income has been added, your new balance is :${finance.balance.amount}`,
status:true
}

return data
} catch (error) {
return error
}

}
}
export default FinanceManager
109 changes: 109 additions & 0 deletions Finance Management/controllers/UserManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import fs from "fs"
class UserManager{
constructor(path){
this.path=path;
}
async readFile(){
try {
const users=await fs.promises.readFile(this.path,"utf-8")
return JSON.parse(users)
} catch (error) {
if(error.code==="ENOENT"){
return []
}else{
return error
}
}
}
async createUser(name,lastname,email,username,password){
try {
const users=await this.readFile(this.path,"utf-8");
const validateUsername=users.some(user=>user.username===username);
if(validateUsername){
const data={
message:"USERNAME ALREADY USED,TRY ANOTHER",
status:null,
}
return data
}else{
const newUser={
username:username,
password:password,
name:name,
lastname:lastname,
email:email,
loginFailed:0,
status:true,
id:await this.getNextId(),
monthlyIncome:false
}
users.push(newUser);
if(users.length>1)fs.unlinkSync(this.path);
await fs.promises.writeFile(this.path,JSON.stringify(users,null,'\t'),"utf-8");
const data={
message:"USER HAS BEEN CREATED SUCCESSFULLY",
status:true
}
return data
}

} catch (error) {
return error
}
}
async validateUser(username,password){
try {
const users=await this.readFile();
const user=users.find(user=>user.username===username);
if(!user){
const data={
message:"THERE IS NO USER REGISTERED WITH THE INPUTED USERNAME",
status:null
}
return data
}else{
if(user.status===false){
const data={
message:"THIS USER IS BANNED,CONCATC WITH AN ADMIN",
status:false
}
return data
}else{
if(user.password===password){
return user
}else{
user.loginFailed=user.loginFailed+1
if(user.loginFailed===3){
user.status=false
await fs.promises.writeFile(this.path,JSON.stringify(users,null,"\t"),"utf-8")
const data={
message:"WRONG PASSWORD 3 TIMES,YOUR USER HAS BEEN BANNED",
status:false
}
return data
}else{
await fs.promises.writeFile(this.path,JSON.stringify(users,null,"\t"),"utf-8")
const data={
message:`WRONG PASSWORD, U HAVE ${3-user.loginFailed} MORE TRYS`,
status:null
}
return data
}
}
}
}
} catch (error) {
return error
}
}
async getNextId(){
let products=await this.readFile();
if(products==="ENOENT"||products.length===0){
return 1
}else{
console.log
return products[products.length -1].id+1
}
}
}
export default UserManager
33 changes: 33 additions & 0 deletions Finance Management/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import express from "express"
import __dirname from "./utils.js"
import handlebars from "express-handlebars"
import userRouter from "./router/userRouter.js"
import menuRouter from "./router/menuRouter.js"

const port=8080
const app=express();

app.use(express.json());
app.use(express.urlencoded({extended:true}));
app.use('/',express.static(__dirname+"/public"))
//ROUTES
app.use("/",userRouter)
app.use('/menu',menuRouter)

//SET HANDLEBARS
app.engine('handlebars',handlebars.engine());
app.set('view engine',"handlebars");
app.set('views',__dirname+'/views');


app.listen(port,()=>{
console.log(`app listening on port ${port}`)
}).on("error", function (err) {
process.once("SIGUSR2", function () {
process.kill(process.pid, "SIGUSR2");
});
process.on("SIGINT", function () {
// this is only called on ctrl+c, not restart
process.kill(process.pid, "SIGINT");
});
});
22 changes: 22 additions & 0 deletions Finance Management/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "finance-management",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type":"module",
"scripts": {
"start":"node index.js",
"dev":"nodemon index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.19.2",
"express-handlebars": "^6.0.7"
},
"devDependencies": {
"nodemon": "^3.1.0"
}
}
Binary file added Finance Management/public/img/magment.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading