- ProvisionDnsDomainJob/SuspendDnsDomainJob: tạo domain admin lên Plesk, huỷ = suspend (is_created=3) - Khách xem zone DNS được gán ở /dich-vu/domain, quản lý bản ghi A/AAAA/CNAME/TXT/MX/SRV - send-mail hoá đơn đẩy SendInvoiceMailJob, preview + sửa nội dung, ghi mail.order_id - invoice_attachments.type (payment_proof/deployment) - CacheService xoá theo SCAN pattern, Redis không persistent mặc định - Backfill transactions.public_code Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
480 lines
17 KiB
PHP
480 lines
17 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\Client\Service;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Resources\Api\Service\VpsServiceCollection;
|
|
use App\Http\Resources\Api\Service\Order\InvoiceSummaryResource;
|
|
use App\Http\Resources\Api\Client\Service\ServiceClientDetailResource;
|
|
use App\Http\Services\vCenter;
|
|
use App\Models\DnsDomain;
|
|
use App\Models\Order;
|
|
use App\Models\Service;
|
|
use App\Repositories\Api\ServiceClient\ServiceClientRepository;
|
|
use App\Services\ActivityLogger;
|
|
use App\Services\Api\Order\OrderQueryService;
|
|
use App\Services\Api\ServiceClient\ServiceClientService;
|
|
use App\Services\VirtualMachine\Rebuild\VmRebuildService;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Throwable;
|
|
|
|
class ServiceController extends Controller
|
|
{
|
|
protected $serviceClientService;
|
|
protected $serviceClientRepository;
|
|
protected ActivityLogger $activityLogger;
|
|
|
|
public function __construct(
|
|
ServiceClientService $serviceClientService,
|
|
ServiceClientRepository $serviceClientRepository,
|
|
ActivityLogger $activityLogger,
|
|
) {
|
|
$this->serviceClientService = $serviceClientService;
|
|
$this->serviceClientRepository = $serviceClientRepository;
|
|
$this->activityLogger = $activityLogger;
|
|
}
|
|
|
|
public function indexVps(Request $request): JsonResponse
|
|
{
|
|
$perPage = min((int) $request->get('per_page', 15), 100);
|
|
$customer = $request->user();
|
|
|
|
$services = $this->serviceClientService->getActiveServices($customer->id, 'vps', $perPage);
|
|
|
|
return (new VpsServiceCollection($services))
|
|
->additional([
|
|
'error' => false,
|
|
'message' => 'Lấy dữ liệu thành công',
|
|
'page_type' => 'service_vps',
|
|
])
|
|
->response();
|
|
}
|
|
|
|
public function indexHosting(Request $request): JsonResponse
|
|
{
|
|
$perPage = min((int) $request->get('per_page', 15), 100);
|
|
$customer = $request->user();
|
|
|
|
$services = $this->serviceClientService->getActiveServices($customer->id, 'hosting', $perPage);
|
|
|
|
return (new VpsServiceCollection($services))
|
|
->additional([
|
|
'error' => false,
|
|
'message' => 'Lấy dữ liệu thành công',
|
|
'page_type' => 'service_hosting',
|
|
])
|
|
->response();
|
|
}
|
|
|
|
public function indexDomain(Request $request): JsonResponse
|
|
{
|
|
$customerId = $request->user()->id;
|
|
|
|
$zones = DnsDomain::query()
|
|
->whereHas('customer', fn ($query) => $query->where('customers.id', $customerId))
|
|
->get(['id', 'domain_name', 'is_created'])
|
|
->keyBy(fn (DnsDomain $zone) => mb_strtolower(trim($zone->domain_name)));
|
|
|
|
$zoneFields = fn (?DnsDomain $zone) => [
|
|
'dns_domain_id' => $zone && !$this->isCancelledZone($zone) ? $zone->id : null,
|
|
'cancelled' => $zone !== null && $this->isCancelledZone($zone),
|
|
];
|
|
|
|
$items = Service::query()
|
|
->where('customer_id', $customerId)
|
|
->where('type', 'domain')
|
|
->orderByDesc('id')
|
|
->get(['uuid', 'server_name', 'status', 'started_at', 'expires_at'])
|
|
->map(fn (Service $service) => [
|
|
'domain_name' => $service->server_name,
|
|
'service_uuid' => $service->uuid,
|
|
'status' => $service->status,
|
|
'started_at' => $service->started_at?->toISOString(),
|
|
'expires_at' => $service->expires_at?->toISOString(),
|
|
...$zoneFields($zones->pull(mb_strtolower(trim((string) $service->server_name)))),
|
|
])
|
|
->concat($zones->values()->map(fn (DnsDomain $zone) => [
|
|
'domain_name' => $zone->domain_name,
|
|
'service_uuid' => null,
|
|
'status' => null,
|
|
'started_at' => null,
|
|
'expires_at' => null,
|
|
...$zoneFields($zone),
|
|
]))
|
|
->values();
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'message' => 'Lấy dữ liệu thành công',
|
|
'page_type' => 'service_domain',
|
|
'data' => ['items' => $items],
|
|
]);
|
|
}
|
|
|
|
private function isCancelledZone(DnsDomain $zone): bool
|
|
{
|
|
return (int) $zone->is_created === DnsDomain::STATUS_CANCELLED;
|
|
}
|
|
|
|
public function overview(Request $request, OrderQueryService $orderQueryService): JsonResponse
|
|
{
|
|
$customerId = (int) $request->user()->id;
|
|
|
|
$services = $this->serviceClientService->getActiveServices($customerId, null, 5);
|
|
$invoices = $orderQueryService->getCustomerInvoices($customerId)->take(5);
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'message' => 'Lấy dữ liệu thành công',
|
|
'data' => [
|
|
'services' => (new VpsServiceCollection($services))->collection,
|
|
'invoices' => InvoiceSummaryResource::collection($invoices),
|
|
],
|
|
]);
|
|
}
|
|
|
|
public function indexWebDesign(Request $request): JsonResponse
|
|
{
|
|
$perPage = min((int) $request->get('per_page', 15), 100);
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'message' => 'Lấy dữ liệu thành công',
|
|
'page_type' => 'service_web_design',
|
|
'data' => [
|
|
'items' => [],
|
|
'pagination' => [
|
|
'total' => 0,
|
|
'per_page' => $perPage,
|
|
'current_page' => 1,
|
|
'last_page' => 1,
|
|
],
|
|
],
|
|
]);
|
|
}
|
|
|
|
public function getDetail(Request $request, string $uuid)
|
|
{
|
|
$service = $this->serviceClientService->getDetailService($uuid, $request->user()->id);
|
|
|
|
if (!$service) {
|
|
return response()->json(
|
|
[
|
|
'error' => true,
|
|
'message' => 'Không tìm thấy dịch vụ',
|
|
],
|
|
404,
|
|
);
|
|
}
|
|
|
|
return (new ServiceClientDetailResource($service))->additional([
|
|
'error' => false,
|
|
'message' => 'Lấy dữ liệu thành công',
|
|
'page_type' => 'service',
|
|
]);
|
|
}
|
|
|
|
public function getConsoleTicket(Request $request, string $uuid, vCenter $serviceVCenter)
|
|
{
|
|
$customer = $request->user();
|
|
|
|
$service = Service::with(['vmware.vCenter', 'orderItem.order.vmWare.vCenter'])
|
|
->where('uuid', $uuid)
|
|
->where('customer_id', $customer->id)
|
|
->first();
|
|
|
|
if (!$service) {
|
|
return response()->json(['error' => true, 'message' => 'Không tìm thấy dịch vụ'], 404);
|
|
}
|
|
|
|
$vm = $service->resolveVmware();
|
|
|
|
if (!$vm) {
|
|
return response()->json(['error' => true, 'message' => 'Dịch vụ chưa được cấp phát VM'], 404);
|
|
}
|
|
|
|
$vcenter = $vm->vCenter;
|
|
|
|
if (!$vcenter) {
|
|
return response()->json(['error' => true, 'message' => 'Không tìm thấy thông tin vCenter'], 404);
|
|
}
|
|
|
|
if (!$serviceVCenter->loginServer($vcenter)) {
|
|
return response()->json(['error' => true, 'message' => 'Không thể đăng nhập vào vCenter']);
|
|
}
|
|
|
|
$consoleTicket = $serviceVCenter->GetConsoleTickets($vm->vmware_id);
|
|
|
|
if ($consoleTicket === 'Login_Again') {
|
|
if ($serviceVCenter->loginServer($vcenter)) {
|
|
$consoleTicket = $serviceVCenter->GetConsoleTickets($vm->vmware_id);
|
|
} else {
|
|
return response()->json(['error' => true, 'message' => 'Không thể đăng nhập lại vào vCenter']);
|
|
}
|
|
}
|
|
|
|
if (is_string($consoleTicket)) {
|
|
return response()->json(['error' => true, 'message' => "Failed to get console ticket: $consoleTicket"]);
|
|
}
|
|
|
|
if ($consoleTicket['error']) {
|
|
return response()->json(['error' => true, 'message' => $consoleTicket['data']]);
|
|
}
|
|
|
|
$this->activityLogger->log(
|
|
event: 'console_access',
|
|
logName: 'VM',
|
|
description: "Customer {$customer->email} truy cập console VM: {$vm->name} (service: {$uuid})",
|
|
subject: $vm,
|
|
newValues: ['service_uuid' => $uuid, 'vm_name' => $vm->name],
|
|
);
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'ticket' => $consoleTicket['data']['ticket'],
|
|
'host' => $consoleTicket['data']['host'],
|
|
]);
|
|
}
|
|
|
|
public function getTicketByOrderNum(Request $request, vCenter $serviceVCenter): JsonResponse
|
|
{
|
|
$orderNum = (string) $request->query('order_num', '');
|
|
if ($orderNum === '') {
|
|
return response()->json(['error' => true, 'message' => 'Thiếu order_num'], 422);
|
|
}
|
|
|
|
$customer = $request->user();
|
|
$order = Order::with(['vmWare.vCenter'])
|
|
->where('customer_id', $customer->id)
|
|
->where('order_num', $orderNum)
|
|
->first();
|
|
|
|
if (!$order) {
|
|
return response()->json(['error' => true, 'message' => 'Không tìm thấy đơn hàng'], 404);
|
|
}
|
|
|
|
$vm = $order->vmWare->first();
|
|
if (!$vm) {
|
|
return response()->json(['error' => true, 'message' => 'Đơn hàng chưa có VM'], 404);
|
|
}
|
|
|
|
$vcenter = $vm->vCenter;
|
|
if (!$vcenter) {
|
|
return response()->json(['error' => true, 'message' => 'Không tìm thấy thông tin vCenter'], 404);
|
|
}
|
|
|
|
if (!$serviceVCenter->loginServer($vcenter)) {
|
|
return response()->json(['error' => true, 'message' => 'Không thể đăng nhập vào vCenter']);
|
|
}
|
|
|
|
$consoleTicket = $serviceVCenter->GetConsoleTickets($vm->vmware_id);
|
|
|
|
if ($consoleTicket === 'Login_Again') {
|
|
if ($serviceVCenter->loginServer($vcenter)) {
|
|
$consoleTicket = $serviceVCenter->GetConsoleTickets($vm->vmware_id);
|
|
} else {
|
|
return response()->json(['error' => true, 'message' => 'Không thể đăng nhập lại vào vCenter']);
|
|
}
|
|
}
|
|
|
|
if (is_string($consoleTicket)) {
|
|
return response()->json(['error' => true, 'message' => "Failed to get console ticket: $consoleTicket"]);
|
|
}
|
|
|
|
if ($consoleTicket['error']) {
|
|
return response()->json(['error' => true, 'message' => $consoleTicket['data']]);
|
|
}
|
|
|
|
$this->activityLogger->log(
|
|
event: 'console_access',
|
|
logName: 'VM',
|
|
description: "Customer {$customer->email} truy cập console VM: {$vm->name} (order: {$order->order_num})",
|
|
subject: $vm,
|
|
newValues: ['order_num' => $order->order_num, 'vm_name' => $vm->name],
|
|
);
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'ticket' => $consoleTicket['data']['ticket'],
|
|
'host' => $consoleTicket['data']['host'],
|
|
'order_num' => $order->order_num,
|
|
'vm_id' => $vm->id,
|
|
]);
|
|
}
|
|
|
|
public function getAvailableOs(Request $request, string $uuid): JsonResponse
|
|
{
|
|
$customer = $request->user();
|
|
|
|
$service = Service::with([
|
|
'product.osVersions' => fn($q) => $q->where('status', 1)->with('osGroup'),
|
|
])
|
|
->where('uuid', $uuid)
|
|
->where('customer_id', $customer->id)
|
|
->first();
|
|
|
|
if (!$service) {
|
|
return response()->json(['success' => false, 'message' => 'Không tìm thấy dịch vụ'], 404);
|
|
}
|
|
|
|
if (!in_array($service->status, ['active', 'suspended'], true)) {
|
|
return response()->json(
|
|
['success' => false, 'message' => 'Dịch vụ không ở trạng thái hợp lệ để xem OS'],
|
|
422,
|
|
);
|
|
}
|
|
|
|
if (!$service->product) {
|
|
return response()->json(['success' => false, 'message' => 'Không tìm thấy product của dịch vụ'], 404);
|
|
}
|
|
|
|
$grouped = $service->product->osVersions
|
|
->groupBy(fn($os) => $os->osGroup?->id ?? 0)
|
|
->map(
|
|
fn($items) => [
|
|
'group' => [
|
|
'id' => $items->first()->osGroup?->id,
|
|
'name' => $items->first()->osGroup?->name,
|
|
'thumbnail' => $items->first()->osGroup?->thumbnail,
|
|
],
|
|
'items' => $items
|
|
->map(
|
|
fn($os) => [
|
|
'id' => $os->id,
|
|
'version' => $os->version,
|
|
'template_id' => $os->template_id,
|
|
],
|
|
)
|
|
->values(),
|
|
],
|
|
)
|
|
->values();
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'data' => $grouped,
|
|
]);
|
|
}
|
|
|
|
public function updateAutoRenew(Request $request, string $uuid): JsonResponse
|
|
{
|
|
$validated = $request->validate(['auto_renew' => ['required', 'boolean']]);
|
|
|
|
$service = Service::query()
|
|
->where('uuid', $uuid)
|
|
->where('customer_id', $request->user()->id)
|
|
->first();
|
|
|
|
if (! $service) {
|
|
return response()->json(['error' => true, 'message' => 'Không tìm thấy dịch vụ'], 404);
|
|
}
|
|
|
|
$service->update(['auto_renew' => (bool) $validated['auto_renew']]);
|
|
|
|
$this->activityLogger->log(
|
|
event: 'updated',
|
|
logName: 'Service',
|
|
description: "Customer {$request->user()->email} " . ($validated['auto_renew'] ? 'bật' : 'tắt') . " tự động gia hạn dịch vụ #{$service->id}",
|
|
subject: $service,
|
|
newValues: ['auto_renew' => (bool) $validated['auto_renew']],
|
|
);
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'message' => 'Cập nhật tự động gia hạn thành công.',
|
|
'data' => [
|
|
'uuid' => $service->uuid,
|
|
'id' => $service->id,
|
|
'auto_renew' => (bool) $service->auto_renew,
|
|
],
|
|
]);
|
|
}
|
|
|
|
public function rebuild(Request $request, string $uuid, VmRebuildService $rebuildService): JsonResponse
|
|
{
|
|
$customer = $request->user();
|
|
$payload = $request->validate([
|
|
'os_id' => ['required', 'integer', 'exists:os,id'],
|
|
'password' => ['nullable', 'string', 'min:8', 'max:255'],
|
|
]);
|
|
|
|
$service = Service::with(['product.osVersions', 'vmware'])
|
|
->where('uuid', $uuid)
|
|
->where('customer_id', $customer->id)
|
|
->first();
|
|
|
|
if (!$service) {
|
|
return response()->json(['success' => false, 'message' => 'Không tìm thấy dịch vụ'], 404);
|
|
}
|
|
|
|
if ($service->status === 'suspended') {
|
|
return response()->json(
|
|
['success' => false, 'message' => 'Dịch vụ đang bị tạm ngưng, không thể rebuild'],
|
|
422,
|
|
);
|
|
}
|
|
|
|
if (!$service->isActive()) {
|
|
return response()->json(['success' => false, 'message' => 'Dịch vụ không ở trạng thái active'], 422);
|
|
}
|
|
|
|
$allowedOsIds =
|
|
$service->product?->osVersions()->where('status', 1)->pluck('os.id')->map('intval')->toArray() ?? [];
|
|
|
|
if (!in_array((int) $payload['os_id'], $allowedOsIds, true)) {
|
|
return response()->json(['success' => false, 'message' => 'OS không hợp lệ cho sản phẩm hiện tại'], 422);
|
|
}
|
|
|
|
$vm = $service->vmware;
|
|
if (!$vm) {
|
|
return response()->json(['success' => false, 'message' => 'Dịch vụ chưa được cấp phát VM'], 404);
|
|
}
|
|
|
|
try {
|
|
$log = $rebuildService->dispatchRebuild(
|
|
moduleVmwareId: (int) $vm->id,
|
|
newOsId: (int) $payload['os_id'],
|
|
initiatorCustomerId: (int) $customer->id,
|
|
customPassword: !empty($payload['password']) ? (string) $payload['password'] : null,
|
|
initiator: 'client',
|
|
note: 'Client rebuild via service UUID: ' . $uuid,
|
|
);
|
|
} catch (Throwable $e) {
|
|
Log::error('Client rebuild dispatch failed', [
|
|
'service_uuid' => $uuid,
|
|
'module_vmware_id' => $vm->id,
|
|
'error' => $e->getMessage(),
|
|
]);
|
|
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Không thể gửi yêu cầu rebuild: ' . $e->getMessage(),
|
|
], 400);
|
|
}
|
|
|
|
$this->activityLogger->log(
|
|
event: 'rebuild',
|
|
logName: 'VM',
|
|
description: "Customer {$customer->email} rebuild VM: {$vm->name} với OS ID: {$payload['os_id']} qua service UUID: {$uuid}",
|
|
subject: $vm,
|
|
newValues: [
|
|
'os_id' => (int) $payload['os_id'],
|
|
'service_uuid' => $uuid,
|
|
'reinstall_log_id' => $log->id,
|
|
'custom_password' => !empty($payload['password']),
|
|
],
|
|
);
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Đã gửi yêu cầu rebuild VM',
|
|
'data' => [
|
|
'reinstall_log_id' => $log->id,
|
|
'status' => $log->status,
|
|
],
|
|
]);
|
|
}
|
|
}
|