diff --git a/app/Api/V1/Controllers/Models/Transaction/StoreController.php b/app/Api/V1/Controllers/Models/Transaction/StoreController.php index dcec81a1ca..03a866b8d5 100644 --- a/app/Api/V1/Controllers/Models/Transaction/StoreController.php +++ b/app/Api/V1/Controllers/Models/Transaction/StoreController.php @@ -103,7 +103,9 @@ class StoreController extends Controller throw new ValidationException($validator,0, $e); } app('preferences')->mark(); - event(new StoredTransactionGroup($transactionGroup, $data['apply_rules'] ?? true)); + $applyRules = $data['apply_rules'] ?? true; + $fireWebhooks = $data['fire_webhooks'] ?? true; + event(new StoredTransactionGroup($transactionGroup, $applyRules, $fireWebhooks)); $manager = $this->getManager(); /** @var User $admin */ diff --git a/app/Api/V1/Controllers/Models/Transaction/UpdateController.php b/app/Api/V1/Controllers/Models/Transaction/UpdateController.php index 53fc0f7a78..2741dfaccc 100644 --- a/app/Api/V1/Controllers/Models/Transaction/UpdateController.php +++ b/app/Api/V1/Controllers/Models/Transaction/UpdateController.php @@ -80,7 +80,9 @@ class UpdateController extends Controller $manager = $this->getManager(); app('preferences')->mark(); - event(new UpdatedTransactionGroup($transactionGroup, $data['apply_rules'] ?? true)); + $applyRules = $data['apply_rules'] ?? true; + $fireWebhooks = $data['fire_webhooks'] ?? true; + event(new UpdatedTransactionGroup($transactionGroup, $applyRules, $fireWebhooks)); /** @var User $admin */ $admin = auth()->user(); diff --git a/app/Api/V1/Requests/Models/Transaction/StoreRequest.php b/app/Api/V1/Requests/Models/Transaction/StoreRequest.php index 81fb1afa4e..1298fd6c05 100644 --- a/app/Api/V1/Requests/Models/Transaction/StoreRequest.php +++ b/app/Api/V1/Requests/Models/Transaction/StoreRequest.php @@ -57,6 +57,7 @@ class StoreRequest extends FormRequest 'group_title' => $this->string('group_title'), 'error_if_duplicate_hash' => $this->boolean('error_if_duplicate_hash'), 'apply_rules' => $this->boolean('apply_rules', true), + 'fire_webhooks' => $this->boolean('fire_webhooks', true), 'transactions' => $this->getTransactionData(), ]; // TODO location diff --git a/app/Api/V1/Requests/Models/Transaction/UpdateRequest.php b/app/Api/V1/Requests/Models/Transaction/UpdateRequest.php index 677afd1469..23e775bf87 100644 --- a/app/Api/V1/Requests/Models/Transaction/UpdateRequest.php +++ b/app/Api/V1/Requests/Models/Transaction/UpdateRequest.php @@ -129,6 +129,9 @@ class UpdateRequest extends FormRequest if ($this->has('apply_rules')) { $data['apply_rules'] = $this->boolean('apply_rules', true); } + if ($this->has('fire_webhooks')) { + $data['fire_webhooks'] = $this->boolean('fire_webhooks', true); + } if ($this->has('group_title')) { $data['group_title'] = $this->string('group_title'); } diff --git a/app/Events/StoredTransactionGroup.php b/app/Events/StoredTransactionGroup.php index 558dc9015d..3c497e37bd 100644 --- a/app/Events/StoredTransactionGroup.php +++ b/app/Events/StoredTransactionGroup.php @@ -37,16 +37,19 @@ class StoredTransactionGroup extends Event use SerializesModels; public bool $applyRules; + public bool $fireWebhooks; public TransactionGroup $transactionGroup; + /** * Create a new event instance. * * @param TransactionGroup $transactionGroup * @param bool $applyRules */ - public function __construct(TransactionGroup $transactionGroup, bool $applyRules = true) + public function __construct(TransactionGroup $transactionGroup, bool $applyRules = true, bool $fireWebhooks = true) { $this->transactionGroup = $transactionGroup; + $this->fireWebhooks = $fireWebhooks; $this->applyRules = $applyRules; } } diff --git a/app/Events/UpdatedTransactionGroup.php b/app/Events/UpdatedTransactionGroup.php index eefd05d501..0f5e00f6fa 100644 --- a/app/Events/UpdatedTransactionGroup.php +++ b/app/Events/UpdatedTransactionGroup.php @@ -36,10 +36,9 @@ class UpdatedTransactionGroup extends Event { use SerializesModels; - /** @var bool */ - public $applyRules; - /** @var TransactionGroup The group that was stored. */ - public $transactionGroup; + public bool $applyRules; + public bool $fireWebhooks; + public TransactionGroup $transactionGroup; /** * Create a new event instance. @@ -47,9 +46,10 @@ class UpdatedTransactionGroup extends Event * @param TransactionGroup $transactionGroup * @param bool $applyRules */ - public function __construct(TransactionGroup $transactionGroup, bool $applyRules = true) + public function __construct(TransactionGroup $transactionGroup, bool $applyRules = true, bool $fireWebhooks = true) { $this->transactionGroup = $transactionGroup; + $this->fireWebhooks = $fireWebhooks; $this->applyRules = $applyRules; } } diff --git a/app/Handlers/Events/StoredGroupEventHandler.php b/app/Handlers/Events/StoredGroupEventHandler.php index f89100129e..8a54064090 100644 --- a/app/Handlers/Events/StoredGroupEventHandler.php +++ b/app/Handlers/Events/StoredGroupEventHandler.php @@ -86,7 +86,13 @@ class StoredGroupEventHandler { Log::debug(__METHOD__); $group = $storedGroupEvent->transactionGroup; - $user = $group->user; + if (false === $storedGroupEvent->fireWebhooks) { + Log::info(sprintf('Will not fire webhooks for transaction group #%d', $group->id)); + + return; + } + + $user = $group->user; /** @var MessageGeneratorInterface $engine */ $engine = app(MessageGeneratorInterface::class); $engine->setUser($user); diff --git a/app/Handlers/Events/UpdatedGroupEventHandler.php b/app/Handlers/Events/UpdatedGroupEventHandler.php index 3a6bc19466..b5f9237794 100644 --- a/app/Handlers/Events/UpdatedGroupEventHandler.php +++ b/app/Handlers/Events/UpdatedGroupEventHandler.php @@ -82,9 +82,14 @@ class UpdatedGroupEventHandler */ public function triggerWebhooks(UpdatedTransactionGroup $updatedGroupEvent): void { - Log::debug('UpdatedGroupEventHandler:triggerWebhooks'); + Log::debug(__METHOD__); $group = $updatedGroupEvent->transactionGroup; - $user = $group->user; + if (false === $updatedGroupEvent->fireWebhooks) { + Log::info(sprintf('Will not fire webhooks for transaction group #%d', $group->id)); + + return; + } + $user = $group->user; /** @var MessageGeneratorInterface $engine */ $engine = app(MessageGeneratorInterface::class); $engine->setUser($user); diff --git a/app/Http/Controllers/Transaction/CreateController.php b/app/Http/Controllers/Transaction/CreateController.php index dbae963b8d..3ed7ff63c7 100644 --- a/app/Http/Controllers/Transaction/CreateController.php +++ b/app/Http/Controllers/Transaction/CreateController.php @@ -70,7 +70,7 @@ class CreateController extends Controller $newGroup = $service->cloneGroup($group); // event! - event(new StoredTransactionGroup($newGroup, true)); + event(new StoredTransactionGroup($newGroup)); app('preferences')->mark();