- VmProvisionException/VmProvisionRefund xử lý lỗi cấp phát VM - QueueJobInspector + QueueMonitorController xem chi tiết job - Thêm service uuid cho ticket, thời lượng cho activity_logs - Migration chuẩn hoá RAM addon sang MB, trạng thái deleting cho snapshot Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
79 lines
3.1 KiB
PHP
79 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Resources\Api\Service;
|
|
|
|
use App\Models\Product;
|
|
use App\Models\Ticket;
|
|
use App\Support\ServiceGroup;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Resources\Json\JsonResource;
|
|
|
|
class VpsServiceResource extends JsonResource
|
|
{
|
|
private function resolveSpecs(?Product $product): ?array
|
|
{
|
|
$addons = $this->addons ?? collect();
|
|
if ($addons->isNotEmpty()) {
|
|
return [
|
|
'cpu' => (int) $addons->where('addon_type', 'cpu')->sum('addon_value'),
|
|
'ram' => round($addons->where('addon_type', 'ram')->sum('addon_value') / 1024, 2),
|
|
'disk' => (int) $addons->where('addon_type', 'disk')->sum('addon_value'),
|
|
'bandwidth' => (int) $addons->where('addon_type', 'bandwidth')->sum('addon_value'),
|
|
];
|
|
}
|
|
return $product ? [
|
|
'cpu' => (int) ($product->cpu ?? 0),
|
|
'ram' => (int) ($product->ram ?? 0),
|
|
'disk' => (int) ($product->disk ?? 0),
|
|
'bandwidth' => (int) ($product->bandwidth ?? 0),
|
|
] : null;
|
|
}
|
|
|
|
public function toArray(Request $request): array
|
|
{
|
|
$orderItem = $this->orderItem;
|
|
$invoice = $this->invoice;
|
|
$order = $orderItem?->order ?? $invoice?->order;
|
|
$product = $orderItem?->product ?? $this->product;
|
|
$type = $this->type ?? $product?->product_type;
|
|
|
|
return [
|
|
'uuid' => $this->uuid,
|
|
'type' => $type,
|
|
'group' => ServiceGroup::of($product?->product_type, $product?->name),
|
|
'server_name' => $this->server_name,
|
|
'invoice_num' => $invoice?->invoice_number,
|
|
'order_num' => $order?->order_num,
|
|
'status' => $this->status,
|
|
'status_label' => $this->status_label,
|
|
'failure_ticket' => $this->status === 'failed'
|
|
? Ticket::where('service_uuid', $this->uuid)->latest('id')->first(['ticket_num', 'status'])
|
|
: null,
|
|
'auto_renew' => (bool) $this->auto_renew,
|
|
'cancel_at_expiry' => (bool) $this->cancel_at_expiry,
|
|
'started_at' => $this->started_at?->toISOString(),
|
|
'expires_at' => $this->expires_at?->toISOString(),
|
|
'product' => $product ? [
|
|
'uuid' => $product->uuid,
|
|
'name' => $product->name,
|
|
'slug' => $product->slug,
|
|
'product_type' => $product->product_type,
|
|
'page_type' => 'service_' . str_replace(' ', '_', $product->product_type),
|
|
] : null,
|
|
$this->mergeWhen($type === 'vps', fn () => [
|
|
'os' => [
|
|
'version' => $orderItem?->os?->version,
|
|
'group' => $orderItem?->os?->osGroup?->name,
|
|
],
|
|
'specs' => $this->resolveSpecs($product),
|
|
]),
|
|
$this->mergeWhen($type === 'hosting', fn () => [
|
|
'service_plan' => $product?->servicePlan?->summary(),
|
|
]),
|
|
$this->mergeWhen($type === 'domain', fn () => [
|
|
'dns_records_count' => $this->dns_records_count,
|
|
]),
|
|
];
|
|
}
|
|
}
|