Skip to content

Commit

Permalink
Update split workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
riasvdv committed Nov 9, 2022
1 parent 5b19583 commit 462a910
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 1,799 deletions.
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@
}
],
"require": {
"php": "^8.0",
"php": "^8.1",
"illuminate/support": "^9.0",
"symfony/sendgrid-mailer": "^6.0",
"spatie/laravel-mailcoach": "^5.2"
"spatie/laravel-mailcoach": "^6.0"
},
"require-dev": {
"ext-json": "*",
"friendsofphp/php-cs-fixer": "^2.16",
"fakerphp/faker": "^1.12",
"orchestra/testbench": "^7.0",
"phpunit/phpunit": "^9.5",
Expand Down
19 changes: 0 additions & 19 deletions package.json

This file was deleted.

16 changes: 15 additions & 1 deletion src/MailcoachSendgridFeedbackServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,30 @@
use Illuminate\Mail\Events\MessageSending;
use Illuminate\Mail\Events\MessageSent;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
use Symfony\Component\Mailer\Bridge\Sendgrid\Transport\SendgridTransportFactory;
use Symfony\Component\Mailer\Transport\Dsn;

class MailcoachSendgridFeedbackServiceProvider extends ServiceProvider
{
public function register()
{
Route::macro('sendgridFeedback', fn (string $url) => Route::post($url, '\\' . SendgridWebhookController::class));
Route::macro('sendgridFeedback', fn (string $url) => Route::post("{$url}/{mailer?}", '\\' . SendgridWebhookController::class));

Event::listen(MessageSending::class, AddUniqueArgumentsMailHeader::class);
Event::listen(MessageSent::class, StoreTransportMessageId::class);
}

public function boot()
{
Mail::extend('sendgrid', function (array $config) {
$key = $config['key'] ?? config('services.sendgrid.key');

return (new SendgridTransportFactory())->create(
Dsn::fromString("sendgrid+api://{$key}@default")
);
});
}
}
18 changes: 8 additions & 10 deletions src/ProcessSendgridWebhookJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
use Illuminate\Support\Arr;
use Spatie\Mailcoach\Domain\Campaign\Events\WebhookCallProcessedEvent;
use Spatie\Mailcoach\Domain\Shared\Models\Send;
use Spatie\Mailcoach\Domain\Shared\Support\Config;
use Spatie\Mailcoach\Domain\Shared\Traits\UsesMailcoachModels;
use Spatie\Mailcoach\Mailcoach;
use Spatie\WebhookClient\Jobs\ProcessWebhookJob;
use Spatie\WebhookClient\Models\WebhookCall;

Expand All @@ -20,7 +20,7 @@ public function __construct(WebhookCall $webhookCall)

$this->queue = config('mailcoach.campaigns.perform_on_queue.process_feedback_job');

$this->connection = $this->connection ?? Config::getQueueConnection();
$this->connection = $this->connection ?? Mailcoach::getQueueConnection();
}

public function handle()
Expand Down Expand Up @@ -55,18 +55,16 @@ protected function handleRawEvent(array $rawEvent): ?array

