Skip to content

Commit 333ebb8

Browse files
committed
Enable sending to manager
1 parent 53ff367 commit 333ebb8

File tree

2 files changed

+50
-13
lines changed

2 files changed

+50
-13
lines changed

app/Listeners/CheckoutablesCheckedOutInBulkListener.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use App\Events\CheckoutablesCheckedOutInBulk;
66
use App\Mail\BulkAssetCheckoutMail;
7+
use App\Models\Asset;
8+
use App\Models\Location;
79
use App\Models\Setting;
810
use Exception;
911
use Illuminate\Support\Collection;
@@ -26,9 +28,11 @@ public function handle(CheckoutablesCheckedOutInBulk $event): void
2628
$shouldSendEmailToUser = $this->shouldSendCheckoutEmailToUser($event->assets);
2729
$shouldSendEmailToAlertAddress = $this->shouldSendEmailToAlertAddress($event->assets);
2830

29-
if ($shouldSendEmailToUser && $event->target->email) {
31+
$notifiableUser = $this->getNotifiableUser($event);
32+
33+
if ($shouldSendEmailToUser && $notifiableUser) {
3034
try {
31-
Mail::to($event->target)->send(new BulkAssetCheckoutMail(
35+
Mail::to($notifiableUser)->send(new BulkAssetCheckoutMail(
3236
$event->assets,
3337
$event->target,
3438
$event->admin,
@@ -94,4 +98,19 @@ private function requiresAcceptance(Collection $assets): bool
9498
);
9599
}
96100

101+
private function getNotifiableUser(CheckoutablesCheckedOutInBulk $event)
102+
{
103+
$target = $event->target;
104+
105+
if ($target instanceof Asset) {
106+
$target->load('assignedTo');
107+
return $target->assignedto;
108+
}
109+
110+
if ($target instanceof Location) {
111+
return $target->manager;
112+
}
113+
114+
return $target;
115+
}
97116
}

tests/Feature/Notifications/Email/BulkCheckoutEmailTest.php

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use App\Models\User;
1010
use Illuminate\Support\Facades\Mail;
1111
use PHPUnit\Framework\Attributes\Group;
12+
use RuntimeException;
1213
use Tests\TestCase;
1314

1415
#[Group('notifications')]
@@ -155,23 +156,40 @@ public function test_email_is_sent_to_cc_address_when_assets_do_not_require_acce
155156

156157
private function sendRequest()
157158
{
158-
$types = [
159-
User::class => 'user',
160-
Location::class => 'location',
161-
Asset::class => 'asset',
162-
];
163-
164159
$this->actingAs($this->admin)
165160
->followingRedirects()
166-
->post(route('hardware.bulkcheckout.store'), [
161+
->post(route('hardware.bulkcheckout.store'), array_merge([
167162
'selected_assets' => $this->assets->pluck('id')->toArray(),
168-
'checkout_to_type' => $types[get_class($this->target)],
169-
'assigned_user' => $this->target->id,
170-
'assigned_asset' => null,
171163
'checkout_at' => now()->subWeek()->format('Y-m-d'),
172164
'expected_checkin' => now()->addWeek()->format('Y-m-d'),
173165
'note' => null,
174-
])
166+
], $this->getAssignedArray()))
175167
->assertOk();
176168
}
169+
170+
private function getAssignedArray(): array
171+
{
172+
if ($this->target instanceof User) {
173+
return [
174+
'checkout_to_type' => 'user',
175+
'assigned_user' => $this->target->id,
176+
];
177+
}
178+
179+
if ($this->target instanceof Location) {
180+
return [
181+
'checkout_to_type' => 'location',
182+
'assigned_location' => $this->target->id,
183+
];
184+
}
185+
186+
if ($this->target instanceof Asset) {
187+
return [
188+
'checkout_to_type' => 'asset',
189+
'assigned_asset' => $this->target->id,
190+
];
191+
}
192+
193+
throw new RuntimeException('invalid target type');
194+
}
177195
}

0 commit comments

Comments
 (0)