From f4b916d380b6dfb362935ff8aca728f162152053 Mon Sep 17 00:00:00 2001 From: Saleem Hadad Date: Fri, 14 Jan 2022 23:57:20 +0400 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20ranges?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Domain/Ranges/CurrentMonth.php | 16 ++++++ app/Domain/Ranges/CurrentYear.php | 16 ++++++ app/Domain/Ranges/LastMonth.php | 16 ++++++ app/Domain/Ranges/LastYear.php | 16 ++++++ app/Domain/Ranges/Range.php | 48 ++++++++++++++++ app/Http/Controllers/DashboardController.php | 1 + config/finance.php | 22 ++++++-- graphql/schema.graphql | 2 +- tests/Unit/CurrentMonthTest.php | 26 +++++++++ tests/Unit/CurrentYearTest.php | 26 +++++++++ tests/Unit/LastMonthTest.php | 26 +++++++++ tests/Unit/LastYearTest.php | 26 +++++++++ tests/Unit/RangeTest.php | 58 ++++++++++++++++++++ 13 files changed, 292 insertions(+), 7 deletions(-) create mode 100644 app/Domain/Ranges/CurrentMonth.php create mode 100644 app/Domain/Ranges/CurrentYear.php create mode 100644 app/Domain/Ranges/LastMonth.php create mode 100644 app/Domain/Ranges/LastYear.php create mode 100644 app/Domain/Ranges/Range.php create mode 100644 tests/Unit/CurrentMonthTest.php create mode 100644 tests/Unit/CurrentYearTest.php create mode 100644 tests/Unit/LastMonthTest.php create mode 100644 tests/Unit/LastYearTest.php create mode 100644 tests/Unit/RangeTest.php diff --git a/app/Domain/Ranges/CurrentMonth.php b/app/Domain/Ranges/CurrentMonth.php new file mode 100644 index 0000000..8bc79f5 --- /dev/null +++ b/app/Domain/Ranges/CurrentMonth.php @@ -0,0 +1,16 @@ +startOfMonth()->format("Y-m-d"); + } + + public function end() + { + return now()->endOfMonth()->format("Y-m-d"); + } +} \ No newline at end of file diff --git a/app/Domain/Ranges/CurrentYear.php b/app/Domain/Ranges/CurrentYear.php new file mode 100644 index 0000000..1158a4a --- /dev/null +++ b/app/Domain/Ranges/CurrentYear.php @@ -0,0 +1,16 @@ +startOfYear()->format("Y-m-d"); + } + + public function end() + { + return now()->endOfYear()->format("Y-m-d"); + } +} \ No newline at end of file diff --git a/app/Domain/Ranges/LastMonth.php b/app/Domain/Ranges/LastMonth.php new file mode 100644 index 0000000..173f719 --- /dev/null +++ b/app/Domain/Ranges/LastMonth.php @@ -0,0 +1,16 @@ +subMonth()->startOfMonth()->format("Y-m-d"); + } + + public function end() + { + return now()->subMonth()->endOfMonth()->format("Y-m-d"); + } +} \ No newline at end of file diff --git a/app/Domain/Ranges/LastYear.php b/app/Domain/Ranges/LastYear.php new file mode 100644 index 0000000..94e3960 --- /dev/null +++ b/app/Domain/Ranges/LastYear.php @@ -0,0 +1,16 @@ +subYear()->startOfYear()->format("Y-m-d"); + } + + public function end() + { + return now()->subYear()->endOfYear()->format("Y-m-d"); + } +} \ No newline at end of file diff --git a/app/Domain/Ranges/Range.php b/app/Domain/Ranges/Range.php new file mode 100644 index 0000000..185cbfc --- /dev/null +++ b/app/Domain/Ranges/Range.php @@ -0,0 +1,48 @@ +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(), + ]; + } +} \ No newline at end of file diff --git a/app/Http/Controllers/DashboardController.php b/app/Http/Controllers/DashboardController.php index 44a71dc..439e58b 100644 --- a/app/Http/Controllers/DashboardController.php +++ b/app/Http/Controllers/DashboardController.php @@ -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); diff --git a/config/finance.php b/config/finance.php index e8efc32..c78bc84 100644 --- a/config/finance.php +++ b/config/finance.php @@ -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 ] ]; \ No newline at end of file diff --git a/graphql/schema.graphql b/graphql/schema.graphql index 9bb521c..c0365fc 100644 --- a/graphql/schema.graphql +++ b/graphql/schema.graphql @@ -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 { diff --git a/tests/Unit/CurrentMonthTest.php b/tests/Unit/CurrentMonthTest.php new file mode 100644 index 0000000..330fba6 --- /dev/null +++ b/tests/Unit/CurrentMonthTest.php @@ -0,0 +1,26 @@ +assertEquals([ + 'key' => 'current-month', + 'name' => 'Current Month', + 'start' => '2021-01-01', + 'end' => '2021-01-31', + ], $sut->jsonSerialize()); + } +} diff --git a/tests/Unit/CurrentYearTest.php b/tests/Unit/CurrentYearTest.php new file mode 100644 index 0000000..5c4f566 --- /dev/null +++ b/tests/Unit/CurrentYearTest.php @@ -0,0 +1,26 @@ +assertEquals([ + 'key' => 'current-year', + 'name' => 'Current Year', + 'start' => '2021-01-01', + 'end' => '2021-12-31', + ], $sut->jsonSerialize()); + } +} diff --git a/tests/Unit/LastMonthTest.php b/tests/Unit/LastMonthTest.php new file mode 100644 index 0000000..9868761 --- /dev/null +++ b/tests/Unit/LastMonthTest.php @@ -0,0 +1,26 @@ +assertEquals([ + 'key' => 'last-month', + 'name' => 'Last Month', + 'start' => '2020-12-01', + 'end' => '2020-12-31', + ], $sut->jsonSerialize()); + } +} diff --git a/tests/Unit/LastYearTest.php b/tests/Unit/LastYearTest.php new file mode 100644 index 0000000..77e251d --- /dev/null +++ b/tests/Unit/LastYearTest.php @@ -0,0 +1,26 @@ +assertEquals([ + 'key' => 'last-year', + 'name' => 'Last Year', + 'start' => '2020-01-01', + 'end' => '2020-12-31', + ], $sut->jsonSerialize()); + } +} diff --git a/tests/Unit/RangeTest.php b/tests/Unit/RangeTest.php new file mode 100644 index 0000000..4d14fcc --- /dev/null +++ b/tests/Unit/RangeTest.php @@ -0,0 +1,58 @@ +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"; + } +} \ No newline at end of file