Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions app/Actions/CheckoutRequests/CancelCheckoutRequestAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Actions\CheckoutRequests;

use App\Enums\ActionType;
use App\Models\Actionlog;
use App\Models\Asset;
use App\Models\Company;
Expand All @@ -27,14 +28,9 @@ public static function run(Asset $asset, User $user)
$data['item_quantity'] = 1;
$settings = Setting::getSettings();

$logaction = new Actionlog();
$logaction->item_id = $data['asset_id'] = $asset->id;
$logaction->item_type = $data['item_type'] = Asset::class;
$logaction->created_at = $data['requested_date'] = date('Y-m-d H:i:s');
$logaction->target_id = $data['user_id'] = auth()->id();
$logaction->target_type = User::class;
$logaction->location_id = $user->location_id ?? null;
$logaction->logaction('request canceled');
$asset->setLogTarget(auth()->user());
$asset->setLogLocationOverride($user->location_id);
$asset->saveWithActionType(ActionType::RequestCanceled);

try {
$settings->notify(new RequestAssetCancelation($data));
Expand Down
13 changes: 5 additions & 8 deletions app/Actions/CheckoutRequests/CreateCheckoutRequestAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Actions\CheckoutRequests;

use App\Enums\ActionType;
use App\Exceptions\AssetNotRequestable;
use App\Models\Actionlog;
use App\Models\Asset;
Expand All @@ -28,18 +29,14 @@ public static function run(Asset $asset, User $user): string
}

$data['item'] = $asset;
$data['item_type'] = Asset::class; // TODO - generalize?
$data['target'] = $user;
$data['item_quantity'] = 1;
$settings = Setting::getSettings();

$logaction = new Actionlog();
$logaction->item_id = $data['asset_id'] = $asset->id;
$logaction->item_type = $data['item_type'] = Asset::class;
$logaction->created_at = $data['requested_date'] = date('Y-m-d H:i:s');
$logaction->target_id = $data['user_id'] = auth()->id();
$logaction->target_type = User::class;
$logaction->location_id = $user->location_id ?? null;
$logaction->logaction('requested');
$asset->setLogTarget(auth()->user());
$asset->setLogLocationOverride($user->location_id);
$asset->saveWithActionType(ActionType::Requested);

$asset->request();
$asset->increment('requests_counter', 1);
Expand Down
11 changes: 8 additions & 3 deletions app/Console/Commands/CheckinLicensesFromAllUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Console\Commands;

use App\Enums\ActionType;
use App\Models\License;
use App\Models\LicenseSeat;
use App\Models\User;
Expand Down Expand Up @@ -73,15 +74,19 @@ public function handle()
$this->info($seat->user->username.' has a license seat for '.$license->name);
$seat->assigned_to = null;

