From b7a29bb2c7855597f82201f0f16b2045a4cfa89b Mon Sep 17 00:00:00 2001 From: paulwer Date: Wed, 3 Apr 2024 15:01:57 +0200 Subject: [PATCH] fix: getVendor / getClient --- app/Http/Controllers/BrevoController.php | 23 +++++++++++-------- app/Http/Controllers/MailgunController.php | 4 ++++ app/Http/Controllers/PostMarkController.php | 3 +++ app/Jobs/Brevo/ProcessBrevoInboundWebhook.php | 5 ++-- .../Mailgun/ProcessMailgunInboundWebhook.php | 2 ++ .../InboundMail/InboundMailEngine.php | 10 ++++---- 6 files changed, 32 insertions(+), 15 deletions(-) diff --git a/app/Http/Controllers/BrevoController.php b/app/Http/Controllers/BrevoController.php index be4c7e9de2bd..8cb666d24410 100644 --- a/app/Http/Controllers/BrevoController.php +++ b/app/Http/Controllers/BrevoController.php @@ -14,6 +14,7 @@ namespace App\Http\Controllers; use App\Jobs\Brevo\ProcessBrevoInboundWebhook; use App\Jobs\Brevo\ProcessBrevoWebhook; use App\Libraries\MultiDB; +use App\Models\Company; use Illuminate\Http\Request; use Log; @@ -77,6 +78,9 @@ class BrevoController extends BaseController /** * Process Brevo Inbound Webhook. * + * IMPORTANT NOTICE: brevo strips old sended emails, therefore only current attachements are present + * + * IMPORTANT NOTICE: brevo saves the message and attachemnts for later retrieval, therefore we can process it within a async job for performance reasons * * @OA\Post( * path="/api/v1/brevo_inbound_webhook", @@ -187,17 +191,18 @@ class BrevoController extends BaseController $input = $request->all(); // validation for client mail credentials by recipient - // if ($request->has('company')) { - // if (!($request->has('token'))) - // return response()->json(['message' => 'Unauthorized'], 403); + if ($request->has('company_key')) { + if (!($request->has('token'))) + return response()->json(['message' => 'Unauthorized'], 403); - // $company = MultiDB::findAndSetDbByCompanyId($request->has('company')); - // $company_brevo_secret = $company?->settings?->email_sending_method === 'client_brevo' && $company?->settings?->brevo_secret ? $company->settings->brevo_secret : null; - // if (!$company || !$company_brevo_secret || $request->get('token') !== $company_brevo_secret) - // return response()->json(['message' => 'Unauthorized'], 403); + MultiDB::findAndSetDbByCompanyKey($request->has('company_key')); + $company = Company::where('company_key', $request->has('company_key'))->first(); + $company_brevo_secret = $company?->settings?->email_sending_method === 'client_brevo' && $company?->settings?->brevo_secret ? $company->settings->brevo_secret : null; + if (!$company || !$company_brevo_secret || $request->get('token') !== $company_brevo_secret) + return response()->json(['message' => 'Unauthorized'], 403); - // } else if (!($request->has('token') && $request->get('token') == config('services.brevo.secret'))) - // return response()->json(['message' => 'Unauthorized'], 403); + } else if (!($request->has('token') && $request->get('token') == config('services.brevo.secret'))) + return response()->json(['message' => 'Unauthorized'], 403); if (!array_key_exists('items', $input)) { Log::info('Failed: Message could not be parsed, because required parameters are missing.'); diff --git a/app/Http/Controllers/MailgunController.php b/app/Http/Controllers/MailgunController.php index d097f61ed6ac..35f1c8388e6e 100644 --- a/app/Http/Controllers/MailgunController.php +++ b/app/Http/Controllers/MailgunController.php @@ -80,6 +80,10 @@ class MailgunController extends BaseController /** * Process Mailgun Inbound Webhook. * + * IMPORTANT NOTICE: mailgun does NOT strip old sended emails, therefore all past attachements are present + * + * IMPORTANT NOTICE: mailgun saves the message and attachemnts for later retrieval, therefore we can process it within a async job for performance reasons + * * * @OA\Post( * path="/api/v1/mailgun_inbound_webhook", diff --git a/app/Http/Controllers/PostMarkController.php b/app/Http/Controllers/PostMarkController.php index e76782323c33..ab3b9e74a56a 100644 --- a/app/Http/Controllers/PostMarkController.php +++ b/app/Http/Controllers/PostMarkController.php @@ -78,6 +78,9 @@ class PostMarkController extends BaseController /** * Process Postmark Webhook. * + * IMPORTANT NOTICE: postmark does NOT strip old sended emails, therefore also all past attachements are present + * + * IMPORTANT NOTICE: postmark does not saves attachements for later retrieval, therefore we cannot process it within a async job * * @OA\Post( * path="/api/v1/postmark_inbound_webhook", diff --git a/app/Jobs/Brevo/ProcessBrevoInboundWebhook.php b/app/Jobs/Brevo/ProcessBrevoInboundWebhook.php index 957a7a9ae162..6fafc74ee47d 100644 --- a/app/Jobs/Brevo/ProcessBrevoInboundWebhook.php +++ b/app/Jobs/Brevo/ProcessBrevoInboundWebhook.php @@ -33,6 +33,9 @@ class ProcessBrevoInboundWebhook implements ShouldQueue /** * Create a new job instance. + * + * IMPORTANT NOTICE: brevo strips old sended emails, therefore only current attachements are present + * * $input consists of json/serialized-array: * * array ( @@ -111,8 +114,6 @@ class ProcessBrevoInboundWebhook implements ShouldQueue /** * Execute the job. * - * TODO: insert Mail from Storage - * * @return void */ public function handle() diff --git a/app/Jobs/Mailgun/ProcessMailgunInboundWebhook.php b/app/Jobs/Mailgun/ProcessMailgunInboundWebhook.php index ff0792c52e38..c1846df7fcf1 100644 --- a/app/Jobs/Mailgun/ProcessMailgunInboundWebhook.php +++ b/app/Jobs/Mailgun/ProcessMailgunInboundWebhook.php @@ -40,6 +40,8 @@ class ProcessMailgunInboundWebhook implements ShouldQueue /** * Execute the job. * + * IMPORTANT NOTICE: mailgun does NOT strip old sended emails, therefore all past attachements are present + * * Mail from Storage * { * "Content-Type": "multipart/related; boundary=\"00000000000022bfbe0613e8b7f5\"", diff --git a/app/Services/InboundMail/InboundMailEngine.php b/app/Services/InboundMail/InboundMailEngine.php index 14690a4bd5af..3c1db5a0f0a0 100644 --- a/app/Services/InboundMail/InboundMailEngine.php +++ b/app/Services/InboundMail/InboundMailEngine.php @@ -237,16 +237,18 @@ class InboundMailEngine private function getClient(Company $company, InboundMail $email) { $clientContact = ClientContact::where("company_id", $company->id)->where("email", $email->from)->first(); - $client = $clientContact->client(); + if (!$clientContact) + return null; - return $client; + return $clientContact->client(); } private function getVendor(Company $company, InboundMail $email) { $vendorContact = VendorContact::where("company_id", $company->id)->where("email", $email->from)->first(); - $vendor = $vendorContact->vendor(); + if (!$vendorContact) + return null; - return $vendor; + return $vendorContact->vendor(); } private function logBlocked(Company $company, string $data) {