Expand API to return transaction groups.

This commit is contained in:
James Cole 2023-08-08 15:03:58 +02:00
parent 7a2d4c9bd2
commit 02c906afe6
No known key found for this signature in database
GPG Key ID: B49A324B7EAD6D80
12 changed files with 412 additions and 107 deletions

View File

@ -148,7 +148,7 @@ class Controller extends BaseController
$objects = $paginator->getCollection();
// the transformer, at this point, needs to collect information that ALL items in the collection
// require, like meta data and stuff like that, and save it for later.
// require, like meta-data and stuff like that, and save it for later.
$transformer->collectMetaData($objects);
$resource = new FractalCollection($objects, $transformer, $key);

View File

@ -49,19 +49,17 @@ class AccountController extends Controller
*
* @return JsonResponse
*/
public function listTransactions(ListRequest $request, Account $account): JsonResponse
public function list(ListRequest $request, Account $account): JsonResponse
{
// collect transactions:
$type = $request->get('type') ?? 'default';
$limit = (int)$request->get('limit');
$page = (int)$request->get('page');
$limit = $request->getLimit();
$page = $request->getPage();
$page = max($page, 1);
if ($limit > 0 && $limit <= $this->pageSize) {
$this->pageSize = $limit;
}
$types = $this->mapTransactionTypes($type);
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
@ -69,15 +67,23 @@ class AccountController extends Controller
->withAPIInformation()
->setLimit($this->pageSize)
->setPage($page)
->setTypes($types);
->setTypes($request->getTransactionTypes());
// TODO date filter
//if (null !== $this->parameters->get('start') && null !== $this->parameters->get('end')) {
// $collector->setRange($this->parameters->get('start'), $this->parameters->get('end'));
//}
$start = $request->getStartDate();
$end = $request->getEndDate();
if (null !== $start) {
$collector->setStart($start);
}
if (null !== $end) {
$collector->setEnd($start);
}
$paginator = $collector->getPaginatedGroups();
$paginator->setPath(route('api.v2.accounts.transactions', [$account->id])); // TODO . $this->buildParams()
$paginator->setPath(
sprintf('%s?%s',
route('api.v2.accounts.transactions', [$account->id]),
$request->buildParams())
);
return response()
->json($this->jsonApiList('transactions', $paginator, new TransactionGroupTransformer()))

View File

@ -0,0 +1,83 @@
<?php
/*
* TransactionController.php
* Copyright (c) 2023 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace FireflyIII\Api\V2\Controllers\Transaction\List;
use FireflyIII\Api\V2\Controllers\Controller;
use FireflyIII\Api\V2\Request\Transaction\ListRequest;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Transformers\V2\TransactionGroupTransformer;
use Illuminate\Http\JsonResponse;
/**
* Class TransactionController
*/
class TransactionController extends Controller
{
/**
* @param ListRequest $request
*
* @return JsonResponse
*/
public function list(ListRequest $request): JsonResponse
{
// collect transactions:
$limit = $request->getLimit();
$page = $request->getPage();
$page = max($page, 1);
if ($limit > 0 && $limit <= $this->pageSize) {
$this->pageSize = $limit;
}
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setUserGroup(auth()->user()->userGroup)
->withAPIInformation()
->setLimit($this->pageSize)
->setPage($page)
->setTypes($request->getTransactionTypes());
$start = $request->getStartDate();
$end = $request->getEndDate();
if (null !== $start) {
$collector->setStart($start);
}
if (null !== $end) {
$collector->setEnd($start);
}
$paginator = $collector->getPaginatedGroups();
$paginator->setPath(
sprintf('%s?%s',
route('api.v2.transactions.list'),
$request->buildParams())
);
return response()
->json($this->jsonApiList('transactions', $paginator, new TransactionGroupTransformer()))
->header('Content-Type', self::CONTENT_TYPE);
}
}

View File

@ -24,15 +24,84 @@ declare(strict_types=1);
namespace FireflyIII\Api\V2\Request\Transaction;
use Carbon\Carbon;
use FireflyIII\Support\Http\Api\TransactionFilter;
use FireflyIII\Support\Request\ChecksLogin;
use FireflyIII\Support\Request\ConvertsDataTypes;
use Illuminate\Foundation\Http\FormRequest;
/**
* Class ListRequest
* Used specifically to list transactions.
*/
class ListRequest extends FormRequest
{
use ChecksLogin;
use ConvertsDataTypes;
use TransactionFilter;
/**
* @return string
*/
public function buildParams(): string
{
$array = [
'page' => $this->getPage(),
];
$start = $this->getStartDate();
$end = $this->getEndDate();
if (null !== $start && null !== $end) {
$array['start'] = $start->format('Y-m-d');
$array['end'] = $end->format('Y-m-d');
}
if (0 !== $this->getLimit()) {
$array['limit'] = $this->getLimit();
}
return http_build_query($array);
}
/**
* @return int
*/
public function getPage(): int
{
$page = $this->convertInteger('page');
return 0 === $page || $page > 65536 ? 1 : $page;
}
/**
* @return Carbon|null
*/
public function getStartDate(): ?Carbon
{
return $this->getCarbonDate('start');
}
/**
* @return Carbon|null
*/
public function getEndDate(): ?Carbon
{
return $this->getCarbonDate('end');
}
/**
* @return int
*/
public function getLimit(): int
{
return $this->convertInteger('limit');
}
/**
* @return array
*/
public function getTransactionTypes(): array
{
$type = (string)$this->get('type', 'default');
return $this->mapTransactionTypes($type);
}
/**
* @return array

View File

@ -675,6 +675,23 @@ trait TimeCollection
return $this;
}
/**
* Set the end time of the results to return.
*
* @param Carbon $end
*
* @return GroupCollectorInterface
*/
public function setEnd(Carbon $end): GroupCollectorInterface
{
// always got to end of day / start of day for ranges.
$endStr = $end->format('Y-m-d 23:59:59');
$this->query->where('transaction_journals.date', '<=', $endStr);
return $this;
}
/**
* @param Carbon $date
* @param string $field
@ -822,6 +839,22 @@ trait TimeCollection
return $this;
}
/**
* Set the start time of the results to return.
*
* @param Carbon $start
*
* @return GroupCollectorInterface
*/
public function setStart(Carbon $start): GroupCollectorInterface
{
$startStr = $start->format('Y-m-d 00:00:00');
$this->query->where('transaction_journals.date', '>=', $startStr);
return $this;
}
/**
* Collect transactions updated on a specific date.
*

View File

@ -1078,6 +1078,15 @@ interface GroupCollectorInterface
*/
public function setDestinationAccounts(Collection $accounts): GroupCollectorInterface;
/**
* Set the end time of the results to return.
*
* @param Carbon $end
*
* @return GroupCollectorInterface
*/
public function setEnd(Carbon $end): GroupCollectorInterface;
/**
* @param bool $expandGroupSearch
*/
@ -1262,6 +1271,15 @@ interface GroupCollectorInterface
*/
public function setSourceAccounts(Collection $accounts): GroupCollectorInterface;
/**
* Set the start time of the results to return.
*
* @param Carbon $start
*
* @return GroupCollectorInterface
*/
public function setStart(Carbon $start): GroupCollectorInterface;
/**
* Limit results to a specific tag.
*

View File

@ -0,0 +1,59 @@
<?php
/**
* AccountList.php
* Copyright (c) 2019 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Support\Binder;
use FireflyIII\Models\Account;
use FireflyIII\User;
use Illuminate\Routing\Route;
use Illuminate\Support\Collection;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Class UserGroupAccount.
*/
class UserGroupAccount implements BinderInterface
{
/**
* @param string $value
* @param Route $route
*
* @return Account
* @throws NotFoundHttpException
*
*/
public static function routeBinder(string $value, Route $route): Account
{
if (auth()->check()) {
/** @var User $user */
$user = auth()->user();
$currency = Account::where('id', (int)$value)
->where('user_group_id', $user->user_group_id)
->first();
if (null !== $currency) {
return $currency;
}
}
throw new NotFoundHttpException();
}
}

View File

@ -65,7 +65,11 @@ trait TransactionFilter
'specials' => [TransactionType::OPENING_BALANCE, TransactionType::RECONCILIATION,],
'default' => [TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::TRANSFER,],
];
return $types[$type] ?? $types['default'];
$return = [];
$parts = explode(',', $type);
foreach ($parts as $part) {
$return = array_merge($return, $types[$part] ?? $types['default']);
}
return array_unique($return);
}
}

