Files
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

161 lines
6.0 KiB
PHP

<?php
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
{
public function getDomains(string $uuid): JsonResponse
{
$customer = $this->resolveCustomer($uuid);
$domains = $this->moduleDomainRepository->filter(['customer_id' => $customer->id], ['customer']);
return response()->json(['error' => false, 'data' => $domains]);
}
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'])
->withCount(['ns', 'record'])
->whereHas('customer', fn($q) => $q->where('customers.id', $customer->id))
->orderByDesc('id')
->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,
'ns_count' => (int) ($d->ns_count ?? 0),
'record_count' => (int) ($d->record_count ?? 0),
'server' => $d->server ? [
'id' => $d->server->id,
'name' => $d->server->name,
'ip_address' => $d->server->ip_address,
] : null,
'registrar' => $d->registrar ? [
'id' => $d->registrar->id,
'name' => $d->registrar->name,
'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,
]);
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);
$owns = DB::table('dns_domain_user')
->where('domain_id', $domainId)
->where('user_id', $customer->id)
->exists();
abort_if(!$owns, 403, 'Domain không thuộc về khách hàng này');
$records = DnsRecord::query()
->where('dns_manager_domain_id', $domainId)
->orderBy('type')
->orderBy('name')
->get();
return response()->json(['error' => false, 'data' => $records]);
}
}