Refactor Webhook to BaseModel.php

This commit is contained in:
Lars Kusch 2023-02-07 09:45:02 +01:00
parent d0cfaff6d6
commit 400fb609ee
2 changed files with 16 additions and 11 deletions

View File

@ -12,6 +12,7 @@
namespace App\Models; namespace App\Models;
use App\DataMapper\ClientSettings; use App\DataMapper\ClientSettings;
use App\Jobs\Util\WebhookHandler;
use App\Utils\Traits\MakesHash; use App\Utils\Traits\MakesHash;
use App\Utils\Traits\UserSessionAttributes; use App\Utils\Traits\UserSessionAttributes;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
@ -167,7 +168,7 @@ class BaseModel extends Model
*/ */
public function resolveRouteBinding($value, $field = null) public function resolveRouteBinding($value, $field = null)
{ {
if (is_numeric($value)) { if (is_numeric($value)) {
throw new ModelNotFoundException("Record with value {$value} not found"); throw new ModelNotFoundException("Record with value {$value} not found");
} }
@ -189,12 +190,12 @@ class BaseModel extends Model
public function numberFormatter() 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("([^\w\s\d\-_~,;\[\]\(\).])", '', $number);
$formatted_number = mb_ereg_replace("([\.]{2,})", '', $formatted_number); $formatted_number = mb_ereg_replace("([\.]{2,})", '', $formatted_number);
$formatted_number = preg_replace('/\s+/', '_', $formatted_number); $formatted_number = preg_replace('/\s+/', '_', $formatted_number);
return $formatted_number; return $formatted_number;
@ -205,4 +206,14 @@ class BaseModel extends Model
return ctrans('texts.item'); 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);
}
}
} }

View File

@ -73,13 +73,7 @@ class MarkSent extends AbstractService
if($fire_webhook) if($fire_webhook)
event('eloquent.updated: App\Models\Invoice', $this->invoice); event('eloquent.updated: App\Models\Invoice', $this->invoice);
$subscriptions = Webhook::where('company_id', $this->invoice->company_id) $this->invoice->sendEvent(Webhook::EVENT_SENT_INVOICE, "client");
->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(); return $this->invoice->fresh();
} }