feat(admin): star/tags marks, service product-type filter, campaign bulk actions, SMTP test
- Star (is_starred) and colored tags for services, customers, orders, invoices:
Markable trait, tags/taggables tables, PATCH administrator/marks/{type}/{key},
tag CRUD, list filters starred / tag_ids[]
- GET administrator/services accepts product_type (falls back to product type
for legacy rows without services.type)
- Mail campaigns: bulk cancel / retry failed / delete; cancelled campaigns are
skipped by SendMailCampaignRecipient
- POST administrator/settings/test-smtp sends a test email through the saved
mail transport
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
46aa138eec
commit
40bf6b54f9
@@ -53,7 +53,7 @@ class CustomerController extends Controller
|
||||
{
|
||||
$perPage = min((int) $request->get('per_page', 15), 500);
|
||||
$paginator = $this->customerRepository
|
||||
->filter($request->all(), [], ['hosting', 'vps', 'domain'], ['*'], true)
|
||||
->filter($request->all(), ['tags'], ['hosting', 'vps', 'domain'], ['*'], true)
|
||||
->paginate($perPage);
|
||||
|
||||
return $this->paginatedResponse($paginator);
|
||||
|
||||
@@ -49,6 +49,7 @@ class InvoiceController extends Controller
|
||||
'customer',
|
||||
'order',
|
||||
...OrderItemServiceResource::relationsFor('order.orderItems'),
|
||||
'tags',
|
||||
],
|
||||
[],
|
||||
['*'],
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers\Api\Admin\MailCampaign\Concerns;
|
||||
|
||||
use App\Jobs\Mail\SendMailCampaignRecipient;
|
||||
use App\Models\Mail;
|
||||
use App\Models\MailCampaign;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
trait HandlesCampaignBulkActions
|
||||
{
|
||||
public function bulkAction(Request $request): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'action' => 'required|in:cancel,retry-failed',
|
||||
'uuids' => 'required|array|min:1|max:200',
|
||||
'uuids.*' => 'string',
|
||||
]);
|
||||
|
||||
$campaigns = MailCampaign::whereIn('uuid', $data['uuids'])->get();
|
||||
|
||||
$message = $data['action'] === 'cancel'
|
||||
? $this->cancelCampaigns($campaigns)
|
||||
: $this->retryFailedRecipients($campaigns);
|
||||
|
||||
return response()->json(['error' => false, 'message' => $message]);
|
||||
}
|
||||
|
||||
public function bulkDelete(Request $request): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'uuids' => 'required|array|min:1|max:200',
|
||||
'uuids.*' => 'string',
|
||||
]);
|
||||
|
||||
$deleted = MailCampaign::whereIn('uuid', $data['uuids'])
|
||||
->whereIn('status', MailCampaign::FINISHED_STATUSES)
|
||||
->delete();
|
||||
$skipped = count($data['uuids']) - $deleted;
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => "Đã xoá {$deleted} chiến dịch."
|
||||
. ($skipped > 0 ? " Bỏ qua {$skipped} chiến dịch đang gửi (huỷ gửi trước khi xoá)." : ''),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection<int, MailCampaign> $campaigns
|
||||
*/
|
||||
private function cancelCampaigns(Collection $campaigns): string
|
||||
{
|
||||
$cancelled = MailCampaign::whereKey($campaigns->modelKeys())
|
||||
->whereIn('status', MailCampaign::ACTIVE_STATUSES)
|
||||
->update(['status' => MailCampaign::STATUS_CANCELLED, 'completed_at' => now()]);
|
||||
|
||||
return "Đã huỷ gửi {$cancelled} chiến dịch. Email đã gửi trước đó không bị thu hồi.";
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection<int, MailCampaign> $campaigns
|
||||
*/
|
||||
private function retryFailedRecipients(Collection $campaigns): string
|
||||
{
|
||||
$retried = 0;
|
||||
|
||||
foreach ($campaigns->whereIn('status', ['completed', 'failed']) as $campaign) {
|
||||
DB::transaction(function () use ($campaign, &$retried) {
|
||||
$failedLogs = Mail::where('campaign_id', $campaign->id)
|
||||
->where('status', array_search('failed', Mail::STATUS_MAP, true))
|
||||
->lockForUpdate()
|
||||
->get(['id', 'to', 'customer_id']);
|
||||
if ($failedLogs->isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Mail::whereKey($failedLogs->modelKeys())->delete();
|
||||
$campaign->forceFill([
|
||||
'failed_count' => max(0, $campaign->failed_count - $failedLogs->count()),
|
||||
'status' => 'sending',
|
||||
'completed_at' => null,
|
||||
])->save();
|
||||
|
||||
foreach ($failedLogs as $log) {
|
||||
SendMailCampaignRecipient::dispatch($campaign->id, $log->customer_id, $log->to)->afterCommit();
|
||||
}
|
||||
$retried += $failedLogs->count();
|
||||
});
|
||||
}
|
||||
|
||||
return $retried > 0
|
||||
? "Đã đưa {$retried} email lỗi vào hàng đợi gửi lại."
|
||||
: 'Không có email lỗi nào để gửi lại (chỉ gửi lại chiến dịch đã kết thúc).';
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers\Api\Admin\MailCampaign;
|
||||
|
||||
use App\Http\Controllers\Api\Admin\MailCampaign\Concerns\HandlesCampaignBulkActions;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Jobs\Mail\SendMailCampaignRecipient;
|
||||
use App\Models\Customer;
|
||||
@@ -19,6 +20,8 @@ use Illuminate\Validation\ValidationException;
|
||||
|
||||
final class MailCampaignController extends Controller
|
||||
{
|
||||
use HandlesCampaignBulkActions;
|
||||
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$limit = min((int) $request->get('per_page', 20), 200);
|
||||
|
||||
@@ -38,6 +38,7 @@ class OrderController extends Controller
|
||||
'invoice',
|
||||
'orderItems.os',
|
||||
...OrderItemServiceResource::relationsFor('orderItems'),
|
||||
'tags',
|
||||
],
|
||||
[],
|
||||
['*'],
|
||||
|
||||
@@ -27,10 +27,14 @@ class ServiceController extends Controller
|
||||
{
|
||||
$perPage = min((int) ($request->per_page ?? 15), 100);
|
||||
|
||||
$query = Service::with(['customer:id,uuid,fullname,email', 'product:id,uuid,name', 'invoice:id,invoice_number', 'vmware:id,name,module_ip_address', 'orderItem:id,order_id', 'orderItem.order:id,order_num'])
|
||||
$query = Service::with(['customer:id,uuid,fullname,email', 'product:id,uuid,name,product_type', 'invoice:id,invoice_number', 'vmware:id,name,module_ip_address', 'orderItem:id,order_id', 'orderItem.order:id,order_num', 'tags'])
|
||||
->marked($request->all())
|
||||
->where(fn($q) => $q->whereNull('order_item_id')->orWhereNull('type')->orWhere('type', '!=', 'domain'))
|
||||
->when($request->filter, fn($q, $v) => $q->where('server_name', 'like', "%{$v}%"))
|
||||
->when($request->status, fn($q, $v) => $q->where('status', $v))
|
||||
->when($request->product_type, fn($q, $v) => $q->where(fn($typed) => $typed
|
||||
->where('type', $v)
|
||||
->orWhere(fn($legacy) => $legacy->whereNull('type')->whereHas('product', fn($product) => $product->where('product_type', $v)))))
|
||||
->when($request->customer_id, fn($q, $v) => $q->where('customer_id', $v))
|
||||
->when($request->boolean('unassigned'), fn($q) => $q->whereNull('customer_id'));
|
||||
|
||||
|
||||
@@ -9,11 +9,13 @@ use App\Http\Facade\SignRSA;
|
||||
use App\Http\Services\Telegram;
|
||||
use App\Models\Setting;
|
||||
use App\Support\PublicSetting;
|
||||
use App\Support\AutoEmailGate;
|
||||
use App\Support\MailSettings;
|
||||
use App\Support\Turnstile;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
@@ -189,6 +191,42 @@ class SettingController extends Controller
|
||||
return response()->json(['error' => false, 'message' => 'Đã gửi tin nhắn test thành công.']);
|
||||
}
|
||||
|
||||
public function testSmtp(Request $request): JsonResponse
|
||||
{
|
||||
$data = $request->validate(['email' => 'required|email|max:255']);
|
||||
|
||||
if (!AutoEmailGate::mailEnabled()) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => 'Hệ thống đang tắt gửi email (Mẫu email → Cài đặt). Bật lại rồi gửi test.',
|
||||
], 422);
|
||||
}
|
||||
|
||||
MailSettings::refresh(app('mail.manager'));
|
||||
$mailer = MailSettings::transport() ?? config('mail.default');
|
||||
|
||||
try {
|
||||
Mail::mailer($mailer)->html(
|
||||
'<p>Đây là email test từ Portal Admin.</p>'
|
||||
. '<p>Nếu bạn nhận được email này, cấu hình gửi email (' . e($mailer) . ') đang hoạt động.</p>'
|
||||
. '<p>Gửi lúc ' . now()->format('H:i:s d/m/Y') . '.</p>',
|
||||
fn ($message) => $message->to($data['email'])->subject('[Portal] Email test cấu hình SMTP'),
|
||||
);
|
||||
} catch (\Throwable $e) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => 'Gửi email test thất bại: ' . $e->getMessage(),
|
||||
], 422);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => $mailer === 'log'
|
||||
? 'Driver đang là Log: email được ghi vào log, không gửi thật.'
|
||||
: "Đã gửi email test tới {$data['email']}.",
|
||||
]);
|
||||
}
|
||||
|
||||
public function uploadMedia(Request $request): JsonResponse
|
||||
{
|
||||
$request->validate([
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers\Api\Admin\Tag;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Support\Markables;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class MarkController extends Controller
|
||||
{
|
||||
public function update(Request $request, string $key): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'is_starred' => 'sometimes|boolean',
|
||||
'tag_ids' => 'sometimes|array',
|
||||
'tag_ids.*' => 'integer|distinct|exists:tags,id',
|
||||
]);
|
||||
|
||||
$model = Markables::find($request->route('type'), $key);
|
||||
|
||||
DB::transaction(function () use ($model, $data) {
|
||||
if (array_key_exists('is_starred', $data)) {
|
||||
$model->timestamps = false;
|
||||
$model->forceFill(['is_starred' => $data['is_starred']])->save();
|
||||
}
|
||||
if (array_key_exists('tag_ids', $data)) {
|
||||
$model->tags()->sync($data['tag_ids']);
|
||||
}
|
||||
});
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'data' => [
|
||||
'is_starred' => (bool) $model->is_starred,
|
||||
'tags' => $model->tags()->get(),
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers\Api\Admin\Tag;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Tag;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class TagController extends Controller
|
||||
{
|
||||
public function index(): JsonResponse
|
||||
{
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'data' => Tag::withCount(['services', 'customers', 'orders', 'invoices'])->orderBy('title')->get(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function store(Request $request): JsonResponse
|
||||
{
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => 'Đã tạo tag.',
|
||||
'data' => Tag::create($this->validated($request)),
|
||||
], 201);
|
||||
}
|
||||
|
||||
public function update(Request $request, int $id): JsonResponse
|
||||
{
|
||||
$tag = Tag::findOrFail($id);
|
||||
$tag->update($this->validated($request, $tag->id));
|
||||
|
||||
return response()->json(['error' => false, 'message' => 'Đã cập nhật tag.', 'data' => $tag]);
|
||||
}
|
||||
|
||||
public function destroy(int $id): JsonResponse
|
||||
{
|
||||
Tag::findOrFail($id)->delete();
|
||||
|
||||
return response()->json(['error' => false, 'message' => 'Đã xoá tag.']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{title: string, color: string}
|
||||
*/
|
||||
private function validated(Request $request, ?int $ignoreId = null): array
|
||||
{
|
||||
return $request->validate([
|
||||
'title' => ['required', 'string', 'max:50', Rule::unique('tags', 'title')->ignore($ignoreId)],
|
||||
'color' => ['required', 'string', 'regex:' . Tag::COLOR_PATTERN],
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user