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:
@@ -9,6 +9,7 @@ use App\Models\Product;
|
||||
use App\Services\ConfigService;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
|
||||
|
||||
class AutomationEngine
|
||||
{
|
||||
@@ -87,6 +88,53 @@ class AutomationEngine
|
||||
return $this->conditions->apply($this->conditions->baseQuery(), $conditions)->count();
|
||||
}
|
||||
|
||||
/**
|
||||
* اجرای فوری برای تست ادمین — بدون محدودیت ساعت، بدون touch کردن last_sms_run_at.
|
||||
*
|
||||
* @return array{audience: int, sent: int, failed: int, skipped: int}
|
||||
*/
|
||||
public function runNow(Automation $auto, bool $force = true): array
|
||||
{
|
||||
if (! app(ConfigService::class)->isAutomationEnabled()) {
|
||||
throw new UnprocessableEntityHttpException('قابلیت اتوماسیون خاموش است.');
|
||||
}
|
||||
if (! $auto->is_active) {
|
||||
throw new UnprocessableEntityHttpException('اتوماسیون غیرفعال است.');
|
||||
}
|
||||
if (! $this->inWindow($auto)) {
|
||||
throw new UnprocessableEntityHttpException('اتوماسیون خارج از بازهٔ شروع/پایان است.');
|
||||
}
|
||||
|
||||
$stats = ['audience' => 0, 'sent' => 0, 'failed' => 0, 'skipped' => 0];
|
||||
$audienceQuery = $this->conditions->apply(
|
||||
$this->conditions->baseQuery(),
|
||||
$auto->conditions ?? []
|
||||
);
|
||||
$stats['audience'] = (int) $audienceQuery->count();
|
||||
|
||||
foreach ($this->channels($auto) as $channel) {
|
||||
$this->conditions
|
||||
->apply($this->conditions->baseQuery(), $auto->conditions ?? [])
|
||||
->select(['id', 'name', 'fname', 'lname', 'mobile', 'type', 'is_archive'])
|
||||
->chunkById(80, function ($customers) use ($auto, $channel, $force, &$stats) {
|
||||
foreach ($customers as $customer) {
|
||||
if (! $force && ! $this->sender->maySend($auto, $customer, $channel)) {
|
||||
$stats['skipped']++;
|
||||
|
||||
continue;
|
||||
}
|
||||
if ($this->sender->send($auto, $customer, $channel, $force)) {
|
||||
$stats['sent']++;
|
||||
} else {
|
||||
$stats['failed']++;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return $stats;
|
||||
}
|
||||
|
||||
private function tryPriceFire(Automation $auto, array $payload): void
|
||||
{
|
||||
if (! $this->inWindow($auto)) {
|
||||
|
||||
Reference in New Issue
Block a user