Implement package limit notification system
This commit is contained in:
66
app/Jobs/Packages/SendTenantPackageExpiringNotification.php
Normal file
66
app/Jobs/Packages/SendTenantPackageExpiringNotification.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Packages;
|
||||
|
||||
use App\Models\TenantPackage;
|
||||
use App\Notifications\Packages\TenantPackageExpiringNotification;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Notification;
|
||||
|
||||
class SendTenantPackageExpiringNotification implements ShouldQueue
|
||||
{
|
||||
use Dispatchable;
|
||||
use InteractsWithQueue;
|
||||
use Queueable;
|
||||
use SerializesModels;
|
||||
|
||||
public function __construct(
|
||||
public int $tenantPackageId,
|
||||
public int $daysRemaining,
|
||||
) {}
|
||||
|
||||
public function handle(): void
|
||||
{
|
||||
$tenantPackage = TenantPackage::with(['tenant', 'package'])->find($this->tenantPackageId);
|
||||
|
||||
if (! $tenantPackage || ! $tenantPackage->tenant) {
|
||||
Log::warning('Tenant package expiry warning skipped', [
|
||||
'tenant_package_id' => $this->tenantPackageId,
|
||||
]);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$preferences = app(\App\Services\Packages\TenantNotificationPreferences::class);
|
||||
if (! $preferences->shouldNotify($tenantPackage->tenant, 'package_expiring')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$emails = collect([
|
||||
$tenantPackage->tenant->contact_email,
|
||||
$tenantPackage->tenant->user?->email,
|
||||
])->filter(fn ($email) => is_string($email) && filter_var($email, FILTER_VALIDATE_EMAIL))
|
||||
->unique();
|
||||
|
||||
if ($emails->isEmpty()) {
|
||||
Log::info('Tenant package expiry warning skipped due to missing recipients', [
|
||||
'tenant_package_id' => $tenantPackage->id,
|
||||
'days_remaining' => $this->daysRemaining,
|
||||
]);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($emails as $email) {
|
||||
Notification::route('mail', $email)->notify(new TenantPackageExpiringNotification(
|
||||
$tenantPackage,
|
||||
$this->daysRemaining,
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user