adds Checkin and Checkout mailables and listner logic

This commit is contained in:
Godfrey M 2024-10-16 15:27:34 -07:00
parent 9710436d54
commit dcdf600b78
7 changed files with 162 additions and 105 deletions

View file

@ -3,6 +3,8 @@
namespace App\Listeners; namespace App\Listeners;
use App\Events\CheckoutableCheckedOut; use App\Events\CheckoutableCheckedOut;
use App\Mail\CheckinAccessoryMail;
use App\Mail\CheckoutAccessoryMail;
use App\Mail\CheckoutAssetMail; use App\Mail\CheckoutAssetMail;
use App\Mail\CheckinAssetMail; use App\Mail\CheckinAssetMail;
use App\Models\Accessory; use App\Models\Accessory;
@ -47,14 +49,7 @@ class CheckoutableListener
*/ */
$acceptance = $this->getCheckoutAcceptance($event); $acceptance = $this->getCheckoutAcceptance($event);
$notifiable = $this->getNotifiable($event); $notifiable = $this->getNotifiable($event);
$mailable = (new CheckoutAssetMail( $mailable = $this->getCheckoutMailType($event, $acceptance);
$event->checkoutable,
$event->checkedOutTo,
$event->checkedOutBy,
$event->note,
$acceptance,
));
// Send email notifications // Send email notifications
try { try {
if (!$event->checkedOutTo->locale){ if (!$event->checkedOutTo->locale){
@ -102,12 +97,7 @@ class CheckoutableListener
} }
$notifiable = $this->getNotifiable($event); $notifiable = $this->getNotifiable($event);
$mailable = (new CheckInAssetMail( $mailable = $this->getCheckinMailType($event);
$event->checkoutable,
$event->checkedOutTo,
$event->checkedInBy,
$event->note,
));
// Send email notifications // Send email notifications
try { try {
@ -233,9 +223,32 @@ class CheckoutableListener
break; break;
} }
return new $notificationClass($event->checkoutable, $event->checkedOutTo, $event->checkedOutBy, $acceptance, $event->note); return new $notificationClass($event->checkoutable, $event->checkedOutTo, $event->checkedOutBy, $acceptance, $event->note);
} }
private function getCheckoutMailType($event, $acceptance){
$lookup = [
Accessory::class => CheckoutAccessoryMail::class,
Asset::class => CheckoutAssetMail::class,
// Consumable::class =>
// LicenseSeat::class =>
];
$mailable= $lookup[get_class($event->checkoutable)];
return new $mailable($event->checkoutable, $event->checkedOutTo, $event->checkedOutBy, $event->note, $acceptance);
}
private function getCheckinMailType($event){
$lookup = [
Accessory::class => CheckinAccessoryMail::class,
Asset::class => CheckinAssetMail::class,
// Consumable::class =>
// LicenseSeat::class =>
];
$mailable= $lookup[get_class($event->checkoutable)];
return new $mailable($event->checkoutable, $event->checkedOutTo, $event->checkedInBy, $event->note);
}
/** /**
* Register the listeners for the subscriber. * Register the listeners for the subscriber.

View file

@ -0,0 +1,98 @@
<?php
namespace App\Mail;
use App\Models\Accessory;
use App\Models\Setting;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Address;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
class CheckoutAccessoryMail extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*/
public function __construct(Accessory $accessory, $checkedOutTo, User $checkedOutBy,$note, $acceptance)
{
$this->item = $accessory;
$this->admin = $checkedOutBy;
$this->note = $note;
$this->checkout_qty = $accessory->checkout_qty;
$this->target = $checkedOutTo;
$this->acceptance = $acceptance;
$this->settings = Setting::getSettings();
}
/**
* Get the message envelope.
*/
public function envelope(): Envelope
{
$from = null;
$cc = [];
if (!empty(Setting::getSettings()->alert_email)) {
$from = new Address(Setting::getSettings()->alert_email);
}
if (!empty(Setting::getSettings()->admin_cc_email)) {
$cc[] = new Address(Setting::getSettings()->admin_cc_email);
}
return new Envelope(
from: $from ?? new Address('default@example.com', 'Default Sender'),
cc: $cc,
subject: (trans('mail.Accessory_Checkout_Notification')),
);
}
/**
* Get the message content definition.
*/
public function content(): Content
{
Log::debug($this->item->getImageUrl());
$eula = $this->item->getEula();
$req_accept = $this->item->requireAcceptance();
$accept_url = is_null($this->acceptance) ? null : route('account.accept.item', $this->acceptance);
// Check if the item has custom fields associated with it
if (($this->item->model) && ($this->item->model->fieldset)) {
$fields = $this->item->model->fieldset->fields;
}
$accept_url = is_null($this->acceptance) ? null : route('account.accept.item', $this->acceptance);
return new Content(
markdown: 'mail.markdown.checkout-accessory',
with: [
'item' => $this->item,
'admin' => $this->admin,
'note' => $this->note,
'target' => $this->target,
'eula' => $eula,
'req_accept' => $req_accept,
'accept_url' => $accept_url,
'checkout_qty' => $this->checkout_qty,
],
);
}
/**
* Get the attachments for the message.
*
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
*/
public function attachments(): array
{
return [];
}
}

View file