if ($seat->save()) {
// Log the checkin
$seat->setLogTarget($seat->user);
$seat->setLogNote('Checked in via cli tool');
if ($seat->saveWithActionType(ActionType::CheckinFrom)) {

// Override the email address so we don't notify on checkin
// TODO - I don't see any *actual* notification firing - should we?
// or should we delete the following 3 lines?
if (! $notify) {
$seat->user->email = null;
}

// Log the checkin
$seat->logCheckin($seat->user, 'Checked in via cli tool');
// $seat->logCheckin($seat->user, 'Checked in via cli tool');
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions app/Enums/ActionType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Enums;

enum ActionType: string {
case Restore = 'restore';
case TwoFactorReset = '2FA reset';
case CheckinFrom = 'checkin from';
case RequestCanceled = 'request canceled';
case Requested = 'requested';
case DeleteSeats = 'delete seats';
case AddSeats = 'add seats';
case Update = 'update';
case Create = 'create';
case Delete = 'delete';
case Uploaded = 'uploaded';
case NoteAdded = 'note added';
case Audit = 'audit';
case Checkout = 'checkout';
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers\Accessories;

use App\Enums\ActionType;
use App\Events\CheckoutableCheckedOut;
use App\Helpers\Helper;
use App\Http\Controllers\CheckInOutRequest;
Expand Down Expand Up @@ -71,11 +72,11 @@ public function store(AccessoryCheckoutRequest $request, Accessory $accessory) :
$this->authorize('checkout', $accessory);

$target = $this->determineCheckoutTarget();

$checkout_qty = $request->input('checkout_qty', 1);

session()->put(['checkout_to_type' => $target]);

$accessory->checkout_qty = $request->input('checkout_qty', 1);

for ($i = 0; $i < $accessory->checkout_qty; $i++) {
for ($i = 0; $i < $checkout_qty; $i++) {

$accessory_checkout = new AccessoryCheckout([
'accessory_id' => $accessory->id,
Expand All @@ -89,6 +90,10 @@ public function store(AccessoryCheckoutRequest $request, Accessory $accessory) :
$accessory_checkout->save();
}

$accessory->setLogTarget($target);
$accessory->setLogNote($request->input('note'));
$accessory->setLogQuantity($checkout_qty);
$accessory->saveWithActionType(ActionType::Checkout);
event(new CheckoutableCheckedOut($accessory, $target, auth()->user(), $request->input('note')));

$request->request->add(['checkout_to_type' => request('checkout_to_type')]);
Expand Down
14 changes: 11 additions & 3 deletions app/Http/Controllers/Api/AccessoriesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers\Api;

use App\Enums\ActionType;
use App\Events\CheckoutableCheckedOut;
use App\Helpers\Helper;
use App\Http\Controllers\CheckInOutRequest;
Expand Down Expand Up @@ -276,9 +277,9 @@ public function checkout(AccessoryCheckoutRequest $request, Accessory $accessory
{
$this->authorize('checkout', $accessory);
$target = $this->determineCheckoutTarget();
$accessory->checkout_qty = $request->input('checkout_qty', 1);
$checkout_qty = $request->input('checkout_qty', 1);

for ($i = 0; $i < $accessory->checkout_qty; $i++) {
for ($i = 0; $i < $checkout_qty; $i++) {

$accessory_checkout = new AccessoryCheckout([
'accessory_id' => $accessory->id,
Expand All @@ -302,6 +303,11 @@ public function checkout(AccessoryCheckoutRequest $request, Accessory $accessory
];
}

$accessory->setLogTarget($target);
$accessory->setLogNote($request->input('note'));
$accessory->setLogQuantity($checkout_qty);
$accessory->saveWithActionType(ActionType::Checkout);

// Set this value to be able to pass the qty through to the event
event(new CheckoutableCheckedOut($accessory, $target, auth()->user(), $request->input('note')));

Expand Down Expand Up @@ -329,7 +335,9 @@ public function checkin(Request $request, $accessoryUserId = null)
$accessory = Accessory::find($accessory_checkout->accessory_id);
$this->authorize('checkin', $accessory);

$accessory->logCheckin(User::find($accessory_checkout->assigned_to), $request->input('note'));
$accessory->setLogTarget(User::find($accessory_checkout->assigned_to));
$accessory->setLogNote($request->input('note'));
$accessory->saveWithActionType(ActionType::CheckinFrom);

// Was the accessory updated?
if ($accessory_checkout->delete()) {
Expand Down
35 changes: 7 additions & 28 deletions app/Http/Controllers/Api/AssetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers\Api;

use App\Enums\ActionType;
use App\Events\CheckoutableCheckedIn;
use App\Http\Requests\StoreAssetRequest;
use App\Http\Requests\UpdateAssetRequest;
Expand Down Expand Up @@ -991,6 +992,7 @@ public function checkin(Request $request, $asset_id): JsonResponse
$asset->last_checkin = now();
$asset->assignedTo()->disassociate($asset);
$asset->accepted = null;
$asset->increment('checkin_counter'); // TODO: if we go transactional, this should be in it

if ($request->has('name')) {
$asset->name = $request->input('name');
Expand Down Expand Up @@ -1037,7 +1039,7 @@ function (Builder $query) use ($asset) {
$acceptance->delete();
});

if ($asset->save()) {
if ($asset->saveWithActionType(ActionType::CheckinFrom)) {
event(new CheckoutableCheckedIn($asset, $target, auth()->user(), $request->input('note'), $checkin_at, $originalValues));

return response()->json(Helper::formatStandardApiResponse('success', [
Expand Down Expand Up @@ -1159,33 +1161,10 @@ public function audit(Request $request, Asset $asset): JsonResponse
return response()->json(Helper::formatStandardApiResponse('error', ['asset_tag' => $asset->asset_tag], $asset->getErrors()));
}


/**
* Even though we do a save() further down, we don't want to log this as a "normal" asset update,
* which would trigger the Asset Observer and would log an asset *update* log entry (because the
* de-normed fields like next_audit_date on the asset itself will change on save()) *in addition* to
* the audit log entry we're creating through this controller.
*
* To prevent this double-logging (one for update and one for audit), we skip the observer and bypass
* that de-normed update log entry by using unsetEventDispatcher(), BUT invoking unsetEventDispatcher()
* will bypass normal model-level validation that's usually handled at the observer)
*
* We handle validation on the save() by checking if the asset is valid via the ->isValid() method,
* which manually invokes Watson Validating to make sure the asset's model is valid.
*
* @see \App\Observers\AssetObserver::updating()
* @see \App\Models\Asset::save()
*/

$asset->unsetEventDispatcher();


/**
* Invoke Watson Validating to check the asset itself and check to make sure it saved correctly.
* We have to invoke this manually because of the unsetEventDispatcher() above.)
*/
if ($asset->isValid() && $asset->save()) {
$asset->logAudit(request('note'), request('location_id'), null, $originalValues);
// this 'new' audit system logs the changes via log_meta
$asset->setLogNote(request('note'));
$asset->setLogLocationOverride(request('location_id'));
if ($asset->saveWithActionType(ActionType::Audit)) {
return response()->json(Helper::formatStandardApiResponse('success', $payload, trans('admin/hardware/message.audit.success')));
}

Expand Down
13 changes: 11 additions & 2 deletions app/Http/Controllers/Api/ComponentsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers\Api;

use App\Enums\ActionType;
use App\Helpers\Helper;
use App\Http\Controllers\Controller;
use App\Http\Transformers\ComponentsTransformer;
Expand Down Expand Up @@ -285,7 +286,7 @@ public function checkout(Request $request, $componentId) : JsonResponse
if ($component->numRemaining() >= $request->get('assigned_qty')) {

$asset = Asset::find($request->input('assigned_to'));
$component->assigned_to = $request->input('assigned_to');
$component->setLogTarget($asset);

$component->assets()->attach($component->id, [
'component_id' => $component->id,
Expand All @@ -296,7 +297,10 @@ public function checkout(Request $request, $componentId) : JsonResponse
'note' => $request->get('note'),
]);

$component->logCheckout($request->input('note'), $asset);
$component->setLogLocationOverride($asset->location);
$component->setLogNote($request->input('note'));
$component->setLogQuantity($request->get('assigned_qty'));
$component->saveWithActionType(ActionType::Checkout);

return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/components/message.checkout.success')));
}
Expand Down Expand Up @@ -350,6 +354,11 @@ public function checkin(Request $request, $component_asset_id) : JsonResponse

$asset = Asset::find($component_assets->asset_id);

// the 'event()' below no longer does the logging; that needs to be done here.
$component->setLogTarget($asset);
$component->setLogNote($request->input('note'));
$component->saveWithActionType(ActionType::CheckinFrom);

event(new CheckoutableCheckedIn($component, $asset, auth()->user(), $request->input('note'), Carbon::now()));

return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/components/message.checkin.success')));
Expand Down
15 changes: 9 additions & 6 deletions app/Http/Controllers/Api/ConsumablesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers\Api;

use App\Enums\ActionType;
use App\Events\CheckoutableCheckedOut;
use App\Helpers\Helper;
use App\Http\Controllers\Controller;
Expand Down Expand Up @@ -263,7 +264,7 @@ public function checkout(Request $request, $id) : JsonResponse

$this->authorize('checkout', $consumable);

$consumable->checkout_qty = $request->input('checkout_qty', 1);
$checkout_qty = $request->input('checkout_qty', 1);

// Make sure there is at least one available to checkout
if ($consumable->numRemaining() <= 0) {
Expand All @@ -276,8 +277,8 @@ public function checkout(Request $request, $id) : JsonResponse
}

// Make sure there is at least one available to checkout
if ($consumable->numRemaining() <= 0 || $consumable->checkout_qty > $consumable->numRemaining()) {
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/consumables/message.checkout.unavailable', ['requested' => $consumable->checkout_qty, 'remaining' => $consumable->numRemaining() ])));
if ($consumable->numRemaining() <= 0 || $checkout_qty > $consumable->numRemaining()) {
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/consumables/message.checkout.unavailable', ['requested' => $checkout_qty, 'remaining' => $consumable->numRemaining()])));
}


Expand All @@ -289,9 +290,9 @@ public function checkout(Request $request, $id) : JsonResponse
}

// Update the consumable data
$consumable->assigned_to = $request->input('assigned_to');
$consumable->setLogTarget($user);

for ($i = 0; $i < $consumable->checkout_qty; $i++) {
for ($i = 0; $i < $checkout_qty; $i++) {
$consumable->users()->attach($consumable->id,
[
'consumable_id' => $consumable->id,
Expand All @@ -301,7 +302,9 @@ public function checkout(Request $request, $id) : JsonResponse
]
);
}

$consumable->setLogQuantity($checkout_qty);
$consumable->setLogNote($request->input('note'));
$consumable->saveWithActionType(ActionType::Checkout);

event(new CheckoutableCheckedOut($consumable, $user, auth()->user(), $request->input('note')));

Expand Down
26 changes: 12 additions & 14 deletions app/Http/Controllers/Api/LicenseSeatsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers\Api;

use App\Enums\ActionType;
use App\Helpers\Helper;
use App\Http\Controllers\Controller;
use App\Http\Transformers\LicenseSeatsTransformer;
Expand Down Expand Up @@ -128,7 +129,7 @@ public function update(Request $request, $licenseId, $seatId) : JsonResponse | a
// nothing to update
return response()->json(Helper::formatStandardApiResponse('success', $licenseSeat, trans('admin/licenses/message.update.success')));
}
if( $touched && $licenseSeat->unreassignable_seat) {
if ($touched && $licenseSeat->unreassignable_seat) {
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/licenses/message.checkout.unavailable')));
}
// the logging functions expect only one "target". if both asset and user are present in the request,
Expand All @@ -144,20 +145,17 @@ public function update(Request $request, $licenseId, $seatId) : JsonResponse | a
return response()->json(Helper::formatStandardApiResponse('error', null, 'Target not found'));
}

if ($licenseSeat->save()) {

if ($is_checkin) {
if(!$licenseSeat->license->reassignable){
$licenseSeat->unreassignable_seat = true;
$licenseSeat->save();
}
$licenseSeat->logCheckin($target, $licenseSeat->notes);

return response()->json(Helper::formatStandardApiResponse('success', $licenseSeat, trans('admin/licenses/message.update.success')));
$license->setLogTarget($target);
$license->setLogNote($request->input('notes'));
if ($is_checkin) {
if (!$licenseSeat->license->reassignable) {
$licenseSeat->unreassignable_seat = true;
}

// in this case, relevant fields are touched but it's not a checkin operation. so it must be a checkout operation.
$licenseSeat->logCheckout($request->input('notes'), $target);
$license->setLogAction(ActionType::CheckinFrom);
} else {
$license->setLogAction(ActionType::Checkout);
}
if ($licenseSeat->save() && $license->save()) {

return response()->json(Helper::formatStandardApiResponse('success', $licenseSeat, trans('admin/licenses/message.update.success')));
}
Expand Down
Loading
Loading