Skip to content

Commit

Permalink
Dashboard page refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
simba77 committed Sep 2, 2024
1 parent 6d9d802 commit a2123a3
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 16 deletions.
24 changes: 12 additions & 12 deletions src/Controller/HomepageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,30 @@ public function __construct(
#[Route('/dashboard', name: 'app_homepage_index')]
public function index(#[CurrentUser] ?User $user): Response
{
$invested = (float) $this->entityManager->getRepository(Investment::class)->getSumByUserId($user->getId());
$allAssetsSum = 0;
$invested = $this->entityManager->getRepository(Investment::class)->getSumByUserId($user->getId());
$allAssetsSum = '0';
$depositsSum = $this->entityManager->getRepository(Deposit::class)->getSumOfDepositsForUser($user);
$depositAccounts = $this->depositsService->getDepositAccountsWithSummaryForUser($user);
$depositAccounts = array_filter($depositAccounts, function ($item) {
return $item->total > 0;
});

$dailyChange = 0;
$dailyChange = '0';
$allActiveDeals = $this->entityManager->getRepository(Deal::class)->findForUser($user);
foreach ($allActiveDeals as $deal) {
$dealData = new DealData($deal, $deal['deal']->getAccount(), $this->currencyService);
$dailyChange += $dealData->getFullDailyProfitInBaseCurrency();
$dailyChange = bcadd($dailyChange, $dealData->getFullDailyProfitInBaseCurrency(), 2);
}


$accounts = $this->accountService->getAccountsListForUser($user);
foreach ($accounts as $account) {
$allAssetsSum += $account->currentValue;
$allAssetsSum = bcadd($account->currentValue, $allAssetsSum, 2);
}

$profit = $allAssetsSum - $invested;
$profit = bcsub($allAssetsSum, $invested, 2);
if ($invested > 0) {
$profitPercent = round($profit / $invested * 100, 2);
$profitPercent = bcmul(bcdiv($profit, $invested, 5), '100', 2);
}

return $this->json(
Expand All @@ -79,31 +79,31 @@ public function index(#[CurrentUser] ?User $user): Response
[
'name' => 'Deposits + Investments',
'helpText' => 'Deposits + Investments',
'total' => $invested + $depositsSum,
'total' => bcadd($invested, $depositsSum, 2),
'currency' => '',
],

[
'name' => 'All Assets',
'helpText' => 'The sum of all assets held by brokers',
'dailyChange' => round($dailyChange, 2),
'percent' => round($dailyChange / $allAssetsSum * 100, 2),
'dailyChange' => $dailyChange,
'percent' => bcmul(bcdiv($dailyChange, $allAssetsSum, 5), '100', 2),
'total' => $allAssetsSum,
'currency' => '',
],

[
'name' => 'Profit',
'helpText' => 'Assets for The Current Day - The Invested Amount',
'percent' => $profitPercent ?? 0,
'percent' => $profitPercent ?? '0',
'total' => $profit,
'currency' => '',
],

[
'name' => 'Saving + All Brokers Assets',
'helpText' => 'Assets for The Current Day + Deposits',
'total' => $allAssetsSum + $depositsSum,
'total' => bcadd($allAssetsSum, $depositsSum, 2),
'currency' => '',
],
],
Expand Down
4 changes: 2 additions & 2 deletions src/Repository/DepositRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __construct(ManagerRegistry $registry)
parent::__construct($registry, Deposit::class);
}

public function getSumOfDepositsForUser(User $user)
public function getSumOfDepositsForUser(User $user): string
{
$data = $this->createQueryBuilder('d')
->andWhere('d.user = :user')
Expand All @@ -31,6 +31,6 @@ public function getSumOfDepositsForUser(User $user)
->getQuery()
->getOneOrNullResult();

return $data['sum_of_deposits'];
return (string) $data['sum_of_deposits'];
}
}
4 changes: 2 additions & 2 deletions src/Repository/InvestmentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ public function getByUserId(int $userId): array
->getResult();
}

public function getSumByUserId(int $userId)
public function getSumByUserId(int $userId): string
{
return $this->createQueryBuilder('inv')
return (string) $this->createQueryBuilder('inv')
->select('SUM(inv.sum) as allInvestments')
->where('inv.userId = :user_id')
->setParameter('user_id', $userId)
Expand Down
6 changes: 6 additions & 0 deletions src/Services/AccountService.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public function __construct(
) {
}

/**
* @return array<AccountResponseDTO>
*/
public function getSimpleListOfAccountsForUser(?User $user): array
{
$accounts = $this->accountRepository->findBy(['userId' => $user->getId()]);
Expand All @@ -29,6 +32,9 @@ public function getSimpleListOfAccountsForUser(?User $user): array
return $result;
}

/**
* @return array<AccountListItemResponseDTO>
*/
public function getAccountsListForUser(?User $user): array
{
$items = $this->accountRepository->findByUserIdWithDeposits($user->getId() ?? 0);
Expand Down
3 changes: 3 additions & 0 deletions src/Services/DepositsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public function getDepositAccountsForUser(User $user): array
return $result;
}

/**
* @return array<DepositAccountSummaryListItemDTO>
*/
public function getDepositAccountsWithSummaryForUser(User $user): array
{
$accounts = $this->entityManager->getRepository(DepositAccount::class)->getDepositAccountsWithSummary($user);
Expand Down

0 comments on commit a2123a3

Please sign in to comment.