2025-12-29 02:57:25 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Actions\User;
|
|
|
|
|
|
|
|
|
|
use App\Models\User;
|
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
|
|
|
|
|
class EditUserAction
|
|
|
|
|
{
|
|
|
|
|
public function execute(User $user, array $data): bool
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
DB::beginTransaction();
|
2026-08-17 13:30:49 +02:00
|
|
|
|
2025-12-29 02:57:25 +01:00
|
|
|
Log::info('EditUserAction: Starting user update', [
|
|
|
|
|
'user_id' => $user->id,
|
|
|
|
|
'old_name' => $user->name,
|
|
|
|
|
'new_name' => $data['name'],
|
|
|
|
|
'planner_id' => $user->planner_id,
|
|
|
|
|
]);
|
2026-08-17 13:30:49 +02:00
|
|
|
|
2025-12-29 02:57:25 +01:00
|
|
|
$result = $user->update([
|
|
|
|
|
'name' => $data['name'],
|
|
|
|
|
]);
|
2026-08-17 13:30:49 +02:00
|
|
|
|
2025-12-29 02:57:25 +01:00
|
|
|
Log::info('EditUserAction: Update result', [
|
|
|
|
|
'result' => $result,
|
|
|
|
|
'user_id' => $user->id,
|
|
|
|
|
]);
|
2026-08-17 13:30:49 +02:00
|
|
|
|
|
|
|
|
if (! $result) {
|
2025-12-29 02:57:25 +01:00
|
|
|
throw new \Exception('User update returned false');
|
|
|
|
|
}
|
2026-08-17 13:30:49 +02:00
|
|
|
|
2025-12-29 02:57:25 +01:00
|
|
|
// Verify the update actually happened
|
|
|
|
|
$user->refresh();
|
|
|
|
|
if ($user->name !== $data['name']) {
|
|
|
|
|
throw new \Exception('User update did not persist to database');
|
|
|
|
|
}
|
2026-08-17 13:30:49 +02:00
|
|
|
|
2025-12-29 02:57:25 +01:00
|
|
|
DB::commit();
|
2026-08-17 13:30:49 +02:00
|
|
|
|
2025-12-29 02:57:25 +01:00
|
|
|
Log::info('EditUserAction: User successfully updated', [
|
|
|
|
|
'user_id' => $user->id,
|
|
|
|
|
'updated_name' => $user->name,
|
|
|
|
|
]);
|
2026-08-17 13:30:49 +02:00
|
|
|
|
2025-12-29 02:57:25 +01:00
|
|
|
return true;
|
2026-08-17 13:30:49 +02:00
|
|
|
|
2025-12-29 02:57:25 +01:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
DB::rollBack();
|
2026-08-17 13:30:49 +02:00
|
|
|
|
2025-12-29 02:57:25 +01:00
|
|
|
Log::error('EditUserAction: User update failed', [
|
|
|
|
|
'user_id' => $user->id,
|
|
|
|
|
'error' => $e->getMessage(),
|
|
|
|
|
'trace' => $e->getTraceAsString(),
|
|
|
|
|
]);
|
2026-08-17 13:30:49 +02:00
|
|
|
|
2025-12-29 02:57:25 +01:00
|
|
|
throw $e;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-08-17 13:30:49 +02:00
|
|
|
}
|