From a2984f299b9f1f862b0742bfb033a02a9e7c5916 Mon Sep 17 00:00:00 2001 From: James Cole Date: Wed, 9 Aug 2023 14:14:33 +0200 Subject: [PATCH] Extend API --- .../Autocomplete/AccountController.php | 10 ++++--- .../Controllers/Chart/AccountController.php | 10 +++---- .../Controllers/Chart/BalanceController.php | 7 +++-- .../V2/Controllers/Chart/BudgetController.php | 2 ++ .../Controllers/Chart/CategoryController.php | 6 ++--- app/Api/V2/Controllers/Controller.php | 10 ++++--- .../Controllers/Model/Bill/SumController.php | 22 +++++++-------- .../Model/Budget/ListController.php | 2 ++ .../V2/Request/Chart/BalanceChartRequest.php | 2 -- .../Administration/Bill/BillRepository.php | 27 ++++++++++++++++++- .../Bill/BillRepositoryInterface.php | 11 ++++++++ .../Http/Api/ConvertsExchangeRates.php | 13 +++++++++ routes/api.php | 7 ++--- 13 files changed, 95 insertions(+), 34 deletions(-) diff --git a/app/Api/V2/Controllers/Autocomplete/AccountController.php b/app/Api/V2/Controllers/Autocomplete/AccountController.php index 393c5ce06c..4f0bdc4931 100644 --- a/app/Api/V2/Controllers/Autocomplete/AccountController.php +++ b/app/Api/V2/Controllers/Autocomplete/AccountController.php @@ -65,12 +65,16 @@ class AccountController extends Controller /** * 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 * * @return JsonResponse - * @throws JsonException * @throws FireflyException * @throws FireflyException */ @@ -79,7 +83,7 @@ class AccountController extends Controller $data = $request->getData(); $types = $data['types']; $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']); $return = []; diff --git a/app/Api/V2/Controllers/Chart/AccountController.php b/app/Api/V2/Controllers/Chart/AccountController.php index dbfd1fa160..62292e1920 100644 --- a/app/Api/V2/Controllers/Chart/AccountController.php +++ b/app/Api/V2/Controllers/Chart/AccountController.php @@ -71,6 +71,8 @@ class AccountController extends Controller * If a transaction has foreign currency = native currency, the foreign amount will be used, no conversion * will take place. * + * TODO validate and set administration_id from request + * * @param DateRequest $request * * @return JsonResponse @@ -80,15 +82,11 @@ class AccountController extends Controller */ public function dashboard(DateRequest $request): JsonResponse { - // parameters for chart: - $dates = $request->getAll(); /** @var Carbon $start */ - $start = $dates['start']; + $start = $this->parameters->get('start'); /** @var Carbon $end */ - $end = $dates['end']; + $end = $this->parameters->get('end'); $end->endOfDay(); - /** @var User $user */ - $user = auth()->user(); // user's preferences $defaultSet = $this->repository->getAccountsByType([AccountType::ASSET, AccountType::DEFAULT])->pluck('id')->toArray(); diff --git a/app/Api/V2/Controllers/Chart/BalanceController.php b/app/Api/V2/Controllers/Chart/BalanceController.php index e70793fd52..0756ffe99c 100644 --- a/app/Api/V2/Controllers/Chart/BalanceController.php +++ b/app/Api/V2/Controllers/Chart/BalanceController.php @@ -69,6 +69,9 @@ class BalanceController extends Controller * 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. * + * TODO validate and set administration_id + * TODO collector set group, not user + * * @param BalanceChartRequest $request * * @return JsonResponse @@ -78,9 +81,9 @@ class BalanceController extends Controller { $params = $request->getAll(); /** @var Carbon $start */ - $start = $params['start']; + $start = $this->parameters->get('start'); /** @var Carbon $end */ - $end = $params['end']; + $end = $this->parameters->get('end'); $end->endOfDay(); /** @var Collection $accounts */ $accounts = $params['accounts']; diff --git a/app/Api/V2/Controllers/Chart/BudgetController.php b/app/Api/V2/Controllers/Chart/BudgetController.php index 236ccf9de1..83d2d93939 100644 --- a/app/Api/V2/Controllers/Chart/BudgetController.php +++ b/app/Api/V2/Controllers/Chart/BudgetController.php @@ -69,6 +69,8 @@ class BudgetController extends Controller /** * @param DateRequest $request * + * TODO see autocomplete/accountcontroller + * * @return JsonResponse * @throws FireflyException */ diff --git a/app/Api/V2/Controllers/Chart/CategoryController.php b/app/Api/V2/Controllers/Chart/CategoryController.php index bf167b3bad..db8404237e 100644 --- a/app/Api/V2/Controllers/Chart/CategoryController.php +++ b/app/Api/V2/Controllers/Chart/CategoryController.php @@ -61,6 +61,7 @@ class CategoryController extends Controller /** * TODO may be worth to move to a handler but the data is simple enough. + * TODO see autoComplete/account controller * * @param DateRequest $request * @@ -69,11 +70,10 @@ class CategoryController extends Controller */ public function dashboard(DateRequest $request): JsonResponse { - $params = $request->getAll(); /** @var Carbon $start */ - $start = $params['start']; + $start = $this->parameters->get('start'); /** @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]); $default = app('amount')->getDefaultCurrency(); $converter = new ExchangeRateConverter(); diff --git a/app/Api/V2/Controllers/Controller.php b/app/Api/V2/Controllers/Controller.php index a4eec48b3f..72c7975473 100644 --- a/app/Api/V2/Controllers/Controller.php +++ b/app/Api/V2/Controllers/Controller.php @@ -93,21 +93,25 @@ class Controller extends BaseController // some date fields: foreach ($dates as $field) { $date = null; + $obj = null; try { $date = request()->query->get($field); } catch (BadRequestException $e) { Log::error(sprintf('Request field "%s" contains a non-scalar value. Value set to NULL.', $field)); Log::error($e->getMessage()); - $value = null; } - $obj = null; if (null !== $date) { try { - $obj = Carbon::parse($date); + $obj = Carbon::parse($date, config('app.timezone')); } catch (InvalidDateException | InvalidFormatException $e) { // don't care 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); } diff --git a/app/Api/V2/Controllers/Model/Bill/SumController.php b/app/Api/V2/Controllers/Model/Bill/SumController.php index 61456329b8..f8288b833d 100644 --- a/app/Api/V2/Controllers/Model/Bill/SumController.php +++ b/app/Api/V2/Controllers/Model/Bill/SumController.php @@ -26,7 +26,7 @@ namespace FireflyIII\Api\V2\Controllers\Model\Bill; use FireflyIII\Api\V2\Controllers\Controller; 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 Illuminate\Http\JsonResponse; @@ -35,8 +35,6 @@ use Illuminate\Http\JsonResponse; */ class SumController extends Controller { - use ConvertsExchangeRates; - private BillRepositoryInterface $repository; /** @@ -58,35 +56,37 @@ class SumController extends Controller * This endpoint is documented at: * 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 * * @return JsonResponse */ public function paid(DateRequest $request): JsonResponse { - $dates = $request->getAll(); - $result = $this->repository->sumPaidInRange($dates['start'], $dates['end']); - $converted = $this->cerSum($result); + $this->repository->setAdministrationId(auth()->user()->user_group_id); + $result = $this->repository->sumPaidInRange($this->parameters->get('start'), $this->parameters->get('end')); // convert to JSON response: - return response()->api($converted); + return response()->api(array_values($result)); } /** * This endpoint is documented at: * 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 * * @return JsonResponse */ public function unpaid(DateRequest $request): JsonResponse { - $dates = $request->getAll(); - $result = $this->repository->sumUnpaidInRange($dates['start'], $dates['end']); - $converted = $this->cerSum($result); + $this->repository->setAdministrationId(auth()->user()->user_group_id); + $result = $this->repository->sumUnpaidInRange($this->parameters->get('start'), $this->parameters->get('end')); // convert to JSON response: - return response()->api($converted); + return response()->api(array_values($result)); } } diff --git a/app/Api/V2/Controllers/Model/Budget/ListController.php b/app/Api/V2/Controllers/Model/Budget/ListController.php index f2f5645f30..3a3cca394d 100644 --- a/app/Api/V2/Controllers/Model/Budget/ListController.php +++ b/app/Api/V2/Controllers/Model/Budget/ListController.php @@ -60,6 +60,8 @@ class ListController extends Controller */ public function index(Request $request): JsonResponse { + echo 'this needs move to Administration'; + exit; $collection = $this->repository->getActiveBudgets(); $total = $collection->count(); $collection->slice($this->pageSize * $this->parameters->get('page'), $this->pageSize); diff --git a/app/Api/V2/Request/Chart/BalanceChartRequest.php b/app/Api/V2/Request/Chart/BalanceChartRequest.php index 48703b997d..f7003d967e 100644 --- a/app/Api/V2/Request/Chart/BalanceChartRequest.php +++ b/app/Api/V2/Request/Chart/BalanceChartRequest.php @@ -41,8 +41,6 @@ class BalanceChartRequest extends FormRequest public function getAll(): array { return [ - 'start' => $this->getCarbonDate('start'), - 'end' => $this->getCarbonDate('end'), 'accounts' => $this->getAccountList(), 'period' => $this->string('period'), ]; diff --git a/app/Repositories/Administration/Bill/BillRepository.php b/app/Repositories/Administration/Bill/BillRepository.php index c0d8442555..bb123447a3 100644 --- a/app/Repositories/Administration/Bill/BillRepository.php +++ b/app/Repositories/Administration/Bill/BillRepository.php @@ -39,6 +39,32 @@ class BillRepository implements BillRepositoryInterface { 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 */ @@ -109,7 +135,6 @@ class BillRepository implements BillRepositoryInterface ->get(['bills.*']); } - /** * @inheritDoc */ diff --git a/app/Repositories/Administration/Bill/BillRepositoryInterface.php b/app/Repositories/Administration/Bill/BillRepositoryInterface.php index 80ff3b15fa..12327f568c 100644 --- a/app/Repositories/Administration/Bill/BillRepositoryInterface.php +++ b/app/Repositories/Administration/Bill/BillRepositoryInterface.php @@ -32,11 +32,22 @@ use Illuminate\Support\Collection; */ interface BillRepositoryInterface { + /** + * TODO duplicate of other repos + * Add correct order to bills. + */ + public function correctOrder(): void; + /** * @return 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. * diff --git a/app/Support/Http/Api/ConvertsExchangeRates.php b/app/Support/Http/Api/ConvertsExchangeRates.php index 42fe3119de..b34eecd144 100644 --- a/app/Support/Http/Api/ConvertsExchangeRates.php +++ b/app/Support/Http/Api/ConvertsExchangeRates.php @@ -42,6 +42,7 @@ trait ConvertsExchangeRates * @param array $set * * @return array + * @deprecated */ public function cerChartSet(array $set): array { @@ -80,6 +81,7 @@ trait ConvertsExchangeRates /** * @return void + * @deprecated */ private function getPreference(): void { @@ -90,6 +92,7 @@ trait ConvertsExchangeRates * @param int $currencyId * * @return TransactionCurrency + * @deprecated */ private function getCurrency(int $currencyId): TransactionCurrency { @@ -107,6 +110,8 @@ trait ConvertsExchangeRates * * @return string * @throws FireflyException + * + * @deprecated */ private function getRate(TransactionCurrency $from, TransactionCurrency $to, Carbon $date): string { @@ -140,6 +145,8 @@ trait ConvertsExchangeRates * @param string $date * * @return string|null + * + * @deprecated */ private function getFromDB(int $from, int $to, string $date): ?string { @@ -178,6 +185,8 @@ trait ConvertsExchangeRates * * @return string * @throws FireflyException + * + * @deprecated */ private function getEuroRate(TransactionCurrency $currency, Carbon $date): string { @@ -212,6 +221,8 @@ trait ConvertsExchangeRates /** * @return int * @throws FireflyException + * + * @deprecated */ private function getEuroId(): int { @@ -293,6 +304,8 @@ trait ConvertsExchangeRates * @param Carbon|null $date * * @return string + * + * @deprecated */ private function convertAmount(string $amount, TransactionCurrency $from, TransactionCurrency $to, ?Carbon $date = null): string { diff --git a/routes/api.php b/routes/api.php index b862f6dda7..46a67cb10e 100644 --- a/routes/api.php +++ b/routes/api.php @@ -124,15 +124,16 @@ Route::group( ); /** - * V2 API route for bills. + * V2 API route for subscriptions. */ Route::group( [ 'namespace' => 'FireflyIII\Api\V2\Controllers\Model\Bill', - 'prefix' => 'v2/bills', - 'as' => 'api.v2.bills.', + 'prefix' => 'v2/subscriptions', + 'as' => 'api.v2.subscriptions.', ], static function () { + Route::get('', ['uses' => 'ShowController@index', 'as' => 'index']); Route::get('sum/paid', ['uses' => 'SumController@paid', 'as' => 'sum.paid']); Route::get('sum/unpaid', ['uses' => 'SumController@unpaid', 'as' => 'sum.unpaid']); }