From dd5476bfc74d13ca6efbef6d401e6ba251d5114d Mon Sep 17 00:00:00 2001 From: James Cole Date: Thu, 5 Feb 2026 06:02:32 +0100 Subject: [PATCH] Clean up events and filters. --- .../Autocomplete/AccountController.php | 28 +++---- app/Api/V1/Requests/AggregateFormRequest.php | 6 +- .../CreatedSingleTransactionGroup.php | 7 +- app/Factory/TransactionJournalFactory.php | 2 +- .../ProcessesDestroyedTransactionGroup.php | 8 +- .../ProcessesNewTransactionGroup.php | 5 +- .../ProcessesUpdatedTransactionGroup.php | 2 +- .../SupportsGroupProcessingTrait.php | 81 ++++++++++--------- .../TransactionGroupRepository.php | 1 - 9 files changed, 66 insertions(+), 74 deletions(-) diff --git a/app/Api/V1/Controllers/Autocomplete/AccountController.php b/app/Api/V1/Controllers/Autocomplete/AccountController.php index 51aa06ce3d..92b8b20c88 100644 --- a/app/Api/V1/Controllers/Autocomplete/AccountController.php +++ b/app/Api/V1/Controllers/Autocomplete/AccountController.php @@ -31,7 +31,6 @@ use FireflyIII\Enums\UserRoleEnum; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\Account; use FireflyIII\Repositories\Account\AccountRepositoryInterface; -use FireflyIII\Support\Debug\Timer; use FireflyIII\Support\Facades\Amount; use FireflyIII\Support\Facades\Steam; use FireflyIII\Support\Http\Api\AccountFilter; @@ -51,7 +50,7 @@ class AccountController extends Controller protected array $acceptedRoles = [UserRoleEnum::READ_ONLY]; /** @var array */ - private array $balanceTypes; + private array $balanceTypes; private AccountRepositoryInterface $repository; /** @@ -80,19 +79,17 @@ class AccountController extends Controller */ public function accounts(AutocompleteApiRequest $request): JsonResponse { - Log::debug('Before All.'); - ['types' => $types, 'query' => $query, 'date' => $date, 'limit' => $limit] = $request->attributes->all(); + // Log::debug('Before All.'); + ['types' => $types, 'query' => $query, 'date' => $date, 'limit' => $limit] = $request->attributes->all(); $date ??= today(config('app.timezone')); // set date to end-of-day for account balance. so it is at $date 23:59:59 $date->endOfDay(); - $return = []; - $timer = Timer::getInstance(); - $timer->start(sprintf('AC accounts "%s"', $query)); - $result = $this->repository->searchAccount((string) $query, $types, $limit); - $allBalances = Steam::accountsBalancesOptimized($result, $date, $this->primaryCurrency, $this->convertToPrimary); + $return = []; + $result = $this->repository->searchAccount((string)$query, $types, $limit); + $allBalances = Steam::accountsBalancesOptimized($result, $date, $this->primaryCurrency, $this->convertToPrimary); /** @var Account $account */ foreach ($result as $account) { @@ -109,18 +106,18 @@ class AccountController extends Controller $nameWithBalance = sprintf('%s (%s)', $account->name, Amount::formatAnything($useCurrency, $amount, false)); } - $return[] = [ - 'id' => (string) $account->id, + $return[] = [ + 'id' => (string)$account->id, 'name' => $account->name, 'name_with_balance' => $nameWithBalance, 'active' => $account->active, 'type' => $account->accountType->type, - 'currency_id' => (string) $useCurrency->id, + 'currency_id' => (string)$useCurrency->id, 'currency_name' => $useCurrency->name, 'currency_code' => $useCurrency->code, 'currency_symbol' => $useCurrency->symbol, 'currency_decimal_places' => $useCurrency->decimal_places, - 'account_currency_id' => (string) $currency->id, + 'account_currency_id' => (string)$currency->id, 'account_currency_name' => $currency->name, 'account_currency_code' => $currency->code, 'account_currency_symbol' => $currency->symbol, @@ -131,12 +128,11 @@ class AccountController extends Controller // custom order. usort($return, static function (array $left, array $right): int { $order = [AccountTypeEnum::ASSET->value, AccountTypeEnum::REVENUE->value, AccountTypeEnum::EXPENSE->value]; - $posA = (int) array_search($left['type'], $order, true); - $posB = (int) array_search($right['type'], $order, true); + $posA = (int)array_search($left['type'], $order, true); + $posB = (int)array_search($right['type'], $order, true); return $posA - $posB; }); - $timer->stop(sprintf('AC accounts "%s"', $query)); return response()->api($return); } diff --git a/app/Api/V1/Requests/AggregateFormRequest.php b/app/Api/V1/Requests/AggregateFormRequest.php index bc97d9f538..76d12e162f 100644 --- a/app/Api/V1/Requests/AggregateFormRequest.php +++ b/app/Api/V1/Requests/AggregateFormRequest.php @@ -53,7 +53,7 @@ abstract class AggregateFormRequest extends ApiRequest parent::initialize($query, $request, $attributes, $cookies, $files, $server, $content); // instantiate all subrequests and share current requests' bags with them - Log::debug('Initializing AggregateFormRequest.'); + // Log::debug('Initializing AggregateFormRequest.'); /** @var array|string $config */ foreach ($this->getRequests() as $config) { @@ -62,7 +62,7 @@ abstract class AggregateFormRequest extends ApiRequest if (!is_a($requestClass, Request::class, true)) { throw new RuntimeException('getRequests() must return class-strings of subclasses of Request'); } - Log::debug(sprintf('Initializing subrequest %s', $requestClass)); + // Log::debug(sprintf('Initializing subrequest %s', $requestClass)); $instance = $this->requests[] = new $requestClass(); $instance->request = $this->request; @@ -77,7 +77,7 @@ abstract class AggregateFormRequest extends ApiRequest $instance->handleConfig(is_array($config) ? $config : []); } } - Log::debug('Done initializing AggregateFormRequest.'); + // Log::debug('Done initializing AggregateFormRequest.'); } public function rules(): array diff --git a/app/Events/Model/TransactionGroup/CreatedSingleTransactionGroup.php b/app/Events/Model/TransactionGroup/CreatedSingleTransactionGroup.php index 95288fa8ef..f362d2de24 100644 --- a/app/Events/Model/TransactionGroup/CreatedSingleTransactionGroup.php +++ b/app/Events/Model/TransactionGroup/CreatedSingleTransactionGroup.php @@ -35,10 +35,5 @@ class CreatedSingleTransactionGroup extends Event /** * Create a new event instance. */ - public function __construct( - public TransactionGroupEventFlags $flags, - public TransactionGroupEventObjects $objects - ) { - Log::debug(__METHOD__); - } + public function __construct(public TransactionGroupEventFlags $flags, public TransactionGroupEventObjects $objects) {} } diff --git a/app/Factory/TransactionJournalFactory.php b/app/Factory/TransactionJournalFactory.php index a0fccb6d7c..0648ea7127 100644 --- a/app/Factory/TransactionJournalFactory.php +++ b/app/Factory/TransactionJournalFactory.php @@ -328,7 +328,7 @@ class TransactionJournalFactory throw new FireflyException($e->getMessage(), 0, $e); } - Log::debug(sprintf('Is part of a batch submission? %s', var_export($row['batch_submission'], true))); + // Log::debug(sprintf('Is part of a batch submission? %s', var_export($row['batch_submission'], true))); $journal->save(); $this->storeBudget($journal, $row); $this->storeCategory($journal, $row); diff --git a/app/Listeners/Model/TransactionGroup/ProcessesDestroyedTransactionGroup.php b/app/Listeners/Model/TransactionGroup/ProcessesDestroyedTransactionGroup.php index 741eb2b6e2..87689b4650 100644 --- a/app/Listeners/Model/TransactionGroup/ProcessesDestroyedTransactionGroup.php +++ b/app/Listeners/Model/TransactionGroup/ProcessesDestroyedTransactionGroup.php @@ -26,17 +26,13 @@ namespace FireflyIII\Listeners\Model\TransactionGroup; use FireflyIII\Enums\WebhookTrigger; use FireflyIII\Events\Model\TransactionGroup\DestroyedSingleTransactionGroup; -use FireflyIII\Events\Model\Webhook\WebhookMessagesRequestSending; -use FireflyIII\Generator\Webhook\MessageGeneratorInterface; -use FireflyIII\Support\Facades\FireflyConfig; -use FireflyIII\Support\Models\AccountBalanceCalculator; use Illuminate\Contracts\Queue\ShouldQueue; -use Illuminate\Support\Collection; use Illuminate\Support\Facades\Log; class ProcessesDestroyedTransactionGroup implements ShouldQueue { use SupportsGroupProcessingTrait; + public function handle(DestroyedSingleTransactionGroup $event): void { Log::debug(sprintf('User called %s', get_class($event))); @@ -52,7 +48,7 @@ class ProcessesDestroyedTransactionGroup implements ShouldQueue $this->recalculateCredit($event->objects->accounts); } if ($event->flags->fireWebhooks) { - $this->fireWebhooks($event->objects->transactionGroups, WebhookTrigger::DESTROY_TRANSACTION); + $this->createWebhookMessages($event->objects->transactionGroups, WebhookTrigger::DESTROY_TRANSACTION); } $this->removePeriodStatistics($event->objects); $this->recalculateRunningBalance($event->objects); diff --git a/app/Listeners/Model/TransactionGroup/ProcessesNewTransactionGroup.php b/app/Listeners/Model/TransactionGroup/ProcessesNewTransactionGroup.php index 4899f7d970..a4be0d6eb6 100644 --- a/app/Listeners/Model/TransactionGroup/ProcessesNewTransactionGroup.php +++ b/app/Listeners/Model/TransactionGroup/ProcessesNewTransactionGroup.php @@ -38,7 +38,7 @@ class ProcessesNewTransactionGroup implements ShouldQueue public function handle(CreatedSingleTransactionGroup|UserRequestedBatchProcessing $event): void { - Log::debug(sprintf('User called %s', get_class($event))); + Log::debug(sprintf('Running event handler for %s', get_class($event))); $setting = FireflyConfig::get('enable_batch_processing', false)->data; if (true === $event->flags->batchSubmission && true === $setting) { @@ -46,7 +46,6 @@ class ProcessesNewTransactionGroup implements ShouldQueue return; } - Log::debug('Will also collect all open transaction groups and process them as well.'); $repository = app(JournalRepositoryInterface::class); $journals = $event->objects->transactionJournals->merge($repository->getAllUncompletedJournals()); @@ -68,7 +67,7 @@ class ProcessesNewTransactionGroup implements ShouldQueue $this->recalculateCredit($event->objects->accounts); } if ($event->flags->fireWebhooks) { - $this->fireWebhooks($event->objects->transactionGroups, WebhookTrigger::STORE_TRANSACTION); + $this->createWebhookMessages($event->objects->transactionGroups, WebhookTrigger::STORE_TRANSACTION); } $this->removePeriodStatistics($event->objects); $this->recalculateRunningBalance($event->objects); diff --git a/app/Listeners/Model/TransactionGroup/ProcessesUpdatedTransactionGroup.php b/app/Listeners/Model/TransactionGroup/ProcessesUpdatedTransactionGroup.php index 8f4f431d74..9aab835a83 100644 --- a/app/Listeners/Model/TransactionGroup/ProcessesUpdatedTransactionGroup.php +++ b/app/Listeners/Model/TransactionGroup/ProcessesUpdatedTransactionGroup.php @@ -60,7 +60,7 @@ class ProcessesUpdatedTransactionGroup $this->recalculateCredit($event->objects->accounts); } if ($event->flags->fireWebhooks) { - $this->fireWebhooks($event->objects->transactionGroups, WebhookTrigger::UPDATE_TRANSACTION); + $this->createWebhookMessages($event->objects->transactionGroups, WebhookTrigger::UPDATE_TRANSACTION); } $this->removePeriodStatistics($event->objects); $this->recalculateRunningBalance($event->objects); diff --git a/app/Listeners/Model/TransactionGroup/SupportsGroupProcessingTrait.php b/app/Listeners/Model/TransactionGroup/SupportsGroupProcessingTrait.php index 78db6c443f..62e3278390 100644 --- a/app/Listeners/Model/TransactionGroup/SupportsGroupProcessingTrait.php +++ b/app/Listeners/Model/TransactionGroup/SupportsGroupProcessingTrait.php @@ -34,15 +34,16 @@ trait SupportsGroupProcessingTrait $object = app(CreditRecalculateService::class); $object->setAccounts($accounts); $object->recalculate(); + Log::debug(sprintf('Done with recalculateCredit for %d account(s)', $accounts->count())); } - private function fireWebhooks(Collection $groups, WebhookTrigger $trigger): void + private function createWebhookMessages(Collection $groups, WebhookTrigger $trigger): void { - Log::debug(__METHOD__); + Log::debug(sprintf('Will now create webhook messages for %d group(s)', $groups->count())); /** @var TransactionGroup $first */ - $first = $groups->first(); - $user = $first->user; + $first = $groups->first(); + $user = $first->user; /** @var MessageGeneratorInterface $engine */ $engine = app(MessageGeneratorInterface::class); @@ -54,36 +55,41 @@ trait SupportsGroupProcessingTrait $engine->setObjects($groups); // tell the generator to generate the messages $engine->generateMessages(); + + Log::debug(sprintf('Done with create webhook messages for %d group(s)', $groups->count())); } protected function removePeriodStatistics(TransactionGroupEventObjects $objects): void { - if (auth()->check()) { - // since you get a bunch of journals AND a bunch of - // objects, this needs to be a collection - /** @var PeriodStatisticRepositoryInterface $repository */ - $repository = app(PeriodStatisticRepositoryInterface::class); - $dates = $this->collectDatesFromJournals($objects->transactionJournals); - $repository->deleteStatisticsForType(Account::class, $objects->accounts, $dates); - $repository->deleteStatisticsForType(Budget::class, $objects->budgets, $dates); - $repository->deleteStatisticsForType(Category::class, $objects->categories, $dates); - $repository->deleteStatisticsForType(Tag::class, $objects->tags, $dates); - - // remove if no stuff present: - // remove for no tag, no cat, etc. - if (0 === $objects->budgets->count()) { - Log::debug('No budgets, delete "no_category" stats.'); - $repository->deleteStatisticsForPrefix('no_budget', $dates); - } - if (0 === $objects->categories->count()) { - Log::debug('No categories, delete "no_category" stats.'); - $repository->deleteStatisticsForPrefix('no_category', $dates); - } - if (0 === $objects->tags->count()) { - Log::debug('No tags, delete "no_category" stats.'); - $repository->deleteStatisticsForPrefix('no_tag', $dates); - } + if (!auth()->check()) { + Log::debug('Will NOT remove period statistics for all objects, because no user detected.'); } + Log::debug('Will now remove period statistics for all objects.'); + // since you get a bunch of journals AND a bunch of + // objects, this needs to be a collection + /** @var PeriodStatisticRepositoryInterface $repository */ + $repository = app(PeriodStatisticRepositoryInterface::class); + $dates = $this->collectDatesFromJournals($objects->transactionJournals); + $repository->deleteStatisticsForType(Account::class, $objects->accounts, $dates); + $repository->deleteStatisticsForType(Budget::class, $objects->budgets, $dates); + $repository->deleteStatisticsForType(Category::class, $objects->categories, $dates); + $repository->deleteStatisticsForType(Tag::class, $objects->tags, $dates); + + // remove if no stuff present: + // remove for no tag, no cat, etc. + if (0 === $objects->budgets->count()) { + Log::debug('No budgets, delete "no_category" stats.'); + $repository->deleteStatisticsForPrefix('no_budget', $dates); + } + if (0 === $objects->categories->count()) { + Log::debug('No categories, delete "no_category" stats.'); + $repository->deleteStatisticsForPrefix('no_category', $dates); + } + if (0 === $objects->tags->count()) { + Log::debug('No tags, delete "no_category" stats.'); + $repository->deleteStatisticsForPrefix('no_tag', $dates); + } + Log::debug('Done with remove period statistics for all objects.'); } private function collectDatesFromJournals(Collection $journals): Collection @@ -99,12 +105,12 @@ trait SupportsGroupProcessingTrait protected function processRules(Collection $set, string $type): void { Log::debug(sprintf('Will now processRules("%s") for %d journal(s)', $type, $set->count())); - $array = $set->pluck('id')->toArray(); + $array = $set->pluck('id')->toArray(); /** @var TransactionJournal $first */ - $first = $set->first(); - $journalIds = implode(',', $array); - $user = $first->user; + $first = $set->first(); + $journalIds = implode(',', $array); + $user = $first->user; Log::debug(sprintf('Add local operator for journal(s): %s', $journalIds)); // collect rules: @@ -114,20 +120,21 @@ trait SupportsGroupProcessingTrait // add the groups to the rule engine. // it should run the rules in the group and cancel the group if necessary. Log::debug(sprintf('Fire processRules with ALL %s rule groups.', $type)); - $groups = $ruleGroupRepository->getRuleGroupsWithRules($type); + $groups = $ruleGroupRepository->getRuleGroupsWithRules($type); // create and fire rule engine. - $newRuleEngine = app(RuleEngineInterface::class); + $newRuleEngine = app(RuleEngineInterface::class); $newRuleEngine->setUser($user); - $newRuleEngine->addOperator(['type' => 'journal_id', 'value' => $journalIds]); + $newRuleEngine->addOperator(['type' => 'journal_id', 'value' => $journalIds]); $newRuleEngine->setRuleGroups($groups); $newRuleEngine->fire(); + Log::debug(sprintf('Done with processRules("%s") for %d journal(s)', $type, $set->count())); } protected function recalculateRunningBalance(TransactionGroupEventObjects $objects): void { Log::debug('Now in recalculateRunningBalance'); - if (true === FireflyConfig::get('use_running_balance', config('firefly.feature_flags.running_balance_column'))->data) { + if (false === FireflyConfig::get('use_running_balance', config('firefly.feature_flags.running_balance_column'))->data) { Log::debug('Running balance is disabled.'); return; diff --git a/app/Repositories/TransactionGroup/TransactionGroupRepository.php b/app/Repositories/TransactionGroup/TransactionGroupRepository.php index 6f56d4a5c5..dbb0876367 100644 --- a/app/Repositories/TransactionGroup/TransactionGroupRepository.php +++ b/app/Repositories/TransactionGroup/TransactionGroupRepository.php @@ -420,7 +420,6 @@ class TransactionGroupRepository implements TransactionGroupRepositoryInterface, $flags->applyRules = $data['apply_rules'] ?? true; $flags->fireWebhooks = $data['fire_webhooks'] ?? true; $flags->batchSubmission = $data['batch_submission'] ?? false; - Log::debug('CreatedSingleTransactionGroup'); event(new CreatedSingleTransactionGroup($flags, $objects)); Log::debug(sprintf('send event WebhookMessagesRequestSending from %s', __METHOD__)); event(new WebhookMessagesRequestSending());