This commit is contained in:
baodev0
2026-06-22 08:43:32 +07:00
parent 3243473843
commit 0c436153c6
22 changed files with 1389 additions and 95 deletions
+2 -1
View File
@@ -39,7 +39,8 @@
"Bash(grep -n -A 40 \"CREATE TABLE \\\\`services\\\\`\" /Users/baro/Desktop/Workspace/thegioiso/web/portal-infra/portal.sql)",
"Bash(grep -n -A 30 \"CREATE TABLE \\\\`products\\\\`\" /Users/baro/Desktop/Workspace/thegioiso/web/portal-infra/portal.sql)",
"Bash(grep -n -A 30 \"CREATE TABLE \\\\`vmware\\\\`\" /Users/baro/Desktop/Workspace/thegioiso/web/portal-infra/portal.sql)",
"Bash(pnpm tsc *)"
"Bash(pnpm tsc *)",
"Bash(grep *)"
]
}
}
@@ -2,28 +2,115 @@
namespace App\Http\Controllers\Api\Admin\Dashboard;
use App\Enums\Invoice\InvoiceStatus;
use App\Enums\Order\OrderStatus;
use App\Enums\TransactionStatus;
use App\Http\Controllers\Controller;
use App\Models\Customer;
use App\Models\Invoice;
use App\Models\Order;
use App\Models\Service;
use App\Models\Transaction;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class DashboardController extends Controller
{
// Các type được tính là "nạp tiền vào ví"
private const DEPOSIT_TYPES = ['manual', 'vnpay', 'deposit'];
/** Map Order status int → string FE */
private function orderStatusLabel(int $status): string
{
return match ($status) {
OrderStatus::PENDING->value => 'pending',
OrderStatus::ACTIVE->value => 'active',
OrderStatus::CANCELLED->value => 'cancelled',
OrderStatus::PROCESSING->value => 'processing',
default => (string) $status,
};
}
/** Map Invoice status int → FE payment status (paid/pending/failed) */
private function paymentStatusFromInvoice(?int $status): string
{
return match ($status) {
InvoiceStatus::PAID->value => 'paid',
InvoiceStatus::UNPAID->value => 'pending',
InvoiceStatus::CANCELLED->value => 'failed',
default => 'pending',
};
}
/** % delta giữa current vs previous (an toàn chia 0) */
private function deltaPct(float $current, float $previous): float
{
if ($previous == 0.0) {
return $current > 0 ? 100.0 : 0.0;
}
return round((($current - $previous) / $previous) * 100, 2);
}
public function stats(): JsonResponse
{
$total_customers = Customer::count();
$now = now();
$startOfMonth = $now->copy()->startOfMonth();
$startOfPrevMonth = $now->copy()->subMonthNoOverflow()->startOfMonth();
$endOfPrevMonth = $now->copy()->subMonthNoOverflow()->endOfMonth();
$total_deposits = (float) Transaction::where('type', 'manual')
->where('status', TransactionStatus::COMPLETED->value)
// Customers
$totalCustomers = Customer::count();
$newThisMonth = Customer::where('created_at', '>=', $startOfMonth)->count();
$newPrevMonth = Customer::whereBetween('created_at', [$startOfPrevMonth, $endOfPrevMonth])->count();
// Deposits (transactions completed, type ∈ DEPOSIT_TYPES)
$depositBase = Transaction::whereIn('type', self::DEPOSIT_TYPES)
->where('status', TransactionStatus::COMPLETED->value);
$totalDeposits = (float) (clone $depositBase)->sum('amount');
$depositsThisMonth = (float) (clone $depositBase)
->where('created_at', '>=', $startOfMonth)
->sum('amount');
$depositsPrevMonth = (float) (clone $depositBase)
->whereBetween('created_at', [$startOfPrevMonth, $endOfPrevMonth])
->sum('amount');
// Số dư ví khách hàng (cột customers.total)
$totalBalance = (float) Customer::sum('total');
// Services
$totalActiveServices = Service::where('status', 'active')->count();
$expiring7d = Service::where('status', 'active')
->whereNotNull('expires_at')
->whereBetween('expires_at', [$now, $now->copy()->addDays(7)])
->count();
// Orders
$totalOrdersThisMonth = Order::where('created_at', '>=', $startOfMonth)->count();
$totalOrdersPrevMonth = Order::whereBetween('created_at', [$startOfPrevMonth, $endOfPrevMonth])->count();
$pendingOrders = Order::where('status', OrderStatus::PENDING->value)->count();
return response()->json([
'error' => false,
'data' => [
'total_customers' => $total_customers,
'total_deposits' => $total_deposits,
'total_customers' => $totalCustomers,
'new_customers_this_month' => $newThisMonth,
'new_customers_delta_pct' => $this->deltaPct($newThisMonth, $newPrevMonth),
'total_deposits' => $totalDeposits,
'deposits_this_month' => $depositsThisMonth,
'deposits_delta_pct' => $this->deltaPct($depositsThisMonth, $depositsPrevMonth),
'total_balance' => $totalBalance,
'total_active_services' => $totalActiveServices,
'expiring_services_7d' => $expiring7d,
'total_orders_this_month' => $totalOrdersThisMonth,
'pending_orders' => $pendingOrders,
'orders_delta_pct' => $this->deltaPct($totalOrdersThisMonth, $totalOrdersPrevMonth),
],
]);
}
@@ -32,34 +119,322 @@ class DashboardController extends Controller
{
$year = $request->integer('year', now()->year);
$deposits = Transaction::where('type', 'manual')
$deposits = Transaction::whereIn('type', self::DEPOSIT_TYPES)
->where('status', TransactionStatus::COMPLETED->value)
->whereYear('created_at', $year)
->selectRaw('MONTH(created_at) as month, SUM(amount) as total_deposits')
->groupBy('month')
->pluck('total_deposits', 'month');
->selectRaw('MONTH(created_at) as m, SUM(amount) as total')
->groupBy('m')
->pluck('total', 'm');
$newCustomers = Customer::whereYear('created_at', $year)
->selectRaw('MONTH(created_at) as month, COUNT(*) as new_customers')
->groupBy('month')
->pluck('new_customers', 'month');
->selectRaw('MONTH(created_at) as m, COUNT(*) as c')
->groupBy('m')
->pluck('c', 'm');
$months = ['T1', 'T2', 'T3', 'T4', 'T5', 'T6', 'T7', 'T8', 'T9', 'T10', 'T11', 'T12'];
$orders = Order::whereYear('created_at', $year)
->selectRaw('MONTH(created_at) as m, COUNT(*) as c')
->groupBy('m')
->pluck('c', 'm');
$data = collect(range(1, 12))->map(function ($m) use ($months, $deposits, $newCustomers) {
return [
'month' => $months[$m - 1],
// Doanh thu dịch vụ = tổng invoice đã PAID trong tháng
$serviceRevenue = Invoice::where('status', InvoiceStatus::PAID->value)
->whereYear('paid_at', $year)
->selectRaw('MONTH(paid_at) as m, SUM(total) as total')
->groupBy('m')
->pluck('total', 'm');
$data = collect(range(1, 12))->map(fn ($m) => [
'month' => 'T' . $m,
'deposits' => (float) ($deposits[$m] ?? 0),
'new_customers' => (int) ($newCustomers[$m] ?? 0),
'orders' => (int) ($orders[$m] ?? 0),
'service_revenue' => (float) ($serviceRevenue[$m] ?? 0),
])->values();
return response()->json(['error' => false, 'data' => $data]);
}
public function dailyStats(Request $request): JsonResponse
{
$month = $request->integer('month', now()->month);
$year = $request->integer('year', now()->year);
$start = Carbon::create($year, $month, 1)->startOfMonth();
$end = $start->copy()->endOfMonth();
$daysInMonth = $start->daysInMonth;
$orders = Order::whereBetween('created_at', [$start, $end])
->selectRaw('DAY(created_at) as d, COUNT(*) as c')
->groupBy('d')
->pluck('c', 'd');
$depositsAgg = Transaction::whereIn('type', self::DEPOSIT_TYPES)
->where('status', TransactionStatus::COMPLETED->value)
->whereBetween('created_at', [$start, $end])
->selectRaw('DAY(created_at) as d, COUNT(*) as c, SUM(amount) as total')
->groupBy('d')
->get()
->keyBy('d');
$data = collect(range(1, $daysInMonth))->map(fn ($d) => [
'day' => str_pad((string) $d, 2, '0', STR_PAD_LEFT),
'orders' => (int) ($orders[$d] ?? 0),
'deposits_count' => (int) ($depositsAgg[$d]->c ?? 0),
'deposits_amount' => (float) ($depositsAgg[$d]->total ?? 0),
])->values();
return response()->json(['error' => false, 'data' => $data]);
}
public function recentOrders(Request $request): JsonResponse
{
$limit = (int) $request->integer('limit', 8);
$limit = max(1, min(50, $limit));
$orders = Order::with([
'customer:id,fullname,email',
'orderItems:id,order_id,product_id',
'orderItems.product:id,name',
'invoice:id,order_id,status',
])
->orderByDesc('created_at')
->limit($limit)
->get();
$items = $orders->map(function (Order $o) {
$firstItem = $o->orderItems->first();
$serviceName = $firstItem?->product?->name ?? '—';
$invoiceStatus = $o->invoice?->status ?? null;
return [
'id' => $o->id,
'code' => $o->order_num,
'customer_id' => $o->customer_id,
'customer_name' => $o->customer->fullname ?? '—',
'customer_email' => $o->customer->email ?? '',
'service_name' => $serviceName,
'amount' => (float) $o->total,
'payment_status' => $this->paymentStatusFromInvoice($invoiceStatus),
'status' => $this->orderStatusLabel((int) $o->status),
'created_at' => $o->created_at?->toISOString(),
];
})->values();
return response()->json([
'error' => false,
'data' => $data,
'data' => ['items' => $items, 'total' => Order::count()],
]);
}
public function recentDeposits(Request $request): JsonResponse
{
$limit = (int) $request->integer('limit', 6);
$limit = max(1, min(50, $limit));
$txs = Transaction::with('customer:id,fullname')
->whereIn('type', self::DEPOSIT_TYPES)
->orderByDesc('created_at')
->limit($limit)
->get();
$items = $txs->map(function (Transaction $t) {
$status = match ((int) $t->status) {
TransactionStatus::COMPLETED->value => 'success',
TransactionStatus::PENDING->value => 'pending',
TransactionStatus::FAILED->value => 'failed',
default => 'pending',
};
return [
'id' => $t->id,
'code' => $t->public_code ?? $t->trans ?? ('TX-' . $t->id),
'customer_id' => $t->customer_id,
'customer_name' => $t->customer->fullname ?? '—',
'amount' => (float) $t->amount,
'method' => (string) ($t->type ?? 'manual'),
'status' => $status,
'created_at' => $t->created_at?->toISOString(),
];
})->values();
return response()->json([
'error' => false,
'data' => [
'items' => $items,
'total' => Transaction::whereIn('type', self::DEPOSIT_TYPES)->count(),
],
]);
}
public function topServices(Request $request): JsonResponse
{
$limit = (int) $request->integer('limit', 5);
$limit = max(1, min(20, $limit));
$startOfMonth = now()->startOfMonth();
$startOfPrevMonth = now()->copy()->subMonthNoOverflow()->startOfMonth();
$endOfPrevMonth = now()->copy()->subMonthNoOverflow()->endOfMonth();
// Top theo số service đang active của từng product
$rows = Service::query()
->select(
'services.product_id',
'products.name as product_name',
'products.slug as product_slug',
DB::raw('COUNT(*) as active_count')
)
->join('products', 'products.id', '=', 'services.product_id')
->where('services.status', 'active')
->whereNotNull('services.product_id')
->groupBy('services.product_id', 'products.name', 'products.slug')
->orderByDesc('active_count')
->limit($limit)
->get();
$productIds = $rows->pluck('product_id')->all();
// Đã bán & doanh thu kỳ hiện tại / kỳ trước (theo paid invoice item)
$soldCurrent = DB::table('order_items')
->join('orders', 'orders.id', '=', 'order_items.order_id')
->whereIn('order_items.product_id', $productIds)
->where('orders.created_at', '>=', $startOfMonth)
->selectRaw('order_items.product_id, COUNT(*) as sold, SUM(order_items.price * order_items.quantity) as revenue')
->groupBy('order_items.product_id')
->get()
->keyBy('product_id');
$soldPrev = DB::table('order_items')
->join('orders', 'orders.id', '=', 'order_items.order_id')
->whereIn('order_items.product_id', $productIds)
->whereBetween('orders.created_at', [$startOfPrevMonth, $endOfPrevMonth])
->selectRaw('order_items.product_id, SUM(order_items.price * order_items.quantity) as revenue')
->groupBy('order_items.product_id')
->get()
->keyBy('product_id');
$data = $rows->map(function ($r) use ($soldCurrent, $soldPrev) {
$cur = (float) ($soldCurrent[$r->product_id]->revenue ?? 0);
$prev = (float) ($soldPrev[$r->product_id]->revenue ?? 0);
return [
'id' => (int) $r->product_id,
'name' => $r->product_name,
'code' => $r->product_slug,
'active_count' => (int) $r->active_count,
'sold_count' => (int) ($soldCurrent[$r->product_id]->sold ?? 0),
'revenue' => $cur,
'trend_pct' => $this->deltaPct($cur, $prev),
];
})->values();
return response()->json(['error' => false, 'data' => $data]);
}
public function expiringServices(Request $request): JsonResponse
{
$withinDays = (int) $request->integer('within_days', 7);
$withinDays = max(1, min(60, $withinDays));
$now = now();
$until = $now->copy()->addDays($withinDays);
$services = Service::with(['customer:id,fullname', 'product:id,name'])
->where('status', 'active')
->whereNotNull('expires_at')
->whereBetween('expires_at', [$now, $until])
->orderBy('expires_at')
->limit(20)
->get();
$data = $services->map(function (Service $s) use ($now) {
$expires = $s->expires_at instanceof Carbon
? $s->expires_at
: Carbon::parse((string) $s->expires_at);
return [
'id' => $s->id,
'service_name' => $s->product->name ?? ($s->server_name ?? 'Dịch vụ'),
'customer_id' => $s->customer_id,
'customer_name' => $s->customer->fullname ?? '—',
'expired_at' => $expires->toISOString(),
'days_left' => max(0, (int) $now->diffInDays($expires, false)),
'amount' => 0.0,
];
})->values();
return response()->json(['error' => false, 'data' => $data]);
}
public function activities(Request $request): JsonResponse
{
$limit = (int) $request->integer('limit', 8);
$limit = max(1, min(30, $limit));
// Tổng hợp 3 nguồn rồi sort theo thời gian
$orders = Order::with('customer:id,fullname')
->orderByDesc('created_at')
->limit($limit)
->get()
->map(function (Order $o) {
$isCancelled = (int) $o->status === OrderStatus::CANCELLED->value;
$isActive = (int) $o->status === OrderStatus::ACTIVE->value;
$type = $isCancelled ? 'order_cancelled' : ($isActive ? 'order_completed' : 'order_created');
return [
'id' => 'order-' . $o->id,
'type' => $type,
'title' => 'Đơn ' . $o->order_num . ' ' . match ($type) {
'order_cancelled' => 'đã huỷ',
'order_completed' => 'đã kích hoạt',
default => 'được tạo',
},
'description' => ($o->customer->fullname ?? '—') . ' · ' . number_format((float) $o->total, 0, ',', '.') . 'đ',
'created_at' => $o->created_at?->toISOString(),
'_ts' => $o->created_at?->timestamp ?? 0,
];
});
$deposits = Transaction::with('customer:id,fullname')
->whereIn('type', self::DEPOSIT_TYPES)
->orderByDesc('created_at')
->limit($limit)
->get()
->map(function (Transaction $t) {
$ok = (int) $t->status === TransactionStatus::COMPLETED->value;
return [
'id' => 'tx-' . $t->id,
'type' => $ok ? 'deposit_success' : 'deposit_failed',
'title' => $ok ? 'Nạp tiền thành công' : 'Giao dịch nạp thất bại',
'description' => ($t->customer->fullname ?? '—') . ' · ' . number_format((float) $t->amount, 0, ',', '.') . 'đ',
'created_at' => $t->created_at?->toISOString(),
'_ts' => $t->created_at?->timestamp ?? 0,
];
});
$customers = Customer::orderByDesc('created_at')
->limit($limit)
->get(['id', 'fullname', 'email', 'created_at'])
->map(fn (Customer $c) => [
'id' => 'customer-' . $c->id,
'type' => 'customer_registered',
'title' => 'Khách hàng mới đăng ký',
'description' => ($c->fullname ?: $c->email) ?? '—',
'created_at' => $c->created_at?->toISOString(),
'_ts' => $c->created_at?->timestamp ?? 0,
]);
$data = $orders->concat($deposits)->concat($customers)
->sortByDesc('_ts')
->take($limit)
->map(function ($a) {
unset($a['_ts']);
return $a;
})
->values();
return response()->json(['error' => false, 'data' => $data]);
}
public function latestCustomers(): JsonResponse
{
$customers = Customer::orderByDesc('created_at')
@@ -69,7 +444,7 @@ class DashboardController extends Controller
return response()->json([
'error' => false,
'data' => [
'items' => $customers->map(fn(Customer $c) => [
'items' => $customers->map(fn (Customer $c) => [
'id' => $c->id,
'uuid' => $c->uuid,
'fullname' => $c->fullname,
@@ -23,6 +23,7 @@ class DomainController extends Controller
$perPage = min((int) $request->get('per_page', 15), 500);
$filter = $request->get('filter');
$status = $request->get('status');
$customerName = trim((string) $request->get('customer_name', ''));
$query = $this->domainRepository->model
->with(['server', 'customer'])
@@ -36,6 +37,12 @@ class DomainController extends Controller
$query->where('is_created', (int) $status);
}
if ($customerName !== '') {
$query->whereHas('customer', function ($q) use ($customerName) {
$q->where('fullname', 'like', "%{$customerName}%");
});
}
$paginator = $query->orderByDesc('id')->paginate($perPage);
$items = $paginator->getCollection()->map(fn($domain) => $this->formatDomain($domain));
@@ -0,0 +1,79 @@
<?php
declare(strict_types=1);
namespace App\Http\Controllers\Api\Admin\Provider;
use App\Http\Controllers\Controller;
use App\Services\VirtualMachine\VmActiveProviderService;
use Illuminate\Http\JsonResponse;
use Throwable;
/**
* Admin endpoints to toggle which row in the
* vcenter / datacenter / host / datastore / network chain is active.
*
* Only ONE row per parent scope can be active at a time — enforced by
* VmActiveProviderService::activate() inside a transaction. Deactivating
* a row cascade-deactivates its descendants.
*/
class ActiveInfraController extends Controller
{
public function __construct(private readonly VmActiveProviderService $service)
{
}
/** GET /administrator/provider/active-chain — returns the current active chain (or error). */
public function chain(): JsonResponse
{
try {
$chain = $this->service->resolveChain();
} catch (Throwable $e) {
return response()->json([
'error' => true,
'message' => $e->getMessage(),
], 422);
}
return response()->json([
'error' => false,
'data' => [
'vcenter' => $chain['vcenter']->only(['id', 'host', 'user', 'status']),
'datacenter' => $chain['datacenter']->only(['id', 'name', 'datacenter_id', 'status']),
'host' => $chain['host']->only(['id', 'name', 'host_id', 'status']),
'datastore' => $chain['datastore']->only(['id', 'name', 'datastore', 'status']),
'network' => $chain['network']->only(['id', 'name', 'network', 'status']),
],
]);
}
/** POST /administrator/provider/{type}/{id}/activate */
public function activate(string $type, int $id): JsonResponse
{
try {
$row = $this->service->activate($type, $id);
} catch (Throwable $e) {
return response()->json(['error' => true, 'message' => $e->getMessage()], 422);
}
return response()->json([
'error' => false,
'data' => $row,
]);
}
/** POST /administrator/provider/{type}/{id}/deactivate */
public function deactivate(string $type, int $id): JsonResponse
{
try {
$row = $this->service->deactivate($type, $id);
} catch (Throwable $e) {
return response()->json(['error' => true, 'message' => $e->getMessage()], 422);
}
return response()->json([
'error' => false,
'data' => $row,
]);
}
}
@@ -0,0 +1,73 @@
<?php
declare(strict_types=1);
namespace App\Http\Controllers\Api\Admin\Provider;
use App\Http\Controllers\Controller;
use App\Models\vCenter;
use App\Services\VirtualMachine\VmProviderSettingsService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
/**
* Reads / writes the provider config blob in setting key=`provider`.
*
* active_vcenter_id is read-only here — it mirrors module_vcenter.status=1.
* To switch the active vCenter, call ActiveInfraController::activate().
*/
class ProviderSettingController extends Controller
{
public function __construct(private readonly VmProviderSettingsService $service)
{
}
/** GET /administrator/provider/settings */
public function index(): JsonResponse
{
$data = $this->service->all();
// Mirror current truth: active vCenter id from module_vcenter
$data['active_vcenter_id'] = vCenter::where('status', 1)->value('id');
return response()->json([
'error' => false,
'data' => $data,
]);
}
/** PUT /administrator/provider/settings */
public function update(Request $request): JsonResponse
{
$validator = Validator::make($request->all(), [
'default_folder' => 'sometimes|string|max:100',
'default_resource_pool' => 'sometimes|string|max:100',
'default_datastore' => 'sometimes|string|max:100',
'name' => 'sometimes|nullable|string|max:120',
'region' => 'sometimes|nullable|string|max:60',
'tags' => 'sometimes|array',
'tags.*' => 'string|max:40',
]);
if ($validator->fails()) {
return response()->json([
'error' => true,
'message' => $validator->errors()->first(),
], 422);
}
$patch = $validator->validated();
// active_vcenter_id is read-only — strip if client sent it
unset($patch['active_vcenter_id']);
$merged = $this->service->merge($patch);
$merged['active_vcenter_id'] = vCenter::where('status', 1)->value('id');
return response()->json([
'error' => false,
'data' => $merged,
]);
}
}
@@ -8,6 +8,7 @@ use Illuminate\Http\Client\ConnectionException;
use Illuminate\Support\Str;
use App\Http\Services\Discord;
use App\Models\ModuleDatastore;
use App\Services\VirtualMachine\VmProviderSettingsService;
use Exception;
class VirtualMachineService
@@ -77,11 +78,13 @@ class VirtualMachineService
'addon' => $addon
]);
$datastores = ModuleDatastore::where('module_host_id', $host->id)->get();
/** @var VmProviderSettingsService $providerSettings */
$providerSettings = app(VmProviderSettingsService::class);
$datastoreId = $datastores->isNotEmpty()
? $datastores->first()->datastore
: 'datastore-3004';
$activeDatastore = ModuleDatastore::where('module_host_id', $host->id)
->where('status', 1)
->first();
$datastoreId = $activeDatastore?->datastore ?? $providerSettings->defaultDatastore();
$body = [
'spec' => [
@@ -89,9 +92,9 @@ class VirtualMachineService
'source' => $template,
'placement' => [
'datacenter' => $datacenter->datacenter_id,
'folder' => 'group-v1008',
'folder' => $providerSettings->defaultFolder(),
'host' => $host->host_id,
'resource_pool' => 'resgroup-3002',
'resource_pool' => $providerSettings->defaultResourcePool(),
'datastore' => $datastoreId,
],
'power_on' => false,
@@ -9,6 +9,7 @@ use App\Jobs\VirtualMachine\Provision\AssignIpToGuestJob;
use App\Models\OrderItem;
use App\Services\VirtualMachine\VmAddonResolver;
use App\Services\VirtualMachine\VmInfrastructureResolver;
use App\Services\VirtualMachine\VmIpReservationService;
use App\Services\VirtualMachine\VmModuleService;
use App\Services\VirtualMachine\VmNotificationService;
use App\Services\VirtualMachine\VmPowerService;
@@ -50,7 +51,11 @@ class CreateVirtualMachineJob implements ShouldQueue
VmServiceSyncService $serviceSync,
VmNotificationService $notification,
VmVcenterErrorTranslator $errorTranslator,
VmIpReservationService $ipReservation,
): bool {
$reservation = null;
$ipPoolId = null;
try {
$vmLogger->step('Create VM job started', [
'order_item_id' => $this->item->id,
@@ -66,6 +71,39 @@ class CreateVirtualMachineJob implements ShouldQueue
$infra = $infraResolver->resolve($this->item, $product);
$addonData = $addonResolver->resolve($this->item);
// Reserve IP BEFORE clone so VM name on vCenter, ModuleVmware.name
// and services.server_name all carry the format {ip}_{server_name}.
// Network comes from the active chain (active host → active network);
// product->ipAddress is no longer consulted for network selection.
if (!$infra->network) {
throw new RuntimeException("Active host id={$infra->host->id} chưa có network active.");
}
$networkId = $infra->network->id;
$dns = $product->ipAddress->dns ?? '8.8.8.8';
$picked = $ipReservation->reserve($networkId, $infra->host->id, $dns);
if (!$picked) {
throw new RuntimeException(
"Không còn IP free trong pool (network_id={$networkId}) để tạo VM."
);
}
$reservation = $picked['reservation'];
$ipPoolId = $picked['ip_pool_id'];
$reservedIp = $reservation->ip;
$originalName = $this->item->server_name ?? ('VM-' . $this->item->id);
$combinedName = "{$reservedIp}_{$originalName}";
$this->item->server_name = $combinedName;
$vmLogger->step('IP reserved for VM', [
'order_item_id' => $this->item->id,
'ip' => $reservedIp,
'ip_pool_id' => $ipPoolId,
'server_name' => $combinedName,
]);
if (!$vCenter->loginServer($infra->vcenter)) {
throw new RuntimeException("Login vào vCenter thất bại với host: {$infra->vcenter->host}");
}
@@ -142,6 +180,8 @@ class CreateVirtualMachineJob implements ShouldQueue
(string) $vmId,
$infra->isWindows ? VmProvisionService::OS_WINDOWS : VmProvisionService::OS_LINUX,
VmProvisionService::TEMPLATE_PASSWORD,
$reservation->id,
$ipPoolId,
)->onQueue('default');
$vmLogger->success('AssignIpToGuestJob dispatched', [
@@ -160,6 +200,10 @@ class CreateVirtualMachineJob implements ShouldQueue
return true;
} catch (Throwable $e) {
if ($reservation) {
$ipReservation->release($reservation, $ipPoolId);
}
$vmLogger->exception('Create VM job failed', $e, [
'order_item_id' => $this->item->id,
'is_retry' => $this->isRetry,
@@ -58,6 +58,8 @@ class AssignIpToGuestJob implements ShouldQueue
public readonly string $vmId,
public readonly string $osFamily, // VmProvisionService::OS_LINUX | OS_WINDOWS
public readonly string $guestPassword = VmProvisionService::TEMPLATE_PASSWORD,
public readonly ?int $reservationId = null,
public readonly ?int $reservedIpPoolId = null,
) {}
// ─────────────────────────────────────────────────────────────────────────
@@ -103,16 +105,22 @@ class AssignIpToGuestJob implements ShouldQueue
$guestUser = strtoupper($this->osFamily) === 'WINDOWS' ? 'Administrator' : 'root';
// ─── Attempt loop ────────────────────────────────────────────────────
// If reservationId is supplied, the IP was reserved upstream (CreateVirtualMachineJob)
// and baked into the VM name — we can't swap IPs on retry, so we run a single attempt.
$maxAttempts = $this->reservationId ? 1 : self::MAX_ATTEMPTS;
for ($attempt = 1; $attempt <= self::MAX_ATTEMPTS; $attempt++) {
for ($attempt = 1; $attempt <= $maxAttempts; $attempt++) {
Log::info('AssignIpToGuestJob: attempt', [
'attempt' => $attempt,
'module_vmware_id' => $this->moduleVmwareId,
'network_id' => $networkId,
'preserved' => (bool) $this->reservationId,
]);
// [3] Reserve a free IP with transaction + row lock
$picked = $this->reserveIp($networkId, $hostId, $dns);
// [3] Use upstream reservation if provided, else reserve a fresh IP.
$picked = $this->reservationId
? $this->loadReservation($this->reservationId, $this->reservedIpPoolId)
: $this->reserveIp($networkId, $hostId, $dns);
if (!$picked) {
Log::error('AssignIpToGuestJob: no free IPs in pool', [
@@ -220,7 +228,7 @@ class AssignIpToGuestJob implements ShouldQueue
Log::error('AssignIpToGuestJob: all attempts failed — marking VM inactive', [
'module_vmware_id' => $this->moduleVmwareId,
'vm_id' => $this->vmId,
'attempts' => self::MAX_ATTEMPTS,
'attempts' => $maxAttempts,
]);
ModuleVmware::where('id', $this->moduleVmwareId)->update(['status' => 0]);
@@ -336,6 +344,34 @@ class AssignIpToGuestJob implements ShouldQueue
});
}
/**
* Load a reservation that was created upstream (e.g. by CreateVirtualMachineJob).
* Returns the same shape as reserveIp() so the rest of the flow is identical.
*
* @return array{reservation:ModuleIpAddress,ip_pool_id:int,gateway:string,cidr:int}|null
*/
private function loadReservation(int $reservationId, ?int $ipPoolId): ?array
{
$reservation = ModuleIpAddress::find($reservationId);
if (!$reservation) {
Log::error('AssignIpToGuestJob: upstream reservation missing', [
'reservation_id' => $reservationId,
]);
return null;
}
$ipPool = $ipPoolId ? IpPool::find($ipPoolId) : null;
$cidr = (int) ($ipPool->cidr ?? 24);
$gateway = (string) ($reservation->gateway ?: ($ipPool->gateway ?? ''));
return [
'reservation' => $reservation,
'ip_pool_id' => $ipPoolId ?? 0,
'gateway' => $gateway,
'cidr' => $cidr ?: 24,
];
}
/**
* Release a failed reservation by deleting the pending ModuleIpAddress row.
* This returns the IP to the available pool.
+64
View File
@@ -0,0 +1,64 @@
<?php
namespace App\Mail\Customer;
use App\Models\Setting;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
class Welcome extends Mailable
{
use Queueable, SerializesModels;
public function __construct(public array $data) {}
public function envelope(): Envelope
{
$appName = $this->payload()['company_name'] ?? config('app.name', 'The Gioi So');
return new Envelope(
subject: 'Chào mừng bạn đến với ' . $appName,
);
}
public function content(): Content
{
$data = $this->payload();
return new Content(
view: 'emails.customer.welcome',
with: [
'appName' => $data['company_name'] ?? config('app.name', 'The Gioi So'),
'fullName' => $data['fullname'] ?? 'Quý khách',
'loginUrl' => $data['login_url'] ?? rtrim(env('FRONTEND_URL', config('app.url')), '/') . '/dang-nhap',
'supportEmail' => $data['support_email'] ?? config('mail.support_email', 'support@thegioiso.com'),
'supportPhone' => $data['support_phone'] ?? config('mail.support_phone', '1900.xxxx'),
],
);
}
public function attachments(): array
{
return [];
}
private function payload(): array
{
try {
$setting = Setting::where('key', 'setting')->first();
$settingValue = $setting ? json_decode((string) $setting->value, true) : [];
} catch (\Throwable) {
$settingValue = [];
}
return array_merge([
'company_name' => $settingValue['company'] ?? config('app.name', 'The Gioi So'),
'support_email' => $settingValue['email'] ?? config('mail.support_email', 'support@thegioiso.com'),
'support_phone' => $settingValue['hotline'] ?? ($settingValue['phone'] ?? config('mail.support_phone', '1900.xxxx')),
'login_url' => rtrim(env('FRONTEND_URL', config('app.url')), '/') . '/dang-nhap',
], $this->data);
}
}
+7 -1
View File
@@ -14,11 +14,17 @@ class ModuleDatacenter extends Model
protected $fillable = [
'name',
'datacenter_id',
'vcenter_id'
'vcenter_id',
'status',
];
public function vCenter()
{
return $this->belongsTo(\App\Models\vCenter::class,'vcenter_id','id');
}
public function hosts()
{
return $this->hasMany(\App\Models\ModuleHost::class, 'module_datacenter_id', 'id');
}
}
+2 -1
View File
@@ -17,7 +17,8 @@ class ModuleHost extends Model
'template',
'module_datacenter_id',
'console',
'iso_path'
'iso_path',
'status',
];
public function datacenter()
+2 -1
View File
@@ -15,7 +15,8 @@ class ModuleNetwork extends Model
'name',
'type',
'network',
'module_host_id'
'module_host_id',
'status',
];
public function host()
@@ -29,7 +29,7 @@ class SettingCachedRepository extends BaseCachedRepository implements SettingRep
return $this->settingRepository->updateOrCreateByKey($key, $data);
}
public function clearCacheByKey(string $key)
public function clearCacheByKey(string $key): void
{
$cacheKey = $this->getCacheKey("setting_getByKey:{$key}");
Cache::forget($cacheKey);
@@ -2,9 +2,12 @@
namespace App\Services\Api\Client;
use App\Mail\Customer\Welcome as WelcomeMail;
use App\Models\RefreshToken;
use App\Repositories\Api\Client\ClientRepositoryInterface;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use Tymon\JWTAuth\Facades\JWTAuth;
@@ -71,6 +74,8 @@ class ClientAuthService
$token = JWTAuth::fromUser($client);
$refreshToken = $this->createRefreshToken($client->id);
$this->sendWelcomeEmail($client);
return [
'client' => $client,
'token' => $token,
@@ -78,6 +83,24 @@ class ClientAuthService
];
}
/**
* Gửi email chào mừng cho khách hàng mới đăng ký.
*/
protected function sendWelcomeEmail($client): void
{
try {
Mail::to($client->email)->send(new WelcomeMail([
'fullname' => $client->fullname,
'email' => $client->email,
]));
} catch (\Throwable $e) {
Log::warning('Send welcome email failed: ' . $e->getMessage(), [
'customer_id' => $client->id,
'email' => $client->email,
]);
}
}
/**
* Tạo refresh token mới, lưu hash vào DB, trả về raw token.
*/
@@ -4,7 +4,9 @@ declare(strict_types=1);
namespace App\Services\VirtualMachine\Dto;
use App\Models\ModuleDatastore;
use App\Models\ModuleHost;
use App\Models\ModuleNetwork;
use App\Models\Os;
use App\Models\vCenter;
@@ -19,6 +21,8 @@ class VmInfrastructureContext
public readonly bool $isWindows,
public readonly string $vmUser,
public readonly int $vmPort,
public readonly ?ModuleDatastore $datastore = null,
public readonly ?ModuleNetwork $network = null,
) {
}
}
@@ -0,0 +1,226 @@
<?php
declare(strict_types=1);
namespace App\Services\VirtualMachine;
use App\Models\ModuleDatacenter;
use App\Models\ModuleDatastore;
use App\Models\ModuleHost;
use App\Models\ModuleNetwork;
use App\Models\vCenter;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use RuntimeException;
/**
* Single source of truth for the "active" provisioning chain:
*
* vCenter (status=1, global)
* └── ModuleDatacenter (status=1 per vCenter)
* └── ModuleHost (status=1 per datacenter)
* ├── ModuleDatastore (status=1 per host)
* └── ModuleNetwork (status=1 per host)
*
* Rules enforced here:
* - At most ONE row per parent scope may have status=1.
* - Setting status=1 on row X auto-deactivates all siblings in same parent.
* - Setting status=0 on row X cascade-deactivates ALL descendants below it.
* - resolveChain() returns the full active chain or throws if any layer is missing.
*
* Settings provider mirroring:
* - activate('vcenter', $id) also writes provider.active_vcenter_id into the
* setting table so the Settings → Provider page reflects the same value
* without owning the truth.
*/
class VmActiveProviderService
{
public const TYPE_VCENTER = 'vcenter';
public const TYPE_DATACENTER = 'datacenter';
public const TYPE_HOST = 'host';
public const TYPE_DATASTORE = 'datastore';
public const TYPE_NETWORK = 'network';
/** type → [modelClass, parentColumn|null (null = global), childTypes[]] */
private const MAP = [
self::TYPE_VCENTER => [vCenter::class, null, [self::TYPE_DATACENTER]],
self::TYPE_DATACENTER => [ModuleDatacenter::class, 'vcenter_id', [self::TYPE_HOST]],
self::TYPE_HOST => [ModuleHost::class, 'module_datacenter_id', [self::TYPE_DATASTORE, self::TYPE_NETWORK]],
self::TYPE_DATASTORE => [ModuleDatastore::class, 'module_host_id', []],
self::TYPE_NETWORK => [ModuleNetwork::class, 'module_host_id', []],
];
public function __construct(private readonly VmProviderSettingsService $providerSettings)
{
}
// ─────────────────────────────────────────────────────────────────────────
// Resolve helpers
// ─────────────────────────────────────────────────────────────────────────
public function activeVcenter(): ?vCenter
{
return vCenter::where('status', 1)->first();
}
public function activeDatacenter(int $vcenterId): ?ModuleDatacenter
{
return ModuleDatacenter::where('vcenter_id', $vcenterId)
->where('status', 1)
->first();
}
public function activeHost(int $datacenterId): ?ModuleHost
{
return ModuleHost::where('module_datacenter_id', $datacenterId)
->where('status', 1)
->first();
}
public function activeDatastore(int $hostId): ?ModuleDatastore
{
return ModuleDatastore::where('module_host_id', $hostId)
->where('status', 1)
->first();
}
public function activeNetwork(int $hostId): ?ModuleNetwork
{
return ModuleNetwork::where('module_host_id', $hostId)
->where('status', 1)
->first();
}
/**
* Return the full active chain. Throws if any layer has no active row.
*
* @return array{vcenter:vCenter,datacenter:ModuleDatacenter,host:ModuleHost,datastore:ModuleDatastore,network:ModuleNetwork}
*/
public function resolveChain(): array
{
$vcenter = $this->activeVcenter();
if (!$vcenter) {
throw new RuntimeException('Không có vCenter nào đang active. Vui lòng kích hoạt 1 vCenter trong admin.');
}
$datacenter = $this->activeDatacenter($vcenter->id);
if (!$datacenter) {
throw new RuntimeException("vCenter active id={$vcenter->id} chưa có Datacenter nào active.");
}
$host = $this->activeHost($datacenter->id);
if (!$host) {
throw new RuntimeException("Datacenter active id={$datacenter->id} chưa có Host nào active.");
}
$datastore = $this->activeDatastore($host->id);
if (!$datastore) {
throw new RuntimeException("Host active id={$host->id} chưa có Datastore nào active.");
}
$network = $this->activeNetwork($host->id);
if (!$network) {
throw new RuntimeException("Host active id={$host->id} chưa có Network nào active.");
}
return compact('vcenter', 'datacenter', 'host', 'datastore', 'network');
}
// ─────────────────────────────────────────────────────────────────────────
// Mutators
// ─────────────────────────────────────────────────────────────────────────
/**
* Set this row active, deactivating all siblings in the same parent scope.
*/
public function activate(string $type, int $id): Model
{
[$modelClass, $parentColumn] = $this->mapOrFail($type);
return DB::transaction(function () use ($modelClass, $parentColumn, $type, $id) {
/** @var Model $row */
$row = $modelClass::query()->lockForUpdate()->findOrFail($id);
// Deactivate siblings under same parent (or all rows if global scope)
$siblings = $modelClass::query()->where('id', '!=', $row->getKey());
if ($parentColumn) {
$siblings->where($parentColumn, $row->{$parentColumn});
}
$siblings->update(['status' => 0]);
$row->status = 1;
$row->save();
if ($type === self::TYPE_VCENTER) {
$this->providerSettings->setActiveVcenterId((int) $row->getKey());
}
return $row->refresh();
});
}
/**
* Set this row inactive AND cascade-deactivate every descendant beneath it.
*/
public function deactivate(string $type, int $id): Model
{
[$modelClass] = $this->mapOrFail($type);
return DB::transaction(function () use ($modelClass, $type, $id) {
/** @var Model $row */
$row = $modelClass::query()->lockForUpdate()->findOrFail($id);
$row->status = 0;
$row->save();
$this->cascadeDeactivate($type, (int) $row->getKey());
if ($type === self::TYPE_VCENTER) {
$this->providerSettings->setActiveVcenterId(null);
}
return $row->refresh();
});
}
/**
* Recursively set status=0 on every descendant of $parentId under $type.
*/
private function cascadeDeactivate(string $type, int $parentId): void
{
$childTypes = self::MAP[$type][2] ?? [];
foreach ($childTypes as $childType) {
[$childModel, $childParentColumn] = $this->mapOrFail($childType);
if (!$childParentColumn) {
continue;
}
$childIds = $childModel::query()
->where($childParentColumn, $parentId)
->pluck('id')
->all();
if (!$childIds) {
continue;
}
$childModel::query()->whereIn('id', $childIds)->update(['status' => 0]);
foreach ($childIds as $childId) {
$this->cascadeDeactivate($childType, (int) $childId);
}
}
}
/**
* @return array{0:class-string<Model>,1:?string,2:string[]}
*/
private function mapOrFail(string $type): array
{
if (!isset(self::MAP[$type])) {
throw new RuntimeException("Unknown infra type: {$type}");
}
return self::MAP[$type];
}
}
@@ -4,22 +4,31 @@ declare(strict_types=1);
namespace App\Services\VirtualMachine;
use App\Models\ModuleHost;
use App\Models\OrderItem;
use App\Models\Os;
use App\Services\VirtualMachine\Dto\VmInfrastructureContext;
use RuntimeException;
/**
* Resolves the provisioning target by walking the ACTIVE chain only:
*
* active vCenter → active Datacenter → active Host → active Datastore + Network
*
* Product / OS bindings are now informational only — the host/datacenter/vcenter
* configured per-product no longer drive VM creation. The whole platform clones
* onto whatever single chain is currently marked status=1.
*/
class VmInfrastructureResolver
{
public function __construct(private readonly VmProvisionLogger $logger)
{
public function __construct(
private readonly VmProvisionLogger $logger,
private readonly VmActiveProviderService $activeProvider,
) {
}
public function resolve(OrderItem $item, $product): VmInfrastructureContext
{
$os = Os::with(['host.datacenter.vCenter', 'osGroup.host.datacenter.vCenter'])
->find($item->os_id);
$os = Os::with('osGroup')->find($item->os_id);
if (!$os) {
throw new RuntimeException(
@@ -27,63 +36,31 @@ class VmInfrastructureResolver
);
}
// Priority 1: host attached directly to OS
$host = $os->host;
$datacenter = $host?->datacenter;
$vcenter = $datacenter?->vCenter;
$chain = $this->activeProvider->resolveChain();
// Priority 2: host from OS group fallback
if (!$host || !$datacenter || !$vcenter) {
$groupHost = $os->osGroup?->host;
$host = $host ?? $groupHost;
$datacenter = $datacenter ?? $groupHost?->datacenter;
$vcenter = $vcenter ?? $groupHost?->datacenter?->vCenter;
if ($groupHost) {
$this->logger->step('Infrastructure fallback: osGroup host', [
'os_id' => $os->id,
'os_group_id' => $os->os_group_id,
'host_id' => $groupHost->id,
$this->logger->step('Infrastructure resolved from active chain', [
'order_item_id' => $item->id,
'vcenter_id' => $chain['vcenter']->id,
'datacenter_id' => $chain['datacenter']->id,
'host_id' => $chain['host']->id,
'datastore_id' => $chain['datastore']->id,
'network_id' => $chain['network']->id,
]);
}
}
// Priority 3: host from product (Provisioning tab)
if ((!$host || !$datacenter || !$vcenter) && $product && $product->module_host_id) {
$productHost = ModuleHost::with('datacenter.vCenter')->find($product->module_host_id);
if ($productHost) {
$host = $host ?? $productHost;
$datacenter = $datacenter ?? $productHost->datacenter;
$vcenter = $vcenter ?? $productHost->datacenter?->vCenter;
$this->logger->step('Infrastructure fallback: product host', [
'product_id' => $product->id,
'host_id' => $productHost->id,
'datacenter_id' => $productHost->datacenter?->id,
]);
}
}
if (!$vcenter || !$host || !$datacenter) {
throw new RuntimeException(
"Thiếu thông tin host/datacenter/vcenter từ sản phẩm ID #" . ($product->id ?? 'unknown') . ". "
. "Vui lòng cấu hình vCenter trong tab Provisioning của sản phẩm, "
. "hoặc gán host cho OS group của OS được chọn."
);
}
$osGroupName = $os->osGroup?->name ?? '';
$isWindows = str_contains(strtolower($osGroupName), 'windows');
return new VmInfrastructureContext(
os: $os,
host: $host,
datacenter: $datacenter,
vcenter: $vcenter,
host: $chain['host'],
datacenter: $chain['datacenter'],
vcenter: $chain['vcenter'],
template: $os->template_id,
isWindows: $isWindows,
vmUser: $isWindows ? 'administrator' : 'root',
vmPort: $isWindows ? 3389 : 22,
datastore: $chain['datastore'],
network: $chain['network'],
);
}
}
@@ -0,0 +1,92 @@
<?php
declare(strict_types=1);
namespace App\Services\VirtualMachine;
use App\Models\IpPool;
use App\Models\ModuleIpAddress;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
/**
* Reserves and releases IPs from ip_pools / module_ip_addresses with row-level locking.
*
* Extracted so CreateVirtualMachineJob can pick an IP BEFORE cloning the VM
* (the IP is baked into the VM name), and AssignIpToGuestJob can later use
* the same reservation instead of picking its own.
*/
class VmIpReservationService
{
/**
* @return array{reservation:ModuleIpAddress,ip_pool_id:int,gateway:string,cidr:int}|null
*/
public function reserve(int $networkId, int $hostId, string $dns): ?array
{
return DB::transaction(function () use ($networkId, $hostId, $dns) {
$usedIps = ModuleIpAddress::where('module_network_id', $networkId)
->lockForUpdate()
->pluck('ip')
->toArray();
$ipPool = IpPool::where('module_network_id', $networkId)
->where('status', 'free')
->whereNotNull('gateway')
->where('gateway', '!=', '')
->whereNotIn('ip_address', $usedIps)
->lockForUpdate()
->inRandomOrder()
->first();
if (!$ipPool) {
return null;
}
$ipPool->update(['status' => 'reserved']);
$cidr = (int) ($ipPool->cidr ?: 24);
$gateway = (string) ($ipPool->gateway ?? '');
$netmask = $this->cidrToNetmask($cidr);
$reservation = ModuleIpAddress::create([
'vm_name' => '__pending__',
'module_host_id' => $hostId,
'module_network_id' => $networkId,
'ip' => $ipPool->ip_address,
'gateway' => $gateway,
'netmask' => $netmask,
'dns' => $dns,
'status' => 0,
]);
return [
'reservation' => $reservation,
'ip_pool_id' => $ipPool->id,
'gateway' => $gateway,
'cidr' => $cidr,
];
});
}
public function release(ModuleIpAddress $reservation, ?int $ipPoolId): void
{
try {
$reservation->delete();
if ($ipPoolId) {
IpPool::where('id', $ipPoolId)->update(['status' => 'free']);
}
Log::info('VmIpReservationService: IP released', ['ip' => $reservation->ip]);
} catch (\Throwable $e) {
Log::warning('VmIpReservationService: release failed', [
'ip' => $reservation->ip,
'error' => $e->getMessage(),
]);
}
}
public function cidrToNetmask(int $cidr): string
{
$cidr = max(0, min(32, $cidr));
return long2ip(-1 << (32 - $cidr));
}
}
@@ -0,0 +1,87 @@
<?php
declare(strict_types=1);
namespace App\Services\VirtualMachine;
use App\Repositories\Setting\SettingRepositoryInterface;
/**
* Reads / writes the provisioning "provider" config stored in setting table
* under the single key `provider`.
*
* Shape:
* {
* "active_vcenter_id": 1, // mirror of module_vcenter.status=1
* "default_folder": "group-v1008",
* "default_resource_pool": "resgroup-3002",
* "default_datastore": "datastore-3004", // fallback if active host has no active datastore
* "name": "Acme Cloud",
* "region": "HCM",
* "tags": ["prod"]
* }
*
* The active_vcenter_id field is mirrored from module_vcenter — module_vcenter
* remains the source of truth, this is just a convenience view for the UI.
*/
class VmProviderSettingsService
{
public const KEY = 'provider';
public const DEFAULTS = [
'active_vcenter_id' => null,
'default_folder' => 'group-v1008',
'default_resource_pool' => 'resgroup-3002',
'default_datastore' => 'datastore-3004',
'name' => null,
'region' => null,
'tags' => [],
];
public function __construct(private readonly SettingRepositoryInterface $settings)
{
}
/** @return array<string,mixed> */
public function all(): array
{
$raw = $this->settings->getByKey(self::KEY);
$data = is_string($raw) ? json_decode($raw, true) : [];
return array_merge(self::DEFAULTS, is_array($data) ? $data : []);
}
public function get(string $field, mixed $default = null): mixed
{
return $this->all()[$field] ?? $default;
}
/** Merge-write a subset of fields. */
public function merge(array $patch): array
{
$merged = array_merge($this->all(), $patch);
$this->settings->updateOrCreateByKey(self::KEY, [
'value' => json_encode($merged, JSON_UNESCAPED_UNICODE),
]);
return $merged;
}
public function setActiveVcenterId(?int $id): void
{
$this->merge(['active_vcenter_id' => $id]);
}
public function defaultFolder(): string
{
return (string) $this->get('default_folder', self::DEFAULTS['default_folder']);
}
public function defaultResourcePool(): string
{
return (string) $this->get('default_resource_pool', self::DEFAULTS['default_resource_pool']);
}
public function defaultDatastore(): string
{
return (string) $this->get('default_datastore', self::DEFAULTS['default_datastore']);
}
}
@@ -0,0 +1,104 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
/**
* Adds `status` (tinyint, 0=inactive / 1=active) to module_host and module_network,
* then backfills 1-active-per-parent across the full provisioning chain:
*
* module_vcenter (1 active globally)
* module_datacenter (1 active per vCenter)
* module_host (1 active per datacenter)
* module_datastore (1 active per host)
* module_network (1 active per host)
*
* Picks the smallest-id row per parent as the active one when none exists yet,
* matching the deterministic "first row wins" rule confirmed with the product owner.
*/
return new class extends Migration {
public function up(): void
{
if (!Schema::hasColumn('module_host', 'status')) {
Schema::table('module_host', function (Blueprint $table) {
$table->tinyInteger('status')->default(0)->after('iso_path');
});
}
if (!Schema::hasColumn('module_network', 'status')) {
Schema::table('module_network', function (Blueprint $table) {
$table->tinyInteger('status')->default(0)->after('module_host_id');
});
}
$this->backfillOneActiveGlobal('module_vcenter');
$this->backfillOneActivePerParent('module_datacenter', 'vcenter_id');
$this->backfillOneActivePerParent('module_host', 'module_datacenter_id');
$this->backfillOneActivePerParent('module_datastore', 'module_host_id');
$this->backfillOneActivePerParent('module_network', 'module_host_id');
}
public function down(): void
{
if (Schema::hasColumn('module_network', 'status')) {
Schema::table('module_network', function (Blueprint $table) {
$table->dropColumn('status');
});
}
if (Schema::hasColumn('module_host', 'status')) {
Schema::table('module_host', function (Blueprint $table) {
$table->dropColumn('status');
});
}
}
/** Keep one global active row (smallest id wins), zero out the rest. */
private function backfillOneActiveGlobal(string $table): void
{
$activeId = DB::table($table)->where('status', 1)->orderBy('id')->value('id')
?? DB::table($table)->orderBy('id')->value('id');
if (!$activeId) {
return;
}
DB::table($table)->where('id', '!=', $activeId)->update(['status' => 0]);
DB::table($table)->where('id', $activeId)->update(['status' => 1]);
}
/** For each parent value, keep one child active (smallest id wins). */
private function backfillOneActivePerParent(string $table, string $parentColumn): void
{
$parents = DB::table($table)->select($parentColumn)->distinct()->pluck($parentColumn);
foreach ($parents as $parentId) {
if ($parentId === null) {
continue;
}
$activeId = DB::table($table)
->where($parentColumn, $parentId)
->where('status', 1)
->orderBy('id')
->value('id');
$activeId = $activeId ?? DB::table($table)
->where($parentColumn, $parentId)
->orderBy('id')
->value('id');
if (!$activeId) {
continue;
}
DB::table($table)
->where($parentColumn, $parentId)
->where('id', '!=', $activeId)
->update(['status' => 0]);
DB::table($table)->where('id', $activeId)->update(['status' => 1]);
}
}
};
@@ -0,0 +1,67 @@
<!DOCTYPE html>
<html lang="vi">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chào mừng đến với {{ $appName }}</title>
<style>
* { box-sizing: border-box; }
body { font-family: 'Segoe UI', Arial, sans-serif; background: #f0f4f8; margin: 0; padding: 0; }
.wrapper { max-width: 620px; margin: 30px auto; }
.card { background: #fff; border-radius: 12px; overflow: hidden; box-shadow: 0 4px 20px rgba(0,0,0,0.08); }
.header { background: linear-gradient(135deg, #16a34a 0%, #15803d 100%); padding: 36px 40px; text-align: center; }
.header .icon { font-size: 48px; margin-bottom: 12px; }
.header h1 { color: #fff; margin: 0 0 6px; font-size: 24px; font-weight: 700; }
.header p { color: #bbf7d0; margin: 0; font-size: 14px; }
.body { padding: 36px 40px; color: #374151; line-height: 1.6; }
.footer { text-align: center; padding: 20px 40px; border-top: 1px solid #f3f4f6; font-size: 12px; color: #9ca3af; }
.feature-list { margin: 0 0 18px 18px; padding: 0; color: #374151; line-height: 1.8; }
.cta-wrap { text-align: center; margin: 28px 0; }
.cta-btn { display: inline-block; background-color: #16a34a; color: #ffffff !important; text-decoration: none; padding: 14px 28px; border-radius: 8px; font-size: 15px; font-weight: 600; }
</style>
</head>
<body>
<div class="wrapper">
<div class="card">
<div class="header">
<div class="icon">🎉</div>
<h1>Chào mừng bạn đến với {{ $appName }}</h1>
<p>Tài khoản của bạn đã được tạo thành công</p>
</div>
<div class="body">
<p style="margin:0 0 12px 0;">
Xin chào <strong>{{ $fullName }}</strong>,
</p>
<p style="margin:0 0 18px 0;">
Chào mừng bạn đến với <strong>{{ $appName }}</strong>!
Tài khoản của bạn đã được khởi tạo thành công và sẵn sàng sử dụng.
</p>
<p style="margin:0 0 12px 0;">
Tại <span>{{ $appName }}</span>, bạn có thể:
</p>
<ul class="feature-list">
<li>Khám phá và đăng ký các dịch vụ Cloud VPS, Hosting, Tên miền</li>
<li>Đặt hàng nhanh chóng, thanh toán an toàn</li>
<li>Theo dõi lịch sử đơn hàng và quản lý dịch vụ dễ dàng</li>
<li>Nhận ưu đãi và khuyến mãi độc quyền dành cho khách hàng</li>
</ul>
<div class="cta-wrap">
<a href="{{ $loginUrl }}" class="cta-btn">Đăng nhập ngay</a>
</div>
<p style="margin:0;color:#6b7280;">
Cảm ơn bạn đã tin tưởng và đồng hành cùng <span>{{ $appName }}</span>.
Chúc bạn có những trải nghiệm tuyệt vời!
</p>
</div>
<div class="footer">
Email này được gửi tự động. Vui lòng không trả lời email này.<br>
Cần hỗ trợ? Liên hệ <strong>{{ $supportEmail }}</strong> hoặc <strong>{{ $supportPhone }}</strong>
</div>
</div>
</div>
</body>
</html>
+24
View File
@@ -382,12 +382,36 @@ Route::prefix('v1')
Route::post('media/upload', [AdminSettingController::class, 'uploadMedia']);
// Provider (active vCenter chain + provider settings — module: module-vcenter)
Route::prefix('provider')
->group(function () {
Route::get('active-chain', [\App\Http\Controllers\Api\Admin\Provider\ActiveInfraController::class, 'chain'])
->middleware('perm:module-vcenter.view');
Route::post('{type}/{id}/activate', [\App\Http\Controllers\Api\Admin\Provider\ActiveInfraController::class, 'activate'])
->whereIn('type', ['vcenter', 'datacenter', 'host', 'datastore', 'network'])
->middleware('perm:module-vcenter.edit');
Route::post('{type}/{id}/deactivate', [\App\Http\Controllers\Api\Admin\Provider\ActiveInfraController::class, 'deactivate'])
->whereIn('type', ['vcenter', 'datacenter', 'host', 'datastore', 'network'])
->middleware('perm:module-vcenter.edit');
Route::get('settings', [\App\Http\Controllers\Api\Admin\Provider\ProviderSettingController::class, 'index'])
->middleware('perm:module-vcenter.view');
Route::put('settings', [\App\Http\Controllers\Api\Admin\Provider\ProviderSettingController::class, 'update'])
->middleware('perm:module-vcenter.edit');
});
// Dashboard Stats
Route::prefix('dashboard')
->controller(\App\Http\Controllers\Api\Admin\Dashboard\DashboardController::class)
->group(function () {
Route::get('stats', 'stats');
Route::get('monthly-stats', 'monthlyStats');
Route::get('daily-stats', 'dailyStats');
Route::get('recent-orders', 'recentOrders');
Route::get('recent-deposits', 'recentDeposits');
Route::get('top-services', 'topServices');
Route::get('expiring-services', 'expiringServices');
Route::get('activities', 'activities');
Route::get('latest-customers', 'latestCustomers');
});