migrant-web/app/Notifications/PendingConfirmationNotification.php

115 lines
3.6 KiB
PHP

<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use App\Notifications\Channels\FcmChannel;
class PendingConfirmationNotification extends Notification implements ShouldQueue
{
use Queueable;
public $actionType;
public $messageTitle;
public $messageBody;
public $daysRemaining;
public $reminderLevel;
public $entityType;
public $entityId;
/**
* Create a new notification instance.
*/
public function __construct($actionType, $messageTitle, $messageBody, $daysRemaining, $reminderLevel, $entityType, $entityId)
{
$this->actionType = $actionType;
$this->messageTitle = $messageTitle;
$this->messageBody = $messageBody;
$this->daysRemaining = $daysRemaining;
$this->reminderLevel = $reminderLevel;
$this->entityType = $entityType;
$this->entityId = $entityId;
}
/**
* Get the notification's delivery channels.
*/
public function via($notifiable): array
{
$channels = ['database'];
// Determine if email channel should be included
$emailEnabled = true;
if (method_exists($notifiable, 'employerProfile') && $notifiable->employerProfile) {
$emailEnabled = (bool) ($notifiable->employerProfile->email_notifications ?? true);
}
if ($emailEnabled && !empty($notifiable->email)) {
$channels[] = 'mail';
}
// Determine if push channel should be included
$pushEnabled = true;
if (method_exists($notifiable, 'employerProfile') && $notifiable->employerProfile) {
$pushEnabled = (bool) ($notifiable->employerProfile->push_notifications ?? true);
}
if ($pushEnabled && !empty($notifiable->fcm_token)) {
$channels[] = FcmChannel::class;
}
return $channels;
}
/**
* Get the mail representation of the notification.
*/
public function toMail($notifiable): MailMessage
{
$name = $notifiable->name ?? $notifiable->full_name ?? 'User';
return (new MailMessage)
->subject("Pending Action: {$this->messageTitle}")
->greeting("Hello {$name},")
->line($this->messageBody)
->line("This action is pending and needs your confirmation within {$this->daysRemaining} days.")
->action('View Actions', url('/dashboard'))
->line('Thank you for using our application!');
}
/**
* Get the array representation of the notification for in-app storage.
*/
public function toArray($notifiable): array
{
return [
'title' => $this->messageTitle,
'body' => $this->messageBody,
'action_type' => $this->actionType,
'days_remaining' => $this->daysRemaining,
'reminder_level' => $this->reminderLevel,
'entity_type' => $this->entityType,
'entity_id' => $this->entityId,
'type' => 'pending_confirmation',
];
}
/**
* Get the push notification representation.
*/
public function toFcm($notifiable): array
{
return [
'token' => $notifiable->fcm_token,
'title' => $this->messageTitle,
'body' => $this->messageBody,
'data' => [
'type' => 'pending_confirmation',
'action_type' => $this->actionType,
'entity_id' => $this->entityId,
'days_remaining' => $this->daysRemaining,
],
];
}
}