View File

@ -36,6 +36,8 @@ abstract class AbstractTransformer extends TransformerAbstract
protected ParameterBag $parameters;
/**
* This method is called exactly ONCE from FireflyIII\Api\V2\Controllers\Controller::jsonApiList
*
* @param Collection $objects
*
* @return void

View File

@ -25,13 +25,19 @@ declare(strict_types=1);
namespace FireflyIII\Transformers\V2;
use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Note;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionJournalMeta;
use FireflyIII\Models\TransactionType;
use FireflyIII\Support\Http\Api\ConvertsExchangeRates;
use FireflyIII\Support\Http\Api\ExchangeRateConverter;
use FireflyIII\Support\NullArrayObject;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use stdClass;
/**
* Class TransactionGroupTransformer
@ -40,9 +46,12 @@ class TransactionGroupTransformer extends AbstractTransformer
{
use ConvertsExchangeRates;
private ExchangeRateConverter $converter;
private array $currencies = [];
private TransactionCurrency $default;
private array $meta;
private array $notes;
private array $tags;
/**
* @inheritDoc
@ -55,15 +64,12 @@ class TransactionGroupTransformer extends AbstractTransformer
/** @var array $object */
foreach ($objects as $object) {
foreach ($object['sums'] as $sum) {
$id = $sum['currency_id'];
if (!array_key_exists($id, $currencies)) {
$currencyObject = TransactionCurrency::find($sum['currency_id']);
$currencies[$id] = $currencyObject;
}
$id = (int)$sum['currency_id'];
$currencies[$id] = $currencies[$id] ?? TransactionCurrency::find($sum['currency_id']);
}
/** @var array $transaction */
foreach ($object['transactions'] as $transaction) {
$id = $transaction['transaction_journal_id'];
$id = (int)$transaction['transaction_journal_id'];
$journals[$id] = [];
}
}
@ -77,6 +83,28 @@ class TransactionGroupTransformer extends AbstractTransformer
$id = (int)$entry->transaction_journal_id;
$this->meta[$id][$entry->name] = $entry->data;
}
// grab all notes for all journals:
$notes = Note::whereNoteableType(TransactionJournal::class)->whereIn('noteable_id', array_keys($journals))->get();
/** @var Note $note */
foreach ($notes as $note) {
$id = (int)$note->noteable_id;
$this->notes[$id] = $note;
}
// grab all tags for all journals:
$tags = DB::table('tag_transaction_journal')
->leftJoin('tags', 'tags.id', 'tag_transaction_journal.tag_id')
->whereIn('tag_transaction_journal.transaction_journal_id', array_keys($journals))
->get(['tag_transaction_journal.transaction_journal_id', 'tags.tag']);
/** @var stdClass $tag */
foreach ($tags as $tag) {
$id = (int)$tag->transaction_journal_id;
$this->tags[$id][] = $tag->tag;
}
// create converter
$this->converter = new ExchangeRateConverter();
}
/**
@ -92,6 +120,7 @@ class TransactionGroupTransformer extends AbstractTransformer
'created_at' => $first['created_at']->toAtomString(),
'updated_at' => $first['updated_at']->toAtomString(),
'user' => (string)$first['user_id'],
'user_group' => (string)$first['user_group_id'],
'group_title' => $group['title'] ?? null,
'transactions' => $this->transformTransactions($group['transactions'] ?? []),
'links' => [
@ -122,37 +151,40 @@ class TransactionGroupTransformer extends AbstractTransformer
* @param array $transaction
*
* @return array
* @throws FireflyException
*/
private function transformTransaction(array $transaction): array
{
$transaction = new NullArrayObject($transaction);
$type = $this->stringFromArray($transaction, 'transaction_type_type', TransactionType::WITHDRAWAL);
$journalId = (int)$transaction['transaction_journal_id'];
$meta = new NullArrayObject($this->meta[$journalId] ?? []);
$type = $this->stringFromArray($transaction, 'transaction_type_type', TransactionType::WITHDRAWAL);
/**
* Convert and use amount:
*/
$amount = app('steam')->positive((string)($transaction['amount'] ?? '0'));
$currencyId = (int)$transaction['currency_id'];
$nativeAmount = $this->converter->convert($this->default, $this->currencies[$currencyId], $transaction['date'], $amount);
$foreignAmount = null;
$nativeForeignAmount = null;
if (null !== $transaction['foreign_amount']) {
$foreignCurrencyId = (int)$transaction['foreign_currency_id'];
$foreignAmount = app('steam')->positive($transaction['foreign_amount']);
$nativeForeignAmount = $foreignAmount;
if ($transaction['foreign_currency_id'] !== $this->default->id) {
$rate = $this->getRate($this->currencies[$transaction['foreign_currency_id']], $this->default, $transaction['date']);
$nativeForeignAmount = bcmul($foreignAmount, $rate);
}
}
$nativeAmount = $amount;
if ($transaction['currency_id'] !== $this->default->id) {
$rate = $this->getRate($this->currencies[$transaction['currency_id']], $this->default, $transaction['date']);
$nativeAmount = bcmul($amount, $rate);
$nativeForeignAmount = $this->converter->convert($this->default, $this->currencies[$foreignCurrencyId], $transaction['date'], $foreignAmount);
}
return [
'user' => (string)$transaction['user_id'],
'user_group' => (string)$transaction['user_group_id'],
'transaction_journal_id' => (string)$transaction['transaction_journal_id'],
'type' => strtolower($type),
'date' => $transaction['date']->toAtomString(),
'order' => $transaction['order'],
'amount' => $amount,
'native_amount' => $nativeAmount,
'foreign_amount' => $foreignAmount,
'native_foreign_amount' => $nativeForeignAmount,
'currency_id' => (string)$transaction['currency_id'],
'currency_code' => $transaction['currency_code'],
'currency_name' => $transaction['currency_name'],
@ -160,26 +192,20 @@ class TransactionGroupTransformer extends AbstractTransformer
'currency_decimal_places' => (int)$transaction['currency_decimal_places'],
// converted to native currency
'native_currency_converted' => $transaction['currency_id'] !== $this->default->id,
'native_currency_id' => (string)$this->default->id,
'native_currency_code' => $this->default->code,
'native_currency_name' => $this->default->name,
'native_currency_symbol' => $this->default->symbol,
'native_currency_decimal_places' => (int)$this->default->decimal_places,
'native_id' => (string)$this->default->id,
'native_code' => $this->default->code,
'native_name' => $this->default->name,
'native_symbol' => $this->default->symbol,
'native_decimal_places' => (int)$this->default->decimal_places,
// foreign currency amount:
'foreign_currency_id' => $this->stringFromArray($transaction, 'foreign_currency_id', null),
'foreign_currency_code' => $transaction['foreign_currency_code'],
'foreign_currency_name' => $transaction['foreign_currency_name'],
'foreign_currency_symbol' => $transaction['foreign_currency_symbol'],
'foreign_currency_decimal_places' => $transaction['foreign_currency_decimal_places'],
// foreign converted to native currency:
'foreign_currency_converted' => null !== $transaction['foreign_currency_id'] && $transaction['foreign_currency_id'] !== $this->default->id,
'amount' => $amount,
'native_amount' => $nativeAmount,
'foreign_amount' => $foreignAmount,
'native_foreign_amount' => $nativeForeignAmount,
// foreign converted to native:
'description' => $transaction['description'],
'source_id' => (string)$transaction['source_account_id'],
'source_name' => $transaction['source_account_name'],
@ -196,10 +222,8 @@ class TransactionGroupTransformer extends AbstractTransformer
'bill_id' => $this->stringFromArray($transaction, 'bill_id', null),
'bill_name' => $transaction['bill_name'],
'reconciled' => $transaction['reconciled'],
//'notes' => $this->groupRepos->getNoteText((int) $row['transaction_journal_id']),
//'tags' => $this->groupRepos->getTags((int) $row['transaction_journal_id']),
'notes' => $this->notes[$journalId] ?? null,
'tags' => $this->tags[$journalId] ?? [],
'internal_reference' => $meta['internal_reference'],
'external_id' => $meta['external_id'],
'original_source' => $meta['original_source'],
@ -217,7 +241,6 @@ class TransactionGroupTransformer extends AbstractTransformer
'sepa_ep' => $meta['sepa_ep'],
'sepa_ci' => $meta['sepa_ci'],
'sepa_batch_id' => $meta['sepa_batch_id'],
'interest_date' => $this->date($meta['interest_date']),
'book_date' => $this->date($meta['book_date']),
'process_date' => $this->date($meta['process_date']),
@ -237,6 +260,9 @@ class TransactionGroupTransformer extends AbstractTransformer
/**
* TODO also in the old transformer.
*
* Used to extract a value from the given array, and fall back on a sensible default or NULL
* if it can't be helped.
*
* @param NullArrayObject $array
* @param string $key
* @param string|null $default

View File

@ -60,6 +60,7 @@ use FireflyIII\Support\Binder\EitherConfigKey;
use FireflyIII\Support\Binder\JournalList;
use FireflyIII\Support\Binder\TagList;
use FireflyIII\Support\Binder\TagOrId;
use FireflyIII\Support\Binder\UserGroupAccount;
use FireflyIII\TransactionRules\Actions\AddTag;
use FireflyIII\TransactionRules\Actions\AppendDescription;
use FireflyIII\TransactionRules\Actions\AppendDescriptionToNotes;
@ -476,6 +477,9 @@ return [
'dynamicConfigKey' => DynamicConfigKey::class,
'eitherConfigKey' => EitherConfigKey::class,
// V2 API endpoints:
'userGroupAccount' => UserGroupAccount::class,
],
'rule-actions' => [

View File

@ -58,7 +58,8 @@ Route::group(
'as' => 'api.v2.',
],
static function () {
Route::get('accounts/{account}/transactions', ['uses' => 'AccountController@listTransactions', 'as' => 'accounts.transactions']);
Route::get('transactions', ['uses' => 'TransactionController@list', 'as' => 'transactions.list']);
Route::get('accounts/{userGroupAccount}/transactions', ['uses' => 'AccountController@list', 'as' => 'accounts.transactions']);
}
);