Files
portal-api/app/Services/Domain/CustomerDomainServiceSync.php
T
baodev0andClaude Opus 5.5 c5115b52dd feat: nạp tiền thủ công mã 30 phút, gia hạn trừ số dư, đồng bộ domain từ Plesk
- Nạp tiền: mã thanh toán sống 30 phút (ManualDepositDraft), khôi phục/huỷ mã, lệnh dọn mã hết hạn; total_pending chỉ tính giao dịch đã nộp chứng từ, admin duyệt/từ chối/xoá giao dịch
- Gia hạn dịch vụ: phát hành invoice gia hạn trên order gốc và trừ số dư ngay
- Đồng bộ domain từ Plesk theo server, ServiceOrderRecorder tạo order/invoice cho dịch vụ admin dựng
- Danh sách dịch vụ trả field theo loại (service_plan, dns_records_count), đếm dịch vụ theo loại

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 23:07:51 +07:00

117 lines
4.2 KiB
PHP

<?php
namespace App\Services\Domain;
use App\Models\DnsDomain;
use App\Models\Service;
use App\Services\Order\ServiceOrderRecorder;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
class CustomerDomainServiceSync
{
public const AUTO_CREATED_NOTE = 'Tự động tạo từ phân công DNS domain';
public function __construct(private readonly ServiceOrderRecorder $orderRecorder) {}
public function syncCustomers(DnsDomain $domain, array $customerIds, ?string $previousDomainName = null): void
{
$customerIds = collect($customerIds)->map(fn ($id) => (int) $id)->unique()->values();
$previousCustomerIds = $domain->customer()->pluck('customers.id');
$oldName = $previousDomainName ?? $domain->domain_name;
DB::transaction(function () use ($domain, $customerIds, $previousCustomerIds, $oldName): void {
$domain->customer()->sync($customerIds->all());
foreach ($customerIds as $customerId) {
$service = $this->findService($customerId, $oldName, $domain->domain_name);
if ($service?->trashed()) {
$service->restore();
}
$service ??= new Service([
'customer_id' => $customerId,
'type' => 'domain',
'note' => self::AUTO_CREATED_NOTE,
]);
$service->fill([
'server_name' => $domain->domain_name,
'status' => $this->serviceStatus($domain),
'started_at' => $service->started_at ?? $domain->created_at ?? Carbon::now(),
])->save();
if ($service->status !== 'cancelled') {
$this->orderRecorder->ensureForService($service);
}
}
$removedCustomerIds = $previousCustomerIds->diff($customerIds);
if ($removedCustomerIds->isNotEmpty()) {
Service::query()
->whereIn('customer_id', $removedCustomerIds)
->where('type', 'domain')
->whereIn(DB::raw('LOWER(TRIM(server_name))'), array_unique([
$this->domainKey($oldName),
$this->domainKey($domain->domain_name),
]))
->get()
->each(function (Service $service): void {
$this->orderRecorder->cancelUnpaidInvoice($service);
$service->delete();
});
}
});
}
public function updateCustomerDomain(
DnsDomain $domain,
int $customerId,
?string $expiresAt,
): Service {
$this->syncCustomers($domain, $domain->customer()->pluck('customers.id')->all());
$service = $this->findService($customerId, $domain->domain_name, $domain->domain_name);
abort_if(! $service, 404, 'Không tìm thấy dịch vụ tên miền');
$service->fill([
'status' => $this->serviceStatus($domain),
'started_at' => $domain->created_at,
'expires_at' => $expiresAt
? Carbon::parse($expiresAt)->setTimeFrom($service->expires_at ?? Carbon::today())
: null,
])->save();
return $service;
}
private function findService(int $customerId, string $oldName, string $currentName): ?Service
{
return Service::withTrashed()
->where('customer_id', $customerId)
->where('type', 'domain')
->whereIn(DB::raw('LOWER(TRIM(server_name))'), array_unique([
$this->domainKey($oldName),
$this->domainKey($currentName),
]))
->orderBy('id')
->first();
}
private function serviceStatus(DnsDomain $domain): string
{
return match ((int) $domain->is_created) {
DnsDomain::STATUS_CREATED => 'active',
DnsDomain::STATUS_INACTIVE => 'suspended',
DnsDomain::STATUS_CANCELLED => 'cancelled',
default => 'pending',
};
}
private function domainKey(?string $domainName): string
{
return mb_strtolower(trim((string) $domainName));
}
}