UserNotification.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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_CHAT_MESSAGE = 'chat_message';
  26. public const EVENT_NAMES = [
  27. self::EVENT_CREATED => 'Создание',
  28. self::EVENT_STATUS_CHANGED => 'Смена статуса',
  29. self::EVENT_SCHEDULE_ADDED => 'Добавлено в график',
  30. self::EVENT_CHAT_MESSAGE => 'Сообщение в чате',
  31. ];
  32. public const DEFAULT_SORT_BY = 'created_at';
  33. public const DEFAULT_ORDER_BY = 'desc';
  34. protected $fillable = [
  35. 'user_id',
  36. 'type',
  37. 'event',
  38. 'title',
  39. 'message',
  40. 'message_html',
  41. 'data',
  42. 'read_at',
  43. ];
  44. protected function casts(): array
  45. {
  46. return [
  47. 'data' => 'array',
  48. 'read_at' => 'datetime',
  49. ];
  50. }
  51. public function user(): BelongsTo
  52. {
  53. return $this->belongsTo(User::class);
  54. }
  55. public function isRead(): bool
  56. {
  57. return $this->read_at !== null;
  58. }
  59. public function getTypeNameAttribute(): string
  60. {
  61. return self::TYPE_NAMES[$this->type] ?? $this->type;
  62. }
  63. public function getEventNameAttribute(): string
  64. {
  65. return self::EVENT_NAMES[$this->event] ?? $this->event;
  66. }
  67. }