Skip to content

A free open source IT asset/license management system

WIP: introduce event

Marcus Moore7017a0cae178
diff --git a/app/Events/CheckoutablesCheckedOutInBulk.php b/app/Events/CheckoutablesCheckedOutInBulk.php
new file mode 100644
index 0000000000..6dd8ccde7a
--- /dev/null
+++ b/app/Events/CheckoutablesCheckedOutInBulk.php
@@ -0,0 +1,23 @@
+<?php
+
+namespace App\Events;
+
+use App\Models\User;
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Foundation\Events\Dispatchable;
+use Illuminate\Queue\SerializesModels;
+use Illuminate\Support\Collection;
+
+class CheckoutablesCheckedOutInBulk
+{
+ use Dispatchable, SerializesModels;
+
+ public function __construct(
+ public Collection $assets,
+ public Model $target,
+ public User $admin,
+ public string $checkout_at,
+ public string $expected_checkin,
+ ) {
+ }
+}
diff --git a/app/Http/Controllers/Assets/BulkAssetsController.php b/app/Http/Controllers/Assets/BulkAssetsController.php
index 30047438e1..f6f9dca44c 100644
--- a/app/Http/Controllers/Assets/BulkAssetsController.php
+++ b/app/Http/Controllers/Assets/BulkAssetsController.php
@@ -2,6 +2,7 @@
namespace App\Http\Controllers\Assets;
+use App\Events\CheckoutablesCheckedOutInBulk;
use App\Helpers\Helper;
use App\Http\Controllers\CheckInOutRequest;
use App\Http\Controllers\Controller;
@@ -720,6 +721,15 @@ class BulkAssetsController extends Controller
});
if (! $errors) {
+ CheckoutablesCheckedOutInBulk::dispatch(
+ $assets,
+ $target,
+ $admin,
+ $checkout_at,
+ $expected_checkin,
+ e($request->get('note')),
+ );
+
// Redirect to the new asset page
return redirect()->to('hardware')->with('success', trans_choice('admin/hardware/message.multi-checkout.success', $asset_ids));
}
diff --git a/app/Mail/BulkAssetCheckoutMail.php b/app/Mail/BulkAssetCheckoutMail.php
new file mode 100644
index 0000000000..11ccebfa56
--- /dev/null
+++ b/app/Mail/BulkAssetCheckoutMail.php
@@ -0,0 +1,52 @@
+<?php
+
+namespace App\Mail;
+
+use Illuminate\Bus\Queueable;
+use Illuminate\Mail\Mailable;
+use Illuminate\Mail\Mailables\Content;
+use Illuminate\Mail\Mailables\Envelope;
+use Illuminate\Queue\SerializesModels;
+
+class BulkAssetCheckoutMail extends Mailable
+{
+ use Queueable, SerializesModels;
+
+ /**
+ * Create a new message instance.
+ */
+ public function __construct()
+ {
+ //
+ }
+
+ /**
+ * Get the message envelope.
+ */
+ public function envelope(): Envelope
+ {
+ return new Envelope(
+ subject: 'Bulk Asset Checkout Mail',
+ );
+ }
+
+ /**
+ * Get the message content definition.
+ */
+ public function content(): Content
+ {
+ return new Content(
+ markdown: 'mail.bulk-asset-checkout-mail',
+ );
+ }
+
+ /**
+ * Get the attachments for the message.
+ *
+ * @return array<int, \Illuminate\Mail\Mailables\Attachment>
+ */
+ public function attachments(): array
+ {
+ return [];
+ }
+}
diff --git a/resources/views/mail/bulk-asset-checkout-mail.blade.php b/resources/views/mail/bulk-asset-checkout-mail.blade.php
new file mode 100644
index 0000000000..de9a155b34
--- /dev/null
+++ b/resources/views/mail/bulk-asset-checkout-mail.blade.php
@@ -0,0 +1,12 @@
+<x-mail::message>
+# Introduction
+
+The body of your message.
+
+<x-mail::button :url="''">
+Button Text
+</x-mail::button>
+
+Thanks,<br>
+{{ config('app.name') }}
+</x-mail::message>
diff --git a/tests/Feature/Checkouts/Ui/BulkAssetCheckoutTest.php b/tests/Feature/Checkouts/Ui/BulkAssetCheckoutTest.php
index 51d945cb2f..797fa0651c 100644
--- a/tests/Feature/Checkouts/Ui/BulkAssetCheckoutTest.php
+++ b/tests/Feature/Checkouts/Ui/BulkAssetCheckoutTest.php
@@ -2,13 +2,17 @@
namespace Tests\Feature\Checkouts\Ui;
+use App\Events\CheckoutablesCheckedOutInBulk;
+use App\Mail\BulkAssetCheckoutMail;
use App\Mail\CheckoutAssetMail;
use App\Models\Asset;
use App\Models\Company;
use App\Models\Location;
use App\Models\User;
+use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Mail;
use PHPUnit\Framework\Attributes\DataProvider;
+use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\ExpectationFailedException;
use Tests\TestCase;
@@ -188,8 +192,10 @@ class BulkAssetCheckoutTest extends TestCase
$response->assertRedirectToRoute('hardware.bulkcheckout.show');
}
+ #[Group('notifications')]
public function test_one_email_is_sent_instead_of_multiple_individual_ones()
{
+ Event::fake();
Mail::fake();
$assets = Asset::factory()->requiresAcceptance()->count(2)->create();
@@ -202,16 +208,29 @@ class BulkAssetCheckoutTest extends TestCase
'checkout_to_type' => 'user',
'assigned_user' => $user->id,
'assigned_asset' => null,
+ 'checkout_at' => now()->subWeek()->format('Y-m-d'),
+ 'expected_checkin' => now()->addWeek()->format('Y-m-d'),
'note' => null,
])
->assertOk();
- $assets->fresh()->each(function ($asset) {
- $this->assertHasTheseActionLogs($asset, ['create', 'checkout']);
- });
+ // @todo:
+ // $assets->fresh()->each(function ($asset) {
+ // $this->assertHasTheseActionLogs($asset, ['create', 'checkout']);
+ // });
Mail::assertSent(CheckoutAssetMail::class, 0);
- $this->markTestIncomplete('assert one email sent for both assets');
+ Event::assertDispatchedTimes(CheckoutablesCheckedOutInBulk::class, 1);
+ Event::assertDispatched(CheckoutablesCheckedOutInBulk::class, function (CheckoutablesCheckedOutInBulk $event) {
+ // @todo:
+ dd($event);
+ });
+
+ // @todo: move to Notifications test directory?
+ // Mail::assertSent(BulkAssetCheckoutMail::class, function (BulkAssetCheckoutMail $mail) {
+ // // @todo: assert contents
+ // return $mail->hasTo('[email protected]');
+ // });
}
}