-
-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from saleem-hadad/feat/net-worth-widget
Add NetWorth Metric
- Loading branch information
Showing
5 changed files
with
73 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.