wip: first initial setup for creating expenses from webhooks

This commit is contained in:
paulwer 2023-12-16 09:17:25 +01:00
parent 0e24ac0a14
commit e05db36841
2 changed files with 120 additions and 7 deletions

View File

@ -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();
}

View File

@ -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\" <yourhash+SampleHash@inbound.postmarkapp.com>",
// "ToFull": [
// {
// "Email": "yourhash+SampleHash@inbound.postmarkapp.com",
// "Name": "Firstname Lastname",
// "MailboxHash": "SampleHash"
// }
// ],
// "Cc": "\"First Cc\" <firstcc@postmarkapp.com>, secondCc@postmarkapp.com>",
// "CcFull": [
// {
// "Email": "firstcc@postmarkapp.com",
// "Name": "First Cc",
// "MailboxHash": ""
// },
// {
// "Email": "secondCc@postmarkapp.com",
// "Name": "",
// "MailboxHash": ""
// }
// ],
// "Bcc": "\"First Bcc\" <firstbcc@postmarkapp.com>, 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": "<html><body><p>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
);
}
}