Admin subscriptions: batch mark expired with safety guards
This commit is contained in:
@@ -384,6 +384,43 @@ class SiteSubscriptionController extends Controller
|
||||
return redirect()->back()->with('success', '订阅状态已更新:' . ($this->statusLabels()[$subscription->status] ?? $subscription->status));
|
||||
}
|
||||
|
||||
public function batchMarkExpired(Request $request): \Illuminate\Http\RedirectResponse
|
||||
{
|
||||
$this->ensurePlatformAdmin($request);
|
||||
|
||||
// 仅支持在“已过期(expiry=expired)集合”上执行,避免误把正常订阅批量标记为已过期。
|
||||
$filters = [
|
||||
'status' => trim((string) $request->input('status', '')),
|
||||
'keyword' => trim((string) $request->input('keyword', '')),
|
||||
'merchant_id' => trim((string) $request->input('merchant_id', '')),
|
||||
'plan_id' => trim((string) $request->input('plan_id', '')),
|
||||
'expiry' => trim((string) $request->input('expiry', '')),
|
||||
];
|
||||
|
||||
if ((string) ($filters['expiry'] ?? '') !== 'expired') {
|
||||
return redirect()->back()->with('warning', '为避免误操作:批量标记已过期仅允许在「已过期(expiry=expired)」集合视图下执行。');
|
||||
}
|
||||
|
||||
// 防误操作:需要二次确认
|
||||
if ((string) $request->input('confirm', '') !== 'YES') {
|
||||
return redirect()->back()->with('warning', '为避免误操作,请在确认框输入 YES 后再批量标记已过期。');
|
||||
}
|
||||
|
||||
$query = $this->applyFilters(SiteSubscription::query(), $filters);
|
||||
|
||||
// 再加一道硬条件:ends_at 必须 < now(与 expiry=expired 一致)
|
||||
$query->whereNotNull('ends_at')->where('ends_at', '<', now());
|
||||
|
||||
// 仅把“非已过期”的订阅更新为 expired
|
||||
$affected = (clone $query)
|
||||
->where('status', '!=', 'expired')
|
||||
->update([
|
||||
'status' => 'expired',
|
||||
]);
|
||||
|
||||
return redirect()->back()->with('success', '已批量标记已过期:' . (int) $affected . ' 条。');
|
||||
}
|
||||
|
||||
protected function applyFilters(Builder $query, array $filters): Builder
|
||||
{
|
||||
return $query
|
||||
|
||||
@@ -237,6 +237,12 @@
|
||||
|
||||
<div class="card mb-20">
|
||||
<h3>工具</h3>
|
||||
|
||||
@php
|
||||
$batchMarkExpiredEnabled = (string) ($filters['expiry'] ?? '') === 'expired';
|
||||
$batchMarkExpiredReason = $batchMarkExpiredEnabled ? '' : '请先进入「已过期(expiry=expired)」集合后再执行批量标记。';
|
||||
@endphp
|
||||
|
||||
<div class="grid-2">
|
||||
<form method="get" action="/admin/site-subscriptions/export" class="actions gap-10">
|
||||
<input type="hidden" name="download" value="1">
|
||||
@@ -264,6 +270,28 @@
|
||||
@if(!($isExpiryView ?? false) && $attachOrderId <= 0)
|
||||
<a class="btn btn-sm" href="{!! $createOrderFromSubIndexUrl !!}">续费下单(先选订阅)</a>
|
||||
@endif
|
||||
|
||||
<form method="post" action="/admin/site-subscriptions/batch-mark-expired" data-action="disable-on-submit" onsubmit="return confirm('确认将当前筛选集合内的订阅批量标记为已过期?该操作会更新 status 字段。');" class="actions gap-10">
|
||||
@csrf
|
||||
<input type="hidden" name="status" value="{{ $filters['status'] ?? '' }}">
|
||||
<input type="hidden" name="merchant_id" value="{{ $filters['merchant_id'] ?? '' }}">
|
||||
<input type="hidden" name="plan_id" value="{{ $filters['plan_id'] ?? '' }}">
|
||||
<input type="hidden" name="expiry" value="{{ $filters['expiry'] ?? '' }}">
|
||||
<input type="hidden" name="keyword" value="{{ $filters['keyword'] ?? '' }}">
|
||||
|
||||
<label class="muted form-inline-row">
|
||||
<span>确认输入</span>
|
||||
<input type="text" name="confirm" placeholder="YES" class="w-140" @disabled(! $batchMarkExpiredEnabled)>
|
||||
<span>(必须输入 YES 才会执行)</span>
|
||||
</label>
|
||||
|
||||
<div>
|
||||
<button class="btn btn-danger btn-sm" type="submit" @disabled(! $batchMarkExpiredEnabled) title="{{ $batchMarkExpiredReason }}">批量标记已过期(当前集合)</button>
|
||||
@if(! $batchMarkExpiredEnabled)
|
||||
<div class="adm-tool-blocked-hint">提示:{{ $batchMarkExpiredReason }}</div>
|
||||
@endif
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
<div class="muted muted-xs mt-6">
|
||||
@if($attachOrderId > 0)
|
||||
|
||||
@@ -138,6 +138,7 @@ Route::prefix('admin')->group(function () {
|
||||
Route::get('/site-subscriptions/export', [SiteSubscriptionController::class, 'export']);
|
||||
Route::get('/site-subscriptions/{subscription}', [SiteSubscriptionController::class, 'show']);
|
||||
Route::post('/site-subscriptions/{subscription}/set-status', [SiteSubscriptionController::class, 'setStatus']);
|
||||
Route::post('/site-subscriptions/batch-mark-expired', [SiteSubscriptionController::class, 'batchMarkExpired']);
|
||||
|
||||
Route::get('/plans', [PlanController::class, 'index']);
|
||||
Route::get('/plans/export', [PlanController::class, 'export']);
|
||||
|
||||
@@ -54,7 +54,8 @@ class AdminSiteSubscriptionSyncFailedReasonLongDoesNotRenderKeywordLinkTest exte
|
||||
'activated_at' => now()->subDay(),
|
||||
]);
|
||||
|
||||
$longReason = str_repeat('错', 120);
|
||||
// 超过 config('saasshop.platform_orders.sync_error_keyword_link_max_len', 200)
|
||||
$longReason = str_repeat('错', 220);
|
||||
|
||||
PlatformOrder::query()->create([
|
||||
'merchant_id' => $merchant->id,
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\Merchant;
|
||||
use App\Models\Plan;
|
||||
use App\Models\SiteSubscription;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class AdminSiteSubscriptionsBatchMarkExpiredShouldBlockWhenNotExpiredScopeTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
protected function loginAsPlatformAdmin(): void
|
||||
{
|
||||
$this->seed();
|
||||
|
||||
$this->post('/admin/login', [
|
||||
'email' => 'platform.admin@demo.local',
|
||||
'password' => 'Platform@123456',
|
||||
])->assertRedirect('/admin');
|
||||
}
|
||||
|
||||
public function test_batch_mark_expired_should_block_when_expiry_not_expired(): void
|
||||
{
|
||||
$this->loginAsPlatformAdmin();
|
||||
|
||||
$merchant = Merchant::query()->firstOrFail();
|
||||
$plan = Plan::query()->create([
|
||||
'code' => 'sub_batch_mark_expired_block_scope_plan',
|
||||
'name' => '订阅批量标记过期阻断(scope)测试套餐',
|
||||
'billing_cycle' => 'monthly',
|
||||
'price' => 1,
|
||||
'list_price' => 1,
|
||||
'status' => 'active',
|
||||
'sort' => 10,
|
||||
'published_at' => now(),
|
||||
]);
|
||||
|
||||
$sub = SiteSubscription::query()->create([
|
||||
'merchant_id' => $merchant->id,
|
||||
'plan_id' => $plan->id,
|
||||
'status' => 'activated',
|
||||
'source' => 'manual',
|
||||
'subscription_no' => 'SUB_BATCH_EXPIRED_BLOCK_SCOPE_0001',
|
||||
'plan_name' => $plan->name,
|
||||
'billing_cycle' => $plan->billing_cycle,
|
||||
'period_months' => 1,
|
||||
'amount' => 1,
|
||||
'starts_at' => now()->subDay(),
|
||||
'ends_at' => now()->addDays(5),
|
||||
'activated_at' => now()->subDay(),
|
||||
]);
|
||||
|
||||
$this->post('/admin/site-subscriptions/batch-mark-expired', [
|
||||
'expiry' => 'expiring_7d',
|
||||
'confirm' => 'YES',
|
||||
])->assertRedirect()->assertSessionHas('warning');
|
||||
|
||||
$sub->refresh();
|
||||
$this->assertSame('activated', (string) $sub->status);
|
||||
}
|
||||
|
||||
public function test_batch_mark_expired_should_block_when_confirm_missing(): void
|
||||
{
|
||||
$this->loginAsPlatformAdmin();
|
||||
|
||||
$merchant = Merchant::query()->firstOrFail();
|
||||
$plan = Plan::query()->create([
|
||||
'code' => 'sub_batch_mark_expired_block_confirm_plan',
|
||||
'name' => '订阅批量标记过期阻断(confirm)测试套餐',
|
||||
'billing_cycle' => 'monthly',
|
||||
'price' => 1,
|
||||
'list_price' => 1,
|
||||
'status' => 'active',
|
||||
'sort' => 10,
|
||||
'published_at' => now(),
|
||||
]);
|
||||
|
||||
$sub = SiteSubscription::query()->create([
|
||||
'merchant_id' => $merchant->id,
|
||||
'plan_id' => $plan->id,
|
||||
'status' => 'activated',
|
||||
'source' => 'manual',
|
||||
'subscription_no' => 'SUB_BATCH_EXPIRED_BLOCK_CONFIRM_0001',
|
||||
'plan_name' => $plan->name,
|
||||
'billing_cycle' => $plan->billing_cycle,
|
||||
'period_months' => 1,
|
||||
'amount' => 1,
|
||||
'starts_at' => now()->subDay(),
|
||||
'ends_at' => now()->subDays(1),
|
||||
'activated_at' => now()->subDay(),
|
||||
]);
|
||||
|
||||
$this->post('/admin/site-subscriptions/batch-mark-expired', [
|
||||
'expiry' => 'expired',
|
||||
'confirm' => '',
|
||||
])->assertRedirect()->assertSessionHas('warning');
|
||||
|
||||
$sub->refresh();
|
||||
$this->assertSame('activated', (string) $sub->status);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\Merchant;
|
||||
use App\Models\Plan;
|
||||
use App\Models\SiteSubscription;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class AdminSiteSubscriptionsBatchMarkExpiredShouldMarkExpiredOnlyWhenEndsAtPastTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
protected function loginAsPlatformAdmin(): void
|
||||
{
|
||||
$this->seed();
|
||||
|
||||
$this->post('/admin/login', [
|
||||
'email' => 'platform.admin@demo.local',
|
||||
'password' => 'Platform@123456',
|
||||
])->assertRedirect('/admin');
|
||||
}
|
||||
|
||||
public function test_batch_mark_expired_should_only_update_subscriptions_with_ends_at_past(): void
|
||||
{
|
||||
$this->loginAsPlatformAdmin();
|
||||
|
||||
$merchant = Merchant::query()->firstOrFail();
|
||||
$plan = Plan::query()->create([
|
||||
'code' => 'sub_batch_mark_expired_success_plan',
|
||||
'name' => '订阅批量标记过期(成功)测试套餐',
|
||||
'billing_cycle' => 'monthly',
|
||||
'price' => 1,
|
||||
'list_price' => 1,
|
||||
'status' => 'active',
|
||||
'sort' => 10,
|
||||
'published_at' => now(),
|
||||
]);
|
||||
|
||||
$expiredA = SiteSubscription::query()->create([
|
||||
'merchant_id' => $merchant->id,
|
||||
'plan_id' => $plan->id,
|
||||
'status' => 'activated',
|
||||
'source' => 'manual',
|
||||
'subscription_no' => 'SUB_BATCH_EXPIRED_OK_0001',
|
||||
'plan_name' => $plan->name,
|
||||
'billing_cycle' => $plan->billing_cycle,
|
||||
'period_months' => 1,
|
||||
'amount' => 1,
|
||||
'starts_at' => now()->subDays(40),
|
||||
'ends_at' => now()->subDays(1),
|
||||
'activated_at' => now()->subDays(40),
|
||||
]);
|
||||
|
||||
$expiredAlready = SiteSubscription::query()->create([
|
||||
'merchant_id' => $merchant->id,
|
||||
'plan_id' => $plan->id,
|
||||
'status' => 'expired',
|
||||
'source' => 'manual',
|
||||
'subscription_no' => 'SUB_BATCH_EXPIRED_ALREADY_0001',
|
||||
'plan_name' => $plan->name,
|
||||
'billing_cycle' => $plan->billing_cycle,
|
||||
'period_months' => 1,
|
||||
'amount' => 1,
|
||||
'starts_at' => now()->subDays(70),
|
||||
'ends_at' => now()->subDays(10),
|
||||
'activated_at' => now()->subDays(70),
|
||||
]);
|
||||
|
||||
$notExpired = SiteSubscription::query()->create([
|
||||
'merchant_id' => $merchant->id,
|
||||
'plan_id' => $plan->id,
|
||||
'status' => 'activated',
|
||||
'source' => 'manual',
|
||||
'subscription_no' => 'SUB_BATCH_EXPIRED_SKIP_0001',
|
||||
'plan_name' => $plan->name,
|
||||
'billing_cycle' => $plan->billing_cycle,
|
||||
'period_months' => 1,
|
||||
'amount' => 1,
|
||||
'starts_at' => now()->subDays(5),
|
||||
'ends_at' => now()->addDays(5),
|
||||
'activated_at' => now()->subDays(5),
|
||||
]);
|
||||
|
||||
$this->post('/admin/site-subscriptions/batch-mark-expired', [
|
||||
'expiry' => 'expired',
|
||||
'confirm' => 'YES',
|
||||
])->assertRedirect()->assertSessionHas('success');
|
||||
|
||||
$expiredA->refresh();
|
||||
$expiredAlready->refresh();
|
||||
$notExpired->refresh();
|
||||
|
||||
$this->assertSame('expired', (string) $expiredA->status);
|
||||
$this->assertSame('expired', (string) $expiredAlready->status);
|
||||
$this->assertSame('activated', (string) $notExpired->status);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user