Merge pull request #38 from JoaquinPar/feature/vehicle-depreciation

feature/vehicle depreciation calculated towards total assets value
This commit is contained in:
Nícolas Agostini authored and GitHub committed 2025-05-21 16:40:15 -07:00
commit 0672981e69
4 files changed
+37 -6

No files matched your search

+6 -1
View File
@@ -1,5 +1,5 @@
const getRates = require("../util/exchangeRate"); const getRates = require("../util/exchangeRate");
const { calculateProgress, updatePlanProgressInDB, updateProgress } = require("../util/calculations"); const { calculateProgress, updatePlanProgressInDB, updateProgress, calculateTotalAssetValue } = require("../util/calculations");
const suggestions = require("../util/suggestions"); const suggestions = require("../util/suggestions");
const status = require("../util/statuses"); const status = require("../util/statuses");
const ObjectId = require('mongodb').ObjectId; const ObjectId = require('mongodb').ObjectId;
@@ -110,11 +110,14 @@ module.exports = (middleware, users, plans, assets) => {
router.get('/assets', async (req, res) => { router.get('/assets', async (req, res) => {
let userAssets = await assets.find({ userId: new ObjectId(req.session.userId) }).toArray(); let userAssets = await assets.find({ userId: new ObjectId(req.session.userId) }).toArray();
let totalUserAssetValue = await calculateTotalAssetValue(userAssets);
res.render('assets', { res.render('assets', {
user: req.session.user, user: req.session.user,
errMessage: req.session.errMessage, errMessage: req.session.errMessage,
assets: userAssets, assets: userAssets,
geoData: req.session.geoData, geoData: req.session.geoData,
totalUserAssetValue: totalUserAssetValue,
icons: icons, icons: icons,
}); });
return res.status(status.Ok); return res.status(status.Ok);
@@ -142,6 +145,7 @@ module.exports = (middleware, users, plans, assets) => {
try { try {
const planId = req.params.id; const planId = req.params.id;
let userAssets = await assets.find({ userId: new ObjectId(req.session.userId) }).toArray(); let userAssets = await assets.find({ userId: new ObjectId(req.session.userId) }).toArray();
let totalUserAssetValue = await calculateTotalAssetValue(userAssets);
if (!ObjectId.isValid(planId)) { if (!ObjectId.isValid(planId)) {
req.session.errMessage = "Invalid plan ID format."; req.session.errMessage = "Invalid plan ID format.";
@@ -160,6 +164,7 @@ module.exports = (middleware, users, plans, assets) => {
user: req.session.user, user: req.session.user,
plan: plan, plan: plan,
geoData: req.session.geoData, geoData: req.session.geoData,
totalUserAssetValue: totalUserAssetValue,
assets: userAssets, assets: userAssets,
progress: progress, progress: progress,
suggestions: await suggestions.generateSuggestions(), suggestions: await suggestions.generateSuggestions(),
+29 -3
View File
@@ -12,7 +12,7 @@ async function calculatePlanProgress(plans, assets, userId) {
try { try {
const userAssets = await assets.find({ userId: new ObjectId(userId) }).toArray(); const userAssets = await assets.find({ userId: new ObjectId(userId) }).toArray();
const totalUserAssetValue = userAssets.reduce((total, asset) => total + asset.value, 0); const totalUserAssetValue = await calculateTotalAssetValue(userAssets);
let percentage = 0; let percentage = 0;
if (plans.retirementAssets > 0) { if (plans.retirementAssets > 0) {
@@ -42,6 +42,31 @@ async function updatePlanProgressInDB(planId, percentage, plans) {
} }
} }
async function calculateTotalAssetValue(assets) {
if (!assets || typeof assets.find !== 'function') {
console.error("Error with the assets collection");
return;
}
try {
let totalAssetValue = 0;
for (const asset of assets) {
if(asset.icon == "Motorcycle" || asset.icon == "Car"){
today = new Date();
asset.year = new Date(asset.year).getFullYear();
let age = today.getFullYear() - asset.year;
let value = asset.value * Math.pow(0.85, age);
totalAssetValue += value;
} else {
totalAssetValue += asset.value;
}
}
return totalAssetValue;
} catch (err) {
console.error("Error in calculateTotalAssetValue:", err);
return 0;
}
}
async function calculateProgress(plan, assets, users, userId) { async function calculateProgress(plan, assets, users, userId) {
if (!plan || typeof plan !== 'object') { if (!plan || typeof plan !== 'object') {
@@ -63,8 +88,9 @@ async function calculateProgress(plan, assets, users, userId) {
try { try {
const userAssets = await assets.find({ userId: new ObjectId(userId) }).toArray(); const userAssets = await assets.find({ userId: new ObjectId(userId) }).toArray();
const totalUserAssetValue = userAssets.reduce((total, asset) => total + asset.value, 0); const totalUserAssetValue = await calculateTotalAssetValue(userAssets);
const totalUserPlanValue = plan.retirementAssets; const totalUserPlanValue = plan.retirementAssets;
const userDoc = await users.findOne({ _id: new ObjectId(userId) }); const userDoc = await users.findOne({ _id: new ObjectId(userId) });
@@ -125,4 +151,4 @@ async function updateProgress(plans, assets, users, userId) {
module.exports = { calculatePlanProgress, updatePlanProgressInDB, calculateProgress, updateProgress}; module.exports = { calculatePlanProgress, updatePlanProgressInDB, calculateTotalAssetValue, calculateProgress, updateProgress};
+1 -1
View File
@@ -19,7 +19,7 @@
<h1 class="text-lg font-medium">Total Value:</h1> <h1 class="text-lg font-medium">Total Value:</h1>
<h1 class="text-lg font-medium"> <h1 class="text-lg font-medium">
<!-- sum of assets array values and formated as proper dollar amount --> <!-- sum of assets array values and formated as proper dollar amount -->
<%- new Intl.NumberFormat('en-US', { style: 'currency', currency: geoData.currency ? geoData.currency : 'CAD' }).format(assets.reduce((total, e) => total + e.value, 0)) %> <%- new Intl.NumberFormat('en-US', { style: 'currency', currency: geoData.currency ? geoData.currency : 'CAD' }).format(totalUserAssetValue) %>
</h1> </h1>
</div> </div>
</div> </div>
+1 -1
View File
@@ -34,7 +34,7 @@
</div> </div>
<div> <div>
<label class="block text-sm font-medium text-gray-400">Total Value:</label> <label class="block text-sm font-medium text-gray-400">Total Value:</label>
<p class="mt-1 text-md text-white"><%- new Intl.NumberFormat('en-US', { style: 'currency', currency: geoData.currency ? geoData.currency : 'CAD' }).format(assets.reduce((total, asset) => total + asset.value, 0)) %></p> <p class="mt-1 text-md text-white"><%- new Intl.NumberFormat('en-US', { style: 'currency', currency: geoData.currency ? geoData.currency : 'CAD' }).format(totalUserAssetValue) %></p>
</div> </div>
</div> </div>
</div> </div>