Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 20 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| ChatConversation | |
0.00% |
0 / 20 |
|
0.00% |
0 / 5 |
132 | |
0.00% |
0 / 1 |
| messages | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getOrCreate | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| getAiHistory | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| getPlatformBadgeAttribute | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
20 | |||
| getStatusBadgeAttribute | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Models; |
| 4 | |
| 5 | use Illuminate\Database\Eloquent\Model; |
| 6 | |
| 7 | class ChatConversation extends Model |
| 8 | { |
| 9 | protected $fillable = ['platform', 'sender_id', 'sender_name', 'status', 'order_id']; |
| 10 | |
| 11 | public function messages() |
| 12 | { |
| 13 | return $this->hasMany(ChatMessage::class, 'conversation_id'); |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * Get or create an active conversation for this sender |
| 18 | */ |
| 19 | public static function getOrCreate(string $platform, string $senderId, string $senderName = 'Customer'): self |
| 20 | { |
| 21 | return static::firstOrCreate( |
| 22 | ['platform' => $platform, 'sender_id' => $senderId, 'status' => 'active'], |
| 23 | ['sender_name' => $senderName] |
| 24 | ); |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Get conversation history formatted for AI |
| 29 | */ |
| 30 | public function getAiHistory(): array |
| 31 | { |
| 32 | return $this->messages() |
| 33 | ->orderBy('created_at') |
| 34 | ->get() |
| 35 | ->map(fn ($m) => ['role' => $m->role, 'content' => $m->message]) |
| 36 | ->toArray(); |
| 37 | } |
| 38 | |
| 39 | public function getPlatformBadgeAttribute(): string |
| 40 | { |
| 41 | return match ($this->platform) { |
| 42 | 'facebook' => '<span class="badge" style="background:#1877f2;color:#fff">Facebook</span>', |
| 43 | 'whatsapp' => '<span class="badge" style="background:#25d366;color:#fff">WhatsApp</span>', |
| 44 | default => '<span class="badge bg-secondary">' . $this->platform . '</span>', |
| 45 | }; |
| 46 | } |
| 47 | |
| 48 | public function getStatusBadgeAttribute(): string |
| 49 | { |
| 50 | return match ($this->status) { |
| 51 | 'ordered' => '<span class="badge" style="background:#027a48;color:#fff">✅ অর্ডার হয়েছে</span>', |
| 52 | 'closed' => '<span class="badge bg-secondary">বন্ধ</span>', |
| 53 | default => '<span class="badge" style="background:#b54708;color:#fff">💬 Active</span>', |
| 54 | }; |
| 55 | } |
| 56 | } |