up
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire\Whatsapp;
|
||||
|
||||
use App\Models\WhatsappConversation;
|
||||
use App\Models\WhatsappUser;
|
||||
use Livewire\Component;
|
||||
use Livewire\WithPagination;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use App\Models\WhatsappSystemConfig;
|
||||
|
||||
class ShowConversaciones extends Component
|
||||
{
|
||||
use WithPagination;
|
||||
|
||||
public ?int $selectedUserId = null;
|
||||
public string $search = '';
|
||||
public string $replyText = '';
|
||||
public ?WhatsappUser $selectedUser = null;
|
||||
|
||||
protected $listeners = ['refreshChatList' => '$refresh'];
|
||||
|
||||
public function selectUser(int $userId): void
|
||||
{
|
||||
$this->selectedUserId = $userId;
|
||||
$this->selectedUser = WhatsappUser::find($userId);
|
||||
$this->replyText = '';
|
||||
|
||||
// Marcar mensajes como leídos
|
||||
WhatsappConversation::where('user_id', $userId)
|
||||
->where('direction', 'incoming')
|
||||
->where('is_read', 0)
|
||||
->update(['is_read' => 1]);
|
||||
}
|
||||
|
||||
public function sendReply(): void
|
||||
{
|
||||
$this->validate(['replyText' => 'required|string|max:4096']);
|
||||
|
||||
if (! $this->selectedUser) {
|
||||
return;
|
||||
}
|
||||
|
||||
$token = WhatsappSystemConfig::get('whatsapp_token');
|
||||
$phoneNumberId = WhatsappSystemConfig::get('phone_number_id');
|
||||
$apiUrl = WhatsappSystemConfig::get('whatsapp_api_url', 'https://graph.facebook.com/v22.0/');
|
||||
|
||||
$response = Http::withToken($token)
|
||||
->post("{$apiUrl}{$phoneNumberId}/messages", [
|
||||
'messaging_product' => 'whatsapp',
|
||||
'to' => $this->selectedUser->phone_number,
|
||||
'type' => 'text',
|
||||
'text' => ['body' => $this->replyText],
|
||||
]);
|
||||
|
||||
if ($response->successful()) {
|
||||
WhatsappConversation::create([
|
||||
'user_id' => $this->selectedUserId,
|
||||
'message_id' => $response->json('messages.0.id'),
|
||||
'direction' => 'outgoing',
|
||||
'message_type' => 'text',
|
||||
'content' => $this->replyText,
|
||||
'status' => 'sent',
|
||||
]);
|
||||
|
||||
$this->replyText = '';
|
||||
$this->dispatch('scrollToBottom');
|
||||
} else {
|
||||
session()->flash('error', 'Error al enviar el mensaje: ' . $response->body());
|
||||
}
|
||||
}
|
||||
|
||||
public function toggleBlockUser(): void
|
||||
{
|
||||
if (! $this->selectedUser) {
|
||||
return;
|
||||
}
|
||||
|
||||
$newStatus = $this->selectedUser->status === 'blocked' ? 'active' : 'blocked';
|
||||
$this->selectedUser->update(['status' => $newStatus]);
|
||||
$this->selectedUser->refresh();
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
$users = WhatsappUser::when($this->search, function ($q) {
|
||||
$q->where(function ($q2) {
|
||||
$q2->where('name', 'like', '%' . $this->search . '%')
|
||||
->orWhere('phone_number', 'like', '%' . $this->search . '%');
|
||||
});
|
||||
})
|
||||
->withCount(['conversations as unread_count' => function ($q) {
|
||||
$q->where('direction', 'incoming')->where('is_read', 0);
|
||||
}])
|
||||
->with('lastMessage')
|
||||
->orderByDesc(
|
||||
WhatsappConversation::select('created_at')
|
||||
->whereColumn('user_id', 'whatsapp_users.id')
|
||||
->orderByDesc('created_at')
|
||||
->limit(1)
|
||||
)
|
||||
->paginate(20);
|
||||
|
||||
$messages = $this->selectedUserId
|
||||
? WhatsappConversation::where('user_id', $this->selectedUserId)
|
||||
->orderBy('created_at')
|
||||
->get()
|
||||
: collect();
|
||||
|
||||
return view('livewire.whatsapp.show-conversaciones', compact('users', 'messages'));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user