protected function getSend(array $rawEvent): ?Send
{
$uuid = Arr::get($rawEvent, 'send_uuid');
$id = Arr::get($rawEvent, 'send_uuid') ?? explode('.', Arr::get($rawEvent, 'sg_message_id'))[0] ?? null;

/** @var class-string<Send> $sendClass */
$sendClass = self::getSendClass();

if ($uuid) {
return $sendClass::findByUuid($uuid);
if (! $id) {
return null;
}

$messageId = explode('.', Arr::get($rawEvent, 'sg_message_id'))[0] ?? null;
/** @var class-string<Send> $sendClass */
$sendClass = self::getSendClass();

return $sendClass::findByTransportMessageId($messageId);
return $sendClass::findByUuid($id) ?? $sendClass::findByTransportMessageId($id);
}

protected function isFirstOfThisSendgridMessage(array $rawEvent): bool
Expand Down
20 changes: 20 additions & 0 deletions src/SendgridWebhookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,35 @@
namespace Spatie\MailcoachSendgridFeedback;

use Illuminate\Http\Request;
use Spatie\Mailcoach\Domain\Shared\Traits\UsesMailcoachModels;

class SendgridWebhookController
{
use UsesMailcoachModels;

public function __invoke(Request $request)
{
$this->registerMailerConfig($request->route('mailer'));

$webhookConfig = SendgridWebhookConfig::get();

(new WebhookProcessor($request, $webhookConfig))->process();

return response()->json(['message' => 'ok']);
}

public function registerMailerConfig(?string $mailer): void
{
if (! $mailer) {
return;
}

$mailer = cache()->remember(
"mailcoach-mailer-{$mailer}",
now()->addMinute(),
fn () => self::getMailerClass()::findByConfigKeyName($mailer),
);

$mailer?->registerConfigValues();
}
}
9 changes: 5 additions & 4 deletions src/StoreTransportMessageId.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ public function handle(MessageSent $event)
return;
}

if (! $event->message->getHeaders()->has('X-Sendgrid-Message-ID')) {
if (! $messageId = $event->sent->getMessageId()) {
return;
}

/** @var \Spatie\Mailcoach\Models\Send $send */
/** @var \Spatie\Mailcoach\Domain\Shared\Models\Send $send */
$send = $event->data['send'];

$transportMessageId = $event->message->getHeaders()->get('X-Sendgrid-Message-ID')->getBodyAsString();
$messageId = ltrim($messageId, '<');
$messageId = rtrim($messageId, '>');

$send->storeTransportMessageId($transportMessageId);
$send->storeTransportMessageId($messageId);
}
}
30 changes: 26 additions & 4 deletions tests/ProcessSendgridWebhookJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function it_can_handle_multiple_events()
(new ProcessSendgridWebhookJob($this->webhookCall))->handle();

$this->assertEquals(2, SendFeedbackItem::count());
$this->assertEquals(SendFeedbackType::BOUNCE, SendFeedbackItem::first()->type);
$this->assertEquals(SendFeedbackType::Bounce, SendFeedbackItem::first()->type);
$this->assertTrue($this->send->is(SendFeedbackItem::first()->send));
}

Expand All @@ -59,7 +59,7 @@ public function it_processes_a_sendgrid_complaint_webhook_call()

$this->assertEquals(1, SendFeedbackItem::count());
tap(SendFeedbackItem::first(), function (SendFeedbackItem $sendFeedbackItem) {
$this->assertEquals(SendFeedbackType::COMPLAINT, $sendFeedbackItem->type);
$this->assertEquals(SendFeedbackType::Complaint, $sendFeedbackItem->type);
$this->assertEquals(Carbon::createFromTimestamp(1574854444), $sendFeedbackItem->created_at);
$this->assertTrue($this->send->is($sendFeedbackItem->send));
});
Expand All @@ -86,6 +86,28 @@ public function it_processes_a_sendgrid_click_webhook_call()
$this->assertCount(1, CampaignLink::first()->clicks);
}

/** @test */
public function it_processes_a_sendgrid_click_webhook_call_with_message_id()
{
$this->send->update(['transport_message_id' => '14c5d75ce93']);

$payload = $this->getStub('clickPayload');
unset($payload['send_uuid']);

$this->webhookCall->update(['payload' => $payload]);
(new ProcessSendgridWebhookJob($this->webhookCall))->handle();

$this->assertEquals(1, CampaignLink::count());
$this->assertEquals('https://example.com', CampaignLink::first()->url);
$this->assertCount(1, CampaignLink::first()->clicks);
$this->assertEquals(Carbon::createFromTimestamp(1574854444), CampaignLink::first()->clicks->first()->created_at);

$this->send->subscriber->update(['email' => '[email protected]']);
(new ProcessSendgridWebhookJob($this->webhookCall))->handle();
$this->assertEquals(1, CampaignLink::count());
$this->assertCount(1, CampaignLink::first()->clicks);
}

