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
14 changes: 5 additions & 9 deletions app/Actions/CheckoutRequests/CancelCheckoutRequestAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace App\Actions\CheckoutRequests;

use App\Enums\ActionType;
use App\Models\Actionlog;
use App\Models\Asset;
use App\Models\Company;
use App\Models\Setting;
use App\Models\User;
use App\Notifications\RequestAssetCancelation;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Support\Facades\Auth;

class CancelCheckoutRequestAction
{
Expand All @@ -18,7 +20,7 @@ public static function run(Asset $asset, User $user)
throw new AuthorizationException();
}

$asset->cancelRequest();
$asset->cancelRequest(); //TODO - should the below logic just be here?

$asset->decrement('requests_counter', 1);

Expand All @@ -27,14 +29,8 @@ 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());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we please use auth()->user here instead?

$asset->logAndSaveIfNeeded(ActionType::RequestCanceled);

try {
$settings->notify(new RequestAssetCancelation($data));
Expand Down
16 changes: 6 additions & 10 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 @@ -10,6 +11,7 @@
use App\Models\User;
use App\Notifications\RequestAssetNotification;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Support\Facades\Auth;
use Log;

class CreateCheckoutRequestAction
Expand All @@ -32,17 +34,11 @@ 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());
$asset->logAndSaveIfNeeded(ActionType::Requested);

$asset->request();
$asset->increment('requests_counter', 1);
$asset->request(); // TODO: could argue that the above stuff belongs here? This
$asset->increment('requests_counter', 1); // TODO - would rather hit the DB once, but we don't yet have transaction safety
try {
$settings->notify(new RequestAssetNotification($data));
} catch (\Exception $e) {
Expand Down
8 changes: 3 additions & 5 deletions app/Console/Commands/CheckinLicensesFromAllUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,13 @@ public function handle()
$this->info($seat->user->username.' has a license seat for '.$license->name);
$seat->assigned_to = null;

if ($seat->save()) {
$seat->setLogNote('Checked in via cli tool');
if ($seat->checkInAndSave()) {

// Override the email address so we don't notify on checkin
if (! $notify) {
$seat->user->email = null;
$seat->user->email = null; //FIXME - this is probably not implemented just yet? maybe hoist this above the checkin?
}

// Log the checkin
$seat->logCheckin($seat->user, 'Checked in via cli tool');
}
}
}
Expand Down
68 changes: 1 addition & 67 deletions app/Console/Commands/MergeUsersByUsername.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Console\Commands;

use App\Events\UserMerged;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Console\Command;
Expand Down Expand Up @@ -59,72 +58,7 @@ public function handle()

foreach ($bad_users as $bad_user) {
$this->info($bad_user->username.' ('.$bad_user->id.') will be merged into '.$user->username.' ('.$user->id.') ');

// Walk the list of assets
foreach ($bad_user->assets as $asset) {
$this->info('Updating asset '.$asset->asset_tag.' '.$asset->id.' to user '.$user->id);
$asset->assigned_to = $user->id;
if (! $asset->save()) {
$this->error('Could not update assigned_to field on asset '.$asset->asset_tag.' '.$asset->id.' to user '.$user->id);
$this->error('Error saving: '.$asset->getErrors());
}
}

// Walk the list of licenses
foreach ($bad_user->licenses as $license) {
$this->info('Updating license '.$license->name.' '.$license->id.' to user '.$user->id);
$bad_user->licenses()->updateExistingPivot($license->id, ['assigned_to' => $user->id]);
}

// Walk the list of consumables
foreach ($bad_user->consumables as $consumable) {
$this->info('Updating consumable '.$consumable->id.' to user '.$user->id);
$bad_user->consumables()->updateExistingPivot($consumable->id, ['assigned_to' => $user->id]);
}

// Walk the list of accessories
foreach ($bad_user->accessories as $accessory) {
$this->info('Updating accessory '.$accessory->id.' to user '.$user->id);
$bad_user->accessories()->updateExistingPivot($accessory->id, ['assigned_to' => $user->id]);
}

// Walk the list of logs
foreach ($bad_user->userlog as $log) {
$this->info('Updating action log record '.$log->id.' to user '.$user->id);
$log->target_id = $user->id;
$log->save();
}

// Update any manager IDs
$this->info('Updating managed user records to user '.$user->id);
User::where('manager_id', '=', $bad_user->id)->update(['manager_id' => $user->id]);

// Update location manager IDs
foreach ($bad_user->managedLocations as $managedLocation) {
$this->info('Updating managed location record '.$managedLocation->name.' to manager '.$user->id);
$managedLocation->manager_id = $user->id;
$managedLocation->save();
}

foreach ($bad_user->uploads as $upload) {
$this->info('Updating upload log record '.$upload->id.' to user '.$user->id);
$upload->item_id = $user->id;
$upload->save();
}

foreach ($bad_user->acceptances as $acceptance) {
$this->info('Updating acceptance log record '.$acceptance->id.' to user '.$user->id);
$acceptance->item_id = $user->id;
$acceptance->save();
}

// Mark the user as deleted
$this->info('Marking the user as deleted');
$bad_user->deleted_at = Carbon::now()->timestamp;
$bad_user->save();

event(new UserMerged($bad_user, $user, null));

$user->merge($bad_user); //THAT's IT!

}
}
Expand Down
23 changes: 23 additions & 0 deletions app/Enums/ActionType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Enums;

