Ensure notification is sent when consumable is checked out via api
This commit is contained in:
parent
13c37e708f
commit
2e0e39ccc8
3 changed files with 73 additions and 17 deletions
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace App\Http\Controllers\Api;
|
namespace App\Http\Controllers\Api;
|
||||||
|
|
||||||
|
use App\Events\CheckoutableCheckedOut;
|
||||||
use App\Helpers\Helper;
|
use App\Helpers\Helper;
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use App\Http\Transformers\ConsumablesTransformer;
|
use App\Http\Transformers\ConsumablesTransformer;
|
||||||
|
@ -11,6 +12,7 @@ use App\Models\Consumable;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use App\Http\Requests\ImageUploadRequest;
|
use App\Http\Requests\ImageUploadRequest;
|
||||||
|
use Illuminate\Support\Facades\Auth;
|
||||||
|
|
||||||
class ConsumablesController extends Controller
|
class ConsumablesController extends Controller
|
||||||
{
|
{
|
||||||
|
@ -290,15 +292,7 @@ class ConsumablesController extends Controller
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
// Log checkout event
|
event(new CheckoutableCheckedOut($consumable, $user, Auth::user(), $request->input('note')));
|
||||||
$logaction = $consumable->logCheckout($request->input('note'), $user);
|
|
||||||
$data['log_id'] = $logaction->id;
|
|
||||||
$data['eula'] = $consumable->getEula();
|
|
||||||
$data['first_name'] = $user->first_name;
|
|
||||||
$data['item_name'] = $consumable->name;
|
|
||||||
$data['checkout_date'] = $logaction->created_at;
|
|
||||||
$data['note'] = $logaction->note;
|
|
||||||
$data['require_acceptance'] = $consumable->requireAcceptance();
|
|
||||||
|
|
||||||
return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/consumables/message.checkout.success')));
|
return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/consumables/message.checkout.success')));
|
||||||
|
|
||||||
|
|
|
@ -109,4 +109,11 @@ class ConsumableFactory extends Factory
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function requiringAcceptance()
|
||||||
|
{
|
||||||
|
return $this->afterCreating(function (Consumable $consumable) {
|
||||||
|
$consumable->category->update(['require_acceptance' => 1]);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,11 @@
|
||||||
|
|
||||||
namespace Tests\Feature\Api\Consumables;
|
namespace Tests\Feature\Api\Consumables;
|
||||||
|
|
||||||
|
use App\Models\Actionlog;
|
||||||
|
use App\Models\Consumable;
|
||||||
|
use App\Models\User;
|
||||||
|
use App\Notifications\CheckoutConsumableNotification;
|
||||||
|
use Illuminate\Support\Facades\Notification;
|
||||||
use Tests\Support\InteractsWithSettings;
|
use Tests\Support\InteractsWithSettings;
|
||||||
use Tests\TestCase;
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
@ -11,31 +16,81 @@ class ConsumableCheckoutTest extends TestCase
|
||||||
|
|
||||||
public function testCheckingOutConsumableRequiresCorrectPermission()
|
public function testCheckingOutConsumableRequiresCorrectPermission()
|
||||||
{
|
{
|
||||||
$this->markTestIncomplete();
|
$this->actingAsForApi(User::factory()->create())
|
||||||
|
->postJson(route('api.consumables.checkout', Consumable::factory()->create()))
|
||||||
|
->assertForbidden();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testValidationWhenCheckingOutConsumable()
|
public function testValidationWhenCheckingOutConsumable()
|
||||||
{
|
{
|
||||||
$this->markTestIncomplete();
|
$this->actingAsForApi(User::factory()->checkoutConsumables()->create())
|
||||||
|
->postJson(route('api.consumables.checkout', Consumable::factory()->create()), [
|
||||||
|
// missing assigned_to
|
||||||
|
])
|
||||||
|
->assertStatusMessageIs('error');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testConsumableMustBeAvailableWhenCheckingOut()
|
public function testConsumableMustBeAvailableWhenCheckingOut()
|
||||||
{
|
{
|
||||||
$this->markTestIncomplete();
|
$this->actingAsForApi(User::factory()->checkoutConsumables()->create())
|
||||||
|
->postJson(route('api.consumables.checkout', Consumable::factory()->withoutItemsRemaining()->create()), [
|
||||||
|
'assigned_to' => User::factory()->create()->id,
|
||||||
|
])
|
||||||
|
->assertStatusMessageIs('error');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testConsumableCanBeCheckedOut()
|
public function testConsumableCanBeCheckedOut()
|
||||||
{
|
{
|
||||||
$this->markTestIncomplete();
|
$consumable = Consumable::factory()->create();
|
||||||
|
$user = User::factory()->create();
|
||||||
|
|
||||||
|
$this->actingAsForApi(User::factory()->checkoutConsumables()->create())
|
||||||
|
->postJson(route('api.consumables.checkout', $consumable), [
|
||||||
|
'assigned_to' => $user->id,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertTrue($user->consumables->contains($consumable));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testUserSentNotificationUponCheckout()
|
public function testUserSentNotificationUponCheckout()
|
||||||
{
|
{
|
||||||
$this->markTestIncomplete();
|
Notification::fake();
|
||||||
|
|
||||||
|
$consumable = Consumable::factory()->requiringAcceptance()->create();
|
||||||
|
|
||||||
|
$user = User::factory()->create();
|
||||||
|
|
||||||
|
$this->actingAsForApi(User::factory()->checkoutConsumables()->create())
|
||||||
|
->postJson(route('api.consumables.checkout', $consumable), [
|
||||||
|
'assigned_to' => $user->id,
|
||||||
|
]);
|
||||||
|
|
||||||
|
Notification::assertSentTo($user, CheckoutConsumableNotification::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testActionLogCreatedUponCheckout()
|
public function testActionLogCreatedUponCheckout()
|
||||||
{
|
{$consumable = Consumable::factory()->create();
|
||||||
$this->markTestIncomplete();
|
$actor = User::factory()->checkoutConsumables()->create();
|
||||||
|
$user = User::factory()->create();
|
||||||
|
|
||||||
|
$this->actingAsForApi($actor)
|
||||||
|
->postJson(route('api.consumables.checkout', $consumable), [
|
||||||
|
'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' => $consumable->id,
|
||||||
|
'item_type' => Consumable::class,
|
||||||
|
'user_id' => $actor->id,
|
||||||
|
'note' => 'oh hi there',
|
||||||
|
])->count(),
|
||||||
|
'Log entry either does not exist or there are more than expected'
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue