Extend API

This commit is contained in:
James Cole 2023-08-09 14:14:33 +02:00
parent 03f39f53d8
commit a2984f299b
No known key found for this signature in database
GPG Key ID: B49A324B7EAD6D80
13 changed files with 95 additions and 34 deletions

View File

@ -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 = [];

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
* 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();

View File

@ -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'];

View File

@ -69,6 +69,8 @@ class BudgetController extends Controller
/**
* @param DateRequest $request
*
* TODO see autocomplete/accountcontroller
*
* @return JsonResponse
* @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 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();

View File

@ -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);
}

View File

@ -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));
}
}

View File

@ -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);

View File

@ -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'),
];

View File

@ -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
*/

View File

@ -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.
*

View File

@ -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
{

View File

@ -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']);
}