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>
This commit is contained in:
baodev0
2026-10-11 23:07:51 +07:00
co-authored by Claude Opus 5.5
parent 10af41c5cf
commit c5115b52dd
53 changed files with 1887 additions and 613 deletions
@@ -4,7 +4,11 @@ namespace App\Http\Controllers\Api\Admin\Customer\Concerns;
use App\Models\DnsDomain;
use App\Models\DnsRecord;
use App\Models\Service;
use App\Services\Domain\CustomerDomainServiceSync;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Collection;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
trait ManagesCustomerDomains
@@ -21,6 +25,7 @@ trait ManagesCustomerDomains
public function getDnsDomains(string $uuid): JsonResponse
{
$customer = $this->resolveCustomer($uuid);
$domainServices = $this->customerDomainServices($customer->id);
$domains = DnsDomain::query()
->with(['server:id,name,ip_address', 'registrar:id,name,short_name,website'])
@@ -30,6 +35,7 @@ trait ManagesCustomerDomains
->get()
->map(fn($d) => [
'id' => $d->id,
'service_id' => $domainServices->get($this->domainKey($d->domain_name))?->id,
'domain_name' => $d->domain_name,
'is_created' => (int) $d->is_created,
'plesk_id' => $d->plesk_id,
@@ -46,6 +52,7 @@ trait ManagesCustomerDomains
'short_name' => $d->registrar->short_name,
'website' => $d->registrar->website,
] : null,
'expires_at' => $domainServices->get($this->domainKey($d->domain_name))?->expires_at,
'created_at' => $d->created_at,
'updated_at' => $d->updated_at,
]);
@@ -53,6 +60,84 @@ trait ManagesCustomerDomains
return response()->json(['error' => false, 'data' => $domains]);
}
public function updateDnsDomain(string $uuid, int $domainId, Request $request): JsonResponse
{
$domain = $this->resolveCustomerDnsDomain($uuid, $domainId);
$data = $request->validate([
'is_created' => 'required|integer|in:0,1,2,3',
'created_at' => 'required|date',
'expires_at' => 'nullable|date',
]);
$customer = $this->resolveCustomer($uuid);
DB::transaction(function () use ($domain, $customer, $data) {
$domain->forceFill(['is_created' => $data['is_created'], 'created_at' => $data['created_at']])->save();
app(CustomerDomainServiceSync::class)->updateCustomerDomain($domain, $customer->id, $data['expires_at']);
});
return response()->json(['error' => false, 'message' => 'Cập nhật domain thành công']);
}
public function detachDnsDomain(string $uuid, int $domainId): JsonResponse
{
$customer = $this->resolveCustomer($uuid);
$domain = $this->resolveCustomerDnsDomain($uuid, $domainId);
$remainingCustomerIds = $domain->customer()->pluck('customers.id')->reject(
fn ($customerId) => (int) $customerId === (int) $customer->id,
)->all();
app(CustomerDomainServiceSync::class)->syncCustomers($domain, $remainingCustomerIds);
return response()->json(['error' => false, 'message' => 'Đã gỡ domain khỏi khách hàng']);
}
public function assignDnsDomain(string $uuid, Request $request): JsonResponse
{
$customer = $this->resolveCustomer($uuid);
$data = $request->validate([
'domain_id' => ['required', 'integer', 'exists:dns_manager_domain,id'],
'service_id' => ['required', 'integer', 'exists:services,id'],
]);
abort_unless(
Service::query()->whereKey($data['service_id'])->where('customer_id', $customer->id)->exists(),
422,
'Dịch vụ không thuộc khách hàng này',
);
$domain = DnsDomain::query()->findOrFail($data['domain_id']);
$customerIds = $domain->customer()->pluck('customers.id')->push($customer->id)->all();
app(CustomerDomainServiceSync::class)->syncCustomers($domain, $customerIds);
return response()->json(['error' => false, 'message' => 'Gán domain thành công']);
}
private function customerDomainServices(int $customerId): Collection
{
return Service::query()
->where('customer_id', $customerId)
->where('type', 'domain')
->orderBy('id')
->get()
->keyBy(fn(Service $service) => $this->domainKey($service->server_name));
}
private function domainKey(?string $domainName): string
{
return mb_strtolower(trim((string) $domainName));
}
private function resolveCustomerDnsDomain(string $uuid, int $domainId): DnsDomain
{
$customer = $this->resolveCustomer($uuid);
return DnsDomain::query()
->whereHas('customer', fn($q) => $q->where('customers.id', $customer->id))
->findOrFail($domainId);
}
public function getDnsDomainRecords(string $uuid, int $domainId): JsonResponse
{
$customer = $this->resolveCustomer($uuid);