Files
L-Ami-Fiduciaire/app/Models/Message.php
Saad Ibn-Ezzoubayr fd43a6f429 feat: complete Epic 0 — foundation migration & infrastructure setup
Stories 0.2-0.5: rename folders→declarations (backend+frontend), configure
Redis for cache/queue/sessions, add foundation database migrations
(permissions, archived_at), replace DeclarationStatus enum with architecture
lifecycle values, create DeclarationObserver for status transition validation
and auto-archive, fix controller status transitions to respect observer rules.

93 tests pass (240 assertions).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-12 18:25:32 +00:00

86 lines
1.9 KiB
PHP

<?php
namespace App\Models;
use App\Enums\ActorType;
use App\Enums\MessageType;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Message extends Model
{
/**
* The attributes that are mass assignable.
*
* @var list<string>
*/
protected $fillable = [
'declaration_id',
'type',
'body',
'sent_by_type',
'sent_by_id',
'metadata',
];
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'type' => MessageType::class,
'sent_by_type' => ActorType::class,
'metadata' => 'array',
];
}
/**
* Get the declaration that owns the message.
*
* @return BelongsTo<Declaration, $this>
*/
public function declaration(): BelongsTo
{
return $this->belongsTo(Declaration::class);
}
/**
* Get the user who sent the message (when sent_by_type is user).
*
* @return BelongsTo<User, $this>
*/
public function senderUser(): BelongsTo
{
return $this->belongsTo(User::class, 'sent_by_id');
}
/**
* Get the client who sent the message (when sent_by_type is client).
*
* @return BelongsTo<Client, $this>
*/
public function senderClient(): BelongsTo
{
return $this->belongsTo(Client::class, 'sent_by_id');
}
/**
* Get the sender display name.
*/
public function getSenderNameAttribute(): string
{
if ($this->sent_by_type?->is(ActorType::User)) {
return $this->senderUser?->name ?? '—';
}
if ($this->sent_by_type?->is(ActorType::Client)) {
return $this->senderClient?->company_name ?? 'Client';
}
return '—';
}
}