diff --git a/app/Http/Controllers/Api/AccessoriesController.php b/app/Http/Controllers/Api/AccessoriesController.php
index 654f3c2e2..7dcee9320 100644
--- a/app/Http/Controllers/Api/AccessoriesController.php
+++ b/app/Http/Controllers/Api/AccessoriesController.php
@@ -2,6 +2,7 @@
namespace App\Http\Controllers\Api;
+use App\Events\CheckoutableCheckedOut;
use App\Helpers\Helper;
use App\Http\Controllers\Controller;
use App\Http\Transformers\AccessoriesTransformer;
@@ -278,7 +279,7 @@ class AccessoriesController extends Controller
public function checkout(Request $request, $accessoryId)
{
// Check if the accessory exists
- if (is_null($accessory = Accessory::find($accessoryId))) {
+ if (is_null($accessory = Accessory::withCount('users as users_count')->find($accessoryId))) {
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/accessories/message.does_not_exist')));
}
@@ -302,7 +303,7 @@ class AccessoriesController extends Controller
'note' => $request->get('note'),
]);
- $accessory->logCheckout($request->input('note'), $user);
+ event(new CheckoutableCheckedOut($accessory, $user, Auth::user(), $request->input('note')));
return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/accessories/message.checkout.success')));
}
diff --git a/app/Http/Controllers/Api/CompaniesController.php b/app/Http/Controllers/Api/CompaniesController.php
index 580bc5d9b..9c667973a 100644
--- a/app/Http/Controllers/Api/CompaniesController.php
+++ b/app/Http/Controllers/Api/CompaniesController.php
@@ -40,7 +40,9 @@ class CompaniesController extends Controller
'components_count',
];
- $companies = Company::withCount('assets as assets_count', 'licenses as licenses_count', 'accessories as accessories_count', 'consumables as consumables_count', 'components as components_count', 'users as users_count');
+ $companies = Company::withCount(['assets as assets_count' => function ($query) {
+ $query->AssetsForShow();
+ }])->withCount('licenses as licenses_count', 'accessories as accessories_count', 'consumables as consumables_count', 'components as components_count', 'users as users_count');
if ($request->filled('search')) {
$companies->TextSearch($request->input('search'));
diff --git a/app/Models/Setting.php b/app/Models/Setting.php
index caf142cbd..c7e67d24d 100755
--- a/app/Models/Setting.php
+++ b/app/Models/Setting.php
@@ -352,7 +352,6 @@ class Setting extends Model
'ldap_client_tls_cert',
'ldap_default_group',
'ldap_dept',
- 'ldap_emp_num',
'ldap_phone_field',
'ldap_jobtitle',
'ldap_manager',
diff --git a/database/factories/AccessoryFactory.php b/database/factories/AccessoryFactory.php
index 8ce34303b..406652677 100644
--- a/database/factories/AccessoryFactory.php
+++ b/database/factories/AccessoryFactory.php
@@ -33,7 +33,7 @@ class AccessoryFactory extends Factory
$this->faker->randomElement(['Keyboard', 'Wired'])
),
'user_id' => User::factory()->superuser(),
- 'category_id' => Category::factory(),
+ 'category_id' => Category::factory()->forAccessories(),
'model_number' => $this->faker->numberBetween(1000000, 50000000),
'location_id' => Location::factory(),
'qty' => 1,
@@ -114,4 +114,30 @@ class AccessoryFactory extends Factory
];
});
}
+
+ public function withoutItemsRemaining()
+ {
+ return $this->state(function () {
+ return [
+ 'qty' => 1,
+ ];
+ })->afterCreating(function ($accessory) {
+ $user = User::factory()->create();
+
+ $accessory->users()->attach($accessory->id, [
+ 'accessory_id' => $accessory->id,
+ 'created_at' => now(),
+ 'user_id' => $user->id,
+ 'assigned_to' => $user->id,
+ 'note' => '',
+ ]);
+ });
+ }
+
+ public function requiringAcceptance()
+ {
+ return $this->afterCreating(function ($accessory) {
+ $accessory->category->update(['require_acceptance' => 1]);
+ });
+ }
}
diff --git a/database/factories/CategoryFactory.php b/database/factories/CategoryFactory.php
index 94a9626da..fe6bc255b 100644
--- a/database/factories/CategoryFactory.php
+++ b/database/factories/CategoryFactory.php
@@ -172,4 +172,10 @@ class CategoryFactory extends Factory
]);
}
+ public function forAccessories()
+ {
+ return $this->state([
+ 'category_type' => 'accessory',
+ ]);
+ }
}
diff --git a/resources/views/companies/view.blade.php b/resources/views/companies/view.blade.php
index ffa5815b6..0b94446b2 100644
--- a/resources/views/companies/view.blade.php
+++ b/resources/views/companies/view.blade.php
@@ -21,7 +21,7 @@
{{ trans('general.assets') }}
- {!! (($company->assets) && ($company->assets()->AssetsForShow()->count() > 0 )) ? ''.number_format($company->assets()->AssetsForShow()->count()).'' : '' !!}
+ {!! ($company->assets()->AssetsForShow()->count() > 0 ) ? ''.number_format($company->assets()->AssetsForShow()->count()).'' : '' !!}
@@ -33,7 +33,7 @@
{{ trans('general.licenses') }}
- {!! (($company->licenses) && ($company->licenses->count() > 0 )) ? ''.number_format($company->licenses->count()).'' : '' !!}
+ {!! ($company->licenses->count() > 0 ) ? ''.number_format($company->licenses->count()).'' : '' !!}
@@ -43,7 +43,7 @@
{{ trans('general.accessories') }}
- {!! (($company->accessories) && ($company->accessories->count() > 0 )) ? ''.number_format($company->accessories->count()).'' : '' !!}
+ {!! ($company->accessories->count() > 0 ) ? ''.number_format($company->accessories->count()).'' : '' !!}
@@ -53,7 +53,7 @@
{{ trans('general.consumables') }}
- {!! (($company->consumables) && ($company->consumables->count() > 0 )) ? ''.number_format($company->consumables->count()).'' : '' !!}
+ {!! ($company->consumables->count() > 0 ) ? ''.number_format($company->consumables->count()).'' : '' !!}
diff --git a/tests/Feature/Api/Accessories/AccessoryCheckoutTest.php b/tests/Feature/Api/Accessories/AccessoryCheckoutTest.php
new file mode 100644
index 000000000..f4f49f0db
--- /dev/null
+++ b/tests/Feature/Api/Accessories/AccessoryCheckoutTest.php
@@ -0,0 +1,96 @@
+actingAsForApi(User::factory()->create())
+ ->postJson(route('api.accessories.checkout', Accessory::factory()->create()))
+ ->assertForbidden();
+ }
+
+ public function testValidation()
+ {
+ $this->actingAsForApi(User::factory()->checkoutAccessories()->create())
+ ->postJson(route('api.accessories.checkout', Accessory::factory()->create()), [
+ // missing assigned_to
+ ])
+ ->assertStatusMessageIs('error');
+ }
+
+ public function testAccessoryMustBeAvailableWhenCheckingOut()
+ {
+ $this->actingAsForApi(User::factory()->checkoutAccessories()->create())
+ ->postJson(route('api.accessories.checkout', Accessory::factory()->withoutItemsRemaining()->create()), [
+ 'assigned_to' => User::factory()->create()->id,
+ ])
+ ->assertStatusMessageIs('error');
+ }
+
+ public function testAccessoryCanBeCheckedOut()
+ {
+ $accessory = Accessory::factory()->create();
+ $user = User::factory()->create();
+
+ $this->actingAsForApi(User::factory()->checkoutAccessories()->create())
+ ->postJson(route('api.accessories.checkout', $accessory), [
+ 'assigned_to' => $user->id,
+ ]);
+
+ $this->assertTrue($accessory->users->contains($user));
+ }
+
+ public function testUserSentNotificationUponCheckout()
+ {
+ Notification::fake();
+
+ $accessory = Accessory::factory()->requiringAcceptance()->create();
+ $user = User::factory()->create();
+
+ $this->actingAsForApi(User::factory()->checkoutAccessories()->create())
+ ->postJson(route('api.accessories.checkout', $accessory), [
+ 'assigned_to' => $user->id,
+ ]);
+
+ Notification::assertSentTo($user, CheckoutAccessoryNotification::class);
+ }
+
+ public function testActionLogCreatedUponCheckout()
+ {
+ $accessory = Accessory::factory()->create();
+ $actor = User::factory()->checkoutAccessories()->create();
+ $user = User::factory()->create();
+
+ $this->actingAsForApi($actor)
+ ->postJson(route('api.accessories.checkout', $accessory), [
+ 'assigned_to' => $user->id,
+ 'note' => 'oh hi there',
+ ]);
+
+ $this->assertEquals(
+ 1,
+ Actionlog::where([
+ 'action_type' => 'checkout',
+ 'target_id' => $user->id,
+ 'target_type' => User::class,
+ 'item_id' => $accessory->id,
+ 'item_type' => Accessory::class,
+ 'user_id' => $actor->id,
+ 'note' => 'oh hi there',
+ ])->count(),
+ 'Log entry either does not exist or there are more than expected'
+ );
+ }
+}
diff --git a/tests/Feature/Checkouts/AccessoryCheckoutTest.php b/tests/Feature/Checkouts/AccessoryCheckoutTest.php
new file mode 100644
index 000000000..74269bfbb
--- /dev/null
+++ b/tests/Feature/Checkouts/AccessoryCheckoutTest.php
@@ -0,0 +1,96 @@
+actingAs(User::factory()->create())
+ ->post(route('accessories.checkout.store', Accessory::factory()->create()))
+ ->assertForbidden();
+ }
+
+ public function testValidation()
+ {
+ $this->actingAs(User::factory()->checkoutAccessories()->create())
+ ->post(route('accessories.checkout.store', Accessory::factory()->create()), [
+ // missing assigned_to
+ ])
+ ->assertSessionHas('error');
+ }
+
+ public function testAccessoryMustBeAvailableWhenCheckingOut()
+ {
+ $this->actingAs(User::factory()->checkoutAccessories()->create())
+ ->post(route('accessories.checkout.store', Accessory::factory()->withoutItemsRemaining()->create()), [
+ 'assigned_to' => User::factory()->create()->id,
+ ])
+ ->assertSessionHas('error');
+ }
+
+ public function testAccessoryCanBeCheckedOut()
+ {
+ $accessory = Accessory::factory()->create();
+ $user = User::factory()->create();
+
+ $this->actingAs(User::factory()->checkoutAccessories()->create())
+ ->post(route('accessories.checkout.store', $accessory), [
+ 'assigned_to' => $user->id,
+ ]);
+
+ $this->assertTrue($accessory->users->contains($user));
+ }
+
+ public function testUserSentNotificationUponCheckout()
+ {
+ Notification::fake();
+
+ $accessory = Accessory::factory()->requiringAcceptance()->create();
+ $user = User::factory()->create();
+
+ $this->actingAs(User::factory()->checkoutAccessories()->create())
+ ->post(route('accessories.checkout.store', $accessory), [
+ 'assigned_to' => $user->id,
+ ]);
+
+ Notification::assertSentTo($user, CheckoutAccessoryNotification::class);
+ }
+
+ public function testActionLogCreatedUponCheckout()
+ {
+ $accessory = Accessory::factory()->create();
+ $actor = User::factory()->checkoutAccessories()->create();
+ $user = User::factory()->create();
+
+ $this->actingAs($actor)
+ ->post(route('accessories.checkout.store', $accessory), [
+ 'assigned_to' => $user->id,
+ 'note' => 'oh hi there',
+ ]);
+
+ $this->assertEquals(
+ 1,
+ Actionlog::where([
+ 'action_type' => 'checkout',
+ 'target_id' => $user->id,
+ 'target_type' => User::class,
+ 'item_id' => $accessory->id,
+ 'item_type' => Accessory::class,
+ 'user_id' => $actor->id,
+ 'note' => 'oh hi there',
+ ])->count(),
+ 'Log entry either does not exist or there are more than expected'
+ );
+ }
+}