@ -58,18 +58,18 @@ class CheckinAccessoryNotification extends Notification
$notifyBy[] = 'slack'; $notifyBy[] = 'slack';
} }
/** // /**
* Only send notifications to users that have email addresses // * Only send notifications to users that have email addresses
*/ // */
if ($this->target instanceof User && $this->target->email != '') { // if ($this->target instanceof User && $this->target->email != '') {
Log::debug('The target is a user'); // Log::debug('The target is a user');
//
if ($this->item->checkin_email()) { // if ($this->item->checkin_email()) {
$notifyBy[] = 'mail'; // $notifyBy[] = 'mail';
} // }
} // }
//
Log::debug('checkin_email on this category is '.$this->item->checkin_email()); // Log::debug('checkin_email on this category is '.$this->item->checkin_email());
return $notifyBy; return $notifyBy;
} }
@ -143,23 +143,23 @@ class CheckinAccessoryNotification extends Notification
} }
/** // /**
* Get the mail representation of the notification. // * Get the mail representation of the notification.
* // *
* @param mixed $notifiable // * @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage // * @return \Illuminate\Notifications\Messages\MailMessage
*/ // */
public function toMail() // public function toMail()
{ // {
Log::debug('to email called'); // Log::debug('to email called');
//
return (new MailMessage)->markdown('notifications.markdown.checkin-accessory', // return (new MailMessage)->markdown('notifications.markdown.checkin-accessory',
[ // [
'item' => $this->item, // 'item' => $this->item,
'admin' => $this->admin, // 'admin' => $this->admin,
'note' => $this->note, // 'note' => $this->note,
'target' => $this->target, // 'target' => $this->target,
]) // ])
->subject(trans('mail.Accessory_Checkin_Notification')); // ->subject(trans('mail.Accessory_Checkin_Notification'));
} // }
} }

View file

@ -6,6 +6,7 @@ use App\Models\Accessory;
use App\Models\Setting; use App\Models\Setting;
use App\Models\User; use App\Models\User;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Channels\SlackWebhookChannel;
use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Messages\SlackMessage; use Illuminate\Notifications\Messages\SlackMessage;
use Illuminate\Notifications\Notification; use Illuminate\Notifications\Notification;
@ -55,35 +56,7 @@ class CheckoutAccessoryNotification extends Notification
} }
if (Setting::getSettings()->webhook_selected == 'slack' || Setting::getSettings()->webhook_selected == 'general' ) { if (Setting::getSettings()->webhook_selected == 'slack' || Setting::getSettings()->webhook_selected == 'general' ) {
$notifyBy[] = 'slack'; $notifyBy[] = SlackWebhookChannel::class;
}
/**
* Only send notifications to users that have email addresses
*/
if ($this->target instanceof User && $this->target->email != '') {
/**
* Send an email if the asset requires acceptance,
* so the user can accept or decline the asset
*/
if ($this->item->requireAcceptance()) {
$notifyBy[1] = 'mail';
}
/**
* Send an email if the item has a EULA, since the user should always receive it
*/
if ($this->item->getEula()) {
$notifyBy[1] = 'mail';
}
/**
* Send an email if an email should be sent at checkin/checkout
*/
if ($this->item->checkin_email()) {
$notifyBy[1] = 'mail';
}
} }
return $notifyBy; return $notifyBy;
@ -163,31 +136,4 @@ class CheckoutAccessoryNotification extends Notification
} }
/**
* Get the mail representation of the notification.
*
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail()
{
Log::debug($this->item->getImageUrl());
$eula = $this->item->getEula();
$req_accept = $this->item->requireAcceptance();
$accept_url = is_null($this->acceptance) ? null : route('account.accept.item', $this->acceptance);
return (new MailMessage)->markdown('notifications.markdown.checkout-accessory',
[
'item' => $this->item,
'admin' => $this->admin,
'note' => $this->note,
'target' => $this->target,
'eula' => $eula,
'req_accept' => $req_accept,
'accept_url' => $accept_url,
'checkout_qty' => $this->checkout_qty,
])
->subject(trans('mail.Confirm_accessory_delivery'));
}
} }

View file

@ -24,6 +24,7 @@ use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\Auth\ForgotPasswordController; use App\Http\Controllers\Auth\ForgotPasswordController;
use App\Http\Controllers\Auth\ResetPasswordController; use App\Http\Controllers\Auth\ResetPasswordController;
use App\Livewire\Importer; use App\Livewire\Importer;
use App\Models\Accessory;
use App\Models\Asset; use App\Models\Asset;
use App\Models\User; use App\Models\User;
use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Route;
@ -56,7 +57,7 @@ Route::group(['middleware' => 'auth'], function () {
* Locations * Locations
*/ */
Route::get('/test-email', function() { Route::get('/test-email', function() {
$item = Asset::find(1); // Load some test data $item = Accessory::find(1); // Load some test data
$admin = User::find(1); $admin = User::find(1);
$target = User::find(2); $target = User::find(2);
$acceptance = null; // Simulate acceptance data $acceptance = null; // Simulate acceptance data
@ -67,11 +68,10 @@ Route::group(['middleware' => 'auth'], function () {
$fields = $item->model->fieldset->fields; $fields = $item->model->fieldset->fields;
} }
return new \App\Mail\CheckinAssetMail( return new \App\Mail\CheckinAccessoryMail(
$item, $item,
$admin, $admin,
$target, $target,
$acceptance,
$note); $note);
}); });