/** @test */
public function it_can_process_a_sendgrid_open_webhook_call()
{
Expand All @@ -107,7 +129,7 @@ public function it_can_process_a_sendgrid_bounce_webhook_call()
(new ProcessSendgridWebhookJob($this->webhookCall))->handle();

$this->assertEquals(1, SendFeedbackItem::count());
$this->assertEquals(SendFeedbackType::BOUNCE, SendFeedbackItem::first()->type);
$this->assertEquals(SendFeedbackType::Bounce, SendFeedbackItem::first()->type);
$this->assertTrue($this->send->is(SendFeedbackItem::first()->send));
}

Expand All @@ -126,7 +148,7 @@ public function it_wont_process_a_sendgrid_temporary_bounce_webhook_call()
/** @test */
public function it_will_fire_an_event_when_processing_is_complete()
{
Event::fake();
Event::fake(WebhookCallProcessedEvent::class);

$this->webhookCall->update(['payload' => $this->getStub('openPayload')]);
(new ProcessSendgridWebhookJob($this->webhookCall))->handle();
Expand Down
25 changes: 18 additions & 7 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

namespace Spatie\MailcoachSendgridFeedback\Tests;

use CreateMailCoachTables;
use CreateWebhookCallsTable;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Schema;
use Livewire\LivewireServiceProvider;
use Orchestra\Testbench\TestCase as Orchestra;
use Spatie\Mailcoach\MailcoachServiceProvider;
use Spatie\MailcoachEditor\MailcoachEditorServiceProvider;
use Spatie\MailcoachMailgunFeedback\MailcoachMailgunFeedbackServiceProvider;
use Spatie\MailcoachPostmarkFeedback\MailcoachPostmarkFeedbackServiceProvider;
use Spatie\MailcoachSendgridFeedback\MailcoachSendgridFeedbackServiceProvider;
use Spatie\MailcoachSesFeedback\MailcoachSesFeedbackServiceProvider;

class TestCase extends Orchestra
{
Expand All @@ -30,8 +33,12 @@ protected function getPackageProviders($app)
{
return [
LivewireServiceProvider::class,
MailcoachServiceProvider::class,
MailcoachMailgunFeedbackServiceProvider::class,
MailcoachSesFeedbackServiceProvider::class,
MailcoachSendgridFeedbackServiceProvider::class,
MailcoachPostmarkFeedbackServiceProvider::class,
MailcoachEditorServiceProvider::class,
MailcoachServiceProvider::class,
];
}

Expand All @@ -49,11 +56,15 @@ protected function getEnvironmentSetUp($app)

protected function setUpDatabase()
{
include_once __DIR__.'/../../../vendor/spatie/laravel-mailcoach/database/migrations/create_webhook_calls_table.php.stub';
(new CreateWebhookCallsTable())->up();
if (! Schema::hasTable('webhook_calls')) {
$createWebhookCalls = require __DIR__.'/../../../vendor/spatie/laravel-mailcoach/database/migrations/2022_02_10_000003_create_webhook_calls_table.php';
$createWebhookCalls->up();
}

include_once __DIR__.'/../../../vendor/spatie/laravel-mailcoach/database/migrations/create_mailcoach_tables.php.stub';
(new CreateMailCoachTables())->up();
if (! Schema::hasTable('mailcoach_campaigns')) {
$createMailcoachTables = require __DIR__.'/../../../vendor/spatie/laravel-mailcoach/database/migrations/2022_02_10_000001_create_mailcoach_tables.php';
$createMailcoachTables->up();
}
}

public function getStub(string $name): array
Expand Down
Loading

0 comments on commit 462a910

Please sign in to comment.