feat(automations): add run-now test endpoint and sends log table
Enable immediate automation test runs without waiting for the scheduled hour, expose send history for debugging, and return all SuperAdmin overlay keys on refresh instead of only enable_automation. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -5,6 +5,7 @@ namespace App\Http\Controllers\Admin;
|
||||
use App\DB\Te\TableEngine;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Automation;
|
||||
use App\Models\AutomationSend;
|
||||
use App\Services\Automation\AutomationCatalog;
|
||||
use App\Services\Automation\AutomationEngine;
|
||||
use App\Services\Automation\AutomationFingerprint;
|
||||
@@ -89,6 +90,49 @@ class AutomationController extends Controller
|
||||
return response()->json(true);
|
||||
}
|
||||
|
||||
/** اجرای فوری برای تست — بدون انتظار ساعت cron */
|
||||
public function runNow(Request $request, $id)
|
||||
{
|
||||
$this->assertFeatureEnabled();
|
||||
$row = Automation::query()->findOrFail($id);
|
||||
$force = $request->boolean('force', true);
|
||||
$stats = app(AutomationEngine::class)->runNow($row, $force);
|
||||
|
||||
return response()->json([
|
||||
'message' => sprintf(
|
||||
'مخاطب %d — ارسال موفق %d، ناموفق %d، ردشده %d',
|
||||
$stats['audience'],
|
||||
$stats['sent'],
|
||||
$stats['failed'],
|
||||
$stats['skipped']
|
||||
),
|
||||
...$stats,
|
||||
]);
|
||||
}
|
||||
|
||||
public function sendsTable(Request $request, $id)
|
||||
{
|
||||
$this->assertFeatureEnabled();
|
||||
Automation::query()->findOrFail($id);
|
||||
|
||||
$query = AutomationSend::query()
|
||||
->where('automation_id', (int) $id)
|
||||
->leftJoin('customers', 'customers.id', '=', 'automation_sends.customer_id')
|
||||
->select([
|
||||
'automation_sends.*',
|
||||
'customers.name as customer_name',
|
||||
'customers.mobile as customer_mobile',
|
||||
])
|
||||
->orderByDesc('automation_sends.id');
|
||||
|
||||
TableEngine::create($query, $request->all())
|
||||
->initSearch(['customers.name', 'customers.mobile', 'automation_sends.status', 'automation_sends.error'])
|
||||
->select(['automation_sends.*', 'customers.name as customer_name', 'customers.mobile as customer_mobile'])
|
||||
->export($query);
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
private function assertFeatureEnabled(): void
|
||||
{
|
||||
if (! app(ConfigService::class)->isAutomationEnabled()) {
|
||||
|
||||
@@ -80,7 +80,7 @@ class SettingsController extends Controller
|
||||
|
||||
/**
|
||||
* پاک کردن کش overlay و خواندن دوباره از سوپرادمین
|
||||
* (سوییچهایی مثل enable_automation بعد از تغییر در پنل سوپرادمین).
|
||||
* (همهٔ کلیدهای can_edit=0 مالک سوپرادمین — دسترسی مشتری به قابلیتها).
|
||||
*/
|
||||
public function refreshSuperAdminOverlay()
|
||||
{
|
||||
@@ -91,14 +91,17 @@ class SettingsController extends Controller
|
||||
'message' => 'ارتباط با سوپرادمین برقرار نشد؛ کش پاک شد و در درخواست بعدی دوباره تلاش میشود',
|
||||
'ok' => false,
|
||||
'count' => 0,
|
||||
'items' => [],
|
||||
], 502);
|
||||
}
|
||||
|
||||
$count = (int) ($result['count'] ?? 0);
|
||||
|
||||
return response()->json([
|
||||
'message' => 'تنظیمات پیشرفته از سوپرادمین همگام شد',
|
||||
'message' => "همگام شد ({$count} کلید دسترسی از سوپرادمین)",
|
||||
'ok' => true,
|
||||
'count' => (int) ($result['count'] ?? 0),
|
||||
'enable_automation' => $result['enable_automation'] ?? null,
|
||||
'count' => $count,
|
||||
'items' => $result['items'] ?? [],
|
||||
'source' => $result['source'] ?? 'superadmin',
|
||||
]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user