121 lines
3.6 KiB
PHP
121 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs\Packages;
|
|
|
|
use App\Jobs\Concerns\LogsTenantNotifications;
|
|
use App\Models\EventPackage;
|
|
use App\Notifications\Packages\EventPackagePhotoThresholdNotification;
|
|
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 SendEventPackagePhotoThresholdWarning implements ShouldQueue
|
|
{
|
|
use Dispatchable;
|
|
use InteractsWithQueue;
|
|
use LogsTenantNotifications;
|
|
use Queueable;
|
|
use SerializesModels;
|
|
|
|
public function __construct(
|
|
public int $eventPackageId,
|
|
public float $threshold,
|
|
public int $limit,
|
|
public int $used,
|
|
) {}
|
|
|
|
public function handle(): void
|
|
{
|
|
$eventPackage = EventPackage::with(['event', 'package', 'event.tenant'])->find($this->eventPackageId);
|
|
|
|
if (! $eventPackage) {
|
|
Log::warning('Package threshold job skipped; event package missing', [
|
|
'event_package_id' => $this->eventPackageId,
|
|
]);
|
|
|
|
return;
|
|
}
|
|
|
|
$tenant = $eventPackage->event?->tenant;
|
|
if (! $tenant) {
|
|
return;
|
|
}
|
|
|
|
$context = $this->context($eventPackage);
|
|
|
|
if ($this->isDuplicateNotification($tenant, 'photo_threshold', $context, ['event_package_id', 'threshold', 'limit'])) {
|
|
$this->logNotification($tenant, [
|
|
'type' => 'photo_threshold',
|
|
'status' => 'skipped',
|
|
'context' => array_merge($context, ['reason' => 'duplicate']),
|
|
]);
|
|
|
|
return;
|
|
}
|
|
|
|
$preferences = app(\App\Services\Packages\TenantNotificationPreferences::class);
|
|
if (! $preferences->shouldNotify($tenant, 'photo_thresholds')) {
|
|
$this->logNotification($tenant, [
|
|
'type' => 'photo_threshold',
|
|
'status' => 'skipped',
|
|
'context' => $context,
|
|
]);
|
|
|
|
return;
|
|
}
|
|
|
|
$emails = collect([
|
|
$tenant->contact_email,
|
|
$tenant->user?->email,
|
|
])->filter(fn ($email) => is_string($email) && filter_var($email, FILTER_VALIDATE_EMAIL))
|
|
->unique();
|
|
|
|
if ($emails->isEmpty()) {
|
|
Log::info('Package threshold notification skipped due to missing recipients', [
|
|
'event_package_id' => $eventPackage->id,
|
|
'threshold' => $this->threshold,
|
|
]);
|
|
|
|
$this->logNotification($tenant, [
|
|
'type' => 'photo_threshold',
|
|
'status' => 'skipped',
|
|
'context' => array_merge($context, ['reason' => 'no_recipient']),
|
|
]);
|
|
|
|
return;
|
|
}
|
|
|
|
$this->dispatchToRecipients(
|
|
$tenant,
|
|
$emails,
|
|
'photo_threshold',
|
|
function (string $email) use ($eventPackage) {
|
|
Notification::route('mail', $email)->notify(
|
|
new EventPackagePhotoThresholdNotification(
|
|
$eventPackage,
|
|
$this->threshold,
|
|
$this->limit,
|
|
$this->used,
|
|
)
|
|
);
|
|
},
|
|
$context
|
|
);
|
|
}
|
|
|
|
private function context(EventPackage $eventPackage): array
|
|
{
|
|
return [
|
|
'event_package_id' => $eventPackage->id,
|
|
'event_id' => $eventPackage->event_id,
|
|
'threshold' => $this->threshold,
|
|
'limit' => $this->limit,
|
|
'used' => $this->used,
|
|
];
|
|
}
|
|
}
|