diff --git a/app/Api/V1/Controllers/TransactionController.php b/app/Api/V1/Controllers/TransactionController.php index 59a64a288a..b33f64cf1e 100644 --- a/app/Api/V1/Controllers/TransactionController.php +++ b/app/Api/V1/Controllers/TransactionController.php @@ -35,6 +35,8 @@ use FireflyIII\Helpers\Filter\PositiveAmountFilter; use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionType; use FireflyIII\Repositories\Journal\JournalRepositoryInterface; +use FireflyIII\Transformers\AttachmentTransformer; +use FireflyIII\Transformers\PiggyBankEventTransformer; use FireflyIII\Transformers\TransactionTransformer; use FireflyIII\User; use Illuminate\Http\JsonResponse; @@ -75,6 +77,44 @@ class TransactionController extends Controller ); } + /** + * @param Request $request + * @param Transaction $transaction + * + * @return JsonResponse + */ + public function attachments(Request $request, Transaction $transaction): JsonResponse + { + $manager = new Manager(); + $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; + $manager->setSerializer(new JsonApiSerializer($baseUrl)); + + $attachments = $transaction->transactionJournal->attachments()->get(); + $resource = new FractalCollection($attachments, new AttachmentTransformer($this->parameters), 'attachments'); + + return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); + + } + + /** + * @param Request $request + * @param Transaction $transaction + * + * @return JsonResponse + */ + public function piggyBankEvents(Request $request, Transaction $transaction): JsonResponse + { + $manager = new Manager(); + $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; + $manager->setSerializer(new JsonApiSerializer($baseUrl)); + + $events= $transaction->transactionJournal->piggyBankEvents()->get(); + $resource = new FractalCollection($events, new PiggyBankEventTransformer($this->parameters), 'piggy_bank_events'); + + return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); + + } + /** * Remove the specified resource from storage. * @@ -135,7 +175,6 @@ class TransactionController extends Controller return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); } - /** * Show a single transaction. * diff --git a/app/Models/PiggyBankEvent.php b/app/Models/PiggyBankEvent.php index 6cc3042832..752f70c16e 100644 --- a/app/Models/PiggyBankEvent.php +++ b/app/Models/PiggyBankEvent.php @@ -29,11 +29,12 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo; /** * Class PiggyBankEvent. * - * @property PiggyBank $piggyBank - * @property int $transaction_journal_id - * @property int $piggy_bank_id - * @property int $id - * @property Carbon date + * @property PiggyBank $piggyBank + * @property int $transaction_journal_id + * @property int $piggy_bank_id + * @property int $id + * @property Carbon date + * @property TransactionJournal transactionJournal */ class PiggyBankEvent extends Model { diff --git a/app/Transformers/AttachmentTransformer.php b/app/Transformers/AttachmentTransformer.php index c2f05bf208..d5e497e55a 100644 --- a/app/Transformers/AttachmentTransformer.php +++ b/app/Transformers/AttachmentTransformer.php @@ -26,8 +26,6 @@ namespace FireflyIII\Transformers; use FireflyIII\Models\Attachment; use FireflyIII\Repositories\Attachment\AttachmentRepositoryInterface; -use League\Fractal\Resource\Collection as FractalCollection; -use League\Fractal\Resource\Item; use League\Fractal\TransformerAbstract; use Symfony\Component\HttpFoundation\ParameterBag; @@ -36,19 +34,6 @@ use Symfony\Component\HttpFoundation\ParameterBag; */ class AttachmentTransformer extends TransformerAbstract { - /** - * List of resources possible to include - * - * @var array - */ - protected $availableIncludes = ['user']; - /** - * List of resources to automatically include - * - * @var array - */ - protected $defaultIncludes = ['user']; - /** @var ParameterBag */ protected $parameters; @@ -68,20 +53,6 @@ class AttachmentTransformer extends TransformerAbstract $this->repository = app(AttachmentRepositoryInterface::class); } - /** - * Attach the user. - * - * @codeCoverageIgnore - * - * @param Attachment $attachment - * - * @return Item - */ - public function includeUser(Attachment $attachment): Item - { - return $this->item($attachment->user, new UserTransformer($this->parameters), 'users'); - } - /** * Transform attachment. * @@ -97,7 +68,8 @@ class AttachmentTransformer extends TransformerAbstract 'id' => (int)$attachment->id, 'updated_at' => $attachment->updated_at->toAtomString(), 'created_at' => $attachment->created_at->toAtomString(), - 'attachable_type' => $attachment->attachable_type, + 'attachable_id' => $attachment->attachable_id, + 'attachable_type' => str_replace('FireflyIII\\Models\\','',$attachment->attachable_type), 'md5' => $attachment->md5, 'filename' => $attachment->filename, 'download_uri' => route('api.v1.attachments.download', [$attachment->id]), diff --git a/app/Transformers/PiggyBankEventTransformer.php b/app/Transformers/PiggyBankEventTransformer.php index 5999421b30..a95c0565c2 100644 --- a/app/Transformers/PiggyBankEventTransformer.php +++ b/app/Transformers/PiggyBankEventTransformer.php @@ -24,12 +24,9 @@ declare(strict_types=1); namespace FireflyIII\Transformers; -use FireflyIII\Helpers\Collector\TransactionCollectorInterface; use FireflyIII\Models\PiggyBankEvent; use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; -use Illuminate\Support\Collection; -use League\Fractal\Resource\Item; use League\Fractal\TransformerAbstract; use Symfony\Component\HttpFoundation\ParameterBag; @@ -38,19 +35,6 @@ use Symfony\Component\HttpFoundation\ParameterBag; */ class PiggyBankEventTransformer extends TransformerAbstract { - /** - * List of resources possible to include - * - * @var array - */ - protected $availableIncludes = ['piggy_bank', 'transaction']; - /** - * List of resources to automatically include - * - * @var array - */ - protected $defaultIncludes = []; - /** @var ParameterBag */ protected $parameters; @@ -66,50 +50,6 @@ class PiggyBankEventTransformer extends TransformerAbstract $this->parameters = $parameters; } - /** - * Include piggy bank into end result. - * - * @codeCoverageIgnore - * - * @param PiggyBankEvent $event - * - * @return Item - */ - public function includePiggyBank(PiggyBankEvent $event): Item - { - return $this->item($event->piggyBank, new PiggyBankTransformer($this->parameters), 'piggy_banks'); - } - - /** - * Include transaction into end result. - * - * @codeCoverageIgnore - * - * @param PiggyBankEvent $event - * - * @return Item - */ - public function includeTransaction(PiggyBankEvent $event): Item - { - $journal = $event->transactionJournal; - $pageSize = (int)app('preferences')->getForUser($journal->user, 'listPageSize', 50)->data; - - // journals always use collector and limited using URL parameters. - /** @var TransactionCollectorInterface $collector */ - $collector = app(TransactionCollectorInterface::class); - $collector->setUser($journal->user); - $collector->withOpposingAccount()->withCategoryInformation()->withCategoryInformation(); - $collector->setAllAssetAccounts(); - $collector->setJournals(new Collection([$journal])); - if (null !== $this->parameters->get('start') && null !== $this->parameters->get('end')) { - $collector->setRange($this->parameters->get('start'), $this->parameters->get('end')); - } - $collector->setLimit($pageSize)->setPage($this->parameters->get('page')); - $transactions = $collector->getTransactions(); - - return $this->item($transactions->first(), new TransactionTransformer($this->parameters), 'transactions'); - } - /** * Convert piggy bank event. * @@ -125,6 +65,8 @@ class PiggyBankEventTransformer extends TransformerAbstract $accountRepos->setUser($account->user); $currencyId = (int)$accountRepos->getMetaValue($account, 'currency_id'); + $journal = $event->transactionJournal; + $transactionId = null; $decimalPlaces = 2; if ($currencyId > 0) { /** @var CurrencyRepositoryInterface $repository */ @@ -134,13 +76,24 @@ class PiggyBankEventTransformer extends TransformerAbstract /** @noinspection NullPointerExceptionInspection */ $decimalPlaces = $currency->decimal_places; } + if (0 === $currencyId) { + $currency = app('amount')->getDefaultCurrencyByUser($account->user); + } + if (null !== $journal) { + $transactionId = $journal->transactions()->first()->id; + } $data = [ - 'id' => (int)$event->id, - 'updated_at' => $event->updated_at->toAtomString(), - 'created_at' => $event->created_at->toAtomString(), - 'amount' => round($event->amount, $decimalPlaces), - 'links' => [ + 'id' => (int)$event->id, + 'updated_at' => $event->updated_at->toAtomString(), + 'created_at' => $event->created_at->toAtomString(), + 'amount' => round($event->amount, $decimalPlaces), + 'currency_id' => $currency->id, + 'currency_code' => $currency->code, + 'currency_symbol' => $currency->symbol, + 'currency_dp' => $currency->decimal_places, + 'transaction_id' => $transactionId, + 'links' => [ [ 'rel' => 'self', 'uri' => '/piggy_bank_events/' . $event->id, diff --git a/routes/api.php b/routes/api.php index 37a33015f7..41f3073cc3 100644 --- a/routes/api.php +++ b/routes/api.php @@ -253,6 +253,8 @@ Route::group( Route::get('', ['uses' => 'TransactionController@index', 'as' => 'index']); Route::post('', ['uses' => 'TransactionController@store', 'as' => 'store']); Route::get('{transaction}', ['uses' => 'TransactionController@show', 'as' => 'show']); + Route::get('{transaction}/attachments', ['uses' => 'TransactionController@attachments', 'as' => 'attachments']); + Route::get('{transaction}/piggy_bank_events', ['uses' => 'TransactionController@piggyBankEvents', 'as' => 'piggy_bank_events']); Route::put('{transaction}', ['uses' => 'TransactionController@update', 'as' => 'update']); Route::delete('{transaction}', ['uses' => 'TransactionController@delete', 'as' => 'delete']); }