migrant-web/app/Notifications/DocumentExpiryNotification.php

107 lines
3.5 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 DocumentExpiryNotification extends Notification implements ShouldQueue
{
use Queueable;
public $documentType;
public $expiryDate;
public $daysRemaining;
public $reminderLevel;
/**
* Create a new notification instance.
*/
public function __construct($documentType, $expiryDate, $daysRemaining, $reminderLevel)
{
$this->documentType = $documentType;
$this->expiryDate = $expiryDate;
$this->daysRemaining = $daysRemaining;
$this->reminderLevel = $reminderLevel;
}
/**
* 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("Document Expiry Alert: {$this->documentType}")
->greeting("Hello {$name},")
->line("Your {$this->documentType} is expiring in {$this->daysRemaining} days on {$this->expiryDate}.")
->line("Please renew your document as soon as possible to avoid service disruption.")
->action('Update Profile', url('/profile'))
->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' => "Document Expiry Warning",
'body' => "Your {$this->documentType} will expire in {$this->daysRemaining} days on {$this->expiryDate}.",
'document_type' => $this->documentType,
'expiry_date' => $this->expiryDate,
'days_remaining' => $this->daysRemaining,
'reminder_level' => $this->reminderLevel,
'type' => 'document_expiry',
];
}
/**
* Get the push notification representation.
*/
public function toFcm($notifiable): array
{
return [
'token' => $notifiable->fcm_token,
'title' => "Document Expiry Alert",
'body' => "Your {$this->documentType} will expire in {$this->daysRemaining} days.",
'data' => [
'type' => 'document_expiry',
'document_type' => $this->documentType,
'days_remaining' => $this->daysRemaining,
],
];
}
}