feature/vehicle depreciation calculated towards total assets value

This commit is contained in:
nicoagostini committed 2025-05-21 16:39:19 -07:00
1 parent b12d4bebe1
commit 5b147d4f46
4 files changed
+37 -6

No files matched your search

+6 -1
View File
@@ -1,5 +1,5 @@
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 status = require("../util/statuses");
const ObjectId = require('mongodb').ObjectId;
@@ -110,11 +110,14 @@ module.exports = (middleware, users, plans, assets) => {
router.get('/assets', async (req, res) => {
let userAssets = await assets.find({ userId: new ObjectId(req.session.userId) }).toArray();
let totalUserAssetValue = await calculateTotalAssetValue(userAssets);
res.render('assets', {
user: req.session.user,
errMessage: req.session.errMessage,
assets: userAssets,
geoData: req.session.geoData,
totalUserAssetValue: totalUserAssetValue,
icons: icons,
});
return res.status(status.Ok);
@@ -142,6 +145,7 @@ module.exports = (middleware, users, plans, assets) => {
try {
const planId = req.params.id;
let userAssets = await assets.find({ userId: new ObjectId(req.session.userId) }).toArray();
let totalUserAssetValue = await calculateTotalAssetValue(userAssets);
if (!ObjectId.isValid(planId)) {
req.session.errMessage = "Invalid plan ID format.";
@@ -160,6 +164,7 @@ module.exports = (middleware, users, plans, assets) => {
user: req.session.user,
plan: plan,
geoData: req.session.geoData,
totalUserAssetValue: totalUserAssetValue,
assets: userAssets,
progress: progress,
suggestions: await suggestions.generateSuggestions(),
+29 -3
View File
@@ -12,7 +12,7 @@ async function calculatePlanProgress(plans, assets, userId) {
try {
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;
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) {
if (!plan || typeof plan !== 'object') {
@@ -63,9 +88,10 @@ async function calculateProgress(plan, assets, users, userId) {
try {
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 userDoc = await users.findOne({ _id: new ObjectId(userId) });
if (!userDoc || !userDoc.dob) {
@@ -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">
<!-- 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>
</div>
</div>
+1 -1
View File
@@ -34,7 +34,7 @@
</div>
<div>
<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>