update
This commit is contained in:
@@ -450,13 +450,20 @@ class CustomerController extends Controller
|
||||
'password' => Hash::make($password),
|
||||
]);
|
||||
|
||||
\Illuminate\Support\Facades\Mail::to($customer->email)->send(
|
||||
new \App\Mail\Customer\PasswordResetByAdmin([
|
||||
'fullname' => $customer->fullname,
|
||||
'email' => $customer->email,
|
||||
'password' => $password,
|
||||
])
|
||||
);
|
||||
$passwordMail = new \App\Mail\Customer\PasswordResetByAdmin([
|
||||
'fullname' => $customer->fullname,
|
||||
'email' => $customer->email,
|
||||
'password' => $password,
|
||||
]);
|
||||
|
||||
\Illuminate\Support\Facades\Mail::to($customer->email)->send($passwordMail);
|
||||
|
||||
\App\Models\Mail::create([
|
||||
'subject' => $passwordMail->envelope()->subject,
|
||||
'to' => $customer->email,
|
||||
'customer_id' => $customer->id,
|
||||
'message' => $passwordMail->render(),
|
||||
]);
|
||||
|
||||
Log::create([
|
||||
'agent' => 'admin',
|
||||
@@ -558,6 +565,15 @@ class CustomerController extends Controller
|
||||
|
||||
$mailer->send(new ManualCustomerEmail($subject, $message));
|
||||
|
||||
\App\Models\Mail::create([
|
||||
'template_id' => $template->id ?? 0,
|
||||
'subject' => $subject,
|
||||
'to' => implode(', ', $to),
|
||||
'cc' => implode(', ', $cc),
|
||||
'customer_id' => $customer->id,
|
||||
'message' => $message,
|
||||
]);
|
||||
|
||||
$admin = Auth::guard('admin')->user();
|
||||
Log::create([
|
||||
'agent' => 'admin',
|
||||
|
||||
@@ -15,7 +15,7 @@ final class EmailLogController extends Controller
|
||||
{
|
||||
$limit = min((int) $request->get('limit', $request->get('per_page', 20)), 200);
|
||||
|
||||
$query = Mail::query()->orderByDesc('created_at');
|
||||
$query = Mail::query()->with(['customer', 'template'])->orderByDesc('created_at');
|
||||
|
||||
// sort=field_asc|field_desc
|
||||
if ($sort = $request->get('sort')) {
|
||||
@@ -30,7 +30,7 @@ final class EmailLogController extends Controller
|
||||
}
|
||||
|
||||
if ($status = $request->get('status')) {
|
||||
$query->where('status', $status);
|
||||
$query->where('status', array_search($status, Mail::STATUS_MAP, true) ?: $status);
|
||||
}
|
||||
if ($customerId = $request->get('customer_id')) {
|
||||
$query->where('customer_id', (int) $customerId);
|
||||
@@ -47,7 +47,7 @@ final class EmailLogController extends Controller
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'data' => [
|
||||
'items' => $paginator->items(),
|
||||
'items' => $paginator->getCollection()->map($this->mapLog(...))->all(),
|
||||
'total' => $paginator->total(),
|
||||
'per_page' => $paginator->perPage(),
|
||||
'current_page' => $paginator->currentPage(),
|
||||
@@ -58,12 +58,28 @@ final class EmailLogController extends Controller
|
||||
|
||||
public function show(int $id): JsonResponse
|
||||
{
|
||||
$log = Mail::find($id);
|
||||
$log = Mail::with(['customer', 'template'])->find($id);
|
||||
|
||||
if (!$log) {
|
||||
return response()->json(['error' => true, 'message' => 'Không tìm thấy email log'], 404);
|
||||
}
|
||||
|
||||
return response()->json(['error' => false, 'data' => $log]);
|
||||
return response()->json(['error' => false, 'data' => $this->mapLog($log)]);
|
||||
}
|
||||
|
||||
private function mapLog(Mail $log): array
|
||||
{
|
||||
return [
|
||||
'id' => $log->id,
|
||||
'to_email' => $log->to,
|
||||
'to_name' => $log->customer->fullname ?? null,
|
||||
'customer_id' => $log->customer_id,
|
||||
'subject' => $log->subject,
|
||||
'template' => $log->template->name ?? '—',
|
||||
'status' => $log->status,
|
||||
'error_message' => null,
|
||||
'sent_at' => $log->status === 'sent' ? $log->created_at : null,
|
||||
'created_at' => $log->created_at,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,22 +13,68 @@ class NotificationController extends Controller
|
||||
{
|
||||
$recipient = $this->resolveRecipient($recipientType);
|
||||
|
||||
$notifications = Notification::query()
|
||||
$query = Notification::query()
|
||||
->where('recipient_type', $recipientType)
|
||||
->where('recipient_id', $recipient->id)
|
||||
->orderByDesc('created_at')
|
||||
->limit(20)
|
||||
->get();
|
||||
->where('recipient_id', $recipient->id);
|
||||
|
||||
$unreadCount = (clone $query)->where('is_read', false)->count();
|
||||
|
||||
if ($request->filled('is_read')) {
|
||||
$query->where('is_read', $request->boolean('is_read'));
|
||||
}
|
||||
|
||||
if ($request->filled('search')) {
|
||||
$search = $request->string('search');
|
||||
$query->where(function ($q) use ($search) {
|
||||
$q->where('title', 'like', "%{$search}%")
|
||||
->orWhere('body', 'like', "%{$search}%");
|
||||
});
|
||||
}
|
||||
|
||||
$query->orderByDesc('created_at');
|
||||
|
||||
if ($request->filled('page') || $request->filled('per_page')) {
|
||||
$perPage = (int) $request->integer('per_page', 15);
|
||||
$paginator = $query->paginate($perPage, ['*'], 'page', (int) $request->integer('page', 1));
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'data' => [
|
||||
'items' => $paginator->items(),
|
||||
'total' => $paginator->total(),
|
||||
'unread_count' => $unreadCount,
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
$notifications = $query->limit(20)->get();
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'data' => [
|
||||
'items' => $notifications,
|
||||
'unread_count' => $notifications->where('is_read', false)->count(),
|
||||
'unread_count' => $unreadCount,
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function show(Request $request, string $recipientType, int $id): JsonResponse
|
||||
{
|
||||
$recipient = $this->resolveRecipient($recipientType);
|
||||
|
||||
$notification = Notification::query()
|
||||
->where('recipient_type', $recipientType)
|
||||
->where('recipient_id', $recipient->id)
|
||||
->whereKey($id)
|
||||
->first();
|
||||
|
||||
if (!$notification) {
|
||||
return response()->json(['error' => true, 'message' => 'Notification not found'], 404);
|
||||
}
|
||||
|
||||
return response()->json(['error' => false, 'data' => $notification]);
|
||||
}
|
||||
|
||||
public function markRead(Request $request, string $recipientType, int $id): JsonResponse
|
||||
{
|
||||
$recipient = $this->resolveRecipient($recipientType);
|
||||
|
||||
@@ -661,6 +661,77 @@ class VCenterManagerController extends Controller
|
||||
], 400);
|
||||
}
|
||||
|
||||
public function stats(Request $request)
|
||||
{
|
||||
$vmId = (int) $request->input('vm_id');
|
||||
|
||||
$vm = VirtualMachines::find($vmId);
|
||||
|
||||
if (!$vm) {
|
||||
return response()->json(['error' => true, 'message' => 'VM not found'], 404);
|
||||
}
|
||||
|
||||
$vmwareId = $vm->vm_id;
|
||||
|
||||
if (!$vmwareId) {
|
||||
return response()->json(['error' => true, 'message' => 'VM VMware ID not found'], 400);
|
||||
}
|
||||
|
||||
if ($vm->power_state !== 'POWERED_ON') {
|
||||
return response()->json(['error' => true, 'message' => 'VM is not powered on'], 400);
|
||||
}
|
||||
|
||||
$vcenter = $vm->datacenter?->vCenter ?? $vm->host?->datacenter?->vCenter;
|
||||
|
||||
if (!$vcenter) {
|
||||
return response()->json(['error' => true, 'message' => 'vCenter not found'], 400);
|
||||
}
|
||||
|
||||
$vCenterService = new \App\Http\Services\vCenter();
|
||||
|
||||
if (!$vCenterService->loginServer($vcenter)) {
|
||||
return response()->json(['error' => true, 'message' => 'Cannot login to vCenter'], 400);
|
||||
}
|
||||
|
||||
$result = $vCenterService->GetVMStats($vmwareId);
|
||||
|
||||
if ($result['error'] ?? true) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => $result['message'] ?? 'Failed to fetch VM stats',
|
||||
], 400);
|
||||
}
|
||||
|
||||
$stats = $result['data'];
|
||||
$cpuPercent = $stats['cpu']['used_percent'] ?? 0;
|
||||
$memPercent = $stats['memory']['usage_percent'] ?? 0;
|
||||
|
||||
// ponytail: overall_status is a CPU/RAM usage threshold heuristic, not real vCenter
|
||||
// health/alarms — that needs the SOAP EventManager/AlarmManager, not wired up in this
|
||||
// codebase (REST Automation API only). Upgrade path: add a SOAP client and read
|
||||
// triggeredAlarmState off the VM's ManagedEntity if per-alarm detail is ever needed.
|
||||
$overallStatus = match (true) {
|
||||
$cpuPercent >= 90 || $memPercent >= 90 => 'red',
|
||||
$cpuPercent >= 75 || $memPercent >= 75 => 'yellow',
|
||||
default => 'green',
|
||||
};
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'data' => [
|
||||
'overall_status' => $overallStatus,
|
||||
'cpu_usage_percent' => round($cpuPercent, 1),
|
||||
'mem_usage_percent' => round($memPercent, 1),
|
||||
'mem_usage_mb' => $stats['memory']['used_MiB'] ?? 0,
|
||||
'mem_total_mb' => $stats['memory']['size_MiB'] ?? 0,
|
||||
// ponytail: no uptime source wired up (would need SOAP PerfManager
|
||||
// sys.uptime.latest); frontend renders "—" when null.
|
||||
'uptime_seconds' => null,
|
||||
'alarms' => [],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function getPoweredOffVms()
|
||||
{
|
||||
$vms = VirtualMachines::whereNotNull('vm_id')
|
||||
|
||||
Reference in New Issue
Block a user