feat: Implement client status validation on login, refine password rules, and simplify customer resource status.

This commit is contained in:
pbao9
2026-03-10 16:27:23 +07:00
parent cf0f2cf686
commit 7fdcc01027
18 changed files with 831 additions and 12 deletions
@@ -40,14 +40,15 @@ class CustomerController extends Controller
public function index(Request $request): JsonResponse
{
$perPage = min((int) $request->get('per_page', 15), 100);
$paginator = $this->customerRepository->filter(
$perPage = min((int) $request->get('per_page', 15), 500);
$query = $this->customerRepository->filter(
$request->all(),
[],
['hosting', 'vps', 'domain'],
['*'],
false
true
);
$paginator = $query->paginate($perPage);
return response()->json([
'error' => false,
@@ -0,0 +1,271 @@
<?php
namespace App\Http\Controllers\Api\Admin\DnsManager;
use App\Http\Controllers\Controller;
use App\Repositories\DnsManager\Domain\DnsDomainRepositoryInterface;
use App\Repositories\DnsManager\Record\DnsRecordRepositoryInterface;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class DomainController extends Controller
{
public function __construct(
private readonly DnsDomainRepositoryInterface $domainRepository,
private readonly DnsRecordRepositoryInterface $recordRepository,
) {}
// ─── Domains ─────────────────────────────────────────────────────────────
public function index(Request $request): JsonResponse
{
$perPage = min((int) $request->get('per_page', 15), 500);
$filter = $request->get('filter');
$status = $request->get('status');
$query = $this->domainRepository->model
->with(['server', 'customer'])
->newQuery();
if ($filter) {
$query->where('domain_name', 'like', "%{$filter}%");
}
if ($status !== null && $status !== '') {
$query->where('is_created', (int) $status);
}
$paginator = $query->orderByDesc('id')->paginate($perPage);
$items = $paginator->getCollection()->map(fn($domain) => $this->formatDomain($domain));
return response()->json([
'error' => false,
'data' => [
'items' => $items,
'total' => $paginator->total(),
'per_page' => $paginator->perPage(),
'current_page' => $paginator->currentPage(),
'last_page' => $paginator->lastPage(),
],
]);
}
public function show(int $id): JsonResponse
{
$domain = $this->domainRepository->model
->with(['server', 'customer'])
->find($id);
if (!$domain) {
return response()->json(['error' => true, 'message' => "Không tìm thấy domain #$id"], 404);
}
return response()->json([
'error' => false,
'data' => $this->formatDomain($domain),
]);
}
public function store(Request $request): JsonResponse
{
$request->validate([
'domain_name' => 'required|string|max:255',
'dns_manager_server_id' => 'required|integer|exists:dns_manager_servers,id',
'customer_ids' => 'nullable|array',
'customer_ids.*' => 'integer|exists:customers,id',
'is_created' => 'nullable|integer|in:0,1,2',
]);
DB::beginTransaction();
try {
$domain = $this->domainRepository->create([
'domain_name' => $request->domain_name,
'dns_manager_server_id' => $request->dns_manager_server_id,
'is_created' => $request->is_created ?? 0,
]);
if (!empty($request->customer_ids)) {
$domain->customer()->sync($request->customer_ids);
}
DB::commit();
return response()->json([
'error' => false,
'data' => ['id' => $domain->id, 'domain_name' => $domain->domain_name],
], 201);
} catch (\Exception $e) {
DB::rollBack();
return response()->json(['error' => true, 'message' => $e->getMessage()], 500);
}
}
public function update(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);
}
$request->validate([
'domain_name' => 'sometimes|required|string|max:255',
'dns_manager_server_id' => 'nullable|integer|exists:dns_manager_servers,id',
'customer_ids' => 'nullable|array',
'customer_ids.*' => 'integer|exists:customers,id',
'is_created' => 'nullable|integer|in:0,1,2',
]);
DB::beginTransaction();
try {
$this->domainRepository->update($id, $request->only([
'domain_name',
'dns_manager_server_id',
'is_created',
]));
// Sync customers (empty array = detach all)
if ($request->has('customer_ids')) {
$domain->customer()->sync($request->customer_ids ?? []);
}
DB::commit();
return $this->show($id);
} catch (\Exception $e) {
DB::rollBack();
return response()->json(['error' => true, 'message' => $e->getMessage()], 500);
}
}
public function destroy(int $id): JsonResponse
{
$domain = $this->domainRepository->find($id);
if (!$domain) {
return response()->json(['error' => true, 'message' => "Không tìm thấy domain #$id"], 404);
}
DB::beginTransaction();
try {
$domain->customer()->detach();
$domain->record()->delete();
$this->domainRepository->delete($id);
DB::commit();
return response()->json(['error' => false, 'message' => 'Đã xoá domain thành công']);
} catch (\Exception $e) {
DB::rollBack();
return response()->json(['error' => true, 'message' => $e->getMessage()], 500);
}
}
// ─── DNS Records ─────────────────────────────────────────────────────────
// ─── Helpers ─────────────────────────────────────────────────────────────
private function formatDomain($domain): array
{
return [
'id' => $domain->id,
'domain_name' => $domain->domain_name,
'dns_manager_server_id' => $domain->dns_manager_server_id,
'server_name' => $domain->server?->name,
'customer_ids' => $domain->customer->pluck('id')->toArray(),
'customers' => $domain->customer->map(fn($c) => [
'id' => $c->id,
'name' => $c->fullname,
'email' => $c->email,
])->values()->toArray(),
'is_created' => $domain->is_created,
'created_at' => $domain->created_at,
'updated_at' => $domain->updated_at,
];
}
// ─── DNS Records ─────────────────────────────────────────────────────────
public function dnsIndex(int $domainId): JsonResponse
{
$domain = $this->domainRepository->find($domainId);
if (!$domain) {
return response()->json(['error' => true, 'message' => "Không tìm thấy domain #$domainId"], 404);
}
$records = $this->recordRepository->model
->where('dns_manager_domain_id', $domainId)
->orderBy('type')
->get();
return response()->json(['error' => false, 'data' => $records]);
}
public function dnsStore(int $domainId, Request $request): JsonResponse
{
$domain = $this->domainRepository->find($domainId);
if (!$domain) {
return response()->json(['error' => true, 'message' => "Không tìm thấy domain #$domainId"], 404);
}
$request->validate([
'type' => 'required|string|in:A,AAAA,CNAME,TXT,MX,NS,SRV',
'name' => 'required|string|max:255',
'value' => 'required|string',
'ttl' => 'nullable|integer|min:60',
'priority' => 'nullable|integer|min:0',
]);
$record = $this->recordRepository->create([
'dns_manager_domain_id' => $domainId,
'type' => $request->type,
'name' => $request->name,
'value' => $request->value,
'ttl' => $request->ttl ?? 300,
'priority' => $request->priority,
]);
return response()->json(['error' => false, 'data' => $record], 201);
}
public function dnsUpdate(int $domainId, int $recordId, Request $request): JsonResponse
{
$record = $this->recordRepository->model
->where('dns_manager_domain_id', $domainId)
->find($recordId);
if (!$record) {
return response()->json(['error' => true, 'message' => "Không tìm thấy bản ghi DNS #$recordId"], 404);
}
$request->validate([
'type' => 'required|string|in:A,AAAA,CNAME,TXT,MX,NS,SRV',
'name' => 'required|string|max:255',
'value' => 'required|string',
'ttl' => 'nullable|integer|min:60',
'priority' => 'nullable|integer|min:0',
]);
$this->recordRepository->update($recordId, $request->only(['type', 'name', 'value', 'ttl', 'priority']));
return response()->json(['error' => false, 'data' => $this->recordRepository->find($recordId)]);
}
public function dnsDestroy(int $domainId, int $recordId): JsonResponse
{
$record = $this->recordRepository->model
->where('dns_manager_domain_id', $domainId)
->find($recordId);
if (!$record) {
return response()->json(['error' => true, 'message' => "Không tìm thấy bản ghi DNS #$recordId"], 404);
}
$this->recordRepository->delete($recordId);
return response()->json(['error' => false, 'message' => 'Đã xoá bản ghi DNS']);
}
}
@@ -0,0 +1,134 @@
<?php
namespace App\Http\Controllers\Api\Admin\DnsManager;
use App\Http\Controllers\Controller;
use App\Repositories\DnsManager\Server\DnsServerRepositoryInterface;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\DB;
class ServerController extends Controller
{
public function __construct(
private readonly DnsServerRepositoryInterface $serverRepository,
) {}
public function index(Request $request): JsonResponse
{
$perPage = min((int) $request->get('per_page', 15), 500);
$filter = $request->get('filter');
$status = $request->get('status');
$query = $this->serverRepository->model->newQuery();
if ($filter) {
$query->where(function ($q) use ($filter) {
$q->where('name', 'like', "%{$filter}%")
->orWhere('ip_address', 'like', "%{$filter}%");
});
}
if ($status) {
$query->where('status', $status);
}
$paginator = $query->paginate($perPage);
return response()->json([
'error' => false,
'data' => [
'items' => $paginator->items(),
'total' => $paginator->total(),
'per_page' => $paginator->perPage(),
'current_page' => $paginator->currentPage(),
'last_page' => $paginator->lastPage(),
],
]);
}
public function show(int $id): JsonResponse
{
$server = $this->serverRepository->find($id);
if (!$server) {
return response()->json(['error' => true, 'message' => "Không tìm thấy server #$id"], 404);
}
return response()->json(['error' => false, 'data' => $server]);
}
public function store(Request $request): JsonResponse
{
$request->validate([
'name' => 'required|string|max:255',
'ip_address' => 'required|string|max:255',
'username' => 'required|string|max:255',
'password' => 'required|string|max:255',
'api_key' => 'nullable|string',
'status' => 'nullable|string|in:activated,Stopped',
]);
DB::beginTransaction();
try {
$data = $request->all();
$data['password'] = Crypt::encryptString($request->password);
$server = $this->serverRepository->create($data);
DB::commit();
return response()->json(['error' => false, 'data' => $server], 201);
} catch (\Exception $e) {
DB::rollBack();
return response()->json(['error' => true, 'message' => $e->getMessage()], 500);
}
}
public function update(int $id, Request $request): JsonResponse
{
$server = $this->serverRepository->find($id);
if (!$server) {
return response()->json(['error' => true, 'message' => "Không tìm thấy server #$id"], 404);
}
$request->validate([
'name' => 'required|string|max:255',
'ip_address' => 'required|string|max:255',
'username' => 'required|string|max:255',
'password' => 'nullable|string|max:255',
'api_key' => 'nullable|string',
'status' => 'nullable|string|in:activated,Stopped',
]);
DB::beginTransaction();
try {
$data = $request->except(['password']);
if (!empty($request->password)) {
$data['password'] = Crypt::encryptString($request->password);
}
$this->serverRepository->update($id, $data);
DB::commit();
return response()->json(['error' => false, 'data' => $this->serverRepository->find($id)]);
} catch (\Exception $e) {
DB::rollBack();
return response()->json(['error' => true, 'message' => $e->getMessage()], 500);
}
}
public function destroy(int $id): JsonResponse
{
$server = $this->serverRepository->find($id);
if (!$server) {
return response()->json(['error' => true, 'message' => "Không tìm thấy server #$id"], 404);
}
$this->serverRepository->delete($id);
return response()->json(['error' => false, 'message' => 'Đã xoá server thành công']);
}
}
@@ -5,14 +5,18 @@ namespace App\Http\Controllers\Api\Admin\Invoice;
use App\Http\Controllers\Controller;
use App\Http\Requests\InvoiceRequest;
use App\Http\Resources\Api\Admin\InvoiceResource;
use App\Mail\InvoiceNotificationMail;
use App\Models\Customer;
use App\Models\Payment;
use App\Repositories\Invoice\InvoiceRepositoryInterface;
use App\Repositories\InvoiceAttachment\InvoiceAttachmentRepositoryInterface;
use App\Repositories\InvoiceItem\InvoiceItemRepositoryInterface;
use App\Repositories\Order\OrderRepositoryInterface;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Storage;
use Yajra\DataTables\Facades\DataTables;
class InvoiceController extends Controller
@@ -21,6 +25,7 @@ class InvoiceController extends Controller
private readonly InvoiceRepositoryInterface $invoiceRepository,
private readonly OrderRepositoryInterface $orderRepository,
private readonly InvoiceItemRepositoryInterface $invoiceItemRepository,
private readonly InvoiceAttachmentRepositoryInterface $attachmentRepository,
) {}
public function index(Request $request): JsonResponse
@@ -253,4 +258,106 @@ class InvoiceController extends Controller
], 500);
}
}
// ─── Attachments ────────────────────────────────────────────────────────────
public function listAttachments(int $id): JsonResponse
{
$invoice = $this->invoiceRepository->find($id);
if (!$invoice) {
return response()->json(['error' => true, 'message' => "Can't find invoice #$id"], 404);
}
$attachments = $this->attachmentRepository->getByInvoice($id);
return response()->json([
'error' => false,
'data' => $attachments,
]);
}
public function uploadAttachment(Request $request, int $id): JsonResponse
{
$invoice = $this->invoiceRepository->find($id);
if (!$invoice) {
return response()->json(['error' => true, 'message' => "Can't find invoice #$id"], 404);
}
$request->validate([
'file' => 'required|file|mimes:jpg,jpeg,png,gif,pdf,doc,docx,xls,xlsx|max:10240',
]);
$file = $request->file('file');
$path = $file->store("invoice-attachments/{$id}", 'public');
$uploader = $request->user()?->username ?? $request->user()?->name ?? 'admin';
$attachment = $this->attachmentRepository->create([
'invoice_id' => $id,
'filename' => $file->getClientOriginalName(),
'path' => $path,
'disk' => 'public',
'mime_type' => $file->getMimeType(),
'size' => $file->getSize(),
'uploaded_by' => $uploader,
]);
return response()->json([
'error' => false,
'message' => 'File uploaded successfully',
'data' => $attachment,
], 201);
}
public function deleteAttachment(int $id, int $attachmentId): JsonResponse
{
$invoice = $this->invoiceRepository->find($id);
if (!$invoice) {
return response()->json(['error' => true, 'message' => "Can't find invoice #$id"], 404);
}
$attachment = $this->attachmentRepository->find($attachmentId);
if (!$attachment || $attachment->invoice_id !== $id) {
return response()->json(['error' => true, 'message' => 'Attachment not found'], 404);
}
$this->attachmentRepository->deleteWithFile($attachmentId);
return response()->json([
'error' => false,
'message' => 'Attachment deleted',
]);
}
// ─── Send Mail ───────────────────────────────────────────────────────────────
public function sendMail(Request $request, int $id): JsonResponse
{
$request->validate([
'message' => 'nullable|string|max:1000',
]);
$invoice = $this->invoiceRepository->find($id);
if (!$invoice) {
return response()->json(['error' => true, 'message' => "Can't find invoice #$id"], 404);
}
$invoice->load('customer');
$customer = $invoice->customer;
if (!$customer || !$customer->email) {
return response()->json(['error' => true, 'message' => 'Customer email not found'], 422);
}
$attachments = $this->attachmentRepository->getByInvoice($id);
$companyName = setting('general.company_name') ?: config('app.name');
$message = $request->get('message', "Vui lòng xem thông tin hóa đơn #{$invoice->order_num} của bạn.");
Mail::to($customer->email)->queue(
new InvoiceNotificationMail($invoice, $attachments, $customer->fullname, $message, $companyName)
);
return response()->json([
'error' => false,
'message' => "Email đã được gửi tới {$customer->email}",
]);
}
}