- admin can edit, approve or reject broker registration - admin can edit, delete or impersonate as broker
80 lines
2.6 KiB
PHP
80 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Actions\UpdateBrokerAction;
|
|
use App\Enums\UserStatus;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\StoreBrokerProfileRequest;
|
|
use App\Models\Broker;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class BrokerController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
return view('dashboards.admin.brokers.index')
|
|
->with('activeBrokers', Broker::select(['id', 'location'])
|
|
->whereRelation('user', 'status', UserStatus::Active->value)
|
|
->with('user:id,name,email,role_id,role_type')
|
|
->get()
|
|
)
|
|
->with('pendingBrokers', Broker::select(['id', 'location'])
|
|
->whereRelation('user', 'status', UserStatus::Pending->value)
|
|
->with('user:id,name,email,role_id,role_type')
|
|
->get()
|
|
);
|
|
}
|
|
|
|
public function edit(Broker $broker)
|
|
{
|
|
return view('dashboards.admin.brokers.edit')
|
|
->with('profile', $broker->user)
|
|
->with('backLink', route('admin.brokers.index'))
|
|
->with('actionLink', route('admin.brokers.update', $broker));
|
|
}
|
|
|
|
public function update(StoreBrokerProfileRequest $request, Broker $broker, UpdateBrokerAction $action)
|
|
{
|
|
try {
|
|
$action->execute($request->validated(), $broker->user);
|
|
|
|
return to_route('admin.brokers.index')
|
|
->with('success', 'Profile updated successfully.');
|
|
} catch (\Throwable $e) {
|
|
Log::error('Broker Profile Update Failed: ', [$e->getMessage(), $e->getTrace()]);
|
|
|
|
return back()->withInput()->with('error', 'Something went wrong.');
|
|
}
|
|
}
|
|
|
|
public function destroy(Broker $broker)
|
|
{
|
|
try {
|
|
\DB::transaction(function () use ($broker) {
|
|
$broker->user->delete();
|
|
$broker->delete();
|
|
});
|
|
|
|
return back()->with('success', 'Broker deleted successfully.');
|
|
} catch (\Throwable $e) {
|
|
Log::error('Broker Delete Failed: ', [$e->getMessage(), $e->getTrace()]);
|
|
|
|
return back()->with('error', 'Something went wrong.');
|
|
}
|
|
}
|
|
|
|
public function approve(Broker $broker)
|
|
{
|
|
try {
|
|
$broker->user->update(['status' => UserStatus::Active->value]);
|
|
|
|
return to_route('admin.brokers.index')->with('success', 'Broker approved successfully.');
|
|
} catch (\Throwable $e) {
|
|
Log::error('Broker Approval Failed: ', [$e->getMessage(), $e->getTrace()]);
|
|
|
|
return back()->with('error', 'Something went wrong.');
|
|
}
|
|
}
|
|
}
|