Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
76.19% |
16 / 21 |
|
44.44% |
4 / 9 |
CRAP | |
0.00% |
0 / 1 |
| NotificationRecipient | |
76.19% |
16 / 21 |
|
44.44% |
4 / 9 |
11.35 | |
0.00% |
0 / 1 |
| casts | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| message | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| tenant | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| store | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| influencer | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| user | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| scopeUnread | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| scopeRead | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| markAsRead | |
87.50% |
7 / 8 |
|
0.00% |
0 / 1 |
2.01 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Models; |
| 4 | |
| 5 | use App\Enums\NotificationRecipientStatus; |
| 6 | use Illuminate\Database\Eloquent\Builder; |
| 7 | use Illuminate\Database\Eloquent\Factories\HasFactory; |
| 8 | use Illuminate\Database\Eloquent\Model; |
| 9 | use Illuminate\Database\Eloquent\Relations\BelongsTo; |
| 10 | use Illuminate\Http\Request; |
| 11 | |
| 12 | class NotificationRecipient extends Model |
| 13 | { |
| 14 | use HasFactory; |
| 15 | |
| 16 | protected $fillable = [ |
| 17 | 'notification_message_id', 'tenant_id', 'store_id', 'influencer_id', 'user_id', |
| 18 | 'status', 'received_at', 'read_at', 'read_ip', 'read_user_agent', 'metadata', |
| 19 | ]; |
| 20 | |
| 21 | protected function casts(): array |
| 22 | { |
| 23 | return [ |
| 24 | 'status' => NotificationRecipientStatus::class, |
| 25 | 'received_at' => 'datetime', |
| 26 | 'read_at' => 'datetime', |
| 27 | 'metadata' => 'array', |
| 28 | ]; |
| 29 | } |
| 30 | |
| 31 | public function message(): BelongsTo { return $this->belongsTo(NotificationMessage::class, 'notification_message_id'); } |
| 32 | public function tenant(): BelongsTo { return $this->belongsTo(Tenant::class); } |
| 33 | public function store(): BelongsTo { return $this->belongsTo(Store::class); } |
| 34 | public function influencer(): BelongsTo { return $this->belongsTo(Influencer::class); } |
| 35 | public function user(): BelongsTo { return $this->belongsTo(User::class); } |
| 36 | |
| 37 | public function scopeUnread(Builder $query): Builder |
| 38 | { |
| 39 | return $query->whereNull('read_at'); |
| 40 | } |
| 41 | |
| 42 | public function scopeRead(Builder $query): Builder |
| 43 | { |
| 44 | return $query->whereNotNull('read_at'); |
| 45 | } |
| 46 | |
| 47 | public function markAsRead(?Request $request = null): void |
| 48 | { |
| 49 | if ($this->read_at) { |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | $this->update([ |
| 54 | 'status' => NotificationRecipientStatus::Read, |
| 55 | 'read_at' => now(), |
| 56 | 'read_ip' => $request?->ip(), |
| 57 | 'read_user_agent' => $request?->userAgent(), |
| 58 | ]); |
| 59 | } |
| 60 | } |