Refactor for observers

This commit is contained in:
David Bomba 2023-02-01 01:06:21 +11:00
parent 399b397ecf
commit b8d97a69a2
23 changed files with 103 additions and 63 deletions

View File

@ -47,27 +47,27 @@ class Handler extends ExceptionHandler
* @var array
*/
protected $dontReport = [
PDOException::class,
MaxAttemptsExceededException::class,
CommandNotFoundException::class,
ValidationException::class,
ModelNotFoundException::class,
NotFoundHttpException::class,
// PDOException::class,
// MaxAttemptsExceededException::class,
// CommandNotFoundException::class,
// ValidationException::class,
// ModelNotFoundException::class,
// NotFoundHttpException::class,
];
protected $selfHostDontReport = [
FilePermissionsFailure::class,
PDOException::class,
MaxAttemptsExceededException::class,
CommandNotFoundException::class,
ValidationException::class,
ModelNotFoundException::class,
NotFoundHttpException::class,
UnableToCreateDirectory::class,
GuzzleHttp\Exception\ConnectException::class,
Symfony\Component\Process\Exception\RuntimeException::class,
InvalidArgumentException::class,
RuntimeException::class,
// FilePermissionsFailure::class,
// PDOException::class,
// MaxAttemptsExceededException::class,
// CommandNotFoundException::class,
// ValidationException::class,
// ModelNotFoundException::class,
// NotFoundHttpException::class,
// UnableToCreateDirectory::class,
// GuzzleHttp\Exception\ConnectException::class,
// Symfony\Component\Process\Exception\RuntimeException::class,
// InvalidArgumentException::class,
// RuntimeException::class,
];
protected $hostedDontReport = [

View File

@ -422,16 +422,6 @@ class InvoiceController extends BaseController
event(new InvoiceWasUpdated($invoice, $invoice->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
// $transaction = [
// 'invoice' => $invoice->transaction_event(),
// 'payment' => [],
// 'client' => $invoice->client->transaction_event(),
// 'credit' => [],
// 'metadata' => [],
// ];
// TransactionLog::dispatch(TransactionEvent::INVOICE_UPDATED, $transaction, $invoice->company->db);
return $this->itemResponse($invoice);
}

View File

@ -160,9 +160,5 @@ class AdjustProductInventory implements ShouldQueue
});
// $nmo->to_user = $this->company->owner();
// NinjaMailerJob::dispatch($nmo);
}
}

View File

@ -131,7 +131,7 @@ class UpdateOrCreateProduct implements ShouldQueue
$product->company_id = $this->invoice->company_id;
$product->project_id = $this->invoice->project_id;
$product->vendor_id = $this->invoice->vendor_id;
$product->save();
$product->saveQuietly();
}
}

View File

