Viewing file: AccountPoolController.php (6.49 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request; use App\Models\HilltopAccount; use App\Models\AuditLog; use App\Models\SystemAlert; use Illuminate\Support\Facades\DB;
class AccountPoolController extends Controller { /** * Display a listing of accounts. */ public function index(Request $request) { $query = HilltopAccount::with('assignedUser');
// Filter by status if ($request->has('status')) { $query->where('status', $request->status); }
// Search if ($request->has('search')) { $search = $request->search; $query->where(function ($q) use ($search) { $q->where('alias', 'like', "%{$search}%") ->orWhere('hilltop_id', 'like', "%{$search}%") ->orWhereHas('assignedUser', function ($q) use ($search) { $q->where('email', 'like', "%{$search}%"); }); }); }
$accounts = $query->orderBy('created_at', 'desc')->get();
return response()->json($accounts); }
/** * Store a newly created account. */ public function store(Request $request) { $validated = $request->validate([ 'alias' => 'required|string|unique:hilltop_accounts', 'hilltop_id' => 'required|string|unique:hilltop_accounts', 'balance' => 'nullable|numeric|min:0', 'notes' => 'nullable|string', ]);
$account = HilltopAccount::create([ 'alias' => $validated['alias'], 'hilltop_id' => $validated['hilltop_id'], 'balance' => $validated['balance'] ?? 0, 'notes' => $validated['notes'] ?? null, 'status' => 'available', ]);
AuditLog::log('Account Created', 'account_pool', $account->id, ['alias' => $account->alias, 'hilltop_id' => $account->hilltop_id]);
return response()->json([ 'message' => 'Account added successfully', 'account' => $account->load('assignedUser'), ], 201); }
/** * Update the specified account. */ public function update(Request $request, $id) { $account = HilltopAccount::findOrFail($id);
$validated = $request->validate([ 'alias' => 'sometimes|string|unique:hilltop_accounts,alias,' . $id, 'hilltop_id' => 'sometimes|string|unique:hilltop_accounts,hilltop_id,' . $id, 'balance' => 'sometimes|numeric|min:0', 'notes' => 'nullable|string', 'status' => 'sometimes|in:available,assigned,unavailable', ]);
$account->update($validated);
AuditLog::log('Account Updated', 'account_pool', $account->id, $account->getChanges());
return response()->json([ 'message' => 'Account updated successfully', 'account' => $account->load('assignedUser'), ]); }
/** * Remove the specified account. */ public function destroy($id) { $account = HilltopAccount::findOrFail($id); if ($account->status === 'assigned') { return response()->json([ 'message' => 'Cannot delete an assigned account. Please unassign it first.', ], 422); }
$account->delete();
AuditLog::log('Account Deleted', 'account_pool', $account->id, ['alias' => $account->alias]);
return response()->json([ 'message' => 'Account deleted successfully', ]); }
/** * Sync a single account balance. */ public function sync($id) { $account = HilltopAccount::findOrFail($id);
// TODO: Implement actual HilltopAds API sync // For now, just update the last_synced_at timestamp $account->update([ 'last_synced_at' => now(), ]);
AuditLog::log('Account Synced', 'account_pool', $account->id, ['alias' => $account->alias]);
return response()->json([ 'message' => 'Account synced successfully', 'account' => $account->load('assignedUser'), ]); }
/** * Sync all accounts. */ public function syncAll() { // TODO: Implement actual HilltopAds API sync for all accounts // For now, just update all last_synced_at timestamps HilltopAccount::query()->update([ 'last_synced_at' => now(), ]);
AuditLog::log('All Accounts Synced', 'account_pool', 0, []);
return response()->json([ 'message' => 'All accounts synced successfully', ]); }
/** * Assign account to a user. */ public function assign(Request $request, $id) { $account = HilltopAccount::findOrFail($id);
$validated = $request->validate([ 'user_id' => 'required|exists:users,id', ]);
if ($account->status === 'assigned') { return response()->json([ 'message' => 'Account is already assigned', ], 422); }
$account->update([ 'assigned_user_id' => $validated['user_id'], 'status' => 'assigned', ]);
AuditLog::log('Account Assigned', 'account_pool', $account->id, ['user_id' => $validated['user_id'], 'alias' => $account->alias]);
return response()->json([ 'message' => 'Account assigned successfully', 'account' => $account->load('assignedUser'), ]); }
/** * Unassign account from user. */ public function unassign($id) { $account = HilltopAccount::findOrFail($id);
$account->update([ 'assigned_user_id' => null, 'status' => 'available', ]);
AuditLog::log('Account Unassigned', 'account_pool', $account->id, ['alias' => $account->alias]);
return response()->json([ 'message' => 'Account unassigned successfully', 'account' => $account->load('assignedUser'), ]); }
/** * Get statistics. */ public function stats() { $total = HilltopAccount::count(); $available = HilltopAccount::where('status', 'available')->count(); $assigned = HilltopAccount::where('status', 'assigned')->count(); $unavailable = HilltopAccount::where('status', 'unavailable')->count();
return response()->json([ 'total' => $total, 'available' => $available, 'assigned' => $assigned, 'unavailable' => $unavailable, ]); } }
|