netgescon-day0/app/Models/CommunicationMessage.php

76 lines
1.7 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class CommunicationMessage extends Model
{
use HasFactory;
protected $fillable = [
'channel',
'direction',
'protocol_number',
'external_chat_id',
'external_message_id',
'phone_number',
'target_extension',
'assigned_user_id',
'stabile_id',
'sender_name',
'message_text',
'attachments',
'ticket_id',
'post_it_id',
'insurance_claim_id',
'status',
'received_at',
'metadata',
];
protected $casts = [
'attachments' => 'array',
'received_at' => 'datetime',
'metadata' => 'array',
];
protected static function booted(): void
{
static::creating(function (self $message): void {
if (! empty($message->protocol_number)) {
return;
}
$stamp = now()->format('YmdHis');
$rand = strtoupper(bin2hex(random_bytes(2)));
$message->protocol_number = "COM-{$stamp}-{$rand}";
});
}
public function ticket()
{
return $this->belongsTo(Ticket::class, 'ticket_id');
}
public function assignedUser()
{
return $this->belongsTo(User::class, 'assigned_user_id');
}
public function stabile()
{
return $this->belongsTo(Stabile::class, 'stabile_id');
}
public function postIt()
{
return $this->belongsTo(ChiamataPostIt::class, 'post_it_id');
}
public function insuranceClaim()
{
return $this->belongsTo(InsuranceClaim::class, 'insurance_claim_id');
}
}