Skip to content

Commit

Permalink
Merge pull request #43 from saleem-hadad/feat/net-worth-widget
Browse files Browse the repository at this point in the history
Add NetWorth Metric
  • Loading branch information
saleem-hadad authored Mar 22, 2023
2 parents 935c468 + 6d8cf96 commit d5712e4
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 3 deletions.
28 changes: 28 additions & 0 deletions app/GraphQL/Queries/NetWorth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\GraphQL\Queries;

use App\Models\Transaction;
use App\Domain\Metrics\ValueMetric;

class NetWorth extends ValueMetric
{
public function ranges()
{
return null;
}

/**
* @param null $_
* @param array<string, mixed> $args
*/
public function __invoke($_, array $args)
{
$income = Transaction::income()->sum('amount');
$expenses = Transaction::expenses()->sum('amount');

return [
'value' => $income - $expenses
];
}
}
8 changes: 5 additions & 3 deletions config/finance.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use App\Domain\SectionDivider;
use App\GraphQL\Queries\NetWorth;
use App\GraphQL\Queries\TotalCash;
use App\GraphQL\Queries\TotalIncome;
use App\GraphQL\Queries\TotalSavings;
Expand Down Expand Up @@ -37,9 +38,10 @@
],
'reports' => [
(new SectionDivider)->withTitle("🔎 Overview"),
(new TotalCash)->setWidth('1/3'),
(new TotalSavings)->setWidth('1/3'),
(new TotalInvestment)->setWidth('1/3'),
(new NetWorth)->setWidth('1/4'),
(new TotalCash)->setWidth('1/4'),
(new TotalSavings)->setWidth('1/4'),
(new TotalInvestment)->setWidth('1/4'),
(new SectionDivider)->withTitle("📊 Analytics"),
new TotalIncome,
new TotalExpenses,
Expand Down
1 change: 1 addition & 0 deletions graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type Query {
totalSavings: Json
totalInvestment: Json
totalCash: Json
netWorth: Json

expensesPerCategory(range: String!): Json
incomePerCategory(range: String!): Json
Expand Down
39 changes: 39 additions & 0 deletions tests/Feature/GraphQL/Queries/NetWorthTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Tests\Feature\GraphQL\Queries;

use Tests\TestCase;
use App\Models\Brand;
use App\Models\Category;
use App\Models\Transaction;
use Illuminate\Foundation\Testing\RefreshDatabase;

class NetWorthTest extends TestCase
{
use RefreshDatabase;

/** @test */
public function it_returns_correct_data()
{
$incomeCategory = Category::factory()->create(['type' => Category::INCOME]);
$expensesCategory = Category::factory()->create(['type' => Category::EXPENSES]);

$incomeBrand = Brand::factory()->create(['category_id' => $incomeCategory->id]);
$expensesBrand = Brand::factory()->create(['category_id' => $expensesCategory->id]);

// Income
Transaction::factory()->create(['brand_id' => $incomeBrand->id, 'amount' => 1000]);
// Expenses
Transaction::factory()->create(['brand_id' => $expensesBrand, 'amount' => 200]);

$this->graphQL(/** @lang GraphQL */ '
{
netWorth
}
')->assertJson([
'data' => [
'netWorth' => '{"value":800}'
],
]);
}
}
File renamed without changes.

0 comments on commit d5712e4

Please sign in to comment.