2018-03-25 13:46:57 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Notifications;
|
|
|
|
|
2021-10-07 12:32:57 -07:00
|
|
|
use App\Helpers\Helper;
|
2018-07-27 15:27:19 -07:00
|
|
|
use App\Models\Asset;
|
2018-03-25 13:46:57 -07:00
|
|
|
use App\Models\Setting;
|
2018-07-16 14:09:04 -07:00
|
|
|
use App\Models\User;
|
2024-01-23 13:05:39 -08:00
|
|
|
use Exception;
|
2018-03-25 13:46:57 -07:00
|
|
|
use Illuminate\Bus\Queueable;
|
2024-10-16 12:51:19 -07:00
|
|
|
use Illuminate\Notifications\Channels\SlackWebhookChannel;
|
2018-03-25 13:46:57 -07:00
|
|
|
use Illuminate\Notifications\Messages\SlackMessage;
|
|
|
|
use Illuminate\Notifications\Notification;
|
2024-01-30 12:38:17 -08:00
|
|
|
use NotificationChannels\GoogleChat\Card;
|
|
|
|
use NotificationChannels\GoogleChat\Enums\Icon;
|
|
|
|
use NotificationChannels\GoogleChat\Enums\ImageStyle;
|
|
|
|
use NotificationChannels\GoogleChat\GoogleChatChannel;
|
|
|
|
use NotificationChannels\GoogleChat\GoogleChatMessage;
|
|
|
|
use NotificationChannels\GoogleChat\Section;
|
|
|
|
use NotificationChannels\GoogleChat\Widgets\KeyValue;
|
2024-01-22 10:57:22 -08:00
|
|
|
use NotificationChannels\MicrosoftTeams\MicrosoftTeamsChannel;
|
|
|
|
use NotificationChannels\MicrosoftTeams\MicrosoftTeamsMessage;
|
2024-05-29 04:38:15 -07:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2018-03-25 13:46:57 -07:00
|
|
|
class CheckoutAssetNotification extends Notification
|
|
|
|
{
|
|
|
|
use Queueable;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new notification instance.
|
|
|
|
*
|
|
|
|
* @param $params
|
|
|
|
*/
|
2018-07-28 03:45:33 -07:00
|
|
|
public function __construct(Asset $asset, $checkedOutTo, User $checkedOutBy, $acceptance, $note)
|
2018-03-25 13:46:57 -07:00
|
|
|
{
|
2024-10-16 12:51:19 -07:00
|
|
|
$this->settings = Setting::getSettings();
|
|
|
|
$this->item = $asset;
|
|
|
|
$this->admin = $checkedOutBy;
|
|
|
|
$this->note = $note;
|
|
|
|
$this->target = $checkedOutTo;
|
|
|
|
$this->last_checkout = '';
|
|
|
|
$this->expected_checkin = '';
|
|
|
|
|
|
|
|
if ($this->item->last_checkout) {
|
|
|
|
$this->last_checkout = Helper::getFormattedDateObject($this->item->last_checkout, 'date',
|
|
|
|
false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->item->expected_checkin) {
|
|
|
|
$this->expected_checkin = Helper::getFormattedDateObject($this->item->expected_checkin, 'date',
|
|
|
|
false);
|
|
|
|
}
|
2018-03-25 13:46:57 -07:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Get the notification's delivery channels.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function via()
|
|
|
|
{
|
2024-01-30 12:38:17 -08:00
|
|
|
$notifyBy = [];
|
2024-10-16 12:51:19 -07:00
|
|
|
|
|
|
|
if (Setting::getSettings()->webhook_selected === 'google' && Setting::getSettings()->webhook_endpoint) {
|
2024-01-30 12:38:17 -08:00
|
|
|
|
|
|
|
$notifyBy[] = GoogleChatChannel::class;
|
|
|
|
}
|
|
|
|
|
2024-10-16 12:51:19 -07:00
|
|
|
if (Setting::getSettings()->webhook_selected === 'microsoft' && Setting::getSettings()->webhook_endpoint) {
|
2024-01-22 10:57:22 -08:00
|
|
|
|
2024-01-30 12:38:17 -08:00
|
|
|
$notifyBy[] = MicrosoftTeamsChannel::class;
|
2024-01-22 10:57:22 -08:00
|
|
|
}
|
2018-03-25 13:46:57 -07:00
|
|
|
|
|
|
|
|
2024-10-16 12:51:19 -07:00
|
|
|
if (Setting::getSettings()->webhook_selected === 'slack' || Setting::getSettings()->webhook_selected === 'general' ) {
|
2024-02-08 14:59:25 -08:00
|
|
|
|
2024-05-29 04:38:15 -07:00
|
|
|
Log::debug('use webhook');
|
2024-10-16 12:51:19 -07:00
|
|
|
$notifyBy[] = SlackWebhookChannel::class;
|
2018-03-25 13:46:57 -07:00
|
|
|
}
|
2018-07-16 14:09:04 -07:00
|
|
|
|
2018-03-25 13:46:57 -07:00
|
|
|
return $notifyBy;
|
|
|
|
}
|
|
|
|
|
2024-10-16 12:51:19 -07:00
|
|
|
public function toSlack() :SlackMessage
|
2018-03-25 13:46:57 -07:00
|
|
|
{
|
|
|
|
$target = $this->target;
|
|
|
|
$admin = $this->admin;
|
|
|
|
$item = $this->item;
|
|
|
|
$note = $this->note;
|
2024-10-16 12:51:19 -07:00
|
|
|
$botname = ($this->settings->webhook_botname) ?: 'Snipe-Bot';
|
2023-06-02 02:59:47 -07:00
|
|
|
$channel = ($this->settings->webhook_channel) ? $this->settings->webhook_channel : '';
|
2018-03-25 13:46:57 -07:00
|
|
|
|
|
|
|
$fields = [
|
|
|
|
'To' => '<'.$target->present()->viewUrl().'|'.$target->present()->fullName().'>',
|
|
|
|
'By' => '<'.$admin->present()->viewUrl().'|'.$admin->present()->fullName().'>',
|
|
|
|
];
|
|
|
|
|
2024-10-16 12:51:19 -07:00
|
|
|
if (($this->expected_checkin) && ($this->expected_checkin !== '')) {
|
2018-03-25 13:46:57 -07:00
|
|
|
$fields['Expected Checkin'] = $this->expected_checkin;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (new SlackMessage)
|
2023-05-15 09:01:30 -07:00
|
|
|
->content(':arrow_up: :computer: '.trans('mail.Asset_Checkout_Notification'))
|
2018-03-25 13:46:57 -07:00
|
|
|
->from($botname)
|
2023-06-02 02:59:47 -07:00
|
|
|
->to($channel)
|
2018-03-25 13:46:57 -07:00
|
|
|
->attachment(function ($attachment) use ($item, $note, $admin, $fields) {
|
|
|
|
$attachment->title(htmlspecialchars_decode($item->present()->name), $item->present()->viewUrl())
|
|
|
|
->fields($fields)
|
|
|
|
->content($note);
|
|
|
|
});
|
|
|
|
}
|
2024-01-22 10:57:22 -08:00
|
|
|
public function toMicrosoftTeams()
|
|
|
|
{
|
2024-01-24 10:38:06 -08:00
|
|
|
$target = $this->target;
|
2024-01-22 10:57:22 -08:00
|
|
|
$admin = $this->admin;
|
|
|
|
$item = $this->item;
|
|
|
|
$note = $this->note;
|
|
|
|
|
2024-01-24 10:38:06 -08:00
|
|
|
return MicrosoftTeamsMessage::create()
|
|
|
|
->to($this->settings->webhook_endpoint)
|
|
|
|
->type('success')
|
|
|
|
->title(trans('mail.Asset_Checkout_Notification'))
|
|
|
|
->addStartGroupToSection('activityText')
|
|
|
|
->fact(trans('mail.assigned_to'), $target->present()->name)
|
|
|
|
->fact(htmlspecialchars_decode($item->present()->name), '', 'activityText')
|
|
|
|
->fact(trans('mail.Asset_Checkout_Notification') . " by ", $admin->present()->fullName())
|
2024-01-24 14:49:42 -08:00
|
|
|
->fact(trans('mail.notes'), $note ?: '');
|
2024-01-24 10:38:06 -08:00
|
|
|
|
2024-01-23 13:05:39 -08:00
|
|
|
|
2024-01-22 10:57:22 -08:00
|
|
|
}
|
2024-01-30 12:38:17 -08:00
|
|
|
public function toGoogleChat()
|
|
|
|
{
|
|
|
|
$target = $this->target;
|
|
|
|
$item = $this->item;
|
|
|
|
$note = $this->note;
|
|
|
|
|
|
|
|
return GoogleChatMessage::create()
|
|
|
|
->to($this->settings->webhook_endpoint)
|
|
|
|
->card(
|
|
|
|
Card::create()
|
|
|
|
->header(
|
|
|
|
'<strong>'.trans('mail.Asset_Checkout_Notification').'</strong>' ?: '',
|
|
|
|
htmlspecialchars_decode($item->present()->name) ?: '',
|
|
|
|
)
|
|
|
|
->section(
|
|
|
|
Section::create(
|
|
|
|
KeyValue::create(
|
|
|
|
trans('mail.assigned_to') ?: '',
|
|
|
|
$target->present()->name ?: '',
|
|
|
|
$note ?: '',
|
|
|
|
)
|
|
|
|
->onClick(route('users.show', $target->id))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
2018-03-25 13:46:57 -07:00
|
|
|
}
|