Files
portal-api/app/Http/Controllers/Api/Admin/IpPool/Concerns/ManagesIpPoolAllocation.php
T
baodev0andClaude Opus 5.5 46aa138eec feat(admin): IP pool allocation, billing exports, service attachments
- IP pool: sheet import (xlsx/csv) matched by address, bulk allocate/revoke
  with notification email (ip_allocated/ip_revoked), ip_pool_logs history.
- Orders/invoices: Excel export on branded template (BrandedReportWriter),
  shared billing filters, order item service info in list responses.
- Web design quote Excel export; activity log export.
- Service attachments with copy-to-invoice; deployment invoice attachments.
- Customer email attachments stored on disk and attached by path.
- Admin password change endpoint; user.fullname backfill migration;
  orders.user_id backfill from logs.
- Disable deploy workflow until CI/CD is set up.
- Ignore stored customer email and service attachment uploads.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-15 19:58:36 +07:00

96 lines
3.8 KiB
PHP

<?php
namespace App\Http\Controllers\Api\Admin\IpPool\Concerns;
use App\Models\Customer;
use App\Models\IpPool;
use App\Services\IpPool\IpAllocationService;
use App\Support\MailAttachments;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
trait ManagesIpPoolAllocation
{
public function allocationPreview(Request $request, IpAllocationService $allocation): JsonResponse
{
$data = $this->validateAllocation($request);
return response()->json([
'error' => false,
'data' => $allocation->previewAllocation($data['ids'], $this->allocationCustomer($data), $data),
]);
}
public function allocate(Request $request, IpAllocationService $allocation): JsonResponse
{
$data = $this->validateAllocation($request);
$count = $allocation->allocate($data['ids'], $this->allocationCustomer($data), $data, $request->user('admin'));
return response()->json(['error' => false, 'message' => "Đã cấp phát {$count} IP và gửi email cho khách hàng"]);
}
public function revocationPreview(Request $request, IpAllocationService $allocation): JsonResponse
{
$data = $this->validateRevocation($request);
return response()->json(['error' => false, 'data' => $allocation->previewRevocation($data['ids'], $data)]);
}
public function revoke(Request $request, IpAllocationService $allocation): JsonResponse
{
$data = $this->validateRevocation($request, withRecipients: true);
$recipients = collect($data['recipients'])->keyBy('key')->all();
$count = $allocation->revoke($data['ids'], $data, $recipients, $request->user('admin'));
return response()->json(['error' => false, 'message' => "Đã thu hồi {$count} IP và gửi email thông báo"]);
}
public function logs(int $id): JsonResponse
{
$pool = IpPool::withTrashed()->findOrFail($id);
return response()->json([
'error' => false,
'data' => $pool->logs()->with('user:id,username,fullname')->limit(200)->get(),
]);
}
private function validateAllocation(Request $request): array
{
return $request->validate([
'ids' => 'required|array|min:1|max:1000',
'ids.*' => 'integer',
'customer_uuid' => 'required|string|exists:customers,uuid',
'to' => ['required', 'string', 'max:1000', $this->multiEmailRule()],
'cc' => ['nullable', 'string', 'max:1000', $this->multiEmailRule()],
'date' => 'required|date',
'service_name' => 'nullable|string|max:255',
'services' => 'nullable|array',
'services.*' => 'nullable|string|max:255',
'note' => 'nullable|string|max:2000',
'subject' => 'nullable|string|max:255',
...MailAttachments::rules(),
]);
}
private function validateRevocation(Request $request, bool $withRecipients = false): array
{
return $request->validate([
'ids' => 'required|array|min:1|max:1000',
'ids.*' => 'integer',
'date' => 'required|date',
'note' => 'nullable|string|max:2000',
'recipients' => $withRecipients ? 'required|array|min:1' : 'prohibited',
'recipients.*.key' => 'required|string',
'recipients.*.to' => ['nullable', 'string', 'max:1000', $this->multiEmailRule()],
'recipients.*.cc' => ['nullable', 'string', 'max:1000', $this->multiEmailRule()],
'recipients.*.subject' => 'nullable|string|max:255',
]);
}
private function allocationCustomer(array $data): Customer
{
return Customer::where('uuid', $data['customer_uuid'])->firstOrFail();
}
}