MInor fixes

This commit is contained in:
David Bomba 2023-01-31 21:05:01 +11:00
parent 58e53aa961
commit f3e5682d11
13 changed files with 198 additions and 126 deletions

View File

@ -11,53 +11,34 @@
namespace App\Jobs\Util;
use App\Jobs\Util\SystemLogger;
use App\Jobs\Util\WebhookSingle;
use App\Libraries\MultiDB;
use App\Models\Client as ClientModel;
use App\Models\SystemLog;
use App\Models\Company;
use App\Models\Webhook;
use App\Transformers\ArraySerializer;
use App\Utils\Ninja;
use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use League\Fractal\Manager;
use League\Fractal\Resource\Item;
class WebhookHandler implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private $entity;
private $event_id;
private $company;
public $tries = 1; //number of retries
public $deleteWhenMissingModels = true;
private string $includes;
/**
* Create a new job instance.
*
* @param $event_id
* @param $entity
*/
public function __construct($event_id, $entity, $company, $includes = '')
{
$this->event_id = $event_id;
$this->entity = $entity;
$this->company = $company;
$this->includes = $includes;
}
public function __construct(private int $event_id, private $entity, private Company $company, private string $includes = ''){}
/**
* Execute the job.
@ -74,10 +55,10 @@ class WebhookHandler implements ShouldQueue
return true;
}
$subscriptions = Webhook::where('company_id', $this->company->id)
->where('event_id', $this->event_id)
->cursor()
->each(function ($subscription) {
Webhook::where('company_id', $this->company->id)
->where('event_id', $this->event_id)
->cursor()
->each(function ($subscription) {
WebhookSingle::dispatch($subscription->id, $this->entity, $this->company->db, $this->includes);

View File

@ -51,5 +51,7 @@ class RestoreClientActivity implements ShouldQueue
$fields->activity_type_id = Activity::RESTORE_CLIENT;
$this->activity_repo->save($fields, $event->client, $event->event_vars);
return false;
}
}

View File

@ -17,6 +17,9 @@ use App\Models\Webhook;
class ClientObserver
{
public $afterCommit = true;
/**
* Handle the client "created" event.
*
@ -30,7 +33,7 @@ class ClientObserver
->exists();
if ($subscriptions) {
WebhookHandler::dispatch(Webhook::EVENT_CREATE_CLIENT, $client, $client->company)->delay(now()->addSeconds(2));
WebhookHandler::dispatch(Webhook::EVENT_CREATE_CLIENT, $client, $client->company)->delay(now()->addSeconds(rand(1,5)));
}
}
@ -42,29 +45,43 @@ class ClientObserver
*/
public function updated(Client $client)
{
nlog("updated event {$client->id}");
$event = Webhook::EVENT_UPDATE_CLIENT;
if($client->is_deleted)
$event = Webhook::EVENT_DELETE_CLIENT; //this event works correctly.
$subscriptions = Webhook::where('company_id', $client->company->id)
->where('event_id', Webhook::EVENT_UPDATE_CLIENT)
->where('event_id', $event)
->exists();
if ($subscriptions) {
WebhookHandler::dispatch(Webhook::EVENT_UPDATE_CLIENT, $client, $client->company)->delay(now()->addSeconds(2));
WebhookHandler::dispatch($event, $client, $client->company)->delay(now()->addSeconds(rand(1,5)));
}
}
/**
* Handle the client "deleted" event.
* Handle the client "archived" event.
*
* @param Client $client
* @return void
*/
public function deleted(Client $client)
{
if($client->is_deleted)
return;
nlog("deleted event {$client->id}");
$subscriptions = Webhook::where('company_id', $client->company->id)
->where('event_id', Webhook::EVENT_DELETE_CLIENT)
->where('event_id', Webhook::EVENT_ARCHIVE_CLIENT)
->exists();
if ($subscriptions) {
WebhookHandler::dispatch(Webhook::EVENT_DELETE_CLIENT, $client, $client->company)->delay(now()->addSeconds(2));
WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_CLIENT, $client, $client->company)->delay(now()->addSeconds(rand(1,5)));
}
}
@ -76,7 +93,18 @@ class ClientObserver
*/
public function restored(Client $client)
{
//
nlog("Restored {$client->id}");
$subscriptions = Webhook::where('company_id', $client->company->id)
->where('event_id', Webhook::EVENT_RESTORE_CLIENT)
->exists();
if ($subscriptions) {
WebhookHandler::dispatch(Webhook::EVENT_RESTORE_CLIENT, $client, $client->company)->delay(now()->addSeconds(rand(1,5)));
}
return false;
}
/**

View File

@ -32,7 +32,7 @@ class CreditObserver
->exists();
if ($subscriptions) {
WebhookHandler::dispatch(Webhook::EVENT_CREATE_CREDIT, $credit, $credit->company)->delay(now()->addSeconds(2));
WebhookHandler::dispatch(Webhook::EVENT_CREATE_CREDIT, $credit, $credit->company)->delay(now()->addSeconds(rand(1,5)));
}
}
@ -49,7 +49,7 @@ class CreditObserver
->exists();
if ($subscriptions) {
WebhookHandler::dispatch(Webhook::EVENT_UPDATE_CREDIT, $credit, $credit->company)->delay(now()->addSeconds(2));
WebhookHandler::dispatch(Webhook::EVENT_UPDATE_CREDIT, $credit, $credit->company)->delay(now()->addSeconds(rand(1,5)));
}
}
@ -66,7 +66,7 @@ class CreditObserver
->exists();
if ($subscriptions) {
WebhookHandler::dispatch(Webhook::EVENT_DELETE_CREDIT, $credit, $credit->company)->delay(now()->addSeconds(2));
WebhookHandler::dispatch(Webhook::EVENT_DELETE_CREDIT, $credit, $credit->company)->delay(now()->addSeconds(rand(1,5)));
}
}

View File

@ -30,7 +30,7 @@ class ExpenseObserver
->exists();
if ($subscriptions) {
WebhookHandler::dispatch(Webhook::EVENT_CREATE_EXPENSE, $expense, $expense->company)->delay(now()->addSeconds(2));
WebhookHandler::dispatch(Webhook::EVENT_CREATE_EXPENSE, $expense, $expense->company)->delay(now()->addSeconds(rand(1,5)));
}
}
@ -47,7 +47,7 @@ class ExpenseObserver
->exists();
if ($subscriptions) {
WebhookHandler::dispatch(Webhook::EVENT_UPDATE_EXPENSE, $expense, $expense->company)->delay(now()->addSeconds(2));
WebhookHandler::dispatch(Webhook::EVENT_UPDATE_EXPENSE, $expense, $expense->company)->delay(now()->addSeconds(rand(1,5)));
}
}
@ -64,7 +64,7 @@ class ExpenseObserver
->exists();
if ($subscriptions) {
WebhookHandler::dispatch(Webhook::EVENT_DELETE_EXPENSE, $expense, $expense->company)->delay(now()->addSeconds(2));
WebhookHandler::dispatch(Webhook::EVENT_DELETE_EXPENSE, $expense, $expense->company)->delay(now()->addSeconds(rand(1,5)));
}
}

View File

@ -34,7 +34,7 @@ class InvoiceObserver
->exists();
if ($subscriptions) {
WebhookHandler::dispatch(Webhook::EVENT_CREATE_INVOICE, $invoice, $invoice->company, 'client')->delay(now()->addSeconds(2));
WebhookHandler::dispatch(Webhook::EVENT_CREATE_INVOICE, $invoice, $invoice->company, 'client')->delay(now()->addSeconds(rand(1,5)));
}
}
@ -51,7 +51,7 @@ class InvoiceObserver
->exists();
if ($subscriptions) {
WebhookHandler::dispatch(Webhook::EVENT_UPDATE_INVOICE, $invoice, $invoice->company, 'client')->delay(now()->addSeconds(2));
WebhookHandler::dispatch(Webhook::EVENT_UPDATE_INVOICE, $invoice, $invoice->company, 'client')->delay(now()->addSeconds(rand(1,5)));
}
}
@ -68,7 +68,7 @@ class InvoiceObserver
->exists();
if ($subscriptions) {
WebhookHandler::dispatch(Webhook::EVENT_DELETE_INVOICE, $invoice, $invoice->company, 'client')->delay(now()->addSeconds(2));
WebhookHandler::dispatch(Webhook::EVENT_DELETE_INVOICE, $invoice, $invoice->company, 'client')->delay(now()->addSeconds(rand(1,5)));
}
}

View File

@ -32,7 +32,7 @@ class ProjectObserver
->exists();
if ($subscriptions) {
WebhookHandler::dispatch(Webhook::EVENT_PROJECT_CREATE, $project, $project->company, 'client')->delay(now()->addSeconds(2));
WebhookHandler::dispatch(Webhook::EVENT_PROJECT_CREATE, $project, $project->company, 'client')->delay(now()->addSeconds(rand(1,5)));
}
}
@ -49,7 +49,7 @@ class ProjectObserver
->exists();
if ($subscriptions) {
WebhookHandler::dispatch(Webhook::EVENT_PROJECT_UPDATE, $project, $project->company, 'client')->delay(now()->addSeconds(2));
WebhookHandler::dispatch(Webhook::EVENT_PROJECT_UPDATE, $project, $project->company, 'client')->delay(now()->addSeconds(rand(1,5)));
}
}
@ -67,7 +67,7 @@ class ProjectObserver
->exists();
if ($subscriptions) {
WebhookHandler::dispatch(Webhook::EVENT_PROJECT_DELETE, $project, $project->company, 'client')->delay(now()->addSeconds(2));
WebhookHandler::dispatch(Webhook::EVENT_PROJECT_DELETE, $project, $project->company, 'client')->delay(now()->addSeconds(rand(1,5)));
}
}

View File

@ -32,7 +32,7 @@ class QuoteObserver
if ($subscriptions) {
$quote->load('client');
WebhookHandler::dispatch(Webhook::EVENT_CREATE_QUOTE, $quote, $quote->company, 'client')->delay(now()->addSeconds(2));
WebhookHandler::dispatch(Webhook::EVENT_CREATE_QUOTE, $quote, $quote->company, 'client')->delay(now()->addSeconds(rand(1,5)));
}
}
@ -50,7 +50,7 @@ class QuoteObserver
if ($subscriptions) {
$quote->load('client');
WebhookHandler::dispatch(Webhook::EVENT_UPDATE_QUOTE, $quote, $quote->company, 'client')->delay(now()->addSeconds(2));
WebhookHandler::dispatch(Webhook::EVENT_UPDATE_QUOTE, $quote, $quote->company, 'client')->delay(now()->addSeconds(rand(1,5)));
}
}
@ -68,7 +68,7 @@ class QuoteObserver
if ($subscriptions) {
$quote->load('client');
WebhookHandler::dispatch(Webhook::EVENT_DELETE_QUOTE, $quote, $quote->company, 'client')->delay(now()->addSeconds(2));
WebhookHandler::dispatch(Webhook::EVENT_DELETE_QUOTE, $quote, $quote->company, 'client')->delay(now()->addSeconds(rand(1,5)));
}
}

View File

@ -30,7 +30,7 @@ class TaskObserver
->exists();
if ($subscriptions) {
WebhookHandler::dispatch(Webhook::EVENT_CREATE_TASK, $task, $task->company)->delay(now()->addSeconds(2));
WebhookHandler::dispatch(Webhook::EVENT_CREATE_TASK, $task, $task->company)->delay(now()->addSeconds(rand(1,5)));
}
}
@ -47,7 +47,7 @@ class TaskObserver
->exists();
if ($subscriptions) {
WebhookHandler::dispatch(Webhook::EVENT_UPDATE_TASK, $task, $task->company)->delay(now()->addSeconds(2));
WebhookHandler::dispatch(Webhook::EVENT_UPDATE_TASK, $task, $task->company)->delay(now()->addSeconds(rand(1,5)));
}
}
@ -64,7 +64,7 @@ class TaskObserver
->exists();
if ($subscriptions) {
WebhookHandler::dispatch(Webhook::EVENT_DELETE_TASK, $task, $task->company)->delay(now()->addSeconds(2));
WebhookHandler::dispatch(Webhook::EVENT_DELETE_TASK, $task, $task->company)->delay(now()->addSeconds(rand(1,5)));
}
}

View File

@ -0,0 +1,93 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
*
* @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
{
/**
* Handle the vendor "created" event.
*
* @param Vendor $vendor
* @return void
*/
public function created(Vendor $vendor)
{
$subscriptions = Webhook::where('company_id', $vendor->company->id)
->where('event_id', Webhook::EVENT_CREATE_VENDOR)
->exists();
if ($subscriptions) {
WebhookHandler::dispatch(Webhook::EVENT_CREATE_VENDOR, $vendor, $vendor->company)->delay(now()->addSeconds(rand(1,5)));
}
}
/**
* Handle the vendor "updated" event.
*
* @param Vendor $vendor
* @return void
*/
public function updated(Vendor $vendor)
{
$subscriptions = Webhook::where('company_id', $vendor->company->id)
->where('event_id', Webhook::EVENT_UPDATE_VENDOR)
->exists();
if ($subscriptions) {
WebhookHandler::dispatch(Webhook::EVENT_UPDATE_VENDOR, $vendor, $vendor->company)->delay(now()->addSeconds(rand(1,5)));
}
}
/**
* Handle the vendor "deleted" event.
*
* @param Vendor $vendor
* @return void
*/
public function deleted(Vendor $vendor)
{
$subscriptions = Webhook::where('company_id', $vendor->company->id)
->where('event_id', Webhook::EVENT_DELETE_VENDOR)
->exists();
if ($subscriptions) {
WebhookHandler::dispatch(Webhook::EVENT_DELETE_VENDOR, $vendor, $vendor->company)->delay(now()->addSeconds(rand(1,5)));
}
}
/**
* 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)
{
//
}
}

View File

@ -241,6 +241,7 @@ use App\Models\Quote;
use App\Models\Subscription;
use App\Models\Task;
use App\Models\User;
use App\Models\Vendor;
use App\Models\VendorContact;
use App\Observers\AccountObserver;
use App\Observers\ClientContactObserver;
@ -258,9 +259,10 @@ use App\Observers\ProposalObserver;
use App\Observers\PurchaseOrderObserver;
use App\Observers\QuoteObserver;
use App\Observers\SubscriptionObserver;
use App\Observers\VendorContactObserver;
use App\Observers\TaskObserver;
use App\Observers\UserObserver;
use App\Observers\VendorContactObserver;
use App\Observers\VendorObserver;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Mail\Events\MessageSending;
use Illuminate\Mail\Events\MessageSent;
@ -651,6 +653,7 @@ class EventServiceProvider extends ServiceProvider
Quote::observe(QuoteObserver::class);
Task::observe(TaskObserver::class);
User::observe(UserObserver::class);
Vendor::observe(VendorObserver::class);
VendorContact::observe(VendorContactObserver::class);
PurchaseOrder::observe(PurchaseOrderObserver::class);
}

View File

@ -55,7 +55,7 @@ class BaseRepository
/**
* @param $entity
*/
public function archive($entity): void
public function archive($entity)
{
if ($entity->trashed()) {
return;
@ -67,8 +67,9 @@ class BaseRepository
if (class_exists($className)) {
event(new $className($entity, $entity->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
$this->handleWebhook($entity, false);
}
// $this->handleWebhook($entity, 'ARCHIVE');
}
/**
@ -87,109 +88,70 @@ class BaseRepository
if ($entity->is_deleted) {
$fromDeleted = true;
$entity->is_deleted = false;
$entity->save();
$entity->saveQuietly();
}
$className = $this->getEventClass($entity, 'Restored');
if (class_exists($className)) {
event(new $className($entity, $fromDeleted, $entity->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
$this->handleWebhook($entity, true);
}
// $this->handleWebhook($entity, 'RESTORE');
}
private function handleWebhook($entity, $restore)
private function handleWebhook($entity, $action): void
{
switch(true) {
$includes = '';
$event = null;
switch($entity) {
case $entity instanceof Invoice:
if ($restore){
$webhookEvent = Webhook::EVENT_RESTORE_INVOICE;
}
else {
$webhookEvent = Webhook::EVENT_ARCHIVE_INVOICE;}
$event = "EVENT_{$action}_INVOICE";
$includes = 'client';
break;
case $entity instanceof Quote:
if ($restore){
$webhookEvent = Webhook::EVENT_RESTORE_QUOTE;
}
else {
$webhookEvent = Webhook::EVENT_ARCHIVE_QUOTE;
}
break;
$event = "EVENT_{$action}_QUOTE";
$includes = 'client';
case $entity instanceof Credit:
if ($restore){
$webhookEvent = Webhook::EVENT_RESTORE_CREDIT;
}
else {
$webhookEvent = Webhook::EVENT_ARCHIVE_CREDIT;}
$event = "EVENT_{$action}_CREDIT";
$includes = 'client';
break;
case $entity instanceof Payment:
if ($restore){
$webhookEvent = Webhook::EVENT_RESTORE_PAYMENT;
}
else {
$webhookEvent = Webhook::EVENT_ARCHIVE_PAYMENT;}
$event = "EVENT_{$action}_PAYMENT";
$includes = 'invoices,client';
break;
case $entity instanceof Task:
if ($restore){
$webhookEvent = Webhook::EVENT_RESTORE_TASK;
}
else {
$webhookEvent = Webhook::EVENT_ARCHIVE_TASK;}
$event = "EVENT_{$action}_TASK";
$includes = '';
break;
case $entity instanceof Project:
if ($restore){
$webhookEvent = Webhook::EVENT_RESTORE_PROJECT;
}
else {
$webhookEvent = Webhook::EVENT_ARCHIVE_PROJECT;}
$event = "EVENT_{$action}PROJECT";
$includes = 'client';
break;
case $entity instanceof Client:
if ($restore){
$webhookEvent = Webhook::EVENT_RESTORE_CLIENT;
}
else{
$webhookEvent = Webhook::EVENT_ARCHIVE_CLIENT;
}
$event = "EVENT_{$action}_CLIENT";
$includes = '';
break;
case $entity instanceof Expense:
if ($restore){
$webhookEvent= Webhook::EVENT_RESTORE_EXPENSE;
}
else{
$webhookEvent = Webhook::EVENT_ARCHIVE_EXPENSE;
}
$event = "EVENT_{$action}_EXPENSE";
$includes = '';
break;
case $entity instanceof Vendor:
if ($restore){
$webhookEvent = Webhook::EVENT_RESTORE_VENDOR;
}
else {
$webhookEvent = Webhook::EVENT_ARCHIVE_VENDOR;
}
$event = "EVENT_{$action}_VENDOR";
$includes = '';
break;
}
if (isset($webhookEvent)){
if (isset($event)){
$subscriptions = Webhook::where('company_id', $entity->company_id)
->where('event_id', $webhookEvent)
->where('event_id', $event)
->exists();
if ($subscriptions) {
switch(true){
case $webhookEvent == Webhook::EVENT_RESTORE_PAYMENT:
case $webhookEvent == Webhook::EVENT_ARCHIVE_PAYMENT:
WebhookHandler::dispatch($webhookEvent, $entity, $entity->company, 'invoices,client')->delay(now()->addSeconds(2));
break;
case $webhookEvent == Webhook::EVENT_RESTORE_EXPENSE:
case $webhookEvent == Webhook::EVENT_ARCHIVE_EXPENSE:
case $webhookEvent == Webhook::EVENT_ARCHIVE_CREDIT:
case $webhookEvent == Webhook::EVENT_RESTORE_CREDIT:
case $webhookEvent == Webhook::EVENT_RESTORE_CLIENT:
case $webhookEvent == Webhook::EVENT_ARCHIVE_CLIENT:
WebhookHandler::dispatch($webhookEvent, $entity, $entity->company)->delay(now()->addSeconds(2));
break;
default:
WebhookHandler::dispatch($webhookEvent, $entity, $entity->company, 'client')->delay(now()->addSeconds(2));
}
WebhookHandler::dispatch($event, $entity, $entity->company, $includes)->delay(now()->addSeconds(rand(1,5)));
}
}

View File

@ -11,10 +11,13 @@
namespace Tests\Feature;
use App\Repositories\ClientContactRepository;
use App\Repositories\ClientRepository;
use App\Utils\Traits\MakesHash;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Routing\Middleware\ThrottleRequests;
use Illuminate\Support\Facades\Queue;
use Tests\MockAccountData;
use Tests\TestCase;