NotificationDeliveryLog.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Casts\Attribute;
  4. use Illuminate\Database\Eloquent\Factories\HasFactory;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  7. class NotificationDeliveryLog extends Model
  8. {
  9. use HasFactory;
  10. public const CHANNEL_IN_APP = 'in_app';
  11. public const CHANNEL_BROWSER = 'browser';
  12. public const CHANNEL_PUSH = 'push';
  13. public const CHANNEL_EMAIL = 'email';
  14. public const STATUS_SENT = 'sent';
  15. public const STATUS_FAILED = 'failed';
  16. public const STATUS_SKIPPED = 'skipped';
  17. public const STATUS_DEAD_LETTER = 'dead_letter';
  18. public const CHANNEL_LABELS = [
  19. self::CHANNEL_IN_APP => 'В приложении',
  20. self::CHANNEL_BROWSER => 'Браузер',
  21. self::CHANNEL_PUSH => 'Push',
  22. self::CHANNEL_EMAIL => 'Email',
  23. ];
  24. public const STATUS_LABELS = [
  25. self::STATUS_SENT => 'Отправлено',
  26. self::STATUS_FAILED => 'Ошибка',
  27. self::STATUS_SKIPPED => 'Пропущено',
  28. self::STATUS_DEAD_LETTER => 'Dead letter',
  29. ];
  30. public const DEFAULT_SORT_BY = 'created_at';
  31. public const DEFAULT_ORDER_BY = 'desc';
  32. protected $fillable = [
  33. 'user_notification_id',
  34. 'user_id',
  35. 'channel',
  36. 'status',
  37. 'attempt',
  38. 'message',
  39. 'error',
  40. ];
  41. protected function channel(): Attribute
  42. {
  43. return Attribute::make(
  44. get: fn($value) => self::CHANNEL_LABELS[$value] ?? $value,
  45. );
  46. }
  47. protected function status(): Attribute
  48. {
  49. return Attribute::make(
  50. get: fn($value) => self::STATUS_LABELS[$value] ?? $value,
  51. );
  52. }
  53. public function user(): BelongsTo
  54. {
  55. return $this->belongsTo(User::class);
  56. }
  57. public function userNotification(): BelongsTo
  58. {
  59. return $this->belongsTo(UserNotification::class);
  60. }
  61. }