From 399b397ecfdb0f4f477a86ce32dfa637bda4eb90 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 31 Jan 2023 23:53:54 +1100 Subject: [PATCH] Refactor for observers --- app/Models/Webhook.php | 25 +++++++++++++ app/Observers/ClientObserver.php | 12 +++---- app/Observers/CreditObserver.php | 14 ++++---- app/Observers/ExpenseObserver.php | 34 ++++++++++++------ app/Observers/InvoiceObserver.php | 35 +++++++++++------- app/Observers/PaymentObserver.php | 37 ++++++++++++------- app/Observers/ProductObserver.php | 37 +++++++++++++++++-- app/Observers/ProjectObserver.php | 40 +++++++++++++-------- app/Observers/PurchaseOrderObserver.php | 37 +++++++++++++++++++ app/Observers/QuoteObserver.php | 48 +++++++++++++++---------- app/Observers/TaskObserver.php | 41 +++++++++++++-------- app/Observers/VendorObserver.php | 31 ++++++++++------ 12 files changed, 280 insertions(+), 111 deletions(-) diff --git a/app/Models/Webhook.php b/app/Models/Webhook.php index 6e412e908d..367f2f4466 100644 --- a/app/Models/Webhook.php +++ b/app/Models/Webhook.php @@ -116,11 +116,36 @@ class Webhook extends BaseModel const EVENT_RESTORE_VENDOR = 49; + const EVENT_CREATE_PRODUCT = 50; + const EVENT_UPDATE_PRODUCT = 51; + const EVENT_DELETE_PRODUCT = 52; + const EVENT_RESTORE_PRODUCT = 53; + + const EVENT_ARCHIVE_PRODUCT = 54; + + const EVENT_CREATE_PURCHASE_ORDER = 55; + + const EVENT_UPDATE_PURCHASE_ORDER = 56; + + const EVENT_DELETE_PURCHASE_ORDER = 57; + + const EVENT_RESTORE_PURCHASE_ORDER = 58; + + const EVENT_ARCHIVE_PURCHASE_ORDER = 59; public static $valid_events = [ + self::EVENT_CREATE_PURCHASE_ORDER, + self::EVENT_UPDATE_PURCHASE_ORDER, + self::EVENT_DELETE_PURCHASE_ORDER, + self::EVENT_RESTORE_PURCHASE_ORDER, + self::EVENT_ARCHIVE_PURCHASE_ORDER, + self::EVENT_UPDATE_PRODUCT, + self::EVENT_DELETE_PRODUCT, + self::EVENT_RESTORE_PRODUCT, + self::EVENT_ARCHIVE_PRODUCT, self::EVENT_CREATE_CLIENT, self::EVENT_CREATE_INVOICE, self::EVENT_CREATE_QUOTE, diff --git a/app/Observers/ClientObserver.php b/app/Observers/ClientObserver.php index d5ad6b4e82..af7ee3ec9e 100644 --- a/app/Observers/ClientObserver.php +++ b/app/Observers/ClientObserver.php @@ -26,12 +26,12 @@ class ClientObserver */ public function created(Client $client) { - $subscriptions = Webhook::where('company_id', $client->company->id) + $subscriptions = Webhook::where('company_id', $client->company_id) ->where('event_id', Webhook::EVENT_CREATE_CLIENT) ->exists(); if ($subscriptions) - WebhookHandler::dispatch(Webhook::EVENT_CREATE_CLIENT, $client, $client->company)->delay(now()->addSeconds(rand(1,5))); + WebhookHandler::dispatch(Webhook::EVENT_CREATE_CLIENT, $client, $client->company)->delay(rand(1,5)); } @@ -53,12 +53,12 @@ class ClientObserver $event = Webhook::EVENT_DELETE_CLIENT; - $subscriptions = Webhook::where('company_id', $client->company->id) + $subscriptions = Webhook::where('company_id', $client->company_id) ->where('event_id', $event) ->exists(); if ($subscriptions) - WebhookHandler::dispatch($event, $client, $client->company)->delay(now()->addSeconds(rand(1,5))); + WebhookHandler::dispatch($event, $client, $client->company)->delay(rand(1,5)); } @@ -74,12 +74,12 @@ class ClientObserver if($client->is_deleted) return; - $subscriptions = Webhook::where('company_id', $client->company->id) + $subscriptions = Webhook::where('company_id', $client->company_id) ->where('event_id', Webhook::EVENT_ARCHIVE_CLIENT) ->exists(); if ($subscriptions) - WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_CLIENT, $client, $client->company)->delay(now()->addSeconds(rand(1,5))); + WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_CLIENT, $client, $client->company)->delay(rand(1,5)); } diff --git a/app/Observers/CreditObserver.php b/app/Observers/CreditObserver.php index 799799a9ef..d457ac6762 100644 --- a/app/Observers/CreditObserver.php +++ b/app/Observers/CreditObserver.php @@ -32,7 +32,7 @@ class CreditObserver ->exists(); if ($subscriptions) { - WebhookHandler::dispatch(Webhook::EVENT_CREATE_CREDIT, $credit, $credit->company)->delay(now()->addSeconds(rand(1,5))); + WebhookHandler::dispatch(Webhook::EVENT_CREATE_CREDIT, $credit, $credit->company)->delay(rand(1,5)); } } @@ -53,12 +53,12 @@ class CreditObserver $event = Webhook::EVENT_DELETE_CREDIT; $subscriptions = Webhook::where('company_id', $credit->company->id) - ->where('event_id', Webhook::EVENT_UPDATE_CREDIT) + ->where('event_id', $event) ->exists(); - if ($subscriptions) { - WebhookHandler::dispatch(Webhook::EVENT_UPDATE_CREDIT, $credit, $credit->company)->delay(now()->addSeconds(rand(1,5))); - } + if ($subscriptions) + WebhookHandler::dispatch($event, $credit, $credit->company)->delay(rand(1,5)); + } /** @@ -73,11 +73,11 @@ class CreditObserver return; $subscriptions = Webhook::where('company_id', $credit->company->id) - ->where('event_id', Webhook::EVENT_DELETE_CREDIT) + ->where('event_id', Webhook::EVENT_ARCHIVE_CREDIT) ->exists(); if ($subscriptions) - WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_CREDIT, $credit, $credit->company)->delay(now()->addSeconds(rand(1,5))); + WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_CREDIT, $credit, $credit->company)->delay(rand(1,5)); } diff --git a/app/Observers/ExpenseObserver.php b/app/Observers/ExpenseObserver.php index e9c0cdcfe6..b2b4ce4700 100644 --- a/app/Observers/ExpenseObserver.php +++ b/app/Observers/ExpenseObserver.php @@ -30,7 +30,7 @@ class ExpenseObserver ->exists(); if ($subscriptions) { - WebhookHandler::dispatch(Webhook::EVENT_CREATE_EXPENSE, $expense, $expense->company)->delay(now()->addSeconds(rand(1,5))); + WebhookHandler::dispatch(Webhook::EVENT_CREATE_EXPENSE, $expense, $expense->company)->delay(rand(1,5)); } } @@ -42,13 +42,22 @@ class ExpenseObserver */ public function updated(Expense $expense) { - $subscriptions = Webhook::where('company_id', $expense->company->id) - ->where('event_id', Webhook::EVENT_UPDATE_EXPENSE) - ->exists(); + $event = Webhook::EVENT_UPDATE_EXPENSE; - if ($subscriptions) { - WebhookHandler::dispatch(Webhook::EVENT_UPDATE_EXPENSE, $expense, $expense->company)->delay(now()->addSeconds(rand(1,5))); - } + if($expense->getOriginal('deleted_at') && !$expense->deleted_at) + $event = Webhook::EVENT_RESTORE_EXPENSE; + + if($expense->is_deleted) + $event = Webhook::EVENT_DELETE_EXPENSE; + + + $subscriptions = Webhook::where('company_id', $expense->company->id) + ->where('event_id', $event) + ->exists(); + + if ($subscriptions) + WebhookHandler::dispatch($event, $expense, $expense->company)->delay(rand(1,5)); + } /** @@ -59,13 +68,16 @@ class ExpenseObserver */ public function deleted(Expense $expense) { + if($expense->is_deleted) + return; + $subscriptions = Webhook::where('company_id', $expense->company->id) - ->where('event_id', Webhook::EVENT_DELETE_EXPENSE) + ->where('event_id', Webhook::EVENT_ARCHIVE_EXPENSE) ->exists(); - if ($subscriptions) { - WebhookHandler::dispatch(Webhook::EVENT_DELETE_EXPENSE, $expense, $expense->company)->delay(now()->addSeconds(rand(1,5))); - } + if ($subscriptions) + WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_EXPENSE, $expense, $expense->company)->delay(rand(1,5)); + } /** diff --git a/app/Observers/InvoiceObserver.php b/app/Observers/InvoiceObserver.php index 112c16e8ad..9caa003e02 100644 --- a/app/Observers/InvoiceObserver.php +++ b/app/Observers/InvoiceObserver.php @@ -11,9 +11,7 @@ namespace App\Observers; -use App\Jobs\Util\UnlinkFile; use App\Jobs\Util\WebhookHandler; -use App\Models\Client; use App\Models\Invoice; use App\Models\Webhook; @@ -34,7 +32,7 @@ class InvoiceObserver ->exists(); if ($subscriptions) { - WebhookHandler::dispatch(Webhook::EVENT_CREATE_INVOICE, $invoice, $invoice->company, 'client')->delay(now()->addSeconds(rand(1,5))); + WebhookHandler::dispatch(Webhook::EVENT_CREATE_INVOICE, $invoice, $invoice->company, 'client')->delay(rand(1,5)); } } @@ -46,13 +44,21 @@ class InvoiceObserver */ public function updated(Invoice $invoice) { - $subscriptions = Webhook::where('company_id', $invoice->company_id) - ->where('event_id', Webhook::EVENT_UPDATE_INVOICE) - ->exists(); + $event = Webhook::EVENT_UPDATE_INVOICE; - if ($subscriptions) { - WebhookHandler::dispatch(Webhook::EVENT_UPDATE_INVOICE, $invoice, $invoice->company, 'client')->delay(now()->addSeconds(rand(1,5))); - } + if($invoice->getOriginal('deleted_at') && !$invoice->deleted_at) + $event = Webhook::EVENT_RESTORE_INVOICE; + + if($invoice->is_deleted) + $event = Webhook::EVENT_DELETE_INVOICE; + + + $subscriptions = Webhook::where('company_id', $invoice->company->id) + ->where('event_id', $event) + ->exists(); + + if ($subscriptions) + WebhookHandler::dispatch($event, $invoice, $invoice->company)->delay(rand(1,5)); } /** @@ -63,13 +69,16 @@ class InvoiceObserver */ public function deleted(Invoice $invoice) { + if($invoice->is_deleted) + return; + $subscriptions = Webhook::where('company_id', $invoice->company_id) - ->where('event_id', Webhook::EVENT_DELETE_INVOICE) + ->where('event_id', Webhook::EVENT_ARCHIVE_INVOICE) ->exists(); - if ($subscriptions) { - WebhookHandler::dispatch(Webhook::EVENT_DELETE_INVOICE, $invoice, $invoice->company, 'client')->delay(now()->addSeconds(rand(1,5))); - } + if ($subscriptions) + WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_INVOICE, $invoice, $invoice->company, 'client')->delay(rand(1,5)); + } /** diff --git a/app/Observers/PaymentObserver.php b/app/Observers/PaymentObserver.php index 0007b4ae62..205ba93077 100644 --- a/app/Observers/PaymentObserver.php +++ b/app/Observers/PaymentObserver.php @@ -25,13 +25,13 @@ class PaymentObserver */ public function created(Payment $payment) { - $subscriptions = Webhook::where('company_id', $payment->company->id) + $subscriptions = Webhook::where('company_id', $payment->company_id) ->where('event_id', Webhook::EVENT_CREATE_PAYMENT) ->exists(); - if ($subscriptions) { - WebhookHandler::dispatch(Webhook::EVENT_CREATE_PAYMENT, $payment, $payment->company, 'invoices,client')->delay(now()->addSeconds(20)); - } + if ($subscriptions) + WebhookHandler::dispatch(Webhook::EVENT_CREATE_PAYMENT, $payment, $payment->company, 'invoices,client')->delay(20); + } /** @@ -42,13 +42,21 @@ class PaymentObserver */ public function updated(Payment $payment) { - $subscriptions = Webhook::where('company_id', $payment->company->id) - ->where('event_id', Webhook::EVENT_UPDATE_PAYMENT) - ->exists(); + $event = Webhook::EVENT_UPDATE_PAYMENT; - if ($subscriptions) { - WebhookHandler::dispatch(Webhook::EVENT_UPDATE_PAYMENT, $payment, $payment->company, 'invoices,client')->delay(now()->addSeconds(20)); - } + if($payment->getOriginal('deleted_at') && !$payment->deleted_at) + $event = Webhook::EVENT_RESTORE_PAYMENT; + + if($payment->is_deleted) + $event = Webhook::EVENT_DELETE_PAYMENT; + + + $subscriptions = Webhook::where('company_id', $payment->company_id) + ->where('event_id', $event) + ->exists(); + + if ($subscriptions) + WebhookHandler::dispatch($event, $payment, $payment->company, 'invoices,client')->delay(20); } /** @@ -59,12 +67,15 @@ class PaymentObserver */ public function deleted(Payment $payment) { - $subscriptions = Webhook::where('company_id', $payment->company->id) - ->where('event_id', Webhook::EVENT_DELETE_PAYMENT) + if($payment->is_deleted) + return; + + $subscriptions = Webhook::where('company_id', $payment->company_id) + ->where('event_id', Webhook::EVENT_ARCHIVE_PAYMENT) ->exists(); if ($subscriptions) { - WebhookHandler::dispatch(Webhook::EVENT_DELETE_PAYMENT, $payment, $payment->company, 'invoices,client')->delay(now()->addSeconds(20)); + WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_PAYMENT, $payment, $payment->company, 'invoices,client')->delay(20); } } diff --git a/app/Observers/ProductObserver.php b/app/Observers/ProductObserver.php index 11707fe6a3..b94ffcc597 100644 --- a/app/Observers/ProductObserver.php +++ b/app/Observers/ProductObserver.php @@ -11,7 +11,9 @@ namespace App\Observers; +use App\Jobs\Util\WebhookHandler; use App\Models\Product; +use App\Models\Webhook; class ProductObserver { @@ -23,7 +25,12 @@ 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(rand(1,5)); } /** @@ -34,7 +41,23 @@ class ProductObserver */ public function updated(Product $product) { - // + + $event = Webhook::EVENT_UPDATE_PRODUCT; + + if($product->getOriginal('deleted_at') && !$product->deleted_at) + $event = Webhook::EVENT_RESTORE_PRODUCT; + + if($product->is_deleted) + $event = Webhook::EVENT_DELETE_PRODUCT; + + + $subscriptions = Webhook::where('company_id', $product->company_id) + ->where('event_id', $event) + ->exists(); + + if ($subscriptions) + WebhookHandler::dispatch($event, $product, $product->company)->delay(rand(1,5)); + } /** @@ -45,7 +68,15 @@ class ProductObserver */ public function deleted(Product $product) { - // + if($product->is_deleted) + return; + + $subscriptions = Webhook::where('company_id', $product->company_id) + ->where('event_id', Webhook::EVENT_ARCHIVE_PRODUCT) + ->exists(); + + if ($subscriptions) + WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_PRODUCT, $product, $product->company)->delay(rand(1,5)); } /** diff --git a/app/Observers/ProjectObserver.php b/app/Observers/ProjectObserver.php index 030c57007d..eece39f6dd 100644 --- a/app/Observers/ProjectObserver.php +++ b/app/Observers/ProjectObserver.php @@ -31,9 +31,9 @@ class ProjectObserver ->where('event_id', Webhook::EVENT_PROJECT_CREATE) ->exists(); - if ($subscriptions) { - WebhookHandler::dispatch(Webhook::EVENT_PROJECT_CREATE, $project, $project->company, 'client')->delay(now()->addSeconds(rand(1,5))); - } + if ($subscriptions) + WebhookHandler::dispatch(Webhook::EVENT_PROJECT_CREATE, $project, $project->company, 'client')->delay(rand(1,5)); + } /** @@ -44,13 +44,23 @@ class ProjectObserver */ public function updated(Project $project) { - $subscriptions = Webhook::where('company_id', $project->company_id) - ->where('event_id', Webhook::EVENT_PROJECT_UPDATE) - ->exists(); - if ($subscriptions) { - WebhookHandler::dispatch(Webhook::EVENT_PROJECT_UPDATE, $project, $project->company, 'client')->delay(now()->addSeconds(rand(1,5))); - } + $event = Webhook::EVENT_PROJECT_UPDATE; + + if($project->getOriginal('deleted_at') && !$project->deleted_at) + $event = Webhook::EVENT_RESTORE_PROJECT; + + if($project->is_deleted) + $event = Webhook::EVENT_PROJECT_DELETE; + + + $subscriptions = Webhook::where('company_id', $project->company_id) + ->where('event_id', $event) + ->exists(); + + if ($subscriptions) + WebhookHandler::dispatch($event, $project, $project->company, 'client')->delay(rand(1,5)); + } /** @@ -61,14 +71,16 @@ class ProjectObserver */ public function deleted(Project $project) { - //EVENT_PROJECT_DELETE + if($project->is_deleted) + return; + $subscriptions = Webhook::where('company_id', $project->company_id) - ->where('event_id', Webhook::EVENT_PROJECT_DELETE) + ->where('event_id', Webhook::EVENT_ARCHIVE_PROJECT) ->exists(); - if ($subscriptions) { - WebhookHandler::dispatch(Webhook::EVENT_PROJECT_DELETE, $project, $project->company, 'client')->delay(now()->addSeconds(rand(1,5))); - } + if ($subscriptions) + WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_PROJECT, $project, $project->company, 'client')->delay(rand(1,5)); + } /** diff --git a/app/Observers/PurchaseOrderObserver.php b/app/Observers/PurchaseOrderObserver.php index 1cac6a7323..14fa2f10ed 100644 --- a/app/Observers/PurchaseOrderObserver.php +++ b/app/Observers/PurchaseOrderObserver.php @@ -11,7 +11,9 @@ namespace App\Observers; +use App\Jobs\Util\WebhookHandler; use App\Models\PurchaseOrder; +use App\Models\Webhook; class PurchaseOrderObserver { @@ -23,6 +25,14 @@ class PurchaseOrderObserver */ public function created(PurchaseOrder $purchase_order) { + + $subscriptions = Webhook::where('company_id', $purchase_order->company_id) + ->where('event_id', Webhook::EVENT_CREATE_PURCHASE_ORDER) + ->exists(); + + if ($subscriptions) + WebhookHandler::dispatch(Webhook::EVENT_CREATE_PURCHASE_ORDER, $purchase_order, $purchase_order->company, 'vendor')->delay(rand(1,5)); + } /** @@ -33,6 +43,23 @@ class PurchaseOrderObserver */ public function updated(PurchaseOrder $purchase_order) { + + $event = Webhook::EVENT_UPDATE_PURCHASE_ORDER; + + if($purchase_order->getOriginal('deleted_at') && !$purchase_order->deleted_at) + $event = Webhook::EVENT_RESTORE_PURCHASE_ORDER; + + if($purchase_order->is_deleted) + $event = Webhook::EVENT_DELETE_PURCHASE_ORDER; + + + $subscriptions = Webhook::where('company_id', $purchase_order->company_id) + ->where('event_id', $event) + ->exists(); + + if ($subscriptions) + WebhookHandler::dispatch($event, $purchase_order, $purchase_order->company, 'vendor')->delay(rand(1,5)); + } /** @@ -43,6 +70,16 @@ class PurchaseOrderObserver */ public function deleted(PurchaseOrder $purchase_order) { + if($purchase_order->is_deleted) + return; + + $subscriptions = Webhook::where('company_id', $purchase_order->company_id) + ->where('event_id', Webhook::EVENT_ARCHIVE_PURCHASE_ORDER) + ->exists(); + + if ($subscriptions) + WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_PURCHASE_ORDER, $purchase_order, $purchase_order->company, 'vendor')->delay(rand(1,5)); + } /** diff --git a/app/Observers/QuoteObserver.php b/app/Observers/QuoteObserver.php index f8cf4cad2a..ba9248ede3 100644 --- a/app/Observers/QuoteObserver.php +++ b/app/Observers/QuoteObserver.php @@ -11,7 +11,6 @@ namespace App\Observers; -use App\Jobs\Util\UnlinkFile; use App\Jobs\Util\WebhookHandler; use App\Models\Quote; use App\Models\Webhook; @@ -26,14 +25,14 @@ class QuoteObserver */ public function created(Quote $quote) { - $subscriptions = Webhook::where('company_id', $quote->company->id) + + $subscriptions = Webhook::where('company_id', $quote->company_id) ->where('event_id', Webhook::EVENT_CREATE_QUOTE) ->exists(); - if ($subscriptions) { - $quote->load('client'); - WebhookHandler::dispatch(Webhook::EVENT_CREATE_QUOTE, $quote, $quote->company, 'client')->delay(now()->addSeconds(rand(1,5))); - } + if ($subscriptions) + WebhookHandler::dispatch(Webhook::EVENT_CREATE_QUOTE, $quote, $quote->company, 'client')->delay(rand(1,5)); + } /** @@ -44,14 +43,23 @@ class QuoteObserver */ public function updated(Quote $quote) { - $subscriptions = Webhook::where('company_id', $quote->company->id) - ->where('event_id', Webhook::EVENT_UPDATE_QUOTE) - ->exists(); - if ($subscriptions) { - $quote->load('client'); - WebhookHandler::dispatch(Webhook::EVENT_UPDATE_QUOTE, $quote, $quote->company, 'client')->delay(now()->addSeconds(rand(1,5))); - } + $event = Webhook::EVENT_UPDATE_QUOTE; + + if($quote->getOriginal('deleted_at') && !$quote->deleted_at) + $event = Webhook::EVENT_RESTORE_QUOTE; + + if($quote->is_deleted) + $event = Webhook::EVENT_DELETE_QUOTE; + + + $subscriptions = Webhook::where('company_id', $quote->company_id) + ->where('event_id', $event) + ->exists(); + + if ($subscriptions) + WebhookHandler::dispatch($event, $quote, $quote->company, 'client')->delay(rand(1,5)); + } /** @@ -62,14 +70,16 @@ class QuoteObserver */ public function deleted(Quote $quote) { - $subscriptions = Webhook::where('company_id', $quote->company->id) - ->where('event_id', Webhook::EVENT_DELETE_QUOTE) + if($quote->is_deleted) + return; + + $subscriptions = Webhook::where('company_id', $quote->company_id) + ->where('event_id', Webhook::EVENT_ARCHIVE_QUOTE) ->exists(); - if ($subscriptions) { - $quote->load('client'); - WebhookHandler::dispatch(Webhook::EVENT_DELETE_QUOTE, $quote, $quote->company, 'client')->delay(now()->addSeconds(rand(1,5))); - } + if ($subscriptions) + WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_QUOTE, $quote, $quote->company, 'client')->delay(rand(1,5)); + } /** diff --git a/app/Observers/TaskObserver.php b/app/Observers/TaskObserver.php index dbc516dbc5..bc51069a0d 100644 --- a/app/Observers/TaskObserver.php +++ b/app/Observers/TaskObserver.php @@ -25,13 +25,13 @@ class TaskObserver */ public function created(Task $task) { - $subscriptions = Webhook::where('company_id', $task->company->id) + $subscriptions = Webhook::where('company_id', $task->company_id) ->where('event_id', Webhook::EVENT_CREATE_TASK) ->exists(); - if ($subscriptions) { - WebhookHandler::dispatch(Webhook::EVENT_CREATE_TASK, $task, $task->company)->delay(now()->addSeconds(rand(1,5))); - } + if ($subscriptions) + WebhookHandler::dispatch(Webhook::EVENT_CREATE_TASK, $task, $task->company)->delay(rand(1,5)); + } /** @@ -42,13 +42,21 @@ class TaskObserver */ public function updated(Task $task) { - $subscriptions = Webhook::where('company_id', $task->company->id) - ->where('event_id', Webhook::EVENT_UPDATE_TASK) - ->exists(); + $event = Webhook::EVENT_UPDATE_TASK; - if ($subscriptions) { - WebhookHandler::dispatch(Webhook::EVENT_UPDATE_TASK, $task, $task->company)->delay(now()->addSeconds(rand(1,5))); - } + if($task->getOriginal('deleted_at') && !$task->deleted_at) + $event = Webhook::EVENT_RESTORE_TASK; + + if($task->is_deleted) + $event = Webhook::EVENT_DELETE_TASK; + + + $subscriptions = Webhook::where('company_id', $task->company_id) + ->where('event_id', $event) + ->exists(); + + if ($subscriptions) + WebhookHandler::dispatch($event, $task, $task->company)->delay(rand(1,5)); } /** @@ -59,13 +67,16 @@ class TaskObserver */ public function deleted(Task $task) { - $subscriptions = Webhook::where('company_id', $task->company->id) - ->where('event_id', Webhook::EVENT_DELETE_TASK) + if($task->is_deleted) + return; + + $subscriptions = Webhook::where('company_id', $task->company_id) + ->where('event_id', Webhook::EVENT_ARCHIVE_TASK) ->exists(); - if ($subscriptions) { - WebhookHandler::dispatch(Webhook::EVENT_DELETE_TASK, $task, $task->company)->delay(now()->addSeconds(rand(1,5))); - } + if ($subscriptions) + WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_TASK, $task, $task->company)->delay(rand(1,5)); + } /** diff --git a/app/Observers/VendorObserver.php b/app/Observers/VendorObserver.php index ad81735028..461c6f1173 100644 --- a/app/Observers/VendorObserver.php +++ b/app/Observers/VendorObserver.php @@ -25,12 +25,12 @@ class VendorObserver */ public function created(Vendor $vendor) { - $subscriptions = Webhook::where('company_id', $vendor->company->id) + $subscriptions = Webhook::where('company_id', $vendor->company_id) ->where('event_id', Webhook::EVENT_CREATE_VENDOR) ->exists(); if ($subscriptions) { - WebhookHandler::dispatch(Webhook::EVENT_CREATE_VENDOR, $vendor, $vendor->company)->delay(now()->addSeconds(rand(1,5))); + WebhookHandler::dispatch(Webhook::EVENT_CREATE_VENDOR, $vendor, $vendor->company)->delay(rand(1,5)); } } @@ -42,13 +42,21 @@ class VendorObserver */ public function updated(Vendor $vendor) { - $subscriptions = Webhook::where('company_id', $vendor->company->id) - ->where('event_id', Webhook::EVENT_UPDATE_VENDOR) + $event = Webhook::EVENT_UPDATE_VENDOR; + + if($vendor->getOriginal('deleted_at') && !$vendor->deleted_at) + $event = Webhook::EVENT_RESTORE_VENDOR; + + if($vendor->is_deleted) + $event = Webhook::EVENT_DELETE_VENDOR; + + + $subscriptions = Webhook::where('company_id', $vendor->company_id) + ->where('event_id', $event) ->exists(); - if ($subscriptions) { - WebhookHandler::dispatch(Webhook::EVENT_UPDATE_VENDOR, $vendor, $vendor->company)->delay(now()->addSeconds(rand(1,5))); - } + if ($subscriptions) + WebhookHandler::dispatch($event, $vendor, $vendor->company)->delay(rand(1,5)); } /** @@ -59,12 +67,15 @@ class VendorObserver */ public function deleted(Vendor $vendor) { - $subscriptions = Webhook::where('company_id', $vendor->company->id) - ->where('event_id', Webhook::EVENT_DELETE_VENDOR) + if($vendor->is_deleted) + return; + + $subscriptions = Webhook::where('company_id', $vendor->company_id) + ->where('event_id', Webhook::EVENT_ARCHIVE_VENDOR) ->exists(); if ($subscriptions) { - WebhookHandler::dispatch(Webhook::EVENT_DELETE_VENDOR, $vendor, $vendor->company)->delay(now()->addSeconds(rand(1,5))); + WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_VENDOR, $vendor, $vendor->company)->delay(rand(1,5)); } }