@ -15,11 +15,14 @@ use App\Jobs\Util\SystemLogger;
use App\Libraries\MultiDB;
use App\Models\Client as ClientModel;
use App\Models\Company;
use App\Models\Product;
use App\Models\SystemLog;
use App\Models\Webhook;
use App\Transformers\ArraySerializer;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\BadResponseException;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ServerException;
use GuzzleHttp\RequestOptions;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
@ -28,8 +31,6 @@ use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use League\Fractal\Manager;
use League\Fractal\Resource\Item;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ServerException;
class WebhookSingle implements ShouldQueue
{
@ -78,9 +79,15 @@ class WebhookSingle implements ShouldQueue
MultiDB::setDb($this->db);
$subscription = Webhook::with('company')->find($this->subscription_id);
if($subscription)
nlog("firing event ID {$subscription->event_id}");
if(!$subscription){
$this->fail();
nlog("failed to fire event, could not find webhook ID {$this->subscription_id}");
return;
}
@ -237,17 +244,19 @@ class WebhookSingle implements ShouldQueue
private function resolveClient()
{
//make sure it isn't an instance of the Client Model
if ((! $this->entity instanceof ClientModel) && $this->entity->client()->exists()) {
if (! $this->entity instanceof ClientModel && ! $this->entity instanceof Product && $this->entity->client()->exists()) {
return $this->entity->client;
}
return $this->company->clients()->first();
return false;
}
public function failed($exception = null)
{
if($exception)
if($exception){
nlog("failed in webhooksingle");
nlog($exception->getMessage());
}
}
}

View File

@ -38,21 +38,18 @@ class CreateInvoicePdf implements ShouldQueue
if (isset($event->invoice)) {
$event->invoice->invitations->each(function ($invitation) {
// CreateEntityPdf::dispatch($invitation->load('invoice', 'contact.client.company'));
(new CreateEntityPdf($invitation->load('invoice', 'contact.client.company')))->handle();
});
}
if (isset($event->quote)) {
$event->quote->invitations->each(function ($invitation) {
// CreateEntityPdf::dispatch($invitation->load('quote', 'contact.client.company'));
(new CreateEntityPdf($invitation->load('quote', 'contact.client.company')))->handle();
});
}
if (isset($event->credit)) {
$event->credit->invitations->each(function ($invitation) {
// CreateEntityPdf::dispatch($invitation->load('credit', 'contact.client.company'));
(new CreateEntityPdf($invitation->load('credit', 'contact.client.company')))->handle();
});

View File

@ -212,7 +212,7 @@ class Quote extends BaseModel
$this->invitations->each(function ($invitation) {
if (! isset($invitation->sent_date)) {
$invitation->sent_date = Carbon::now();
$invitation->save();
$invitation->saveQuietly();
}
});
}

View File

@ -18,11 +18,11 @@ class Webhook extends BaseModel
use SoftDeletes;
use Filterable;
const EVENT_CREATE_CLIENT = 1;
const EVENT_CREATE_CLIENT = 1; //tested
const EVENT_CREATE_INVOICE = 2;
const EVENT_CREATE_INVOICE = 2; //tested
const EVENT_CREATE_QUOTE = 3;
const EVENT_CREATE_QUOTE = 3; //tested
const EVENT_CREATE_PAYMENT = 4;
@ -32,9 +32,9 @@ class Webhook extends BaseModel
const EVENT_DELETE_QUOTE = 7;
const EVENT_UPDATE_INVOICE = 8;
const EVENT_UPDATE_INVOICE = 8; //tested
const EVENT_DELETE_INVOICE = 9;
const EVENT_DELETE_INVOICE = 9; //tested
const EVENT_UPDATE_CLIENT = 10;
@ -82,7 +82,7 @@ class Webhook extends BaseModel
const EVENT_ARCHIVE_PAYMENT = 32;
const EVENT_ARCHIVE_INVOICE = 33;
const EVENT_ARCHIVE_INVOICE = 33; //tested
const EVENT_ARCHIVE_QUOTE = 34;

View File

@ -17,6 +17,7 @@ use App\Models\Webhook;
class ClientObserver
{
public $afterCommit = true;
/**
* Handle the client "created" event.

View File

@ -19,6 +19,9 @@ use App\Models\Webhook;
class CreditObserver
{
public $afterCommit = true;
/**
* Handle the client "created" event.
*

View File

@ -17,6 +17,8 @@ use App\Models\Webhook;
class ExpenseObserver
{
public $afterCommit = true;
/**
* Handle the expense "created" event.
*

View File

@ -17,6 +17,9 @@ use App\Models\Webhook;
class PaymentObserver
{
public $afterCommit = true;
/**
* Handle the payment "created" event.
*

View File

@ -17,6 +17,9 @@ use App\Models\Webhook;
class ProductObserver
{
public $afterCommit = true;
/**
* Handle the product "created" event.
*

View File

@ -17,6 +17,9 @@ use App\Models\Webhook;
class PurchaseOrderObserver
{
public $afterCommit = true;
/**
* Handle the client "created" event.
*

View File

@ -17,6 +17,9 @@ use App\Models\Webhook;
class QuoteObserver
{
public $afterCommit = true;
/**
* Handle the quote "created" event.
*
@ -42,7 +45,7 @@ class QuoteObserver
* @return void
*/
public function updated(Quote $quote)
{
{nlog("updated");
$event = Webhook::EVENT_UPDATE_QUOTE;

View File

@ -17,6 +17,9 @@ use App\Models\Webhook;
class TaskObserver
{
public $afterCommit = true;
/**
* Handle the task "created" event.
*

View File

@ -17,6 +17,9 @@ use App\Models\Webhook;
class VendorObserver
{
public $afterCommit = true;
/**
* Handle the vendor "created" event.
*

View File

@ -231,8 +231,9 @@ class BaseRepository
$invitation_class = sprintf('App\\Models\\%sInvitation', $resource);
$invitation = $invitation_class::where('key', $invitation)->first();
if ($invitation)
if ($invitation){
$invitation->delete();
}
});
@ -257,7 +258,6 @@ class BaseRepository
->first();
if ($new_invitation && $new_invitation->trashed()) {
$new_invitation->restore();
} else {
@ -267,7 +267,7 @@ class BaseRepository
$new_invitation->{$lcfirst_resource_id} = $model->id;
$new_invitation->client_contact_id = $contact->id;
$new_invitation->key = $this->createDbHash($model->company->db);
$new_invitation->save();
$new_invitation->saveQuietly();
}
}
@ -350,7 +350,6 @@ class BaseRepository
$model = $model->calc()->getQuote();
if($this->new_model)
event('eloquent.created: App\Models\Quote', $model);
else
@ -371,9 +370,7 @@ class BaseRepository
event('eloquent.updated: App\Models\RecurringInvoice', $model);
}
$model->save();
// nlog("save time = ". microtime(true) - $start);
$model->saveQuietly();
return $model->fresh();
}

View File

@ -156,8 +156,8 @@ class MarkInvoiceDeleted extends AbstractService
$this->invoice->number = $number;
//wipe references to invoices from related entities.
$this->invoice->tasks()->update(['invoice_id' => null]);
$this->invoice->expenses()->update(['invoice_id' => null]);
$this->invoice->tasks()->updateQuietly(['invoice_id' => null]);
$this->invoice->expenses()->updateQuietly(['invoice_id' => null]);
return $this;
}

View File

@ -37,12 +37,10 @@ class ApplyNumber
switch ($this->client->getSetting('counter_number_applied')) {
case 'when_saved':
$quote = $this->trySaving($quote);
// $quote->number = $this->getNextQuoteNumber($this->client, $quote);
break;
case 'when_sent':
if ($quote->status_id == Quote::STATUS_SENT) {
$quote = $this->trySaving($quote);
// $quote->number = $this->getNextQuoteNumber($this->client, $quote);
}
break;

View File

@ -52,7 +52,7 @@ class CreateInvitations
$ii->key = $this->createDbHash($this->quote->company->db);
$ii->quote_id = $this->quote->id;
$ii->client_contact_id = $contact->id;
$ii->save();
$ii->saveQuietly();
} elseif ($invitation && ! $contact->send_email) {
$invitation->delete();
}
@ -81,7 +81,7 @@ class CreateInvitations
$ii->key = $this->createDbHash($this->quote->company->db);
$ii->quote_id = $this->quote->id;
$ii->client_contact_id = $contact->id;
$ii->save();
$ii->saveQuietly();
}
return $this->quote->fresh();
@ -93,6 +93,6 @@ class CreateInvitations
$new_contact->client_id = $this->quote->client_id;
$new_contact->contact_key = Str::random(40);
$new_contact->is_primary = true;
$new_contact->save();
$new_contact->saveQuietly();
}
}

View File

@ -58,6 +58,7 @@ class ClientApiTest extends TestCase
}
public function testCrossCompanyBulkActionsFail()
{

View File

@ -11,6 +11,7 @@
namespace Tests\Feature;
use App\Jobs\Util\WebhookHandler;
use App\Repositories\ClientContactRepository;
use App\Repositories\ClientRepository;
use App\Utils\Traits\MakesHash;
@ -48,6 +49,33 @@ class WebhookAPITest extends TestCase
$this->withoutExceptionHandling();
}
// public function testClientWebhooks()
// {
// // client archived = 37
// $data = [
// 'target_url' => 'http://hook.com',
// 'event_id' => 37,
// 'rest_method' => 'post',
// 'format' => 'JSON',
// ];
// $response = $this->withHeaders([
// 'X-API-SECRET' => config('ninja.api_secret'),
// 'X-API-TOKEN' => $this->token,
// ])->post('/api/v1/webhooks', $data);
// $repo = new ClientRepository(new ClientContactRepository());
// $repo->archive($this->client);
// \Illuminate\Support\Facades\Queue::after(function (WebhookHandler $event) {
// $this->assertTrue($event->job->isReleased());
// });
// \Illuminate\Support\Facades\Queue::assertPushed(WebhookHandler::class);
// }
public function testWebhookGetFilter()
{
$response = $this->withHeaders([