Extend API

This commit is contained in:
James Cole
2023-08-09 14:14:33 +02:00
parent 03f39f53d8
commit a2984f299b
13 changed files with 95 additions and 34 deletions

View File

@@ -65,12 +65,16 @@ class AccountController extends Controller
/** /**
* Documentation for this endpoint: * Documentation for this endpoint:
* TODO endpoint is not documented. * TODO list of checks
* 1. use dates from ParameterBag
* 2. Request validates dates
* 3. Request includes user_group_id as administration_id
* 4. Endpoint is documented.
* 5. Collector uses administration_id
* *
* @param AutocompleteRequest $request * @param AutocompleteRequest $request
* *
* @return JsonResponse * @return JsonResponse
* @throws JsonException
* @throws FireflyException * @throws FireflyException
* @throws FireflyException * @throws FireflyException
*/ */
@@ -79,7 +83,7 @@ class AccountController extends Controller
$data = $request->getData(); $data = $request->getData();
$types = $data['types']; $types = $data['types'];
$query = $data['query']; $query = $data['query'];
$date = $data['date'] ?? today(config('app.timezone')); $date = $this->parameters->get('date') ?? today(config('app.timezone'));
$this->adminRepository->setAdministrationId($data['administration_id']); $this->adminRepository->setAdministrationId($data['administration_id']);
$return = []; $return = [];

View File

