fixes for webhook-handlers

This commit is contained in:
paulwer 2024-02-18 17:50:33 +01:00
parent 4e180db731
commit 33560bd6a6
2 changed files with 61 additions and 50 deletions

View File

@ -52,6 +52,8 @@ class ProcessBrevoWebhook implements ShouldQueue
'events' => [], 'events' => [],
]; ];
private ?Company $company = null;
/** /**
* Create a new job instance. * Create a new job instance.
* *
@ -86,12 +88,12 @@ class ProcessBrevoWebhook implements ShouldQueue
public function handle() public function handle()
{ {
MultiDB::findAndSetDbByCompanyKey($this->request['tags'][0]); MultiDB::findAndSetDbByCompanyKey($this->request['tags'][0]);
$company = Company::where('company_key', $this->request['tags'][0])->first(); $this->company = Company::where('company_key', $this->request['tags'][0])->first();
$this->invitation = $this->discoverInvitation($this->request['message-id']); $this->invitation = $this->discoverInvitation($this->request['message-id']);
if ($company && $this->request['event'] == 'spam' && config('ninja.notification.slack')) { if ($this->company && $this->request['event'] == 'spam' && config('ninja.notification.slack')) {
$company->notification(new EmailSpamNotification($company))->ninja(); $this->company->notification(new EmailSpamNotification($this->company))->ninja();
} }
if (!$this->invitation) { if (!$this->invitation) {
@ -111,7 +113,7 @@ class ProcessBrevoWebhook implements ShouldQueue
case 'blocked': case 'blocked':
if ($this->request['subject'] == ctrans('texts.confirmation_subject')) { if ($this->request['subject'] == ctrans('texts.confirmation_subject')) {
$company->notification(new EmailBounceNotification($this->request['email']))->ninja(); $this->company->notification(new EmailBounceNotification($this->request['email']))->ninja();
} }
return $this->processBounce(); return $this->processBounce();
@ -419,8 +421,10 @@ class ProcessBrevoWebhook implements ShouldQueue
public function getRawMessage(string $message_id) public function getRawMessage(string $message_id)
{ {
$Brevo = new TransactionalEmailsApi(null, Configuration::getDefaultConfiguration()->setApiKey('api-key', config('services.brevo.key'))); $brevo_secret = $this->company->settings->brevo_secret ?? config('services.brevo.key');
$messageDetail = $Brevo->getTransacEmailContent($message_id);
$brevo = new TransactionalEmailsApi(null, Configuration::getDefaultConfiguration()->setApiKey('api-key', $brevo_secret));
$messageDetail = $brevo->getTransacEmailContent($message_id);
return $messageDetail; return $messageDetail;
} }
@ -431,7 +435,6 @@ class ProcessBrevoWebhook implements ShouldQueue
$messageDetail = $this->getRawMessage($message_id); $messageDetail = $this->getRawMessage($message_id);
$event = collect($messageDetail->getEvents())->first(function ($event) { $event = collect($messageDetail->getEvents())->first(function ($event) {
return $event?->Details?->BounceID ?? false; return $event?->Details?->BounceID ?? false;

View File

@ -53,6 +53,8 @@ class ProcessPostmarkWebhook implements ShouldQueue
'events' => [], 'events' => [],
]; ];
private ?Company $company = null;
/** /**
* Create a new job instance. * Create a new job instance.
* *
@ -87,12 +89,12 @@ class ProcessPostmarkWebhook implements ShouldQueue
public function handle() public function handle()
{ {
MultiDB::findAndSetDbByCompanyKey($this->request['Tag']); MultiDB::findAndSetDbByCompanyKey($this->request['Tag']);
$company = Company::where('company_key', $this->request['Tag'])->first(); $this->company = Company::where('company_key', $this->request['Tag'])->first();
$this->invitation = $this->discoverInvitation($this->request['MessageID']); $this->invitation = $this->discoverInvitation($this->request['MessageID']);
if ($company && $this->request['RecordType'] == 'SpamComplaint' && config('ninja.notification.slack')) { if ($this->company && $this->request['RecordType'] == 'SpamComplaint' && config('ninja.notification.slack')) {
$company->notification(new EmailSpamNotification($company))->ninja(); $this->company->notification(new EmailSpamNotification($this->company))->ninja();
} }
if (!$this->invitation) { if (!$this->invitation) {
@ -109,7 +111,7 @@ class ProcessPostmarkWebhook implements ShouldQueue
case 'Bounce': case 'Bounce':
if ($this->request['Subject'] == ctrans('texts.confirmation_subject')) { if ($this->request['Subject'] == ctrans('texts.confirmation_subject')) {
$company->notification(new EmailBounceNotification($this->request['Email']))->ninja(); $this->company->notification(new EmailBounceNotification($this->request['Email']))->ninja();
} }
return $this->processBounce(); return $this->processBounce();
@ -174,14 +176,16 @@ class ProcessPostmarkWebhook implements ShouldQueue
return; return;
} }
(new SystemLogger( (
new SystemLogger(
$data, $data,
SystemLog::CATEGORY_MAIL, SystemLog::CATEGORY_MAIL,
SystemLog::EVENT_MAIL_OPENED, SystemLog::EVENT_MAIL_OPENED,
SystemLog::TYPE_WEBHOOK_RESPONSE, SystemLog::TYPE_WEBHOOK_RESPONSE,
$this->invitation->contact->client, $this->invitation->contact->client,
$this->invitation->company $this->invitation->company
))->handle(); )
)->handle();
} }
// { // {
@ -212,14 +216,16 @@ class ProcessPostmarkWebhook implements ShouldQueue
return; return;
} }
(new SystemLogger( (
new SystemLogger(
$data, $data,
SystemLog::CATEGORY_MAIL, SystemLog::CATEGORY_MAIL,
SystemLog::EVENT_MAIL_DELIVERY, SystemLog::EVENT_MAIL_DELIVERY,
SystemLog::TYPE_WEBHOOK_RESPONSE, SystemLog::TYPE_WEBHOOK_RESPONSE,
$this->invitation->contact->client, $this->invitation->contact->client,
$this->invitation->company $this->invitation->company
))->handle(); )
)->handle();
} }
// { // {
@ -380,7 +386,9 @@ class ProcessPostmarkWebhook implements ShouldQueue
try { try {
$postmark = new PostmarkClient(config('services.postmark.token')); $postmark_secret = $this->company->settings->postmark_secret ?? config('services.postmark.token');
$postmark = new PostmarkClient($postmark_secret);
$messageDetail = $postmark->getOutboundMessageDetails($this->request['MessageID']); $messageDetail = $postmark->getOutboundMessageDetails($this->request['MessageID']);
$recipients = collect($messageDetail['recipients'])->flatten()->implode(','); $recipients = collect($messageDetail['recipients'])->flatten()->implode(',');