| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Models; |
| 4 |
|
| 5 |
use FluentSupport\App\Services\Notifications\NotificationSettings; |
| 6 |
use FluentSupport\App\Services\Helper; |
| 7 |
|
| 8 |
class Notification extends Model |
| 9 |
{ |
| 10 |
protected $table = 'fs_notifications'; |
| 11 |
|
| 12 |
protected $fillable = [ |
| 13 |
'actor_id', |
| 14 |
'ticket_id', |
| 15 |
'conversation_id', |
| 16 |
'event_type', |
| 17 |
'category', |
| 18 |
'payload' |
| 19 |
]; |
| 20 |
|
| 21 |
public static function boot() |
| 22 |
{ |
| 23 |
parent::boot(); |
| 24 |
|
| 25 |
static::creating(function ($model) { |
| 26 |
$model->created_at = current_time('mysql'); |
| 27 |
$model->updated_at = current_time('mysql'); |
| 28 |
}); |
| 29 |
|
| 30 |
static::updating(function ($model) { |
| 31 |
$model->updated_at = current_time('mysql'); |
| 32 |
}); |
| 33 |
|
| 34 |
static::deleting(function ($model) { |
| 35 |
NotificationUser::where('notification_id', $model->id)->delete(); |
| 36 |
}); |
| 37 |
} |
| 38 |
|
| 39 |
public function setPayloadAttribute($payload) |
| 40 |
{ |
| 41 |
$this->attributes['payload'] = maybe_serialize($payload); |
| 42 |
} |
| 43 |
|
| 44 |
public function getPayloadAttribute($payload) |
| 45 |
{ |
| 46 |
return Helper::safeUnserialize($payload); |
| 47 |
} |
| 48 |
|
| 49 |
public function recipients() |
| 50 |
{ |
| 51 |
$class = __NAMESPACE__ . '\NotificationUser'; |
| 52 |
|
| 53 |
return $this->hasMany($class, 'notification_id', 'id'); |
| 54 |
} |
| 55 |
|
| 56 |
public function ticket() |
| 57 |
{ |
| 58 |
$class = __NAMESPACE__ . '\Ticket'; |
| 59 |
|
| 60 |
return $this->belongsTo($class, 'ticket_id', 'id'); |
| 61 |
} |
| 62 |
|
| 63 |
public function conversation() |
| 64 |
{ |
| 65 |
$class = __NAMESPACE__ . '\Conversation'; |
| 66 |
|
| 67 |
return $this->belongsTo($class, 'conversation_id', 'id'); |
| 68 |
} |
| 69 |
|
| 70 |
public function actor() |
| 71 |
{ |
| 72 |
$class = __NAMESPACE__ . '\Person'; |
| 73 |
|
| 74 |
return $this->belongsTo($class, 'actor_id', 'id'); |
| 75 |
} |
| 76 |
|
| 77 |
public static function deleteByTicketId($ticketId) |
| 78 |
{ |
| 79 |
if (!(new NotificationSettings())->notificationTablesExist()) { |
| 80 |
return; |
| 81 |
} |
| 82 |
|
| 83 |
$ticketId = (int) $ticketId; |
| 84 |
|
| 85 |
if (!$ticketId) { |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
(new static())->getConnection()->transaction(function () use ($ticketId) { |
| 90 |
do { |
| 91 |
$notificationIds = static::where('ticket_id', $ticketId) |
| 92 |
->orderBy('id', 'ASC') |
| 93 |
->limit(100) |
| 94 |
->pluck('id') |
| 95 |
->toArray(); |
| 96 |
|
| 97 |
if ($notificationIds) { |
| 98 |
NotificationUser::whereIn('notification_id', $notificationIds)->delete(); |
| 99 |
static::whereIn('id', $notificationIds)->delete(); |
| 100 |
} |
| 101 |
} while ($notificationIds); |
| 102 |
}); |
| 103 |
} |
| 104 |
|
| 105 |
public function scopeByCategory($query, $category) |
| 106 |
{ |
| 107 |
if ($category && $category !== 'all') { |
| 108 |
$query->where('category', $category); |
| 109 |
} |
| 110 |
|
| 111 |
return $query; |
| 112 |
} |
| 113 |
} |
| 114 |
|