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;
@ -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();
} }