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