Fixes for chart tests

This commit is contained in:
David Bomba 2022-01-21 12:00:32 +11:00
parent c60045da58
commit 38031ec7a3
5 changed files with 94 additions and 14 deletions

View File

@ -12,6 +12,7 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Http\Requests\Chart\ShowChartRequest; use App\Http\Requests\Chart\ShowChartRequest;
use App\Services\Chart\ChartService;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Http\Response; use Illuminate\Http\Response;
@ -25,8 +26,8 @@ class ChartController extends BaseController
/** /**
* @OA\Post( * @OA\Post(
* path="/api/v1/charts", * path="/api/v1/charts/totals",
* operationId="getCharts", * operationId="getChartTotals",
* tags={"charts"}, * tags={"charts"},
* summary="Get chart data", * summary="Get chart data",
* description="Get chart data", * description="Get chart data",
@ -67,10 +68,11 @@ class ChartController extends BaseController
* @param Request $request * @param Request $request
* @return Response|mixed * @return Response|mixed
*/ */
public function index(ShowChartRequest $request) public function totals(ShowChartRequest $request)
{ {
$cs = new ChartService(auth()->user()->company());
return response()->json([],200); return response()->json($cs->totals($request->input('start_date'), $request->input('end_date')),200);
} }
} }

View File

@ -29,6 +29,22 @@ class ShowChartRequest extends Request
public function rules() public function rules()
{ {
return [ return [
'start_date' => 'date',
'end_date' => 'date',
]; ];
} }
protected function prepareForValidation()
{
$input = $this->all();
if(!array_key_exists('start_date', $input))
$input['start_date'] = now()->subDays(20);
if(!array_key_exists('end_date', $input))
$input['end_date'] = now();
$this->replace($input);
}
} }

View File

