From e05db368411ca5e6b723868d7e20e9ad8144fe1a Mon Sep 17 00:00:00 2001 From: paulwer Date: Sat, 16 Dec 2023 09:17:25 +0100 Subject: [PATCH] wip: first initial setup for creating expenses from webhooks --- .../Mail/Webhook/BaseWebhookHandler.php | 27 ++++- .../Postmark/PostmarkWebhookHandler.php | 100 ++++++++++++++++-- 2 files changed, 120 insertions(+), 7 deletions(-) diff --git a/app/Helpers/Mail/Webhook/BaseWebhookHandler.php b/app/Helpers/Mail/Webhook/BaseWebhookHandler.php index b579ad3bd9e6..e8ae7fe3d871 100644 --- a/app/Helpers/Mail/Webhook/BaseWebhookHandler.php +++ b/app/Helpers/Mail/Webhook/BaseWebhookHandler.php @@ -11,16 +11,41 @@ namespace App\Helpers\Mail\Webhook; +use App\Factory\ExpenseFactory; use App\Models\Company; +use App\Utils\Traits\GeneratesCounter; +use App\Utils\Traits\SavesDocuments; interface BaseWebhookHandler { + use GeneratesCounter; + use SavesDocuments; public function process() { } + protected function createExpense(string $email, string $subject, string $plain_message, string $html_message, string $date, array $documents) + { + $company = $this->matchCompany($email); + if (!$company) + return false; - protected function matchCompany(string $email) + $expense = ExpenseFactory::create($company->id, $company->owner()->id); + + $expense->public_notes = $subject; + $expense->private_notes = $plain_message; + $expense->date = $date; + + // TODO: add html_message as document to the expense + + $this->saveDocuments($documents, $expense); + + $expense->saveQuietly(); + + return $expense; + } + + private function matchCompany(string $email) { return Company::where("expense_mailbox", $email)->first(); } diff --git a/app/Helpers/Mail/Webhook/Postmark/PostmarkWebhookHandler.php b/app/Helpers/Mail/Webhook/Postmark/PostmarkWebhookHandler.php index 51e458cb57d8..e64064191845 100644 --- a/app/Helpers/Mail/Webhook/Postmark/PostmarkWebhookHandler.php +++ b/app/Helpers/Mail/Webhook/Postmark/PostmarkWebhookHandler.php @@ -16,16 +16,104 @@ use App\Helpers\Mail\Webhook\BaseWebhookHandler; interface PostmarkWebhookHandler extends BaseWebhookHandler { + // { + // "FromName": "Postmarkapp Support", + // "MessageStream": "inbound", + // "From": "support@postmarkapp.com", + // "FromFull": { + // "Email": "support@postmarkapp.com", + // "Name": "Postmarkapp Support", + // "MailboxHash": "" + // }, + // "To": "\"Firstname Lastname\" ", + // "ToFull": [ + // { + // "Email": "yourhash+SampleHash@inbound.postmarkapp.com", + // "Name": "Firstname Lastname", + // "MailboxHash": "SampleHash" + // } + // ], + // "Cc": "\"First Cc\" , secondCc@postmarkapp.com>", + // "CcFull": [ + // { + // "Email": "firstcc@postmarkapp.com", + // "Name": "First Cc", + // "MailboxHash": "" + // }, + // { + // "Email": "secondCc@postmarkapp.com", + // "Name": "", + // "MailboxHash": "" + // } + // ], + // "Bcc": "\"First Bcc\" , secondbcc@postmarkapp.com>", + // "BccFull": [ + // { + // "Email": "firstbcc@postmarkapp.com", + // "Name": "First Bcc", + // "MailboxHash": "" + // }, + // { + // "Email": "secondbcc@postmarkapp.com", + // "Name": "", + // "MailboxHash": "" + // } + // ], + // "OriginalRecipient": "yourhash+SampleHash@inbound.postmarkapp.com", + // "Subject": "Test subject", + // "MessageID": "73e6d360-66eb-11e1-8e72-a8904824019b", + // "ReplyTo": "replyto@postmarkapp.com", + // "MailboxHash": "SampleHash", + // "Date": "Fri, 1 Aug 2014 16:45:32 -04:00", + // "TextBody": "This is a test text body.", + // "HtmlBody": "

This is a test html body.<\/p><\/body><\/html>", + // "StrippedTextReply": "This is the reply text", + // "Tag": "TestTag", + // "Headers": [ + // { + // "Name": "X-Header-Test", + // "Value": "" + // }, + // { + // "Name": "X-Spam-Status", + // "Value": "No" + // }, + // { + // "Name": "X-Spam-Score", + // "Value": "-0.1" + // }, + // { + // "Name": "X-Spam-Tests", + // "Value": "DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,SPF_PASS" + // } + // ], + // "Attachments": [ + // { + // "Name": "test.txt", + // "Content": "VGhpcyBpcyBhdHRhY2htZW50IGNvbnRlbnRzLCBiYXNlLTY0IGVuY29kZWQu", + // "ContentType": "text/plain", + // "ContentLength": 45 + // } + // ] + // } public function process($data) { - $email = ''; + $from = $data["From"]; + $subject = $data["Subject"]; + $plain_message = $data["TextBody"]; + $html_message = $data["HtmlBody"]; + $date = $data["Date"]; // TODO + $attachments = $data["Attachments"]; // TODO - $company = $this->matchCompany($email); - if (!$company) - return false; - - $expense = ExpenseFactory::create($company->id, $company->owner()->id); + return $this->createExpense( + $from, // from + $subject, // subject + $plain_message, // plain_message + $html_message, // html_message + $date, // date + $attachments, // attachments + ); } }