- Evolution API v2 integration with full HTTP client - WhatsApp instance management (Create, Connect, Delete, LogOut, Restart) - Real-time QR Code display with Alpine.js countdown timer - Pairing code support for WhatsApp Web linking - Webhook endpoint for receiving Evolution API events - Complete instance settings (reject calls, always online, read messages, etc.) - Filament v4 Resource with modal QR Code after instance creation - Table actions for Connect, View, and Edit - Status badges with Filament's native components - Full translations support (English and Portuguese) - Native Filament multi-tenancy support - DTOs with Spatie Laravel Data for type safety - Laravel Events for extensibility - Background job processing for webhooks and messages - Comprehensive configuration file
96 lines
2.2 KiB
PHP
96 lines
2.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace WallaceMartinss\FilamentEvolution\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use WallaceMartinss\FilamentEvolution\Enums\WebhookEventEnum;
|
|
use WallaceMartinss\FilamentEvolution\Models\Concerns\HasTenant;
|
|
|
|
class WhatsappWebhook extends Model
|
|
{
|
|
use HasFactory;
|
|
use HasTenant;
|
|
|
|
protected $table = 'whatsapp_webhooks';
|
|
|
|
protected $fillable = [
|
|
'instance_id',
|
|
'event',
|
|
'payload',
|
|
'processed',
|
|
'error',
|
|
'processing_time_ms',
|
|
];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'event' => WebhookEventEnum::class,
|
|
'payload' => 'array',
|
|
'processed' => 'boolean',
|
|
'processing_time_ms' => 'integer',
|
|
];
|
|
}
|
|
|
|
public function instance(): BelongsTo
|
|
{
|
|
return $this->belongsTo(WhatsappInstance::class, 'instance_id');
|
|
}
|
|
|
|
public function isProcessed(): bool
|
|
{
|
|
return $this->processed;
|
|
}
|
|
|
|
public function hasError(): bool
|
|
{
|
|
return ! empty($this->error);
|
|
}
|
|
|
|
public function markAsProcessed(int $processingTimeMs = null): void
|
|
{
|
|
$this->update([
|
|
'processed' => true,
|
|
'processing_time_ms' => $processingTimeMs,
|
|
]);
|
|
}
|
|
|
|
public function markAsFailed(string $error, int $processingTimeMs = null): void
|
|
{
|
|
$this->update([
|
|
'processed' => false,
|
|
'error' => $error,
|
|
'processing_time_ms' => $processingTimeMs,
|
|
]);
|
|
}
|
|
|
|
public function getEventLabel(): string
|
|
{
|
|
return $this->event?->getLabel() ?? $this->getRawOriginal('event') ?? 'Unknown';
|
|
}
|
|
|
|
public function scopePending($query)
|
|
{
|
|
return $query->where('processed', false)->whereNull('error');
|
|
}
|
|
|
|
public function scopeFailed($query)
|
|
{
|
|
return $query->where('processed', false)->whereNotNull('error');
|
|
}
|
|
|
|
public function scopeProcessed($query)
|
|
{
|
|
return $query->where('processed', true);
|
|
}
|
|
|
|
public function scopeByEvent($query, WebhookEventEnum $event)
|
|
{
|
|
return $query->where('event', $event->value);
|
|
}
|
|
}
|