From a569ac5cc6f7cc2ae1a0ed546aa23987e9f564cd Mon Sep 17 00:00:00 2001 From: Rumperuu Date: Tue, 1 Nov 2022 04:07:07 +0000 Subject: [PATCH] Add email digests --- app/Console/Commands/SendDigest.php | 57 ++++++++++++ app/Mail/Digest.php | 91 +++++++++++++++++++ app/Mail/WeeklyDigest.php | 34 ------- config/app.php | 13 +++ resources/views/emails/digest.blade.php | 57 ++++++++++++ .../views/emails/weekly-digest.blade.php | 1 - 6 files changed, 218 insertions(+), 35 deletions(-) create mode 100644 app/Console/Commands/SendDigest.php create mode 100644 app/Mail/Digest.php delete mode 100644 app/Mail/WeeklyDigest.php create mode 100644 resources/views/emails/digest.blade.php delete mode 100644 resources/views/emails/weekly-digest.blade.php diff --git a/app/Console/Commands/SendDigest.php b/app/Console/Commands/SendDigest.php new file mode 100644 index 0000000..fc9040c --- /dev/null +++ b/app/Console/Commands/SendDigest.php @@ -0,0 +1,57 @@ +option('daily') && !$this->option('weekly') && !$this->option('monthly')) { + $this->error('No schedule specified.'); + return; + } + + // These are seperated because I may want to send multiple types + // of digest in a single commend. + if ($this->option('daily')) { + foreach (config('app.daily_digest_recipients') as $recipient) { + Mail::to($recipient)->send(new Digest('daily', config('app.current_trip_id'))); + } + } + + if ($this->option('weekly')) { + foreach (config('app.weekly_digest_recipients') as $recipient) { + Mail::to($recipient)->send(new Digest('weekly', config('app.current_trip_id'))); + } + } + + if ($this->option('monthly')) { + foreach (config('app.monthly_digest_recipients') as $recipient) { + Mail::to($recipient)->send(new Digest('monthly', config('app.current_trip_id'))); + } + } + } +} diff --git a/app/Mail/Digest.php b/app/Mail/Digest.php new file mode 100644 index 0000000..4de5749 --- /dev/null +++ b/app/Mail/Digest.php @@ -0,0 +1,91 @@ +digest_type = $digest_type; + $trip = (new TrackerController)->get_trip_data($trip_id); + + $cutoffDateTime = new DateTime(); + switch ($this->digest_type) { + case 'daily': + $cutoffDateTime->modify('-1 day'); + break; + case 'weekly': + $cutoffDateTime->modify('-1 week'); + break; + case 'monthly': + $cutoffDateTime->modify('-1 month'); + break; + default: + } + + $this->locations = array_filter( + $trip->locations, + function($elem) use ($cutoffDateTime) { + $elemDateTime = new DateTime($elem->created_at); + return $elemDateTime > $cutoffDateTime; + } + ); + + $this->checkins = array_filter( + $trip->checkins, + function($elem) use ($cutoffDateTime) { + $elemDateTime = new DateTime($elem->created_at); + return $elemDateTime > $cutoffDateTime; + } + ); + } + + /** + * Build the message. + * + * @return $this + */ + public function build() + { + return $this->view('emails.digest') + ->subject("track.bengoldsworthy.net ".ucwords($this->digest_type)." Digest"); + } +} diff --git a/app/Mail/WeeklyDigest.php b/app/Mail/WeeklyDigest.php deleted file mode 100644 index b1f6440..0000000 --- a/app/Mail/WeeklyDigest.php +++ /dev/null @@ -1,34 +0,0 @@ -view('emails.weekly-digest') - ->subject('Weekly Digest'); - } -} diff --git a/config/app.php b/config/app.php index e4de739..99ddef1 100644 --- a/config/app.php +++ b/config/app.php @@ -72,6 +72,19 @@ return [ 'past_trip_ids' => explode(',', env('PAST_TRIP_IDS')), + /* + |-------------------------------------------------------------------------- + | Digest Mailing Lists + |-------------------------------------------------------------------------- + | + | The the mailing lists for the various digests. + | + */ + + 'daily_digest_recipients' => explode(',', env('DAILY_DIGEST_TO')), + 'weekly_digest_recipients' => explode(',', env('WEEKLY_DIGEST_TO')), + 'monthly_digest_recipients' => explode(',', env('MONTHLY_DIGEST_TO')), + /* |-------------------------------------------------------------------------- | Application Timezone diff --git a/resources/views/emails/digest.blade.php b/resources/views/emails/digest.blade.php new file mode 100644 index 0000000..131247d --- /dev/null +++ b/resources/views/emails/digest.blade.php @@ -0,0 +1,57 @@ + + +

track.bengoldsworthy.net {{ ucwords($digest_type) }} Digest

+

Generated: {{ (new DateTime())->format('D jS F Y') }}

+

View the tracker here.

+
+

Checkins

+
    +@foreach($checkins as $checkin) +
  1. +
    + +

    {!! $checkin->title ?? "Untitled" !!}

    +

    {!! render_date_difference($checkin->date) !!}

    +
    + @if($checkin->note) + {!! $checkin->note !!} + @endif + @if($checkin->image_url) + + @endif +
    +
  2. +@endforeach +
diff --git a/resources/views/emails/weekly-digest.blade.php b/resources/views/emails/weekly-digest.blade.php deleted file mode 100644 index 100d283..0000000 --- a/resources/views/emails/weekly-digest.blade.php +++ /dev/null @@ -1 +0,0 @@ -

Hello, world