Expand API for attachments.

This commit is contained in:
James Cole
2018-06-24 06:51:22 +02:00
parent 32b6ded008
commit ad6a9a7df7
28 changed files with 1290 additions and 59 deletions

View File

@@ -26,7 +26,6 @@ namespace FireflyIII\Transformers;
use Carbon\Carbon;
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
use FireflyIII\Models\Bill;
use FireflyIII\Models\Note;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use Illuminate\Support\Collection;
use League\Fractal\Resource\Collection as FractalCollection;
@@ -45,13 +44,13 @@ class BillTransformer extends TransformerAbstract
*
* @var array
*/
protected $availableIncludes = ['attachments', 'transactions', 'user'];
protected $availableIncludes = ['attachments', 'transactions', 'user', 'notes', 'rules'];
/**
* List of resources to automatically include
*
* @var array
*/
protected $defaultIncludes = [];
protected $defaultIncludes = ['notes', 'rules'];
/** @var ParameterBag */
protected $parameters;
@@ -83,6 +82,40 @@ class BillTransformer extends TransformerAbstract
return $this->collection($attachments, new AttachmentTransformer($this->parameters), 'attachments');
}
/**
* Attach the notes.
*
* @codeCoverageIgnore
*
* @param Bill $bill
*
* @return FractalCollection
*/
public function includeNotes(Bill $bill): FractalCollection
{
return $this->collection($bill->notes, new NoteTransformer($this->parameters), 'notes');
}
/**
* Attach the rules.
*
* @codeCoverageIgnore
*
* @param Bill $bill
*
* @return FractalCollection
*/
public function includeRules(Bill $bill): FractalCollection
{
/** @var BillRepositoryInterface $repository */
$repository = app(BillRepositoryInterface::class);
$repository->setUser($bill->user);
// add info about rules:
$rules = $repository->getRulesForBill($bill);
return $this->collection($rules, new RuleTransformer($this->parameters), 'rules');
}
/**
* Include any transactions.
*
@@ -141,19 +174,17 @@ class BillTransformer extends TransformerAbstract
'name' => $bill->name,
'currency_id' => $bill->transaction_currency_id,
'currency_code' => $bill->transactionCurrency->code,
'match' => explode(',', $bill->match),
'amount_min' => round($bill->amount_min, 2),
'amount_max' => round($bill->amount_max, 2),
'date' => $bill->date->format('Y-m-d'),
'repeat_freq' => $bill->repeat_freq,
'skip' => (int)$bill->skip,
'automatch' => (int)$bill->automatch === 1,
'active' => (int)$bill->active === 1,
'automatch' => $bill->automatch,
'active' => $bill->active,
'attachments_count' => $bill->attachments()->count(),
'pay_dates' => $payDates,
'paid_dates' => $paidData['paid_dates'],
'next_expected_match' => $paidData['next_expected_match'],
'notes' => null,
'links' => [
[
'rel' => 'self',
@@ -161,11 +192,6 @@ class BillTransformer extends TransformerAbstract
],
],
];
/** @var Note $note */
$note = $bill->notes()->first();
if (null !== $note) {
$data['notes'] = $note->text;
}
return $data;