- Mail campaign: mẫu nhắc gia hạn thanh toán QR, chọn dịch vụ + chu kỳ từng dòng (renewal_services) - Hosting: nhiều server Plesk cho một gói (service_plan_servers), chuyển server khi server chết - Admin xem/sửa giấy tờ định danh khách, tuỳ chọn gia hạn cho khách, nhật ký cấp phát dịch vụ - Đánh dấu mẫu email dịch vụ là non-marketing Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
166 lines
7.3 KiB
PHP
166 lines
7.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Resources\Api\Client\Service;
|
|
|
|
use App\Models\DnsDomain;
|
|
use App\Support\ResponseCipher;
|
|
use App\Support\ServiceGroup;
|
|
use App\Models\ModuleHostingPleskClient;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Resources\Json\JsonResource;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Facades\Crypt;
|
|
|
|
class ServiceClientDetailResource extends JsonResource
|
|
{
|
|
private const IRRELEVANT_KEYS = [
|
|
'hosting' => ['os', 'specs', 'vm', 'custom_product', 'ssl_certificate', 'dns_domain_id', 'dns_cancelled'],
|
|
'vps' => ['hosting', 'service_plan', 'ssl_certificate', 'dns_domain_id', 'dns_cancelled'],
|
|
'cloud' => ['hosting', 'service_plan', 'ssl_certificate', 'dns_domain_id', 'dns_cancelled'],
|
|
'domain' => ['os', 'specs', 'vm', 'hosting', 'service_plan', 'custom_product', 'ssl_certificate'],
|
|
'ssl' => ['os', 'specs', 'vm', 'hosting', 'service_plan', 'custom_product', 'dns_domain_id', 'dns_cancelled'],
|
|
];
|
|
|
|
/** Plesk panel login (persisted, reusable) — separate from the one-time FTP password, which is never stored. */
|
|
private function resolvePleskCredentials($hosting): ?array
|
|
{
|
|
$client = ModuleHostingPleskClient::forHosting($hosting);
|
|
|
|
if (!$client) {
|
|
return null;
|
|
}
|
|
|
|
return ['login' => $client->login, 'password' => Crypt::decryptString($client->password)];
|
|
}
|
|
|
|
private function resolveDnsZone(): ?DnsDomain
|
|
{
|
|
if ($this->type !== 'domain' || !$this->server_name) {
|
|
return null;
|
|
}
|
|
|
|
return DnsDomain::query()
|
|
->whereRaw('LOWER(domain_name) = ?', [mb_strtolower(trim($this->server_name))])
|
|
->whereHas('customer', fn ($query) => $query->where('customers.id', $this->customer_id))
|
|
->first(['id', 'is_created']);
|
|
}
|
|
|
|
/**
|
|
* Transform the resource into an array.
|
|
*
|
|
* @return array<string, mixed>
|
|
*/
|
|
private function resolveSpecs($vm, $product, $custom = null): ?array
|
|
{
|
|
if ($custom) {
|
|
return [
|
|
'cpu' => (int) ($custom->cpu ?? 0),
|
|
'ram' => round(($custom->ram_mb ?? 0) / 1024, 2),
|
|
'disk' => (int) ($custom->disk_gb ?? 0),
|
|
'bandwidth' => (int) ($custom->bandwidth_gb ?? 0),
|
|
];
|
|
}
|
|
|
|
if ($vm && ($vm->cpu !== null || $vm->ram_mb !== null || $vm->disk_gb !== null)) {
|
|
return [
|
|
'cpu' => (int) ($vm->cpu ?? 0),
|
|
'ram' => round(($vm->ram_mb ?? 0) / 1024, 2),
|
|
'disk' => (int) ($vm->disk_gb ?? 0),
|
|
'bandwidth' => (int) ($product->bandwidth ?? 0),
|
|
];
|
|
}
|
|
|
|
$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
|
|
{
|
|
if (!$this->resource) {
|
|
return [];
|
|
}
|
|
|
|
$orderItem = $this->orderItem;
|
|
$order = $orderItem?->order;
|
|
$vm = $this->resource->vmware;
|
|
$product = $orderItem?->product ?? $this->resource->product;
|
|
$custom = $this->resource->customProduct;
|
|
$hosting = $this->resource->hostingAccount;
|
|
$pleskCredentials = $hosting ? $this->resolvePleskCredentials($hosting) : null;
|
|
$dnsZone = $this->resolveDnsZone();
|
|
$dnsCancelled = $dnsZone !== null && (int) $dnsZone->is_created === DnsDomain::STATUS_CANCELLED;
|
|
|
|
$payload = [
|
|
'uuid' => $this->uuid,
|
|
'type' => $this->type,
|
|
'group' => ServiceGroup::of($this->orderItem?->product?->product_type ?? $this->product?->product_type, $this->orderItem?->product?->name ?? $this->product?->name),
|
|
'server_name' => $this->server_name,
|
|
'status' => $this->status,
|
|
'auto_renew' => $this->auto_renew,
|
|
'started_at' => $this->started_at?->toISOString(),
|
|
'expires_at' => $this->expires_at?->toISOString(),
|
|
'os' => [
|
|
'version' => $orderItem?->os?->version ?? $vm?->os?->version,
|
|
'group' => $orderItem?->os?->osGroup?->name ?? $vm?->os?->osGroup?->name,
|
|
],
|
|
'specs' => $this->type === 'hosting' ? null : $this->resolveSpecs($vm, $product, $custom),
|
|
'service_plan' => $this->type === 'hosting' ? $product?->servicePlan?->summary() : null,
|
|
'custom_product' => $custom?->summary(),
|
|
'order' => $order ? [
|
|
'total' => (float) $order->total,
|
|
'months' => (int) $orderItem->months,
|
|
'billing_label' => $orderItem->billing_label,
|
|
] : null,
|
|
'vm' => $vm ? [
|
|
'ip_address' => $vm->ipAddress?->ip ?? $vm->ip,
|
|
'hostname' => $vm->hostname,
|
|
'username' => ResponseCipher::encrypt($vm->user),
|
|
'password' => ResponseCipher::encrypt($vm->plain_password),
|
|
'port' => $vm->port,
|
|
'status' => $vm->status,
|
|
'ip' => $vm->ip ?? null,
|
|
'dns' => $vm->dns ?? null,
|
|
] : null,
|
|
'hosting' => $hosting ? [
|
|
'ip_address' => $hosting->server?->publicHost(),
|
|
'server_ip' => $hosting->server?->ip_address,
|
|
'plesk_url' => $hosting->server?->panelUrl(),
|
|
'username' => ResponseCipher::encrypt($pleskCredentials['login'] ?? null),
|
|
'password' => ResponseCipher::encrypt($pleskCredentials['password'] ?? null),
|
|
] : null,
|
|
'attachments' => \App\Models\ServiceAttachment::forCustomer()
|
|
->where('service_id', $this->resource->id)
|
|
->latest()
|
|
->get()
|
|
->map(fn ($file) => [
|
|
'id' => $file->id,
|
|
'filename' => $file->filename,
|
|
'mime_type' => $file->mime_type,
|
|
'size' => (int) $file->size,
|
|
'uploaded_at' => $file->created_at?->toISOString(),
|
|
])
|
|
->values(),
|
|
'cancel_at_expiry' => (bool) $this->cancel_at_expiry,
|
|
'cancellation_request' => \App\Http\Controllers\Api\Client\Service\ServiceCancellationController::payload($this->resource->cancellationRequests()->pending()->latest()->first()),
|
|
'ssl_certificate' => $this->resource->sslCertificate?->data,
|
|
'dns_domain_id' => $dnsZone && !$dnsCancelled ? $dnsZone->id : null,
|
|
'dns_cancelled' => $dnsCancelled,
|
|
];
|
|
|
|
return Arr::except($payload, self::IRRELEVANT_KEYS[$this->type] ?? []);
|
|
}
|
|
}
|