Files
boursecu_server/app/Http/Controllers/Chat/SupportChatNotifyController.php
T

120 lines
3.7 KiB
PHP
Raw Normal View History

2026-08-04 18:59:38 +03:30
<?php
namespace App\Http\Controllers\Chat;
use App\Http\Controllers\Controller;
use App\Models\Customer;
use App\Models\User;
use App\Services\ChatWebhookService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
class SupportChatNotifyController extends Controller
{
public const FOCUS_CACHE_PREFIX = 'admin_support_focus:';
public const FOCUS_TTL_SECONDS = 45;
/**
* Webhook از ah_chat وقتی اولین پیام خوانده‌نشدهٔ مشتری در اتاق SUPPORT ذخیره شود.
*/
public function notify(Request $request)
{
$secret = (string) self::Config('chat_webhook_secret');
$incoming = (string) $request->header('X-Chat-Webhook-Secret', '');
if ($secret === '' || ! hash_equals($secret, $incoming)) {
return response()->json(['message' => 'unauthorized'], 401);
}
if (! self::isSmsNotifEnabled('sms_notif_support_admin')) {
return response()->json(['ok' => true, 'skipped' => 'disabled']);
}
$roomId = trim((string) $request->input('room_id', ''));
if ($roomId === '') {
return response()->json(['message' => 'room_id required'], 422);
}
if (Cache::has(self::FOCUS_CACHE_PREFIX.$roomId)) {
return response()->json(['ok' => true, 'skipped' => 'admin_focused']);
}
$customer = Customer::query()
->where('chat_room_id', $roomId)
->first();
$name = $customer
? trim((string) ($customer->name ?: $customer->username ?: 'مشتری'))
: 'مشتری';
if ($name === '') {
$name = 'مشتری';
}
$message = 'شما از '.$name.' پیام دارید';
try {
User::where('send_sms', 1)->get()->each(function ($admin) use ($message) {
if (! empty($admin->mobile)) {
$this->send($admin->mobile, $message);
}
});
} catch (\Throwable $e) {
Log::warning('support chat SMS notify failed', [
'room_id' => $roomId,
'error' => $e->getMessage(),
]);
}
return response()->json(['ok' => true]);
}
/**
* Heartbeat تمرکز ادمین روی یک اتاق پشتیبانی (یا ترک تمرکز).
*/
public function focus(Request $request)
{
$roomId = trim((string) $request->input('room_id', ''));
$previousRoomId = trim((string) $request->input('previous_room_id', ''));
if ($previousRoomId !== '' && $previousRoomId !== $roomId) {
Cache::forget(self::FOCUS_CACHE_PREFIX.$previousRoomId);
}
if ($roomId === '') {
return response()->json(['ok' => true]);
}
Cache::put(self::FOCUS_CACHE_PREFIX.$roomId, true, self::FOCUS_TTL_SECONDS);
return response()->json(['ok' => true, 'room_id' => $roomId]);
}
/**
* ترک تمرکز روی یک اتاق مشخص.
*/
public function unfocus(Request $request)
{
$roomId = trim((string) $request->input('room_id', ''));
if ($roomId !== '') {
Cache::forget(self::FOCUS_CACHE_PREFIX.$roomId);
}
return response()->json(['ok' => true]);
}
/**
* ثبت/تازه‌سازی وب‌هوک روی ah_chat (از پنل ادمین).
*/
public function ensureWebhook()
{
$ok = app(ChatWebhookService::class)->ensureRegistered();
return response()->json([
'ok' => $ok,
'message' => $ok ? 'webhook registered' : 'webhook register skipped or failed',
], $ok ? 200 : 422);
}
}