Refactor code structure for improved readability and maintainability
This commit is contained in:
@@ -6,12 +6,14 @@ use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Api\Client\ClientAuthRequest;
|
||||
use App\Http\Requests\Api\Client\ClientChangePasswordRequest;
|
||||
use App\Http\Requests\Api\Client\ClientRegisterRequest;
|
||||
use App\Http\Requests\Api\Client\ClientUpdateCccdRequest;
|
||||
use App\Http\Requests\Api\Client\ClientUpdateRequest;
|
||||
use App\Http\Resources\Api\Client\ClientResource;
|
||||
use App\Services\ActivityLogger;
|
||||
use App\Services\Api\Client\ClientAuthService;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Tymon\JWTAuth\Facades\JWTAuth;
|
||||
use Tymon\JWTAuth\Exceptions\JWTException;
|
||||
|
||||
@@ -467,6 +469,43 @@ class ClientController extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
public function updateCccd(ClientUpdateCccdRequest $request)
|
||||
{
|
||||
$user = auth('client')->user();
|
||||
|
||||
$oldValues = array_intersect_key($user->toArray(), $request->validated());
|
||||
|
||||
$user->identity_number = $request->validated('cmnd');
|
||||
$user->front_identity_number = Storage::disk('public')->url(
|
||||
$request->file('cmnd_before')->store('cccd', 'public')
|
||||
);
|
||||
$user->back_identity_number = Storage::disk('public')->url(
|
||||
$request->file('cmnd_after')->store('cccd', 'public')
|
||||
);
|
||||
|
||||
$dirty = $user->getDirty();
|
||||
$user->save();
|
||||
|
||||
if (!empty($dirty)) {
|
||||
$this->activityLogger->log(
|
||||
event: 'cccd_update',
|
||||
logName: 'Customer',
|
||||
description: "Customer {$user->email} cập nhật chứng minh thư",
|
||||
subject: $user,
|
||||
oldValues: array_intersect_key($oldValues, $dirty),
|
||||
newValues: $dirty,
|
||||
);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => 'Cập nhật chứng minh thư thành công',
|
||||
'data' => [
|
||||
'client' => new ClientResource($user->loadCount(['vps', 'hosting', 'domain'])),
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function changePassword(ClientChangePasswordRequest $request)
|
||||
{
|
||||
$user = auth('client')->user();
|
||||
|
||||
@@ -103,7 +103,7 @@ class TicketController extends Controller
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => 'Ticket đã được tạo thành công.',
|
||||
'message' => 'Ticket đã được khởi tạo.',
|
||||
'data' => new TicketResource($ticket),
|
||||
], 201);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Api\Client;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class ClientUpdateCccdRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return auth('client')->check();
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'cmnd' => 'required|string|max:50',
|
||||
'cmnd_before' => 'required|image|max:5120',
|
||||
'cmnd_after' => 'required|image|max:5120',
|
||||
];
|
||||
}
|
||||
|
||||
public function messages(): array
|
||||
{
|
||||
return [
|
||||
'cmnd.required' => 'Số CMT/CCCD là bắt buộc',
|
||||
'cmnd_before.required' => 'Ảnh mặt trước là bắt buộc',
|
||||
'cmnd_before.image' => 'Ảnh mặt trước phải là hình ảnh',
|
||||
'cmnd_after.required' => 'Ảnh mặt sau là bắt buộc',
|
||||
'cmnd_after.image' => 'Ảnh mặt sau phải là hình ảnh',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -31,8 +31,8 @@ class ClientResource extends JsonResource
|
||||
'date' => $this->created_at ? $this->created_at->format('Y-m-d H:i:s') : null,
|
||||
'company' => $this->company ?? '',
|
||||
'avatar' => $this->avatar,
|
||||
'cmnd_after' => $this->front_identity_number,
|
||||
'cmnd_before' => $this->back_identity_number,
|
||||
'cmnd_before' => $this->front_identity_number,
|
||||
'cmnd_after' => $this->back_identity_number,
|
||||
'cmnd' => $this->identity_number,
|
||||
'fb_link' => null,
|
||||
'active_2fa' => 0,
|
||||
|
||||
@@ -8,6 +8,8 @@ use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class InvoiceDetailResource extends JsonResource
|
||||
{
|
||||
use ResolvesPaymentMethod;
|
||||
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
@@ -20,7 +22,7 @@ class InvoiceDetailResource extends JsonResource
|
||||
'discount_amount' => (float) ($this->discount_amount ?? 0),
|
||||
'tax' => (float) $this->tax,
|
||||
'total' => (float) $this->total,
|
||||
'payment_method' => $this->payment_method,
|
||||
'payment_method' => $this->resolvePaymentMethod($this->resource),
|
||||
'auto_renew' => (bool) $this->auto_renew,
|
||||
'created_at' => $this->created_at?->toISOString(),
|
||||
'due_date' => $this->due_date?->toISOString(),
|
||||
|
||||
@@ -9,6 +9,8 @@ use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class OrderDetailResource extends JsonResource
|
||||
{
|
||||
use ResolvesPaymentMethod;
|
||||
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
@@ -33,6 +35,7 @@ class OrderDetailResource extends JsonResource
|
||||
'total' => (float) $this->invoice->total,
|
||||
'due_date' => $this->invoice->due_date?->toISOString(),
|
||||
'paid_at' => $this->invoice->paid_at?->toISOString(),
|
||||
'payment_method' => $this->resolvePaymentMethod($this->invoice),
|
||||
]),
|
||||
'items' => $this->whenLoaded('orderItems') ? OrderItemResource::collection($this->orderItems) : [],
|
||||
];
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources\Api\Service\Order;
|
||||
|
||||
use App\Models\Invoice;
|
||||
|
||||
trait ResolvesPaymentMethod
|
||||
{
|
||||
/** invoice.payment_method holds a code (balance/credit/transfer/...), not always a Payment row id. */
|
||||
private const METHOD_LABELS = [
|
||||
'balance' => 'Số dư tài khoản',
|
||||
'credit' => 'Số dư tài khoản',
|
||||
'transfer' => 'Chuyển khoản ngân hàng',
|
||||
'vnpay' => 'VNPay',
|
||||
'momo' => 'Momo',
|
||||
'bank_transfer' => 'Chuyển khoản ngân hàng',
|
||||
'admin' => 'Xác nhận thủ công',
|
||||
];
|
||||
|
||||
private function resolvePaymentMethod(Invoice $invoice): ?array
|
||||
{
|
||||
$method = $invoice->payment_method;
|
||||
if (! $method) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (ctype_digit((string) $method) && $invoice->payment) {
|
||||
return [
|
||||
'code' => null,
|
||||
'name' => $invoice->payment->name,
|
||||
'bank_name' => $invoice->payment->bankName,
|
||||
'logo' => $invoice->payment->logo,
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'code' => $method,
|
||||
'name' => self::METHOD_LABELS[$method] ?? $method,
|
||||
'bank_name' => null,
|
||||
'logo' => null,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -343,12 +343,41 @@ class vCenter
|
||||
// ─── Legacy VPS Direct SSH ────────────────────────────────────────────────
|
||||
// These methods use direct SSH to guest VM, not via jumphost.
|
||||
|
||||
/** Look up host/user/password for a legacy target from infra_management, decrypting the stored password. */
|
||||
private function resolveLegacyInfra(string $type): ?array
|
||||
{
|
||||
$infra = \App\Models\InfraManagement::where('type', $type)
|
||||
->where('status', true)
|
||||
->first();
|
||||
|
||||
if (!$infra) {
|
||||
Log::error("Legacy VPS lookup failed: no active infra_management row for type={$type}.");
|
||||
return null;
|
||||
}
|
||||
|
||||
return [
|
||||
'ip' => $infra->host,
|
||||
'user' => $infra->username,
|
||||
'password' => \App\Http\Facade\SignRSA::DecryptFunc($infra->password),
|
||||
];
|
||||
}
|
||||
|
||||
protected function loginVPS(array|null $data = []): ?SSH2
|
||||
{
|
||||
try {
|
||||
$user = $data['user'] ?? 'root';
|
||||
$ip = $data['ip'] ?? '103.42.58.37';
|
||||
$password = $data['password'] ?? 'TGS@1232';
|
||||
$ip = $data['ip'] ?? null;
|
||||
$user = $data['user'] ?? null;
|
||||
$password = $data['password'] ?? null;
|
||||
|
||||
if ($ip === null || $user === null || $password === null) {
|
||||
$infra = $this->resolveLegacyInfra('legacy_vps');
|
||||
if (!$infra) {
|
||||
throw new Exception('No legacy VPS credentials available (data not provided and no active infra_management row).');
|
||||
}
|
||||
$ip ??= $infra['ip'];
|
||||
$user ??= $infra['user'];
|
||||
$password ??= $infra['password'];
|
||||
}
|
||||
|
||||
$this->process = new SSH2($ip, 22, 10);
|
||||
if (!$this->process->login($user, $password)) {
|
||||
@@ -362,13 +391,19 @@ class vCenter
|
||||
}
|
||||
}
|
||||
|
||||
public function change_password_linux(
|
||||
array $data = ['ip' => '10.10.10.173', 'user' => 'admin', 'password' => 'TGS@123', 'new_password' => ''],
|
||||
): bool {
|
||||
public function change_password_linux(array $data = ['new_password' => '']): bool
|
||||
{
|
||||
try {
|
||||
if (empty($data['new_password'])) {
|
||||
throw new Exception('New password cannot be empty.');
|
||||
}
|
||||
if (!isset($data['ip'], $data['user'], $data['password'])) {
|
||||
$infra = $this->resolveLegacyInfra('jumphost');
|
||||
if (!$infra) {
|
||||
throw new Exception('No jumphost credentials available in infra_management.');
|
||||
}
|
||||
$data += $infra;
|
||||
}
|
||||
$this->loginVPS($data);
|
||||
if (!$this->process) {
|
||||
throw new Exception('Cannot login to VPS.');
|
||||
@@ -383,18 +418,19 @@ class vCenter
|
||||
}
|
||||
}
|
||||
|
||||
public function change_password_windows(
|
||||
array $data = [
|
||||
'ip' => '103.42.58.37',
|
||||
'user' => 'administrator',
|
||||
'password' => 'TGS@123',
|
||||
'new_password' => '',
|
||||
],
|
||||
): bool {
|
||||
public function change_password_windows(array $data = ['new_password' => '']): bool
|
||||
{
|
||||
try {
|
||||
if (empty($data['new_password'])) {
|
||||
throw new Exception('New password cannot be empty.');
|
||||
}
|
||||
if (!isset($data['ip'], $data['user'], $data['password'])) {
|
||||
$infra = $this->resolveLegacyInfra('legacy_vps');
|
||||
if (!$infra) {
|
||||
throw new Exception('No legacy VPS credentials available in infra_management.');
|
||||
}
|
||||
$data += $infra;
|
||||
}
|
||||
$this->loginVPS($data);
|
||||
if (!$this->process) {
|
||||
throw new Exception('Cannot login to VPS.');
|
||||
|
||||
Reference in New Issue
Block a user