1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00

Fixes for observers

This commit is contained in:
David Bomba 2023-01-31 22:27:29 +11:00
parent da245c073a
commit b260a62a73
2 changed files with 14 additions and 62 deletions

View File

@ -44,6 +44,14 @@ class CreditObserver
*/
public function updated(Credit $credit)
{
$event = Webhook::EVENT_UPDATE_CREDIT;
if($credit->getOriginal('deleted_at') && !$credit->deleted_at)
$event = Webhook::EVENT_RESTORE_CREDIT;
if($credit->is_deleted)
$event = Webhook::EVENT_DELETE_CREDIT;
$subscriptions = Webhook::where('company_id', $credit->company->id)
->where('event_id', Webhook::EVENT_UPDATE_CREDIT)
->exists();
@ -61,13 +69,16 @@ class CreditObserver
*/
public function deleted(Credit $credit)
{
if($credit->is_deleted)
return;
$subscriptions = Webhook::where('company_id', $credit->company->id)
->where('event_id', Webhook::EVENT_DELETE_CREDIT)
->exists();
if ($subscriptions) {
WebhookHandler::dispatch(Webhook::EVENT_DELETE_CREDIT, $credit, $credit->company)->delay(now()->addSeconds(rand(1,5)));
}
if ($subscriptions)
WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_CREDIT, $credit, $credit->company)->delay(now()->addSeconds(rand(1,5)));
}
/**

View File

@ -69,7 +69,6 @@ class BaseRepository
event(new $className($entity, $entity->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
}
// $this->handleWebhook($entity, 'ARCHIVE');
}
/**
@ -97,64 +96,6 @@ class BaseRepository
event(new $className($entity, $fromDeleted, $entity->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
}
// $this->handleWebhook($entity, 'RESTORE');
}
private function handleWebhook($entity, $action): void
{
$includes = '';
$event = null;
switch($entity) {
case $entity instanceof Invoice:
$event = "EVENT_{$action}_INVOICE";
$includes = 'client';
break;
case $entity instanceof Quote:
$event = "EVENT_{$action}_QUOTE";
$includes = 'client';
case $entity instanceof Credit:
$event = "EVENT_{$action}_CREDIT";
$includes = 'client';
break;
case $entity instanceof Payment:
$event = "EVENT_{$action}_PAYMENT";
$includes = 'invoices,client';
break;
case $entity instanceof Task:
$event = "EVENT_{$action}_TASK";
$includes = '';
break;
case $entity instanceof Project:
$event = "EVENT_{$action}PROJECT";
$includes = 'client';
break;
case $entity instanceof Client:
$event = "EVENT_{$action}_CLIENT";
$includes = '';
break;
case $entity instanceof Expense:
$event = "EVENT_{$action}_EXPENSE";
$includes = '';
break;
case $entity instanceof Vendor:
$event = "EVENT_{$action}_VENDOR";
$includes = '';
break;
}
if (isset($event)){
$subscriptions = Webhook::where('company_id', $entity->company_id)
->where('event_id', $event)
->exists();
if ($subscriptions) {
WebhookHandler::dispatch($event, $entity, $entity->company, $includes)->delay(now()->addSeconds(rand(1,5)));
}
}
}
/**