2021-02-15 11:52:50 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Credit Ninja (https://creditninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/creditninja/creditninja source repository
|
|
|
|
*
|
2022-04-27 05:20:41 +02:00
|
|
|
* @copyright Copyright (c) 2022. Credit Ninja LLC (https://creditninja.com)
|
2021-02-15 11:52:50 +01:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2021-02-15 11:52:50 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Observers;
|
|
|
|
|
|
|
|
use App\Jobs\Util\WebhookHandler;
|
|
|
|
use App\Models\Client;
|
|
|
|
use App\Models\Credit;
|
|
|
|
use App\Models\Webhook;
|
|
|
|
|
|
|
|
class CreditObserver
|
|
|
|
{
|
2023-01-31 15:06:21 +01:00
|
|
|
public $afterCommit = true;
|
|
|
|
|
2021-02-15 11:52:50 +01:00
|
|
|
/**
|
|
|
|
* Handle the client "created" event.
|
|
|
|
*
|
|
|
|
* @param Credit $credit
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function created(Credit $credit)
|
|
|
|
{
|
2023-02-01 05:00:45 +01:00
|
|
|
$subscriptions = Webhook::where('company_id', $credit->company_id)
|
2022-09-15 09:43:33 +02:00
|
|
|
->where('event_id', Webhook::EVENT_CREATE_CREDIT)
|
|
|
|
->exists();
|
|
|
|
|
|
|
|
if ($subscriptions) {
|
2023-02-01 05:00:45 +01:00
|
|
|
WebhookHandler::dispatch(Webhook::EVENT_CREATE_CREDIT, $credit, $credit->company)->delay(0);
|
2022-09-15 09:43:33 +02:00
|
|
|
}
|
2021-02-15 11:52:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the client "updated" event.
|
|
|
|
*
|
|
|
|
* @param Credit $credit
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function updated(Credit $credit)
|
|
|
|
{
|
2023-01-31 12:27:29 +01:00
|
|
|
$event = Webhook::EVENT_UPDATE_CREDIT;
|
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if ($credit->getOriginal('deleted_at') && !$credit->deleted_at) {
|
2023-01-31 12:27:29 +01:00
|
|
|
$event = Webhook::EVENT_RESTORE_CREDIT;
|
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 ($credit->is_deleted) {
|
|
|
|
$event = Webhook::EVENT_DELETE_CREDIT;
|
|
|
|
}
|
2023-01-31 12:27:29 +01:00
|
|
|
|
2023-02-01 05:00:45 +01:00
|
|
|
$subscriptions = Webhook::where('company_id', $credit->company_id)
|
2023-01-31 13:53:54 +01:00
|
|
|
->where('event_id', $event)
|
2022-09-15 09:43:33 +02:00
|
|
|
->exists();
|
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if ($subscriptions) {
|
2023-02-01 05:00:45 +01:00
|
|
|
WebhookHandler::dispatch($event, $credit, $credit->company)->delay(0);
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2021-02-15 11:52:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the client "deleted" event.
|
|
|
|
*
|
|
|
|
* @param Credit $credit
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function deleted(Credit $credit)
|
|
|
|
{
|
2023-02-16 02:36:09 +01:00
|
|
|
if ($credit->is_deleted) {
|
2023-01-31 12:27:29 +01:00
|
|
|
return;
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-02-01 05:00:45 +01:00
|
|
|
$subscriptions = Webhook::where('company_id', $credit->company_id)
|
2023-01-31 13:53:54 +01:00
|
|
|
->where('event_id', Webhook::EVENT_ARCHIVE_CREDIT)
|
2022-09-15 09:43:33 +02:00
|
|
|
->exists();
|
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if ($subscriptions) {
|
2023-02-01 05:00:45 +01:00
|
|
|
WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_CREDIT, $credit, $credit->company)->delay(0);
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2021-02-15 11:52:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the client "restored" event.
|
|
|
|
*
|
|
|
|
* @param Credit $credit
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function restored(Credit $credit)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the client "force deleted" event.
|
|
|
|
*
|
|
|
|
* @param Credit $credit
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function forceDeleted(Credit $credit)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
2023-01-29 14:43:57 +01:00
|
|
|
/**
|
|
|
|
* Handle the client "archive" event.
|
|
|
|
*
|
|
|
|
* @param Credit $credit
|
|
|
|
* @return void
|
|
|
|
*/
|
2021-02-15 11:52:50 +01:00
|
|
|
}
|