Skip to content
Closed
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->logAndSaveIfNeeded(ActionType::RequestCanceled);

try {
$settings->notify(new RequestAssetCancelation($data));
Expand Down
12 changes: 4 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 Down Expand Up @@ -32,14 +33,9 @@ public static function run(Asset $asset, User $user): string
$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());
//$logaction->location_id = $user->location_id ?? null; //??? FIXME - we were setting this logaction's location_id before and now we're not?!
$asset->logAndSaveIfNeeded(ActionType::Requested);

$asset->request();
$asset->increment('requests_counter', 1);
Expand Down
6 changes: 5 additions & 1 deletion 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 @@ -81,7 +82,10 @@ public function handle()
}

// Log the checkin
$seat->logCheckin($seat->user, 'Checked in via cli tool');
$seat->setLogTarget($seat->user);
$seat->setLogNote('Checked in via cli tool');
$seat->logAndSaveIfNeeded(ActionType::CheckinFrom); //this is going to be a dual-save, do we want that?!
// $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\Helpers\StorageHelper;
use App\Http\Controllers\Controller;
use App\Http\Requests\UploadFileRequest;
Expand Down Expand Up @@ -47,7 +48,9 @@ public function store(UploadFileRequest $request, $accessoryId = null) : Redirec

$file_name = $request->handleFile('private_uploads/accessories/', 'accessory-'.$accessory->id, $file);
//Log the upload to the log
$accessory->logUpload($file_name, e($request->input('notes')));
$accessory->setLogFilename($file_name);
$accessory->setLogNote(e($request->input('notes')));
$accessory->logAndSaveIfNeeded(ActionType::Uploaded);
}


Expand Down
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,10 +72,10 @@ public function store(AccessoryCheckoutRequest $request, Accessory $accessory) :
$this->authorize('checkout', $accessory);

$target = $this->determineCheckoutTarget();
$accessory->checkout_qty = $request->input('checkout_qty', 1);
for ($i = 0; $i < $accessory->checkout_qty; $i++) {

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

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

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

$accessory->setLogTarget($target);
$accessory->setLogNote($request->input('note'));
$accessory->setLogQuantity($checkout_qty);
$accessory->logAndSaveIfNeeded(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 @@ -292,6 +293,11 @@ public function checkout(AccessoryCheckoutRequest $request, Accessory $accessory
$accessory_checkout->save();
}

$accessory->setLogTarget($target);
$accessory->setLogNote($request->input('note'));
$accessory->setLogQuantity($checkout_qty);
$accessory->logAndSaveIfNeeded(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 @@ -319,7 +325,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->logAndSaveIfNeeded(ActionType::CheckinFrom);

// Was the accessory updated?
if ($accessory_checkout->delete()) {
Expand Down
7 changes: 5 additions & 2 deletions app/Http/Controllers/Api/AssetFilesController.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\StorageHelper;
use App\Http\Transformers\UploadedFilesTransformer;
use Illuminate\Support\Facades\Storage;
Expand Down Expand Up @@ -55,8 +56,10 @@ public function store(UploadFileRequest $request, $assetId = null) : JsonRespons
// Loop over the attached files and add them to the asset
foreach ($request->file('file') as $file) {
$file_name = $request->handleFile('private_uploads/assets/','hardware-'.$asset->id, $file);

$asset->logUpload($file_name, e($request->get('notes')));

$asset->setLogFilename($file_name);
$asset->setLogNote(e($request->input('notes')));
$asset->logAndSaveIfNeeded(ActionType::Uploaded);
}

// All done - report success
Expand Down
7 changes: 5 additions & 2 deletions app/Http/Controllers/Api/AssetModelFilesController.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\StorageHelper;
use Illuminate\Support\Facades\Storage;
use App\Helpers\Helper;
Expand Down Expand Up @@ -54,8 +55,10 @@ public function store(UploadFileRequest $request, $assetModelId = null) : JsonRe
// Loop over the attached files and add them to the asset
foreach ($request->file('file') as $file) {
$file_name = $request->handleFile('private_uploads/assetmodels/','model-'.$assetModel->id, $file);

$assetModel->logUpload($file_name, e($request->get('notes')));

$assetModel->setLogFilename($file_name);
$assetModel->setLogNote(e($request->input('notes')));
$assetModel->logAndSaveIfNeeded(ActionType::Uploaded);
}

// All done - report success
Expand Down
32 changes: 5 additions & 27 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 @@ -1141,33 +1142,10 @@ public function audit(Request $request, Asset $asset): JsonResponse
return response()->json(Helper::formatStandardApiResponse('error', null, $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->location_id = request('location_id'); //FIXME - is this changing the assets location? the action_log location? or what?
if ($asset->logAndSaveIfNeeded(ActionType::Audit)) {
return response()->json(Helper::formatStandardApiResponse('success', $payload, trans('admin/hardware/message.audit.success')));
}

Expand Down
15 changes: 13 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,11 @@ public function checkout(Request $request, $componentId) : JsonResponse
'note' => $request->get('note'),
]);

$component->logCheckout($request->input('note'), $asset);
//FIXME - something here with location_id - maybe something for setLogLocation or something like that?
$component->setLogLocationOverride($asset->location);
$component->setLogNote($request->input('note'));
$component->setLogQuantity($request->get('assigned_qty'));
$component->logAndSaveIfNeeded(ActionType::Checkout);

return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/components/message.checkout.success')));
}
Expand Down Expand Up @@ -350,6 +355,12 @@ 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);
//FIXME - problem with 'lcoation_id' apaparently?!
$component->setLogNote($request->input('note'));
$component->logAndSaveIfNeeded(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 @@ -258,7 +259,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 @@ -271,8 +272,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 @@ -284,9 +285,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 @@ -296,7 +297,9 @@ public function checkout(Request $request, $id) : JsonResponse
]
);
}

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

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

Expand Down
Loading
Loading