Enable sending to manager

This commit is contained in:
Marcus Moore
2025-11-19 17:08:00 -08:00
parent 53ff367473
commit 333ebb88b9
2 changed files with 50 additions and 13 deletions

View File

@@ -4,6 +4,8 @@ namespace App\Listeners;
use App\Events\CheckoutablesCheckedOutInBulk;
use App\Mail\BulkAssetCheckoutMail;
use App\Models\Asset;
use App\Models\Location;
use App\Models\Setting;
use Exception;
use Illuminate\Support\Collection;
@@ -26,9 +28,11 @@ class CheckoutablesCheckedOutInBulkListener
$shouldSendEmailToUser = $this->shouldSendCheckoutEmailToUser($event->assets);
$shouldSendEmailToAlertAddress = $this->shouldSendEmailToAlertAddress($event->assets);
if ($shouldSendEmailToUser && $event->target->email) {
$notifiableUser = $this->getNotifiableUser($event);
if ($shouldSendEmailToUser && $notifiableUser) {
try {
Mail::to($event->target)->send(new BulkAssetCheckoutMail(
Mail::to($notifiableUser)->send(new BulkAssetCheckoutMail(
$event->assets,
$event->target,
$event->admin,
@@ -94,4 +98,19 @@ class CheckoutablesCheckedOutInBulkListener
);
}
private function getNotifiableUser(CheckoutablesCheckedOutInBulk $event)
{
$target = $event->target;
if ($target instanceof Asset) {
$target->load('assignedTo');
return $target->assignedto;
}
if ($target instanceof Location) {
return $target->manager;
}
return $target;
}
}

View File

@@ -9,6 +9,7 @@ use App\Models\Location;
use App\Models\User;
use Illuminate\Support\Facades\Mail;
use PHPUnit\Framework\Attributes\Group;
use RuntimeException;
use Tests\TestCase;
#[Group('notifications')]
@@ -155,23 +156,40 @@ class BulkCheckoutEmailTest extends TestCase
private function sendRequest()
{
$types = [
User::class => 'user',
Location::class => 'location',
Asset::class => 'asset',
];
$this->actingAs($this->admin)
->followingRedirects()
->post(route('hardware.bulkcheckout.store'), [
->post(route('hardware.bulkcheckout.store'), array_merge([
'selected_assets' => $this->assets->pluck('id')->toArray(),
'checkout_to_type' => $types[get_class($this->target)],
'assigned_user' => $this->target->id,
'assigned_asset' => null,
'checkout_at' => now()->subWeek()->format('Y-m-d'),
'expected_checkin' => now()->addWeek()->format('Y-m-d'),
'note' => null,
])
], $this->getAssignedArray()))
->assertOk();
}
private function getAssignedArray(): array
{
if ($this->target instanceof User) {
return [
'checkout_to_type' => 'user',
'assigned_user' => $this->target->id,
];
}
if ($this->target instanceof Location) {
return [
'checkout_to_type' => 'location',
'assigned_location' => $this->target->id,
];
}
if ($this->target instanceof Asset) {
return [
'checkout_to_type' => 'asset',
'assigned_asset' => $this->target->id,
];
}
throw new RuntimeException('invalid target type');
}
}