update logic
This commit is contained in:
@@ -575,6 +575,119 @@ class CustomerController extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a service for a customer. For VPS products, accepts a
|
||||
* `virtual_machine_id` from the vCenter inventory and auto-creates the
|
||||
* matching module_vmware row so `services.vm_id` can be linked.
|
||||
*/
|
||||
public function createService(string $uuid, Request $request): JsonResponse
|
||||
{
|
||||
$customer = $this->customerRepository->findBy('uuid', $uuid);
|
||||
if (!$customer) {
|
||||
return response()->json(['error' => true, 'message' => "Can't find customer"], 404);
|
||||
}
|
||||
|
||||
$data = $request->validate([
|
||||
'product_id' => 'required|integer|exists:products,id',
|
||||
'virtual_machine_id' => 'nullable|integer|exists:virtual_machines,id',
|
||||
'server_name' => 'nullable|string|max:255',
|
||||
'status' => 'nullable|in:pending,active,suspended,expired,cancelled,terminated',
|
||||
'auto_renew' => 'nullable|boolean',
|
||||
'started_at' => 'nullable|date',
|
||||
'expires_at' => 'nullable|date',
|
||||
'note' => 'nullable|string',
|
||||
]);
|
||||
|
||||
$product = \App\Models\Product::find($data['product_id']);
|
||||
if ($product->product_type === 'vps' && empty($data['virtual_machine_id'])) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => 'VPS service requires a virtual_machine_id',
|
||||
], 422);
|
||||
}
|
||||
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
|
||||
$vmId = null;
|
||||
if (!empty($data['virtual_machine_id'])) {
|
||||
$vm = \App\Models\VirtualMachines::find($data['virtual_machine_id']);
|
||||
$moduleVm = \App\Models\ModuleVmware::create([
|
||||
'customer_id' => $customer->id,
|
||||
'name' => $vm->name,
|
||||
'vmware_id' => $vm->vm_id,
|
||||
'virtual_machine_id' => $vm->id,
|
||||
'hostname' => $data['server_name'] ?? $vm->name,
|
||||
'status' => 1,
|
||||
]);
|
||||
$vmId = $moduleVm->id;
|
||||
}
|
||||
|
||||
$service = \App\Models\Service::create([
|
||||
'customer_id' => $customer->id,
|
||||
'product_id' => $data['product_id'],
|
||||
'vm_id' => $vmId,
|
||||
'server_name' => $data['server_name'] ?? null,
|
||||
'status' => $data['status'] ?? 'active',
|
||||
'auto_renew' => (bool) ($data['auto_renew'] ?? false),
|
||||
'started_at' => $data['started_at'] ?? now(),
|
||||
'expires_at' => $data['expires_at'] ?? null,
|
||||
'note' => $data['note'] ?? null,
|
||||
]);
|
||||
|
||||
DB::commit();
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => 'Service created',
|
||||
'data' => $service->load(['product']),
|
||||
]);
|
||||
} catch (\Throwable $e) {
|
||||
DB::rollBack();
|
||||
return response()->json(['error' => true, 'message' => $e->getMessage()], 500);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* List virtual machines from vCenter inventory for the VPS picker.
|
||||
* Annotates each VM with `assigned_service_id` if already linked to a service.
|
||||
*/
|
||||
public function listAvailableVms(Request $request): JsonResponse
|
||||
{
|
||||
$search = trim((string) $request->get('q', ''));
|
||||
|
||||
$assignments = DB::table('module_vmware as mv')
|
||||
->join('services as s', 's.vm_id', '=', 'mv.id')
|
||||
->whereNotNull('mv.virtual_machine_id')
|
||||
->select(
|
||||
'mv.virtual_machine_id',
|
||||
's.id as service_id',
|
||||
's.customer_id as service_customer_id',
|
||||
)
|
||||
->get()
|
||||
->keyBy('virtual_machine_id');
|
||||
|
||||
$vms = DB::table('virtual_machines')
|
||||
->select('id', 'vm_id', 'name', 'power_state', 'cpu', 'ram_mb', 'ip')
|
||||
->where('is_template', 0)
|
||||
->where('is_system', 0)
|
||||
->when($search, fn ($q) => $q->where(function ($w) use ($search) {
|
||||
$w->where('name', 'like', "%{$search}%")
|
||||
->orWhere('vm_id', 'like', "%{$search}%")
|
||||
->orWhere('ip', 'like', "%{$search}%");
|
||||
}))
|
||||
->orderBy('name')
|
||||
->limit(200)
|
||||
->get()
|
||||
->map(function ($vm) use ($assignments) {
|
||||
$a = $assignments->get($vm->id);
|
||||
$vm->assigned_service_id = $a->service_id ?? null;
|
||||
$vm->assigned_customer_id = $a->service_customer_id ?? null;
|
||||
return $vm;
|
||||
});
|
||||
|
||||
return response()->json(['error' => false, 'data' => $vms]);
|
||||
}
|
||||
|
||||
public function getOrders(string $uuid, Request $request): JsonResponse
|
||||
{
|
||||
$customer = $this->customerRepository->findBy('uuid', $uuid);
|
||||
|
||||
@@ -189,6 +189,7 @@ class VCenterController extends Controller
|
||||
'db_id' => $vm->id,
|
||||
'uuid' => $vm->uuid,
|
||||
'vm_id' => $vm->vm_id,
|
||||
'ip' => $vm->ip,
|
||||
'power_state' => $vm->power_state,
|
||||
'folder' => $vm->folder,
|
||||
'cpu' => $vm->cpu,
|
||||
@@ -263,6 +264,7 @@ class VCenterController extends Controller
|
||||
'db_id' => $vm->id,
|
||||
'uuid' => $vm->uuid,
|
||||
'vm_id' => $vm->vm_id,
|
||||
'ip' => $vm->ip,
|
||||
'power_state' => $vm->power_state,
|
||||
'folder' => $vm->folder,
|
||||
'cpu' => $vm->cpu,
|
||||
|
||||
@@ -0,0 +1,236 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Service;
|
||||
|
||||
use App\DTOs\Order\ClientOrderFilters;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Api\Order\CheckServerNameRequest;
|
||||
use App\Http\Requests\Api\Order\PayOrderRequest;
|
||||
use App\Http\Requests\Api\Order\RenewServiceRequest;
|
||||
use App\Http\Requests\Api\Order\StoreOrderRequest;
|
||||
use App\Http\Resources\Api\Service\Order\InvoiceDetailResource;
|
||||
use App\Http\Resources\Api\Service\Order\InvoiceSummaryResource;
|
||||
use App\Http\Resources\Api\Service\Order\OrderDetailResource;
|
||||
use App\Http\Resources\Api\Service\Order\OrderItemResource;
|
||||
use App\Http\Resources\Api\Service\Order\OrderSummaryResource;
|
||||
use App\Http\Resources\Api\Service\Order\ServiceDetailResource;
|
||||
use App\Http\Resources\Api\Service\Order\ServiceSummaryResource;
|
||||
use App\Repositories\Api\Order\OrderRepository;
|
||||
use App\Services\ActivityLogger;
|
||||
use App\Services\Api\Order\OrderQueryService;
|
||||
use App\Services\Api\Order\OrderService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class OrderControllerRefactor extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly OrderService $orderService,
|
||||
private readonly OrderRepository $orderRepository,
|
||||
private readonly OrderQueryService $orderQueryService,
|
||||
private readonly ActivityLogger $activityLogger,
|
||||
) {
|
||||
}
|
||||
|
||||
public function getList(Request $request): JsonResponse
|
||||
{
|
||||
$orders = $this->orderQueryService->getOrdersForCustomer(
|
||||
(int) $request->user()->id,
|
||||
ClientOrderFilters::fromArray($request->only(['total_price', 'pending']))
|
||||
);
|
||||
|
||||
return response()->json(['success' => true, 'data' => $orders]);
|
||||
}
|
||||
|
||||
public function checkServerName(CheckServerNameRequest $request): JsonResponse
|
||||
{
|
||||
$result = $this->orderQueryService->checkServerName($request->string('name')->toString());
|
||||
|
||||
return response()->json($result['available'] ? ['available' => true] : $result);
|
||||
}
|
||||
|
||||
public function getClientOrders(Request $request): JsonResponse
|
||||
{
|
||||
$orders = $this->orderRepository->getOrders((int) $request->user()->id, []);
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'data' => OrderSummaryResource::collection($orders->getCollection()->load('invoice')),
|
||||
]);
|
||||
}
|
||||
|
||||
public function getClientOrderDetail(Request $request, int $id): JsonResponse
|
||||
{
|
||||
$order = $this->orderQueryService->findCustomerOrder((int) $request->user()->id, $id);
|
||||
|
||||
if (! $order) {
|
||||
return response()->json(['success' => false, 'message' => 'Đơn hàng không tồn tại.'], 404);
|
||||
}
|
||||
|
||||
return response()->json(['success' => true, 'data' => new OrderDetailResource($order)]);
|
||||
}
|
||||
|
||||
public function storeOrder(StoreOrderRequest $request): JsonResponse
|
||||
{
|
||||
try {
|
||||
$customer = $request->user();
|
||||
$data = $request->validated();
|
||||
|
||||
$product = \App\Models\Product::where('uuid', $data['product_id'])->first();
|
||||
if (! $product) {
|
||||
return response()->json(['success' => false, 'message' => 'Sản phẩm không tìm thấy hoặc đã bị xóa.'], 404);
|
||||
}
|
||||
|
||||
$data['product_id'] = $product->id;
|
||||
$result = $this->orderService->create($data, $customer);
|
||||
|
||||
$order = $result['order']->load(['orderItems.product', 'invoice', 'customer']);
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'Tạo đơn hàng thành công.',
|
||||
'notice' => 'Dịch vụ VPS sẽ được khởi tạo trong 5-10 phút sau khi thanh toán.',
|
||||
'data' => [
|
||||
'order' => new OrderDetailResource($order),
|
||||
'invoice' => new InvoiceDetailResource($result['invoice']->load(['customer', 'order'])),
|
||||
'items' => OrderItemResource::collection($order->orderItems),
|
||||
],
|
||||
], 201);
|
||||
} catch (\Throwable $e) {
|
||||
return response()->json(['success' => false, 'message' => $e->getMessage()], 400);
|
||||
}
|
||||
}
|
||||
|
||||
public function payOrder(int $id, PayOrderRequest $request): JsonResponse
|
||||
{
|
||||
$order = $this->orderRepository->find($id);
|
||||
|
||||
if (! $order) {
|
||||
return response()->json(['success' => false, 'message' => 'Đơn hàng không tồn tại.'], 404);
|
||||
}
|
||||
|
||||
if ($order->customer_id !== $request->user()->id) {
|
||||
return response()->json(['success' => false, 'message' => 'Không có quyền truy cập.'], 403);
|
||||
}
|
||||
|
||||
$this->orderService->pay($order, $request->payment_method);
|
||||
|
||||
$this->activityLogger->log(
|
||||
event: 'payment',
|
||||
logName: 'Order',
|
||||
description: "Customer {$request->user()->email} thanh toán đơn hàng #{$order->order_num} qua {$request->payment_method}",
|
||||
subject: $order,
|
||||
newValues: ['payment_method' => $request->payment_method, 'total' => $order->total],
|
||||
);
|
||||
|
||||
return response()->json(['success' => true, 'message' => 'Thanh toán thành công.']);
|
||||
}
|
||||
|
||||
public function retryOrderProvisioning(Request $request, int $id): JsonResponse
|
||||
{
|
||||
$order = $this->orderRepository->find($id);
|
||||
|
||||
if (! $order) {
|
||||
return response()->json(['success' => false, 'message' => 'Đơn hàng không tồn tại.'], 404);
|
||||
}
|
||||
|
||||
$result = $this->orderQueryService->retryProvisioning($order, (int) $request->user()->id);
|
||||
|
||||
if (! $result['ok']) {
|
||||
return response()->json(['success' => false, 'message' => $result['message']], $result['code']);
|
||||
}
|
||||
|
||||
$this->activityLogger->log(
|
||||
event: 'retry_provisioning',
|
||||
logName: 'Order',
|
||||
description: "Customer {$request->user()->email} yêu cầu khởi tạo lại dịch vụ cho đơn hàng #{$order->order_num}",
|
||||
subject: $order,
|
||||
);
|
||||
|
||||
return response()->json(['success' => true, 'message' => $result['message']]);
|
||||
}
|
||||
|
||||
public function getClientInvoices(Request $request): JsonResponse
|
||||
{
|
||||
$invoices = $this->orderQueryService->getCustomerInvoices((int) $request->user()->id);
|
||||
|
||||
return response()->json(['success' => true, 'data' => InvoiceSummaryResource::collection($invoices)]);
|
||||
}
|
||||
|
||||
public function getClientInvoiceDetail(Request $request, int $id): JsonResponse
|
||||
{
|
||||
$invoice = $this->orderQueryService->findCustomerInvoice((int) $request->user()->id, $id);
|
||||
|
||||
if (! $invoice) {
|
||||
return response()->json(['success' => false, 'message' => 'Hóa đơn không tồn tại.'], 404);
|
||||
}
|
||||
|
||||
return response()->json(['success' => true, 'data' => new InvoiceDetailResource($invoice)]);
|
||||
}
|
||||
|
||||
public function getClientInvoiceDetailByOrderNum(Request $request, string $orderNumber): JsonResponse
|
||||
{
|
||||
$order = $this->orderQueryService->findCustomerInvoiceByOrderNum((int) $request->user()->id, $orderNumber);
|
||||
|
||||
if (! $order?->invoice) {
|
||||
return response()->json(['success' => false, 'message' => 'Hóa đơn không tồn tại.'], 404);
|
||||
}
|
||||
|
||||
return response()->json(['success' => true, 'data' => new InvoiceDetailResource($order->invoice->setRelation('order', $order))]);
|
||||
}
|
||||
|
||||
public function getClientServices(Request $request): JsonResponse
|
||||
{
|
||||
$services = $this->orderQueryService->getCustomerServices((int) $request->user()->id);
|
||||
|
||||
return response()->json(['success' => true, 'data' => ServiceSummaryResource::collection($services)]);
|
||||
}
|
||||
|
||||
public function getClientServiceDetail(Request $request, string $uuid): JsonResponse
|
||||
{
|
||||
$service = $this->orderQueryService->findCustomerService((int) $request->user()->id, $uuid);
|
||||
|
||||
if (! $service) {
|
||||
return response()->json(['success' => false, 'message' => 'Không tìm thấy dịch vụ.'], 404);
|
||||
}
|
||||
|
||||
return response()->json(['success' => true, 'data' => new ServiceDetailResource($service)]);
|
||||
}
|
||||
|
||||
public function updateClientServiceAutoRenew(Request $request, int $id): JsonResponse
|
||||
{
|
||||
$validated = $request->validate(['auto_renew' => ['required', 'boolean']]);
|
||||
|
||||
$service = \App\Models\Service::query()
|
||||
->where('id', $id)
|
||||
->where('customer_id', $request->user()->id)
|
||||
->first();
|
||||
|
||||
if (! $service) {
|
||||
return response()->json(['success' => false, '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(['success' => true, 'message' => 'Cập nhật tự động gia hạn thành công.', 'data' => ['id' => $service->id, 'auto_renew' => (bool) $service->auto_renew]]);
|
||||
}
|
||||
|
||||
public function renewService(RenewServiceRequest $request, int $id): JsonResponse
|
||||
{
|
||||
$result = $this->orderQueryService->renewService($id, (int) $request->user()->id, (int) $request->validated()['months']);
|
||||
|
||||
if (! $result['ok']) {
|
||||
return response()->json(['success' => false, 'message' => $result['message']], $result['code']);
|
||||
}
|
||||
|
||||
return response()->json(['success' => true, 'message' => $result['message'], 'data' => $result['data']]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user