UserNotification.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  6. class UserNotification extends Model
  7. {
  8. use HasFactory;
  9. public const TYPE_PLATFORM = 'platform';
  10. public const TYPE_RECLAMATION = 'reclamation';
  11. public const TYPE_SCHEDULE = 'schedule';
  12. public const TYPE_NAMES = [
  13. self::TYPE_PLATFORM => 'Площадки',
  14. self::TYPE_RECLAMATION => 'Рекламации',
  15. self::TYPE_SCHEDULE => 'График монтажей',
  16. ];
  17. public const TYPE_COLORS = [
  18. self::TYPE_PLATFORM => 'primary',
  19. self::TYPE_RECLAMATION => 'success',
  20. self::TYPE_SCHEDULE => 'warning',
  21. ];
  22. public const EVENT_CREATED = 'created';
  23. public const EVENT_STATUS_CHANGED = 'status_changed';
  24. public const EVENT_SCHEDULE_ADDED = 'schedule_added';
  25. public const EVENT_NAMES = [
  26. self::EVENT_CREATED => 'Создание',
  27. self::EVENT_STATUS_CHANGED => 'Смена статуса',
  28. self::EVENT_SCHEDULE_ADDED => 'Добавлено в график',
  29. ];
  30. public const DEFAULT_SORT_BY = 'created_at';
  31. public const DEFAULT_ORDER_BY = 'desc';
  32. protected $fillable = [
  33. 'user_id',
  34. 'type',
  35. 'event',
  36. 'title',
  37. 'message',
  38. 'message_html',
  39. 'data',
  40. 'read_at',
  41. ];
  42. protected function casts(): array
  43. {
  44. return [
  45. 'data' => 'array',
  46. 'read_at' => 'datetime',
  47. ];
  48. }
  49. public function user(): BelongsTo
  50. {
  51. return $this->belongsTo(User::class);
  52. }
  53. public function isRead(): bool
  54. {
  55. return $this->read_at !== null;
  56. }
  57. public function getTypeNameAttribute(): string
  58. {
  59. return self::TYPE_NAMES[$this->type] ?? $this->type;
  60. }
  61. public function getEventNameAttribute(): string
  62. {
  63. return self::EVENT_NAMES[$this->event] ?? $this->event;
  64. }
  65. }