Add EmailLogController and routes for email logs management
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers\Api\Admin\EmailLog;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Mail;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
final class EmailLogController extends Controller
|
||||
{
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$limit = min((int) $request->get('limit', $request->get('per_page', 20)), 200);
|
||||
|
||||
$query = Mail::query()->orderByDesc('created_at');
|
||||
|
||||
// sort=field_asc|field_desc
|
||||
if ($sort = $request->get('sort')) {
|
||||
$parts = explode('_', $sort);
|
||||
$dir = array_pop($parts);
|
||||
$col = implode('_', $parts);
|
||||
|
||||
$allowed = ['created_at', 'subject', 'status', 'to'];
|
||||
if (in_array($col, $allowed, true) && in_array($dir, ['asc', 'desc'], true)) {
|
||||
$query->reorder($col, $dir);
|
||||
}
|
||||
}
|
||||
|
||||
if ($status = $request->get('status')) {
|
||||
$query->where('status', $status);
|
||||
}
|
||||
if ($customerId = $request->get('customer_id')) {
|
||||
$query->where('customer_id', (int) $customerId);
|
||||
}
|
||||
if ($search = trim((string) $request->get('search', ''))) {
|
||||
$query->where(function ($q) use ($search) {
|
||||
$q->where('to', 'like', "%{$search}%")
|
||||
->orWhere('subject', 'like', "%{$search}%");
|
||||
});
|
||||
}
|
||||
|
||||
$paginator = $query->paginate($limit);
|
||||
|
||||
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
|
||||
{
|
||||
$log = Mail::find($id);
|
||||
|
||||
if (!$log) {
|
||||
return response()->json(['error' => true, 'message' => 'Không tìm thấy email log'], 404);
|
||||
}
|
||||
|
||||
return response()->json(['error' => false, 'data' => $log]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user