2022-11-01 04:07:07 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Mail;
|
|
|
|
|
|
|
|
use DateTime;
|
|
|
|
use App\Http\Controllers\TrackerController;
|
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Mail\Mailable;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
|
|
class Digest extends Mailable
|
|
|
|
{
|
|
|
|
use Queueable, SerializesModels;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The type of digest (e.g. weekly, monthly, etc.).
|
|
|
|
* TODO: Replace with enum
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $digest_type;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The current trip locations as a JSON object.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $locations;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The current trip checkins as a JSON object.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2022-11-28 19:49:14 +00:00
|
|
|
public $checkinsList;
|
2022-11-01 04:07:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new message instance.
|
|
|
|
*
|
|
|
|
* @param string $digest_type
|
|
|
|
* @param string $trip_id
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct(string $digest_type, string $trip_id)
|
|
|
|
{
|
|
|
|
$this->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;
|
2022-11-10 02:15:42 +00:00
|
|
|
case 'fortnightly':
|
|
|
|
$cutoffDateTime->modify('-2 weeks');
|
|
|
|
break;
|
2022-11-01 04:07:07 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2022-11-28 19:49:14 +00:00
|
|
|
$this->checkinsList = array_filter(
|
|
|
|
$trip->checkinsList,
|
2022-11-01 04:07:07 +00:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|