Files
boursecu_server/app/Console/Commands/PriceChannelTest.php
T

93 lines
4.1 KiB
PHP
Raw Normal View History

2026-07-07 16:14:11 +03:30
<?php
namespace App\Console\Commands;
use App\Services\PriceChannelBroadcastService;
use Illuminate\Console\Command;
class PriceChannelTest extends Command
{
protected $signature = 'price:channel-test {--send : ارسال واقعی به کانال‌ها (نادیده‌گرفتن cooldown)}';
protected $description = 'بررسی وضعیت ارسال قیمت به کانال‌ها و ارسال آزمایشی';
public function handle(PriceChannelBroadcastService $service): int
{
$info = $service->diagnose();
$this->line('');
$this->info('=== وضعیت ارسال قیمت به کانال ===');
$this->line('قابلیت روشن (price_channel_enabled): ' . ($info['enabled'] ? 'بله' : 'خیر'));
$this->line('پیام‌رسان‌های انتخاب‌شده: ' . (implode(', ', $info['selected_messengers']) ?: '— (خالی)'));
$this->line('حداقل فاصله ارسال (ثانیه): ' . $info['min_seconds']);
$this->line('فاصله cooldown گذشته؟ ' . ($info['cooldown_elapsed'] ? 'بله' : 'خیر (هنوز زوده)'));
2026-07-08 13:15:29 +03:30
$this->line('منبع قالب: ' . ($info['template_source'] === 'db' ? 'تنظیمات ادمین' : 'پیش‌فرض داخلی'));
2026-07-07 16:14:11 +03:30
$this->line('تعداد محصولات واجد شرایط: ' . $info['eligible_products']);
$this->line('مقصدهای فعال (آماده ارسال): ' . (implode(', ', $info['active_targets']) ?: '— (هیچ)'));
$this->line('');
$this->info('--- جزئیات هر پیام‌رسان ---');
$this->table(
['پیام‌رسان', 'انتخاب‌شده', 'توکن', 'مقصدها', 'ارسال می‌شود؟'],
collect($info['messengers'])->map(fn ($m, $key) => [
$key,
$m['selected'] ? '✓' : '✗',
$m['has_token'] ? '✓' : '✗ (خالی)',
implode(', ', $m['destinations']) ?: '—',
$m['will_send'] ? '✓' : '✗',
])->values()->all()
);
$this->line('');
$this->info('--- پیش‌نمایش پیام ---');
$this->line($info['message_preview'] !== '' ? $info['message_preview'] : '(پیام خالی — هیچ محصول واجد شرایطی نیست)');
if ($this->diagnoseBlocker($info)) {
return self::SUCCESS;
}
if ($this->option('send')) {
$this->line('');
$this->info('=== ارسال آزمایشی (بدون cooldown) ===');
$result = $service->forceSend();
$this->line('ارسال شد؟ ' . ($result['sent'] ? 'بله' : 'خیر'));
foreach ($result['results'] as $messenger => $chatResults) {
$this->line("[{$messenger}]");
foreach ((array) $chatResults as $chatId => $status) {
$this->line(" {$chatId} → {$status}");
}
}
} else {
$this->line('');
$this->comment('برای ارسال واقعی: php artisan price:channel-test --send');
}
return self::SUCCESS;
}
/** پیام راهنما وقتی چیزی مانع ارسال است */
private function diagnoseBlocker(array $info): bool
{
$blocked = false;
if (! $info['enabled']) {
$this->warn('⚠ price_channel_enabled خاموش است — روشنش کن.');
$blocked = true;
}
if ($info['selected_messengers'] === []) {
$this->warn('⚠ هیچ پیام‌رسانی در price_channel_messengers انتخاب نشده.');
$blocked = true;
}
if ($info['active_targets'] === []) {
$this->warn('⚠ هیچ مقصد فعالی نیست — توکن بات و «مقصد ارسال» (price_channel_*_to) را پر کن.');
$blocked = true;
}
if ($info['eligible_products'] === 0) {
$this->warn('⚠ هیچ محصول واجد شرایطی نیست (active + قیمت>0 + open_buy/sell + داخل ساعت کاری).');
$blocked = true;
}
return $blocked;
}
}