Files
portal-api/app/Models/Service.php
T
baodev0andClaude Sonnet 5.5 6315a392c0 feat(service): custom product, lịch sử giám sát VM và mã hoá response theo phiên
- Dịch vụ custom: bảng custom_products + services.custom_product_id; createService nhận
  custom{} thay product_id, giá = đơn giá tháng x số tháng chu kỳ (1/3/6/12/24/36),
  gia hạn tính theo cùng đơn giá, ServiceClientDetailResource trả custom_product và specs.
- Giám sát VM: GET v1/system/vm-metrics/history gom lịch sử về tối đa 240 điểm;
  lỗi lấy dung lượng đĩa không còn làm hỏng cả thẻ metrics.
- SealResponse: AES-256-GCM theo khoá phiên do proxy Next cấp qua header x-seal-key
  (HMAC của session id). Client opt-in bằng x-seal; chi tiết dịch vụ, mật khẩu VM và
  console ticket luôn được mã hoá. Body gửi lên dạng {enc} được giải mã trước validate;
  khoá lệch trả 428 để client lấy khoá mới. Đây là che mắt DevTools, không phải bảo mật
  chống chính chủ tài khoản.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-27 13:58:50 +07:00

164 lines
4.5 KiB
PHP

<?php
namespace App\Models;
use App\Models\Concerns\Markable;
use App\Support\ServiceExpirySync;
use App\Traits\LogsActivity;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Service extends Model
{
use HasFactory, HasUuids, SoftDeletes, LogsActivity, Markable;
public function uniqueIds(): array
{
return ['uuid'];
}
protected static function booted(): void
{
static::saving(function (Service $service) {
if ($service->type === null && $service->product_id) {
$service->type = Product::query()->where('id', $service->product_id)->value('product_type');
}
});
static::saved(function (Service $service) {
if ($service->wasRecentlyCreated || $service->wasChanged(['expires_at', 'vm_id'])) {
ServiceExpirySync::toVmware($service);
}
});
}
protected $table = 'services';
public const HOSTING_ACCOUNT_RELATIONS = [
'hostingAccount.server:id,name,ip_address,domain,panel_port',
'hostingAccount.domain:id,domain_name',
];
protected $fillable = [
'uuid',
'customer_id',
'product_id',
'custom_product_id',
'type',
'order_item_id',
'invoice_id',
'vm_id',
'server_name',
'status',
'auto_renew',
'started_at',
'expires_at',
'suspended_at',
'cancelled_at',
'refunded_at',
'retry_requested_at',
'note',
'requirements',
'feedback',
'registrar_reference',
'kyc_url',
'rejection_reason',
'registrant_type',
'registrant_info',
];
protected $casts = [
'auto_renew' => 'boolean',
'started_at' => 'datetime',
'expires_at' => 'datetime',
'suspended_at' => 'datetime',
'cancelled_at' => 'datetime',
'refunded_at' => 'datetime',
'retry_requested_at' => 'datetime',
'registrant_info' => 'array',
];
// -------------------------------------------------------------------------
// Relationships
// -------------------------------------------------------------------------
public function customer()
{
return $this->belongsTo(Customer::class);
}
public function product()
{
return $this->belongsTo(Product::class, 'product_id');
}
public function customProduct()
{
return $this->belongsTo(CustomProduct::class, 'custom_product_id');
}
/** The order item that triggered provisioning. */
public function orderItem()
{
return $this->belongsTo(OrderItem::class, 'order_item_id');
}
/** The most recent invoice covering this service (updated on renewal). */
public function invoice()
{
return $this->belongsTo(Invoice::class, 'invoice_id');
}
/** Configurable resources / features attached to this service. */
public function addons()
{
return $this->hasMany(ServiceAddon::class, 'service_id');
}
/** The VPS/VM associated with this service — FK: services.vm_id → module_vmware.id */
public function vmware()
{
return $this->belongsTo(ModuleVmware::class, 'vm_id');
}
public function resolveVmware(): ?ModuleVmware
{
return $this->vmware ?? $this->orderItem?->order?->vmWare?->first();
}
/** The Plesk hosting account associated with this service, if type = hosting. */
public function hostingAccount()
{
return $this->hasOne(ModuleHostingAccount::class, 'service_id');
}
// -------------------------------------------------------------------------
// Helpers
// -------------------------------------------------------------------------
public function isActive(): bool
{
return $this->status === 'active';
}
public function isExpired(): bool
{
return $this->expires_at !== null && $this->expires_at->isPast();
}
public function getStatusLabelAttribute(): string
{
return match ($this->status) {
'pending' => 'Chờ kích hoạt',
'active' => 'Đang hoạt động',
'suspended' => 'Tạm ngưng',
'expired' => 'Hết hạn',
'cancelled' => 'Đã hủy',
'terminated' => 'Đã xóa',
default => ucfirst($this->status),
};
}
}