2019-11-20 23:16:01 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Notifications\Messages;
|
|
|
|
|
2020-09-04 00:50:42 +08:00
|
|
|
use App\Contracts\Notification;
|
2019-11-20 23:16:01 +08:00
|
|
|
use App\Models\Pirep;
|
2021-06-04 22:51:59 +08:00
|
|
|
use App\Notifications\Channels\Discord\DiscordMessage;
|
2021-06-05 04:57:27 +08:00
|
|
|
use App\Notifications\Channels\Discord\DiscordWebhook;
|
2019-11-20 23:16:01 +08:00
|
|
|
use App\Notifications\Channels\MailChannel;
|
2021-06-04 22:51:59 +08:00
|
|
|
use App\Support\Units\Distance;
|
|
|
|
use App\Support\Units\Time;
|
2021-05-20 23:54:07 +08:00
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
2021-06-04 22:51:59 +08:00
|
|
|
use PhpUnitsOfMeasure\Exception\NonNumericValue;
|
|
|
|
use PhpUnitsOfMeasure\Exception\NonStringUnitName;
|
2019-11-20 23:16:01 +08:00
|
|
|
|
2021-05-20 23:54:07 +08:00
|
|
|
class PirepSubmitted extends Notification implements ShouldQueue
|
2019-11-20 23:16:01 +08:00
|
|
|
{
|
|
|
|
use MailChannel;
|
|
|
|
|
|
|
|
private $pirep;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new notification instance.
|
|
|
|
*
|
|
|
|
* @param \App\Models\Pirep $pirep
|
|
|
|
*/
|
|
|
|
public function __construct(Pirep $pirep)
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->pirep = $pirep;
|
|
|
|
|
|
|
|
$this->setMailable(
|
|
|
|
'New PIREP Submitted',
|
|
|
|
'notifications.mail.admin.pirep.submitted',
|
|
|
|
['pirep' => $this->pirep]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-06-04 22:51:59 +08:00
|
|
|
public function via($notifiable)
|
|
|
|
{
|
2021-06-05 04:57:27 +08:00
|
|
|
return ['mail', DiscordWebhook::class];
|
2021-06-04 22:51:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a Discord notification
|
|
|
|
*
|
|
|
|
* @param Pirep $pirep
|
|
|
|
*
|
|
|
|
* @return DiscordMessage|null
|
|
|
|
*/
|
|
|
|
public function toDiscordChannel($pirep): ?DiscordMessage
|
|
|
|
{
|
|
|
|
if (empty(setting('notifications.discord_public_webhook_url'))) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$title = 'Flight '.$pirep->airline->code.$pirep->ident.' Filed';
|
|
|
|
$fields = [
|
|
|
|
'Flight' => $pirep->airline->code.$pirep->ident,
|
|
|
|
'Departure Airport' => $pirep->dpt_airport_id,
|
|
|
|
'Arrival Airport' => $pirep->arr_airport_id,
|
|
|
|
'Equipment' => $pirep->aircraft->ident,
|
|
|
|
'Flight Time (Planned)' => Time::minutesToTimeString($pirep->planned_flight_time),
|
|
|
|
];
|
|
|
|
|
|
|
|
if ($pirep->planned_distance) {
|
|
|
|
try {
|
|
|
|
$planned_distance = new Distance(
|
|
|
|
$pirep->planned_distance,
|
|
|
|
config('phpvms.internal_units.distance')
|
|
|
|
);
|
|
|
|
|
|
|
|
$pd = $planned_distance[$planned_distance->unit].' '.$planned_distance->unit;
|
|
|
|
$fields['Distance (Planned)'] = $pd;
|
2021-09-08 22:51:04 +08:00
|
|
|
} catch (NonNumericValue|NonStringUnitName $e) {
|
2021-06-04 22:51:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$dm = new DiscordMessage();
|
|
|
|
return $dm->webhook(setting('notifications.discord_public_webhook_url'))
|
|
|
|
->success()
|
|
|
|
->title($title)
|
|
|
|
->description($pirep->user->discord_id ? 'Flight by <@'.$pirep->user->discord_id.'>' : '')
|
|
|
|
->url(route('frontend.pireps.show', [$pirep->id]))
|
|
|
|
->author([
|
|
|
|
'name' => $pirep->user->ident.' - '.$pirep->user->name_private,
|
2021-06-09 23:43:08 +08:00
|
|
|
'url' => route('frontend.profile.show', [$pirep->user_id]),
|
2021-06-04 22:51:59 +08:00
|
|
|
'icon_url' => $pirep->user->resolveAvatarUrl(),
|
|
|
|
])
|
|
|
|
->fields($fields);
|
|
|
|
}
|
|
|
|
|
2019-11-20 23:16:01 +08:00
|
|
|
/**
|
|
|
|
* Get the array representation of the notification.
|
|
|
|
*
|
|
|
|
* @param mixed $notifiable
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function toArray($notifiable)
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'pirep_id' => $this->pirep->id,
|
|
|
|
'user_id' => $this->pirep->user_id,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|