UserNotification.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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_PRODUCTION_ORDER = 'production_order';
  13. public const TYPE_NAMES = [
  14. self::TYPE_PLATFORM => 'Площадки',
  15. self::TYPE_RECLAMATION => 'Рекламации',
  16. self::TYPE_SCHEDULE => 'График монтажей',
  17. self::TYPE_PRODUCTION_ORDER => 'График заказов',
  18. ];
  19. public const TYPE_COLORS = [
  20. self::TYPE_PLATFORM => 'primary',
  21. self::TYPE_RECLAMATION => 'success',
  22. self::TYPE_SCHEDULE => 'warning',
  23. self::TYPE_PRODUCTION_ORDER => 'info',
  24. ];
  25. public const EVENT_CREATED = 'created';
  26. public const EVENT_STATUS_CHANGED = 'status_changed';
  27. public const EVENT_SCHEDULE_ADDED = 'schedule_added';
  28. public const EVENT_CHAT_MESSAGE = 'chat_message';
  29. public const EVENT_NAMES = [
  30. self::EVENT_CREATED => 'Создание',
  31. self::EVENT_STATUS_CHANGED => 'Смена статуса',
  32. self::EVENT_SCHEDULE_ADDED => 'Добавлено в график',
  33. self::EVENT_CHAT_MESSAGE => 'Сообщение в чате',
  34. 'delivery_added' => 'Добавлена доставка',
  35. 'installation_added' => 'Добавлен монтаж',
  36. 'reclamation_added' => 'Добавлена рекламация',
  37. ];
  38. public const DEFAULT_SORT_BY = 'created_at';
  39. public const DEFAULT_ORDER_BY = 'desc';
  40. protected $fillable = [
  41. 'user_id',
  42. 'type',
  43. 'event',
  44. 'title',
  45. 'message',
  46. 'message_html',
  47. 'data',
  48. 'read_at',
  49. ];
  50. protected function casts(): array
  51. {
  52. return [
  53. 'data' => 'array',
  54. 'read_at' => 'datetime',
  55. ];
  56. }
  57. public function user(): BelongsTo
  58. {
  59. return $this->belongsTo(User::class);
  60. }
  61. public function isRead(): bool
  62. {
  63. return $this->read_at !== null;
  64. }
  65. public function getTypeNameAttribute(): string
  66. {
  67. return self::TYPE_NAMES[$this->type] ?? $this->type;
  68. }
  69. public function getEventNameAttribute(): string
  70. {
  71. return self::EVENT_NAMES[$this->event] ?? $this->event;
  72. }
  73. }