From 610bc108e76914d76d51e14b6ce2bd5d738e3897 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 12 Aug 2023 20:57:51 +0200 Subject: [PATCH] Add some slack notifications and a todo to fix the rest --- .../Model/Rule/RuleActionFailedOnArray.php | 52 ++++++++ app/Handlers/Events/Model/RuleHandler.php | 60 +++++++++ .../Admin/VersionCheckResult.php | 19 +-- app/Notifications/User/RuleActionFailed.php | 116 ++++++++++++++++++ app/Providers/EventServiceProvider.php | 6 + app/TransactionRules/Actions/AddTag.php | 5 +- .../Actions/AppendDescriptionToNotes.php | 2 + .../Actions/AppendNotesToDescription.php | 3 + app/TransactionRules/Actions/ClearBudget.php | 2 + .../Actions/ClearCategory.php | 2 + app/TransactionRules/Actions/ClearNotes.php | 2 + .../Actions/ConvertToDeposit.php | 9 +- .../Actions/ConvertToTransfer.php | 12 +- .../Actions/ConvertToWithdrawal.php | 8 +- app/TransactionRules/Actions/LinkToBill.php | 3 +- .../Actions/MoveDescriptionToNotes.php | 1 + .../Actions/MoveNotesToDescription.php | 3 + .../Actions/RemoveAllTags.php | 1 + app/TransactionRules/Actions/RemoveTag.php | 2 + app/TransactionRules/Actions/SetBudget.php | 4 +- app/TransactionRules/Actions/SetCategory.php | 4 +- .../Actions/SetDestinationAccount.php | 10 +- .../Actions/SetSourceAccount.php | 10 +- .../Actions/SwitchAccounts.php | 6 +- .../Actions/UpdatePiggybank.php | 4 +- resources/lang/en_US/rules.php | 41 +++++++ 26 files changed, 346 insertions(+), 41 deletions(-) create mode 100644 app/Events/Model/Rule/RuleActionFailedOnArray.php create mode 100644 app/Handlers/Events/Model/RuleHandler.php create mode 100644 app/Notifications/User/RuleActionFailed.php create mode 100644 resources/lang/en_US/rules.php diff --git a/app/Events/Model/Rule/RuleActionFailedOnArray.php b/app/Events/Model/Rule/RuleActionFailedOnArray.php new file mode 100644 index 0000000000..dc7cc0cb55 --- /dev/null +++ b/app/Events/Model/Rule/RuleActionFailedOnArray.php @@ -0,0 +1,52 @@ +. + */ + +namespace FireflyIII\Events\Model\Rule; + +use FireflyIII\Models\RuleAction; +use Illuminate\Queue\SerializesModels; + +/** + * Class RuleActionFailedOnArray + */ +class RuleActionFailedOnArray +{ + use SerializesModels; + + public string $error; + public array $journal; + public RuleAction $ruleAction; + + /** + * @param RuleAction $ruleAction + * @param array $journal + * @param string $error + */ + public function __construct(RuleAction $ruleAction, array $journal, string $error) + { + app('log')->debug('Created new RuleActionFailedOnArray'); + $this->ruleAction = $ruleAction; + $this->journal = $journal; + $this->error = $error; + } +} diff --git a/app/Handlers/Events/Model/RuleHandler.php b/app/Handlers/Events/Model/RuleHandler.php new file mode 100644 index 0000000000..60f33d64e2 --- /dev/null +++ b/app/Handlers/Events/Model/RuleHandler.php @@ -0,0 +1,60 @@ +. + */ + +namespace FireflyIII\Handlers\Events\Model; + +use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray; +use FireflyIII\Notifications\User\RuleActionFailed; +use Illuminate\Support\Facades\Notification; + +/** + * Class RuleHandler + */ +class RuleHandler +{ + /** + * @param RuleActionFailedOnArray $event + * + * @return void + */ + public function ruleActionFailed(RuleActionFailedOnArray $event): void + { + app('log')->debug('Now in ruleActionFailed'); + $ruleAction = $event->ruleAction; + $rule = $ruleAction->rule; + $journal = $event->journal; + $error = $event->error; + $user = $ruleAction->rule->user; + + $mainMessage = trans('rules.main_message', ['rule' => $rule->title, 'action' => $ruleAction->action_type, 'group' => $journal['transaction_group_id'], 'error' => $error]); + $groupTitle = $journal['description'] ?? ''; + $groupLink = route('transactions.show', [$journal['transaction_group_id']]); + $ruleTitle = $rule->title; + $ruleLink = route('rules.edit', [$rule->id]); + $params = [$mainMessage, $groupTitle, $groupLink, $ruleTitle, $ruleLink]; + + + Notification::send($user, new RuleActionFailed($params)); + } + +} diff --git a/app/Notifications/Admin/VersionCheckResult.php b/app/Notifications/Admin/VersionCheckResult.php index d6f29ceecd..75b36ffb0e 100644 --- a/app/Notifications/Admin/VersionCheckResult.php +++ b/app/Notifications/Admin/VersionCheckResult.php @@ -87,21 +87,10 @@ class VersionCheckResult extends Notification */ public function toSlack($notifiable) { - // return (new SlackMessage())->text($this->message) - // ->sectionBlock(function (SectionBlock $block) { - // $button = new ButtonElement('Button'); - // $button->url('https://github.com/firefly-iii/firefly-iii/releases'); - // $block->accessory($button); - // }); - //// ->attachment(function ($attachment) { - //// $attachment->title('Firefly III @ GitHub', 'https://github.com/firefly-iii/firefly-iii/releases'); - //// }); - - return (new SlackMessage())->content($this->message) - ->attachment(function ($attachment) { - $attachment->title('Firefly III @ GitHub', 'https://github.com/firefly-iii/firefly-iii/releases'); - }); + ->attachment(function ($attachment) { + $attachment->title('Firefly III @ GitHub', 'https://github.com/firefly-iii/firefly-iii/releases'); + }); } /** @@ -114,7 +103,7 @@ class VersionCheckResult extends Notification public function via($notifiable) { /** @var User|null $user */ - $user = auth()->user(); + $user = auth()->user(); $slackUrl = null === $user ? '' : (string)app('preferences')->getForUser(auth()->user(), 'slack_webhook_url', '')->data; if (str_starts_with($slackUrl, 'https://hooks.slack.com/services/')) { return ['mail', 'slack']; diff --git a/app/Notifications/User/RuleActionFailed.php b/app/Notifications/User/RuleActionFailed.php new file mode 100644 index 0000000000..ad2f12bcfe --- /dev/null +++ b/app/Notifications/User/RuleActionFailed.php @@ -0,0 +1,116 @@ +. + */ + +declare(strict_types=1); + +namespace FireflyIII\Notifications\User; + +use FireflyIII\User; +use Illuminate\Bus\Queueable; +use Illuminate\Notifications\Messages\SlackMessage; +use Illuminate\Notifications\Notification; + +/** + * Class RuleActionFailed + */ +class RuleActionFailed extends Notification +{ + use Queueable; + + private string $groupLink; + private string $groupTitle; + private string $message; + private string $ruleLink; + private string $ruleTitle; + + /** + * Create a new notification instance. + * + * @return void + */ + public function __construct(array $params) + { + [$mainMessage, $groupTitle, $groupLink, $ruleTitle, $ruleLink] = $params; + $this->message = $mainMessage; + $this->groupTitle = $groupTitle; + $this->groupLink = $groupLink; + $this->ruleTitle = $ruleTitle; + $this->ruleLink = $ruleLink; + + + } + + /** + * Get the array representation of the notification. + * + * @param mixed $notifiable + * + * @return array + */ + public function toArray($notifiable) + { + return [ + // + ]; + } + + /** + * Get the Slack representation of the notification. + * + * @param mixed $notifiable + * + * @return SlackMessage + */ + public function toSlack($notifiable) + { + $groupTitle = $this->groupTitle; + $groupLink = $this->groupLink; + $ruleTitle = $this->ruleTitle; + $ruleLink = $this->ruleLink; + + return (new SlackMessage())->content($this->message)->attachment(function ($attachment) use ($groupTitle, $groupLink) { + $attachment->title((string)trans('rules.inspect_transaction', ['title' => $groupTitle]), $groupLink); + })->attachment(function ($attachment) use ($ruleTitle, $ruleLink) { + $attachment->title((string)trans('rules.inspect_rule', ['title' => $ruleTitle]), $ruleLink); + }); + } + + /** + * Get the notification's delivery channels. + * + * @param mixed $notifiable + * + * @return array + */ + public function via($notifiable) + { + /** @var User|null $user */ + $user = auth()->user(); + $slackUrl = null === $user ? '' : (string)app('preferences')->getForUser(auth()->user(), 'slack_webhook_url', '')->data; + if (str_starts_with($slackUrl, 'https://hooks.slack.com/services/')) { + app('log')->debug('Will send ruleActionFailed through Slack!'); + return ['slack']; + } + app('log')->debug('Will NOT send ruleActionFailed through Slack'); + return []; + } +} diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index 0ef77cded6..8ed72a2bd7 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -32,6 +32,7 @@ use FireflyIII\Events\DetectedNewIPAddress; use FireflyIII\Events\Model\BudgetLimit\Created; use FireflyIII\Events\Model\BudgetLimit\Deleted; use FireflyIII\Events\Model\BudgetLimit\Updated; +use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray; use FireflyIII\Events\NewVersionAvailable; use FireflyIII\Events\RegisteredUser; use FireflyIII\Events\RequestedNewPassword; @@ -170,6 +171,11 @@ class EventServiceProvider extends ServiceProvider 'FireflyIII\Handlers\Events\Model\BudgetLimitHandler@deleted', ], + // rule actions + RuleActionFailedOnArray::class => [ + 'FireflyIII\Handlers\Events\Model\RuleHandler@ruleActionFailed', + ], + ]; /** diff --git a/app/TransactionRules/Actions/AddTag.php b/app/TransactionRules/Actions/AddTag.php index 44ffabbafe..057473463c 100644 --- a/app/TransactionRules/Actions/AddTag.php +++ b/app/TransactionRules/Actions/AddTag.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace FireflyIII\TransactionRules\Actions; use DB; +use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray; use FireflyIII\Events\TriggeredAuditLog; use FireflyIII\Factory\TagFactory; use FireflyIII\Models\RuleAction; @@ -61,7 +62,7 @@ class AddTag implements ActionInterface if (null === $tag) { // could not find, could not create tag. - Log::error(sprintf('RuleAction AddTag. Could not find or create tag "%s"', $this->action->action_value)); + event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.find_or_create_tag_failed', ['tag' => $this->action->action_value]))); return false; } @@ -78,12 +79,12 @@ class AddTag implements ActionInterface // event for audit log entry event(new TriggeredAuditLog($this->action->rule, $object, 'add_tag', null, $tag->tag)); - return true; } Log::debug( sprintf('RuleAction AddTag fired but tag %d ("%s") was already added to journal %d.', $tag->id, $tag->tag, $journal['transaction_journal_id']) ); + event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.tag_already_added', ['tag' => $this->action->action_value]))); return false; } diff --git a/app/TransactionRules/Actions/AppendDescriptionToNotes.php b/app/TransactionRules/Actions/AppendDescriptionToNotes.php index f5c0280e48..a10e442140 100644 --- a/app/TransactionRules/Actions/AppendDescriptionToNotes.php +++ b/app/TransactionRules/Actions/AppendDescriptionToNotes.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace FireflyIII\TransactionRules\Actions; +use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray; use FireflyIII\Events\TriggeredAuditLog; use FireflyIII\Models\Note; use FireflyIII\Models\RuleAction; @@ -56,6 +57,7 @@ class AppendDescriptionToNotes implements ActionInterface $object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']); if (null === $object) { Log::error(sprintf('No journal #%d belongs to user #%d.', $journal['transaction_journal_id'], $journal['user_id'])); + event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.journal_other_user'))); return false; } $note = $object->notes()->first(); diff --git a/app/TransactionRules/Actions/AppendNotesToDescription.php b/app/TransactionRules/Actions/AppendNotesToDescription.php index 20a48901d7..0762b11850 100644 --- a/app/TransactionRules/Actions/AppendNotesToDescription.php +++ b/app/TransactionRules/Actions/AppendNotesToDescription.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace FireflyIII\TransactionRules\Actions; +use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray; use FireflyIII\Events\TriggeredAuditLog; use FireflyIII\Models\Note; use FireflyIII\Models\RuleAction; @@ -60,6 +61,7 @@ class AppendNotesToDescription implements ActionInterface $object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']); if (null === $object) { Log::error(sprintf('No journal #%d belongs to user #%d.', $journal['transaction_journal_id'], $journal['user_id'])); + event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.journal_other_user'))); return false; } $note = $object->notes()->first(); @@ -80,6 +82,7 @@ class AppendNotesToDescription implements ActionInterface return true; } + // TODO introduce error return false; } diff --git a/app/TransactionRules/Actions/ClearBudget.php b/app/TransactionRules/Actions/ClearBudget.php index 24599dcc7e..2c79a535fb 100644 --- a/app/TransactionRules/Actions/ClearBudget.php +++ b/app/TransactionRules/Actions/ClearBudget.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace FireflyIII\TransactionRules\Actions; use DB; +use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray; use FireflyIII\Events\TriggeredAuditLog; use FireflyIII\Models\RuleAction; use FireflyIII\Models\TransactionJournal; @@ -56,6 +57,7 @@ class ClearBudget implements ActionInterface $budget = $object->budgets()->first(); if (null === $budget) { Log::debug(sprintf('RuleAction ClearBudget, no budget in journal #%d.', $journal['transaction_journal_id'])); + event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.journal_already_no_budget'))); return false; } diff --git a/app/TransactionRules/Actions/ClearCategory.php b/app/TransactionRules/Actions/ClearCategory.php index c478ea05d8..22b9df33f7 100644 --- a/app/TransactionRules/Actions/ClearCategory.php +++ b/app/TransactionRules/Actions/ClearCategory.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace FireflyIII\TransactionRules\Actions; use DB; +use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray; use FireflyIII\Events\TriggeredAuditLog; use FireflyIII\Models\RuleAction; use FireflyIII\Models\TransactionJournal; @@ -56,6 +57,7 @@ class ClearCategory implements ActionInterface $category = $object->categories()->first(); if (null === $category) { Log::debug(sprintf('RuleAction ClearCategory, no category in journal #%d.', $journal['transaction_journal_id'])); + event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.journal_already_no_category'))); return false; } diff --git a/app/TransactionRules/Actions/ClearNotes.php b/app/TransactionRules/Actions/ClearNotes.php index 77757456df..d67a1b3d90 100644 --- a/app/TransactionRules/Actions/ClearNotes.php +++ b/app/TransactionRules/Actions/ClearNotes.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace FireflyIII\TransactionRules\Actions; use DB; +use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray; use FireflyIII\Events\TriggeredAuditLog; use FireflyIII\Models\RuleAction; use FireflyIII\Models\TransactionJournal; @@ -55,6 +56,7 @@ class ClearNotes implements ActionInterface $notes = $object->notes()->first(); if (null === $notes) { Log::debug(sprintf('RuleAction ClearNotes, journal #%d has no notes.', $journal['transaction_journal_id'])); + event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.journal_already_no_notes'))); return false; } $before = $notes->text; diff --git a/app/TransactionRules/Actions/ConvertToDeposit.php b/app/TransactionRules/Actions/ConvertToDeposit.php index 803b2c7516..273744c069 100644 --- a/app/TransactionRules/Actions/ConvertToDeposit.php +++ b/app/TransactionRules/Actions/ConvertToDeposit.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace FireflyIII\TransactionRules\Actions; use DB; +use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray; use FireflyIII\Events\TriggeredAuditLog; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Factory\AccountFactory; @@ -65,11 +66,13 @@ class ConvertToDeposit implements ActionInterface $object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']); if (null === $object) { Log::error(sprintf('Cannot find journal #%d, cannot convert to deposit.', $journal['transaction_journal_id'])); + event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.journal_not_found'))); return false; } $groupCount = TransactionJournal::where('transaction_group_id', $journal['transaction_group_id'])->count(); if ($groupCount > 1) { Log::error(sprintf('Group #%d has more than one transaction in it, cannot convert to deposit.', $journal['transaction_group_id'])); + event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.split_group'))); return false; } @@ -77,7 +80,7 @@ class ConvertToDeposit implements ActionInterface $type = $object->transactionType->type; if (TransactionType::DEPOSIT === $type) { Log::error(sprintf('Journal #%d is already a deposit (rule #%d).', $journal['transaction_journal_id'], $this->action->rule_id)); - + event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.is_already_deposit'))); return false; } @@ -89,6 +92,7 @@ class ConvertToDeposit implements ActionInterface } catch (JsonException | FireflyException $e) { Log::debug('Could not convert withdrawal to deposit.'); Log::error($e->getMessage()); + event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.complex_error'))); return false; } @@ -104,13 +108,14 @@ class ConvertToDeposit implements ActionInterface } catch (JsonException | FireflyException $e) { Log::debug('Could not convert transfer to deposit.'); Log::error($e->getMessage()); + event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.complex_error'))); return false; } event(new TriggeredAuditLog($this->action->rule, $object, 'update_transaction_type', TransactionType::TRANSFER, TransactionType::DEPOSIT)); return $res; } - + // TODO introduce error return false; } diff --git a/app/TransactionRules/Actions/ConvertToTransfer.php b/app/TransactionRules/Actions/ConvertToTransfer.php index ddfbab0d6c..d64abdfdd5 100644 --- a/app/TransactionRules/Actions/ConvertToTransfer.php +++ b/app/TransactionRules/Actions/ConvertToTransfer.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace FireflyIII\TransactionRules\Actions; use DB; +use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray; use FireflyIII\Events\TriggeredAuditLog; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\Account; @@ -62,11 +63,13 @@ class ConvertToTransfer implements ActionInterface $object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']); if (null === $object) { Log::error(sprintf('Cannot find journal #%d, cannot convert to transfer.', $journal['transaction_journal_id'])); + event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.journal_not_found'))); return false; } $groupCount = TransactionJournal::where('transaction_group_id', $journal['transaction_group_id'])->count(); if ($groupCount > 1) { Log::error(sprintf('Group #%d has more than one transaction in it, cannot convert to transfer.', $journal['transaction_group_id'])); + event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.split_group'))); return false; } @@ -77,6 +80,7 @@ class ConvertToTransfer implements ActionInterface Log::error( sprintf('Journal #%d is already a transfer so cannot be converted (rule #%d).', $object->id, $this->action->rule_id) ); + event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.is_already_transfer'))); return false; } @@ -107,6 +111,7 @@ class ConvertToTransfer implements ActionInterface $this->action->rule_id ) ); + event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.no_valid_opposing', ['name' => $this->action->action_value]))); return false; } @@ -118,6 +123,7 @@ class ConvertToTransfer implements ActionInterface } catch (FireflyException $e) { Log::debug('Could not convert withdrawal to transfer.'); Log::error($e->getMessage()); + event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.complex_error'))); return false; } event(new TriggeredAuditLog($this->action->rule, $object, 'update_transaction_type', TransactionType::WITHDRAWAL, TransactionType::TRANSFER)); @@ -130,12 +136,13 @@ class ConvertToTransfer implements ActionInterface } catch (FireflyException $e) { Log::debug('Could not convert deposit to transfer.'); Log::error($e->getMessage()); + event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.complex_error'))); return false; } event(new TriggeredAuditLog($this->action->rule, $object, 'update_transaction_type', TransactionType::DEPOSIT, TransactionType::TRANSFER)); return $res; } - + // TODO introduce error return false; } @@ -166,6 +173,7 @@ class ConvertToTransfer implements ActionInterface $journal = TransactionJournal::find($journalId); if (null === $journal) { Log::error(sprintf('Journal #%d does not exist. Cannot convert to transfer.', $journalId)); + // TODO introduce error return ''; } return (string)$journal->transactions()->where('amount', '>', 0)->first()?->account?->accountType?->type; @@ -192,6 +200,7 @@ class ConvertToTransfer implements ActionInterface [$journal->id, $opposing->name, $this->action->rule_id] ) ); + // TODO introduce error return false; } @@ -250,6 +259,7 @@ class ConvertToTransfer implements ActionInterface [$journal->id, $opposing->name, $this->action->rule_id] ) ); + // TODO introduce error return false; } diff --git a/app/TransactionRules/Actions/ConvertToWithdrawal.php b/app/TransactionRules/Actions/ConvertToWithdrawal.php index 8ce97e5c8c..4c15cd551e 100644 --- a/app/TransactionRules/Actions/ConvertToWithdrawal.php +++ b/app/TransactionRules/Actions/ConvertToWithdrawal.php @@ -65,18 +65,20 @@ class ConvertToWithdrawal implements ActionInterface $object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']); if (null === $object) { Log::error(sprintf('Cannot find journal #%d, cannot convert to withdrawal.', $journal['transaction_journal_id'])); + // TODO introduce error return false; } $groupCount = TransactionJournal::where('transaction_group_id', $journal['transaction_group_id'])->count(); if ($groupCount > 1) { Log::error(sprintf('Group #%d has more than one transaction in it, cannot convert to withdrawal.', $journal['transaction_group_id'])); + // TODO introduce error return false; } $type = $object->transactionType->type; if (TransactionType::WITHDRAWAL === $type) { Log::error(sprintf('Journal #%d is already a withdrawal (rule #%d).', $journal['transaction_journal_id'], $this->action->rule_id)); - + // TODO introduce error return false; } @@ -87,6 +89,7 @@ class ConvertToWithdrawal implements ActionInterface } catch (JsonException | FireflyException $e) { Log::debug('Could not convert transfer to deposit.'); Log::error($e->getMessage()); + // TODO introduce error return false; } event(new TriggeredAuditLog($this->action->rule, $object, 'update_transaction_type', TransactionType::DEPOSIT, TransactionType::WITHDRAWAL)); @@ -101,13 +104,14 @@ class ConvertToWithdrawal implements ActionInterface } catch (JsonException | FireflyException $e) { Log::debug('Could not convert transfer to deposit.'); Log::error($e->getMessage()); + // TODO introduce error return false; } event(new TriggeredAuditLog($this->action->rule, $object, 'update_transaction_type', TransactionType::TRANSFER, TransactionType::WITHDRAWAL)); return $res; } - + // TODO introduce error return false; } diff --git a/app/TransactionRules/Actions/LinkToBill.php b/app/TransactionRules/Actions/LinkToBill.php index a996116bfd..fb61479982 100644 --- a/app/TransactionRules/Actions/LinkToBill.php +++ b/app/TransactionRules/Actions/LinkToBill.php @@ -73,6 +73,7 @@ class LinkToBill implements ActionInterface $billName ) ); + // TODO introduce error return false; } @@ -97,7 +98,7 @@ class LinkToBill implements ActionInterface $billName ) ); - + // TODO introduce error return false; } } diff --git a/app/TransactionRules/Actions/MoveDescriptionToNotes.php b/app/TransactionRules/Actions/MoveDescriptionToNotes.php index 611c1841a0..ba543c6dea 100644 --- a/app/TransactionRules/Actions/MoveDescriptionToNotes.php +++ b/app/TransactionRules/Actions/MoveDescriptionToNotes.php @@ -57,6 +57,7 @@ class MoveDescriptionToNotes implements ActionInterface $object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']); if (null === $object) { Log::error(sprintf('No journal #%d belongs to user #%d.', $journal['transaction_journal_id'], $journal['user_id'])); + // TODO introduce error return false; } $note = $object->notes()->first(); diff --git a/app/TransactionRules/Actions/MoveNotesToDescription.php b/app/TransactionRules/Actions/MoveNotesToDescription.php index 5c670629bd..6ef71dac21 100644 --- a/app/TransactionRules/Actions/MoveNotesToDescription.php +++ b/app/TransactionRules/Actions/MoveNotesToDescription.php @@ -63,16 +63,19 @@ class MoveNotesToDescription implements ActionInterface $object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']); if (null === $object) { Log::error(sprintf('No journal #%d belongs to user #%d.', $journal['transaction_journal_id'], $journal['user_id'])); + // TODO introduce error return false; } $note = $object->notes()->first(); if (null === $note) { // nothing to move, return null + // TODO introduce error return false; } if ('' === $note->text) { // nothing to move, return null $note->delete(); + // TODO introduce error return false; } $before = $object->description; diff --git a/app/TransactionRules/Actions/RemoveAllTags.php b/app/TransactionRules/Actions/RemoveAllTags.php index c2ee19ad0b..8a4b29286b 100644 --- a/app/TransactionRules/Actions/RemoveAllTags.php +++ b/app/TransactionRules/Actions/RemoveAllTags.php @@ -55,6 +55,7 @@ class RemoveAllTags implements ActionInterface $count = DB::table('tag_transaction_journal')->where('transaction_journal_id', $journal['transaction_journal_id'])->count(); if (0 === $count) { Log::debug(sprintf('RuleAction RemoveAllTags, journal #%d has no tags.', $journal['transaction_journal_id'])); + // TODO introduce error return false; } Log::debug(sprintf('RuleAction RemoveAllTags removed all tags from journal %d.', $journal['transaction_journal_id'])); diff --git a/app/TransactionRules/Actions/RemoveTag.php b/app/TransactionRules/Actions/RemoveTag.php index c72454e67a..9c0e6bbf7d 100644 --- a/app/TransactionRules/Actions/RemoveTag.php +++ b/app/TransactionRules/Actions/RemoveTag.php @@ -61,6 +61,7 @@ class RemoveTag implements ActionInterface Log::debug( sprintf('RuleAction RemoveTag tried to remove tag "%s" from journal #%d but no such tag exists.', $name, $journal['transaction_journal_id']) ); + // TODO introduce error return false; } $count = DB::table('tag_transaction_journal')->where('transaction_journal_id', $journal['transaction_journal_id'])->where('tag_id', $tag->id)->count(); @@ -68,6 +69,7 @@ class RemoveTag implements ActionInterface Log::debug( sprintf('RuleAction RemoveTag tried to remove tag "%s" from journal #%d but no such tag is linked.', $name, $journal['transaction_journal_id']) ); + // TODO introduce error return false; } diff --git a/app/TransactionRules/Actions/SetBudget.php b/app/TransactionRules/Actions/SetBudget.php index 0ce333dfcb..f14c1b9dfa 100644 --- a/app/TransactionRules/Actions/SetBudget.php +++ b/app/TransactionRules/Actions/SetBudget.php @@ -65,7 +65,7 @@ class SetBudget implements ActionInterface $search ) ); - + // TODO introduce error return false; } @@ -78,7 +78,7 @@ class SetBudget implements ActionInterface $journal['transaction_type_type'] ) ); - + // TODO introduce error return false; } diff --git a/app/TransactionRules/Actions/SetCategory.php b/app/TransactionRules/Actions/SetCategory.php index a0fdab020e..da2ab40e58 100644 --- a/app/TransactionRules/Actions/SetCategory.php +++ b/app/TransactionRules/Actions/SetCategory.php @@ -57,7 +57,7 @@ class SetCategory implements ActionInterface $search = $this->action->action_value; if (null === $user) { Log::error(sprintf('Journal has no valid user ID so action SetCategory("%s") cannot be applied', $search), $journal); - + // TODO introduce error return false; } @@ -73,7 +73,7 @@ class SetCategory implements ActionInterface $search ) ); - + // TODO introduce error return false; } diff --git a/app/TransactionRules/Actions/SetDestinationAccount.php b/app/TransactionRules/Actions/SetDestinationAccount.php index 7cb68f68de..5cd65f2ea4 100644 --- a/app/TransactionRules/Actions/SetDestinationAccount.php +++ b/app/TransactionRules/Actions/SetDestinationAccount.php @@ -65,7 +65,7 @@ class SetDestinationAccount implements ActionInterface if (null === $object) { Log::error('Could not find journal.'); - + // TODO introduce error return false; } $type = $object->transactionType->type; @@ -81,7 +81,7 @@ class SetDestinationAccount implements ActionInterface $this->action->action_value ) ); - + // TODO introduce error return false; } @@ -90,13 +90,13 @@ class SetDestinationAccount implements ActionInterface $source = $object->transactions()->where('amount', '<', 0)->first(); if (null === $source) { Log::error('Could not find source transaction.'); - + // TODO introduce error return false; } // account must not be deleted (in the meantime): if (null === $source->account) { Log::error('Could not find source transaction account.'); - + // TODO introduce error return false; } if (null !== $newAccount && (int)$newAccount->id === (int)$source->account_id) { @@ -107,7 +107,7 @@ class SetDestinationAccount implements ActionInterface $source->account_id ) ); - + // TODO introduce error return false; } diff --git a/app/TransactionRules/Actions/SetSourceAccount.php b/app/TransactionRules/Actions/SetSourceAccount.php index 6493411c4a..8e39b70ea3 100644 --- a/app/TransactionRules/Actions/SetSourceAccount.php +++ b/app/TransactionRules/Actions/SetSourceAccount.php @@ -64,7 +64,7 @@ class SetSourceAccount implements ActionInterface $this->repository = app(AccountRepositoryInterface::class); if (null === $object) { Log::error('Could not find journal.'); - + // TODO introduce error return false; } $type = $object->transactionType->type; @@ -76,7 +76,7 @@ class SetSourceAccount implements ActionInterface Log::error( sprintf('Cant change source account of journal #%d because no asset account with name "%s" exists.', $object->id, $this->action->action_value) ); - + // TODO introduce error return false; } @@ -85,13 +85,13 @@ class SetSourceAccount implements ActionInterface $destination = $object->transactions()->where('amount', '>', 0)->first(); if (null === $destination) { Log::error('Could not find destination transaction.'); - + // TODO introduce error return false; } // account must not be deleted (in the meantime): if (null === $destination->account) { Log::error('Could not find destination transaction account.'); - + // TODO introduce error return false; } if (null !== $newAccount && (int)$newAccount->id === (int)$destination->account_id) { @@ -102,7 +102,7 @@ class SetSourceAccount implements ActionInterface $destination->account_id ) ); - + // TODO introduce error return false; } diff --git a/app/TransactionRules/Actions/SwitchAccounts.php b/app/TransactionRules/Actions/SwitchAccounts.php index 98d67259fc..c5daeaa7f4 100644 --- a/app/TransactionRules/Actions/SwitchAccounts.php +++ b/app/TransactionRules/Actions/SwitchAccounts.php @@ -59,18 +59,20 @@ class SwitchAccounts implements ActionInterface $object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']); if (null === $object) { Log::error(sprintf('Cannot find journal #%d, cannot switch accounts.', $journal['transaction_journal_id'])); + // TODO introduce error return false; } $groupCount = TransactionJournal::where('transaction_group_id', $journal['transaction_group_id'])->count(); if ($groupCount > 1) { Log::error(sprintf('Group #%d has more than one transaction in it, cannot switch accounts.', $journal['transaction_group_id'])); + // TODO introduce error return false; } $type = $object->transactionType->type; if (TransactionType::TRANSFER !== $type) { Log::error(sprintf('Journal #%d is NOT a transfer (rule #%d), cannot switch accounts.', $journal['transaction_journal_id'], $this->action->rule_id)); - + // TODO introduce error return false; } @@ -80,7 +82,7 @@ class SwitchAccounts implements ActionInterface $destTransaction = $object->transactions()->where('amount', '>', 0)->first(); if (null === $sourceTransaction || null === $destTransaction) { Log::error(sprintf('Journal #%d has no source or destination transaction (rule #%d), cannot switch accounts.', $journal['transaction_journal_id'], $this->action->rule_id)); - + // TODO introduce error return false; } $sourceAccountId = (int)$sourceTransaction->account_id; diff --git a/app/TransactionRules/Actions/UpdatePiggybank.php b/app/TransactionRules/Actions/UpdatePiggybank.php index a70c0dc548..5b1a538232 100644 --- a/app/TransactionRules/Actions/UpdatePiggybank.php +++ b/app/TransactionRules/Actions/UpdatePiggybank.php @@ -70,7 +70,7 @@ class UpdatePiggybank implements ActionInterface Log::info( sprintf('No piggy bank named "%s", cant execute action #%d of rule #%d', $this->action->action_value, $this->action->id, $this->action->rule_id) ); - + // TODO introduce error return false; } @@ -130,7 +130,7 @@ class UpdatePiggybank implements ActionInterface $destination->account_id ) ); - + // TODO introduce error return false; } diff --git a/resources/lang/en_US/rules.php b/resources/lang/en_US/rules.php new file mode 100644 index 0000000000..b9473825ad --- /dev/null +++ b/resources/lang/en_US/rules.php @@ -0,0 +1,41 @@ +. + */ + + +return [ + 'main_message' => 'Action ":action", present in rule ":rule", could not be applied to transaction #:group: :error', + 'find_or_create_tag_failed' => 'Could not find or create tag ":tag"', + 'tag_already_added' => 'Tag ":tag" is already linked to this transaction.', + 'inspect_transaction' => 'Inspect transaction ":title" @ Firefly III', + 'inspect_rule' => 'Inspect rule ":title" @ Firefly III', + 'journal_other_user' => 'This transaction doesn\'t belong to the user', + 'journal_already_no_budget' => 'This transaction has no budget, so it cannot be removed.', + 'journal_already_no_category' => 'This transaction had no category, so it cannot be removed', + 'journal_already_no_notes' => 'This transaction had no notes, so they cannot be removed', + 'journal_not_found' => 'Firefly III can\'t find the requested transaction.', + 'split_group' => 'Firefly III cannot execute this action on a transaction with multiple splits.', + 'is_already_deposit' => 'This transaction is already a deposit.', + 'is_already_transfer' => 'This transaction is already a transfer.', + 'complex_error' => 'Something complicated went wrong. Please inspect the logs of Firefly III.', + 'no_valid_opposing' => 'Conversion failed because there is no valid account named ":account".', +];