diff --git a/app/Api/V1/Requests/AttachmentRequest.php b/app/Api/V1/Requests/AttachmentRequest.php index 65e4d38d93..a5f51fe9e7 100644 --- a/app/Api/V1/Requests/AttachmentRequest.php +++ b/app/Api/V1/Requests/AttachmentRequest.php @@ -25,6 +25,7 @@ namespace FireflyIII\Api\V1\Requests; use FireflyIII\Models\Bill; use FireflyIII\Models\ImportJob; +use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionJournal; use FireflyIII\Rules\IsValidAttachmentModel; @@ -72,7 +73,7 @@ class AttachmentRequest extends Request str_replace('FireflyIII\\Models\\', '', Bill::class), str_replace('FireflyIII\\Models\\', '', ImportJob::class), str_replace('FireflyIII\\Models\\', '', TransactionJournal::class), - str_replace('FireflyIII\\Models\\', '', ImportJob::class), + str_replace('FireflyIII\\Models\\', '', Transaction::class), ] ); $model = $this->string('model'); diff --git a/app/Factory/AttachmentFactory.php b/app/Factory/AttachmentFactory.php index f8caf4e580..4ebefc9f94 100644 --- a/app/Factory/AttachmentFactory.php +++ b/app/Factory/AttachmentFactory.php @@ -23,8 +23,11 @@ declare(strict_types=1); namespace FireflyIII\Factory; +use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\Attachment; use FireflyIII\Models\Note; +use FireflyIII\Models\Transaction; +use FireflyIII\Models\TransactionJournal; use FireflyIII\User; use Log; @@ -33,6 +36,9 @@ use Log; */ class AttachmentFactory { + /** @var User */ + private $user; + /** * Constructor. */ @@ -42,22 +48,34 @@ class AttachmentFactory Log::warning(sprintf('%s should not be instantiated in the TEST environment!', \get_class($this))); } } - /** @var User */ - private $user; /** * @param array $data * * @return Attachment|null + * @throws FireflyException */ public function create(array $data): ?Attachment { + // append if necessary. + $model = false === strpos('FireflyIII', $data['model']) ? 'FireflyIII\\Models\\' . $data['model'] : $data['model']; + + if (Transaction::class === $model) { + /** @var Transaction $transaction */ + $transaction = $this->user->transactions()->find((int)$data['model_id']); + if (null === $transaction) { + throw new FireflyException('Unexpectedly could not find transaction'); + } + $data['model_id'] = $transaction->transaction_journal_id; + $model = TransactionJournal::class; + } + // create attachment: $attachment = Attachment::create( [ 'user_id' => $this->user->id, 'attachable_id' => $data['model_id'], - 'attachable_type' => $data['model'], + 'attachable_type' => $model, 'md5' => '', 'filename' => $data['filename'], 'title' => '' === $data['title'] ? null : $data['title'], diff --git a/app/Http/Controllers/BillController.php b/app/Http/Controllers/BillController.php index dd41c0304a..cfd7a7c7e2 100644 --- a/app/Http/Controllers/BillController.php +++ b/app/Http/Controllers/BillController.php @@ -252,8 +252,8 @@ class BillController extends Controller // simply fire off all rules? /** @var TransactionMatcher $matcher */ $matcher = app(TransactionMatcher::class); - $matcher->setLimit(100000); // large upper limit - $matcher->setRange(100000); // large upper limit + $matcher->setSearchLimit(100000); // large upper limit + $matcher->setTriggeredLimit(100000); // large upper limit $matcher->setRule($rule); $matchingTransactions = $matcher->findTransactionsByRule(); $total += $matchingTransactions->count(); diff --git a/app/Repositories/ImportJob/ImportJobRepository.php b/app/Repositories/ImportJob/ImportJobRepository.php index 18983bc8e1..da2780ffac 100644 --- a/app/Repositories/ImportJob/ImportJobRepository.php +++ b/app/Repositories/ImportJob/ImportJobRepository.php @@ -154,6 +154,16 @@ class ImportJobRepository implements ImportJobRepositoryInterface throw new FireflyException('Could not create an import job with a unique key after 30 tries.'); } + /** + * @param int $jobId + * + * @return ImportJob|null + */ + public function find(int $jobId): ?ImportJob + { + return $this->user->importJobs()->find($jobId); + } + /** * @param string $key * diff --git a/app/Repositories/ImportJob/ImportJobRepositoryInterface.php b/app/Repositories/ImportJob/ImportJobRepositoryInterface.php index e1cec397cb..8f33c31ed6 100644 --- a/app/Repositories/ImportJob/ImportJobRepositoryInterface.php +++ b/app/Repositories/ImportJob/ImportJobRepositoryInterface.php @@ -69,6 +69,13 @@ interface ImportJobRepositoryInterface */ public function create(string $importProvider): ImportJob; + /** + * @param int $jobId + * + * @return ImportJob|null + */ + public function find(int $jobId): ?ImportJob; + /** * @param string $key * diff --git a/app/Rules/IsValidAttachmentModel.php b/app/Rules/IsValidAttachmentModel.php index f34da76724..ce6828bfa6 100644 --- a/app/Rules/IsValidAttachmentModel.php +++ b/app/Rules/IsValidAttachmentModel.php @@ -24,8 +24,14 @@ declare(strict_types=1); namespace FireflyIII\Rules; +use FireflyIII\Models\Bill; +use FireflyIII\Models\ImportJob; +use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionJournal; +use FireflyIII\Repositories\Bill\BillRepositoryInterface; +use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface; use FireflyIII\Repositories\Journal\JournalRepositoryInterface; +use FireflyIII\User; use Illuminate\Contracts\Validation\Rule; /** @@ -71,7 +77,40 @@ class IsValidAttachmentModel implements Rule if (!auth()->check()) { return false; } + $model = false === strpos('FireflyIII', $this->model) ? 'FireflyIII\\Models\\' . $this->model : $this->model; + if (Bill::class === $model) { + /** @var BillRepositoryInterface $repository */ + $repository = app(BillRepositoryInterface::class); + /** @var User $user */ + $user = auth()->user(); + $repository->setUser($user); + $bill = $repository->find((int)$value); + + return null !== $bill; + } + + if (ImportJob::class === $model) { + /** @var ImportJobRepositoryInterface $repository */ + $repository = app(ImportJobRepositoryInterface::class); + /** @var User $user */ + $user = auth()->user(); + $repository->setUser($user); + $importJob = $repository->find((int)$value); + + return null !== $importJob; + } + + if (Transaction::class === $model) { + /** @var JournalRepositoryInterface $repository */ + $repository = app(JournalRepositoryInterface::class); + /** @var User $user */ + $user = auth()->user(); + $repository->setUser($user); + $transaction = $repository->findTransaction((int)$value); + + return null !== $transaction; + } if (TransactionJournal::class === $this->model) { $repository = app(JournalRepositoryInterface::class); diff --git a/app/Transformers/TransactionLinkTransformer.php b/app/Transformers/TransactionLinkTransformer.php index 766e83417e..656083ea76 100644 --- a/app/Transformers/TransactionLinkTransformer.php +++ b/app/Transformers/TransactionLinkTransformer.php @@ -65,7 +65,7 @@ class TransactionLinkTransformer extends AbstractTransformer 'updated_at' => $link->updated_at->toAtomString(), 'inward_id' => $link->source_id, 'outward_id' => $link->destination_id, - 'notes' => $notes, + 'notes' => '' === $notes ? null : $notes, 'links' => [ [ 'rel' => 'self', diff --git a/routes/web.php b/routes/web.php index fe38fa3dab..ec19b2da72 100755 --- a/routes/web.php +++ b/routes/web.php @@ -35,7 +35,7 @@ Route::group( Route::group( ['middleware' => 'binders-only','namespace' => 'FireflyIII\Http\Controllers\System', 'as' => 'cron.', 'prefix' => 'cron'], function () { - Route::get('run/{cliToken}', ['uses' => 'CronController@cron', 'as' => 'cron']); + Route::get('run/{cliToken}', ['uses' => 'CronController@cron', 'as' => 'cron']); } );