enum ActionType: string {
case Restore = 'restore';
case TwoFactorReset = '2FA reset';
case CheckinFrom = 'checkin from';
case Checkout = 'checkout';
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 NoteAdded = 'note added';
case Audit = 'audit';
case Merged = 'merged';
case Accepted = 'accepted';
case Declined = 'declined';
case Uploaded = 'uploaded';
}
23 changes: 0 additions & 23 deletions app/Events/CheckoutAccepted.php

This file was deleted.

23 changes: 0 additions & 23 deletions app/Events/CheckoutDeclined.php

This file was deleted.

24 changes: 0 additions & 24 deletions app/Events/UserMerged.php

This file was deleted.

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,10 @@ 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);
//$accessory->logUpload($file_name, e($request->input('notes')));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Models\Accessory;
use App\Models\AccessoryCheckout;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use \Illuminate\Contracts\View\View;
Expand Down Expand Up @@ -56,12 +57,12 @@ public function store(Request $request, $accessoryCheckoutId = null, $backto = n
$checkin_hours = date('H:i:s');
$checkin_at = date('Y-m-d H:i:s');
if ($request->filled('checkin_at')) {
$checkin_at = $request->input('checkin_at').' '.$checkin_hours;
$accessory->setLogDate(new Carbon($request->input('checkin_at').' '.$checkin_hours));
}
$accessory->setLogTarget($accessory_checkout);

// Was the accessory updated?
if ($accessory_checkout->delete()) {
event(new CheckoutableCheckedIn($accessory, $accessory_checkout->assignedTo, auth()->user(), $request->input('note'), $checkin_at));
if ($accessory->checkInAndSave()) {

session()->put(['redirect_option' => $request->get('redirect_option')]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,34 +67,21 @@ public function create($id) : View | RedirectResponse
*/
public function store(AccessoryCheckoutRequest $request, Accessory $accessory) : RedirectResponse
{

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

$target = $this->determineCheckoutTarget();

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

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

$accessory_checkout = new AccessoryCheckout([
'accessory_id' => $accessory->id,
'created_at' => Carbon::now(),
'assigned_to' => $target->id,
'assigned_type' => $target::class,
'note' => $request->input('note'),
]);

$accessory_checkout->created_by = auth()->id();
$accessory_checkout->save();
}
$accessory->setLogTarget($this->determineCheckoutTarget());

$accessory->setLogQuantity($request->input('checkout_qty', 1));
$accessory->setLogNote($request->input('note'));

event(new CheckoutableCheckedOut($accessory, $target, auth()->user(), $request->input('note')));
event(new CheckoutableCheckedOut($accessory, $accessory->getLogTarget(), auth()->user(), $accessory->getLogNote()));

$request->request->add(['checkout_to_type' => request('checkout_to_type')]);
$request->request->add(['assigned_to' => $target->id]);
$request->request->add(['assigned_to' => $accessory->getLogTarget()->id]);

session()->put(['redirect_option' => $request->get('redirect_option'), 'checkout_to_type' => $request->get('checkout_to_type')]);

$accessory->checkOutAndSave();

// Redirect to the new accessory page
return redirect()->to(Helper::getRedirectOption($request, $accessory->id, 'Accessories'))
Expand Down
Loading