Refactors for webhooks

This commit is contained in:
David Bomba 2023-02-17 14:47:52 +11:00
parent 7f1a984848
commit 8ae40d7097
3 changed files with 25 additions and 20 deletions

View File

@ -48,4 +48,5 @@ class InvoiceWasPaid
$this->company = $company;
$this->event_vars = $event_vars;
}
}

View File

@ -46,6 +46,7 @@ class WebhookSingle implements ShouldQueue
private string $includes;
private Company $company;
/**
* Create a new job instance.
*
@ -121,41 +122,47 @@ class WebhookSingle implements ShouldQueue
RequestOptions::JSON => $data, // or 'json' => [...]
]);
SystemLogger::dispatch(
(new SystemLogger(
array_merge((array) $response, $data),
SystemLog::CATEGORY_WEBHOOK,
SystemLog::EVENT_WEBHOOK_SUCCESS,
SystemLog::TYPE_WEBHOOK_RESPONSE,
$this->resolveClient(),
$this->company
);
))->handle();
} catch(\GuzzleHttp\Exception\ConnectException $e) {
nlog("connection problem");
nlog($e->getCode());
nlog($e->getMessage());
SystemLogger::dispatch(
(new SystemLogger(
['message' => "Error connecting to ". $subscription->target_url],
SystemLog::CATEGORY_WEBHOOK,
SystemLog::EVENT_WEBHOOK_FAILURE,
SystemLog::TYPE_WEBHOOK_RESPONSE,
$this->resolveClient(),
$this->company
);
))->handle();
} catch (BadResponseException $e) {
if ($e->getResponse()->getStatusCode() >= 400 && $e->getResponse()->getStatusCode() < 500) {
$message = "Server encountered a problem when connecting to {$subscription->target_url} => status code ". $e->getResponse()->getStatusCode(). " scheduling retry.";
$message = "There was a problem when connecting to {$subscription->target_url} => status code ". $e->getResponse()->getStatusCode();
nlog($message);
SystemLogger::dispatch(
(new SystemLogger(
['message' => $message],
SystemLog::CATEGORY_WEBHOOK,
SystemLog::EVENT_WEBHOOK_FAILURE,
SystemLog::TYPE_WEBHOOK_RESPONSE,
$this->resolveClient(),
$this->company
);
))->handle();
/* Some 400's should never be repeated */
if(in_array($e->getResponse()->getStatusCode(), [404, 410])){
$this->fail();
return;
}
$this->release($this->backoff()[$this->attempts()-1]);
}
@ -163,16 +170,16 @@ class WebhookSingle implements ShouldQueue
if ($e->getResponse()->getStatusCode() >= 500) {
nlog("endpoint returned a 500, failing");
$message = "Server encountered a problem when connecting to {$subscription->target_url} => status code ". $e->getResponse()->getStatusCode(). " no retry attempted.";
$message = "The was a problem when connecting to {$subscription->target_url} => status code ". $e->getResponse()->getStatusCode(). " no retry attempted.";
SystemLogger::dispatch(
(new SystemLogger(
['message' => $message],
SystemLog::CATEGORY_WEBHOOK,
SystemLog::EVENT_WEBHOOK_FAILURE,
SystemLog::TYPE_WEBHOOK_RESPONSE,
$this->resolveClient(),
$this->company
);
))->handle();
$this->fail();
return;
@ -181,38 +188,38 @@ class WebhookSingle implements ShouldQueue
nlog("Server exception");
$error = json_decode($e->getResponse()->getBody()->getContents());
SystemLogger::dispatch(
(new SystemLogger(
['message' => $error],
SystemLog::CATEGORY_WEBHOOK,
SystemLog::EVENT_WEBHOOK_FAILURE,
SystemLog::TYPE_WEBHOOK_RESPONSE,
$this->resolveClient(),
$this->company
);
))->handle();
} catch (ClientException $e) {
nlog("Client exception");
$error = json_decode($e->getResponse()->getBody()->getContents());
SystemLogger::dispatch(
(new SystemLogger(
['message' => $error],
SystemLog::CATEGORY_WEBHOOK,
SystemLog::EVENT_WEBHOOK_FAILURE,
SystemLog::TYPE_WEBHOOK_RESPONSE,
$this->resolveClient(),
$this->company
);
))->handle();
} catch (\Exception $e) {
nlog("Exception handler => " . $e->getMessage());
nlog($e->getCode());
SystemLogger::dispatch(
(new SystemLogger(
$e->getMessage(),
SystemLog::CATEGORY_WEBHOOK,
SystemLog::EVENT_WEBHOOK_FAILURE,
SystemLog::TYPE_WEBHOOK_RESPONSE,
$this->resolveClient(),
$this->company,
);
))->handle();
$this->release($this->backoff()[$this->attempts()-1]);
}
@ -220,7 +227,6 @@ class WebhookSingle implements ShouldQueue
private function resolveClient()
{
nlog(get_class($this->entity));
//make sure it isn't an instance of the Client Model
if (!$this->entity instanceof \App\Models\Client &&
@ -231,8 +237,8 @@ class WebhookSingle implements ShouldQueue
return $this->entity->client;
}
return null;
}
public function failed($exception = null)

View File

@ -416,7 +416,6 @@ class EventServiceProvider extends ServiceProvider
],
InvoiceWasPaid::class => [
InvoicePaidActivity::class,
CreateInvoicePdf::class,
],
InvoiceWasViewed::class => [
InvoiceViewedActivity::class,
@ -434,7 +433,6 @@ class EventServiceProvider extends ServiceProvider
],
InvoiceWasDeleted::class => [
InvoiceDeletedActivity::class,
CreateInvoicePdf::class,
],
InvoiceWasArchived::class => [
InvoiceArchivedActivity::class,