feat: create PostCategoryFactory for generating post category data feat: create PostFactory for generating post data feat: add retry_requested_at column to services table feat: add vcenter provisioning fields to products table feat: add module_host_id to os table feat: create email_notification_templates table for email templates feat: add scenario and format fields to template table feat: add admin fields to transactions table fix: update ticket table columns and types feat: add uuid to ticket table and backfill existing records fix: make ticket_reply user_id nullable feat: create notifications table for user notifications fix: make ticket uuid nullable feat: create post_categories table for blog categories feat: create posts table for blog posts feat: add invoice email template for PDF generation
305 lines
11 KiB
PHP
305 lines
11 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\Client\Service;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Resources\Api\Client\Service\ServiceClientDetailResource;
|
|
use \App\Http\Resources\Api\Client\Service\ServiceClientResource;
|
|
use App\Http\Services\vCenter;
|
|
use App\Models\Order;
|
|
use App\Models\Service;
|
|
use App\Repositories\Api\ServiceClient\ServiceClientRepository;
|
|
use App\Services\ActivityLogger;
|
|
use App\Services\Api\ServiceClient\ServiceClientService;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
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 getService(Request $request)
|
|
{
|
|
$limit = $request->get('limit', 10);
|
|
$services = $this->serviceClientService->getActiveServices($limit);
|
|
|
|
return ServiceClientResource::collection($services)->additional([
|
|
'error' => false,
|
|
'message' => 'Lấy dữ liệu thành công',
|
|
'page_type' => 'service',
|
|
]);
|
|
}
|
|
|
|
|
|
public function getDetail(Request $request, string $uuid)
|
|
{
|
|
$service = $this->serviceClientService->getDetailService($uuid);
|
|
|
|
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(['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->orderItem?->order?->vmWare?->first();
|
|
|
|
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']]);
|
|
}
|
|
|
|
$ticket = basename(parse_url((string) $consoleTicket['data'], PHP_URL_PATH));
|
|
|
|
$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,
|
|
'message' => $ticket,
|
|
]);
|
|
}
|
|
|
|
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,
|
|
'data' => $consoleTicket['data'],
|
|
'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 rebuild(Request $request, string $uuid, vCenter $serviceVCenter): JsonResponse
|
|
{
|
|
$customer = $request->user();
|
|
$payload = $request->validate([
|
|
'os_id' => ['required', 'integer', 'exists:os,id'],
|
|
'password' => ['nullable', 'string', '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);
|
|
}
|
|
|
|
DB::table('reinstall_logs')->insert([
|
|
'product_id' => $vm->id,
|
|
'product_type' => 4,
|
|
'custome_id' => $customer->id,
|
|
'os_id' => $payload['os_id'],
|
|
'status' => 'pending',
|
|
'initiator' => 'client',
|
|
'note' => 'Client rebuild via service UUID: ' . $uuid,
|
|
'created_at' => now(),
|
|
'updated_at' => now(),
|
|
]);
|
|
|
|
$result = $serviceVCenter->reBuildVPS((string) $vm->name);
|
|
if (is_string($result) || ($result['error'] ?? false)) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => is_string($result) ? $result : ($result['message'] ?? 'Không thể rebuild VM'),
|
|
], 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' => $payload['os_id'], 'service_uuid' => $uuid],
|
|
);
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Đã gửi yêu cầu rebuild VM',
|
|
]);
|
|
}
|
|
|
|
} |