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:
co-authored by
Claude Sonnet 5.5
parent
e37106f2dc
commit
7136bec265
@@ -93,9 +93,13 @@ class DomainController extends Controller
|
||||
return response()->json(['error' => true, 'message' => "Không tìm thấy domain #$id"], 404);
|
||||
}
|
||||
|
||||
$summary = DomainServiceSummary::forDomains(collect([$domain]));
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'data' => $this->formatDomain($domain),
|
||||
'data' => $this->formatDomain($domain) + [
|
||||
'service' => $summary[DomainServiceSummary::key($domain->domain_name)] ?? null,
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -194,6 +198,37 @@ class DomainController extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
public function updateTerm(int $id, Request $request): JsonResponse
|
||||
{
|
||||
$domain = $this->domainRepository->find($id);
|
||||
|
||||
if (!$domain) {
|
||||
return response()->json(['error' => true, 'message' => "Không tìm thấy domain #$id"], 404);
|
||||
}
|
||||
|
||||
$data = $request->validate([
|
||||
'registered_at' => 'nullable|date',
|
||||
'expires_at' => 'nullable|date|after_or_equal:registered_at',
|
||||
'cycle_years' => 'nullable|integer|min:1|max:10',
|
||||
]);
|
||||
|
||||
$updated = $this->customerDomainServiceSync->applyTerm(
|
||||
$domain,
|
||||
$data['registered_at'] ?? null,
|
||||
$data['expires_at'] ?? null,
|
||||
$data['cycle_years'] ?? null,
|
||||
);
|
||||
|
||||
if ($updated === 0) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => 'Tên miền chưa gắn khách hàng nên chưa có dịch vụ để lưu ngày đăng ký/hết hạn.',
|
||||
], 422);
|
||||
}
|
||||
|
||||
return $this->show($id);
|
||||
}
|
||||
|
||||
public function provision(int $id): JsonResponse
|
||||
{
|
||||
$domain = $this->domainRepository->find($id);
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support;
|
||||
|
||||
use App\Models\ModuleDomain;
|
||||
use App\Models\Order;
|
||||
use App\Models\Service;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
class LegacyDomainTerms
|
||||
{
|
||||
public static function for(Service $service): ?array
|
||||
{
|
||||
$legacy = ModuleDomain::query()
|
||||
->whereRaw('LOWER(TRIM(name)) = ?', [mb_strtolower(trim((string) $service->server_name))])
|
||||
->orderByRaw('customer_id = ? desc', [$service->customer_id])
|
||||
->orderByDesc('next_due_date')
|
||||
->first();
|
||||
|
||||
if (! $legacy) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return [
|
||||
'registered_at' => $legacy->order_id ? Order::whereKey($legacy->order_id)->value('created_at') : null,
|
||||
'expires_at' => $legacy->next_due_date > 0 ? Carbon::createFromTimestamp($legacy->next_due_date) : null,
|
||||
];
|
||||
}
|
||||
|
||||
public static function fill(Service $service): void
|
||||
{
|
||||
$terms = self::for($service);
|
||||
|
||||
if (! $terms) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (! $service->expires_at && $terms['expires_at']) {
|
||||
$service->expires_at = $terms['expires_at'];
|
||||
}
|
||||
|
||||
if ($service->note === \App\Services\Domain\CustomerDomainServiceSync::AUTO_CREATED_NOTE && $terms['registered_at']) {
|
||||
$service->started_at = $terms['registered_at'];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Service;
|
||||
use App\Support\LegacyDomainTerms;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Service::query()
|
||||
->where('type', 'domain')
|
||||
->whereNull('expires_at')
|
||||
->each(function (Service $service): void {
|
||||
LegacyDomainTerms::fill($service);
|
||||
$service->save();
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void {}
|
||||
};
|
||||
@@ -18,6 +18,7 @@ Route::prefix('domains')
|
||||
Route::post('sync-dns', 'syncDns')->middleware('perm:domain.manager');
|
||||
Route::get('{id}', 'show')->middleware('perm:domain.manager');
|
||||
Route::put('{id}', 'update')->middleware('perm:domain.manager');
|
||||
Route::put('{id}/term', 'updateTerm')->middleware('perm:domain.manager');
|
||||
Route::post('{id}/provision', 'provision')->middleware('perm:domain.manager');
|
||||
Route::delete('{id}', 'destroy')->middleware('perm:domain.manager');
|
||||
// DNS Records
|
||||
|
||||
@@ -69,4 +69,28 @@ class CustomerDomainServiceSyncTest extends TestCase
|
||||
'user_id' => $customer->id,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_apply_term_sets_dates_and_cycle_for_domain_services(): void
|
||||
{
|
||||
$customer = Customer::factory()->create();
|
||||
$serverId = DB::table('dns_manager_servers')->insertGetId(['ip_address' => '10.0.0.1', 'password' => 'x']);
|
||||
$domain = DnsDomain::create([
|
||||
'domain_name' => 'devro-tech.com',
|
||||
'dns_manager_server_id' => $serverId,
|
||||
'is_created' => DnsDomain::STATUS_CREATED,
|
||||
]);
|
||||
Product::factory()->create(['product_type' => 'domain', 'is_active' => true]);
|
||||
$sync = app(CustomerDomainServiceSync::class);
|
||||
$sync->syncCustomers($domain, [$customer->id]);
|
||||
|
||||
$updated = $sync->applyTerm($domain, '2025-06-17', '2027-06-17', 2);
|
||||
|
||||
$service = Service::where('customer_id', $customer->id)->where('type', 'domain')->firstOrFail();
|
||||
$this->assertSame(1, $updated);
|
||||
$this->assertSame('2025-06-17', $service->started_at->toDateString());
|
||||
$this->assertSame('2027-06-17', $service->expires_at->toDateString());
|
||||
$this->assertSame(24, (int) $service->orderItem->months);
|
||||
$this->assertSame('biennially', $service->orderItem->billing_cycle);
|
||||
$this->assertSame(0, $sync->applyTerm(DnsDomain::create(['domain_name' => 'solo.vn', 'dns_manager_server_id' => $serverId]), null, null, 1));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user