Skip to content

Commit

Permalink
Merge pull request #50 from afaneca/develop
Browse files Browse the repository at this point in the history
2.1.2
  • Loading branch information
afaneca committed Mar 21, 2024
2 parents 1799f13 + c162f62 commit 576c8e4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 30 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"db:seed": "prisma db seed"
},
"name": "myfin-api",
"version": "2.1.1",
"version": "2.1.2",
"description": "NodeJS API for Myfin",
"main": "src/server.js",
"devDependencies": {
Expand Down
29 changes: 16 additions & 13 deletions src/services/accountService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,23 +453,26 @@ const accountService = {
},
});

let balance = 0;
for (const account of accounts) {
let balanceSnapshotAtMonth = 0;
let balancePromises = []
for(const account of accounts){
if (includeInvestmentAccounts || account.type !== MYFIN.ACCOUNT_TYPES.INVESTING) {
const snapshotAtMonth = (await accountService.getBalanceSnapshotAtMonth(
account.account_id,
month,
year,
prismaTx
)) ?? {balance: 0};
balanceSnapshotAtMonth = parseFloat(String(snapshotAtMonth.balance || 0));
}
if (balanceSnapshotAtMonth) {
balance += balanceSnapshotAtMonth;
balancePromises.push(accountService.getBalanceSnapshotAtMonth(
account.account_id,
month,
year,
prismaTx
))
}
}

const balances = await Promise.all(balancePromises)
let balance = balances.reduce((result, current) => {
const balanceSnapshotAtMonth = parseFloat(String(current.balance || 0));
if(balanceSnapshotAtMonth){
return result + balanceSnapshotAtMonth
} else return result
}, 0);

return balance;
}, dbClient),
getCountOfUserAccounts: async (userId, dbClient = prisma) =>
Expand Down
26 changes: 10 additions & 16 deletions src/services/budgetService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ const getFilteredBudgetsForUserByPage = async (
pageSize: number,
query: string,
status: string,
dbClient = undefined
dbClient = prisma
) =>
performDatabaseRequest(async (prismaTx) => {
const budgetsArr = await getBudgetsForUserByPage(
Expand All @@ -341,24 +341,18 @@ const getFilteredBudgetsForUserByPage = async (
prismaTx
);

for await (const budget of budgetsArr.results) {
budget.balance_value = await calculateBudgetBalance(userId, budget, prismaTx);
budget.balance_change_percentage = await calculateBudgetBalanceChangePercentage(
userId,
budget,
budget.balance_value,
prismaTx
);
const balancePromises = budgetsArr.results.map(async budget => {
const balanceValue = await calculateBudgetBalance(userId, budget, prismaTx);
budget.balance_value = balanceValue;
const balanceChangePercentage = await calculateBudgetBalanceChangePercentage(userId, budget, balanceValue, prismaTx);
budget.balance_change_percentage = balanceChangePercentage;
const budgetSums = await getSumAmountsForBudget(userId, budget, prismaTx);
budget.credit_amount = budgetSums.balance_credit;
budget.debit_amount = budgetSums.balance_debit;
if (parseFloat(budget.credit_amount) === 0) {
budget.savings_rate_percentage = 0;
} else {
budget.savings_rate_percentage =
(parseFloat(budget.balance_value) / parseFloat(budget.credit_amount)) * 100;
}
}
budget.savings_rate_percentage = parseFloat(budget.credit_amount) === 0 ? 0 : (parseFloat(budget.balance_value) / parseFloat(budget.credit_amount)) * 100;
});

await Promise.all(balancePromises);
return budgetsArr;
}, dbClient);

Expand Down

0 comments on commit 576c8e4

Please sign in to comment.