From 052ca31297de0ab2174fb89fc9ffa043fcff9291 Mon Sep 17 00:00:00 2001 From: Lars Kusch Date: Fri, 3 Feb 2023 12:46:55 +0100 Subject: [PATCH 1/4] Add Webhooks at send --- app/Services/Invoice/MarkSent.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/Services/Invoice/MarkSent.php b/app/Services/Invoice/MarkSent.php index 38daae25d480..5f2e8ac321b8 100644 --- a/app/Services/Invoice/MarkSent.php +++ b/app/Services/Invoice/MarkSent.php @@ -70,7 +70,14 @@ class MarkSent extends AbstractService if($fire_webhook) event('eloquent.updated: App\Models\Invoice', $this->invoice); + + $subscriptions = Webhook::where('company_id', $invoice->company_id) + ->where('event_id', Webhook::EVENT_SENT_INVOICE) + ->exists(); + if ($subscriptions) { + WebhookHandler::dispatch(Webhook::EVENT_SENT_INVOICE, $this->invoice, $this->invoice->company, 'client')->delay(0); + } return $this->invoice->fresh(); } From d0cfaff6d6144d10f209dacc027ab3d2deb4ace0 Mon Sep 17 00:00:00 2001 From: Lars Kusch Date: Fri, 3 Feb 2023 12:54:34 +0100 Subject: [PATCH 2/4] Added models and implemented the sent Webhook --- app/Models/Webhook.php | 14 +++++++++++++- app/Services/Credit/MarkSent.php | 12 +++++++++++- app/Services/Invoice/MarkSent.php | 6 ++++-- app/Services/PurchaseOrder/MarkSent.php | 9 +++++++++ app/Services/Quote/MarkSent.php | 9 +++++++++ 5 files changed, 46 insertions(+), 4 deletions(-) diff --git a/app/Models/Webhook.php b/app/Models/Webhook.php index 27d6e81b7dab..6a1c44a87fab 100644 --- a/app/Models/Webhook.php +++ b/app/Models/Webhook.php @@ -136,6 +136,14 @@ class Webhook extends BaseModel const EVENT_ARCHIVE_PURCHASE_ORDER = 59; //tested + const EVENT_SENT_INVOICE = 60; + + const EVENT_SENT_QUOTE = 61; + + const EVENT_SENT_CREDIT = 62; + + const EVENT_SENT_PURCHASE_ORDER = 63; + public static $valid_events = [ self::EVENT_CREATE_PURCHASE_ORDER, self::EVENT_UPDATE_PURCHASE_ORDER, @@ -194,7 +202,11 @@ 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_SENT_INVOICE, + self::EVENT_SENT_QUOTE, + self::EVENT_SENT_CREDIT, + self::EVENT_SENT_PURCHASE_ORDER ]; diff --git a/app/Services/Credit/MarkSent.php b/app/Services/Credit/MarkSent.php index 17ee581a8245..32766372b261 100644 --- a/app/Services/Credit/MarkSent.php +++ b/app/Services/Credit/MarkSent.php @@ -12,7 +12,9 @@ namespace App\Services\Credit; use App\Events\Credit\CreditWasMarkedSent; +use App\Jobs\Util\WebhookHandler; use App\Models\Credit; +use App\Models\Webhook; use App\Utils\Ninja; class MarkSent @@ -49,9 +51,17 @@ class MarkSent ->service() ->adjustCreditBalance($this->credit->amount) ->save(); - + event(new CreditWasMarkedSent($this->credit, $this->credit->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); + $subscriptions = Webhook::where('company_id', $this->credit->company_id) + ->where('event_id', Webhook::EVENT_SENT_CREDIT) + ->exists(); + + if ($subscriptions) { + WebhookHandler::dispatch(Webhook::EVENT_SENT_CREDIT, $this->credit, $this->credit->company)->delay(0); + } + return $this->credit; } } diff --git a/app/Services/Invoice/MarkSent.php b/app/Services/Invoice/MarkSent.php index 5f2e8ac321b8..1607c91e1585 100644 --- a/app/Services/Invoice/MarkSent.php +++ b/app/Services/Invoice/MarkSent.php @@ -12,8 +12,10 @@ namespace App\Services\Invoice; use App\Events\Invoice\InvoiceWasUpdated; +use App\Jobs\Util\WebhookHandler; use App\Models\Client; use App\Models\Invoice; +use App\Models\Webhook; use App\Services\AbstractService; use App\Utils\Ninja; @@ -70,8 +72,8 @@ class MarkSent extends AbstractService if($fire_webhook) event('eloquent.updated: App\Models\Invoice', $this->invoice); - - $subscriptions = Webhook::where('company_id', $invoice->company_id) + + $subscriptions = Webhook::where('company_id', $this->invoice->company_id) ->where('event_id', Webhook::EVENT_SENT_INVOICE) ->exists(); diff --git a/app/Services/PurchaseOrder/MarkSent.php b/app/Services/PurchaseOrder/MarkSent.php index 6d4446ba9946..63c3477aaa47 100644 --- a/app/Services/PurchaseOrder/MarkSent.php +++ b/app/Services/PurchaseOrder/MarkSent.php @@ -11,7 +11,9 @@ namespace App\Services\PurchaseOrder; +use App\Jobs\Util\WebhookHandler; use App\Models\PurchaseOrder; +use App\Models\Webhook; use App\Utils\Ninja; class MarkSent @@ -43,6 +45,13 @@ class MarkSent ->adjustBalance($this->purchase_order->amount) //why was this commented out previously? ->save(); + $subscriptions = Webhook::where('company_id', $this->purchase_order->company_id) + ->where('event_id', Webhook::EVENT_SENT_PURCHASE_ORDER) + ->exists(); + + if ($subscriptions) + WebhookHandler::dispatch(Webhook::EVENT_SENT_PURCHASE_ORDER, $this->purchase_order, $this->purchase_order->company, 'vendor')->delay(0); + return $this->purchase_order; } } diff --git a/app/Services/Quote/MarkSent.php b/app/Services/Quote/MarkSent.php index c428e11bc482..9b4ba391c059 100644 --- a/app/Services/Quote/MarkSent.php +++ b/app/Services/Quote/MarkSent.php @@ -12,7 +12,9 @@ namespace App\Services\Quote; use App\Events\Quote\QuoteWasMarkedSent; +use App\Jobs\Util\WebhookHandler; use App\Models\Quote; +use App\Models\Webhook; use App\Utils\Ninja; use Carbon\Carbon; @@ -52,6 +54,13 @@ class MarkSent event(new QuoteWasMarkedSent($this->quote, $this->quote->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); + $subscriptions = Webhook::where('company_id', $this->quote->company_id) + ->where('event_id', Webhook::EVENT_SENT_QUOTE) + ->exists(); + + if ($subscriptions) + WebhookHandler::dispatch(Webhook::EVENT_SENT_QUOTE, $this->quote, $this->quote->company, 'client')->delay(0); + return $this->quote; } } From 400fb609ee25b7f4e7dcaef68881dc699c09d416 Mon Sep 17 00:00:00 2001 From: Lars Kusch Date: Tue, 7 Feb 2023 09:45:02 +0100 Subject: [PATCH 3/4] Refactor Webhook to BaseModel.php --- app/Models/BaseModel.php | 19 +++++++++++++++---- app/Services/Invoice/MarkSent.php | 8 +------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/app/Models/BaseModel.php b/app/Models/BaseModel.php index 6c8dde938f12..abed950b7cf8 100644 --- a/app/Models/BaseModel.php +++ b/app/Models/BaseModel.php @@ -12,6 +12,7 @@ namespace App\Models; use App\DataMapper\ClientSettings; +use App\Jobs\Util\WebhookHandler; use App\Utils\Traits\MakesHash; use App\Utils\Traits\UserSessionAttributes; use Illuminate\Database\Eloquent\Factories\HasFactory; @@ -167,7 +168,7 @@ class BaseModel extends Model */ public function resolveRouteBinding($value, $field = null) { - + if (is_numeric($value)) { throw new ModelNotFoundException("Record with value {$value} not found"); } @@ -189,12 +190,12 @@ class BaseModel extends Model public function numberFormatter() { - $number = strlen($this->number) >= 1 ? $this->translate_entity() . "_" . $this->number : class_basename($this) . "_" . Str::random(5); + $number = strlen($this->number) >= 1 ? $this->translate_entity() . "_" . $this->number : class_basename($this) . "_" . Str::random(5); $formatted_number = mb_ereg_replace("([^\w\s\d\-_~,;\[\]\(\).])", '', $number); - + $formatted_number = mb_ereg_replace("([\.]{2,})", '', $formatted_number); - + $formatted_number = preg_replace('/\s+/', '_', $formatted_number); return $formatted_number; @@ -205,4 +206,14 @@ class BaseModel extends Model return ctrans('texts.item'); } + public function sendEvent($event_id, $additional_data){ + $subscriptions = Webhook::where('company_id', $this->company_id) + ->where('event_id', $event_id) + ->exists(); + if ($subscriptions) { + WebhookHandler::dispatch($event_id, $this, $this->company, $additional_data)->delay(0); + } + } + + } diff --git a/app/Services/Invoice/MarkSent.php b/app/Services/Invoice/MarkSent.php index 1607c91e1585..f9e3519f3b00 100644 --- a/app/Services/Invoice/MarkSent.php +++ b/app/Services/Invoice/MarkSent.php @@ -73,13 +73,7 @@ class MarkSent extends AbstractService if($fire_webhook) event('eloquent.updated: App\Models\Invoice', $this->invoice); - $subscriptions = Webhook::where('company_id', $this->invoice->company_id) - ->where('event_id', Webhook::EVENT_SENT_INVOICE) - ->exists(); - - if ($subscriptions) { - WebhookHandler::dispatch(Webhook::EVENT_SENT_INVOICE, $this->invoice, $this->invoice->company, 'client')->delay(0); - } + $this->invoice->sendEvent(Webhook::EVENT_SENT_INVOICE, "client"); return $this->invoice->fresh(); } From f0c6df6c4a83ec7fb395472b5e13ade484185eb2 Mon Sep 17 00:00:00 2001 From: Lars Kusch Date: Tue, 7 Feb 2023 15:46:52 +0100 Subject: [PATCH 4/4] Minor fixes --- app/Models/BaseModel.php | 2 +- app/Services/Credit/MarkSent.php | 8 +------- app/Services/PurchaseOrder/MarkSent.php | 7 +------ app/Services/Quote/MarkSent.php | 7 +------ 4 files changed, 4 insertions(+), 20 deletions(-) diff --git a/app/Models/BaseModel.php b/app/Models/BaseModel.php index abed950b7cf8..988928722164 100644 --- a/app/Models/BaseModel.php +++ b/app/Models/BaseModel.php @@ -206,7 +206,7 @@ class BaseModel extends Model return ctrans('texts.item'); } - public function sendEvent($event_id, $additional_data){ + public function sendEvent($event_id, $additional_data=""){ $subscriptions = Webhook::where('company_id', $this->company_id) ->where('event_id', $event_id) ->exists(); diff --git a/app/Services/Credit/MarkSent.php b/app/Services/Credit/MarkSent.php index 32766372b261..d8ad848dbc70 100644 --- a/app/Services/Credit/MarkSent.php +++ b/app/Services/Credit/MarkSent.php @@ -54,13 +54,7 @@ class MarkSent event(new CreditWasMarkedSent($this->credit, $this->credit->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); - $subscriptions = Webhook::where('company_id', $this->credit->company_id) - ->where('event_id', Webhook::EVENT_SENT_CREDIT) - ->exists(); - - if ($subscriptions) { - WebhookHandler::dispatch(Webhook::EVENT_SENT_CREDIT, $this->credit, $this->credit->company)->delay(0); - } + $this->credit->sendEvent(Webhook::EVENT_SENT_CREDIT); return $this->credit; } diff --git a/app/Services/PurchaseOrder/MarkSent.php b/app/Services/PurchaseOrder/MarkSent.php index 63c3477aaa47..a81d91b45433 100644 --- a/app/Services/PurchaseOrder/MarkSent.php +++ b/app/Services/PurchaseOrder/MarkSent.php @@ -45,12 +45,7 @@ class MarkSent ->adjustBalance($this->purchase_order->amount) //why was this commented out previously? ->save(); - $subscriptions = Webhook::where('company_id', $this->purchase_order->company_id) - ->where('event_id', Webhook::EVENT_SENT_PURCHASE_ORDER) - ->exists(); - - if ($subscriptions) - WebhookHandler::dispatch(Webhook::EVENT_SENT_PURCHASE_ORDER, $this->purchase_order, $this->purchase_order->company, 'vendor')->delay(0); + $this->purchase_order->sendEvent(Webhook::EVENT_SENT_PURCHASE_ORDER, "vendor"); return $this->purchase_order; } diff --git a/app/Services/Quote/MarkSent.php b/app/Services/Quote/MarkSent.php index 9b4ba391c059..76a4687db885 100644 --- a/app/Services/Quote/MarkSent.php +++ b/app/Services/Quote/MarkSent.php @@ -54,12 +54,7 @@ class MarkSent event(new QuoteWasMarkedSent($this->quote, $this->quote->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); - $subscriptions = Webhook::where('company_id', $this->quote->company_id) - ->where('event_id', Webhook::EVENT_SENT_QUOTE) - ->exists(); - - if ($subscriptions) - WebhookHandler::dispatch(Webhook::EVENT_SENT_QUOTE, $this->quote, $this->quote->company, 'client')->delay(0); + $this->quote->sendEvent(Webhook::EVENT_SENT_QUOTE, "client"); return $this->quote; }