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;
use App\Events\CheckoutableCheckedOut;
use App\Mail\CheckinAccessoryMail;
use App\Mail\CheckoutAccessoryMail;
use App\Mail\CheckoutAssetMail;
use App\Mail\CheckinAssetMail;
use App\Models\Accessory;
@ -47,14 +49,7 @@ class CheckoutableListener
*/
$acceptance = $this->getCheckoutAcceptance($event);
$notifiable = $this->getNotifiable($event);
$mailable = (new CheckoutAssetMail(
$event->checkoutable,
$event->checkedOutTo,
$event->checkedOutBy,
$event->note,
$acceptance,
));
$mailable = $this->getCheckoutMailType($event, $acceptance);
// Send email notifications
try {
if (!$event->checkedOutTo->locale){
@ -102,12 +97,7 @@ class CheckoutableListener
}
$notifiable = $this->getNotifiable($event);
$mailable = (new CheckInAssetMail(
$event->checkoutable,
$event->checkedOutTo,
$event->checkedInBy,
$event->note,
));
$mailable = $this->getCheckinMailType($event);
// Send email notifications
try {
@ -233,9 +223,32 @@ class CheckoutableListener
break;
}
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.

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

View file

@ -6,6 +6,7 @@ use App\Models\Accessory;
use App\Models\Setting;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Channels\SlackWebhookChannel;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Messages\SlackMessage;
use Illuminate\Notifications\Notification;
@ -55,35 +56,7 @@ class CheckoutAccessoryNotification extends Notification
}
if (Setting::getSettings()->webhook_selected == 'slack' || Setting::getSettings()->webhook_selected == 'general' ) {
$notifyBy[] = 'slack';
}
/**
* 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';
}
$notifyBy[] = SlackWebhookChannel::class;
}
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\ResetPasswordController;
use App\Livewire\Importer;
use App\Models\Accessory;
use App\Models\Asset;
use App\Models\User;
use Illuminate\Support\Facades\Route;
@ -56,7 +57,7 @@ Route::group(['middleware' => 'auth'], function () {
* Locations
*/
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);
$target = User::find(2);
$acceptance = null; // Simulate acceptance data
@ -67,11 +68,10 @@ Route::group(['middleware' => 'auth'], function () {
$fields = $item->model->fieldset->fields;
}
return new \App\Mail\CheckinAssetMail(
return new \App\Mail\CheckinAccessoryMail(
$item,
$admin,
$target,
$acceptance,
$note);
});