@@ -71,6 +71,8 @@ class AccountController extends Controller
* If a transaction has foreign currency = native currency, the foreign amount will be used, no conversion * If a transaction has foreign currency = native currency, the foreign amount will be used, no conversion
* will take place. * will take place.
* *
* TODO validate and set administration_id from request
*
* @param DateRequest $request * @param DateRequest $request
* *
* @return JsonResponse * @return JsonResponse
@@ -80,15 +82,11 @@ class AccountController extends Controller
*/ */
public function dashboard(DateRequest $request): JsonResponse public function dashboard(DateRequest $request): JsonResponse
{ {
// parameters for chart:
$dates = $request->getAll();
/** @var Carbon $start */ /** @var Carbon $start */
$start = $dates['start']; $start = $this->parameters->get('start');
/** @var Carbon $end */ /** @var Carbon $end */
$end = $dates['end']; $end = $this->parameters->get('end');
$end->endOfDay(); $end->endOfDay();
/** @var User $user */
$user = auth()->user();
// user's preferences // user's preferences
$defaultSet = $this->repository->getAccountsByType([AccountType::ASSET, AccountType::DEFAULT])->pluck('id')->toArray(); $defaultSet = $this->repository->getAccountsByType([AccountType::ASSET, AccountType::DEFAULT])->pluck('id')->toArray();

View File

@@ -69,6 +69,9 @@ class BalanceController extends Controller
* If the transaction being processed is already in native currency OR if the * If the transaction being processed is already in native currency OR if the
* foreign amount is in the native currency, the amount will not be converted. * foreign amount is in the native currency, the amount will not be converted.
* *
* TODO validate and set administration_id
* TODO collector set group, not user
*
* @param BalanceChartRequest $request * @param BalanceChartRequest $request
* *
* @return JsonResponse * @return JsonResponse
@@ -78,9 +81,9 @@ class BalanceController extends Controller
{ {
$params = $request->getAll(); $params = $request->getAll();
/** @var Carbon $start */ /** @var Carbon $start */
$start = $params['start']; $start = $this->parameters->get('start');
/** @var Carbon $end */ /** @var Carbon $end */
$end = $params['end']; $end = $this->parameters->get('end');
$end->endOfDay(); $end->endOfDay();
/** @var Collection $accounts */ /** @var Collection $accounts */
$accounts = $params['accounts']; $accounts = $params['accounts'];

View File

@@ -69,6 +69,8 @@ class BudgetController extends Controller
/** /**
* @param DateRequest $request * @param DateRequest $request
* *
* TODO see autocomplete/accountcontroller
*
* @return JsonResponse * @return JsonResponse
* @throws FireflyException * @throws FireflyException
*/ */

View File

@@ -61,6 +61,7 @@ class CategoryController extends Controller
/** /**
* TODO may be worth to move to a handler but the data is simple enough. * TODO may be worth to move to a handler but the data is simple enough.
* TODO see autoComplete/account controller
* *
* @param DateRequest $request * @param DateRequest $request
* *
@@ -69,11 +70,10 @@ class CategoryController extends Controller
*/ */
public function dashboard(DateRequest $request): JsonResponse public function dashboard(DateRequest $request): JsonResponse
{ {
$params = $request->getAll();
/** @var Carbon $start */ /** @var Carbon $start */
$start = $params['start']; $start = $this->parameters->get('start');
/** @var Carbon $end */ /** @var Carbon $end */
$end = $params['end']; $end = $this->parameters->get('end');
$accounts = $this->accountRepos->getAccountsByType([AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE, AccountType::ASSET, AccountType::DEFAULT]); $accounts = $this->accountRepos->getAccountsByType([AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE, AccountType::ASSET, AccountType::DEFAULT]);
$default = app('amount')->getDefaultCurrency(); $default = app('amount')->getDefaultCurrency();
$converter = new ExchangeRateConverter(); $converter = new ExchangeRateConverter();

View File

@@ -93,21 +93,25 @@ class Controller extends BaseController
// some date fields: // some date fields:
foreach ($dates as $field) { foreach ($dates as $field) {
$date = null; $date = null;
$obj = null;
try { try {
$date = request()->query->get($field); $date = request()->query->get($field);
} catch (BadRequestException $e) { } catch (BadRequestException $e) {
Log::error(sprintf('Request field "%s" contains a non-scalar value. Value set to NULL.', $field)); Log::error(sprintf('Request field "%s" contains a non-scalar value. Value set to NULL.', $field));
Log::error($e->getMessage()); Log::error($e->getMessage());
$value = null;
} }
$obj = null;
if (null !== $date) { if (null !== $date) {
try { try {
$obj = Carbon::parse($date); $obj = Carbon::parse($date, config('app.timezone'));
} catch (InvalidDateException | InvalidFormatException $e) { } catch (InvalidDateException | InvalidFormatException $e) {
// don't care // don't care
app('log')->warning(sprintf('Ignored invalid date "%s" in API v2 controller parameter check: %s', substr($date, 0, 20), $e->getMessage())); app('log')->warning(sprintf('Ignored invalid date "%s" in API v2 controller parameter check: %s', substr($date, 0, 20), $e->getMessage()));
} }
// out of range? set to null.
if (null !== $obj && ($obj->year <= 1900 || $obj->year > 2099)) {
app('log')->warning(sprintf('Refuse to use date "%s" in API v2 controller parameter check: %s', $field, $obj->toAtomString()));
$obj = null;
}
} }
$bag->set($field, $obj); $bag->set($field, $obj);
} }

View File

@@ -26,7 +26,7 @@ namespace FireflyIII\Api\V2\Controllers\Model\Bill;
use FireflyIII\Api\V2\Controllers\Controller; use FireflyIII\Api\V2\Controllers\Controller;
use FireflyIII\Api\V2\Request\Generic\DateRequest; use FireflyIII\Api\V2\Request\Generic\DateRequest;
use FireflyIII\Repositories\Bill\BillRepositoryInterface; use FireflyIII\Repositories\Administration\Bill\BillRepositoryInterface;
use FireflyIII\Support\Http\Api\ConvertsExchangeRates; use FireflyIII\Support\Http\Api\ConvertsExchangeRates;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
@@ -35,8 +35,6 @@ use Illuminate\Http\JsonResponse;
*/ */
class SumController extends Controller class SumController extends Controller
{ {
use ConvertsExchangeRates;
private BillRepositoryInterface $repository; private BillRepositoryInterface $repository;
/** /**
@@ -58,35 +56,37 @@ class SumController extends Controller
* This endpoint is documented at: * This endpoint is documented at:
* https://api-docs.firefly-iii.org/?urls.primaryName=2.0.0%20(v2)#/transactions-sum/getBillsPaidTrSum * https://api-docs.firefly-iii.org/?urls.primaryName=2.0.0%20(v2)#/transactions-sum/getBillsPaidTrSum
* *
* TODO see autocomplete/accountcontroller for list.
*
* @param DateRequest $request * @param DateRequest $request
* *
* @return JsonResponse * @return JsonResponse
*/ */
public function paid(DateRequest $request): JsonResponse public function paid(DateRequest $request): JsonResponse
{ {
$dates = $request->getAll(); $this->repository->setAdministrationId(auth()->user()->user_group_id);
$result = $this->repository->sumPaidInRange($dates['start'], $dates['end']); $result = $this->repository->sumPaidInRange($this->parameters->get('start'), $this->parameters->get('end'));
$converted = $this->cerSum($result);
// convert to JSON response: // convert to JSON response:
return response()->api($converted); return response()->api(array_values($result));
} }
/** /**
* This endpoint is documented at: * This endpoint is documented at:
* https://api-docs.firefly-iii.org/?urls.primaryName=2.0.0%20(v2)#/transactions-sum/getBillsUnpaidTrSum * https://api-docs.firefly-iii.org/?urls.primaryName=2.0.0%20(v2)#/transactions-sum/getBillsUnpaidTrSum
* *
* TODO see autocomplete/accountcontroller for list.
*
* @param DateRequest $request * @param DateRequest $request
* *
* @return JsonResponse * @return JsonResponse
*/ */
public function unpaid(DateRequest $request): JsonResponse public function unpaid(DateRequest $request): JsonResponse
{ {
$dates = $request->getAll(); $this->repository->setAdministrationId(auth()->user()->user_group_id);
$result = $this->repository->sumUnpaidInRange($dates['start'], $dates['end']); $result = $this->repository->sumUnpaidInRange($this->parameters->get('start'), $this->parameters->get('end'));
$converted = $this->cerSum($result);
// convert to JSON response: // convert to JSON response:
return response()->api($converted); return response()->api(array_values($result));
} }
} }

View File

@@ -60,6 +60,8 @@ class ListController extends Controller
*/ */
public function index(Request $request): JsonResponse public function index(Request $request): JsonResponse
{ {
echo 'this needs move to Administration';
exit;
$collection = $this->repository->getActiveBudgets(); $collection = $this->repository->getActiveBudgets();
$total = $collection->count(); $total = $collection->count();
$collection->slice($this->pageSize * $this->parameters->get('page'), $this->pageSize); $collection->slice($this->pageSize * $this->parameters->get('page'), $this->pageSize);

View File

@@ -41,8 +41,6 @@ class BalanceChartRequest extends FormRequest
public function getAll(): array public function getAll(): array
{ {
return [ return [
'start' => $this->getCarbonDate('start'),
'end' => $this->getCarbonDate('end'),
'accounts' => $this->getAccountList(), 'accounts' => $this->getAccountList(),
'period' => $this->string('period'), 'period' => $this->string('period'),
]; ];

View File

@@ -39,6 +39,32 @@ class BillRepository implements BillRepositoryInterface
{ {
use AdministrationTrait; use AdministrationTrait;
/**
* Correct order of piggies in case of issues.
*/
public function correctOrder(): void
{
$set = $this->userGroup->bills()->orderBy('order', 'ASC')->get();
$current = 1;
foreach ($set as $bill) {
if ((int)$bill->order !== $current) {
$bill->order = $current;
$bill->save();
}
$current++;
}
}
/**
* @return Collection
*/
public function getBills(): Collection
{
return $this->userGroup->bills()
->orderBy('bills.name', 'ASC')
->get(['bills.*']);
}
/** /**
* @inheritDoc * @inheritDoc
*/ */
@@ -109,7 +135,6 @@ class BillRepository implements BillRepositoryInterface
->get(['bills.*']); ->get(['bills.*']);
} }
/** /**
* @inheritDoc * @inheritDoc
*/ */

View File

@@ -32,11 +32,22 @@ use Illuminate\Support\Collection;
*/ */
interface BillRepositoryInterface interface BillRepositoryInterface
{ {
/**
* TODO duplicate of other repos
* Add correct order to bills.
*/
public function correctOrder(): void;
/** /**
* @return Collection * @return Collection
*/ */
public function getActiveBills(): Collection; public function getActiveBills(): Collection;
/**
* @return Collection
*/
public function getBills(): Collection;
/** /**
* Between start and end, tells you on which date(s) the bill is expected to hit. * Between start and end, tells you on which date(s) the bill is expected to hit.
* *

View File

@@ -42,6 +42,7 @@ trait ConvertsExchangeRates
* @param array $set * @param array $set
* *
* @return array * @return array
* @deprecated
*/ */
public function cerChartSet(array $set): array public function cerChartSet(array $set): array
{ {
@@ -80,6 +81,7 @@ trait ConvertsExchangeRates
/** /**
* @return void * @return void
* @deprecated
*/ */
private function getPreference(): void private function getPreference(): void
{ {
@@ -90,6 +92,7 @@ trait ConvertsExchangeRates
* @param int $currencyId * @param int $currencyId
* *
* @return TransactionCurrency * @return TransactionCurrency
* @deprecated
*/ */
private function getCurrency(int $currencyId): TransactionCurrency private function getCurrency(int $currencyId): TransactionCurrency
{ {
@@ -107,6 +110,8 @@ trait ConvertsExchangeRates
* *
* @return string * @return string
* @throws FireflyException * @throws FireflyException
*
* @deprecated
*/ */
private function getRate(TransactionCurrency $from, TransactionCurrency $to, Carbon $date): string private function getRate(TransactionCurrency $from, TransactionCurrency $to, Carbon $date): string
{ {
@@ -140,6 +145,8 @@ trait ConvertsExchangeRates
* @param string $date * @param string $date
* *
* @return string|null * @return string|null
*
* @deprecated
*/ */
private function getFromDB(int $from, int $to, string $date): ?string private function getFromDB(int $from, int $to, string $date): ?string
{ {
@@ -178,6 +185,8 @@ trait ConvertsExchangeRates
* *
* @return string * @return string
* @throws FireflyException * @throws FireflyException
*
* @deprecated
*/ */
private function getEuroRate(TransactionCurrency $currency, Carbon $date): string private function getEuroRate(TransactionCurrency $currency, Carbon $date): string
{ {
@@ -212,6 +221,8 @@ trait ConvertsExchangeRates
/** /**
* @return int * @return int
* @throws FireflyException * @throws FireflyException
*
* @deprecated
*/ */
private function getEuroId(): int private function getEuroId(): int
{ {
@@ -293,6 +304,8 @@ trait ConvertsExchangeRates
* @param Carbon|null $date * @param Carbon|null $date
* *
* @return string * @return string
*
* @deprecated
*/ */
private function convertAmount(string $amount, TransactionCurrency $from, TransactionCurrency $to, ?Carbon $date = null): string private function convertAmount(string $amount, TransactionCurrency $from, TransactionCurrency $to, ?Carbon $date = null): string
{ {

View File

@@ -124,15 +124,16 @@ Route::group(
); );
/** /**
* V2 API route for bills. * V2 API route for subscriptions.
*/ */
Route::group( Route::group(
[ [
'namespace' => 'FireflyIII\Api\V2\Controllers\Model\Bill', 'namespace' => 'FireflyIII\Api\V2\Controllers\Model\Bill',
'prefix' => 'v2/bills', 'prefix' => 'v2/subscriptions',
'as' => 'api.v2.bills.', 'as' => 'api.v2.subscriptions.',
], ],
static function () { static function () {
Route::get('', ['uses' => 'ShowController@index', 'as' => 'index']);
Route::get('sum/paid', ['uses' => 'SumController@paid', 'as' => 'sum.paid']); Route::get('sum/paid', ['uses' => 'SumController@paid', 'as' => 'sum.paid']);
Route::get('sum/unpaid', ['uses' => 'SumController@unpaid', 'as' => 'sum.unpaid']); Route::get('sum/unpaid', ['uses' => 'SumController@unpaid', 'as' => 'sum.unpaid']);
} }