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>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
ea5697cb81
commit
46aa138eec
@@ -0,0 +1,95 @@
|
||||
<?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();
|
||||
}
|
||||
}
|
||||
@@ -2,15 +2,13 @@
|
||||
|
||||
namespace App\Http\Controllers\Api\Admin\IpPool\Concerns;
|
||||
|
||||
use App\Imports\IpPoolImport;
|
||||
use App\Services\IpPool\IpPoolSheetImporter;
|
||||
use App\Models\IpPool;
|
||||
use App\Models\ModuleIpAddress;
|
||||
use App\Models\ModuleNetwork;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Maatwebsite\Excel\Facades\Excel;
|
||||
use Maatwebsite\Excel\Validators\ValidationException;
|
||||
|
||||
trait ManagesIpPoolBulkActions
|
||||
{
|
||||
@@ -94,39 +92,24 @@ trait ManagesIpPoolBulkActions
|
||||
]);
|
||||
}
|
||||
|
||||
public function import(Request $request): JsonResponse
|
||||
public function importPreview(Request $request, IpPoolSheetImporter $importer): JsonResponse
|
||||
{
|
||||
$request->validate([
|
||||
'file' => 'required|file|mimes:csv,txt|max:10240',
|
||||
]);
|
||||
$request->validate(['file' => self::IMPORT_FILE_RULE]);
|
||||
|
||||
$countBefore = IpPool::withTrashed()->count();
|
||||
return response()->json(['error' => false, 'data' => $importer->preview($request->file('file'))]);
|
||||
}
|
||||
|
||||
try {
|
||||
Excel::import(new IpPoolImport(), $request->file('file'));
|
||||
} catch (ValidationException $e) {
|
||||
$errors = [];
|
||||
foreach ($e->failures() as $failure) {
|
||||
$errors[] = 'Dòng ' . $failure->row() . ': ' . implode(', ', $failure->errors());
|
||||
}
|
||||
public function import(Request $request, IpPoolSheetImporter $importer): JsonResponse
|
||||
{
|
||||
$request->validate(['file' => self::IMPORT_FILE_RULE]);
|
||||
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => 'Import thất bại',
|
||||
'errors' => $errors,
|
||||
], 422);
|
||||
} catch (\Exception $e) {
|
||||
return response()->json(['error' => true, 'message' => 'Import thất bại: ' . $e->getMessage()], 500);
|
||||
}
|
||||
|
||||
$countAfter = IpPool::withTrashed()->count();
|
||||
|
||||
IpPool::resyncAllContacts();
|
||||
$summary = $importer->import($request->file('file'));
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => 'Import thành công',
|
||||
'created' => max(0, $countAfter - $countBefore),
|
||||
'message' => "Đã thêm {$summary['create']}, cập nhật {$summary['update']} IP"
|
||||
. ($summary['error_count'] ? ", bỏ qua {$summary['error_count']} dòng lỗi" : ''),
|
||||
'data' => $summary,
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -142,7 +125,7 @@ trait ManagesIpPoolBulkActions
|
||||
$updated = 0;
|
||||
$hostByNet = ModuleNetwork::pluck('module_host_id', 'id');
|
||||
|
||||
IpPool::chunkById(500, function ($pools) use (&$created, &$updated, $hostByNet) {
|
||||
IpPool::whereNotNull('module_network_id')->chunkById(500, function ($pools) use (&$created, &$updated, $hostByNet) {
|
||||
foreach ($pools as $pool) {
|
||||
$hostId = $hostByNet[$pool->module_network_id] ?? null;
|
||||
$netmask = $pool->netmask ?: '255.255.255.0';
|
||||
|
||||
@@ -6,14 +6,17 @@ use App\Http\Controllers\Controller;
|
||||
use App\Models\IpPool;
|
||||
use App\Models\ModuleIpAddress;
|
||||
use App\Models\ModuleNetwork;
|
||||
use App\Services\IpPool\IpPoolUsageResolver;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use App\Http\Controllers\Api\Admin\IpPool\Concerns\ManagesIpPoolAllocation;
|
||||
use App\Http\Controllers\Api\Admin\IpPool\Concerns\ManagesIpPoolBulkActions;
|
||||
use App\Http\Controllers\Api\Admin\IpPool\Concerns\ReadsIpPoolUsage;
|
||||
|
||||
class IpPoolController extends Controller
|
||||
{
|
||||
use ManagesIpPoolAllocation;
|
||||
use ManagesIpPoolBulkActions;
|
||||
use ReadsIpPoolUsage;
|
||||
|
||||
@@ -22,13 +25,15 @@ class IpPoolController extends Controller
|
||||
* status/service_id which have defaulting + module_ip_address mirror logic below.
|
||||
*/
|
||||
private const EDITABLE_FIELDS = [
|
||||
'owner_type', 'ip_role', 'parent_ip', 'customer_name', 'service_name',
|
||||
'allocated_at', 'revoked_at', 'allocation_status',
|
||||
'ip_block', 'owner_type', 'ip_role', 'parent_ip', 'customer_name', 'service_name',
|
||||
'allocated_at', 'revoked_at',
|
||||
'contact_email', 'contact_cc', 'contact_phone',
|
||||
'monthly_cost', 'selling_price', 'additional_info',
|
||||
'last_email_type', 'last_email_at', 'updated_by', 'note',
|
||||
];
|
||||
|
||||
private const IMPORT_FILE_RULE = 'required|file|extensions:xlsx,xls,csv|max:20480';
|
||||
|
||||
/**
|
||||
* contact_email/contact_cc may hold multiple addresses separated by comma/semicolon.
|
||||
*/
|
||||
@@ -44,14 +49,14 @@ class IpPoolController extends Controller
|
||||
};
|
||||
}
|
||||
|
||||
public function index(Request $request): JsonResponse
|
||||
public function index(Request $request, IpPoolUsageResolver $usageResolver): JsonResponse
|
||||
{
|
||||
// 'all' lets the admin grid load the full dataset once and filter/sort client-side.
|
||||
$perPage = $request->boolean('all') ? 20000 : min((int) $request->get('per_page', 15), 500);
|
||||
$filter = $request->get('filter');
|
||||
$networkId = $request->get('network_id');
|
||||
|
||||
$query = IpPool::with(['network:id,name,type,network', 'service:id,server_name,status,customer_id']);
|
||||
$query = IpPool::with(['network:id,name,type,network', 'service:id,server_name,status,customer_id', 'customer:id,uuid,fullname']);
|
||||
|
||||
if ($filter) {
|
||||
$query->where('ip_address', 'like', "%{$filter}%");
|
||||
@@ -62,61 +67,12 @@ class IpPoolController extends Controller
|
||||
}
|
||||
|
||||
$paginator = $query->paginate($perPage);
|
||||
$usage = $usageResolver->forPools($paginator->getCollection());
|
||||
|
||||
// Enrich each item with usage info
|
||||
$usedIps = ModuleIpAddress::whereIn('ip', $paginator->pluck('ip_address')->toArray())
|
||||
->where('status', 1)
|
||||
->with('host:id,name')
|
||||
->select(['id', 'ip', 'vm_name', 'module_host_id', 'module_network_id', 'status'])
|
||||
->get()
|
||||
->keyBy('ip');
|
||||
|
||||
// Get vmware info for used IPs
|
||||
$vmwareMap = [];
|
||||
if ($usedIps->isNotEmpty()) {
|
||||
$vmwares = DB::table('module_vmware as mv')
|
||||
->join('module_ip_address as mia', 'mv.module_ip_address', '=', 'mia.id')
|
||||
->whereIn('mia.ip', $usedIps->keys()->toArray())
|
||||
->leftJoin('services as s', 's.id', '=', 'mv.service_id')
|
||||
->leftJoin('customers as c', 'c.id', '=', 'mv.customer_id')
|
||||
->select([
|
||||
'mia.ip',
|
||||
'mv.name as vm_name',
|
||||
'mv.id as vm_id',
|
||||
's.id as service_id',
|
||||
's.status as service_status',
|
||||
'c.fullname',
|
||||
'c.email',
|
||||
])
|
||||
->get();
|
||||
|
||||
foreach ($vmwares as $vm) {
|
||||
$vmwareMap[$vm->ip] = $vm;
|
||||
}
|
||||
}
|
||||
|
||||
$items = $paginator->map(function (IpPool $pool) use ($usedIps, $vmwareMap) {
|
||||
$data = $pool->toArray();
|
||||
$ip = $pool->ip_address;
|
||||
|
||||
if (isset($usedIps[$ip])) {
|
||||
$mia = $usedIps[$ip];
|
||||
$vm = $vmwareMap[$ip] ?? null;
|
||||
$data['usage'] = [
|
||||
'assigned' => true,
|
||||
'vm_name' => $vm?->vm_name ?? $mia->vm_name,
|
||||
'vm_id' => $vm?->vm_id ?? null,
|
||||
'service_id' => $vm?->service_id ?? null,
|
||||
'service_status' => $vm?->service_status ?? null,
|
||||
'customer_name' => $vm?->fullname ?? null,
|
||||
'customer_email' => $vm?->email ?? null,
|
||||
];
|
||||
} else {
|
||||
$data['usage'] = ['assigned' => false];
|
||||
}
|
||||
|
||||
return $data;
|
||||
});
|
||||
$items = $paginator->map(fn (IpPool $pool) => [
|
||||
...$pool->toArray(),
|
||||
'usage' => $usage[$pool->ip_address] ?? ['assigned' => false],
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
|
||||
Reference in New Issue
Block a user