2020-05-04 13:13:46 +02:00
|
|
|
<?php
|
2020-11-11 01:13:39 +01:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2023-01-28 23:21:40 +01:00
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-11-11 01:13:39 +01:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-11-11 01:13:39 +01:00
|
|
|
*/
|
2021-06-10 03:15:21 +02:00
|
|
|
|
2020-05-04 13:13:46 +02:00
|
|
|
namespace App\Jobs\Util;
|
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
use App\Libraries\MultiDB;
|
2023-01-31 11:05:01 +01:00
|
|
|
use App\Models\Company;
|
2020-10-28 11:10:49 +01:00
|
|
|
use App\Models\Webhook;
|
2022-12-13 23:25:05 +01:00
|
|
|
use App\Utils\Ninja;
|
2020-05-04 13:13:46 +02:00
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
2023-01-31 11:05:01 +01:00
|
|
|
|
2020-07-06 13:22:36 +02:00
|
|
|
class WebhookHandler implements ShouldQueue
|
2020-05-04 13:13:46 +02:00
|
|
|
{
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
2020-11-22 12:14:49 +01:00
|
|
|
|
2023-01-29 06:19:20 +01:00
|
|
|
public $tries = 1; //number of retries
|
2020-11-22 12:14:49 +01:00
|
|
|
|
|
|
|
public $deleteWhenMissingModels = true;
|
|
|
|
|
2020-05-04 13:13:46 +02:00
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param $event_id
|
|
|
|
* @param $entity
|
2020-05-04 13:13:46 +02:00
|
|
|
*/
|
2023-02-16 02:36:09 +01:00
|
|
|
public function __construct(private int $event_id, private $entity, private Company $company, private string $includes = '')
|
|
|
|
{
|
|
|
|
}
|
2020-05-04 13:13:46 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
*/
|
2020-11-22 12:14:49 +01:00
|
|
|
public function handle()
|
2022-12-13 23:25:05 +01:00
|
|
|
{
|
2020-11-17 00:04:10 +01:00
|
|
|
MultiDB::setDb($this->company->db);
|
|
|
|
|
2022-12-13 23:25:05 +01:00
|
|
|
//If the company is disabled, or if on hosted, the user is not a paid hosted user return early
|
|
|
|
if (! $this->company || $this->company->is_disabled || (Ninja::isHosted() && !$this->company->account->isPaidHostedClient())) {
|
2020-08-24 13:53:22 +02:00
|
|
|
return true;
|
2020-11-25 15:19:52 +01:00
|
|
|
}
|
2020-11-22 12:14:49 +01:00
|
|
|
|
2023-08-08 10:56:31 +02:00
|
|
|
Webhook::query()
|
|
|
|
->where('company_id', $this->company->id)
|
2023-01-31 11:05:01 +01:00
|
|
|
->where('event_id', $this->event_id)
|
|
|
|
->cursor()
|
|
|
|
->each(function ($subscription) {
|
2023-02-16 02:36:09 +01:00
|
|
|
WebhookSingle::dispatch($subscription->id, $this->entity, $this->company->db, $this->includes);
|
|
|
|
});
|
2021-09-20 23:52:21 +02:00
|
|
|
}
|
|
|
|
|
2023-01-30 08:28:19 +01:00
|
|
|
public function failed($exception = null)
|
2020-07-01 08:03:46 +02:00
|
|
|
{
|
|
|
|
}
|
2020-05-04 13:13:46 +02:00
|
|
|
}
|