-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2492a47
commit 9a73e08
Showing
15 changed files
with
693 additions
and
6 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,21 @@ | ||
<?php | ||
|
||
namespace App\Domain\Metrics; | ||
|
||
abstract class RelationTrendMetric extends TrendMetric | ||
{ | ||
protected $relationGraphqlQuery; | ||
protected $relationDisplayUsing; | ||
protected $relationForeignKey; | ||
|
||
public function jsonSerialize() | ||
{ | ||
return array_merge(parent::jsonSerialize(), [ | ||
'relation' => [ | ||
'graphql_query' => $this->relationGraphqlQuery, | ||
'display_using' => $this->relationDisplayUsing, | ||
'foreign_key' => $this->relationForeignKey, | ||
] | ||
]); | ||
} | ||
} |
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,8 @@ | ||
<?php | ||
|
||
namespace App\Domain\Metrics; | ||
|
||
abstract class TrendMetric extends Metric | ||
{ | ||
protected $component = 'trend-metric'; | ||
} |
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,16 @@ | ||
<?php | ||
|
||
namespace App\Domain\Ranges; | ||
|
||
class LastTwelveMonths extends Range | ||
{ | ||
public function start() | ||
{ | ||
return now()->subMonths(12)->format("Y-m-d"); | ||
} | ||
|
||
public function end() | ||
{ | ||
return now()->format("Y-m-d"); | ||
} | ||
} |
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,42 @@ | ||
<?php | ||
|
||
namespace App\GraphQL\Queries; | ||
|
||
use App\Models\Transaction; | ||
use Illuminate\Support\Facades\DB; | ||
use App\Domain\Metrics\TrendMetric; | ||
use App\Domain\Ranges\LastTwelveMonths; | ||
use App\Domain\Ranges\CurrentYear; | ||
use App\Domain\Ranges\LastYear; | ||
|
||
class TotalExpensesTrend extends TrendMetric | ||
{ | ||
public function ranges() | ||
{ | ||
return [ | ||
new LastTwelveMonths, | ||
new CurrentYear, | ||
new LastYear, | ||
]; | ||
} | ||
|
||
/** | ||
* @param null $_ | ||
* @param array<string, mixed> $args | ||
*/ | ||
public function __invoke($_, array $args) | ||
{ | ||
$rangeData = app('findRangeByKey', ["key" => $args['range']]); | ||
|
||
$query = Transaction::query() | ||
->expenses() | ||
->select(DB::raw("date_format(created_at, '%Y-%M') as label, SUM(transactions.amount) as value")) | ||
->groupBy(DB::raw("label")); | ||
|
||
if($rangeData) { | ||
$query->whereBetween('transactions.created_at', [$rangeData->start(), $rangeData->end()]); | ||
} | ||
|
||
return $query->get(); | ||
} | ||
} |
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,42 @@ | ||
<?php | ||
|
||
namespace App\GraphQL\Queries; | ||
|
||
use App\Models\Transaction; | ||
use Illuminate\Support\Facades\DB; | ||
use App\Domain\Metrics\TrendMetric; | ||
use App\Domain\Ranges\LastTwelveMonths; | ||
use App\Domain\Ranges\CurrentYear; | ||
use App\Domain\Ranges\LastYear; | ||
|
||
class TotalIncomeTrend extends TrendMetric | ||
{ | ||
public function ranges() | ||
{ | ||
return [ | ||
new LastTwelveMonths, | ||
new CurrentYear, | ||
new LastYear, | ||
]; | ||
} | ||
|
||
/** | ||
* @param null $_ | ||
* @param array<string, mixed> $args | ||
*/ | ||
public function __invoke($_, array $args) | ||
{ | ||
$rangeData = app('findRangeByKey', ["key" => $args['range']]); | ||
|
||
$query = Transaction::query() | ||
->income() | ||
->select(DB::raw("date_format(created_at, '%Y-%M') as label, SUM(transactions.amount) as value")) | ||
->groupBy(DB::raw("label")); | ||
|
||
if($rangeData) { | ||
$query->whereBetween('transactions.created_at', [$rangeData->start(), $rangeData->end()]); | ||
} | ||
|
||
return $query->get(); | ||
} | ||
} |
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,49 @@ | ||
<?php | ||
|
||
namespace App\GraphQL\Queries; | ||
|
||
use App\Models\Transaction; | ||
use Illuminate\Support\Facades\DB; | ||
use App\Domain\Metrics\RelationTrendMetric; | ||
use App\Domain\Ranges\LastTwelveMonths; | ||
use App\Domain\Ranges\CurrentYear; | ||
use App\Domain\Ranges\LastYear; | ||
|
||
class TotalPerBrandTrend extends RelationTrendMetric | ||
{ | ||
protected $relationGraphqlQuery = 'allBrands'; | ||
protected $relationDisplayUsing = 'name'; | ||
protected $relationForeignKey = 'id'; | ||
|
||
public function ranges() | ||
{ | ||
return [ | ||
new LastTwelveMonths, | ||
new CurrentYear, | ||
new LastYear, | ||
]; | ||
} | ||
|
||
/** | ||
* @param null $_ | ||
* @param array<string, mixed> $args | ||
*/ | ||
public function __invoke($_, array $args) | ||
{ | ||
$rangeData = app('findRangeByKey', ["key" => $args['range']]); | ||
$brandId = $args['id']; | ||
|
||
$query = Transaction::query() | ||
->whereHas('brand', function ($query) use($brandId) { | ||
return $query->where('id', $brandId); | ||
}) | ||
->select(DB::raw("date_format(created_at, '%Y-%M') as label, SUM(transactions.amount) as value")) | ||
->groupBy(DB::raw("label")); | ||
|
||
if($rangeData) { | ||
$query->whereBetween('transactions.created_at', [$rangeData->start(), $rangeData->end()]); | ||
} | ||
|
||
return $query->get(); | ||
} | ||
} |
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,49 @@ | ||
<?php | ||
|
||
namespace App\GraphQL\Queries; | ||
|
||
use App\Models\Transaction; | ||
use Illuminate\Support\Facades\DB; | ||
use App\Domain\Metrics\RelationTrendMetric; | ||
use App\Domain\Ranges\LastTwelveMonths; | ||
use App\Domain\Ranges\CurrentYear; | ||
use App\Domain\Ranges\LastYear; | ||
|
||
class TotalPerCategoryTrend extends RelationTrendMetric | ||
{ | ||
protected $relationGraphqlQuery = 'allCategories'; | ||
protected $relationDisplayUsing = 'name'; | ||
protected $relationForeignKey = 'id'; | ||
|
||
public function ranges() | ||
{ | ||
return [ | ||
new LastTwelveMonths, | ||
new CurrentYear, | ||
new LastYear, | ||
]; | ||
} | ||
|
||
/** | ||
* @param null $_ | ||
* @param array<string, mixed> $args | ||
*/ | ||
public function __invoke($_, array $args) | ||
{ | ||
$rangeData = app('findRangeByKey', ["key" => $args['range']]); | ||
$categoryId = $args['id']; | ||
|
||
$query = Transaction::query() | ||
->whereHas('brand.category', function ($query) use($categoryId) { | ||
return $query->where('id', $categoryId); | ||
}) | ||
->select(DB::raw("date_format(created_at, '%Y-%M') as label, SUM(transactions.amount) as value")) | ||
->groupBy(DB::raw("label")); | ||
|
||
if($rangeData) { | ||
$query->whereBetween('transactions.created_at', [$rangeData->start(), $rangeData->end()]); | ||
} | ||
|
||
return $query->get(); | ||
} | ||
} |
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
Oops, something went wrong.