Skip to content

Firefly III: a personal finances manager

Make sure bills API is consistent.

James Cole2ef1022c9200
diff --git a/app/Api/V1/Controllers/BillController.php b/app/Api/V1/Controllers/BillController.php
index 515a661f9c..845ed637eb 100644
--- a/app/Api/V1/Controllers/BillController.php
+++ b/app/Api/V1/Controllers/BillController.php
@@ -130,11 +130,11 @@ class BillController extends Controller
$manager = new Manager();
- $manager->parseIncludes(['attachments', 'notes', 'transactionJournals', 'user']);
+ $manager->parseIncludes(['attachments', 'journals', 'user']);
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
$manager->setSerializer(new JsonApiSerializer($baseUrl));
- $resource = new Item($bill, new BillTransformer($start, $end), 'bill');
+ $resource = new Item($bill, new BillTransformer($start, $end), 'bills');
return Response::json($manager->createData($resource)->toArray());
}
@@ -155,18 +155,18 @@ class BillController extends Controller
$end = new Carbon($request->get('end'));
}
- $bill = $this->repository->store($request->getAll());
-
+ $bill = $this->repository->store($request->getAll());
$manager = new Manager();
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
$manager->setSerializer(new JsonApiSerializer($baseUrl));
- $resource = new Item($bill, new BillTransformer($start, $end), 'bill');
+ $resource = new Item($bill, new BillTransformer($start, $end), 'bills');
return Response::json($manager->createData($resource)->toArray());
}
+
/**
* @param BillRequest $request
* @param Bill $bill
@@ -186,11 +186,12 @@ class BillController extends Controller
if (null !== $request->get('end')) {
$end = new Carbon($request->get('end'));
}
+
$manager = new Manager();
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
$manager->setSerializer(new JsonApiSerializer($baseUrl));
- $resource = new Item($bill, new BillTransformer($start, $end), 'bill');
+ $resource = new Item($bill, new BillTransformer($start, $end), 'bills');
return Response::json($manager->createData($resource)->toArray());
diff --git a/app/Api/V1/Requests/BillRequest.php b/app/Api/V1/Requests/BillRequest.php
index 3b628d8fae..709722f971 100644
--- a/app/Api/V1/Requests/BillRequest.php
+++ b/app/Api/V1/Requests/BillRequest.php
@@ -44,18 +44,18 @@ class BillRequest extends Request
public function getAll(): array
{
$data = [
- 'name' => $this->string('name'),
- 'match' => $this->string('match'),
- 'amount_min' => $this->string('amount_min'),
- 'amount_max' => $this->string('amount_max'),
- 'currency_id' => $this->integer('currency_id'),
- 'currency_code' => $this->string('currency_code'),
- 'date' => $this->date('date'),
- 'repeat_freq' => $this->string('repeat_freq'),
- 'skip' => $this->integer('skip'),
- 'automatch' => $this->boolean('automatch'),
- 'active' => $this->boolean('active'),
- 'notes' => $this->string('notes'),
+ 'name' => $this->string('name'),
+ 'match' => $this->string('match'),
+ 'amount_min' => $this->string('amount_min'),
+ 'amount_max' => $this->string('amount_max'),
+ //'currency_id' => $this->integer('currency_id'),
+ //'currency_code' => $this->string('currency_code'),
+ 'date' => $this->date('date'),
+ 'repeat_freq' => $this->string('repeat_freq'),
+ 'skip' => $this->integer('skip'),
+ 'automatch' => $this->boolean('automatch'),
+ 'active' => $this->boolean('active'),
+ 'notes' => $this->string('notes'),
];
return $data;
@@ -67,18 +67,18 @@ class BillRequest extends Request
public function rules()
{
$rules = [
- 'name' => 'required|between:1,255|uniqueObjectForUser:bills,name',
- 'match' => 'required|between:1,255|uniqueObjectForUser:bills,match',
- 'amount_min' => 'required|numeric|more:0',
- 'amount_max' => 'required|numeric|more:0',
- 'currency_id' => 'numeric|exists:transaction_currencies,id|required_without:currency_code',
- 'currency_code' => 'min:3|max:3|exists:transaction_currencies,code|required_without:currency_id',
- 'date' => 'required|date',
- 'repeat_freq' => 'required|in:weekly,monthly,quarterly,half-year,yearly',
- 'skip' => 'required|between:0,31',
- 'automatch' => 'required|boolean',
- 'active' => 'required|boolean',
- 'notes' => 'between:1,65536',
+ 'name' => 'required|between:1,255|uniqueObjectForUser:bills,name',
+ 'match' => 'required|between:1,255|uniqueObjectForUser:bills,match',
+ 'amount_min' => 'required|numeric|more:0',
+ 'amount_max' => 'required|numeric|more:0',
+ //'currency_id' => 'numeric|exists:transaction_currencies,id|required_without:currency_code',
+ //'currency_code' => 'min:3|max:3|exists:transaction_currencies,code|required_without:currency_id',
+ 'date' => 'required|date',
+ 'repeat_freq' => 'required|in:weekly,monthly,quarterly,half-year,yearly',
+ 'skip' => 'required|between:0,31',
+ 'automatch' => 'required|boolean',
+ 'active' => 'required|boolean',
+ 'notes' => 'between:1,65536',
];
switch ($this->method()) {
default:
diff --git a/app/Transformers/AttachmentTransformer.php b/app/Transformers/AttachmentTransformer.php
index dc44ce0301..5949241dd9 100644
--- a/app/Transformers/AttachmentTransformer.php
+++ b/app/Transformers/AttachmentTransformer.php
@@ -67,6 +67,8 @@ class AttachmentTransformer extends TransformerAbstract
{
return [
'id' => (int)$attachment->id,
+ 'updated_at' => $attachment->updated_at->toAtomString(),
+ 'created_at' => $attachment->created_at->toAtomString(),
'attachable_type' => $attachment->attachable_type,
'md5' => $attachment->md5,
'filename' => $attachment->filename,
diff --git a/app/Transformers/BillTransformer.php b/app/Transformers/BillTransformer.php
index 6f1eb4e33f..cbffb72c52 100644
--- a/app/Transformers/BillTransformer.php
+++ b/app/Transformers/BillTransformer.php
@@ -25,11 +25,12 @@ namespace FireflyIII\Transformers;
use Carbon\Carbon;
use FireflyIII\Models\Bill;
+use FireflyIII\Models\Note;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use Illuminate\Support\Collection;
+use League\Fractal\Resource\Collection as FractalCollection;
use League\Fractal\Resource\Item;
use League\Fractal\TransformerAbstract;
-use League\Fractal\Resource\Collection as FractalCollection;
/**
* Class BillTransformer
@@ -41,13 +42,13 @@ class BillTransformer extends TransformerAbstract
*
* @var array
*/
- protected $availableIncludes = ['attachments', 'notes', 'transactionJournals', 'user'];
+ protected $availableIncludes = ['attachments', 'journals', 'user'];
/**
* List of resources to automatically include
*
* @var array
*/
- protected $defaultIncludes = ['notes',];
+ protected $defaultIncludes = [];
/** @var Carbon */
private $end = null;
/** @var Carbon */
@@ -65,18 +66,6 @@ class BillTransformer extends TransformerAbstract
$this->end = $end;
}
- /**
- * @param Bill $bill
- *
- * @return \League\Fractal\Resource\Item
- */
- public function includeUser(Bill $bill): Item
- {
- $user = $bill->user()->first();
-
- return $this->item($user, new UserTransformer, 'user');
- }
-
/**
* @param Bill $bill
*
@@ -86,7 +75,7 @@ class BillTransformer extends TransformerAbstract
{
$attachments = $bill->attachments()->get();
- return $this->collection($attachments, new AttachmentTransformer, 'attachment');
+ return $this->collection($attachments, new AttachmentTransformer, 'attachments');
}
/**
@@ -94,23 +83,23 @@ class BillTransformer extends TransformerAbstract
*
* @return FractalCollection
*/
- public function includeNotes(Bill $bill): FractalCollection
+ public function includeJournals(Bill $bill): FractalCollection
{
- $notes = $bill->notes()->get();
+ $journals = $bill->transactionJournals()->get();
- return $this->collection($notes, new NoteTransformer, 'note');
+ return $this->collection($journals, new TransactionJournalTransformer, 'journals');
}
/**
* @param Bill $bill
*
- * @return FractalCollection
+ * @return \League\Fractal\Resource\Item
*/
- public function includeTransactionJournals(Bill $bill): FractalCollection
+ public function includeUser(Bill $bill): Item
{
- $journals = $bill->transactionJournals()->get();
+ $user = $bill->user()->first();
- return $this->collection($journals, new JournalTransformer, 'transaction_journal');
+ return $this->item($user, new UserTransformer, 'users');
}
/**
@@ -124,6 +113,8 @@ class BillTransformer extends TransformerAbstract
$payDates = $this->payDates($bill);
$data = [
'id' => (int)$bill->id,
+ 'updated_at' => $bill->updated_at->toAtomString(),
+ 'created_at' => $bill->created_at->toAtomString(),
'name' => $bill->name,
'match' => explode(',', $bill->match),
'amount_min' => round($bill->amount_min, 2),
@@ -137,15 +128,19 @@ class BillTransformer extends TransformerAbstract
'pay_dates' => $payDates,
'paid_dates' => $paidData['paid_dates'],
'next_expected_match' => $paidData['next_expected_match'],
+ 'notes' => null,
'links' => [
[
'rel' => 'self',
- 'uri' => '/bill/' . $bill->id,
+ 'uri' => '/bills/' . $bill->id,
],
],
];
-
- // todo updated at, created at
+ /** @var Note $note */
+ $note = $bill->notes()->first();
+ if (!is_null($note)) {
+ $data['notes'] = $note->text;
+ }
return $data;
diff --git a/app/Transformers/NoteTransformer.php b/app/Transformers/NoteTransformer.php
deleted file mode 100644
index 0a89959600..0000000000
--- a/app/Transformers/NoteTransformer.php
+++ /dev/null
@@ -1,60 +0,0 @@
-<?php
-/**
- * NoteTransformer.php
- * Copyright (c) 2018 [email protected]
- *
- * This file is part of Firefly III.
- *
- * Firefly III is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * Firefly III 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
- */
-
-declare(strict_types=1);
-
-namespace FireflyIII\Transformers;
-
-
-use FireflyIII\Models\Note;
-use League\CommonMark\CommonMarkConverter;
-use League\Fractal\TransformerAbstract;
-
-/**
- * Class NoteTransformer
- */
-class NoteTransformer extends TransformerAbstract
-{
- /**
- * @param Note $note
- *
- * @return array
- */
- public function transform(Note $note): array
- {
- $converter = new CommonMarkConverter;
-
- return [
- 'id' => (int)$note->id,
- 'notable_type' => $note->noteable_type,
- 'title' => $note->title,
- 'text' => $note->text,
- 'markdown' => $converter->convertToHtml($note->text),
- 'links' => [
- [
- 'rel' => 'self',
- 'uri' => '/note/' . $note->id,
- ],
- ],
- ];
- }
-
-}
\ No newline at end of file
diff --git a/app/Transformers/JournalTransformer.php b/app/Transformers/TransactionJournalTransformer.php
similarity index 68%
rename from app/Transformers/JournalTransformer.php
rename to app/Transformers/TransactionJournalTransformer.php
index ad4cd83eb5..0326ba7e67 100644
--- a/app/Transformers/JournalTransformer.php
+++ b/app/Transformers/TransactionJournalTransformer.php
@@ -24,15 +24,15 @@ declare(strict_types=1);
namespace FireflyIII\Transformers;
+use FireflyIII\Models\Note;
use FireflyIII\Models\TransactionJournal;
-use FireflyIII\Repositories\Journal\JournalTaskerInterface;
use League\Fractal\Resource\Collection as FractalCollection;
use League\Fractal\TransformerAbstract;
/**
- * Class JournalTransformer
+ * Class TransactionJournalTransformer
*/
-class JournalTransformer extends TransformerAbstract
+class TransactionJournalTransformer extends TransformerAbstract
{
/**
@@ -40,13 +40,13 @@ class JournalTransformer extends TransformerAbstract
*
* @var array
*/
- protected $availableIncludes = ['attachments', 'notes', 'transactions', 'user', 'tags', 'budget', 'category', 'bill', 'meta', 'piggy_bank_events'];
+ protected $availableIncludes = ['attachments', 'transactions', 'user', 'tags', 'budget', 'category', 'bill', 'meta', 'piggy_bank_events'];
/**
* List of resources to automatically include
*
* @var array
*/
- protected $defaultIncludes = ['transactions',];
+ protected $defaultIncludes = ['transactions'];
/**
* @param TransactionJournal $journal
@@ -55,13 +55,12 @@ class JournalTransformer extends TransformerAbstract
*/
public function includeTransactions(TransactionJournal $journal): FractalCollection
{
- $tasker = app(JournalTaskerInterface::class);
- $tasker->setUser($journal->user);
- $transactions = $tasker->getTransactionsOverview($journal);
+ $set = $journal->transactions()->where('amount', '<', 0)->get(['transactions.*']);
- return $this->collection($transactions, new TransactionTransformer, 'transaction');
+ return $this->collection($set, new TransactionTransformer, 'transactions');
}
+
/**
* @param TransactionJournal $journal
*
@@ -69,20 +68,30 @@ class JournalTransformer extends TransformerAbstract
*/
public function transform(TransactionJournal $journal): array
{
- return [
+ $data = [
'id' => (int)$journal->id,
+ 'updated_at' => $journal->updated_at->toAtomString(),
+ 'created_at' => $journal->created_at->toAtomString(),
'type' => $journal->transactionType->type,
'description' => $journal->description,
'date' => $journal->date->format('Y-m-d'),
'order' => $journal->order,
'completed' => $journal->completed,
+ 'notes' => null,
'links' => [
[
'rel' => 'self',
- 'uri' => '/transaction_journal/' . $journal->id,
+ 'uri' => '/journals/' . $journal->id,
],
],
];
+ /** @var Note $note */
+ $note = $journal->notes()->first();
+ if (!is_null($note)) {
+ $data['notes'] = $note->text;
+ }
+
+ return $data;
}
}
\ No newline at end of file
diff --git a/app/Transformers/TransactionTransformer.php b/app/Transformers/TransactionTransformer.php
index 803d9e2aff..e96f3df391 100644
--- a/app/Transformers/TransactionTransformer.php
+++ b/app/Transformers/TransactionTransformer.php
@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Transformers;
+use FireflyIII\Models\Transaction;
use League\Fractal\TransformerAbstract;
/**
@@ -32,37 +33,41 @@ use League\Fractal\TransformerAbstract;
class TransactionTransformer extends TransformerAbstract
{
/**
- * @param array $original
+ * @param Transaction $transaction
*
* @return array
*/
- public function transform(array $original): array
+ public function transform(Transaction $transaction): array
{
- $id = $original['source_id'];
- $foreignAmount = null;
- if (!is_null($original['foreign_source_amount'])) {
- $foreignAmount = round($original['foreign_source_amount'], $original['foreign_currency_dp']);
- }
- $return = [
- 'id' => $id,
- 'amount' => round($original['source_amount'], $original['transaction_currency_dp']),
- 'currency_id' => $original['transaction_currency_id'],
- 'currency_code' => $original['transaction_currency_code'],
- 'foreign_amount' => $foreignAmount,
- 'foreign_currency_id' => $original['foreign_currency_id'],
- 'foreign_currency_code' => $original['foreign_currency_code'],
- 'description' => $original['description'],
- 'links' => [
+ $opposing = Transaction
+ ::where('transaction_journal_id', $transaction->transaction_journal_id)
+ ->where('identifier', $transaction->identifier)
+ ->where('amount', $transaction->amount * -1)
+ ->whereNull('deleted_at')
+ ->first(['transactions.*']);
+
+ $data = [
+ 'id' => (int)$transaction->id,
+ 'updated_at' => $transaction->updated_at->toAtomString(),
+ 'created_at' => $transaction->created_at->toAtomString(),
+ 'source_id' => (int)$transaction->account_id,
+ 'destination_id' => $opposing->account_id,
+ 'description' => $transaction->description,
+ 'currency_id' => (int)$transaction->transaction_currency_id,
+ 'amount' => (float)$transaction->amount,
+ 'foreign_currency_id' => is_null($transaction->foreign_currency_id) ? null : (int)$transaction->foreign_currency_id,
+ 'foreign_amount' => is_null($transaction->foreign_amount) ? null : (float)$transaction->foreign_amount,
+ 'identifier' => (int)$transaction->identifier,
+
+ 'links' => [
[
'rel' => 'self',
- 'uri' => '/transaction/' . $id,
+ 'uri' => '/transactions/' . $transaction->id,
],
],
];
- // todo source account, dest account, budget, category
-
- return $return;
+ return $data;
}
}
\ No newline at end of file
diff --git a/app/Transformers/UserTransformer.php b/app/Transformers/UserTransformer.php
index 5e658521b3..c02983d2cb 100644
--- a/app/Transformers/UserTransformer.php
+++ b/app/Transformers/UserTransformer.php
@@ -41,11 +41,16 @@ class UserTransformer extends TransformerAbstract
{
return [
- 'id' => (int)$user->id,
- 'links' => [
+ 'id' => (int)$user->id,
+ 'updated_at' => $user->updated_at->toAtomString(),
+ 'created_at' => $user->created_at->toAtomString(),
+ 'email' => $user->email,
+ 'blocked' => intval($user->blocked) === 1,
+ 'blocked_code' => $user->blocked_code,
+ 'links' => [
[
'rel' => 'self',
- 'uri' => '/user/' . $user->id,
+ 'uri' => '/users/' . $user->id,
],
],
];
diff --git a/routes/api.php b/routes/api.php
index fa9436aa93..e3e7ef16f2 100644
--- a/routes/api.php
+++ b/routes/api.php
@@ -20,7 +20,7 @@
*/
Route::group(
- ['middleware' => ['auth:api','bindings'], 'namespace' => 'FireflyIII\Api\V1\Controllers', 'prefix' => 'bill', 'as' => 'api.v1.bills.'], function () {
+ ['middleware' => ['auth:api','bindings'], 'namespace' => 'FireflyIII\Api\V1\Controllers', 'prefix' => 'bills', 'as' => 'api.v1.bills.'], function () {
// Bills API routes:
Route::get('', ['uses' => 'BillController@index', 'as' => 'index']);