feat(domain): chỉnh ngày bắt đầu/hết hạn tên miền và điền hạn từ dữ liệu cũ

PUT administrator/domains/{id}/term, LegacyDomainTerms và migration backfill hạn cho dịch vụ domain chưa có expires_at.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
baodev0
2026-10-28 17:26:41 +07:00
co-authored by Claude Sonnet 5.5
parent e37106f2dc
commit 7136bec265
6 changed files with 171 additions and 2 deletions
@@ -2,9 +2,12 @@
namespace App\Services\Domain;
use App\Enums\Product\ProductBillingCycle;
use App\Models\DnsDomain;
use App\Models\Service;
use App\Services\Order\ServiceOrderRecorder;
use App\Support\BillingCycleCatalog;
use App\Support\LegacyDomainTerms;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
@@ -40,7 +43,9 @@ class CustomerDomainServiceSync
'server_name' => $domain->domain_name,
'status' => $this->serviceStatus($domain),
'started_at' => $service->started_at ?? $domain->created_at ?? Carbon::now(),
])->save();
]);
LegacyDomainTerms::fill($service);
$service->save();
if ($service->status !== 'cancelled') {
$this->orderRecorder->ensureForService($service);
@@ -86,6 +91,43 @@ class CustomerDomainServiceSync
return $service;
}
public function applyTerm(DnsDomain $domain, ?string $registeredAt, ?string $expiresAt, ?int $cycleYears): int
{
$services = Service::query()
->where('type', 'domain')
->where('status', '!=', 'cancelled')
->whereIn('customer_id', $domain->customer()->pluck('customers.id'))
->whereRaw('LOWER(TRIM(server_name)) = ?', [$this->domainKey($domain->domain_name)])
->with('orderItem')
->get();
DB::transaction(function () use ($services, $registeredAt, $expiresAt, $cycleYears): void {
foreach ($services as $service) {
$attributes = [
'expires_at' => $expiresAt
? Carbon::parse($expiresAt)->setTimeFrom($service->expires_at ?? Carbon::today())
: null,
];
if ($registeredAt) {
$attributes['started_at'] = Carbon::parse($registeredAt);
}
$service->forceFill($attributes)->save();
if ($cycleYears && $service->orderItem) {
$months = $cycleYears * 12;
$service->orderItem->update([
'months' => $months,
'billing_cycle' => collect(ProductBillingCycle::cases())
->first(fn (ProductBillingCycle $cycle) => $cycle->months() === $months)?->value
?? BillingCycleCatalog::codeFor($months),
]);
}
}
});
return $services->count();
}
private function findService(int $customerId, string $oldName, string $currentName): ?Service
{
return Service::withTrashed()