From e1fbbe426820fc917c701b4f928b9b437e88c9cf Mon Sep 17 00:00:00 2001 From: paulwer Date: Tue, 31 Jan 2023 10:34:39 +0100 Subject: [PATCH] adding webhooks for products --- app/Models/Webhook.php | 17 ++++++++++++++++- app/Observers/ProductObserver.php | 24 +++++++++++++++++++++--- app/Repositories/BaseRepository.php | 10 ++++++++++ 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/app/Models/Webhook.php b/app/Models/Webhook.php index 6e412e908d..7566023ff0 100644 --- a/app/Models/Webhook.php +++ b/app/Models/Webhook.php @@ -116,6 +116,16 @@ class Webhook extends BaseModel const EVENT_RESTORE_VENDOR = 49; + const EVENT_CREATE_PRODUCT = 50; + + const EVENT_UPDATE_PRODUCT = 51; + + const EVENT_ARCHIVE_PRODUCT = 52; + + const EVENT_RESTORE_PRODUCT = 53; + + const EVENT_DELETE_PRODUCT = 54; + @@ -169,7 +179,12 @@ class Webhook extends BaseModel self::EVENT_RESTORE_QUOTE, self::EVENT_RESTORE_INVOICE, self::EVENT_RESTORE_PAYMENT, - self::EVENT_RESTORE_VENDOR + self::EVENT_RESTORE_VENDOR, + self::EVENT_CREATE_PRODUCT, + self::EVENT_UPDATE_PRODUCT, + self::EVENT_ARCHIVE_PRODUCT, + self::EVENT_RESTORE_PRODUCT, + self::EVENT_DELETE_PRODUCT ]; diff --git a/app/Observers/ProductObserver.php b/app/Observers/ProductObserver.php index 11707fe6a3..cf759b7981 100644 --- a/app/Observers/ProductObserver.php +++ b/app/Observers/ProductObserver.php @@ -23,7 +23,13 @@ class ProductObserver */ public function created(Product $product) { - // + $subscriptions = Webhook::where('company_id', $product->company->id) + ->where('event_id', Webhook::EVENT_CREATE_PRODUCT) + ->exists(); + + if ($subscriptions) { + WebhookHandler::dispatch(Webhook::EVENT_CREATE_PRODUCT, $product, $product->company)->delay(now()->addSeconds(2)); + } } /** @@ -34,7 +40,13 @@ class ProductObserver */ public function updated(Product $product) { - // + $subscriptions = Webhook::where('company_id', $product->company->id) + ->where('event_id', Webhook::EVENT_UPDATE_PRODUCT) + ->exists(); + + if ($subscriptions) { + WebhookHandler::dispatch(Webhook::EVENT_UPDATE_PRODUCT, $product, $product->company)->delay(now()->addSeconds(2)); + } } /** @@ -45,7 +57,13 @@ class ProductObserver */ public function deleted(Product $product) { - // + $subscriptions = Webhook::where('company_id', $product->company->id) + ->where('event_id', Webhook::EVENT_DELETE_PRODUCT) + ->exists(); + + if ($subscriptions) { + WebhookHandler::dispatch(Webhook::EVENT_DELETE_PRODUCT, $product, $product->company)->delay(now()->addSeconds(2)); + } } /** diff --git a/app/Repositories/BaseRepository.php b/app/Repositories/BaseRepository.php index 6370b0d423..6fb22e85e4 100644 --- a/app/Repositories/BaseRepository.php +++ b/app/Repositories/BaseRepository.php @@ -20,6 +20,7 @@ use App\Models\Credit; use App\Models\Expense; use App\Models\Invoice; use App\Models\Payment; +use App\Models\Product; use App\Models\Project; use App\Models\Quote; use App\Models\RecurringInvoice; @@ -143,6 +144,13 @@ class BaseRepository else { $webhookEvent = Webhook::EVENT_ARCHIVE_PROJECT;} break; + case $entity instanceof Product: + if ($restore){ + $webhookEvent = Webhook::EVENT_RESTORE_PRODUCT; + } + else { + $webhookEvent = Webhook::EVENT_ARCHIVE_PRODUCT;} + break; case $entity instanceof Client: if ($restore){ $webhookEvent = Webhook::EVENT_RESTORE_CLIENT; @@ -185,6 +193,8 @@ class BaseRepository case $webhookEvent == Webhook::EVENT_RESTORE_CREDIT: case $webhookEvent == Webhook::EVENT_RESTORE_CLIENT: case $webhookEvent == Webhook::EVENT_ARCHIVE_CLIENT: + case $webhookEvent == Webhook::EVENT_RESTORE_PRODUCT: + case $webhookEvent == Webhook::EVENT_ARCHIVE_PRODUCT: WebhookHandler::dispatch($webhookEvent, $entity, $entity->company)->delay(now()->addSeconds(2)); break; default: