mirror of
				https://github.com/invoiceninja/invoiceninja.git
				synced 2025-11-04 02:47:34 -05:00 
			
		
		
		
	Fixes for observers
This commit is contained in:
		
							parent
							
								
									da245c073a
								
							
						
					
					
						commit
						b260a62a73
					
				@ -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)));
 | 
			
		||||
        
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
 | 
			
		||||
@ -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)));
 | 
			
		||||
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user