feat: Implement UUID-based customer routing, enhance order request validation, and refactor VM creation job with updated payment logic and ModuleVmware handling.

This commit is contained in:
pbao9
2026-03-12 16:30:02 +07:00
parent 508d5186db
commit dbd3cdafcd
11 changed files with 207 additions and 283 deletions
@@ -23,7 +23,6 @@ use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Yajra\DataTables\Facades\DataTables;
class CustomerController extends Controller
{
@@ -62,23 +61,6 @@ class CustomerController extends Controller
]);
}
public function datatable(Request $request): JsonResponse
{
$query = $this->customerRepository->filter(
$request->all(),
[],
['hosting', 'vps', 'domain'],
['*'],
true
);
return DataTables::eloquent($query)
->addColumn('status', fn($d) => $d->status)
->addColumn('action', fn($d) => $d->id)
->rawColumns(['status', 'action'])
->make(true);
}
public function store(CreateCustomerRequest $request): JsonResponse
{
$request->validated();
@@ -114,10 +96,19 @@ class CustomerController extends Controller
}
}
public function show(int $id): JsonResponse
public function show(string $uuid): JsonResponse
{
$customer = $this->customerRepository->findBy('uuid', $uuid);
if (!$customer) {
return response()->json([
'error' => true,
'message' => "Can't find customer by UUID #" . $uuid
], 404);
}
$customer = $this->customerRepository->findWithRelationships(
$id,
$customer->id,
['mail', 'file'],
[
'invoice' => [
@@ -140,7 +131,7 @@ class CustomerController extends Controller
if (!$customer) {
return response()->json([
'error' => true,
'message' => "Can't find customer by ID #" . $id
'message' => "Can't find customer by UUID #" . $uuid
], 404);
}
@@ -150,14 +141,14 @@ class CustomerController extends Controller
]);
}
public function update(int $id, CreateCustomerRequest $request): JsonResponse
public function update(string $uuid, CreateCustomerRequest $request): JsonResponse
{
$customer = $this->customerRepository->find($id);
$customer = $this->customerRepository->findBy('uuid', $uuid);
if (!$customer) {
return response()->json([
'error' => true,
'message' => "Can't find customer by ID #" . $id
'message' => "Can't find customer by UUID #" . $uuid
], 404);
}
@@ -171,13 +162,13 @@ class CustomerController extends Controller
$data['password'] = Hash::make($request->password);
}
$this->customerRepository->update($id, $data);
$this->customerRepository->update($customer->id, $data);
DB::commit();
return response()->json([
'error' => false,
'message' => 'Customer updated successfully',
'data' => new CustomerResource($this->customerRepository->find($id))
'data' => new CustomerResource($this->customerRepository->find($customer->id))
]);
} catch (\Exception $e) {
DB::rollBack();
@@ -188,22 +179,22 @@ class CustomerController extends Controller
}
}
public function destroy(int $id): JsonResponse
public function destroy(string $uuid): JsonResponse
{
$customer = $this->customerRepository->find($id);
$customer = $this->customerRepository->findBy('uuid', $uuid);
if (!$customer) {
return response()->json([
'error' => true,
'message' => "Can't find customer by ID #" . $id
'message' => "Can't find customer by UUID #" . $uuid
], 404);
}
try {
DB::beginTransaction();
Invoice::where('customer_id', $id)->delete();
Order::where('customer_id', $id)->delete();
Transaction::where('customer_id', $id)->delete();
Invoice::where('customer_id', $customer->id)->delete();
Order::where('customer_id', $customer->id)->delete();
Transaction::where('customer_id', $customer->id)->delete();
$customer->delete();
DB::commit();
@@ -224,33 +215,32 @@ class CustomerController extends Controller
{
$query = $this->customerRepository->onlyTrashed([], ['hosting', 'vps', 'domain']);
return DataTables::eloquent($query)
->addColumn('status', fn($d) => $d->status)
->addColumn('action', fn($d) => $d->id)
->rawColumns(['status', 'action'])
->make(true);
return response()->json([
'error' => false,
'data' => CustomerResource::collection($query->get())
]);
}
public function restore(int $id): JsonResponse
public function restore(string $uuid): JsonResponse
{
$customer = $this->customerRepository->find($id);
$customer = $this->customerRepository->findBy('uuid', $uuid);
if (!$customer) {
$customer = $this->customerRepository->onlyTrashed()->find($id);
$customer = $this->customerRepository->onlyTrashed()->where('uuid', $uuid)->first();
}
if (!$customer) {
return response()->json([
'error' => true,
'message' => "Can't find customer by ID #" . $id
'message' => "Can't find customer by UUID #" . $uuid
], 404);
}
try {
DB::beginTransaction();
$customer->restore();
Invoice::withTrashed()->where('customer_id', $id)->restore();
Order::withTrashed()->where('customer_id', $id)->restore();
Transaction::withTrashed()->where('customer_id', $id)->restore();
Invoice::withTrashed()->where('customer_id', $customer->id)->restore();
Order::withTrashed()->where('customer_id', $customer->id)->restore();
Transaction::withTrashed()->where('customer_id', $customer->id)->restore();
DB::commit();
return response()->json([
@@ -266,22 +256,22 @@ class CustomerController extends Controller
}
}
public function forceDelete(int $id): JsonResponse
public function forceDelete(string $uuid): JsonResponse
{
$customer = $this->customerRepository->onlyTrashed()->find($id);
$customer = $this->customerRepository->onlyTrashed()->where('uuid', $uuid)->first();
if (!$customer) {
return response()->json([
'error' => true,
'message' => "Can't find customer by ID #" . $id
'message' => "Can't find customer by UUID #" . $uuid
], 404);
}
try {
DB::beginTransaction();
Invoice::withTrashed()->where('customer_id', $id)->forceDelete();
Order::withTrashed()->where('customer_id', $id)->forceDelete();
Transaction::withTrashed()->where('customer_id', $id)->forceDelete();
Invoice::withTrashed()->where('customer_id', $customer->id)->forceDelete();
Order::withTrashed()->where('customer_id', $customer->id)->forceDelete();
Transaction::withTrashed()->where('customer_id', $customer->id)->forceDelete();
$customer->forceDelete();
DB::commit();
@@ -298,14 +288,14 @@ class CustomerController extends Controller
}
}
public function resetPassword(Request $request, int $id): JsonResponse
public function resetPassword(Request $request, string $uuid): JsonResponse
{
$customer = $this->customerRepository->find($id);
$customer = $this->customerRepository->findBy('uuid', $uuid);
if (!$customer) {
return response()->json([
'error' => true,
'message' => "Can't find customer by ID #" . $id
'message' => "Can't find customer by UUID #" . $uuid
], 404);
}
@@ -313,7 +303,7 @@ class CustomerController extends Controller
try {
DB::beginTransaction();
$this->customerRepository->update($id, ['password' => Hash::make($password)]);
$this->customerRepository->update($customer->id, ['password' => Hash::make($password)]);
DB::commit();
return response()->json([
@@ -330,19 +320,19 @@ class CustomerController extends Controller
}
}
public function addCredit(Request $request, int $id): JsonResponse
public function addCredit(Request $request, string $uuid): JsonResponse
{
$request->validate([
'amount' => 'required|numeric|min:1',
'action' => 'required|in:+,-'
]);
$customer = $this->customerRepository->find($id);
$customer = $this->customerRepository->findBy('uuid', $uuid);
if (!$customer) {
return response()->json([
'error' => true,
'message' => "Can't find customer by ID #" . $id
'message' => "Can't find customer by UUID #" . $uuid
], 404);
}
@@ -352,12 +342,12 @@ class CustomerController extends Controller
$amount = (int) $request->amount;
if ($request->action === "+") {
$this->customerRepository->update($id, [
$this->customerRepository->update($customer->id, [
'credit' => (int) $customer->credit + $amount,
'total' => (int) $customer->total + $amount,
]);
} else {
$this->customerRepository->update($id, [
$this->customerRepository->update($customer->id, [
'credit' => (int) $customer->credit - $amount,
'total' => (int) $customer->total - $amount,
]);
@@ -378,20 +368,20 @@ class CustomerController extends Controller
}
}
public function getProducts(int $id): JsonResponse
public function getProducts(string $uuid): JsonResponse
{
$customer = $this->customerRepository->find($id);
$customer = $this->customerRepository->findBy('uuid', $uuid);
if (!$customer) {
return response()->json([
'error' => true,
'message' => "Can't find customer by ID #" . $id
'message' => "Can't find customer by UUID #" . $uuid
], 404);
}
$hostingServices = $this->moduleHostingRepository->filter(['customer_id' => $id], ['server']);
$hostingServices = $this->moduleHostingRepository->filter(['customer_id' => $customer->id], ['server']);
$vpsServices = $this->vmwareRepository->filter(
['customer_id' => $id],
['customer_id' => $customer->id],
['ipaddress.host.datacenter.vCenter', 'os.osGroup']
);
@@ -421,19 +411,19 @@ class CustomerController extends Controller
]);
}
public function getInvoices(int $id, Request $request): JsonResponse
public function getInvoices(string $uuid, Request $request): JsonResponse
{
$customer = $this->customerRepository->find($id);
$customer = $this->customerRepository->findBy('uuid', $uuid);
if (!$customer) {
return response()->json([
'error' => true,
'message' => "Can't find customer by ID #" . $id
'message' => "Can't find customer by UUID #" . $uuid
], 404);
}
$invoices = $this->invoiceRepository->filter(
['customer_id' => $id],
['customer_id' => $customer->id],
['payment']
);
@@ -443,18 +433,18 @@ class CustomerController extends Controller
]);
}
public function getTransactions(int $id, Request $request): JsonResponse
public function getTransactions(string $uuid, Request $request): JsonResponse
{
$customer = $this->customerRepository->find($id);
$customer = $this->customerRepository->findBy('uuid', $uuid);
if (!$customer) {
return response()->json([
'error' => true,
'message' => "Can't find customer by ID #" . $id
'message' => "Can't find customer by UUID #" . $uuid
], 404);
}
$transactions = $this->transactionRepository->filter(['customer_id' => $id]);
$transactions = $this->transactionRepository->filter(['customer_id' => $customer->id]);
return response()->json([
'error' => false,
@@ -462,19 +452,19 @@ class CustomerController extends Controller
]);
}
public function getDomains(int $id, Request $request): JsonResponse
public function getDomains(string $uuid, Request $request): JsonResponse
{
$customer = $this->customerRepository->find($id);
$customer = $this->customerRepository->findBy('uuid', $uuid);
if (!$customer) {
return response()->json([
'error' => true,
'message' => "Can't find customer by ID #" . $id
'message' => "Can't find customer by UUID #" . $uuid
], 404);
}
$domains = $this->moduleDomainRepository->filter(
['customer_id' => $id],
['customer_id' => $customer->id],
['customer']
);