@ -50,7 +50,7 @@ class ChartService
->where('company_id', $this->company->id) ->where('company_id', $this->company->id)
->where('is_deleted', 0) ->where('is_deleted', 0)
->distinct() ->distinct()
->get(['currency_id']); ->pluck('currency_id as id');
/* Merge and filter by unique */ /* Merge and filter by unique */
$currencies = $currencies->merge($expense_currencies)->unique(); $currencies = $currencies->merge($expense_currencies)->unique();
@ -72,10 +72,11 @@ class ChartService
public function totals($start_date, $end_date) public function totals($start_date, $end_date) :array
{ {
$data = []; $data = [];
$data['currencies'] = $this->getCurrencyCodes();
$data['revenue'] = $this->getRevenue($start_date, $end_date); $data['revenue'] = $this->getRevenue($start_date, $end_date);
$data['outstanding'] = $this->getOutstanding($start_date, $end_date); $data['outstanding'] = $this->getOutstanding($start_date, $end_date);
$data['expenses'] = $this->getExpenses($start_date, $end_date); $data['expenses'] = $this->getExpenses($start_date, $end_date);
@ -110,7 +111,7 @@ class ChartService
} }
private function getRevenue($start_date, $end_date) private function getRevenue($start_date, $end_date) :array
{ {
$revenue = $this->getRevenueQuery($start_date, $end_date); $revenue = $this->getRevenueQuery($start_date, $end_date);
$revenue = $this->parseTotals($revenue); $revenue = $this->parseTotals($revenue);
@ -119,7 +120,7 @@ class ChartService
return $revenue; return $revenue;
} }
private function getOutstanding($start_date, $end_date) private function getOutstanding($start_date, $end_date) :array
{ {
$outstanding = $this->getOutstandingQuery($start_date, $end_date); $outstanding = $this->getOutstandingQuery($start_date, $end_date);
$outstanding = $this->parseTotals($outstanding); $outstanding = $this->parseTotals($outstanding);
@ -128,16 +129,15 @@ class ChartService
return $outstanding; return $outstanding;
} }
private function getExpenses($start_date, $end_date) private function getExpenses($start_date, $end_date) :array
{ {
$expenses = $this->getExpenseQuery($start_date, $end_date); $expenses = $this->getExpenseQuery($start_date, $end_date);
$expenses = $this->parseTotals($expenses); $expenses = $this->parseTotals($expenses);
$expenses = $this->addCountryCodes($expenses); $expenses = $this->addCountryCodes($expenses);
return $expenses; return $expenses;
} }
private function parseTotals($data_set) private function parseTotals($data_set) :array
{ {
/* Find the key where the company currency amount lives*/ /* Find the key where the company currency amount lives*/
$c_key = array_search($this->company->id , array_column($data_set, 'currency_id')); $c_key = array_search($this->company->id , array_column($data_set, 'currency_id'));
@ -160,22 +160,25 @@ class ChartService
} }
private function addCountryCodes($data_set) private function addCountryCodes($data_set) :array
{ {
$currencies = Cache::get('currencies'); $currencies = Cache::get('currencies');
foreach($data_set as $key => $value) foreach($data_set as $key => $value)
{ {
$data_set[$key]['code'] = $this->getCode($currencies, $value); $data_set[$key]->currency_id = str_replace('"', '', $value->currency_id);
$data_set[$key]->code = $this->getCode($currencies, $data_set[$key]->currency_id);
} }
return $data_set; return $data_set;
} }
private function getCode($currencies, $currency_id) private function getCode($currencies, $currency_id) :string
{ {
nlog($currency_id);
$currency = $currencies->filter(function ($item) use($currency_id) { $currency = $currencies->filter(function ($item) use($currency_id) {
return $item->id == $currency_id; return $item->id == $currency_id;
})->first(); })->first();

View File

@ -31,6 +31,9 @@ Route::group(['middleware' => ['api_db', 'token_auth', 'locale'], 'prefix' => 'a
Route::get('activities', 'ActivityController@index'); Route::get('activities', 'ActivityController@index');
Route::get('activities/download_entity/{activity}', 'ActivityController@downloadHistoricalEntity'); Route::get('activities/download_entity/{activity}', 'ActivityController@downloadHistoricalEntity');
Route::post('charts/totals', 'ChartController@totals')->name('chart.totals');
Route::post('claim_license', 'LicenseController@index')->name('license.index'); Route::post('claim_license', 'LicenseController@index')->name('license.index');
Route::resource('clients', 'ClientController'); // name = (clients. index / create / show / update / destroy / edit Route::resource('clients', 'ClientController'); // name = (clients. index / create / show / update / destroy / edit

View File

@ -12,8 +12,10 @@ namespace Tests\Unit\Chart;
use App\DataMapper\ClientSettings; use App\DataMapper\ClientSettings;
use App\Models\Client; use App\Models\Client;
use App\Models\Invoice;
use App\Services\Chart\ChartService; use App\Services\Chart\ChartService;
use App\Utils\Ninja; use App\Utils\Ninja;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\MockAccountData; use Tests\MockAccountData;
use Tests\TestCase; use Tests\TestCase;
@ -24,6 +26,7 @@ use Tests\TestCase;
class ChartCurrencyTest extends TestCase class ChartCurrencyTest extends TestCase
{ {
use MockAccountData; use MockAccountData;
use DatabaseTransactions;
public function setUp() :void public function setUp() :void
{ {
@ -32,6 +35,42 @@ class ChartCurrencyTest extends TestCase
$this->makeTestData(); $this->makeTestData();
} }
public function testRevenueValues()
{
Invoice::factory()->create([
'client_id' => $this->client->id,
'user_id' => $this->user->id,
'company_id' => $this->company->id,
'paid_to_date' => 100,
'status_id' => 4,
'date' => now(),
'due_date'=> now(),
'number' => 'db_record'
]);
$this->assertDatabaseHas('invoices', ['number' => 'db_record']);
$cs = new ChartService($this->company);
nlog($cs->getRevenueQuery(now()->subDays(20)->format('Y-m-d'), now()->addDays(100)->format('Y-m-d')));
$data = [
'start_date' => now()->subDays(30)->format('Y-m-d'),
'end_date' => now()->addDay()->format('Y-m-d')
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/charts/totals', $data);
$response->assertStatus(200);
nlog($response->json());
}
public function testgetCurrencyCodes() public function testgetCurrencyCodes()
{ {
$settings = ClientSettings::defaults(); $settings = ClientSettings::defaults();
@ -61,6 +100,23 @@ class ChartCurrencyTest extends TestCase
$this->assertFalse(in_array("AUD", $cs->getCurrencyCodes())); $this->assertFalse(in_array("AUD", $cs->getCurrencyCodes()));
} }
public function testGetChartTotalsApi()
{
$data = [
'start_date' => now()->subDays(30)->format('Y-m-d'),
'end_date' => now()->format('Y-m-d')
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/charts/totals', $data);
$response->assertStatus(200);
}
public function testClientServiceDataSetBuild() public function testClientServiceDataSetBuild()
{ {