From d05d4449f73ee91d06b5f32271b6ea24d13e2b75 Mon Sep 17 00:00:00 2001 From: Louis Jeckel Date: Tue, 15 Sep 2020 12:40:57 +0200 Subject: [PATCH] add unsubscribe event --- app/Events/DispatchMailgunEvent.php | 12 ++++++++++-- app/MailgunEvent.php | 10 ++++++++++ app/User.php | 10 ++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/app/Events/DispatchMailgunEvent.php b/app/Events/DispatchMailgunEvent.php index 2a89d92..b3fd576 100644 --- a/app/Events/DispatchMailgunEvent.php +++ b/app/Events/DispatchMailgunEvent.php @@ -4,6 +4,7 @@ namespace App\Events; use App\EmailBatch; use App\MailgunEvent; +use App\User; use Illuminate\Broadcasting\Channel; use Illuminate\Broadcasting\InteractsWithSockets; use Illuminate\Broadcasting\PresenceChannel; @@ -51,6 +52,7 @@ class DispatchMailgunEvent implements ShouldBroadcast abort(200); } + /** @var MailgunEvent $event */ $event = MailgunEvent::query()->updateOrCreate([ 'event' => Arr::get($webhookData, 'event-data.event'), 'recipient' => Arr::get($webhookData, 'event-data.recipient'), @@ -61,10 +63,16 @@ class DispatchMailgunEvent implements ShouldBroadcast $this->broadcast = ($event->wasRecentlyCreated); - $event->touch(); - $this->data = $event; + + if($event->event === 'unsubscribe') { + $event->user->unsubscribe(); + } + + + + } /** diff --git a/app/MailgunEvent.php b/app/MailgunEvent.php index 3b656d9..99161fc 100644 --- a/app/MailgunEvent.php +++ b/app/MailgunEvent.php @@ -10,6 +10,8 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo; * Class MailgunEvent * @package App * @property EmailBatch $batch + * @property string $event + * @property User $user */ class MailgunEvent extends Model @@ -28,4 +30,12 @@ class MailgunEvent extends Model { return $this->belongsTo(EmailBatch::class, 'email_batch_id'); } + + /** + * @return BelongsTo + */ + public function recipient(): BelongsTo + { + return $this->belongsTo(User::class, 'recipient', 'email'); + } } diff --git a/app/User.php b/app/User.php index 0b7ef00..a3e69eb 100644 --- a/app/User.php +++ b/app/User.php @@ -62,6 +62,7 @@ class User extends Authenticatable implements MustVerifyEmail 'city', 'phone', 'type', + 'unsubscribed' ]; /** @@ -87,6 +88,7 @@ class User extends Authenticatable implements MustVerifyEmail 'discover_ends_at' => 'datetime', 'self_registered' => 'bool', 'early_access' => 'bool', + 'unsubscribed' => 'bool', ]; @@ -267,6 +269,14 @@ class User extends Authenticatable implements MustVerifyEmail } + /** + * Unsubscribe user + */ + public function unsubscribe(): void + { + $this->update(['unsubscribed' => 1]); + } + -- 2.39.5