Skip to content

Commit

Permalink
✨ Add ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
saleem-hadad committed Jan 14, 2022
1 parent 2cfaf27 commit f4b916d
Show file tree
Hide file tree
Showing 13 changed files with 292 additions and 7 deletions.
16 changes: 16 additions & 0 deletions app/Domain/Ranges/CurrentMonth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Domain\Ranges;

class CurrentMonth extends Range
{
public function start()
{
return now()->startOfMonth()->format("Y-m-d");
}

public function end()
{
return now()->endOfMonth()->format("Y-m-d");
}
}
16 changes: 16 additions & 0 deletions app/Domain/Ranges/CurrentYear.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Domain\Ranges;

class CurrentYear extends Range
{
public function start()
{
return now()->startOfYear()->format("Y-m-d");
}

public function end()
{
return now()->endOfYear()->format("Y-m-d");
}
}
16 changes: 16 additions & 0 deletions app/Domain/Ranges/LastMonth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Domain\Ranges;

class LastMonth extends Range
{
public function start()
{
return now()->subMonth()->startOfMonth()->format("Y-m-d");
}

public function end()
{
return now()->subMonth()->endOfMonth()->format("Y-m-d");
}
}
16 changes: 16 additions & 0 deletions app/Domain/Ranges/LastYear.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Domain\Ranges;

class LastYear extends Range
{
public function start()
{
return now()->subYear()->startOfYear()->format("Y-m-d");
}

public function end()
{
return now()->subYear()->endOfYear()->format("Y-m-d");
}
}
48 changes: 48 additions & 0 deletions app/Domain/Ranges/Range.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace App\Domain\Ranges;

use JsonSerializable;
use Illuminate\Support\Str;

abstract class Range implements JsonSerializable
{
protected $key;
protected $name;
protected $start;
protected $end;

public function humanizedName()
{
return Str::title(Str::snake(class_basename(get_class($this)), ' '));
}

public function key()
{
return Str::slug($this->name(), '-', null);
}

public function name()
{
return $this->name ?: $this->humanizedName();
}

abstract public function start();

abstract public function end();

/**
* Prepare the element for JSON serialization.
*
* @return array
*/
public function jsonSerialize()
{
return [
'key' => $this->key(),
'name' => $this->name(),
'start' => $this->start(),
'end' => $this->end(),
];
}
}
1 change: 1 addition & 0 deletions app/Http/Controllers/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class DashboardController extends Controller
public function index()
{
$metrics = config('finance.reports');

$graphqlQueries = array_map(function($metric) {
return $metric['graphql_query'];
}, $metrics);
Expand Down
22 changes: 16 additions & 6 deletions config/finance.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,25 @@
'component' => 'value-metric',
'width' => '1/2',
],
[
'name' => 'Expenses per Category',
'graphql_query' => 'expensesPerCategory',
'component' => 'partition-metric',
'width' => '1/2',
'ranges' => [
\App\Domain\Ranges\CurrentMonth::class,
\App\Domain\Ranges\LastMonth::class,
\App\Domain\Ranges\CurrentYear::class,
\App\Domain\Ranges\LastYear::class,
],
],

// Brand per category partition for this month
// Brand per category partition for last month

// Total Income graph for the last 12 months
// Total Expenses graph for the last 12 months
// Graph for expenses per category for the last 12 months
// Brand graph for the last 12 months
// Expenses per category this month
// Expenses per category last month
// Expenses per category this year
// Expenses per category last year
// Brand per category partition for this month
// Brand per category partition for last month
]
];
2 changes: 1 addition & 1 deletion graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type Query {

totalExpenses: Float! @aggregate(model: Transaction column: amount function: SUM scopes:["expenses"])
totalIncome: Float! @aggregate(model: Transaction column: amount function: SUM scopes: ["income"])
expensesPerCategory: [TransactionStatistics!]!
expensesPerCategory: Float! @aggregate(model: Transaction column: amount function: SUM scopes: ["income"])
}

type Mutation {
Expand Down
26 changes: 26 additions & 0 deletions tests/Unit/CurrentMonthTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Tests\Unit;

use Carbon\Carbon;
use Tests\TestCase;
use App\Domain\Ranges\CurrentMonth;

class CurrentMonthTest extends TestCase
{
/** @test */
public function it_has_corrent_json_serializeable()
{
// mock app date
Carbon::setTestNow(Carbon::create(2021, 1, 18));

$sut = new CurrentMonth;

$this->assertEquals([
'key' => 'current-month',
'name' => 'Current Month',
'start' => '2021-01-01',
'end' => '2021-01-31',
], $sut->jsonSerialize());
}
}
26 changes: 26 additions & 0 deletions tests/Unit/CurrentYearTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Tests\Unit;

use Carbon\Carbon;
use Tests\TestCase;
use App\Domain\Ranges\CurrentYear;

class CurrentYearTest extends TestCase
{
/** @test */
public function it_has_corrent_json_serializeable()
{
// mock app date
Carbon::setTestNow(Carbon::create(2021, 1, 18));

$sut = new CurrentYear;

$this->assertEquals([
'key' => 'current-year',
'name' => 'Current Year',
'start' => '2021-01-01',
'end' => '2021-12-31',
], $sut->jsonSerialize());
}
}
26 changes: 26 additions & 0 deletions tests/Unit/LastMonthTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Tests\Unit;

use Carbon\Carbon;
use Tests\TestCase;
use App\Domain\Ranges\LastMonth;

class LastMonthTest extends TestCase
{
/** @test */
public function it_has_corrent_json_serializeable()
{
// mock app date
Carbon::setTestNow(Carbon::create(2021, 1, 18));

$sut = new LastMonth;

$this->assertEquals([
'key' => 'last-month',
'name' => 'Last Month',
'start' => '2020-12-01',
'end' => '2020-12-31',
], $sut->jsonSerialize());
}
}
26 changes: 26 additions & 0 deletions tests/Unit/LastYearTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Tests\Unit;

use Carbon\Carbon;
use Tests\TestCase;
use App\Domain\Ranges\LastYear;

class LastYearTest extends TestCase
{
/** @test */
public function it_has_corrent_json_serializeable()
{
// mock app date
Carbon::setTestNow(Carbon::create(2021, 5, 18));

$sut = new LastYear;

$this->assertEquals([
'key' => 'last-year',
'name' => 'Last Year',
'start' => '2020-01-01',
'end' => '2020-12-31',
], $sut->jsonSerialize());
}
}
58 changes: 58 additions & 0 deletions tests/Unit/RangeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Tests\Unit;

use Tests\TestCase;
use App\Domain\Ranges\Range;

class RangeTest extends TestCase
{
/** @test */
public function it_is_abstract_class()
{
$this->expectException(\Error::class);

new Range;
}

/** @test */
public function it_has_default_name()
{
$sut = new FakeRange;

$this->assertEquals('Fake Range', $sut->name());
}

/** @test */
public function it_has_default_key()
{
$sut = new FakeRange;

$this->assertEquals('fake-range', $sut->key());
}

/** @test */
public function it_is_json_serializeable()
{
$sut = new FakeRange;

$this->assertEquals([
'key' => 'fake-range',
'name' => 'Fake Range',
'start' => 'startTime',
'end' => 'endTime'
], $sut->jsonSerialize());
}
}


class FakeRange extends Range
{
public function start() {
return "startTime";
}

public function end() {
return "endTime";
}
}

0 comments on commit f4b916d

Please sign in to comment.