feat: implement system-wide activity logging with database persistence, repository patterns, and admin controller access.
This commit is contained in:
@@ -19,6 +19,7 @@ use App\Models\Order;
|
||||
use App\Models\Product;
|
||||
use App\Models\Service;
|
||||
use App\Repositories\Api\Order\OrderRepository;
|
||||
use App\Services\ActivityLogger;
|
||||
use App\Services\Api\Order\OrderService;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
@@ -31,8 +32,9 @@ use Illuminate\Support\Facades\DB;
|
||||
class OrderController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
protected OrderService $orderService,
|
||||
protected OrderRepository $orderRepo,
|
||||
protected OrderService $orderService,
|
||||
protected OrderRepository $orderRepo,
|
||||
protected ActivityLogger $activityLogger,
|
||||
) {}
|
||||
|
||||
// =========================================================================
|
||||
@@ -147,6 +149,14 @@ class OrderController extends Controller
|
||||
$order = $result['order']->load(['orderItems.product', 'invoice', 'customer']);
|
||||
$invoice = $result['invoice'];
|
||||
|
||||
$this->activityLogger->log(
|
||||
event: 'created',
|
||||
logName: 'Order',
|
||||
description: "Customer {$customer->email} tạo đơn hàng #{$order->order_num} - Tổng: " . number_format($order->total) . ' VNĐ',
|
||||
subject: $order,
|
||||
newValues: ['order_num' => $order->order_num, 'total' => $order->total, 'product_id' => $data['product_id']],
|
||||
);
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'Tạo đơn hàng thành công.',
|
||||
@@ -190,6 +200,14 @@ class OrderController extends Controller
|
||||
|
||||
$this->orderService->pay($order, $request->payment_method);
|
||||
|
||||
$this->activityLogger->log(
|
||||
event: 'payment',
|
||||
logName: 'Order',
|
||||
description: "Customer {$request->user()->email} thanh toán đơn hàng #{$order->order_num} qua {$request->payment_method}",
|
||||
subject: $order,
|
||||
newValues: ['payment_method' => $request->payment_method, 'total' => $order->total],
|
||||
);
|
||||
|
||||
return response()->json(['success' => true, 'message' => 'Thanh toán thành công.']);
|
||||
} catch (\Exception $e) {
|
||||
return $this->error($e->getMessage());
|
||||
@@ -222,6 +240,13 @@ class OrderController extends Controller
|
||||
|
||||
RetryProvisioningOrchestrator::dispatch($order->id, $request->user()->id);
|
||||
|
||||
$this->activityLogger->log(
|
||||
event: 'retry_provisioning',
|
||||
logName: 'Order',
|
||||
description: "Customer {$request->user()->email} yêu cầu khởi tạo lại dịch vụ cho đơn hàng #{$order->order_num}",
|
||||
subject: $order,
|
||||
);
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'Đã yêu cầu khởi tạo lại dịch vụ. Hệ thống sẽ kiểm tra và xử lý trong vài phút.',
|
||||
@@ -470,6 +495,14 @@ class OrderController extends Controller
|
||||
|
||||
$service->update(['auto_renew' => (bool) $validated['auto_renew']]);
|
||||
|
||||
$this->activityLogger->log(
|
||||
event: 'updated',
|
||||
logName: 'Service',
|
||||
description: "Customer {$request->user()->email} " . ($validated['auto_renew'] ? 'bật' : 'tắt') . " tự động gia hạn dịch vụ #{$service->id}",
|
||||
subject: $service,
|
||||
newValues: ['auto_renew' => (bool) $validated['auto_renew']],
|
||||
);
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'Cập nhật tự động gia hạn thành công.',
|
||||
@@ -597,6 +630,14 @@ class OrderController extends Controller
|
||||
->delay(now()->addSeconds(5));
|
||||
}
|
||||
|
||||
$this->activityLogger->log(
|
||||
event: 'renew',
|
||||
logName: 'Service',
|
||||
description: "Customer {$customer->email} gia hạn dịch vụ #{$service->id} ({$service->server_name}) - {$months} tháng - " . number_format($price) . ' VNĐ',
|
||||
subject: $service,
|
||||
newValues: ['months' => $months, 'price' => $price, 'invoice_number' => $invoice->invoice_number],
|
||||
);
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'Gia hạn thành công. Dịch vụ đã được kích hoạt lại.',
|
||||
|
||||
Reference in New Issue
Block a user