1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Observers/VendorObserver.php

110 lines
2.6 KiB
PHP
Raw Normal View History

2023-01-31 11:05:01 +01:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
2023-01-31 11:05:01 +01:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Observers;
use App\Jobs\Util\WebhookHandler;
use App\Models\Vendor;
use App\Models\Webhook;
class VendorObserver
{
2023-01-31 15:06:21 +01:00
public $afterCommit = true;
2023-01-31 11:05:01 +01:00
/**
* Handle the vendor "created" event.
*
* @param Vendor $vendor
* @return void
*/
public function created(Vendor $vendor)
{
2023-01-31 13:53:54 +01:00
$subscriptions = Webhook::where('company_id', $vendor->company_id)
2023-01-31 11:05:01 +01:00
->where('event_id', Webhook::EVENT_CREATE_VENDOR)
->exists();
2023-02-16 02:36:09 +01:00
if ($subscriptions) {
2023-02-01 05:00:45 +01:00
WebhookHandler::dispatch(Webhook::EVENT_CREATE_VENDOR, $vendor, $vendor->company)->delay(0);
2023-02-16 02:36:09 +01:00
}
2023-01-31 11:05:01 +01:00
}
/**
* Handle the vendor "updated" event.
*
* @param Vendor $vendor
* @return void
*/
public function updated(Vendor $vendor)
{
2023-01-31 13:53:54 +01:00
$event = Webhook::EVENT_UPDATE_VENDOR;
2023-02-16 02:36:09 +01:00
if ($vendor->getOriginal('deleted_at') && !$vendor->deleted_at) {
2023-01-31 13:53:54 +01:00
$event = Webhook::EVENT_RESTORE_VENDOR;
2023-02-16 02:36:09 +01:00
}
2024-01-14 05:05:00 +01:00
2023-02-16 02:36:09 +01:00
if ($vendor->is_deleted) {
$event = Webhook::EVENT_DELETE_VENDOR;
}
2024-01-14 05:05:00 +01:00
2023-01-31 13:53:54 +01:00
$subscriptions = Webhook::where('company_id', $vendor->company_id)
->where('event_id', $event)
2023-01-31 11:05:01 +01:00
->exists();
2023-02-16 02:36:09 +01:00
if ($subscriptions) {
2023-02-01 05:00:45 +01:00
WebhookHandler::dispatch($event, $vendor, $vendor->company)->delay(0);
2023-02-16 02:36:09 +01:00
}
2023-01-31 11:05:01 +01:00
}
/**
* Handle the vendor "deleted" event.
*
* @param Vendor $vendor
* @return void
*/
public function deleted(Vendor $vendor)
{
2023-02-16 02:36:09 +01:00
if ($vendor->is_deleted) {
2023-01-31 13:53:54 +01:00
return;
2023-02-16 02:36:09 +01:00
}
2024-01-14 05:05:00 +01:00
2023-01-31 13:53:54 +01:00
$subscriptions = Webhook::where('company_id', $vendor->company_id)
->where('event_id', Webhook::EVENT_ARCHIVE_VENDOR)
2023-01-31 11:05:01 +01:00
->exists();
if ($subscriptions) {
2023-02-01 05:00:45 +01:00
WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_VENDOR, $vendor, $vendor->company)->delay(0);
2023-01-31 11:05:01 +01:00
}
}
/**
* Handle the vendor "restored" event.
*
* @param Vendor $vendor
* @return void
*/
public function restored(Vendor $vendor)
{
//
}
/**
* Handle the vendor "force deleted" event.
*
* @param Vendor $vendor
* @return void
*/
public function forceDeleted(Vendor $vendor)
{
//
}
}