2023-08-12 20:57:51 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* RuleHandler.php
|
|
|
|
* Copyright (c) 2023 james@firefly-iii.org
|
|
|
|
*
|
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2023-09-20 06:36:43 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2023-08-12 20:57:51 +02:00
|
|
|
namespace FireflyIII\Handlers\Events\Model;
|
|
|
|
|
|
|
|
use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray;
|
2023-08-13 15:01:12 +02:00
|
|
|
use FireflyIII\Events\Model\Rule\RuleActionFailedOnObject;
|
2023-08-12 20:57:51 +02:00
|
|
|
use FireflyIII\Notifications\User\RuleActionFailed;
|
2024-02-14 19:32:15 +01:00
|
|
|
use GuzzleHttp\Exception\ClientException;
|
2024-02-14 19:47:35 +01:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2023-08-12 20:57:51 +02:00
|
|
|
use Illuminate\Support\Facades\Notification;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class RuleHandler
|
|
|
|
*/
|
|
|
|
class RuleHandler
|
|
|
|
{
|
2023-08-13 15:01:12 +02:00
|
|
|
public function ruleActionFailedOnArray(RuleActionFailedOnArray $event): void
|
2023-08-12 20:57:51 +02:00
|
|
|
{
|
2024-01-01 14:43:56 +01:00
|
|
|
$ruleAction = $event->ruleAction;
|
|
|
|
$rule = $ruleAction->rule;
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2023-11-01 18:45:15 +01:00
|
|
|
/** @var bool $preference */
|
2024-01-01 14:43:56 +01:00
|
|
|
$preference = app('preferences')->getForUser($rule->user, 'notification_rule_action_failures', true)->data;
|
2023-08-15 13:52:30 +02:00
|
|
|
if (false === $preference) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
app('log')->debug('Now in ruleActionFailedOnArray');
|
2024-01-01 14:43:56 +01:00
|
|
|
$journal = $event->journal;
|
|
|
|
$error = $event->error;
|
|
|
|
$user = $ruleAction->rule->user;
|
2023-08-12 20:57:51 +02:00
|
|
|
|
|
|
|
$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];
|
2024-02-14 19:47:35 +01:00
|
|
|
|
2024-02-14 19:32:15 +01:00
|
|
|
try {
|
|
|
|
Notification::send($user, new RuleActionFailed($params));
|
2024-02-22 01:29:01 +01:00
|
|
|
} catch (ClientException $e) {
|
2024-02-14 19:32:15 +01:00
|
|
|
Log::error(sprintf('[a] Error sending notification that the rule action failed: %s', $e->getMessage()));
|
|
|
|
}
|
2023-08-13 15:01:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function ruleActionFailedOnObject(RuleActionFailedOnObject $event): void
|
|
|
|
{
|
2024-01-01 14:43:56 +01:00
|
|
|
$ruleAction = $event->ruleAction;
|
|
|
|
$rule = $ruleAction->rule;
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2023-11-01 18:45:15 +01:00
|
|
|
/** @var bool $preference */
|
2024-01-01 14:43:56 +01:00
|
|
|
$preference = app('preferences')->getForUser($rule->user, 'notification_rule_action_failures', true)->data;
|
2023-08-15 13:52:30 +02:00
|
|
|
if (false === $preference) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
app('log')->debug('Now in ruleActionFailedOnObject');
|
2024-01-01 14:43:56 +01:00
|
|
|
$journal = $event->journal;
|
|
|
|
$error = $event->error;
|
|
|
|
$user = $ruleAction->rule->user;
|
2023-08-13 15:01:12 +02:00
|
|
|
|
|
|
|
$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];
|
2024-02-14 19:47:35 +01:00
|
|
|
|
2024-02-14 19:32:15 +01:00
|
|
|
try {
|
|
|
|
Notification::send($user, new RuleActionFailed($params));
|
2024-02-22 01:29:01 +01:00
|
|
|
} catch (ClientException $e) {
|
2024-02-14 19:32:15 +01:00
|
|
|
Log::error(sprintf('[b] Error sending notification that the rule action failed: %s', $e->getMessage()));
|
|
|
|
}
|
2023-08-12 20:57:51 +02:00
|
|
|
}
|
|
|
|
}
|