Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
66.67% covered (warning)
66.67%
10 / 15
44.44% covered (danger)
44.44%
4 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
NotificationMessage
66.67% covered (warning)
66.67%
10 / 15
44.44% covered (danger)
44.44%
4 / 9
12.00
0.00% covered (danger)
0.00%
0 / 1
 casts
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 tenant
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 store
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 createdBy
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 recipients
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 attachments
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 scopeForStore
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getReadRecipientsCountAttribute
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getUnreadRecipientsCountAttribute
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace App\Models;
4
5use App\Enums\NotificationAudience;
6use App\Enums\NotificationStatus;
7use App\Enums\NotificationType;
8use Illuminate\Database\Eloquent\Builder;
9use Illuminate\Database\Eloquent\Factories\HasFactory;
10use Illuminate\Database\Eloquent\Model;
11use Illuminate\Database\Eloquent\Relations\BelongsTo;
12use Illuminate\Database\Eloquent\Relations\HasMany;
13
14class NotificationMessage extends Model
15{
16    use HasFactory;
17
18    protected $fillable = [
19        'tenant_id', 'store_id', 'created_by_user_id', 'type', 'audience', 'title', 'body',
20        'status', 'sent_at', 'metadata',
21    ];
22
23    protected function casts(): array
24    {
25        return [
26            'type' => NotificationType::class,
27            'audience' => NotificationAudience::class,
28            'status' => NotificationStatus::class,
29            'sent_at' => 'datetime',
30            'metadata' => 'array',
31        ];
32    }
33
34    public function tenant(): BelongsTo { return $this->belongsTo(Tenant::class); }
35    public function store(): BelongsTo { return $this->belongsTo(Store::class); }
36    public function createdBy(): BelongsTo { return $this->belongsTo(User::class, 'created_by_user_id'); }
37    public function recipients(): HasMany { return $this->hasMany(NotificationRecipient::class); }
38    public function attachments(): HasMany { return $this->hasMany(NotificationAttachment::class); }
39
40    public function scopeForStore(Builder $query, Store $store): Builder
41    {
42        return $query->where('tenant_id', $store->tenant_id)->where('store_id', $store->id);
43    }
44
45    public function getReadRecipientsCountAttribute(): int
46    {
47        return (int) ($this->recipients_read_count ?? $this->recipients()->whereNotNull('read_at')->count());
48    }
49
50    public function getUnreadRecipientsCountAttribute(): int
51    {
52        return (int) ($this->recipients_unread_count ?? $this->recipients()->whereNull('read_at')->count());
53    }
54}