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